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