import { CHUNK_SIZE } from "../protocol/constants.js"; import { BlockType, LightOcclusion, blockDefinition, isBlockPassable, isGroundPassable, normalizeGround } from "../definitions/tiles.js"; import { tileLightOcclusionAt } from "./chunkOcclusion.js"; /** Chebyshev chunk radius for enclosed bake window (3×3 chunks). */ export const ENCLOSED_BAKE_CHUNK_RADIUS = 1; /** * Door tiles are always enclosure boundaries (open or closed). * Uses BlockType.door / def.door — not light-occlusion-of-open-passage. */ export function isEnclosedDoorBlock (block) { if (!block) { return false; } if ((block.typeId | 0) === BlockType.door) { return true; } return !!blockDefinition(block.typeId)?.door; } /** * Boundary = (solid LoS ∧ impassable) ∨ door. * Water (impassable walk, clear LoS) is NOT boundary → interior candidate. * Doors ignore open/closed passability; they are always boundary. */ export function isEnclosedBoundaryTile (grounds, blocks, localX, localY) { const index = localY * CHUNK_SIZE + localX; const block = blocks?.[index] ?? null; if (isEnclosedDoorBlock(block)) { return true; } const solidLos = tileLightOcclusionAt(grounds, blocks, localX, localY) === LightOcclusion.solid; if (!solidLos) { return false; } const ground = normalizeGround(grounds?.[index]); if (!isGroundPassable(ground.typeId)) { return true; } if (!block) { return false; } return !isBlockPassable(block.typeId, block.state); } /** Interior candidate = not a boundary tile. */ export function isEnclosedInteriorCandidate (grounds, blocks, localX, localY) { return !isEnclosedBoundaryTile(grounds, blocks, localX, localY); } /** * Bake enclosed masks for the 3×3 chunk window around `centerChunkX/Y`. * Floods "outside" from the window edge through interior tiles; remaining * interior components (plus their shared-edge boundary tiles) are enclosed. * Regions that touch the window edge as interior are treated as outdoors. * * @param {number} centerChunkX * @param {number} centerChunkY * @param {(chunkX: number, chunkY: number) => { grounds: any[], blocks: any[] } | null | undefined} loadChunk * @returns {{ chunkX: number, chunkY: number, enclosed: number[] }[]} */ export function bakeEnclosedWindow (centerChunkX, centerChunkY, loadChunk) { const radius = ENCLOSED_BAKE_CHUNK_RADIUS; const minChunkX = centerChunkX - radius; const minChunkY = centerChunkY - radius; const maxChunkX = centerChunkX + radius; const maxChunkY = centerChunkY + radius; const winChunks = 2 * radius + 1; const size = winChunks * CHUNK_SIZE; const minTileX = minChunkX * CHUNK_SIZE; const minTileY = minChunkY * CHUNK_SIZE; const chunkCache = new Map(); function chunkAt (chunkX, chunkY) { const key = `${chunkX},${chunkY}`; if (!chunkCache.has(key)) { chunkCache.set(key, loadChunk(chunkX, chunkY) ?? null); } return chunkCache.get(key); } function tileGroundsBlocks (tileX, tileY) { const chunkX = Math.floor(tileX / CHUNK_SIZE); const chunkY = Math.floor(tileY / CHUNK_SIZE); const data = chunkAt(chunkX, chunkY); if (!data) { return null; } const localX = tileX - chunkX * CHUNK_SIZE; const localY = tileY - chunkY * CHUNK_SIZE; return { grounds: data.grounds, blocks: data.blocks, localX, localY }; } const boundary = new Uint8Array(size * size); const interior = new Uint8Array(size * size); for (let wy = 0; wy < size; wy++) { for (let wx = 0; wx < size; wx++) { const tileX = minTileX + wx; const tileY = minTileY + wy; const loc = tileGroundsBlocks(tileX, tileY); const idx = wy * size + wx; if (!loc) { // Missing chunk → treat as open outdoors (interior) so edge flood escapes. interior[idx] = 1; continue; } if (isEnclosedBoundaryTile(loc.grounds, loc.blocks, loc.localX, loc.localY)) { boundary[idx] = 1; } else { interior[idx] = 1; } } } const outside = new Uint8Array(size * size); const queue = []; function trySeed (wx, wy) { const idx = wy * size + wx; if (!interior[idx] || outside[idx]) { return; } outside[idx] = 1; queue.push(idx); } for (let wx = 0; wx < size; wx++) { trySeed(wx, 0); trySeed(wx, size - 1); } for (let wy = 1; wy < size - 1; wy++) { trySeed(0, wy); trySeed(size - 1, wy); } const neighbors = [[1, 0], [-1, 0], [0, 1], [0, -1]]; while (queue.length > 0) { const idx = queue.pop(); const wx = idx % size; const wy = (idx / size) | 0; for (const [dx, dy] of neighbors) { const nx = wx + dx; const ny = wy + dy; if (nx < 0 || ny < 0 || nx >= size || ny >= size) { continue; } const nIdx = ny * size + nx; if (!interior[nIdx] || outside[nIdx]) { continue; } outside[nIdx] = 1; queue.push(nIdx); } } const enclosedWin = new Uint8Array(size * size); for (let idx = 0; idx < size * size; idx++) { if (interior[idx] && !outside[idx]) { enclosedWin[idx] = 1; } } // Expand onto boundary tiles that enclose interiors (8-neighborhood so // rectangular room corners are included; one step only — no wall-strip flood). const encloseNeighbors = [ [1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [1, -1], [-1, 1], [-1, -1] ]; for (let wy = 0; wy < size; wy++) { for (let wx = 0; wx < size; wx++) { const idx = wy * size + wx; if (!enclosedWin[idx] || !interior[idx]) { continue; } for (const [dx, dy] of encloseNeighbors) { const nx = wx + dx; const ny = wy + dy; if (nx < 0 || ny < 0 || nx >= size || ny >= size) { continue; } const nIdx = ny * size + nx; if (boundary[nIdx]) { enclosedWin[nIdx] = 1; } } } } const results = []; for (let cy = minChunkY; cy <= maxChunkY; cy++) { for (let cx = minChunkX; cx <= maxChunkX; cx++) { const enclosed = new Array(CHUNK_SIZE * CHUNK_SIZE).fill(0); const baseWX = (cx - minChunkX) * CHUNK_SIZE; const baseWY = (cy - minChunkY) * CHUNK_SIZE; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const wIdx = (baseWY + localY) * size + (baseWX + localX); enclosed[localY * CHUNK_SIZE + localX] = enclosedWin[wIdx] ? 1 : 0; } } results.push({ chunkX: cx, chunkY: cy, enclosed }); } } return results; } /** Ambient darkness for a tile: base ambient + enclosed bonus, capped at night peak. */ export function enclosedAmbientDarkness (baseAmbient, enclosedFlag, { enclosedDarknessBonus = 0.10, nightDarkness = 0.72 } = {}) { const base = Math.min(1, Math.max(0, baseAmbient ?? 0)); if (!enclosedFlag) { return base; } const bonus = Math.min(1, Math.max(0, enclosedDarknessBonus ?? 0)); const peak = Math.min(1, Math.max(0, nightDarkness ?? 0.72)); return Math.min(peak, base + bonus); }