import test from "node:test"; import assert from "node:assert/strict"; import { createEmptyChunkData } from "../src/world/chunkData.js"; import { bakeLightMaps } from "../src/lighting/baker.js"; import { bresenhamTiles, dynamicLightAtTile, rayReaches } from "../../shared/lighting/raycast.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 but can be lit themselves", () => { 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); const wallAtTarget = (x, y) => !(x === 2 && y === 0); assert.equal(rayReaches(0, 0, 2, 0, wallAtTarget), true); }); test("a torch lights passable tiles, facing walls, 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.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour"); assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south"); assert.ok(self.light[2] > 0, "wall facing the torch is lit"); assert.ok(self.light[1] > self.light[2], "open tile beside torch is brighter than the wall one step further"); assert.equal(self.light[3], 0, "tile behind wall is unlit"); assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE); }); test("dynamic lights use the same wall occlusion as baked light", () => { const blocked = (x, y) => !(x === 1 && y === 0); const sources = [{ tileX: 0, tileY: 0, range: 8, intensity: 1 }]; assert.equal(dynamicLightAtTile(2, 0, sources, blocked), 0); assert.ok(dynamicLightAtTile(1, 0, sources, () => true) > 0); assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0); });