Bake static torch light through passable tiles on chunk save.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -57,9 +57,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
57
57
58
58
## Baked lighting
59
59
60
**Status:** not implemented
61
**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask.
62
**Code:** —
60
**Status:** full
61
**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask (encoded intensity). Torch range is 8 tiles.
62
**Code:** `server/src/lighting/baker.js`, tests in `server/tests/lighting.test.js`
63
63
64
64
## World editor (server)
65
65
@@ -57,9 +57,9 @@ Each entry is a short note on behaviour and where the code lives. Update this fi57
57
58
## Baked lighting58
## Baked lighting59
59
60
**Status:** not implemented 60
**Status:** full 61
**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask. 61
**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask (encoded intensity). Torch range is 8 tiles. 62
**Code:** —62
**Code:** `server/src/lighting/baker.js`, tests in `server/tests/lighting.test.js`63
63
64
## World editor (server)64
## World editor (server)65
65
@@ -0,0 +1,129 @@
1
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";
2
import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js";
3
import { cloneChunkData, collectLightSources, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js";
4
5
export 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
37
export 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
54
export 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
62
export 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
126
export function defaultLightChunk (groundTypeId)
127
{
128
return createEmptyChunkData(groundTypeId);
129
}
@@ -0,0 +1,129 @@1
import { CHUNK_SIZE } from "../../../shared/protocol/constants.js";2
import { tileToChunk, tileToLocal, chunkKey, maxLightChunkRadius, chebyshevChunks } from "../world/coordinates.js";3
import { cloneChunkData, collectLightSources, createEmptyChunkData, isChunkTilePassable } from "../world/chunkData.js";4
5
export 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
37
export 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
54
export 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
62
export 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
126
export function defaultLightChunk (groundTypeId)127
{128
return createEmptyChunkData(groundTypeId);129
}@@ -0,0 +1,42 @@
1
import test from "node:test";
2
import assert from "node:assert/strict";
3
import { createEmptyChunkData } from "../src/world/chunkData.js";
4
import { bakeLightMaps, rayReaches, bresenhamTiles } from "../src/lighting/baker.js";
5
import { BlockType } from "../../shared/definitions/tiles.js";
6
import { CHUNK_SIZE } from "../../shared/protocol/constants.js";
7
8
test("Bresenham visits start and end tiles", () =>
9
{
10
const tiles = bresenhamTiles(0, 0, 2, 1);
11
assert.deepEqual(tiles[0], { x: 0, y: 0 });
12
assert.deepEqual(tiles.at(-1), { x: 2, y: 1 });
13
});
14
15
test("walls block light rays", () =>
16
{
17
const blocked = (x, y) => !(x === 1 && y === 0);
18
assert.equal(rayReaches(0, 0, 2, 0, blocked), false);
19
assert.equal(rayReaches(0, 0, 2, 0, () => true), true);
20
});
21
22
test("a torch lights passable tiles and not tiles behind walls", () =>
23
{
24
const origin = createEmptyChunkData();
25
origin.open = true;
26
origin.blocks[0] = { typeId: BlockType.torch, config: null };
27
origin.blocks[2] = { typeId: BlockType.stoneWall, config: null };
28
29
const baked = bakeLightMaps(0, 0, (x, y) =>
30
{
31
if (x === 0 && y === 0)
32
{ return origin; }
33
return createEmptyChunkData();
34
}, 8);
35
36
const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
37
assert.ok(self.light[0] > 0, "torch tile is lit");
38
assert.ok(self.light[1] > 0, "tile beside torch is lit");
39
assert.equal(self.light[2], 0, "wall is unlit");
40
assert.equal(self.light[3], 0, "tile behind wall is unlit");
41
assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
42
});
@@ -0,0 +1,42 @@1
import test from "node:test";2
import assert from "node:assert/strict";3
import { createEmptyChunkData } from "../src/world/chunkData.js";4
import { bakeLightMaps, rayReaches, bresenhamTiles } from "../src/lighting/baker.js";5
import { BlockType } from "../../shared/definitions/tiles.js";6
import { CHUNK_SIZE } from "../../shared/protocol/constants.js";7
8
test("Bresenham visits start and end tiles", () =>9
{10
const tiles = bresenhamTiles(0, 0, 2, 1);11
assert.deepEqual(tiles[0], { x: 0, y: 0 });12
assert.deepEqual(tiles.at(-1), { x: 2, y: 1 });13
});14
15
test("walls block light rays", () =>16
{17
const blocked = (x, y) => !(x === 1 && y === 0);18
assert.equal(rayReaches(0, 0, 2, 0, blocked), false);19
assert.equal(rayReaches(0, 0, 2, 0, () => true), true);20
});21
22
test("a torch lights passable tiles and not tiles behind walls", () =>23
{24
const origin = createEmptyChunkData();25
origin.open = true;26
origin.blocks[0] = { typeId: BlockType.torch, config: null };27
origin.blocks[2] = { typeId: BlockType.stoneWall, config: null };28
29
const baked = bakeLightMaps(0, 0, (x, y) =>30
{31
if (x === 0 && y === 0)32
{ return origin; }33
return createEmptyChunkData();34
}, 8);35
36
const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;37
assert.ok(self.light[0] > 0, "torch tile is lit");38
assert.ok(self.light[1] > 0, "tile beside torch is lit");39
assert.equal(self.light[2], 0, "wall is unlit");40
assert.equal(self.light[3], 0, "tile behind wall is unlit");41
assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);42
});