BlueGit

timfa questward

Files

questward / server / tests / lighting.test.js

lighting.test.js

lighting.test.js 6.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 { BlockType, LightOcclusion, blockLightOcclusionForBlock } from "../../shared/definitions/tiles.js";
7import { CHUNK_SIZE } from "../../shared/protocol/constants.js";
8
9test("Bresenham visits start and end tiles", () =>
10{
11 const tiles = bresenhamTiles(0, 0, 2, 1);
12 assert.deepEqual(tiles[0], { x: 0, y: 0 });
13 assert.deepEqual(tiles.at(-1), { x: 2, y: 1 });
14});
15
16test("walls block light rays but can be lit themselves", () =>
17{
18 const blocked = (x, y) => !(x === 1 && y === 0);
19 assert.equal(rayReaches(0, 0, 2, 0, blocked), false);
20 assert.equal(rayReaches(0, 0, 2, 0, () => true), true);
21 const wallAtTarget = (x, y) => !(x === 2 && y === 0);
22 assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true);
23});
24
25test("a torch lights passable tiles, facing walls, and not tiles behind walls", () =>
26{
27 const origin = createEmptyChunkData();
28 origin.open = true;
29 origin.blocks[0] = { typeId: BlockType.torch, config: null };
30 origin.blocks[2] = { typeId: BlockType.stoneWall, config: null };
31
32 const baked = bakeLightMaps(0, 0, (x, y) =>
33 {
34 if (x === 0 && y === 0)
35 { return origin; }
36 return createEmptyChunkData();
37 }, 8);
38
39 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
40 assert.ok(self.light[0] > 0, "torch tile is lit");
41 assert.ok(self.light[1] > 0, "tile beside torch is lit");
42 assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour");
43 assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south");
44 assert.ok(self.light[2] > 0, "wall facing the torch is lit");
45 assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further");
46 assert.equal(self.light[3], 0, "tile behind wall is unlit");
47 assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
48});
49
50test("dynamic lights use the same wall occlusion as baked light", () =>
51{
52 const blocked = (x, y) => !(x === 1 && y === 0);
53 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
54 assert.equal(dynamicLightAtTile(2, 0, sources, blocked), 0);
55 assert.ok(dynamicLightAtTile(1, 0, sources, () => true) > 0);
56 assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0);
57});
58
59test("tables and marketplace counters do not occlude light rays", () =>
60{
61 const origin = createEmptyChunkData();
62 origin.open = true;
63 origin.blocks[0] = { typeId: BlockType.torch, config: null };
64 origin.blocks[1] = { typeId: BlockType.table, state: "pine-0", config: null };
65 origin.blocks[2] = { typeId: BlockType.marketplace, state: "0", config: null };
66
67 const baked = bakeLightMaps(0, 0, (x, y) =>
68 {
69 if (x === 0 && y === 0)
70 { return origin; }
71 return createEmptyChunkData();
72 }, 8);
73
74 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
75 assert.ok(self.light[3] > 0, "light passes through table and counter");
76});
77
78test("crafting stations and ore nodes do not occlude light", () =>
79{
80 const origin = createEmptyChunkData();
81 origin.open = true;
82 origin.blocks[0] = { typeId: BlockType.torch, config: null };
83 origin.blocks[1] = { typeId: BlockType.anvil, config: null };
84 origin.blocks[2] = { typeId: BlockType.furnace, config: null };
85 origin.blocks[3] = { typeId: BlockType.craftingBench, config: null };
86 origin.blocks[4] = { typeId: BlockType.woodworkingBench, config: null };
87 origin.blocks[5] = { typeId: BlockType.boulder, config: null };
88 origin.blocks[6] = { typeId: BlockType.cauldron, config: null };
89
90 const baked = bakeLightMaps(0, 0, (x, y) =>
91 {
92 if (x === 0 && y === 0)
93 { return origin; }
94 return createEmptyChunkData();
95 }, 8);
96
97 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
98 assert.ok(self.light[7] > 0, "light passes through stations and ore");
99});
100
101test("partial occlusion adds falloff distance without stopping the ray", () =>
102{
103 const occlusion = (x, y) => (x === 1 && y === 0
104 ? LightOcclusion.partial
105 : LightOcclusion.clear);
106 const ray = evaluateLightRay(0, 0, 2, 0, occlusion);
107 assert.equal(ray.reaches, true);
108 assert.equal(ray.extraDistance, 1);
109
110 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
111 const throughTree = dynamicLightAtTile(2, 0, sources, occlusion);
112 const open = dynamicLightAtTile(2, 0, sources, () => LightOcclusion.clear);
113 assert.ok(throughTree > 0, "light still reaches past a tree");
114 assert.ok(throughTree < open, "tree canopy darkens falloff vs open path");
115});
116
117test("pine trees partially occlude baked light", () =>
118{
119 const withTree = createEmptyChunkData();
120 withTree.open = true;
121 withTree.blocks[0] = { typeId: BlockType.torch, config: null };
122 withTree.blocks[1] = { typeId: BlockType.pineTree, state: "summer", config: null };
123
124 const open = createEmptyChunkData();
125 open.open = true;
126 open.blocks[0] = { typeId: BlockType.torch, config: null };
127
128 const bakedTree = bakeLightMaps(0, 0, (x, y) =>
129 {
130 if (x === 0 && y === 0)
131 { return withTree; }
132 return createEmptyChunkData();
133 }, 8);
134 const bakedOpen = bakeLightMaps(0, 0, (x, y) =>
135 {
136 if (x === 0 && y === 0)
137 { return open; }
138 return createEmptyChunkData();
139 }, 8);
140
141 const litTree = bakedTree.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
142 const litOpen = bakedOpen.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
143 assert.ok(litTree.light[2] > 0, "light passes through pine canopy");
144 assert.ok(litTree.light[2] < litOpen.light[2], "pine darkens tiles beyond vs open air");
145});
146
147test("tree stumps do not occlude light rays", () =>
148{
149 assert.equal(
150 blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }),
151 LightOcclusion.clear
152 );
153 assert.equal(
154 blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }),
155 LightOcclusion.partial
156 );
157
158 const stump = (x, y) => (x === 1 && y === 0
159 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" })
160 : LightOcclusion.clear);
161 const tree = (x, y) => (x === 1 && y === 0
162 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" })
163 : LightOcclusion.clear);
164 const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }];
165 const pastStump = dynamicLightAtTile(2, 0, sources, stump);
166 const pastTree = dynamicLightAtTile(2, 0, sources, tree);
167 assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree");
168});