BlueGit

timfa questward

Files

questward / server / src / lighting / baker.js

baker.js

baker.js 3.2 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 if (!isPassable(toX, toY))
43 { return false; }
44
45 const tiles = bresenhamTiles(fromX, fromY, toX, toY);
46 for (let i = 1; i < tiles.length - 1; i++)
47 {
48 if (!isPassable(tiles[i].x, tiles[i].y))
49 { return false; }
50 }
51 return true;
52}
53
54export function maxDefinitionLightRange (blockDefs)
55{
56 let max = 0;
57 for (const def of Object.values(blockDefs))
58 { max = Math.max(max, def.lightRangeTiles ?? 0); }
59 return max;
60}
61
62export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange)
63{
64 const halo = maxLightChunkRadius(maxRange);
65 const affected = chebyshevChunks(centerChunkX, centerChunkY, halo);
66 const loaded = new Map();
67
68 function getData (chunkX, chunkY)
69 {
70 const key = chunkKey(chunkX, chunkY);
71 if (!loaded.has(key))
72 { loaded.set(key, cloneChunkData(loadChunk(chunkX, chunkY))); }
73 return loaded.get(key);
74 }
75
76 const sources = [];
77 for (const coord of chebyshevChunks(centerChunkX, centerChunkY, halo + 1))
78 {
79 const data = getData(coord.chunkX, coord.chunkY);
80 sources.push(...collectLightSources(coord.chunkX, coord.chunkY, data));
81 }
82
83 function isPassable (tileX, tileY)
84 {
85 const data = getData(tileToChunk(tileX), tileToChunk(tileY));
86 return isChunkTilePassable(data, tileToLocal(tileX), tileToLocal(tileY));
87 }
88
89 for (const coord of affected)
90 {
91 const data = getData(coord.chunkX, coord.chunkY);
92 for (let localY = 0; localY < CHUNK_SIZE; localY++)
93 {
94 for (let localX = 0; localX < CHUNK_SIZE; localX++)
95 {
96 const tileX = coord.chunkX * CHUNK_SIZE + localX;
97 const tileY = coord.chunkY * CHUNK_SIZE + localY;
98 const index = localY * CHUNK_SIZE + localX;
99 let best = 0;
100 if (isPassable(tileX, tileY))
101 {
102 for (const source of sources)
103 {
104 const dist = Math.hypot(tileX - source.tileX, tileY - source.tileY);
105 if (dist > source.range)
106 { continue; }
107 if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable))
108 {
109 const encoded = Math.max(1, Math.round(255 * (1 - dist / (source.range + 1))));
110 best = Math.max(best, encoded);
111 }
112 }
113 }
114 data.light[index] = best;
115 }
116 }
117 }
118
119 return affected.map((coord) => ({
120 chunkX: coord.chunkX,
121 chunkY: coord.chunkY,
122 data: loaded.get(chunkKey(coord.chunkX, coord.chunkY))
123 }));
124}
125
126export function defaultLightChunk (groundTypeId)
127{
128 return createEmptyChunkData(groundTypeId);
129}