| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createEmptyChunkData } from "../src/world/chunkData.js"; |
| 4 | import { bakeLightMaps } from "../src/lighting/baker.js"; |
| 5 | import { bresenhamTiles, dynamicLightAtTile, evaluateLightRay, rayReaches } from "../../shared/lighting/raycast.js"; |
| 6 | import { makeSnapshotLightOcclusion } from "../../shared/lighting/tilePassable.js"; |
| 7 | import { collectBlockLightSources } from "../../shared/lighting/blockLights.js"; |
| 8 | import { tileLightOcclusionAt, bakeChunkOcclusion } from "../../shared/lighting/chunkOcclusion.js"; |
| 9 | import { BlockType, blockLightOcclusionForBlock, LightOcclusion } from "../../shared/definitions/tiles.js"; |
| 10 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 11 | import { tileLightAt } from "../src/combat/service.js"; |
| 12 | import { maybeOpenDoorOnApproach } from "../src/doors/service.js"; |
| 13 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 14 | |
| 15 | test("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 | |
| 22 | test("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 | |
| 31 | test("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 | |
| 56 | test("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 | |
| 65 | test("tables and marketplace counters do not occlude light rays", () => |
| 66 | { |
| 67 | const origin = createEmptyChunkData(); |
| 68 | origin.open = true; |
| 69 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 70 | origin.blocks[1] = { typeId: BlockType.table, state: "pine-0", config: null }; |
| 71 | origin.blocks[2] = { typeId: BlockType.marketplace, state: "0", config: null }; |
| 72 | |
| 73 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 74 | { |
| 75 | if (x === 0 && y === 0) |
| 76 | { return origin; } |
| 77 | return createEmptyChunkData(); |
| 78 | }, 8); |
| 79 | |
| 80 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 81 | assert.ok(self.light[3] > 0, "light passes through table and counter"); |
| 82 | }); |
| 83 | |
| 84 | test("crafting stations and ore nodes do not occlude light", () => |
| 85 | { |
| 86 | const origin = createEmptyChunkData(); |
| 87 | origin.open = true; |
| 88 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 89 | origin.blocks[1] = { typeId: BlockType.anvil, config: null }; |
| 90 | origin.blocks[2] = { typeId: BlockType.furnace, config: null }; |
| 91 | origin.blocks[3] = { typeId: BlockType.craftingBench, config: null }; |
| 92 | origin.blocks[4] = { typeId: BlockType.woodworkingBench, config: null }; |
| 93 | origin.blocks[5] = { typeId: BlockType.boulder, config: null }; |
| 94 | origin.blocks[6] = { typeId: BlockType.cauldron, config: null }; |
| 95 | |
| 96 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 97 | { |
| 98 | if (x === 0 && y === 0) |
| 99 | { return origin; } |
| 100 | return createEmptyChunkData(); |
| 101 | }, 8); |
| 102 | |
| 103 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 104 | assert.ok(self.light[7] > 0, "light passes through stations and ore"); |
| 105 | }); |
| 106 | |
| 107 | test("partial occlusion adds falloff distance without stopping the ray", () => |
| 108 | { |
| 109 | const occlusion = (x, y) => (x === 1 && y === 0 |
| 110 | ? LightOcclusion.partial |
| 111 | : LightOcclusion.clear); |
| 112 | const ray = evaluateLightRay(0, 0, 2, 0, occlusion); |
| 113 | assert.equal(ray.reaches, true); |
| 114 | assert.equal(ray.extraDistance, 1); |
| 115 | |
| 116 | const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; |
| 117 | const throughTree = dynamicLightAtTile(2, 0, sources, occlusion); |
| 118 | const open = dynamicLightAtTile(2, 0, sources, () => LightOcclusion.clear); |
| 119 | assert.ok(throughTree > 0, "light still reaches past a tree"); |
| 120 | assert.ok(throughTree < open, "tree canopy darkens falloff vs open path"); |
| 121 | }); |
| 122 | |
| 123 | test("pine trees partially occlude baked light", () => |
| 124 | { |
| 125 | const withTree = createEmptyChunkData(); |
| 126 | withTree.open = true; |
| 127 | withTree.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 128 | withTree.blocks[1] = { typeId: BlockType.pineTree, state: "summer", config: null }; |
| 129 | |
| 130 | const open = createEmptyChunkData(); |
| 131 | open.open = true; |
| 132 | open.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 133 | |
| 134 | const bakedTree = bakeLightMaps(0, 0, (x, y) => |
| 135 | { |
| 136 | if (x === 0 && y === 0) |
| 137 | { return withTree; } |
| 138 | return createEmptyChunkData(); |
| 139 | }, 8); |
| 140 | const bakedOpen = bakeLightMaps(0, 0, (x, y) => |
| 141 | { |
| 142 | if (x === 0 && y === 0) |
| 143 | { return open; } |
| 144 | return createEmptyChunkData(); |
| 145 | }, 8); |
| 146 | |
| 147 | const litTree = bakedTree.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 148 | const litOpen = bakedOpen.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 149 | assert.ok(litTree.light[2] > 0, "light passes through pine canopy"); |
| 150 | assert.ok(litTree.light[2] < litOpen.light[2], "pine darkens tiles beyond vs open air"); |
| 151 | }); |
| 152 | |
| 153 | test("tree stumps do not occlude light rays", () => |
| 154 | { |
| 155 | assert.equal( |
| 156 | blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }), |
| 157 | LightOcclusion.clear |
| 158 | ); |
| 159 | assert.equal( |
| 160 | blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }), |
| 161 | LightOcclusion.partial |
| 162 | ); |
| 163 | |
| 164 | const stump = (x, y) => (x === 1 && y === 0 |
| 165 | ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer-stump" }) |
| 166 | : LightOcclusion.clear); |
| 167 | const tree = (x, y) => (x === 1 && y === 0 |
| 168 | ? blockLightOcclusionForBlock({ typeId: BlockType.pineTree, state: "summer" }) |
| 169 | : LightOcclusion.clear); |
| 170 | const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; |
| 171 | const pastStump = dynamicLightAtTile(2, 0, sources, stump); |
| 172 | const pastTree = dynamicLightAtTile(2, 0, sources, tree); |
| 173 | assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree"); |
| 174 | }); |
| 175 | |
| 176 | test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOcclusion", async () => |
| 177 | { |
| 178 | const { encodeChunkBlob, decodeChunkBlob, chunkTileLightOcclusion } = await import("../src/world/chunkData.js"); |
| 179 | const data = createEmptyChunkData(); |
| 180 | data.open = true; |
| 181 | data.blocks[0] = { typeId: BlockType.stoneWall, config: null }; |
| 182 | const blob = encodeChunkBlob(data); |
| 183 | const decoded = decodeChunkBlob(blob); |
| 184 | assert.equal(decoded.occlusion.length, CHUNK_SIZE * CHUNK_SIZE); |
| 185 | assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0)); |
| 186 | assert.equal(decoded.occlusion[1], LightOcclusion.clear); |
| 187 | }); |
| 188 | |
| 189 | test("lit corner blend does not darken daylight beside occluding walls", async () => |
| 190 | { |
| 191 | const { smoothLitCorner } = await import("../../shared/lighting/lightCorners.js"); |
| 192 | const dayOpen = smoothLitCorner({ |
| 193 | homeLit: 1, |
| 194 | homeBlocks: false, |
| 195 | aLit: 1, |
| 196 | aBlocks: false, |
| 197 | bLit: 0.2, |
| 198 | bBlocks: true, |
| 199 | dLit: 0, |
| 200 | dBlocks: true |
| 201 | }); |
| 202 | assert.equal(dayOpen, 1); |
| 203 | const nightDarkBesideLitWall = smoothLitCorner({ |
| 204 | homeLit: 0, |
| 205 | homeBlocks: false, |
| 206 | aLit: 0, |
| 207 | aBlocks: false, |
| 208 | bLit: 0.8, |
| 209 | bBlocks: true, |
| 210 | dLit: 0.5, |
| 211 | dBlocks: true |
| 212 | }); |
| 213 | assert.equal(nightDarkBesideLitWall, 0); |
| 214 | const openBlend = smoothLitCorner({ |
| 215 | homeLit: 1, |
| 216 | homeBlocks: false, |
| 217 | aLit: 0.5, |
| 218 | aBlocks: false, |
| 219 | bLit: 0.5, |
| 220 | bBlocks: false, |
| 221 | dLit: 0.25, |
| 222 | dBlocks: false |
| 223 | }); |
| 224 | assert.equal(openBlend, (1 + 0.5 + 0.5 + 0.25) / 4); |
| 225 | }); |
| 226 | |
| 227 | test("collectBlockLightSources finds torches", () => |
| 228 | { |
| 229 | const data = createEmptyChunkData(); |
| 230 | data.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 231 | const sources = collectBlockLightSources(0, 0, data.blocks); |
| 232 | assert.equal(sources.length, 1); |
| 233 | assert.equal(sources[0].tileX, 0); |
| 234 | assert.equal(sources[0].tileY, 0); |
| 235 | assert.ok(sources[0].range > 0); |
| 236 | }); |
| 237 | |
| 238 | test("dynamic block light is blocked by a resting door and passes when passage-open", () => |
| 239 | { |
| 240 | const chunks = new Map(); |
| 241 | const data = createEmptyChunkData(); |
| 242 | data.open = true; |
| 243 | data.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 244 | data.blocks[1] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; |
| 245 | data.occlusion = bakeChunkOcclusion(data); |
| 246 | chunks.set("0,0", { chunkX: 0, chunkY: 0, grounds: data.grounds, blocks: data.blocks, occlusion: data.occlusion }); |
| 247 | |
| 248 | const occlusion = makeSnapshotLightOcclusion(chunks); |
| 249 | const sources = collectBlockLightSources(0, 0, data.blocks); |
| 250 | assert.equal(dynamicLightAtTile(2, 0, sources, occlusion), 0, "closed door blocks torch"); |
| 251 | |
| 252 | data.blocks[1] = { |
| 253 | typeId: BlockType.door, |
| 254 | state: "pine-s-open", |
| 255 | config: { restingState: "pine-s-closed" } |
| 256 | }; |
| 257 | data.occlusion[1] = tileLightOcclusionAt(data.grounds, data.blocks, 1, 0); |
| 258 | assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0, "passage-open door lets torch through"); |
| 259 | }); |
| 260 | |
| 261 | test("tileLightAt at night rises when a door opens between torch and target", () => |
| 262 | { |
| 263 | const harness = makeSimulation({ workerCount: 1 }); |
| 264 | try |
| 265 | { |
| 266 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 267 | harness.simulation.spawnCharacter(1, character); |
| 268 | harness.simulation.step(); |
| 269 | const record = harness.simulation.entityRecord(character.character_id); |
| 270 | const worker = harness.simulation.workers[record.workerId]; |
| 271 | worker.world.worldTime = () => ({ hour: 0 }); |
| 272 | |
| 273 | const data = worker.world.chunkData(0, 0); |
| 274 | const torchIndex = 8 * CHUNK_SIZE + 8; |
| 275 | const doorIndex = 8 * CHUNK_SIZE + 9; |
| 276 | data.blocks[torchIndex] = { typeId: BlockType.torch, config: null }; |
| 277 | data.blocks[doorIndex] = { typeId: BlockType.door, state: "pine-s-closed", config: null }; |
| 278 | data.occlusion[torchIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 8, 8); |
| 279 | data.occlusion[doorIndex] = tileLightOcclusionAt(data.grounds, data.blocks, 9, 8); |
| 280 | |
| 281 | const closed = tileLightAt(worker.world, 10, 8); |
| 282 | const opened = maybeOpenDoorOnApproach(worker.world, character.character_id, 9, 8); |
| 283 | assert.ok(opened); |
| 284 | const open = tileLightAt(worker.world, 10, 8); |
| 285 | assert.ok(open > closed + 0.05, `expected open (${open}) brighter than closed (${closed})`); |
| 286 | } |
| 287 | finally |
| 288 | { harness.close(); } |
| 289 | }); |