| 1 | import { Component } from "../ecs/components.js"; |
| 2 | import { mobDefinition, isHumanoidMob } from "../../../shared/definitions/mobs.js"; |
| 3 | import { BlockType } from "../../../shared/definitions/tiles.js"; |
| 4 | import { |
| 5 | flipDoorState, |
| 6 | isDoorPassageOpen, |
| 7 | doorRestingState, |
| 8 | parseDoorState |
| 9 | } from "../../../shared/world/doors.js"; |
| 10 | import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js"; |
| 11 | import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; |
| 12 | import { OVERWORLD_ID } from "../../../shared/world/dimensions.js"; |
| 13 | import { tileLightOcclusionAt } from "../../../shared/lighting/chunkOcclusion.js"; |
| 14 | |
| 15 | export function isDoorTypeId (typeId) |
| 16 | { |
| 17 | return (typeId | 0) === BlockType.door; |
| 18 | } |
| 19 | |
| 20 | export function isDoorBlock (block) |
| 21 | { |
| 22 | return isDoorTypeId(block?.typeId); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Players always open doors; humanoid bodies by default; |
| 27 | * set `canOpenDoors: true|false` on a mob def to override. |
| 28 | */ |
| 29 | export function entityCanOpenDoors (ecs, entityId) |
| 30 | { |
| 31 | const handle = ecs.handleOf(entityId); |
| 32 | if (handle == null) |
| 33 | { return false; } |
| 34 | if (ecs.get(handle, Component.character)) |
| 35 | { return true; } |
| 36 | const creature = ecs.get(handle, Component.creature); |
| 37 | if (!creature) |
| 38 | { return false; } |
| 39 | const def = mobDefinition(creature.mobTypeId); |
| 40 | if (!def) |
| 41 | { return false; } |
| 42 | if (def.canOpenDoors === true) |
| 43 | { return true; } |
| 44 | if (def.canOpenDoors === false) |
| 45 | { return false; } |
| 46 | return isHumanoidMob(creature.mobTypeId) || !!def.usePlayerBody; |
| 47 | } |
| 48 | |
| 49 | function ensureHeldDoors (world) |
| 50 | { |
| 51 | if (!world.heldDoors) |
| 52 | { world.heldDoors = new Map(); } |
| 53 | return world.heldDoors; |
| 54 | } |
| 55 | |
| 56 | function readBlock (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 57 | { |
| 58 | const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY), dimensionId); |
| 59 | return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; |
| 60 | } |
| 61 | |
| 62 | function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERWORLD_ID) |
| 63 | { |
| 64 | const chunkX = tileToChunk(tileX); |
| 65 | const chunkY = tileToChunk(tileY); |
| 66 | const localX = tileToLocal(tileX); |
| 67 | const localY = tileToLocal(tileY); |
| 68 | const data = world.chunkData?.(chunkX, chunkY, dimensionId); |
| 69 | const index = localY * CHUNK_SIZE + localX; |
| 70 | const prev = data?.blocks?.[index]; |
| 71 | if (!prev || !isDoorTypeId(prev.typeId)) |
| 72 | { return null; } |
| 73 | const block = { |
| 74 | typeId: BlockType.door, |
| 75 | state, |
| 76 | config: config ?? prev.config ?? null |
| 77 | }; |
| 78 | data.blocks[index] = block; |
| 79 | if (Array.isArray(data.occlusion) && data.occlusion.length > index) |
| 80 | { data.occlusion[index] = tileLightOcclusionAt(data.grounds, data.blocks, localX, localY); } |
| 81 | return { |
| 82 | type: "chunk-delta", |
| 83 | chunkX, |
| 84 | chunkY, |
| 85 | tileX, |
| 86 | tileY, |
| 87 | localX, |
| 88 | localY, |
| 89 | dimensionId, |
| 90 | block |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Flip the door away from its resting (authored) state so a canOpenDoors entity |
| 96 | * can pass. Resting is remembered until the vicinity clears. |
| 97 | */ |
| 98 | export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 99 | { |
| 100 | const block = readBlock(world, tileX, tileY, dimensionId); |
| 101 | if (!isDoorBlock(block)) |
| 102 | { return null; } |
| 103 | const parsed = parseDoorState(block.state); |
| 104 | if (!parsed) |
| 105 | { return null; } |
| 106 | const held = ensureHeldDoors(world); |
| 107 | const key = tileKey(tileX, tileY, dimensionId); |
| 108 | let restingState = held.get(key)?.restingState |
| 109 | ?? doorRestingState(block); |
| 110 | if (!parseDoorState(restingState)) |
| 111 | { restingState = block.state; } |
| 112 | |
| 113 | if (!held.has(key)) |
| 114 | { held.set(key, { restingState, dimensionId }); } |
| 115 | |
| 116 | /** Already flipped for passage — keep held, no visual change. */ |
| 117 | if (block.state !== restingState) |
| 118 | { return null; } |
| 119 | |
| 120 | const next = flipDoorState(restingState); |
| 121 | if (!next || next === block.state) |
| 122 | { return null; } |
| 123 | const config = { |
| 124 | ...(block.config && typeof block.config === "object" ? block.config : {}), |
| 125 | restingState |
| 126 | // Reserved for later: keyItemId, unlockNpcId, tollCoins, forcedPassage |
| 127 | }; |
| 128 | return writeDoorState(world, tileX, tileY, next, config, dimensionId); |
| 129 | } |
| 130 | |
| 131 | function chebyshev (ax, ay, bx, by) |
| 132 | { |
| 133 | return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); |
| 134 | } |
| 135 | |
| 136 | /** Any living body on the door or Chebyshev-adjacent keeps the held state. */ |
| 137 | function doorVicinityOccupied (world, tileX, tileY, dimensionId = OVERWORLD_ID) |
| 138 | { |
| 139 | for (const entity of world.ecs.query(Component.position)) |
| 140 | { |
| 141 | if (world.ecs.has(entity.handle, Component.dead)) |
| 142 | { continue; } |
| 143 | const pos = entity.position; |
| 144 | const posDim = pos.dimensionId ?? OVERWORLD_ID; |
| 145 | if (posDim !== dimensionId) |
| 146 | { continue; } |
| 147 | if (chebyshev(pos.tileX, pos.tileY, tileX, tileY) <= 1) |
| 148 | { return true; } |
| 149 | } |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /** Restore doors whose holders left the tile and its neighbours. */ |
| 154 | export function stepDoors (world) |
| 155 | { |
| 156 | const held = world.heldDoors; |
| 157 | if (!held?.size) |
| 158 | { return []; } |
| 159 | const events = []; |
| 160 | for (const [key, entry] of [...held.entries()]) |
| 161 | { |
| 162 | const parsed = parseTileKey(key); |
| 163 | const tileX = parsed.tileX; |
| 164 | const tileY = parsed.tileY; |
| 165 | const dimensionId = entry.dimensionId ?? parsed.dimensionId ?? OVERWORLD_ID; |
| 166 | if (!Number.isFinite(tileX) || !Number.isFinite(tileY)) |
| 167 | { |
| 168 | held.delete(key); |
| 169 | continue; |
| 170 | } |
| 171 | if (doorVicinityOccupied(world, tileX, tileY, dimensionId)) |
| 172 | { continue; } |
| 173 | const block = readBlock(world, tileX, tileY, dimensionId); |
| 174 | if (!isDoorBlock(block)) |
| 175 | { |
| 176 | held.delete(key); |
| 177 | continue; |
| 178 | } |
| 179 | const resting = entry.restingState ?? doorRestingState(block) ?? block.state; |
| 180 | if (block.state !== resting) |
| 181 | { |
| 182 | const config = { |
| 183 | ...(block.config && typeof block.config === "object" ? block.config : {}), |
| 184 | restingState: resting |
| 185 | }; |
| 186 | const delta = writeDoorState(world, tileX, tileY, resting, config, dimensionId); |
| 187 | if (delta) |
| 188 | { events.push(delta); } |
| 189 | } |
| 190 | held.delete(key); |
| 191 | } |
| 192 | return events; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * If the next path tile is a door still at resting, flip it for passage. |
| 197 | */ |
| 198 | export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY) |
| 199 | { |
| 200 | if (!entityCanOpenDoors(world.ecs, entityId)) |
| 201 | { return null; } |
| 202 | const handle = world.ecs.handleOf(entityId); |
| 203 | const position = handle != null ? world.ecs.get(handle, Component.position) : null; |
| 204 | const dimensionId = position?.dimensionId ?? OVERWORLD_ID; |
| 205 | const block = readBlock(world, nextTileX, nextTileY, dimensionId); |
| 206 | if (!isDoorBlock(block) || isDoorPassageOpen(block)) |
| 207 | { return null; } |
| 208 | return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId); |
| 209 | } |
| 210 | |
| 211 | export function doorBlocksRangedAttacks (block) |
| 212 | { |
| 213 | if (!isDoorBlock(block)) |
| 214 | { return null; } |
| 215 | return !isDoorPassageOpen(block); |
| 216 | } |