import test from "node:test"; import assert from "node:assert/strict"; import { createEmptyChunkData } from "../src/world/chunkData.js"; import { bakeLightMaps } from "../src/lighting/baker.js"; import { bresenhamTiles, dynamicLightAtTile, evaluateLightRay, rayReaches } from "../../shared/lighting/raycast.js"; import { makeSnapshotLightOcclusion } from "../../shared/lighting/tilePassable.js"; import { collectBlockLightSources } from "../../shared/lighting/blockLights.js"; import { tileLightOcclusionAt, bakeChunkOcclusion } from "../../shared/lighting/chunkOcclusion.js"; import { BlockType, blockLightOcclusionForBlock, LightOcclusion } from "../../shared/definitions/tiles.js"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; import { tileLightAt } from "../src/combat/service.js"; import { maybeOpenDoorOnApproach } from "../src/doors/service.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; test("Bresenham visits start and end tiles", () => { const tiles = bresenhamTiles(0, 0, 2, 1); assert.deepEqual(tiles[0], { x: 0, y: 0 }); assert.deepEqual(tiles.at(-1), { x: 2, y: 1 }); }); test("walls block light rays but can be lit themselves", () => { const blocked = (x, y) => !(x === 1 && y === 0); assert.equal(rayReaches(0, 0, 2, 0, blocked), false); assert.equal(rayReaches(0, 0, 2, 0, () => true), true); const wallAtTarget = (x, y) => !(x === 2 && y === 0); assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true); }); test("a torch lights passable tiles, facing walls, and not tiles behind walls", () => { const origin = createEmptyChunkData(); origin.open = true; origin.blocks[0] = { typeId: BlockType.torch, config: null }; origin.blocks[2] = { typeId: BlockType.stoneWall, config: null }; const baked = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return origin; } return createEmptyChunkData(); }, 8); const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; assert.ok(self.light[0] > 0, "torch tile is lit"); assert.ok(self.light[1] > 0, "tile beside torch is lit"); assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour"); assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south"); assert.ok(self.light[2] > 0, "wall facing the torch is lit"); assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further"); assert.equal(self.light[3], 0, "tile behind wall is unlit"); assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); }); test("dynamic lights use the same wall occlusion as baked light", () => { const blocked = (x, y) => !(x === 1 && y === 0); const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; assert.equal(dynamicLightAtTile(2, 0, sources, blocked), 0); assert.ok(dynamicLightAtTile(1, 0, sources, () => true) > 0); assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0); }); test("tables and marketplace counters do not occlude light rays", () => { const origin = createEmptyChunkData(); origin.open = true; origin.blocks[0] = { typeId: BlockType.torch, config: null }; origin.blocks[1] = { typeId: BlockType.table, state: "pine-0", config: null }; origin.blocks[2] = { typeId: BlockType.marketplace, state: "0", config: null }; const baked = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return origin; } return createEmptyChunkData(); }, 8); const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; assert.ok(self.light[3] > 0, "light passes through table and counter"); }); test("crafting stations and ore nodes do not occlude light", () => { const origin = createEmptyChunkData(); origin.open = true; origin.blocks[0] = { typeId: BlockType.torch, config: null }; origin.blocks[1] = { typeId: BlockType.anvil, config: null }; origin.blocks[2] = { typeId: BlockType.furnace, config: null }; origin.blocks[3] = { typeId: BlockType.craftingBench, config: null }; origin.blocks[4] = { typeId: BlockType.woodworkingBench, config: null }; origin.blocks[5] = { typeId: BlockType.boulder, config: null }; origin.blocks[6] = { typeId: BlockType.cauldron, config: null }; const baked = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return origin; } return createEmptyChunkData(); }, 8); const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; assert.ok(self.light[7] > 0, "light passes through stations and ore"); }); test("partial occlusion adds falloff distance without stopping the ray", () => { const occlusion = (x, y) => (x === 1 && y === 0 ? LightOcclusion.partial : LightOcclusion.clear); const ray = evaluateLightRay(0, 0, 2, 0, occlusion); assert.equal(ray.reaches, true); assert.equal(ray.extraDistance, 1); const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; const throughTree = dynamicLightAtTile(2, 0, sources, occlusion); const open = dynamicLightAtTile(2, 0, sources, () => LightOcclusion.clear); assert.ok(throughTree > 0, "light still reaches past a tree"); assert.ok(throughTree < open, "tree canopy darkens falloff vs open path"); }); test("pine trees partially occlude baked light", () => { const withTree = createEmptyChunkData(); withTree.open = true; withTree.blocks[0] = { typeId: BlockType.torch, config: null }; withTree.blocks[1] = { typeId: BlockType.pineTree, state: "summer", config: null }; const open = createEmptyChunkData(); open.open = true; open.blocks[0] = { typeId: BlockType.torch, config: null }; const bakedTree = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return withTree; } return createEmptyChunkData(); }, 8); const bakedOpen = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return open; } return createEmptyChunkData(); }, 8); const litTree = bakedTree.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; const litOpen = bakedOpen.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; assert.ok(litTree.light[2] > 0, "light passes through pine canopy"); assert.ok(litTree.light[2] < litOpen.light[2], "pine darkens tiles beyond vs open air"); }); test("tree stumps do not occlude light rays", () => { assert.equal( blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }), LightOcclusion.clear ); assert.equal( blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }), LightOcclusion.partial ); const stump = (x, y) => (x === 1 && y === 0 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }) : LightOcclusion.clear); const tree = (x, y) => (x === 1 && y === 0 ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }) : LightOcclusion.clear); const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; const pastStump = dynamicLightAtTile(2, 0, sources, stump); const pastTree = dynamicLightAtTile(2, 0, sources, tree); assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree"); }); test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOcclusion", async () => { const { encodeChunkBlob, decodeChunkBlob, chunkTileLightOcclusion } = await import("../src/world/chunkData.js"); const data = createEmptyChunkData(); data.open = true; data.blocks[0] = { typeId: BlockType.stoneWall, config: null }; const blob = encodeChunkBlob(data); const decoded = decodeChunkBlob(blob); assert.equal(decoded.occlusion.length, CHUNK_SIZE * CHUNK_SIZE); assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0)); assert.equal(decoded.occlusion[1], LightOcclusion.clear); }); test("lit corner blend does not darken daylight beside occluding walls", async () => { const { smoothLitCorner } = await import("../../shared/lighting/lightCorners.js"); const dayOpen = smoothLitCorner({ homeLit: 1, homeBlocks: false, aLit: 1, aBlocks: false, bLit: 0.2, bBlocks: true, dLit: 0, dBlocks: true }); assert.equal(dayOpen, 1); const nightDarkBesideLitWall = smoothLitCorner({ homeLit: 0, homeBlocks: false, aLit: 0, aBlocks: false, bLit: 0.8, bBlocks: true, dLit: 0.5, dBlocks: true }); assert.equal(nightDarkBesideLitWall, 0); const openBlend = smoothLitCorner({ homeLit: 1, homeBlocks: false, aLit: 0.5, aBlocks: false, bLit: 0.5, bBlocks: false, dLit: 0.25, dBlocks: false }); assert.equal(openBlend, (1 + 0.5 + 0.5 + 0.25) / 4); }); test("collectBlockLightSources finds torches", () => { const data = createEmptyChunkData(); data.blocks[0] = { typeId: BlockType.torch, config: null }; const sources = collectBlockLightSources(0, 0, data.blocks); assert.equal(sources.length, 1); assert.equal(sources[0].tileX, 0); assert.equal(sources[0].tileY, 0); assert.ok(sources[0].range > 0); }); test("dynamic block light is blocked by a resting door and passes when passage-open", () => { const chunks = new Map(); const data = createEmptyChunkData(); data.open = true; data.blocks[0] = { typeId: BlockType.torch, config: null }; data.blocks[1] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; data.occlusion = bakeChunkOcclusion(data); chunks.set("0,0", { chunkX: 0, chunkY: 0, grounds: data.grounds, blocks: data.blocks, occlusion: data.occlusion }); const occlusion = makeSnapshotLightOcclusion(chunks); const sources = collectBlockLightSources(0, 0, data.blocks); assert.equal(dynamicLightAtTile(2, 0, sources, occlusion), 0, "closed door blocks torch"); data.blocks[1] = { typeId: BlockType.door, state: "pine-s-open", config: { restingState: "pine-s-closed" } }; data.occlusion[1] = tileLightOcclusionAt(data.grounds, data.blocks, 1, 0); assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0, "passage-open door lets torch through"); }); test("tileLightAt at night rises when a door opens between torch and target", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const record = harness.simulation.entityRecord(character.character_id); const worker = harness.simulation.workers[record.workerId]; worker.world.worldTime = () => ({ hour: 0 }); const data = worker.world.chunkData(0, 0); const torchIndex = 8 * CHUNK_SIZE + 8; const doorIndex = 8 * CHUNK_SIZE + 9; data.blocks[torchIndex] = { typeId: BlockType.torch, config: null }; data.blocks[doorIndex] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; data.occlusion[torchIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 8, 8); data.occlusion[doorIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 9, 8); const closed = tileLightAt(worker.world, 10, 8); const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); assert.ok(opened); const open = tileLightAt(worker.world, 10, 8); assert.ok(open > closed + 0.05, `expected open (${open}) brighter than closed (${closed})`); } finally { harness.close(); } });