import test from "node:test"; import assert from "node:assert/strict"; import { createEmptyChunkData, encodeChunkBlob, decodeChunkBlob } from "../src/world/chunkData.js"; import { bakeEnclosedMaps } from "../src/lighting/enclosedBaker.js"; import { isEnclosedBoundaryTile, isEnclosedInteriorCandidate, bakeEnclosedWindow, enclosedAmbientDarkness } from "../../shared/lighting/enclosed.js"; import { BlockType, GroundType } from "../../shared/definitions/tiles.js"; import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../shared/protocol/constants.js"; function idx (localX, localY) { return localY * CHUNK_SIZE + localX; } function wallRect (data, x0, y0, x1, y1) { for (let y = y0; y <= y1; y++) { for (let x = x0; x <= x1; x++) { if (x === x0 || x === x1 || y === y0 || y === y1) { data.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; } } } } function loadMap (chunks) { return (cx, cy) => chunks.get(`${cx},${cy}`) ?? createEmptyChunkData(); } function enclosedAt (results, chunkX, chunkY, localX, localY) { const entry = results.find((r) => r.chunkX === chunkX && r.chunkY === chunkY); assert.ok(entry, `missing enclosed result for ${chunkX},${chunkY}`); return entry.enclosed[idx(localX, localY)]; } test("predicates: open floor is interior; wall is boundary; water is interior; door is boundary", () => { const data = createEmptyChunkData(); data.blocks[idx(1, 1)] = { typeId: BlockType.stoneWall, config: null }; data.grounds[idx(2, 2)] = { typeId: GroundType.water, state: null }; data.blocks[idx(3, 3)] = { typeId: BlockType.door, state: "pine-s-open", config: { restingState: "pine-s-closed" } }; data.blocks[idx(4, 4)] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 0, 0), false); assert.equal(isEnclosedInteriorCandidate(data.grounds, data.blocks, 0, 0), true); assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 1, 1), true); assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 2, 2), false, "water is not boundary"); assert.equal(isEnclosedInteriorCandidate(data.grounds, data.blocks, 2, 2), true); assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 3, 3), true, "open door is boundary"); assert.equal(isEnclosedBoundaryTile(data.grounds, data.blocks, 4, 4), true, "closed door is boundary"); }); test("open field → nothing enclosed", () => { const origin = createEmptyChunkData(); origin.open = true; const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]]))); const self = results.find((r) => r.chunkX === 0 && r.chunkY === 0); assert.ok(self.enclosed.every((v) => v === 0)); }); test("walled room with door → interior + walls + door marked", () => { const origin = createEmptyChunkData(); origin.open = true; // Room at local 4..8, door on south wall at (6,8) wallRect(origin, 4, 4, 8, 8); origin.blocks[idx(6, 8)] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]]))); assert.equal(enclosedAt(results, 0, 0, 6, 6), 1, "interior marked"); assert.equal(enclosedAt(results, 0, 0, 5, 5), 1, "interior near corner marked"); assert.equal(enclosedAt(results, 0, 0, 4, 6), 1, "west wall (shared edge) marked"); assert.equal(enclosedAt(results, 0, 0, 6, 4), 1, "north wall (shared edge) marked"); assert.equal(enclosedAt(results, 0, 0, 6, 8), 1, "door marked"); assert.equal(enclosedAt(results, 0, 0, 4, 4), 1, "outer wall corner marked via diagonal"); assert.equal(enclosedAt(results, 0, 0, 0, 0), 0, "outside not marked"); assert.equal(enclosedAt(results, 0, 0, 6, 9), 0, "just outside door not marked"); }); test("1×1 wall and 3×1 wall strip → nothing enclosed", () => { const origin = createEmptyChunkData(); origin.open = true; origin.blocks[idx(8, 8)] = { typeId: BlockType.stoneWall, config: null }; origin.blocks[idx(10, 8)] = { typeId: BlockType.stoneWall, config: null }; origin.blocks[idx(11, 8)] = { typeId: BlockType.stoneWall, config: null }; origin.blocks[idx(12, 8)] = { typeId: BlockType.stoneWall, config: null }; const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]]))); const self = results.find((r) => r.chunkX === 0 && r.chunkY === 0); assert.ok(self.enclosed.every((v) => v === 0), "lone walls must not mark enclosed"); }); test("water inside courtyard counts as enclosed interior", () => { const origin = createEmptyChunkData(); origin.open = true; wallRect(origin, 4, 4, 10, 10); origin.grounds[idx(7, 7)] = { typeId: GroundType.water, state: null }; origin.grounds[idx(7, 8)] = { typeId: GroundType.water, state: null }; const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]]))); assert.equal(enclosedAt(results, 0, 0, 7, 7), 1, "water tile enclosed"); assert.equal(enclosedAt(results, 0, 0, 7, 8), 1, "water tile enclosed"); assert.equal(enclosedAt(results, 0, 0, 6, 6), 1, "dry interior enclosed"); }); test("cross-chunk room within 3×3 is enclosed", () => { const a = createEmptyChunkData(); const b = createEmptyChunkData(); a.open = true; b.open = true; // Room straddling chunk 0,0 and 1,0: walls from localX 12..15 on chunk0 and 0..3 on chunk1, y 4..8 for (let y = 4; y <= 8; y++) { for (let x = 12; x < CHUNK_SIZE; x++) { if (x === 12 || y === 4 || y === 8) { a.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; } } for (let x = 0; x <= 3; x++) { if (x === 3 || y === 4 || y === 8) { b.blocks[idx(x, y)] = { typeId: BlockType.stoneWall, config: null }; } } } // Close east/west already; seal was on vertical edges. Fill north/south across seam. for (let x = 13; x < CHUNK_SIZE; x++) { a.blocks[idx(x, 4)] = { typeId: BlockType.stoneWall, config: null }; a.blocks[idx(x, 8)] = { typeId: BlockType.stoneWall, config: null }; } for (let x = 0; x <= 2; x++) { b.blocks[idx(x, 4)] = { typeId: BlockType.stoneWall, config: null }; b.blocks[idx(x, 8)] = { typeId: BlockType.stoneWall, config: null }; } const chunks = new Map([["0,0", a], ["1,0", b]]); const results = bakeEnclosedWindow(0, 0, loadMap(chunks)); assert.equal(enclosedAt(results, 0, 0, 14, 6), 1, "interior on chunk 0"); assert.equal(enclosedAt(results, 1, 0, 1, 6), 1, "interior on chunk 1"); assert.equal(enclosedAt(results, 0, 0, 12, 6), 1, "west wall marked"); assert.equal(enclosedAt(results, 1, 0, 3, 6), 1, "east wall marked"); }); test("region open to window edge → not enclosed", () => { const origin = createEmptyChunkData(); origin.open = true; // Three-sided "U" open to the south toward chunk interior / edge path — leave south open // so flood from window edge reaches the pocket. for (let y = 1; y <= 4; y++) { origin.blocks[idx(1, y)] = { typeId: BlockType.stoneWall, config: null }; origin.blocks[idx(5, y)] = { typeId: BlockType.stoneWall, config: null }; } for (let x = 1; x <= 5; x++) { origin.blocks[idx(x, 1)] = { typeId: BlockType.stoneWall, config: null }; } const results = bakeEnclosedWindow(0, 0, loadMap(new Map([["0,0", origin]]))); assert.equal(enclosedAt(results, 0, 0, 3, 3), 0, "open pocket not enclosed"); assert.equal(enclosedAt(results, 0, 0, 1, 1), 0, "walls of open pocket not enclosed"); }); test("bakeEnclosedMaps writes enclosed onto chunk data", () => { const origin = createEmptyChunkData(); origin.open = true; wallRect(origin, 4, 4, 8, 8); const baked = bakeEnclosedMaps(0, 0, (x, y) => (x === 0 && y === 0 ? origin : createEmptyChunkData())); const self = baked.find((c) => c.chunkX === 0 && c.chunkY === 0); assert.equal(self.data.enclosed[idx(6, 6)], 1); assert.equal(self.data.enclosed.length, CHUNK_SIZE * CHUNK_SIZE); }); test("encode/decode persists enclosed (legacy default 0)", () => { const data = createEmptyChunkData(); data.open = true; data.enclosed[idx(2, 2)] = 1; const decoded = decodeChunkBlob(encodeChunkBlob(data)); assert.equal(decoded.enclosed[idx(2, 2)], 1); assert.equal(decoded.enclosed[idx(0, 0)], 0); const legacy = decodeChunkBlob(JSON.stringify({ open: true, grounds: data.grounds, blocks: data.blocks, light: data.light, spawners: [] })); assert.equal(legacy.enclosed.length, CHUNK_SIZE * CHUNK_SIZE); assert.ok(legacy.enclosed.every((v) => v === 0)); }); test("enclosedAmbientDarkness adds bonus and clamps to night peak", () => { assert.equal(enclosedAmbientDarkness(0, 0, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }), 0); assert.equal(enclosedAmbientDarkness(0, 1, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }), 0.1); assert.equal( enclosedAmbientDarkness(0.7, 1, { enclosedDarknessBonus: 0.1, nightDarkness: 0.72 }), 0.72 ); assert.equal( DEFAULT_WORLD_SETTINGS.enclosedDarknessBonus, 0.10 ); });