// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the search mobs find their way with, see server/game/entity/pathfinding.ts, on small made up worlds import { assert, assertEquals } from "@std/assert"; import type { Shape } from "@/common/block_shapes.ts"; import { find_path, type PathBody, type PathOptions, type Point } from "@/server/game/entity/pathfinding.ts"; // a world that's empty except for what's put in it function world() { const blocks = new Map(); const water = new Set(); const through = new Set(); const key = (x: number, y: number, z: number) => `${x},${y},${z}`; const w = { kept: (_chunk_x: number, _chunk_z: number) => true, set(x: number, y: number, z: number, shape: boolean | Shape = true) { blocks.set(key(x, y, z), shape); return w; }, fill(x0: number, y0: number, z0: number, x1: number, y1: number, z1: number, shape: boolean | Shape = true) { for (let x = x0; x <= x1; x++) { for (let y = y0; y <= y1; y++) { for (let z = z0; z <= z1; z++) w.set(x, y, z, shape); } } return w; }, // tagged for mobs to path through through(x: number, y: number, z: number) { through.add(key(x, y, z)); return w; }, water(x: number, y: number, z: number) { water.add(key(x, y, z)); blocks.delete(key(x, y, z)); return w; }, collision_shape: (x: number, y: number, z: number) => blocks.get(key(x, y, z)) ?? false, is_fluid: (x: number, y: number, z: number) => water.has(key(x, y, z)), has_chunk: (chunk_x: number, chunk_z: number) => w.kept(chunk_x, chunk_z), passes_through: (x: number, y: number, z: number) => through.has(key(x, y, z)), }; return w; } // a floor from -8 to 8 at y 0, so feet stand at y 1 const floor = () => world().fill(-8, 0, -8, 8, 0, 8); const OPTIONS: PathOptions = { max_nodes: 2000, range: 32 }; // a mob that jumps 1 block and drops up to 3 function walker(x: number, z: number, width = 0.6, height = 1.8): PathBody { return { x: x + 0.5, y: 1, z: z + 0.5, width, height, max_rise: 1, max_fall: 3 }; } // one that doesn't jump, only steps up, and doesn't drop at all const careful = (x: number, z: number): PathBody => ({ ...walker(x, z, 0.8, 1.9), max_rise: 0.6, max_fall: 0 }); const target = (x: number, y: number, z: number): Point => ({ x: x + 0.5, y, z: z + 0.5 }); const blocks_on = (points: Point[]) => points.map((p) => `${Math.floor(p.x)},${Math.floor(p.y)},${Math.floor(p.z)}`); Deno.test("a mob on open ground goes straight to the target", () => { const path = find_path(floor(), walker(0, 0), target(5, 1, 0), OPTIONS)!; assert(path.reached); assertEquals(blocks_on(path.points), ["1,1,0", "2,1,0", "3,1,0", "4,1,0", "5,1,0"]); }); Deno.test("a mob goes through a doorway in a wall, and around a corner", () => { // a wall along x = 2 with a gap at z = 4, two blocks high so it can't be jumped const w = floor().fill(2, 1, -8, 2, 2, 8).fill(2, 1, 4, 2, 2, 4, false); const path = find_path(w, walker(0, 0), target(5, 1, 0), OPTIONS)!; assert(path.reached); assert(blocks_on(path.points).includes("2,1,4")); }); Deno.test("a mob that can't get there goes as close as it can, and nowhere when it's shut in", () => { const walled = floor().fill(2, 1, -8, 2, 2, 8); const partial = find_path(walled, walker(0, 0), target(5, 1, 0), OPTIONS)!; assert(!partial.reached); assertEquals(blocks_on(partial.points).at(-1), "1,1,0"); const shut_in = floor().fill(-1, 1, -1, 1, 2, 1).fill(0, 1, 0, 0, 2, 0, false); assertEquals(find_path(shut_in, walker(0, 0), target(5, 1, 0), OPTIONS), undefined); }); Deno.test("a mob jumps up one block but not two, and drops three but not four", () => { const step = floor().set(2, 1, 0).fill(3, 1, -8, 8, 1, 8); assert(find_path(step, walker(0, 0), target(5, 2, 0), OPTIONS)!.reached); const wall = floor().fill(2, 1, -8, 8, 2, 8); assert(!find_path(wall, walker(0, 0), target(5, 3, 0), OPTIONS)?.reached); // standing on a ledge at y 3 (feet at 4), the ground beyond it 3 or 4 blocks down const ledge = (drop: number) => world().fill(-8, 3, -8, 1, 3, 8).fill(2, 3 - drop, -8, 8, 3 - drop, 8); const from_ledge = { ...walker(0, 0), y: 4 }; assert(find_path(ledge(3), from_ledge, target(5, 1, 0), OPTIONS)!.reached); assert(!find_path(ledge(4), from_ledge, target(5, 0, 0), OPTIONS)?.reached); }); Deno.test("a mob steps onto a slab", () => { const slab: Shape = [[0, 0, 0, 1, 0.5, 1]]; const w = floor().set(2, 1, 0, slab); const path = find_path(w, walker(0, 0), target(2, 1.5, 0), OPTIONS)!; assert(path.reached); assertEquals(path.points.at(-1)!.y, 1.5); }); Deno.test("a mob gets past a thin open door, but not a closed one or a doorway too low for it", () => { // a wall 3 high along z = 2 with a doorway 2 high at x = 0, the door 1/16 thick const doorway = (door: Shape) => floor().fill(-8, 1, 2, 8, 3, 2).set(0, 1, 2, door).set(0, 2, 2, door); const open: Shape = [[0, 0, 0, 0.0625, 1, 1]]; const closed: Shape = [[0, 0, 0, 1, 1, 0.0625]]; const coilhead = walker(0, 0, 0.8, 1.9); assert(find_path(doorway(open), coilhead, target(0, 1, 5), OPTIONS)!.reached); assert(!find_path(doorway(closed), coilhead, target(0, 1, 5), OPTIONS)?.reached); assert(!find_path(doorway([]), walker(0, 0, 0.8, 2.5), target(0, 1, 5), OPTIONS)?.reached); }); Deno.test("a mob that doesn't jump steps onto a slab but not a block", () => { const slab: Shape = [[0, 0, 0, 1, 0.5, 1]]; assert(find_path(floor().set(2, 1, 0, slab), careful(0, 0), target(2, 1.5, 0), OPTIONS)!.reached); const step = floor().set(2, 1, 0).fill(3, 1, -8, 8, 1, 8); assert(!find_path(step, careful(0, 0), target(5, 2, 0), OPTIONS)?.reached); }); Deno.test("a mob that doesn't drop goes around a pit, and stops at one it can't get around", () => { // a pit 3 deep at 2, 0, right in the way const pit = floor().fill(2, -2, 0, 2, 0, 0, false).set(2, -3, 0); const around = find_path(pit, careful(0, 0), target(4, 1, 0), OPTIONS)!; assert(around.reached); assert(!blocks_on(around.points).some((block) => block.startsWith("2,") && block.endsWith(",0"))); const trench = floor().fill(2, -2, -8, 2, 0, 8, false).fill(2, -3, -8, 2, -3, 8); const stopped = find_path(trench, careful(0, 0), target(4, 1, 0), OPTIONS)!; assert(!stopped.reached); assertEquals(blocks_on(stopped.points).at(-1), "1,1,0"); }); Deno.test("a mob's path goes through blocks tagged for it, like a closed door", () => { const closed: Shape = [[0, 0, 0, 1, 1, 0.0625]]; const door = () => floor().fill(-8, 1, 2, 8, 3, 2).set(0, 1, 2, closed).set(0, 2, 2, closed); assert(!find_path(door(), careful(0, 0), target(0, 1, 5), OPTIONS)?.reached); const tagged = door().through(0, 1, 2).through(0, 2, 2); const path = find_path(tagged, careful(0, 0), target(0, 1, 5), OPTIONS)!; assert(path.reached); assert(blocks_on(path.points).includes("0,1,2")); }); Deno.test("a mob doesn't cut between two blocks that meet at a corner", () => { // the only way to 1, 1 is the gap between the corners of 1, 0 and 0, 1 const w = world().fill(-1, 0, -1, 1, 0, 1).fill(1, 1, 0, 1, 2, 0).fill(0, 1, 1, 0, 2, 1); const path = find_path(w, walker(0, 0), target(1, 1, 1), OPTIONS); assert(!path?.reached); }); Deno.test("a mob swims across water in its way", () => { // a strip of water across the way const pool = floor().fill(2, 0, -8, 2, 0, 8, false); for (let z = -8; z <= 8; z++) pool.water(2, 0, z).water(2, 1, z); pool.set(2, -1, 0); assert(find_path(pool, walker(0, 0), target(4, 1, 0), OPTIONS)!.reached); }); Deno.test("chunks that aren't kept count as solid", () => { const w = world().fill(-8, 0, -8, 24, 0, 8); w.kept = (chunk_x) => chunk_x < 1; const path = find_path(w, walker(0, 0), target(20, 1, 0), OPTIONS)!; assert(!path.reached); assert(path.points.every((p) => p.x < 16)); }); Deno.test("a mob only looks so far", () => { const path = find_path(floor(), walker(0, 0), target(8, 1, 0), { max_nodes: 2000, range: 4 })!; assert(!path.reached); assert(path.points.every((p) => Math.hypot(p.x - 0.5, p.z - 0.5) <= 4)); });