| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { createEmptyChunkData } from "../src/world/chunkData.js"; |
| 4 | import { bakeLightMaps, rayReaches, bresenhamTiles } from "../src/lighting/baker.js"; |
| 5 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 6 | import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; |
| 7 | |
| 8 | test("Bresenham visits start and end tiles", () => |
| 9 | { |
| 10 | const tiles = bresenhamTiles(0, 0, 2, 1); |
| 11 | assert.deepEqual(tiles[0], { x: 0, y: 0 }); |
| 12 | assert.deepEqual(tiles.at(-1), { x: 2, y: 1 }); |
| 13 | }); |
| 14 | |
| 15 | test("walls block light rays", () => |
| 16 | { |
| 17 | const blocked = (x, y) => !(x === 1 && y === 0); |
| 18 | assert.equal(rayReaches(0, 0, 2, 0, blocked), false); |
| 19 | assert.equal(rayReaches(0, 0, 2, 0, () => true), true); |
| 20 | }); |
| 21 | |
| 22 | test("a torch lights passable tiles and not tiles behind walls", () => |
| 23 | { |
| 24 | const origin = createEmptyChunkData(); |
| 25 | origin.open = true; |
| 26 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 27 | origin.blocks[2] = { typeId: BlockType.stoneWall, config: null }; |
| 28 | |
| 29 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 30 | { |
| 31 | if (x === 0 && y === 0) |
| 32 | { return origin; } |
| 33 | return createEmptyChunkData(); |
| 34 | }, 8); |
| 35 | |
| 36 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 37 | assert.ok(self.light[0] > 0, "torch tile is lit"); |
| 38 | assert.ok(self.light[1] > 0, "tile beside torch is lit"); |
| 39 | assert.equal(self.light[2], 0, "wall is unlit"); |
| 40 | assert.equal(self.light[3], 0, "tile behind wall is unlit"); |
| 41 | assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); |
| 42 | }); |