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
+28 -2
View File
@@ -1,5 +1,6 @@
// the coilhead: it goes after players like any monster, but can't move while one of them is looking at it
import type { MobRef, Player, Position, ServerContext } from "cubi/server";
import type { MobRef, Player, Position, ScriptGoal, ServerContext } from "cubi/server";
import { DOOR, type DoorParams, set_door_open } from "./shared.ts";
// how far away a player still freezes it
const SEEN_RANGE = 48;
@@ -7,7 +8,9 @@ const SEEN_RANGE = 48;
// way, about what fits on the screen
const VIEW_COS = 0.5;
// how much faster than its movement_speed it runs at its target
const CHASE_SPEED = 1.25;
const CHASE_SPEED = 2;
// how long it stands at a closed door on its way before opening it, in ticks
const OPEN_DOOR_TICKS = 20 * 20;
export function register_coilhead(ctx: ServerContext) {
const builtin = ctx.entities.goals;
@@ -27,11 +30,34 @@ export function register_coilhead(ctx: ServerContext) {
goals.add(1, chase);
goals.add(2, stroll);
goals.add(3, look_around);
goals.add(4, opens_doors(ctx, mob, OPEN_DOOR_TICKS));
goals.add_target(0, hurt_by);
goals.add_target(1, nearest_player);
});
}
// opens a door on the mob's way after it's stood at it for ticks. without flags, so the goal going after its target
// keeps its way and the mob stays at the door. the door's in the mobs_path_through tag, so its way goes through it
export function opens_doors(ctx: ServerContext, mob: MobRef, ticks: number): ScriptGoal {
let waited = 0;
const door = () => {
const blocked = mob.blocked_by;
return blocked && DOOR in ctx.blocks.components(blocked.id) ? blocked : undefined;
};
return {
can_use: () => door() !== undefined,
start: () => waited = 0,
tick: () => {
// a goal that started this tick, like being seen, can stop the mob's way and with it what blocks it
const blocked = door();
if (!blocked) return;
const { x, y, z, id } = blocked;
if (++waited !== ticks) return;
set_door_open(ctx, x, y, z, 1, ctx.blocks.components(id)[DOOR] as DoorParams);
},
};
}
// whether the player is looking its way with nothing in between, at its head or its middle
function sees(ctx: ServerContext, player: Player, mob: MobRef) {
if (player.health <= 0) return false;
+45 -34
View File
@@ -110,52 +110,63 @@ function register_facility_door(ctx: SharedContext) {
});
}
interface DoorParams {
// the door component's params: "pairs_with" is the tag of doors it makes double doors with
export interface DoorParams {
pairs_with: string;
}
function register_door(ctx: SharedContext) {
// a door's lower half there, of the kind this one pairs with
const door_at = (x: number, y: number, z: number, params: DoorParams) => {
const id = ctx.world.get_block(x, y, z);
return id !== undefined && ctx.tags.has("blocks", params.pairs_with, id) &&
ctx.world.get_state(x, y, z, "half") === 0;
};
// opens or closes both halves of the door whose lower half is there
const set_open = (x: number, y: number, z: number, open: number) => {
if (ctx.world.get_block(x, y + 1, z) === ctx.world.get_block(x, y, z)) {
ctx.world.set_state(x, y + 1, z, "open", open);
}
return ctx.world.set_state(x, y, z, "open", open);
};
// what set_door_open needs, which shared, server and client phases all have
type DoorContext = Pick<SharedContext, "tags"> & {
world: Pick<SharedContext["world"], "get_block" | "get_state" | "set_state">;
};
// a door's lower half there, of the kind a door with params pairs with
function door_at(ctx: DoorContext, x: number, y: number, z: number, params: DoorParams) {
const id = ctx.world.get_block(x, y, z);
return id !== undefined && ctx.tags.has("blocks", params.pairs_with, id) &&
ctx.world.get_state(x, y, z, "half") === 0;
}
// opens (open 1) or closes (open 0) the door with a half at x y z: both its halves, and the other door of a double
// door with it when it's open or closed like this one. players do it by clicking, monsters from their goals. returns
// whether it changed
export function set_door_open(ctx: DoorContext, x: number, y: number, z: number, open: number, params: DoorParams) {
const lower = ctx.world.get_state(x, y, z, "half") ? y - 1 : y;
const state = (name: string, at_x = x, at_z = z) => ctx.world.get_state(at_x, lower, at_z, name);
// both halves of the door whose lower half is at x z
const set_halves = (at_x: number, at_z: number) => {
if (ctx.world.get_block(at_x, lower + 1, at_z) === ctx.world.get_block(at_x, lower, at_z)) {
ctx.world.set_state(at_x, lower + 1, at_z, "open", open);
}
return ctx.world.set_state(at_x, lower, at_z, "open", open);
};
// the other door of a double door is beside its free edge, facing the same way, hinged the other way, and open or
// closed like it
const facing = state("facing")!;
const [step_x, step_z] = FACING_STEPS[(facing + (state("hinge") ? 3 : 1)) % 4];
const [px, pz] = [x + step_x, z + step_z];
if (
door_at(ctx, px, lower, pz, params) && state("facing", px, pz) === facing &&
state("hinge", px, pz) !== state("hinge") && state("open", px, pz) === state("open")
) {
set_halves(px, pz);
}
return set_halves(x, z);
}
function register_door(ctx: SharedContext) {
ctx.components.register_block<DoorParams>("dangerous_business:door", {
on_interact(block, params) {
const { x, z } = block;
const y = ctx.world.get_state(x, block.y, z, "half") ? block.y - 1 : block.y;
const state = (name: string, at_x = x, at_z = z) => ctx.world.get_state(at_x, y, at_z, name);
const was_open = state("open");
const open = was_open ? 0 : 1;
// the other door of a double door is beside its free edge, facing the same way, hinged the other way, and
// open or closed like it
const facing = state("facing")!;
const [step_x, step_z] = FACING_STEPS[(facing + (state("hinge") ? 3 : 1)) % 4];
const [px, pz] = [x + step_x, z + step_z];
if (
door_at(px, y, pz, params) && state("facing", px, pz) === facing &&
state("hinge", px, pz) !== state("hinge") && state("open", px, pz) === was_open
) {
set_open(px, y, pz, open);
}
return set_open(x, y, z, open);
const open = ctx.world.get_state(block.x, block.y, block.z, "open") ? 0 : 1;
return set_door_open(ctx, block.x, block.y, block.z, open, params);
},
state_for_placement(placement, params): Record<string, number> {
const facing = FACINGS.indexOf(placement.facing);
const { x, y, z } = placement;
const [left_x, left_z] = FACING_STEPS[(facing + 3) % 4];
const [right_x, right_z] = FACING_STEPS[(facing + 1) % 4];
const door_left = door_at(x + left_x, y, z + left_z, params);
const door_right = door_at(x + right_x, y, z + right_z, params);
const door_left = door_at(ctx, x + left_x, y, z + left_z, params);
const door_right = door_at(ctx, x + right_x, y, z + right_z, params);
if (door_left !== door_right) return { facing, half: 0, hinge: door_left ? 1 : 0 };
const [step_x, step_z] = FACING_STEPS[facing];
const [hit_x, , hit_z] = placement.hit;
+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 };
}
}
}
+64 -5
View File
@@ -8,7 +8,8 @@ import { assert, assertAlmostEquals, assertEquals, assertNotEquals } from "@std/
import { ItemStack } from "@/common/inventory.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { create_entity } from "@/server/game/entity/entity_types.ts";
import { copy_dir, test_game, test_mob } from "./helpers.ts";
import type { Mob } from "@/server/game/entity/mob.ts";
import { copy_dir, errors_during, test_game, test_mob } from "./helpers.ts";
const HALF = "dangerous_business:concrete_wall_slab";
const CONNECTS = "dangerous_business:floor_grate";
@@ -159,8 +160,12 @@ Deno.test("a player can't stop a coilhead through a wall", async () => {
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();
// a wall along z 0 with a closed factory door at x 0, facing so it's across the way. open says whether both its halves
// are open, open_door opens them
function door_in_wall(
t: Awaited<ReturnType<typeof setup>>["t"],
state: (x: number, y: number, z: number, name: string) => number | undefined,
) {
const world = t.game.world;
const set_state = (x: number, y: number, z: number, name: string, value: number) =>
t.game.set_block_state(
@@ -169,7 +174,6 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
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);
}
@@ -178,6 +182,15 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
set_state(0, y, 0, "facing", 0);
set_state(0, y, 0, "half", half);
}
return {
open: () => state(0, 100, 0, "open") === 1 && state(0, 101, 0, "open") === 1,
open_door: () => [100, 101].forEach((y) => set_state(0, y, 0, "open", 1)),
};
}
Deno.test("a mob's path goes through a closed factory door, and it waits there until the door opens", async () => {
const { t, state } = await setup();
const { open_door } = door_in_wall(t, state);
// thin enough to get past the door once it's open
const mob = test_mob(t.game, {}, 0.5, 100, -2.5);
@@ -186,9 +199,55 @@ Deno.test("a mob's path goes through a closed factory door, and it waits there u
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);
open_door();
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);
});
Deno.test("a coilhead going after a player opens a closed door in its way after standing at it a while", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// alice looks away from the door, so nothing freezes it
t.send(1, { type: "move", x: 0.5, y: 100, z: 3.5, yaw: Math.PI, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
let blocked_at: number | undefined;
let tick = 0;
for (; tick < 300 && !open(); tick++) {
t.game.tick();
if (blocked_at === undefined && coilhead.navigation.blocked_by) blocked_at = tick;
}
assert(open(), "it never opened the door");
assert(blocked_at !== undefined && tick - blocked_at >= 40, `it opened it at ${tick}, blocked from ${blocked_at}`);
});
Deno.test("a coilhead seen while it waits at a closed door stops waiting, and doesn't open it", async () => {
const { t, state } = await setup();
const { open } = door_in_wall(t, state);
// a window 2 wide at head height beside the door, too low to get through
for (const x of [1, 2]) t.game.set_block(x, 101, 0, "cubi:air");
// alice beside the window, looking away
t.send(1, { type: "move", x: 3.5, y: 100, z: 1.5, yaw: -Math.PI / 2, pitch: 0 });
const coilhead = spawn_coilhead(t) as Mob;
for (let i = 0; i < 300 && !coilhead.navigation.blocked_by; i++) t.game.tick();
assert(coilhead.navigation.blocked_by, "it never got to the door");
for (let i = 0; i < 10; i++) t.game.tick();
// she turns to look at it through the window, before it's waited long enough to open the door
const errors = await errors_during(() => {
t.send(1, {
type: "move",
x: 3.5,
y: 100,
z: 1.5,
yaw: Math.atan2(3.5 - coilhead.x, 1.5 - coilhead.z),
pitch: 0,
});
for (let i = 0; i < 60; i++) t.game.tick();
});
assertEquals(errors, []);
assert(!open(), "it opened the door while it was seen");
assertEquals(coilhead.navigation.blocked_by, undefined);
});
+1 -14
View File
@@ -16,7 +16,7 @@ import { LocalPlayer } from "@/client/entity/local_player.ts";
import { Options } from "@/client/options.ts";
import type { Client } from "@/client/client.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, errors_during, test_game } from "./helpers.ts";
import { game_with_pool } from "./chunk_loading.ts";
const [MAJOR, MINOR] = GAME_VERSION.split(".");
@@ -61,19 +61,6 @@ function as_mod<T>(mod: string, fn: () => T, engine_access = true): Promise<T> {
return loading_mod(mod, engine_access, () => Promise.resolve(fn()));
}
// what console.error got while fn ran
async function errors_during(fn: () => unknown): Promise<string[]> {
const errors: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => errors.push(args.map(String).join(" "));
try {
await fn();
} finally {
console.error = original;
}
return errors;
}
Deno.test("a server mod hooks GameServer to drop twice, like the plan's example", async () => {
const dir = mods_dir((dir) =>
write_mod(
+13
View File
@@ -465,3 +465,16 @@ export function test_mob(game: GameServer, fields: Partial<EntityJson>, x: numbe
game.entities.add_entity(mob);
return mob;
}
// what console.error got while fn ran
export async function errors_during(fn: () => unknown): Promise<string[]> {
const errors: string[] = [];
const original = console.error;
console.error = (...args: unknown[]) => errors.push(args.map(String).join(" "));
try {
await fn();
} finally {
console.error = original;
}
return errors;
}