| 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, rayReaches } from "../../shared/lighting/raycast.js"; |
| 6 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 7 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 8 | |
| 9 | test("Bresenham visits start and end tiles", () => |
| 10 | { |
| 11 | const tiles = bresenhamTiles(0, 0, 2, 1); |
| 12 | assert.deepEqual(tiles[0], { x: 0, y: 0 }); |
| 13 | assert.deepEqual(tiles.at(-1), { x: 2, y: 1 }); |
| 14 | }); |
| 15 | |
| 16 | test("walls block light rays but can be lit themselves", () => |
| 17 | { |
| 18 | const blocked = (x, y) => !(x === 1 && y === 0); |
| 19 | assert.equal(rayReaches(0, 0, 2, 0, blocked), false); |
| 20 | assert.equal(rayReaches(0, 0, 2, 0, () => true), true); |
| 21 | const wallAtTarget = (x, y) => !(x === 2 && y === 0); |
| 22 | assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true); |
| 23 | }); |
| 24 | |
| 25 | test("a torch lights passable tiles, facing walls, and not tiles behind walls", () => |
| 26 | { |
| 27 | const origin = createEmptyChunkData(); |
| 28 | origin.open = true; |
| 29 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 30 | origin.blocks[2] = { typeId: BlockType.stoneWall, config: null }; |
| 31 | |
| 32 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 33 | { |
| 34 | if (x === 0 && y === 0) |
| 35 | { return origin; } |
| 36 | return createEmptyChunkData(); |
| 37 | }, 8); |
| 38 | |
| 39 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 40 | assert.ok(self.light[0] > 0, "torch tile is lit"); |
| 41 | assert.ok(self.light[1] > 0, "tile beside torch is lit"); |
| 42 | assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour"); |
| 43 | assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south"); |
| 44 | assert.ok(self.light[2] > 0, "wall facing the torch is lit"); |
| 45 | assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further"); |
| 46 | assert.equal(self.light[3], 0, "tile behind wall is unlit"); |
| 47 | assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); |
| 48 | }); |
| 49 | |
| 50 | test("dynamic lights use the same wall occlusion as baked light", () => |
| 51 | { |
| 52 | const blocked = (x, y) => !(x === 1 && y === 0); |
| 53 | const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; |
| 54 | assert.equal(dynamicLightAtTile(2, 0, sources, blocked), 0); |
| 55 | assert.ok(dynamicLightAtTile(1, 0, sources, () => true) > 0); |
| 56 | assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0); |
| 57 | }); |
| 58 | |
| 59 | test("tables and marketplace counters do not occlude light rays", () => |
| 60 | { |
| 61 | const origin = createEmptyChunkData(); |
| 62 | origin.open = true; |
| 63 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 64 | origin.blocks[1] = { typeId: BlockType.table, state: "pine-0", config: null }; |
| 65 | origin.blocks[2] = { typeId: BlockType.marketplace, state: "0", config: null }; |
| 66 | |
| 67 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 68 | { |
| 69 | if (x === 0 && y === 0) |
| 70 | { return origin; } |
| 71 | return createEmptyChunkData(); |
| 72 | }, 8); |
| 73 | |
| 74 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 75 | assert.ok(self.light[3] > 0, "light passes through table and counter"); |
| 76 | }); |
| 77 | |
| 78 | test("anvil occludes light by default (omit blocksLight)", () => |
| 79 | { |
| 80 | const origin = createEmptyChunkData(); |
| 81 | origin.open = true; |
| 82 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 83 | origin.blocks[1] = { typeId: BlockType.anvil, config: null }; |
| 84 | |
| 85 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 86 | { |
| 87 | if (x === 0 && y === 0) |
| 88 | { return origin; } |
| 89 | return createEmptyChunkData(); |
| 90 | }, 8); |
| 91 | |
| 92 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 93 | assert.equal(self.light[2], 0, "tile behind anvil stays unlit"); |
| 94 | }); |