BlueGit

timfa questward

Files

questward / shared / lighting / tilePassable.js

tilePassable.js

tilePassable.js 1.5 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import {
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 * 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 if (!isGroundPassable(ground.typeId))
31 { return LightOcclusion.solid; }
32 return blockLightOcclusionForBlock(snapshot.blocks?.[index]);
33 };
34}
35
36/**
37 * Legacy boolean clearance (`true` = not solid-occluding).
38 * Prefer {@link makeSnapshotLightOcclusion} for baked/dynamic falloff with partials.
39 */
40export function makeSnapshotPassable (chunks)
41{
42 const occlusionAt = makeSnapshotLightOcclusion(chunks);
43 return (tileX, tileY) => occlusionAt(tileX, tileY) !== LightOcclusion.solid;
44}