Monsters opening doors

This commit is contained in:
2026-11-21 15:16:07 -03:00
parent 2563ec3f69
commit 94463b3cea
6 changed files with 164 additions and 71 deletions
+13 -16
View File
@@ -6,6 +6,7 @@
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;
@@ -159,28 +160,24 @@ export class PathNavigation {
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
// 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 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++) {
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);
if (shape === true || (shape !== false && shape.length > 0)) {
return { x, y, z, id: level.get_block_info(x, y, z)!.id };
}
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 };
}
}
}