| 1 | import { WoodTier, WOOD_TIER_ORDER, MATERIAL_DISPLAY_NAME } from "../definitions/materialTiers.js"; |
| 2 | |
| 3 | /** Facing axes placeable in the editor (E/W reuse N/S art). */ |
| 4 | export const DoorFacing = Object.freeze({ |
| 5 | n: "n", |
| 6 | s: "s" |
| 7 | }); |
| 8 | |
| 9 | export const DoorOpenness = Object.freeze({ |
| 10 | open: "open", |
| 11 | closed: "closed" |
| 12 | }); |
| 13 | |
| 14 | /** |
| 15 | * Visual aliases (documentation / future E-W placement): |
| 16 | * - n-open ≈ e-closed |
| 17 | * - s-open ≈ w-closed |
| 18 | * - s-closed ≈ w-open |
| 19 | * - n-closed ≈ e-open |
| 20 | */ |
| 21 | export const DOOR_FACING_ALIASES = Object.freeze({ |
| 22 | "n-open": "e-closed", |
| 23 | "s-open": "w-closed", |
| 24 | "s-closed": "w-open", |
| 25 | "n-closed": "e-open" |
| 26 | }); |
| 27 | |
| 28 | export const DOOR_FACINGS = Object.freeze([DoorFacing.n, DoorFacing.s]); |
| 29 | export const DOOR_OPENNESS = Object.freeze([DoorOpenness.closed, DoorOpenness.open]); |
| 30 | |
| 31 | /** Asset stem under `blocks/door/` (without wood tier). */ |
| 32 | export function doorAssetStem (facing, open) |
| 33 | { |
| 34 | const face = facing === DoorFacing.n ? "n" : "s"; |
| 35 | const openness = open ? "open" : "closed"; |
| 36 | return `door-${face}-${openness}`; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Authored / runtime door state: `${wood}-${facing}-${open|closed}`. |
| 41 | * @returns {{ wood: string, facing: string, open: boolean } | null} |
| 42 | */ |
| 43 | export function parseDoorState (state) |
| 44 | { |
| 45 | const text = String(state ?? ""); |
| 46 | const match = /^(pine|birch|oak|ebony|lignum|heartwood|elder)-(n|s)-(open|closed)$/u.exec(text); |
| 47 | if (!match) |
| 48 | { return null; } |
| 49 | return { |
| 50 | wood: match[1], |
| 51 | facing: match[2], |
| 52 | open: match[3] === DoorOpenness.open |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | export function composeDoorState (wood, facing, open) |
| 57 | { |
| 58 | const material = WOOD_TIER_ORDER.includes(wood) ? wood : WoodTier.pine; |
| 59 | const face = facing === DoorFacing.n ? DoorFacing.n : DoorFacing.s; |
| 60 | const openness = open ? DoorOpenness.open : DoorOpenness.closed; |
| 61 | return `${material}-${face}-${openness}`; |
| 62 | } |
| 63 | |
| 64 | export function flipDoorState (state) |
| 65 | { |
| 66 | const parsed = parseDoorState(state); |
| 67 | if (!parsed) |
| 68 | { return null; } |
| 69 | return composeDoorState(parsed.wood, parsed.facing, !parsed.open); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Authored resting state (world placement). Passage "open" is the flip of this, |
| 74 | * not the open/closed suffix — e.g. resting `s-open` (≈ W-closed) opens to `s-closed`. |
| 75 | */ |
| 76 | export function doorRestingState (block) |
| 77 | { |
| 78 | const resting = block?.config?.restingState; |
| 79 | if (parseDoorState(resting)) |
| 80 | { return resting; } |
| 81 | return block?.state ?? null; |
| 82 | } |
| 83 | |
| 84 | /** True when the live state is flipped away from resting (someone held it for passage). */ |
| 85 | export function isDoorPassageOpen (block) |
| 86 | { |
| 87 | const current = block?.state; |
| 88 | const resting = doorRestingState(block); |
| 89 | if (!parseDoorState(current) || !parseDoorState(resting)) |
| 90 | { return false; } |
| 91 | return current !== resting; |
| 92 | } |
| 93 | |
| 94 | export function isDoorOpenState (state) |
| 95 | { |
| 96 | return !!parseDoorState(state)?.open; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Y-sort sub-order within the door's tile (see `ySortLayer` / `Y_SORT_SUBORDER_STRIDE`). |
| 101 | * Only S-closed draws in front of blocks/entities on that tile; |
| 102 | * S-open, N-closed, and N-open all draw behind. |
| 103 | * Still shares the tile's sortY so the row above stays behind. |
| 104 | */ |
| 105 | export const DOOR_SORT_BEHIND = 1; |
| 106 | /** Above entity body (3) and equipment overlays (4…). */ |
| 107 | export const DOOR_SORT_FRONT = 12; |
| 108 | |
| 109 | export function doorSortSubOrder (state, defaultSubOrder = 3) |
| 110 | { |
| 111 | const parsed = parseDoorState(state); |
| 112 | if (!parsed) |
| 113 | { return defaultSubOrder; } |
| 114 | if (parsed.facing === DoorFacing.s && !parsed.open) |
| 115 | { return DOOR_SORT_FRONT; } |
| 116 | return DOOR_SORT_BEHIND; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Passability: traversable when held open for passage (flipped from resting), |
| 121 | * or when the mover can open doors (they flip on approach). |
| 122 | * Optional `requirementsAllow: false` blocks everyone (including held-open passage). |
| 123 | */ |
| 124 | export function isDoorTraversable (block, { canOpenDoors = false, requirementsAllow = true } = {}) |
| 125 | { |
| 126 | if (!requirementsAllow) |
| 127 | { return false; } |
| 128 | if (isDoorPassageOpen(block)) |
| 129 | { return true; } |
| 130 | return !!canOpenDoors; |
| 131 | } |
| 132 | |
| 133 | export function listDoorStatesForWood (wood) |
| 134 | { |
| 135 | const states = []; |
| 136 | for (const facing of DOOR_FACINGS) |
| 137 | { |
| 138 | for (const openness of DOOR_OPENNESS) |
| 139 | { |
| 140 | states.push(composeDoorState(wood, facing, openness === DoorOpenness.open)); |
| 141 | } |
| 142 | } |
| 143 | return states; |
| 144 | } |
| 145 | |
| 146 | export function listAllDoorStates () |
| 147 | { |
| 148 | const states = []; |
| 149 | for (const wood of WOOD_TIER_ORDER) |
| 150 | { states.push(...listDoorStatesForWood(wood)); } |
| 151 | return states; |
| 152 | } |
| 153 | |
| 154 | export function doorPaletteDisplayName (state) |
| 155 | { |
| 156 | const parsed = parseDoorState(state); |
| 157 | if (!parsed) |
| 158 | { return "Door"; } |
| 159 | const wood = MATERIAL_DISPLAY_NAME[parsed.wood] ?? parsed.wood; |
| 160 | const face = parsed.facing === DoorFacing.n ? "N" : "S"; |
| 161 | const open = parsed.open ? "open" : "closed"; |
| 162 | return `${wood} door (${face} ${open})`; |
| 163 | } |