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
+5 -1
View File
@@ -6,6 +6,7 @@
import { ATTRIBUTES, type AttributeType } from "./attributes.ts";
import { type DamageSource, Entity, finite } from "./entity.ts";
import { GoalSelector } from "./goals.ts";
import { PathNavigation } from "./navigation.ts";
import { ItemEntity } from "./item_entity.ts";
import { EQUIPMENT_SLOTS, type EquipmentSlot, LivingEntity, type SavedLivingEntity } from "./living_entity.ts";
import type { EntityData } from "@/common/protocol.ts";
@@ -72,6 +73,8 @@ export abstract class Mob extends LivingEntity {
last_attacker: LivingEntity | undefined;
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);
look_control = new LookControl(this);
jump_control = new JumpControl(this);
@@ -128,7 +131,7 @@ export abstract class Mob extends LivingEntity {
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() {
if (!this.#registered_goals) {
this.#registered_goals = true;
@@ -137,6 +140,7 @@ export abstract class Mob extends LivingEntity {
this.no_action_time += 1;
this.target_goals.tick();
this.goals.tick();
this.navigation.tick();
this.move_control.tick();
this.look_control.tick();
this.jump_control.tick();
+183
View File
@@ -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;
}
}
+4 -4
View File
@@ -3,8 +3,8 @@
// 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
// 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
// doorway. see PATHFINDING.md
// partial blocks need nothing of their own: it steps up onto a slab, and goes through a block when its box fits
// beside what's in it. see PATHFINDING.md
import type { Shape } from "@/common/block_shapes.ts";
import { CHUNK_SIZE } from "@/common/constants.ts";
import { overlaps_solid } from "@/common/physics.ts";
@@ -15,8 +15,8 @@ export interface Point {
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
// for its behavior to open it, see PathNavigation
// blocks the search goes through as if they weren't there, solid or not. a mob pressed against a solid one on its way
// waits there, for its behavior to do something about it, see PathNavigation.blocked_by
export const MOBS_PATH_THROUGH = "cubi:mobs_path_through";
// what the search reads, the entity manager is one