Files
2026-11-21 15:16:07 -03:00

187 lines
6.2 KiB
TypeScript

// 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";
import { overlaps_solid } from "@/common/physics.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);
// it stays blocked for as long as the block is solid and in its way, even when its path is searched again, so a
// goal can count how long it's been there
if (this.blocked_by) {
this.blocked_by = this.#solid_block_toward(next);
if (this.blocked_by) return;
this.#closest = Infinity;
this.#stuck = 0;
}
const distance = Math.hypot(next.x - mob.x, next.z - mob.z);
if (distance < this.#closest - 0.01) {
this.#closest = distance;
this.#stuck = 0;
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;
}
// 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 whose shape is in the way of its box, 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 min = [Math.min(mob.x, next.x) - half, Math.min(mob.y, next.y), Math.min(mob.z, next.z) - half];
const max = [
Math.max(mob.x, next.x) + half,
Math.max(mob.y, next.y) + mob.height,
Math.max(mob.z, next.z) + half,
];
for (let x = Math.floor(min[0]); x <= Math.floor(max[0] - EPSILON); x++) {
for (let z = Math.floor(min[2]); z <= Math.floor(max[2] - EPSILON); z++) {
for (let y = Math.floor(min[1]); y <= Math.floor(max[1] - EPSILON); y++) {
if (!level.passes_through(x, y, z)) continue;
const shape = level.collision_shape(x, y, z);
const only_here = (bx: number, by: number, bz: number) => bx === x && by === y && bz === z && shape;
if (overlaps_solid(min, max, only_here)) return { x, y, z, id: level.get_block_info(x, y, z)!.id };
}
}
}
return undefined;
}
}