BlueGit

timfa questward

Files

questward / shared / lighting / tilePassable.js

tilePassable.js

tilePassable.js 1.6 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import {
3 blockLightOcclusionForBlock,
4 groundLightOcclusion,
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 * Prefers live block/ground state so runtime door flips are not stuck on a
13 * stale baked occlusion row.
14 */
15export function makeSnapshotLightOcclusion (chunks)
16{
17 return (tileX, tileY) =>
18 {
19 const chunkX = Math.floor(tileX / CHUNK_SIZE);
20 const chunkY = Math.floor(tileY / CHUNK_SIZE);
21 const snapshot = chunks.get(`${chunkX},${chunkY}`);
22 if (!snapshot)
23 { return LightOcclusion.clear; }
24 const localX = tileX - chunkX * CHUNK_SIZE;
25 const localY = tileY - chunkY * CHUNK_SIZE;
26 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
27 { return LightOcclusion.clear; }
28 const index = localY * CHUNK_SIZE + localX;
29 const ground = normalizeGround(snapshot.grounds?.[index]);
30 const groundOcclusion = groundLightOcclusion(ground.typeId);
31 if (groundOcclusion !== LightOcclusion.clear)
32 { return groundOcclusion; }
33 return blockLightOcclusionForBlock(snapshot.blocks?.[index]);
34 };
35}
36
37/**
38 * Legacy boolean clearance (`true` = not solid-occluding).
39 * Prefer {@link makeSnapshotLightOcclusion} for baked/dynamic falloff with partials.
40 */
41export function makeSnapshotPassable (chunks)
42{
43 const occlusionAt = makeSnapshotLightOcclusion(chunks);
44 return (tileX, tileY) => occlusionAt(tileX, tileY) !== LightOcclusion.solid;
45}