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;