BlueGit

timfa questward

Files

questward / server / src / lighting / baker.js

baker.js

baker.js 3.6 KiB Download Raw
1import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
2import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js";
3import { cloneChunkData, collectLightSources, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js";
4
5export function bresenhamTiles (x0, y0, x1, y1)
6{
7 const tiles = [];
8 let x = x0;
9 let y = y0;
10 const dx = Math.abs(x1 - x0);
11 const dy = Math.abs(y1 - y0);
12 const sx = x0 < x1 ? 1 : -1;
13 const sy = y0 < y1 ? 1 : -1;
14 let err = dx - dy;
15
16 while (true)
17 {
18 tiles.push({ x, y });
19 if (x === x1 && y === y1)
20 { break; }
21
22 const e2 = 2 * err;
23 if (e2 > -dy)
24 {
25 err -= dy;
26 x += sx;
27 }
28 if (e2 < dx)
29 {
30 err += dx;
31 y += sy;
32 }
33 }
34 return tiles;
35}
36
37export function rayReaches (fromX, fromY, toX, toY, isPassable)
38{
39 if (fromX === toX && fromY === toY)
40 { return isPassable(toX, toY); }
41
42 const tiles = bresenhamTiles(fromX, fromY, toX, toY);
43 // Only tiles strictly between source and target can occlude. The destination
44 // may be an impassable block that still catches light on its facing surface.
45 for (let i = 1; i < tiles.length - 1; i++)
46 {
47 if (!isPassable(tiles[i].x, tiles[i].y))
48 { return false; }
49 }
50 return true;
51}
52
53export function falloffAtDistance (dist, range)
54{
55 if (range <= 0 || dist > range)
56 { return 0; }
57
58 const t = 1 - dist / range;
59 return t * t * (3 - 2 * t);
60}
61
62export function encodeLight (dist, range)
63{
64 const falloff = falloffAtDistance(dist, range);
65 if (falloff <= 0)
66 { return 0; }
67
68 return Math.max(1, Math.round(255 * falloff));
69}
70
71export function lightIntensity (encoded)
72{
73 return Math.min(1, Math.max(0, (encoded ?? 0) / 255));
74}
75
76export function maxDefinitionLightRange (blockDefs)
77{
78 let max = 0;
79 for (const def of Object.values(blockDefs))
80 { max = Math.max(max, def.lightRangeTiles ?? 0); }
81 return max;
82}
83
84export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange)
85{
86 const halo = maxLightChunkRadius(maxRange);
87 const affected = chebyshevChunks(centerChunkX, centerChunkY, halo);
88 const loaded = new Map();
89
90 function getData (chunkX, chunkY)
91 {
92 const key = chunkKey(chunkX, chunkY);
93 if (!loaded.has(key))
94 { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); }
95 return loaded.get(key);
96 }
97
98 const sources = [];
99 for (const coord of chebyshevChunks(centerChunkX, centerChunkY, halo + 1))
100 {
101 const data = getData(coord.chunkX, coord.chunkY);
102 sources.push(...collectLightSources(coord.chunkX, coord.chunkY, data));
103 }
104
105 function isPassable (tileX, tileY)
106 {
107 const data = getData(tileToChunk(tileX), tileToChunk(tileY));
108 return isChunkTilePassable(data, tileToLocal(tileX), tileToLocal(tileY));
109 }
110
111 for (const coord of affected)
112 {
113 const data = getData(coord.chunkX, coord.chunkY);
114 for (let localY = 0; localY < CHUNK_SIZE; localY++)
115 {
116 for (let localX = 0; localX < CHUNK_SIZE; localX++)
117 {
118 const tileX = coord.chunkX * CHUNK_SIZE + localX;
119 const tileY = coord.chunkY * CHUNK_SIZE + localY;
120 const index = localY * CHUNK_SIZE + localX;
121 let best = 0;
122 for (const source of sources)
123 {
124 const dist = Math.hypot(tileX - source.tileX, tileY - source.tileY);
125 if (dist > source.range)
126 { continue; }
127 if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable))
128 {
129 best = Math.max(best, encodeLight(dist, source.range));
130 }
131 }
132 data.light[index] = best;
133 }
134 }
135 }
136
137 return affected.map((coord) => ({
138 chunkX: coord.chunkX,
139 chunkY: coord.chunkY,
140 data: loaded.get(chunkKey(coord.chunkX, coord.chunkY))
141 }));
142}
143
144export function defaultLightChunk (groundTypeId)
145{
146 return createEmptyChunkData(groundTypeId);
147}