import type { Face, 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. with "solid" it joins the full sides of other blocks // beside it too, like iron bars, fences and walls set into a wall interface ConnectsParams { up?: boolean; solid?: boolean; } function register_connects(ctx: SharedContext) { const sides = (id: string | undefined, x: number, y: number, z: number, params: ConnectsParams) => { const same = (dx: number, dy: number, dz: number) => ctx.world.get_block(x + dx, y + dy, z + dz) === id ? 1 : 0; // the side of the block there that faces this one const joins = (dx: number, dz: number, face: Face) => same(dx, 0, dz) || (params.solid && ctx.world.is_face_sturdy(x + dx, y, z + dz, face)) ? 1 : 0; const states: Record = { north: joins(0, -1, "south"), east: joins(1, 0, "west"), south: joins(0, 1, "north"), west: joins(-1, 0, "east"), }; if (params.up) states.top = same(0, 1, 0); return states; }; ctx.components.register_block("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), 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))) { 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 }; }, }); } // the door component's params: "pairs_with" is the tag of doors it makes double doors with, and the sounds it makes // opening and closing export interface DoorParams { pairs_with: string; open_sound: string; close_sound: string; } // what set_door_open needs, which shared and server phases both have type DoorContext = Pick & { world: Pick; }; // 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, with its sound. 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); if (state("open") !== open) { ctx.sounds.play(open ? params.open_sound : params.close_sound, x + 0.5, lower + 1, z + 0.5); } // 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("dangerous_business:door", { on_interact(block, params) { 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 { 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(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; 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 }; }, }); }