import test from "node:test"; import assert from "node:assert/strict"; import { createEmptyChunkData } from "../src/world/chunkData.js"; import { bakeLightMaps, rayReaches, bresenhamTiles } from "../src/lighting/baker.js"; import { BlockType } from "../../shared/definitions/tiles.js"; import { CHUNK_SIZE } from "../../shared/protocol/constants.js"; test("Bresenham visits start and end tiles", () => { const tiles = bresenhamTiles(0, 0, 2, 1); assert.deepEqual(tiles[0], { x: 0, y: 0 }); assert.deepEqual(tiles.at(-1), { x: 2, y: 1 }); }); test("walls block light rays", () => { const blocked = (x, y) => !(x === 1 && y === 0); assert.equal(rayReaches(0, 0, 2, 0, blocked), false); assert.equal(rayReaches(0, 0, 2, 0, () => true), true); }); test("a torch lights passable tiles and not tiles behind walls", () => { const origin = createEmptyChunkData(); origin.open = true; origin.blocks[0] = { typeId: BlockType.torch, config: null }; origin.blocks[2] = { typeId: BlockType.stoneWall, config: null }; const baked = bakeLightMaps(0, 0, (x, y) => { if (x === 0 && y === 0) { return origin; } return createEmptyChunkData(); }, 8); const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data; assert.ok(self.light[0] > 0, "torch tile is lit"); assert.ok(self.light[1] > 0, "tile beside torch is lit"); assert.equal(self.light[2], 0, "wall is unlit"); assert.equal(self.light[3], 0, "tile behind wall is unlit"); assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); });