BlueGit

timfa questward

Files

questward / shared / lighting / blockLights.js

blockLights.js

blockLights.js 3.1 KiB Download Raw
1/** Authored block + ground lights (torches, furnaces, lava, …) for dynamic lighting / combat. */
2
3import { CHUNK_SIZE } from "../protocol/constants.js";
4import {
5 BLOCK_DEFINITIONS,
6 GROUND_DEFINITIONS,
7 blockLightRange,
8 groundLightRange,
9 normalizeGround
10} from "../definitions/tiles.js";
11
12let cachedMaxRange = null;
13
14/** Max `lightRangeTiles` across block and ground defs (cached). */
15export function maxBlockLightRangeTiles ()
16{
17 if (cachedMaxRange != null)
18 { return cachedMaxRange; }
19 let max = 0;
20 for (const def of Object.values(BLOCK_DEFINITIONS))
21 { max = Math.max(max, def.lightRangeTiles ?? 0); }
22 for (const def of Object.values(GROUND_DEFINITIONS))
23 { max = Math.max(max, def.lightRangeTiles ?? 0); }
24 cachedMaxRange = max;
25 return max;
26}
27
28/**
29 * Collect block light sources from one chunk's blocks array (row-major, CHUNK_SIZE²).
30 * @returns {Array<{ tileX: number, tileY: number, range: number }>}
31 */
32export function collectBlockLightSources (chunkX, chunkY, blocks)
33{
34 const sources = [];
35 if (!Array.isArray(blocks) || blocks.length < CHUNK_SIZE * CHUNK_SIZE)
36 { return sources; }
37 for (let localY = 0; localY < CHUNK_SIZE; localY++)
38 {
39 for (let localX = 0; localX < CHUNK_SIZE; localX++)
40 {
41 const block = blocks[localY * CHUNK_SIZE + localX];
42 const range = blockLightRange(block?.typeId ?? null);
43 if (range > 0)
44 {
45 sources.push({
46 tileX: chunkX * CHUNK_SIZE + localX,
47 tileY: chunkY * CHUNK_SIZE + localY,
48 range
49 });
50 }
51 }
52 }
53 return sources;
54}
55
56/**
57 * Collect ground-emitted light sources (e.g. lava) from one chunk's grounds array.
58 * @returns {Array<{ tileX: number, tileY: number, range: number }>}
59 */
60export function collectGroundLightSources (chunkX, chunkY, grounds)
61{
62 const sources = [];
63 if (!Array.isArray(grounds) || grounds.length < CHUNK_SIZE * CHUNK_SIZE)
64 { return sources; }
65 for (let localY = 0; localY < CHUNK_SIZE; localY++)
66 {
67 for (let localX = 0; localX < CHUNK_SIZE; localX++)
68 {
69 const ground = normalizeGround(grounds[localY * CHUNK_SIZE + localX]);
70 const range = groundLightRange(ground.typeId);
71 if (range > 0)
72 {
73 sources.push({
74 tileX: chunkX * CHUNK_SIZE + localX,
75 tileY: chunkY * CHUNK_SIZE + localY,
76 range
77 });
78 }
79 }
80 }
81 return sources;
82}
83
84/**
85 * Block + ground lights in chunks that can reach (tileX, tileY).
86 * @param {(chunkX: number, chunkY: number) => { blocks?: any[], grounds?: any[] }|null|undefined} loadChunk
87 */
88export function blockLightSourcesNearTile (loadChunk, tileX, tileY, maxRange = maxBlockLightRangeTiles())
89{
90 const range = Math.max(0, maxRange | 0);
91 const halo = Math.ceil(range / CHUNK_SIZE) + 1;
92 const centerX = Math.floor(tileX / CHUNK_SIZE);
93 const centerY = Math.floor(tileY / CHUNK_SIZE);
94 const sources = [];
95 for (let dy = -halo; dy <= halo; dy++)
96 {
97 for (let dx = -halo; dx <= halo; dx++)
98 {
99 const chunkX = centerX + dx;
100 const chunkY = centerY + dy;
101 const data = loadChunk(chunkX, chunkY);
102 if (data?.blocks)
103 { sources.push(...collectBlockLightSources(chunkX, chunkY, data.blocks)); }
104 if (data?.grounds)
105 { sources.push(...collectGroundLightSources(chunkX, chunkY, data.grounds)); }
106 }
107 }
108 return sources;
109}