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); register_half(ctx); register_pipe(ctx); register_connects(ctx); register_random_look(ctx); } // slabs and the like: in the top half (half 1) when placed against the underside of a block or the upper half of a // side, in the bottom half otherwise function register_half(ctx: SharedContext) { ctx.components.register_block("dangerous_business:half", { state_for_placement(placement) { if (placement.face === "top" || placement.face === "bottom") { return { half: placement.face === "bottom" ? 1 : 0 }; } return { half: placement.hit[1] > 0.5 ? 1 : 0 }; }, }); } // stands up when placed against the top or bottom of a block, and lies along the way it was placed (horizontal 1, facing // that way) against a side, so a run of them can go up walls and along them function register_pipe(ctx: SharedContext) { ctx.components.register_block("dangerous_business:pipe", { state_for_placement(placement): Record { if (placement.face === "top" || placement.face === "bottom") return { horizontal: 0 }; return { horizontal: 1, facing: FACINGS.indexOf(placement.face) }; }, }); } // sets its north, east, south and west states (and top, with "up") to 1 where the same block is beside it, so it can // hide its edges there, like floor grates joining into one floor function register_connects(ctx: SharedContext) { const sides = (id: string | undefined, x: number, y: number, z: number, up: boolean) => { const same = (dx: number, dy: number, dz: number) => ctx.world.get_block(x + dx, y + dy, z + dz) === id ? 1 : 0; const states: Record = { north: same(0, 0, -1), east: same(1, 0, 0), south: same(0, 0, 1), west: same(-1, 0, 0), }; if (up) states.top = same(0, 1, 0); return states; }; ctx.components.register_block<{ up?: boolean }>("dangerous_business:connects", { // the block being placed is the item's state_for_placement: (placement, params) => sides(placement.item, placement.x, placement.y, placement.z, params.up ?? false), on_neighbor_change(block, params) { const id = ctx.world.get_block(block.x, block.y, block.z); for (const [state, value] of Object.entries(sides(id, block.x, block.y, block.z, params.up ?? false))) { ctx.world.set_state(block.x, block.y, block.z, state, value); } }, }); } // picks some of its states when placed, from where it is, so the client and the server pick the same: a puddle's // shape and turn, a bookshelf's books. "states" is how many values each can have function register_random_look(ctx: SharedContext) { ctx.components.register_block<{ states: Record }>("dangerous_business:random_look", { state_for_placement(placement, params) { let seed = Math.imul(placement.x, 73856093) ^ Math.imul(placement.y, 19349663) ^ Math.imul(placement.z, 83492791); const states: Record = {}; for (const [state, count] of Object.entries(params.states)) { seed = Math.imul(seed ^ (seed >>> 15), 2246822519) >>> 0; states[state] = seed % count; } return states; }, }); } // 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 { 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("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 { 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 }; }, }); }