Monsters opening doors

This commit is contained in:
2026-11-21 15:16:07 -03:00
parent 2563ec3f69
commit 94463b3cea
6 changed files with 164 additions and 71 deletions
+64 -5
View File
@@ -8,7 +8,8 @@ import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { copy_dir, test_game, test_mob } from "./helpers.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
import { copy_dir, errors_during, test_game, test_mob } from "./helpers.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
@@ -159,8 +160,12 @@ Deno.test("a player can't stop a coilhead through a wall", async () => {
assert(coilhead.z > -1.5, `it only got to z ${coilhead.z}`);
});
Deno.test("a mob's path goes through a closed factory door, and it waits there until the door opens", async () => {
const { t } = await setup();
// a wall along z 0 with a closed factory door at x 0, facing so it's across the way. open says whether both its halves
// are open, open_door opens them
function door_in_wall(
t: Awaited<ReturnType<typeof setup>>["t"],
state: (x: number, y: number, z: number, name: string) => number | undefined,
) {
const world = t.game.world;
const set_state = (x: number, y: number, z: number, name: string, value: number) =>
t.game.set_block_state(
@@ -169,7 +174,6 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
z,
set_state_value(world.get_block_value(x, y, z), world.get_block_info(x, y, z)!, name, value)! >>> 16,
);
// a wall along z 0 with a closed factory door at x 0, facing so it's across the way
for (let x = -3; x <= 3; x++) {
for (let y = 100; y < 103; y++) t.game.set_block(x, y, 0, FLOOR);
}
@@ -178,6 +182,15 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
set_state(0, y, 0, "facing", 0);
set_state(0, y, 0, "half", half);
}
return {
open: () => state(0, 100, 0, "open") === 1 && state(0, 101, 0, "open") === 1,
open_door: () => [100, 101].forEach((y) => set_state(0, y, 0, "open", 1)),
};
}
Deno.test("a mob's path goes through a closed factory door, and it waits there until the door opens", async () => {
const { t, state } = await setup();
const { open_door } = door_in_wall(t, state);
// thin enough to get past the door once it's open
const mob = test_mob(t.game, {}, 0.5, 100, -2.5);
@@ -186,9 +199,55 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
assertEquals(mob.navigation.blocked_by, { x: 0, y: 100, z: 0, id: FACTORY_DOOR });
assert(mob.z < 1);
for (const y of [100, 101]) set_state(0, y, 0, "open", 1);
open_door();
for (let i = 0; i < 200 && mob.navigation.moving; i++) t.game.tick();
assert(!mob.navigation.moving);
assert(mob.z > 1.5, `it only got to z ${mob.z}`);
assertEquals(mob.navigation.blocked_by, undefined);
});
Deno.test("a coilhead going after a player opens a closed door in its way after standing at it a while", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// alice looks away from the door, so nothing freezes it
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
let blocked_at: number | undefined;
let tick = 0;
for (; tick < 300 && !open(); tick++) {
t.game.tick();
if (blocked_at === undefined && coilhead.navigation.blocked_by) blocked_at = tick;
}
assert(open(), "it never opened the door");
assert(blocked_at !== undefined && tick - blocked_at >= 40, `it opened it at ${tick}, blocked from ${blocked_at}`);
});
Deno.test("a coilhead seen while it waits at a closed door stops waiting, and doesn't open it", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// a window 2 wide at head height beside the door, too low to get through
for (const x of [1, 2]) t.game.set_block(x, 101, 0, "cubi:air");
// alice beside the window, looking away
t.send(1, { type: "move", x: 3.5, y: 100, z: 1.5, yaw: -Math.PI / 2, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
for (let i = 0; i < 300 && !coilhead.navigation.blocked_by; i++) t.game.tick();
assert(coilhead.navigation.blocked_by, "it never got to the door");
for (let i = 0; i < 10; i++) t.game.tick();
// she turns to look at it through the window, before it's waited long enough to open the door
const errors = await errors_during(() => {
t.send(1, {
type: "move",
x: 3.5,
y: 100,
z: 1.5,
yaw: Math.atan2(3.5 - coilhead.x, 1.5 - coilhead.z),
pitch: 0,
});
for (let i = 0; i < 60; i++) t.game.tick();
});
assertEquals(errors, []);
assert(!open(), "it opened the door while it was seen");
assertEquals(coilhead.navigation.blocked_by, undefined);
});
+1 -14
View File
@@ -16,7 +16,7 @@ import { LocalPlayer } from "@/client/entity/local_player.ts";
import { Options } from "@/client/options.ts";
import type { Client } from "@/client/client.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, errors_during, test_game } from "./helpers.ts";
import { game_with_pool } from "./chunk_loading.ts";
const [MAJOR, MINOR] = GAME_VERSION.split(".");
@@ -61,19 +61,6 @@ function as_mod<T>(mod: string, fn: () => T, engine_access = true): Promise<T> {
return loading_mod(mod, engine_access, () => Promise.resolve(fn()));
}
// what console.error got while fn ran
async function errors_during(fn: () => unknown): Promise<string[]> {
const errors: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => errors.push(args.map(String).join(" "));
try {
await fn();
} finally {
console.error = original;
}
return errors;
}
Deno.test("a server mod hooks GameServer to drop twice, like the plan's example", async () => {
const dir = mods_dir((dir) =>
write_mod(
+13
View File
@@ -465,3 +465,16 @@ export function test_mob(game: GameServer, fields: Partial<EntityJson>, x: numbe
game.entities.add_entity(mob);
return mob;
}
// what console.error got while fn ran
export async function errors_during(fn: () => unknown): Promise<string[]> {
const errors: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => errors.push(args.map(String).join(" "));
try {
await fn();
} finally {
console.error = original;
}
return errors;
}