BlueGit

timfa questward

Files

questward / server / tests / enclosed.test.js

enclosed.test.js

enclosed.test.js 8.6 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js";
4import { bakeEnclosedMaps } from "../src/lighting/enclosedBaker.js";
5import {
6 isEnclosedBoundaryTile,
7 isEnclosedInteriorCandidate,
8 bakeEnclosedWindow,
9 enclosedAmbientDarkness
10} from "../../shared/lighting/enclosed.js";
11import { BlockType, GroundType } from "../../shared/definitions/tiles.js";
12import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../shared/protocol/constants.js";
13
14function idx (localX, localY)
15{
16 return localY * CHUNK_SIZE + localX;
17}
18
19function wallRect (data, x0, y0, x1, y1)
20{
21 for (let y = y0; y <= y1; y++)
22 {
23 for (let x = x0; x <= x1; x++)
24 {
25 if (x === x0 || x === x1 || y === y0 || y === y1)
26 { data.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; }
27 }
28 }
29}
30
31function loadMap (chunks)
32{
33 return (cx, cy) => chunks.get(`${cx},${cy}`) ?? createEmptyChunkData();
34}
35
36function enclosedAt (results, chunkX, chunkY, localX, localY)
37{
38 const entry = results.find((r) => r.chunkX === chunkX && r.chunkY === chunkY);
39 assert.ok(entry, `missing enclosed result for ${chunkX},${chunkY}`);
40 return entry.enclosed[idx(localX, localY)];
41}
42
43test("predicates: open floor is interior; wall is boundary; water is interior; door is boundary", () =>
44{
45 const data = createEmptyChunkData();
46 data.blocks[idx(1, 1)] = { typeId: BlockType.stoneWall, config: null };
47 data.grounds[idx(2, 2)] = { typeId: GroundType.water, state: null };
48 data.blocks[idx(3, 3)] = { typeId: BlockType.door, state: "pine-s-open", config: { restingState: "pine-s-closed" } };
49 data.blocks[idx(4, 4)] = { typeId: BlockType.door, state: "pine-s-closed", config: null };
50
51 assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 0, 0), false);
52 assert.equal(isEnclosedInteriorCandidate(data.grounds, data.blocks, 0, 0), true);
53 assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 1, 1), true);
54 assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 2, 2), false, "water is not boundary");
55 assert.equal(isEnclosedInteriorCandidate(data.grounds, data.blocks, 2, 2), true);
56 assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 3, 3), true, "open door is boundary");
57 assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 4, 4), true, "closed door is boundary");
58});
59
60test("open field → nothing enclosed", () =>
61{
62 const origin = createEmptyChunkData();
63 origin.open = true;
64 const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]])));
65 const self = results.find((r) => r.chunkX === 0 && r.chunkY === 0);
66 assert.ok(self.enclosed.every((v) => v === 0));
67});
68
69test("walled room with door → interior + walls + door marked", () =>
70{
71 const origin = createEmptyChunkData();
72 origin.open = true;
73 // Room at local 4..8, door on south wall at (6,8)
74 wallRect(origin, 4, 4, 8, 8);
75 origin.blocks[idx(6, 8)] = { typeId: BlockType.door, state: "pine-s-closed", config: null };
76
77 const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]])));
78 assert.equal(enclosedAt(results, 0, 0, 6, 6), 1, "interior marked");
79 assert.equal(enclosedAt(results, 0, 0, 5, 5), 1, "interior near corner marked");
80 assert.equal(enclosedAt(results, 0, 0, 4, 6), 1, "west wall (shared edge) marked");
81 assert.equal(enclosedAt(results, 0, 0, 6, 4), 1, "north wall (shared edge) marked");
82 assert.equal(enclosedAt(results, 0, 0, 6, 8), 1, "door marked");
83 assert.equal(enclosedAt(results, 0, 0, 4, 4), 1, "outer wall corner marked via diagonal");
84 assert.equal(enclosedAt(results, 0, 0, 0, 0), 0, "outside not marked");
85 assert.equal(enclosedAt(results, 0, 0, 6, 9), 0, "just outside door not marked");
86});
87
88test("1×1 wall and 3×1 wall strip → nothing enclosed", () =>
89{
90 const origin = createEmptyChunkData();
91 origin.open = true;
92 origin.blocks[idx(8, 8)] = { typeId: BlockType.stoneWall, config: null };
93 origin.blocks[idx(10, 8)] = { typeId: BlockType.stoneWall, config: null };
94 origin.blocks[idx(11, 8)] = { typeId: BlockType.stoneWall, config: null };
95 origin.blocks[idx(12, 8)] = { typeId: BlockType.stoneWall, config: null };
96
97 const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]])));
98 const self = results.find((r) => r.chunkX === 0 && r.chunkY === 0);
99 assert.ok(self.enclosed.every((v) => v === 0), "lone walls must not mark enclosed");
100});
101
102test("water inside courtyard counts as enclosed interior", () =>
103{
104 const origin = createEmptyChunkData();
105 origin.open = true;
106 wallRect(origin, 4, 4, 10, 10);
107 origin.grounds[idx(7, 7)] = { typeId: GroundType.water, state: null };
108 origin.grounds[idx(7, 8)] = { typeId: GroundType.water, state: null };
109
110 const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]])));
111 assert.equal(enclosedAt(results, 0, 0, 7, 7), 1, "water tile enclosed");
112 assert.equal(enclosedAt(results, 0, 0, 7, 8), 1, "water tile enclosed");
113 assert.equal(enclosedAt(results, 0, 0, 6, 6), 1, "dry interior enclosed");
114});
115
116test("cross-chunk room within 3×3 is enclosed", () =>
117{
118 const a = createEmptyChunkData();
119 const b = createEmptyChunkData();
120 a.open = true;
121 b.open = true;
122 // Room straddling chunk 0,0 and 1,0: walls from localX 12..15 on chunk0 and 0..3 on chunk1, y 4..8
123 for (let y = 4; y <= 8; y++)
124 {
125 for (let x = 12; x < CHUNK_SIZE; x++)
126 {
127 if (x === 12 || y === 4 || y === 8)
128 { a.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; }
129 }
130 for (let x = 0; x <= 3; x++)
131 {
132 if (x === 3 || y === 4 || y === 8)
133 { b.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; }
134 }
135 }
136 // Close east/west already; seal was on vertical edges. Fill north/south across seam.
137 for (let x = 13; x < CHUNK_SIZE; x++)
138 {
139 a.blocks[idx(x, 4)] = { typeId: BlockType.stoneWall, config: null };
140 a.blocks[idx(x, 8)] = { typeId: BlockType.stoneWall, config: null };
141 }
142 for (let x = 0; x <= 2; x++)
143 {
144 b.blocks[idx(x, 4)] = { typeId: BlockType.stoneWall, config: null };
145 b.blocks[idx(x, 8)] = { typeId: BlockType.stoneWall, config: null };
146 }
147
148 const chunks = new Map([["0,0", a], ["1,0", b]]);
149 const results = bakeEnclosedWindow(0, 0, loadMap(chunks));
150 assert.equal(enclosedAt(results, 0, 0, 14, 6), 1, "interior on chunk 0");
151 assert.equal(enclosedAt(results, 1, 0, 1, 6), 1, "interior on chunk 1");
152 assert.equal(enclosedAt(results, 0, 0, 12, 6), 1, "west wall marked");
153 assert.equal(enclosedAt(results, 1, 0, 3, 6), 1, "east wall marked");
154});
155
156test("region open to window edge → not enclosed", () =>
157{
158 const origin = createEmptyChunkData();
159 origin.open = true;
160 // Three-sided "U" open to the south toward chunk interior / edge path — leave south open
161 // so flood from window edge reaches the pocket.
162 for (let y = 1; y <= 4; y++)
163 {
164 origin.blocks[idx(1, y)] = { typeId: BlockType.stoneWall, config: null };
165 origin.blocks[idx(5, y)] = { typeId: BlockType.stoneWall, config: null };
166 }
167 for (let x = 1; x <= 5; x++)
168 { origin.blocks[idx(x, 1)] = { typeId: BlockType.stoneWall, config: null }; }
169
170 const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]])));
171 assert.equal(enclosedAt(results, 0, 0, 3, 3), 0, "open pocket not enclosed");
172 assert.equal(enclosedAt(results, 0, 0, 1, 1), 0, "walls of open pocket not enclosed");
173});
174
175test("bakeEnclosedMaps writes enclosed onto chunk data", () =>
176{
177 const origin = createEmptyChunkData();
178 origin.open = true;
179 wallRect(origin, 4, 4, 8, 8);
180 const baked = bakeEnclosedMaps(0, 0, (x, y) => (x === 0 && y === 0 ? origin : createEmptyChunkData()));
181 const self = baked.find((c) => c.chunkX === 0 && c.chunkY === 0);
182 assert.equal(self.data.enclosed[idx(6, 6)], 1);
183 assert.equal(self.data.enclosed.length, CHUNK_SIZE * CHUNK_SIZE);
184});
185
186test("encode/decode persists enclosed (legacy default 0)", () =>
187{
188 const data = createEmptyChunkData();
189 data.open = true;
190 data.enclosed[idx(2, 2)] = 1;
191 const decoded = decodeChunkBlob(encodeChunkBlob(data));
192 assert.equal(decoded.enclosed[idx(2, 2)], 1);
193 assert.equal(decoded.enclosed[idx(0, 0)], 0);
194
195 const legacy = decodeChunkBlob(JSON.stringify({
196 open: true,
197 grounds: data.grounds,
198 blocks: data.blocks,
199 light: data.light,
200 spawners: []
201 }));
202 assert.equal(legacy.enclosed.length, CHUNK_SIZE * CHUNK_SIZE);
203 assert.ok(legacy.enclosed.every((v) => v === 0));
204});
205
206test("enclosedAmbientDarkness adds bonus and clamps to night peak", () =>
207{
208 assert.equal(enclosedAmbientDarkness(0, 0, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }), 0);
209 assert.equal(enclosedAmbientDarkness(0, 1, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }), 0.1);
210 assert.equal(
211 enclosedAmbientDarkness(0.7, 1, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }),
212 0.72
213 );
214 assert.equal(
215 DEFAULT_WORLD_SETTINGS.enclosedDarknessBonus,
216 0.10
217 );
218});