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