| 1 | import { CHUNK_SIZE } from "../protocol/constants.js"; |
| 2 | import { |
| 3 | blockLightOcclusionForBlock, |
| 4 | isGroundPassable, |
| 5 | LightOcclusion, |
| 6 | normalizeGround |
| 7 | } from "../definitions/tiles.js"; |
| 8 | |
| 9 | /** |
| 10 | * Light occlusion lookup from a chunk snapshot map keyed by "chunkX,chunkY". |
| 11 | * Returns {@link LightOcclusion} levels (not movement passability). |
| 12 | */ |
| 13 | export function makeSnapshotLightOcclusion (chunks) |
| 14 | { |
| 15 | return (tileX, tileY) => |
| 16 | { |
| 17 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 18 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 19 | const snapshot = chunks.get(`${chunkX},${chunkY}`); |
| 20 | if (!snapshot) |
| 21 | { return LightOcclusion.clear; } |
| 22 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 23 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 24 | const index = localY * CHUNK_SIZE + localX; |
| 25 | const ground = normalizeGround(snapshot.grounds?.[index]); |
| 26 | if (!isGroundPassable(ground.typeId)) |
| 27 | { return LightOcclusion.solid; } |
| 28 | const block = snapshot.blocks?.[index]; |
| 29 | return blockLightOcclusionForBlock(block); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Legacy boolean clearance (`true` = not solid-occluding). |
| 35 | * Prefer {@link makeSnapshotLightOcclusion} for baked/dynamic falloff with partials. |
| 36 | */ |
| 37 | export function makeSnapshotPassable (chunks) |
| 38 | { |
| 39 | const occlusionAt = makeSnapshotLightOcclusion(chunks); |
| 40 | return (tileX, tileY) => occlusionAt(tileX, tileY) !== LightOcclusion.solid; |
| 41 | } |