import { WoodTier, WOOD_TIER_ORDER, MATERIAL_DISPLAY_NAME } from "../definitions/materialTiers.js"; /** Facing axes placeable in the editor (E/W reuse N/S art). */ export const DoorFacing = Object.freeze({ n: "n", s: "s" }); export const DoorOpenness = Object.freeze({ open: "open", closed: "closed" }); /** * Visual aliases (documentation / future E-W placement): * - n-open ≈ e-closed * - s-open ≈ w-closed * - s-closed ≈ w-open * - n-closed ≈ e-open */ export const DOOR_FACING_ALIASES = Object.freeze({ "n-open": "e-closed", "s-open": "w-closed", "s-closed": "w-open", "n-closed": "e-open" }); export const DOOR_FACINGS = Object.freeze([DoorFacing.n, DoorFacing.s]); export const DOOR_OPENNESS = Object.freeze([DoorOpenness.closed, DoorOpenness.open]); /** Asset stem under `blocks/door/` (without wood tier). */ export function doorAssetStem (facing, open) { const face = facing === DoorFacing.n ? "n" : "s"; const openness = open ? "open" : "closed"; return `door-${face}-${openness}`; } /** * Authored / runtime door state: `${wood}-${facing}-${open|closed}`. * @returns {{ wood: string, facing: string, open: boolean } | null} */ export function parseDoorState (state) { const text = String(state ?? ""); const match = /^(pine|birch|oak|ebony|lignum|heartwood|elder)-(n|s)-(open|closed)$/u.exec(text); if (!match) { return null; } return { wood: match[1], facing: match[2], open: match[3] === DoorOpenness.open }; } export function composeDoorState (wood, facing, open) { const material = WOOD_TIER_ORDER.includes(wood) ? wood : WoodTier.pine; const face = facing === DoorFacing.n ? DoorFacing.n : DoorFacing.s; const openness = open ? DoorOpenness.open : DoorOpenness.closed; return `${material}-${face}-${openness}`; } export function flipDoorState (state) { const parsed = parseDoorState(state); if (!parsed) { return null; } return composeDoorState(parsed.wood, parsed.facing, !parsed.open); } /** * Authored resting state (world placement). Passage "open" is the flip of this, * not the open/closed suffix — e.g. resting `s-open` (≈ W-closed) opens to `s-closed`. */ export function doorRestingState (block) { const resting = block?.config?.restingState; if (parseDoorState(resting)) { return resting; } return block?.state ?? null; } /** True when the live state is flipped away from resting (someone held it for passage). */ export function isDoorPassageOpen (block) { const current = block?.state; const resting = doorRestingState(block); if (!parseDoorState(current) || !parseDoorState(resting)) { return false; } return current !== resting; } export function isDoorOpenState (state) { return !!parseDoorState(state)?.open; } /** * Y-sort sub-order within the door's tile (see `ySortLayer` / `Y_SORT_SUBORDER_STRIDE`). * Only S-closed draws in front of blocks/entities on that tile; * S-open, N-closed, and N-open all draw behind. * Still shares the tile's sortY so the row above stays behind. */ export const DOOR_SORT_BEHIND = 1; /** Above entity body (3) and equipment overlays (4…). */ export const DOOR_SORT_FRONT = 12; export function doorSortSubOrder (state, defaultSubOrder = 3) { const parsed = parseDoorState(state); if (!parsed) { return defaultSubOrder; } if (parsed.facing === DoorFacing.s && !parsed.open) { return DOOR_SORT_FRONT; } return DOOR_SORT_BEHIND; } /** * Passability: traversable when held open for passage (flipped from resting), * or when the mover can open doors (they flip on approach). * Optional `requirementsAllow: false` blocks everyone (including held-open passage). */ export function isDoorTraversable (block, { canOpenDoors = false, requirementsAllow = true } = {}) { if (!requirementsAllow) { return false; } if (isDoorPassageOpen(block)) { return true; } return !!canOpenDoors; } export function listDoorStatesForWood (wood) { const states = []; for (const facing of DOOR_FACINGS) { for (const openness of DOOR_OPENNESS) { states.push(composeDoorState(wood, facing, openness === DoorOpenness.open)); } } return states; } export function listAllDoorStates () { const states = []; for (const wood of WOOD_TIER_ORDER) { states.push(...listDoorStatesForWood(wood)); } return states; } export function doorPaletteDisplayName (state) { const parsed = parseDoorState(state); if (!parsed) { return "Door"; } const wood = MATERIAL_DISPLAY_NAME[parsed.wood] ?? parsed.wood; const face = parsed.facing === DoorFacing.n ? "N" : "S"; const open = parsed.open ? "open" : "closed"; return `${wood} door (${face} ${open})`; }