Files
bworld/mods/dangerous_business/scripts/shared.ts
T
2026-11-16 21:27:24 -03:00

96 lines
4.3 KiB
TypeScript

import type { SharedContext } from "cubi/shared";
export const FACILITY_DOOR = "dangerous_business:facility_door";
export const DOOR = "dangerous_business:door";
const FACINGS = ["north", "east", "south", "west"];
// the way along the ground each facing goes, in FACINGS' order
const FACING_STEPS = [[0, -1], [1, 0], [0, 1], [-1, 0]];
export function shared(ctx: SharedContext) {
register_door(ctx);
register_facility_door(ctx);
}
// a door is two blocks, its lower half (half 0) and its upper half (half 1), with the same states. it faces the way the
// player placing it looks, and is hinged on the side away from a door of the same kind beside it, so the two meet in
// the middle as a double door. without one, on the side of the block the player clicked toward. the server places the
// upper half and takes away the other half when one breaks, see register_door in main.ts
function register_facility_door(ctx: SharedContext) {
ctx.components.register_block(FACILITY_DOOR, {
state_for_placement(placement): Record<string, number> {
const facing = FACINGS.indexOf(placement.facing);
const { x, y, z } = placement;
const door_at = ([step_x, step_z]: number[]) => {
const id = ctx.world.get_block(x + step_x, y, z + step_z);
return id !== undefined && FACILITY_DOOR in ctx.blocks.components(id) &&
ctx.world.get_state(x + step_x, y, z + step_z, "half") === 0;
};
const door_left = door_at(FACING_STEPS[(facing + 3) % 4]);
const door_right = door_at(FACING_STEPS[(facing + 1) % 4]);
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;
const right = (step_x < 0 && hit_z < 0.5) || (step_x > 0 && hit_z > 0.5) ||
(step_z < 0 && hit_x > 0.5) || (step_z > 0 && hit_x < 0.5);
return { facing, half: 0, hinge: right ? 1 : 0 };
},
});
}
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);
};
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);
},
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);
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;
const right = (step_x < 0 && hit_z < 0.5) || (step_x > 0 && hit_z > 0.5) ||
(step_z < 0 && hit_x > 0.5) || (step_z > 0 && hit_x < 0.5);
return { facing, half: 0, hinge: right ? 1 : 0 };
},
});
}