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
+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;