More pathing
This commit is contained in:
@@ -82,9 +82,9 @@ back. Depend on the mod whose tag you change, so your file loads after its. chec
|
|||||||
|
|
||||||
The engine reads these by name. No mod owns `cubi`, so adding to them needs no dependency.
|
The engine reads these by name. No mod owns `cubi`, so adding to them needs no dependency.
|
||||||
|
|
||||||
| Tag | Registry | What's in it |
|
| Tag | Registry | What's in it |
|
||||||
| ------------------------- | -------- | ------------------------------------------------------------------------------------------------- |
|
| ------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `#cubi:mobs_path_through` | blocks | Blocks mobs' paths go through, like doors: a mob runs into a closed one and waits for it to open. |
|
| `#cubi:mobs_path_through` | blocks | Blocks mobs' paths go through, solid or not. A mob pressed against a solid one waits there, see [Entities](../entities/index.md). |
|
||||||
|
|
||||||
## Using Tags in Data
|
## Using Tags in Data
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
|
||||||
import { type DamageSource, Entity, finite } from "./entity.ts";
|
import { type DamageSource, Entity, finite } from "./entity.ts";
|
||||||
import { GoalSelector } from "./goals.ts";
|
import { GoalSelector } from "./goals.ts";
|
||||||
|
import { PathNavigation } from "./navigation.ts";
|
||||||
import { ItemEntity } from "./item_entity.ts";
|
import { ItemEntity } from "./item_entity.ts";
|
||||||
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
|
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
|
||||||
import type { EntityData } from "@/common/protocol.ts";
|
import type { EntityData } from "@/common/protocol.ts";
|
||||||
@@ -72,6 +73,8 @@ export abstract class Mob extends LivingEntity {
|
|||||||
last_attacker: LivingEntity | undefined;
|
last_attacker: LivingEntity | undefined;
|
||||||
times_hurt_by_attacker = 0;
|
times_hurt_by_attacker = 0;
|
||||||
|
|
||||||
|
// follows paths, giving the move control one waypoint at a time
|
||||||
|
navigation = new PathNavigation(this);
|
||||||
move_control = new MoveControl(this);
|
move_control = new MoveControl(this);
|
||||||
look_control = new LookControl(this);
|
look_control = new LookControl(this);
|
||||||
jump_control = new JumpControl(this);
|
jump_control = new JumpControl(this);
|
||||||
@@ -128,7 +131,7 @@ export abstract class Mob extends LivingEntity {
|
|||||||
return super.move();
|
return super.move();
|
||||||
}
|
}
|
||||||
|
|
||||||
// the goals, then the controls, like minecraft's Mob.serverAiStep
|
// the goals, then the path they asked for, then the controls, like minecraft's Mob.serverAiStep
|
||||||
override server_ai_step() {
|
override server_ai_step() {
|
||||||
if (!this.#registered_goals) {
|
if (!this.#registered_goals) {
|
||||||
this.#registered_goals = true;
|
this.#registered_goals = true;
|
||||||
@@ -137,6 +140,7 @@ export abstract class Mob extends LivingEntity {
|
|||||||
this.no_action_time += 1;
|
this.no_action_time += 1;
|
||||||
this.target_goals.tick();
|
this.target_goals.tick();
|
||||||
this.goals.tick();
|
this.goals.tick();
|
||||||
|
this.navigation.tick();
|
||||||
this.move_control.tick();
|
this.move_control.tick();
|
||||||
this.look_control.tick();
|
this.look_control.tick();
|
||||||
this.jump_control.tick();
|
this.jump_control.tick();
|
||||||
|
|||||||
@@ -0,0 +1,183 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
// a mob following a path, like minecraft's PathNavigation: it searches for a way (see pathfinding.ts) and walks it one
|
||||||
|
// waypoint at a time through the move control. see PATHFINDING.md
|
||||||
|
import { ATTRIBUTES } from "./attributes.ts";
|
||||||
|
import type { Mob } from "./mob.ts";
|
||||||
|
import { find_path, type Path, type Point } from "./pathfinding.ts";
|
||||||
|
|
||||||
|
// ticks with no closer to its waypoint before it searches again, and then gives up
|
||||||
|
const STUCK_TICKS = 40;
|
||||||
|
// ticks without getting closer before a solid block its path goes through counts as blocking it
|
||||||
|
const BLOCKED_TICKS = 10;
|
||||||
|
// ticks a mob waits after a search that didn't find the whole way before it searches again, since those are the
|
||||||
|
// searches that cost the most
|
||||||
|
const FAILED_SEARCH_DELAY = 20;
|
||||||
|
const EPSILON = 1e-4;
|
||||||
|
|
||||||
|
// a solid block in the mob's way that its path goes through, see MOBS_PATH_THROUGH
|
||||||
|
export interface BlockedBy {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
z: number;
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PathNavigation {
|
||||||
|
readonly mob: Mob;
|
||||||
|
#path: Path | undefined;
|
||||||
|
// the waypoint it's walking to
|
||||||
|
#index = 0;
|
||||||
|
#speed = 1;
|
||||||
|
#target: Point | undefined;
|
||||||
|
// the closest it's been to its waypoint, and for how many ticks it's been no closer
|
||||||
|
#closest = Infinity;
|
||||||
|
#stuck = 0;
|
||||||
|
// whether it searched again because it was stuck, so the next time it gives up
|
||||||
|
#searched_again = false;
|
||||||
|
// the tick before which it doesn't search again, after a search that didn't find the whole way
|
||||||
|
#search_after = -Infinity;
|
||||||
|
// the solid block tagged mobs_path_through it's pressed against on its way, if any
|
||||||
|
blocked_by: BlockedBy | undefined;
|
||||||
|
|
||||||
|
constructor(mob: Mob) {
|
||||||
|
this.mob = mob;
|
||||||
|
}
|
||||||
|
|
||||||
|
// whether it's still on its way
|
||||||
|
get moving() {
|
||||||
|
return this.#path !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// where it's going, when it is
|
||||||
|
get target(): Readonly<Point> | undefined {
|
||||||
|
return this.#target;
|
||||||
|
}
|
||||||
|
|
||||||
|
// searches for a way to x y z and starts walking it, speed times its movement_speed. it walks to as close as it can
|
||||||
|
// get when the whole way isn't there. false when there's no way closer at all, or when a search that didn't find the
|
||||||
|
// whole way was too recent to search again, keeping whatever way it had
|
||||||
|
move_to(x: number, y: number, z: number, speed = 1): boolean {
|
||||||
|
const now = this.mob.level.current_tick;
|
||||||
|
if (now < this.#search_after) {
|
||||||
|
if (!this.#path) return false;
|
||||||
|
this.#speed = speed;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const path = this.#search({ x, y, z });
|
||||||
|
if (!path?.reached) this.#search_after = now + FAILED_SEARCH_DELAY;
|
||||||
|
if (!path) {
|
||||||
|
this.stop();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.#follow(path, { x, y, z }, speed);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
stop() {
|
||||||
|
this.#path = undefined;
|
||||||
|
this.#target = undefined;
|
||||||
|
this.blocked_by = undefined;
|
||||||
|
this.mob.move_control.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
const path = this.#path;
|
||||||
|
if (!path) return;
|
||||||
|
const mob = this.mob;
|
||||||
|
const points = path.points;
|
||||||
|
while (this.#index < points.length && this.#close_to(points[this.#index])) {
|
||||||
|
this.#index += 1;
|
||||||
|
this.#closest = Infinity;
|
||||||
|
this.#stuck = 0;
|
||||||
|
}
|
||||||
|
if (this.#index >= points.length) {
|
||||||
|
this.stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const next = points[this.#index];
|
||||||
|
mob.move_control.move_to(next.x, next.y, next.z, this.#speed);
|
||||||
|
|
||||||
|
const distance = Math.hypot(next.x - mob.x, next.z - mob.z);
|
||||||
|
if (distance < this.#closest - 0.01) {
|
||||||
|
this.#closest = distance;
|
||||||
|
this.#stuck = 0;
|
||||||
|
this.blocked_by = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.#stuck += 1;
|
||||||
|
// pressed against a block its path goes through, it waits there instead of giving up, for its behavior to do
|
||||||
|
// something about it
|
||||||
|
if (this.#stuck >= BLOCKED_TICKS) {
|
||||||
|
this.blocked_by = this.#solid_block_toward(next);
|
||||||
|
if (this.blocked_by) return;
|
||||||
|
}
|
||||||
|
if (this.#stuck >= STUCK_TICKS) {
|
||||||
|
const again = !this.#searched_again && this.#target ? this.#search(this.#target) : undefined;
|
||||||
|
if (again) this.#follow(again, this.#target!, this.#speed, true);
|
||||||
|
else this.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#search(target: Point) {
|
||||||
|
const mob = this.mob;
|
||||||
|
const range = mob.attribute(ATTRIBUTES.follow_range);
|
||||||
|
const body = {
|
||||||
|
x: mob.x,
|
||||||
|
y: mob.y,
|
||||||
|
z: mob.z,
|
||||||
|
width: mob.width,
|
||||||
|
height: mob.height,
|
||||||
|
max_rise: mob.jumps ? 1 : mob.step_height,
|
||||||
|
max_fall: mob.max_fall,
|
||||||
|
};
|
||||||
|
return find_path(mob.level, body, target, { max_nodes: Math.floor(range * 16), range });
|
||||||
|
}
|
||||||
|
|
||||||
|
#follow(path: Path, target: Point, speed: number, searched_again = false) {
|
||||||
|
this.#path = path;
|
||||||
|
this.#target = target;
|
||||||
|
this.#speed = speed;
|
||||||
|
this.#index = 0;
|
||||||
|
this.#closest = Infinity;
|
||||||
|
this.#stuck = 0;
|
||||||
|
this.#searched_again = searched_again;
|
||||||
|
this.blocked_by = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// close enough to a waypoint to go on to the next, like minecraft's maxDistanceToWaypoint
|
||||||
|
#close_to(point: Point) {
|
||||||
|
const mob = this.mob;
|
||||||
|
const reach = mob.width > 0.75 ? mob.width / 2 : 0.75 - mob.width / 2;
|
||||||
|
return Math.hypot(point.x - mob.x, point.z - mob.z) < reach && Math.abs(point.y - mob.y) < 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a block its path goes through that's solid, between where it is and its next waypoint
|
||||||
|
#solid_block_toward(next: Point): BlockedBy | undefined {
|
||||||
|
const mob = this.mob;
|
||||||
|
const level = mob.level;
|
||||||
|
const half = mob.width / 2;
|
||||||
|
const top = Math.max(mob.y, next.y) + mob.height;
|
||||||
|
for (
|
||||||
|
let x = Math.floor(Math.min(mob.x, next.x) - half);
|
||||||
|
x <= Math.floor(Math.max(mob.x, next.x) + half - EPSILON);
|
||||||
|
x++
|
||||||
|
) {
|
||||||
|
for (
|
||||||
|
let z = Math.floor(Math.min(mob.z, next.z) - half);
|
||||||
|
z <= Math.floor(Math.max(mob.z, next.z) + half - EPSILON);
|
||||||
|
z++
|
||||||
|
) {
|
||||||
|
for (let y = Math.floor(Math.min(mob.y, next.y)); y <= Math.floor(top - EPSILON); y++) {
|
||||||
|
if (!level.passes_through(x, y, z)) continue;
|
||||||
|
const shape = level.collision_shape(x, y, z);
|
||||||
|
if (shape === true || (shape !== false && shape.length > 0)) {
|
||||||
|
return { x, y, z, id: level.get_block_info(x, y, z)!.id };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
// how mobs find their way, like minecraft's PathFinder with its WalkNodeEvaluator: an A* search over the blocks a
|
// how mobs find their way, like minecraft's PathFinder with its WalkNodeEvaluator: an A* search over the blocks a
|
||||||
// mob's feet can be in. whether it fits in each one comes from its box against the blocks' collision shapes, so
|
// mob's feet can be in. whether it fits in each one comes from its box against the blocks' collision shapes, so
|
||||||
// partial blocks need nothing of their own: it steps up onto a slab, and gets past a thin open door at the side of a
|
// partial blocks need nothing of their own: it steps up onto a slab, and goes through a block when its box fits
|
||||||
// doorway. see PATHFINDING.md
|
// beside what's in it. see PATHFINDING.md
|
||||||
import type { Shape } from "@/common/block_shapes.ts";
|
import type { Shape } from "@/common/block_shapes.ts";
|
||||||
import { CHUNK_SIZE } from "@/common/constants.ts";
|
import { CHUNK_SIZE } from "@/common/constants.ts";
|
||||||
import { overlaps_solid } from "@/common/physics.ts";
|
import { overlaps_solid } from "@/common/physics.ts";
|
||||||
@@ -15,8 +15,8 @@ export interface Point {
|
|||||||
z: number;
|
z: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// blocks the search goes through as if they weren't there, like doors: a mob that runs into a closed one waits at it
|
// blocks the search goes through as if they weren't there, solid or not. a mob pressed against a solid one on its way
|
||||||
// for its behavior to open it, see PathNavigation
|
// waits there, for its behavior to do something about it, see PathNavigation.blocked_by
|
||||||
export const MOBS_PATH_THROUGH = "cubi:mobs_path_through";
|
export const MOBS_PATH_THROUGH = "cubi:mobs_path_through";
|
||||||
|
|
||||||
// what the search reads, the entity manager is one
|
// what the search reads, the entity manager is one
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
// down. the death block, and the coilhead
|
// down. the death block, and the coilhead
|
||||||
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
|
import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/assert";
|
||||||
import { ItemStack } from "@/common/inventory.ts";
|
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 { 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 HALF = "dangerous_business:concrete_wall_slab";
|
||||||
const CONNECTS = "dangerous_business:floor_grate";
|
const CONNECTS = "dangerous_business:floor_grate";
|
||||||
@@ -17,6 +17,7 @@ const FLOOR = "dangerous_business:concrete_floor";
|
|||||||
const DEATH = "dangerous_business:death";
|
const DEATH = "dangerous_business:death";
|
||||||
const PIPE = "dangerous_business:double_steel_pipes";
|
const PIPE = "dangerous_business:double_steel_pipes";
|
||||||
const COILHEAD = "dangerous_business:coilhead";
|
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
|
// 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
|
// 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
|
// up against the wall, still on its way to her
|
||||||
assert(coilhead.z > -1.5, `it only got to z ${coilhead.z}`);
|
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);
|
||||||
|
});
|
||||||
|
|||||||
@@ -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 as setup_shared } from "@/mods/bworld/scripts/shared.ts";
|
||||||
import { shared_context } from "@/common/shared_context.ts";
|
import { shared_context } from "@/common/shared_context.ts";
|
||||||
import { RecipeBook } from "@/common/mod_loader.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
|
// 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
|
// 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)!),
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ import { IDENTITY } from "@/client/rendering/model_math.ts";
|
|||||||
import { push_model } from "@/client/rendering/model_renderer.ts";
|
import { push_model } from "@/client/rendering/model_renderer.ts";
|
||||||
import { Mob } from "@/server/game/entity/mob.ts";
|
import { Mob } from "@/server/game/entity/mob.ts";
|
||||||
import { create_entity } from "@/server/game/entity/entity_types.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, test_mob } from "./helpers.ts";
|
||||||
import { container_items, test_game } from "./helpers.ts";
|
|
||||||
|
|
||||||
Deno.test("a bedrock geo.json becomes an entity model with only what the engine uses", () => {
|
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"));
|
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.join(1, "alice");
|
||||||
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
|
t.send(1, { type: "move", x: -3.5, y: 100, z: -3.5, yaw: 0, pitch: 0 });
|
||||||
const json: EntityJson = {
|
return { game: t.game, mob: test_mob(t.game, fields, 0.5, 100, 0.5) };
|
||||||
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 };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the highest and lowest its feet got over ticks, walking to x
|
// the highest and lowest its feet got over ticks, walking to x
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user