// 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); });