| 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 but can be lit themselves", () => |
| 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 | const wallAtTarget = (x, y) => !(x === 2 && y === 0); |
| 21 | assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true); |
| 22 | }); |
| 23 | |
| 24 | test("a torch lights passable tiles, facing walls, and not tiles behind walls", () => |
| 25 | { |
| 26 | const origin = createEmptyChunkData(); |
| 27 | origin.open = true; |
| 28 | origin.blocks[0] = { typeId: BlockType.torch, config: null }; |
| 29 | origin.blocks[2] = { typeId: BlockType.stoneWall, config: null }; |
| 30 | |
| 31 | const baked = bakeLightMaps(0, 0, (x, y) => |
| 32 | { |
| 33 | if (x === 0 && y === 0) |
| 34 | { return origin; } |
| 35 | return createEmptyChunkData(); |
| 36 | }, 8); |
| 37 | |
| 38 | const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; |
| 39 | assert.ok(self.light[0] > 0, "torch tile is lit"); |
| 40 | assert.ok(self.light[1] > 0, "tile beside torch is lit"); |
| 41 | assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour"); |
| 42 | assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south"); |
| 43 | assert.ok(self.light[2] > 0, "wall facing the torch is lit"); |
| 44 | assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further"); |
| 45 | assert.equal(self.light[3], 0, "tile behind wall is unlit"); |
| 46 | assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); |
| 47 | }); |