BlueGit

timfa questward

Files

questward / server / tests / lighting.test.js

lighting.test.js

3b2d2eaa History
lighting.test.js 12.5 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { createEmptyChunkData } from "../src/world/chunkData.js";
4import { bakeLightMaps } from "../src/lighting/baker.js";
5import { bresenhamTiles, dynamicLightAtTile, evaluateLightRay, rayReaches } from "../../shared/lighting/raycast.js";
6import { makeSnapshotLightOcclusion } from "../../shared/lighting/tilePassable.js";
7import { collectBlockLightSources, collectGroundLightSources } from "../../shared/lighting/blockLights.js";
8import { tileLightOcclusionAt, bakeChunkOcclusion } from "../../shared/lighting/chunkOcclusion.js";
9import { BlockType, GroundType, blockLightOcclusionForBlock, LightOcclusion } from "../../shared/definitions/tiles.js";
10import { CHUNK_SIZE } from "../../shared/protocol/constants.js";
11import { tileLightAt } from "../src/combat/service.js";
12import { maybeOpenDoorOnApproach } from "../src/doors/service.js";
13import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
14
15test("Bresenham visits start and end tiles", () =>
16{
17 const tiles = bresenhamTiles(0, 0, 2, 1);
18 assert.deepEqual(tiles[0], { x: 0, y: 0 });
19 assert.deepEqual(tiles.at(-1), { x: 2, y: 1 });
20});
21
22test("walls block light rays but can be lit themselves", () =>
23{
24 const blocked = (x, y) => !(x === 1 && y === 0);
25 assert.equal(rayReaches(0, 0, 2, 0, blocked), false);
26 assert.equal(rayReaches(0, 0, 2, 0, () => true), true);
27 const wallAtTarget = (x, y) => !(x === 2 && y === 0);
28 assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true);
29});
30
31test("a torch lights passable tiles, facing walls, and not tiles behind walls", () =>
32{
33 const origin = createEmptyChunkData();
34 origin.open = true;
35 origin.blocks[0] = { typeId: BlockType.torch, config: null };
36 origin.blocks[2] = { typeId: BlockType.stoneWall, config: null };
37
38 const baked = bakeLightMaps(0, 0, (x, y) =>
39 {
40 if (x === 0 && y === 0)
41 { return origin; }
42 return createEmptyChunkData();
43 }, 8);
44
45 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
46 assert.ok(self.light[0] > 0, "torch tile is lit");
47 assert.ok(self.light[1] > 0, "tile beside torch is lit");
48 assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour");
49 assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south");
50 assert.ok(self.light[2] > 0, "wall facing the torch is lit");
51 assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further");
52 assert.equal(self.light[3], 0, "tile behind wall is unlit");
53 assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
54});
55
56test("dynamic lights use the same wall occlusion as baked light", () =>
57{
58 const blocked = (x, y) => !(x === 1 && y === 0);
59 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
60 assert.equal(dynamicLightAtTile(2, 0, sources, blocked), 0);
61 assert.ok(dynamicLightAtTile(1, 0, sources, () => true) > 0);
62 assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0);
63});
64
65test("dynamic lights skip rays beyond range before occlusion work", () =>
66{
67 let occlusionCalls = 0;
68 const occlusion = () =>
69 {
70 occlusionCalls += 1;
71 return LightOcclusion.clear;
72 };
73 const sources = [{ tileX: 0, tileY: 0, range: 4, intensity: 1 }];
74 assert.equal(dynamicLightAtTile(10, 0, sources, occlusion), 0);
75 assert.equal(occlusionCalls, 0, "out-of-range tiles must not run LOS");
76 // Two steps away → one intermediate tile, so occlusion is consulted.
77 assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0);
78 assert.ok(occlusionCalls > 0);
79});
80
81test("evaluateLightRay matches Bresenham intermediates without allocating the path", () =>
82{
83 const blocked = new Set(["1,0"]);
84 const occlusion = (x, y) => (blocked.has(`${x},${y}`)
85 ? LightOcclusion.solid
86 : LightOcclusion.clear);
87 assert.equal(evaluateLightRay(0, 0, 2, 0, occlusion).reaches, false);
88 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).reaches, true);
89 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).extraDistance, 0);
90 const partial = (x, y) => (x === 1 && y === 0 ? LightOcclusion.partial : LightOcclusion.clear);
91 assert.equal(evaluateLightRay(0, 0, 2, 0, partial).extraDistance, 1);
92});
93
94test("tables and marketplace counters do not occlude light rays", () =>
95{
96 const origin = createEmptyChunkData();
97 origin.open = true;
98 origin.blocks[0] = { typeId: BlockType.torch, config: null };
99 origin.blocks[1] = { typeId: BlockType.table, state: "pine-0", config: null };
100 origin.blocks[2] = { typeId: BlockType.marketplace, state: "0", config: null };
101
102 const baked = bakeLightMaps(0, 0, (x, y) =>
103 {
104 if (x === 0 && y === 0)
105 { return origin; }
106 return createEmptyChunkData();
107 }, 8);
108
109 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
110 assert.ok(self.light[3] > 0, "light passes through table and counter");
111});
112
113test("crafting stations and ore nodes do not occlude light", () =>
114{
115 const origin = createEmptyChunkData();
116 origin.open = true;
117 origin.blocks[0] = { typeId: BlockType.torch, config: null };
118 origin.blocks[1] = { typeId: BlockType.anvil, config: null };
119 origin.blocks[2] = { typeId: BlockType.furnace, config: null };
120 origin.blocks[3] = { typeId: BlockType.craftingBench, config: null };
121 origin.blocks[4] = { typeId: BlockType.woodworkingBench, config: null };
122 origin.blocks[5] = { typeId: BlockType.boulder, config: null };
123 origin.blocks[6] = { typeId: BlockType.cauldron, config: null };
124
125 const baked = bakeLightMaps(0, 0, (x, y) =>
126 {
127 if (x === 0 && y === 0)
128 { return origin; }
129 return createEmptyChunkData();
130 }, 8);
131
132 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
133 assert.ok(self.light[7] > 0, "light passes through stations and ore");
134});
135
136test("partial occlusion adds falloff distance without stopping the ray", () =>
137{
138 const occlusion = (x, y) => (x === 1 && y === 0
139 ? LightOcclusion.partial
140 : LightOcclusion.clear);
141 const ray = evaluateLightRay(0, 0, 2, 0, occlusion);
142 assert.equal(ray.reaches, true);
143 assert.equal(ray.extraDistance, 1);
144
145 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
146 const throughTree = dynamicLightAtTile(2, 0, sources, occlusion);
147 const open = dynamicLightAtTile(2, 0, sources, () => LightOcclusion.clear);
148 assert.ok(throughTree > 0, "light still reaches past a tree");
149 assert.ok(throughTree < open, "tree canopy darkens falloff vs open path");
150});
151
152test("pine trees partially occlude baked light", () =>
153{
154 const withTree = createEmptyChunkData();
155 withTree.open = true;
156 withTree.blocks[0] = { typeId: BlockType.torch, config: null };
157 withTree.blocks[1] = { typeId: BlockType.pineTree, state: "summer", config: null };
158
159 const open = createEmptyChunkData();
160 open.open = true;
161 open.blocks[0] = { typeId: BlockType.torch, config: null };
162
163 const bakedTree = bakeLightMaps(0, 0, (x, y) =>
164 {
165 if (x === 0 && y === 0)
166 { return withTree; }
167 return createEmptyChunkData();
168 }, 8);
169 const bakedOpen = bakeLightMaps(0, 0, (x, y) =>
170 {
171 if (x === 0 && y === 0)
172 { return open; }
173 return createEmptyChunkData();
174 }, 8);
175
176 const litTree = bakedTree.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
177 const litOpen = bakedOpen.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
178 assert.ok(litTree.light[2] > 0, "light passes through pine canopy");
179 assert.ok(litTree.light[2] < litOpen.light[2], "pine darkens tiles beyond vs open air");
180});
181
182test("tree stumps do not occlude light rays", () =>
183{
184 assert.equal(
185 blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }),
186 LightOcclusion.clear
187 );
188 assert.equal(
189 blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }),
190 LightOcclusion.partial
191 );
192
193 const stump = (x, y) => (x === 1 && y === 0
194 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" })
195 : LightOcclusion.clear);
196 const tree = (x, y) => (x === 1 && y === 0
197 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" })
198 : LightOcclusion.clear);
199 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
200 const pastStump = dynamicLightAtTile(2, 0, sources, stump);
201 const pastTree = dynamicLightAtTile(2, 0, sources, tree);
202 assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree");
203});
204
205test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOcclusion", async () =>
206{
207 const { encodeChunkBlob, decodeChunkBlob, chunkTileLightOcclusion } = await import("../src/world/chunkData.js");
208 const data = createEmptyChunkData();
209 data.open = true;
210 data.blocks[0] = { typeId: BlockType.stoneWall, config: null };
211 const blob = encodeChunkBlob(data);
212 const decoded = decodeChunkBlob(blob);
213 assert.equal(decoded.occlusion.length, CHUNK_SIZE * CHUNK_SIZE);
214 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
215 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
216});
217
218test("lit corner blend does not darken daylight beside occluding walls", async () =>
219{
220 const { smoothLitCorner } = await import("../../shared/lighting/lightCorners.js");
221 const dayOpen = smoothLitCorner({
222 homeLit: 1,
223 homeBlocks: false,
224 aLit: 1,
225 aBlocks: false,
226 bLit: 0.2,
227 bBlocks: true,
228 dLit: 0,
229 dBlocks: true
230 });
231 assert.equal(dayOpen, 1);
232 const nightDarkBesideLitWall = smoothLitCorner({
233 homeLit: 0,
234 homeBlocks: false,
235 aLit: 0,
236 aBlocks: false,
237 bLit: 0.8,
238 bBlocks: true,
239 dLit: 0.5,
240 dBlocks: true
241 });
242 assert.equal(nightDarkBesideLitWall, 0);
243 const openBlend = smoothLitCorner({
244 homeLit: 1,
245 homeBlocks: false,
246 aLit: 0.5,
247 aBlocks: false,
248 bLit: 0.5,
249 bBlocks: false,
250 dLit: 0.25,
251 dBlocks: false
252 });
253 assert.equal(openBlend, (1 + 0.5 + 0.5 + 0.25) / 4);
254});
255
256test("collectBlockLightSources finds torches", () =>
257{
258 const data = createEmptyChunkData();
259 data.blocks[0] = { typeId: BlockType.torch, config: null };
260 const sources = collectBlockLightSources(0, 0, data.blocks);
261 assert.equal(sources.length, 1);
262 assert.equal(sources[0].tileX, 0);
263 assert.equal(sources[0].tileY, 0);
264 assert.ok(sources[0].range > 0);
265});
266
267test("collectGroundLightSources finds lava", () =>
268{
269 const data = createEmptyChunkData();
270 data.grounds[0] = { typeId: GroundType.lava, state: null };
271 const sources = collectGroundLightSources(0, 0, data.grounds);
272 assert.equal(sources.length, 1);
273 assert.equal(sources[0].tileX, 0);
274 assert.equal(sources[0].tileY, 0);
275 assert.equal(sources[0].range, 4);
276});
277
278test("dynamic block light is blocked by a resting door and passes when passage-open", () =>
279{
280 const chunks = new Map();
281 const data = createEmptyChunkData();
282 data.open = true;
283 data.blocks[0] = { typeId: BlockType.torch, config: null };
284 data.blocks[1] = { typeId: BlockType.door, state: "pine-s-closed", config: null };
285 data.occlusion = bakeChunkOcclusion(data);
286 chunks.set("0,0", { chunkX: 0, chunkY: 0, grounds: data.grounds, blocks: data.blocks, occlusion: data.occlusion });
287
288 const occlusion = makeSnapshotLightOcclusion(chunks);
289 const sources = collectBlockLightSources(0, 0, data.blocks);
290 assert.equal(dynamicLightAtTile(2, 0, sources, occlusion), 0, "closed door blocks torch");
291
292 data.blocks[1] = {
293 typeId: BlockType.door,
294 state: "pine-s-open",
295 config: { restingState: "pine-s-closed" }
296 };
297 data.occlusion[1] = tileLightOcclusionAt(data.grounds, data.blocks, 1, 0);
298 assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0, "passage-open door lets torch through");
299});
300
301test("tileLightAt at night rises when a door opens between torch and target", () =>
302{
303 const harness = makeSimulation({ workerCount: 1 });
304 try
305 {
306 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
307 harness.simulation.spawnCharacter(1, character);
308 harness.simulation.step();
309 const record = harness.simulation.entityRecord(character.character_id);
310 const worker = harness.simulation.workers[record.workerId];
311 worker.world.worldTime = () => ({ hour: 0 });
312
313 const data = worker.world.chunkData(0, 0);
314 const torchIndex = 8 * CHUNK_SIZE + 8;
315 const doorIndex = 8 * CHUNK_SIZE + 9;
316 data.blocks[torchIndex] = { typeId: BlockType.torch, config: null };
317 data.blocks[doorIndex] = { typeId: BlockType.door, state: "pine-s-closed", config: null };
318 data.occlusion[torchIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 8, 8);
319 data.occlusion[doorIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 9, 8);
320
321 const closed = tileLightAt(worker.world, 10, 8);
322 const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8);
323 assert.ok(opened);
324 const open = tileLightAt(worker.world, 10, 8);
325 assert.ok(open > closed + 0.05, `expected open (${open}) brighter than closed (${closed})`);
326 }
327 finally
328 { harness.close(); }
329});