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