97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
// 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";
|
|
import { MeleeAttackGoal } from "@/server/game/entity/goals.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);
|
|
});
|
|
|
|
Deno.test("a monster finds its way around a wall to the player it's after", async () => {
|
|
const { game } = await rooms();
|
|
const alice = game.players()[0];
|
|
alice.set_position(8.5, 100, 0.5);
|
|
const monster = test_mob(game, { behavior: "monster" }, 0.5, 100, 0.5);
|
|
for (let i = 0; i < 400 && Math.hypot(monster.x - 8.5, monster.z - 0.5) > 2; i++) game.tick();
|
|
assert(Math.hypot(monster.x - 8.5, monster.z - 0.5) <= 2, `it got to ${monster.x} ${monster.z}`);
|
|
});
|
|
|
|
Deno.test("a script's mob walks its way, and stops when told", async () => {
|
|
const { game, mob } = await rooms();
|
|
const ref = game.mods.mob(mob);
|
|
assert(ref.move_to(6.5, 100, 0.5));
|
|
assert(ref.moving);
|
|
for (let i = 0; i < 20; i++) game.tick();
|
|
ref.stop();
|
|
assert(!ref.moving);
|
|
const { x, z } = ref.position;
|
|
for (let i = 0; i < 20; i++) game.tick();
|
|
assert(Math.hypot(ref.position.x - x, ref.position.z - z) < 0.5);
|
|
assertEquals(ref.blocked_by, undefined);
|
|
});
|
|
|
|
Deno.test("a mob going after its target stops when a script lets the target go partway through its thinking", async () => {
|
|
const { game, mob } = await rooms();
|
|
const melee = new MeleeAttackGoal(mob);
|
|
mob.target = game.players()[0];
|
|
assert(melee.can_use());
|
|
melee.start();
|
|
melee.tick();
|
|
mob.target = undefined;
|
|
melee.tick();
|
|
assert(!melee.can_continue_to_use());
|
|
});
|