More pathing

This commit is contained in:
2026-11-21 14:21:02 -03:00
parent 0f58a9cbfe
commit 357f8f2bd4
8 changed files with 314 additions and 24 deletions
+37 -2
View File
@@ -6,9 +6,9 @@
// down. the death block, and the coilhead
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value } from "@/common/utils.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 } from "./helpers.ts";
import { copy_dir, test_game, test_mob } from "./helpers.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
@@ -17,6 +17,7 @@ const FLOOR = "dangerous_business:concrete_floor";
const DEATH = "dangerous_business:death";
const PIPE = "dangerous_business:double_steel_pipes";
const COILHEAD = "dangerous_business:coilhead";
const FACTORY_DOOR = "dangerous_business:factory_door";
// alice on a concrete floor at y 100, south of 0 100 0 and looking north, holding each of the blocks. it's a game of
// its own, which tests playing mods/ leave out
@@ -157,3 +158,37 @@ Deno.test("a player can't stop a coilhead through a wall", async () => {
// up against the wall, still on its way to her
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();
const world = t.game.world;
const set_state = (x: number, y: number, z: number, name: string, value: number) =>
t.game.set_block_state(
x,
y,
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);
}
for (const [y, half] of [[100, 0], [101, 1]]) {
t.game.set_block(0, y, 0, FACTORY_DOOR);
set_state(0, y, 0, "facing", 0);
set_state(0, y, 0, "half", half);
}
// thin enough to get past the door once it's open
const mob = test_mob(t.game, {}, 0.5, 100, -2.5);
assert(mob.navigation.move_to(0.5, 100, 2.5));
for (let i = 0; i < 60; i++) t.game.tick();
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);
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);
});
+20
View File
@@ -33,6 +33,9 @@ import type { Face, SharedWorld } from "@/common/mod_api/shared.ts";
import { shared as setup_shared } from "@/mods/bworld/scripts/shared.ts";
import { shared_context } from "@/common/shared_context.ts";
import { RecipeBook } from "@/common/mod_loader.ts";
import type { EntityJson } from "@/common/mod_data.ts";
import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
// the repo's own mods/, read once: reading and checking every mod's files is most of starting a game in a test. only
// mods/ is kept, since tests change the temporary folders they make between games
@@ -445,3 +448,20 @@ export function client_of(game: GameServer, area: Area) {
blocks: () => blocks_in(area, (x, y, z) => at(x, y, z)!),
};
}
// a mob made straight from entity json fields, with no goals, so it only does what a test makes it do. 0.6 wide and
// 1.8 tall unless fields say otherwise, with its feet at x y z
export function test_mob(game: GameServer, fields: Partial<EntityJson>, x: number, y: number, z: number): Mob {
const json: EntityJson = {
id: "test:mob",
texture: "cubi:missing",
behavior: "test:still",
width: 0.6,
height: 1.8,
...fields,
};
const mob = mod_entity_factory(json, new Map([["test:still", () => {}]]))(game.entities) as Mob;
mob.set_position(x, y, z);
game.entities.add_entity(mob);
return mob;
}
+2 -14
View File
@@ -15,8 +15,7 @@ import { IDENTITY } from "@/client/rendering/model_math.ts";
import { push_model } from "@/client/rendering/model_renderer.ts";
import { Mob } from "@/server/game/entity/mob.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { mod_entity_factory } from "@/server/game/entity/mod_entity.ts";
import { container_items, test_game } from "./helpers.ts";
import { container_items, test_game, test_mob } from "./helpers.ts";
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
const geo = JSON.parse(Deno.readTextFileSync("mods/bworld/models/chicken.geo.json"));
@@ -184,18 +183,7 @@ async function walker(fields: Partial<EntityJson>, wall = false) {
}
t.join(1, "alice");
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
const json: EntityJson = {
id: "test:walker",
texture: "bworld:chicken",
behavior: "test:still",
width: 0.6,
height: 1.8,
...fields,
};
const mob = mod_entity_factory(json, new Map([["test:still", () => {}]]))(t.game.entities) as Mob;
mob.set_position(0.5, 100, 0.5);
t.game.entities.add_entity(mob);
return { game: t.game, mob };
return { game: t.game, mob: test_mob(t.game, fields, 0.5, 100, 0.5) };
}
// the highest and lowest its feet got over ticks, walking to x
+60
View File
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// mobs following paths, see server/game/entity/navigation.ts
import { assert, assertEquals } from "@std/assert";
import { test_game, test_mob } from "./helpers.ts";
// two rooms on a stone floor at y 99, split by a wall at x 3 with a doorway 2 high at z 4. a goal-less mob in the
// west room at 0.5 100 0.5, and alice in its corner, so it thinks
async function rooms() {
const t = await test_game("mods");
for (let x = -6; x <= 10; x++) {
for (let z = -6; z <= 6; z++) {
t.game.set_block(x, 99, z, "bworld:stone");
for (let y = 100; y < 105; y++) t.game.set_block(x, y, z, "cubi:air");
if (x === 3) {
for (let y = 100; y < 103; y++) {
if (z !== 4 || y === 102) t.game.set_block(x, y, z, "bworld:stone");
}
}
}
}
t.join(1, "alice");
t.send(1, { type: "move", x: -5.5, y: 100, z: -5.5, yaw: 0, pitch: 0 });
return { game: t.game, mob: test_mob(t.game, {}, 0.5, 100, 0.5) };
}
function tick_while_moving(game: { tick(): void }, mob: { navigation: { moving: boolean } }, ticks: number) {
for (let i = 0; i < ticks && mob.navigation.moving; i++) game.tick();
}
Deno.test("a mob goes through a doorway to get to the next room", async () => {
const { game, mob } = await rooms();
assert(mob.navigation.move_to(6.5, 100, 0.5));
tick_while_moving(game, mob, 400);
assert(!mob.navigation.moving);
assert(Math.hypot(mob.x - 6.5, mob.z - 0.5) < 1, `it got to ${mob.x} ${mob.z}`);
});
Deno.test("a mob shut in has nowhere to go", async () => {
const { game, mob } = await rooms();
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
if (x !== 0 || z !== 0) { for (let y = 100; y < 103; y++) game.set_block(x, y, z, "bworld:stone"); }
}
}
assertEquals(mob.navigation.move_to(6.5, 100, 0.5), false);
assert(!mob.navigation.moving);
});
Deno.test("a mob whose way gets walled up searches again, and gives up when there's no other way", async () => {
const { game, mob } = await rooms();
assert(mob.navigation.move_to(6.5, 100, 0.5));
for (let i = 0; i < 5; i++) game.tick();
game.set_block(3, 100, 4, "bworld:stone");
game.set_block(3, 101, 4, "bworld:stone");
tick_while_moving(game, mob, 400);
assert(!mob.navigation.moving);
assert(mob.x < 3);
});