| 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 | /** Light occlusion for one tile inside a chunk snapshot or editor chunk data. */ |
| 10 | export function tileLightOcclusionAt (grounds, blocks, localX, localY) |
| 11 | { |
| 12 | const index = localY * CHUNK_SIZE + localX; |
| 13 | const ground = normalizeGround(grounds?.[index]); |
| 14 | if (!isGroundPassable(ground.typeId)) |
| 15 | { return LightOcclusion.solid; } |
| 16 | return blockLightOcclusionForBlock(blocks?.[index]); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Pre-bake per-tile light occlusion (0=clear, 1=partial, 2=solid) for a chunk. |
| 21 | * Stored on save so runtime raycasts avoid re-parsing grounds/blocks each step. |
| 22 | */ |
| 23 | export function bakeChunkOcclusion (data) |
| 24 | { |
| 25 | const grounds = data?.grounds; |
| 26 | const blocks = data?.blocks; |
| 27 | const out = new Array(CHUNK_SIZE * CHUNK_SIZE); |
| 28 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 29 | { |
| 30 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 31 | { |
| 32 | const index = localY * CHUNK_SIZE + localX; |
| 33 | out[index] = tileLightOcclusionAt(grounds, blocks, localX, localY); |
| 34 | } |
| 35 | } |
| 36 | return out; |
| 37 | } |
| 38 | |
| 39 | export function ensureChunkOcclusion (data) |
| 40 | { |
| 41 | if (Array.isArray(data?.occlusion) && data.occlusion.length >= CHUNK_SIZE * CHUNK_SIZE) |
| 42 | { return data.occlusion; } |
| 43 | return bakeChunkOcclusion(data); |
| 44 | } |