import { Component } from "../ecs/components.js"; import { mobDefinition, isHumanoidMob } from "../../../shared/definitions/mobs.js"; import { BlockType } from "../../../shared/definitions/tiles.js"; import { flipDoorState, isDoorPassageOpen, doorRestingState, parseDoorState } from "../../../shared/world/doors.js"; import { tileToChunk, tileToLocal, tileKey, parseTileKey } from "../world/coordinates.js"; import { CHUNK_SIZE } from "../../../shared/protocol/constants.js"; import { OVERWORLD_ID } from "../../../shared/world/dimensions.js"; import { tileLightOcclusionAt } from "../../../shared/lighting/chunkOcclusion.js"; import { entityDoorRequirementsAllow } from "./conditionContext.js"; export function isDoorTypeId (typeId) { return (typeId | 0) === BlockType.door; } export function isDoorBlock (block) { return isDoorTypeId(block?.typeId); } /** * Players always open doors; humanoid bodies by default; * set `canOpenDoors: true|false` on a mob def to override. */ export function entityCanOpenDoors (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return false; } if (ecs.get(handle, Component.character)) { return true; } const creature = ecs.get(handle, Component.creature); if (!creature) { return false; } const def = mobDefinition(creature.mobTypeId); if (!def) { return false; } if (def.canOpenDoors === true) { return true; } if (def.canOpenDoors === false) { return false; } return isHumanoidMob(creature.mobTypeId) || !!def.usePlayerBody; } function ensureHeldDoors (world) { if (!world.heldDoors) { world.heldDoors = new Map(); } return world.heldDoors; } function readBlock (world, tileX, tileY, dimensionId = OVERWORLD_ID) { const data = world.chunkData?.(tileToChunk(tileX), tileToChunk(tileY), dimensionId); return data?.blocks?.[tileToLocal(tileY) * CHUNK_SIZE + tileToLocal(tileX)] ?? null; } function writeDoorState (world, tileX, tileY, state, config, dimensionId = OVERWORLD_ID) { const chunkX = tileToChunk(tileX); const chunkY = tileToChunk(tileY); const localX = tileToLocal(tileX); const localY = tileToLocal(tileY); const data = world.chunkData?.(chunkX, chunkY, dimensionId); const index = localY * CHUNK_SIZE + localX; const prev = data?.blocks?.[index]; if (!prev || !isDoorTypeId(prev.typeId)) { return null; } const block = { typeId: BlockType.door, state, config: config ?? prev.config ?? null }; data.blocks[index] = block; if (Array.isArray(data.occlusion) && data.occlusion.length > index) { data.occlusion[index] = tileLightOcclusionAt(data.grounds, data.blocks, localX, localY); } return { type: "chunk-delta", chunkX, chunkY, tileX, tileY, localX, localY, dimensionId, block }; } /** * Flip the door away from its resting (authored) state so a canOpenDoors entity * can pass. Resting is remembered until the vicinity clears. */ export function holdDoorForPassage (world, tileX, tileY, dimensionId = OVERWORLD_ID) { const block = readBlock(world, tileX, tileY, dimensionId); if (!isDoorBlock(block)) { return null; } const parsed = parseDoorState(block.state); if (!parsed) { return null; } const held = ensureHeldDoors(world); const key = tileKey(tileX, tileY, dimensionId); let restingState = held.get(key)?.restingState ?? doorRestingState(block); if (!parseDoorState(restingState)) { restingState = block.state; } if (!held.has(key)) { held.set(key, { restingState, dimensionId }); } /** Already flipped for passage — keep held, no visual change. */ if (block.state !== restingState) { return null; } const next = flipDoorState(restingState); if (!next || next === block.state) { return null; } const config = { ...(block.config && typeof block.config === "object" ? block.config : {}), restingState // Reserved for later: keyItemId, unlockNpcId, tollCoins, forcedPassage }; return writeDoorState(world, tileX, tileY, next, config, dimensionId); } function chebyshev (ax, ay, bx, by) { return Math.max(Math.abs(ax - bx), Math.abs(ay - by)); } /** Any living body on the door or Chebyshev-adjacent keeps the held state. */ function doorVicinityOccupied (world, tileX, tileY, dimensionId = OVERWORLD_ID) { for (const entity of world.ecs.query(Component.position)) { if (world.ecs.has(entity.handle, Component.dead)) { continue; } const pos = entity.position; const posDim = pos.dimensionId ?? OVERWORLD_ID; if (posDim !== dimensionId) { continue; } if (chebyshev(pos.tileX, pos.tileY, tileX, tileY) <= 1) { return true; } } return false; } /** Restore doors whose holders left the tile and its neighbours. */ export function stepDoors (world) { const held = world.heldDoors; if (!held?.size) { return []; } const events = []; for (const [key, entry] of [...held.entries()]) { const parsed = parseTileKey(key); const tileX = parsed.tileX; const tileY = parsed.tileY; const dimensionId = entry.dimensionId ?? parsed.dimensionId ?? OVERWORLD_ID; if (!Number.isFinite(tileX) || !Number.isFinite(tileY)) { held.delete(key); continue; } if (doorVicinityOccupied(world, tileX, tileY, dimensionId)) { continue; } const block = readBlock(world, tileX, tileY, dimensionId); if (!isDoorBlock(block)) { held.delete(key); continue; } const resting = entry.restingState ?? doorRestingState(block) ?? block.state; if (block.state !== resting) { const config = { ...(block.config && typeof block.config === "object" ? block.config : {}), restingState: resting }; const delta = writeDoorState(world, tileX, tileY, resting, config, dimensionId); if (delta) { events.push(delta); } } held.delete(key); } return events; } /** * If the next path tile is a door still at resting, flip it for passage. */ export function maybeOpenDoorOnApproach (world, entityId, nextTileX, nextTileY) { if (!entityCanOpenDoors(world.ecs, entityId)) { return null; } if (!entityDoorRequirementsAllow(world, entityId, nextTileX, nextTileY)) { return null; } const handle = world.ecs.handleOf(entityId); const position = handle != null ? world.ecs.get(handle, Component.position) : null; const dimensionId = position?.dimensionId ?? OVERWORLD_ID; const block = readBlock(world, nextTileX, nextTileY, dimensionId); if (!isDoorBlock(block) || isDoorPassageOpen(block)) { return null; } return holdDoorForPassage(world, nextTileX, nextTileY, dimensionId); } export function doorBlocksRangedAttacks (block) { if (!isDoorBlock(block)) { return null; } return !isDoorPassageOpen(block); }