Files
bworld/tests/entity_light_test.ts
T
2026-11-06 22:49:43 -03:00

82 lines
3.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// entities are lit from the light the chunk worker sends back with each mesh, one byte per block: block << 4 | sky
import { assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { DEFAULT_SHAPE, type WorldShapeJson } from "@/common/world_shape.ts";
import { test_game } from "./helpers.ts";
// the middle chunk's light, with a stone roof at y 20 over x 0-7 and a light block at 12, 5, 8. y counts from the
// bottom of the world
async function mesh_light(shape: WorldShapeJson = DEFAULT_SHAPE) {
const volume = CHUNK_AREA * (shape.max_y - shape.min_y);
await test_game("mods");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
const nid = (id: string) => blocks.findIndex((b) => b?.id === id);
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
worker.postMessage({
type: "init",
shape,
blocks_registry: JSON.parse(JSON.stringify(blocks)),
models: {},
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
image: { width: 1024, height: 1024 },
visuals: [],
});
const chunk = new Uint32Array(volume);
const at = (x: number, y: number, z: number) => y * CHUNK_AREA + z * CHUNK_SIZE + x;
for (let x = 0; x < 8; x++) {
for (let z = 0; z < CHUNK_SIZE; z++) chunk[at(x, 20, z)] = nid("bworld:stone");
}
chunk[at(12, 5, 8)] = nid("bworld:light");
const empty = () => new Uint32Array(volume);
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
const meshed = await new Promise<{ light: Uint8Array; max_y: number }>((resolve) => {
worker.onmessage = (e) => resolve(e.data);
worker.postMessage({
type: "mesh",
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks,
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});
});
worker.terminate();
const light = (x: number, y: number, z: number) => {
const packed = meshed.light[at(x, y, z)];
return { block: packed >> 4, sky: packed & 15 };
};
return { light, size: meshed.light.length, max_y: meshed.max_y };
}
Deno.test("each mesh comes with its chunk's block and sky light, packed a byte per block", async () => {
const { light, size } = await mesh_light();
assertEquals(size, DEFAULT_SHAPE.volume);
assertEquals(light(12, 30, 8), { block: 0, sky: 15 }, "open sky");
assertEquals(light(12, 6, 8), { block: 13, sky: 15 }, "next to the light block");
// under the roof, sky light comes in sideways from the open side, a level less each block
assertEquals(light(7, 10, 8).sky, 14);
assertEquals(light(4, 10, 8).sky, 11);
assertEquals(light(4, 20, 8), { block: 0, sky: 0 }, "inside the stone");
});
Deno.test("without sky light, only blocks light the world", async () => {
const { light } = await mesh_light({ min_y: 0, max_y: 256, sky_light: false });
assertEquals(light(12, 30, 8), { block: 0, sky: 0 }, "the open air is dark");
assertEquals(light(12, 6, 8), { block: 13, sky: 0 }, "next to the light block");
});
Deno.test("a dimension that starts below 0 is lit the same, and meshed where its blocks are", async () => {
const { light, size, max_y } = await mesh_light({ min_y: -64, max_y: 320, sky_light: true });
assertEquals(size, CHUNK_AREA * 384);
assertEquals(light(12, 30, 8), { block: 0, sky: 15 });
assertEquals(light(4, 10, 8).sky, 11);
assertEquals(max_y, -64 + 21, "the top of the roof");
});