BlueGit

timfa questward

Files

questward / server / src / lighting / baker.js

baker.js

baker.js 2.9 KiB Download Raw
1import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
2import { encodeLight, evaluateLightRay, lightIntensity } from "../../../shared/lighting/raycast.js";
3import { OVERWORLD_ID, normalizeDimensionId } from "../../../shared/world/dimensions.js";
4import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js";
5import { cloneChunkData, collectLightSources, createEmptyChunkData, chunkTileLightOcclusion } from "../world/chunkData.js";
6
7export { bresenhamTiles, encodeLight, falloffAtDistance, lightIntensity, rayReaches } from "../../../shared/lighting/raycast.js";
8
9export function maxDefinitionLightRange (blockDefs, groundDefs = null)
10{
11 let max = 0;
12 for (const def of Object.values(blockDefs ?? {}))
13 { max = Math.max(max, def.lightRangeTiles ?? 0); }
14 if (groundDefs)
15 {
16 for (const def of Object.values(groundDefs))
17 { max = Math.max(max, def.lightRangeTiles ?? 0); }
18 }
19 return max;
20}
21
22export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange, dimensionId = OVERWORLD_ID)
23{
24 const dim = normalizeDimensionId(dimensionId);
25 const halo = maxLightChunkRadius(maxRange);
26 const affected = chebyshevChunks(centerChunkX, centerChunkY, halo);
27 const loaded = new Map();
28
29 function getData (chunkX, chunkY)
30 {
31 const key = chunkKey(chunkX, chunkY, dim);
32 if (!loaded.has(key))
33 { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); }
34 return loaded.get(key);
35 }
36
37 const sources = [];
38 for (const coord of chebyshevChunks(centerChunkX, centerChunkY, halo + 1))
39 {
40 const data = getData(coord.chunkX, coord.chunkY);
41 sources.push(...collectLightSources(coord.chunkX, coord.chunkY, data));
42 }
43
44 function occlusionAt (tileX, tileY)
45 {
46 const data = getData(tileToChunk(tileX), tileToChunk(tileY));
47 return chunkTileLightOcclusion(data, tileToLocal(tileX), tileToLocal(tileY));
48 }
49
50 for (const coord of affected)
51 {
52 const data = getData(coord.chunkX, coord.chunkY);
53 for (let localY = 0; localY < CHUNK_SIZE; localY++)
54 {
55 for (let localX = 0; localX < CHUNK_SIZE; localX++)
56 {
57 const tileX = coord.chunkX * CHUNK_SIZE + localX;
58 const tileY = coord.chunkY * CHUNK_SIZE + localY;
59 const index = localY * CHUNK_SIZE + localX;
60 let best = 0;
61 for (const source of sources)
62 {
63 const geo = Math.hypot(tileX - source.tileX, tileY - source.tileY);
64 if (geo > source.range)
65 { continue; }
66 const ray = evaluateLightRay(source.tileX, source.tileY, tileX, tileY, occlusionAt);
67 if (!ray.reaches)
68 { continue; }
69 best = Math.max(best, encodeLight(geo + ray.extraDistance, source.range));
70 }
71 data.light[index] = best;
72 }
73 }
74 }
75
76 return affected.map((coord) => ({
77 chunkX: coord.chunkX,
78 chunkY: coord.chunkY,
79 dimensionId: dim,
80 data: loaded.get(chunkKey(coord.chunkX, coord.chunkY, dim))
81 }));
82}
83
84export function defaultLightChunk (groundTypeId)
85{
86 return createEmptyChunkData(groundTypeId);
87}