Files
bworld/tests/client_level_test.ts
2026-11-07 12:13:43 -03:00

87 lines
3.8 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the client meshes a section at a time, and a block changing only meshes again the sections it can change the look
// or the light of, see ClientLevel's #block_changed
import { assertEquals } from "@std/assert";
import { Camera } from "@/client/camera.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import { CHUNK_AREA } from "@/common/constants.ts";
import { ChunkColumn } from "@/common/section.ts";
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
import { test_game } from "./helpers.ts";
import { client_level } from "./client_level.ts";
const GROUND_TOP = 63;
const ROOF = 200;
// a level of 5x5 chunks of stone up to GROUND_TOP, with a stone roof at ROOF over chunk 0, 0, all meshed. what it asks
// its chunk workers is kept in requests instead of being sent
async function meshed_level() {
await test_game("mods");
const { level, requests } = client_level(DEFAULT_SHAPE);
const stone = EverythingRegistry.get_id("blocks", "bworld:stone")!;
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
const flat = new Uint32Array(DEFAULT_SHAPE.volume).fill(stone, 0, (GROUND_TOP + 1) * CHUNK_AREA);
if (x === 0 && z === 0) flat.fill(stone, ROOF * CHUNK_AREA, (ROOF + 1) * CHUNK_AREA);
const biomes = new Array(CHUNK_AREA).fill("bworld:plains");
level.set_chunk(ChunkColumn.from_flat(x, z, DEFAULT_SHAPE, flat, biomes));
}
}
const camera = new Camera();
level.request_meshes(camera);
requests.length = 0;
// the sections asked for since the last call, as "chunk x,chunk z: indexes"
const remeshed = () => {
level.request_meshes(camera);
const asked = requests.map(({ chunk_x, chunk_z, sections }) =>
`${chunk_x},${chunk_z}: ${sections.map(({ index }) => index).sort((a, b) => a - b).join(" ")}`
).sort();
requests.length = 0;
return asked;
};
return { level, requests, remeshed };
}
Deno.test("a block that doesn't change the light remeshes its section, and the ones whose border it's on", async () => {
const { level, remeshed } = await meshed_level();
level.add_block({ id: "bworld:cobblestone", x: 8, y: 40, z: 8 });
assertEquals(remeshed(), ["0,0: 2"]);
// at x 0, z 15 and y 32 of its chunk, so next to the chunks to -x and +z and the section below
level.add_block({ id: "bworld:cobblestone", x: 0, y: 32, z: 15 });
assertEquals(remeshed(), ["-1,0: 1 2", "-1,1: 1 2", "0,0: 1 2", "0,1: 1 2"]);
});
Deno.test("a request sends the sections around the ones it's for, and their columns' sky heights", async () => {
const { level, requests } = await meshed_level();
level.add_block({ id: "bworld:cobblestone", x: 8, y: 40, z: 8 });
level.request_meshes(new Camera());
const [request] = requests;
assertEquals(request.first, 1);
assertEquals(request.chunks.map((chunk) => chunk?.length), new Array(9).fill(3));
assertEquals(request.sky[4]![8 * 16 + 8], ROOF + 1);
assertEquals(request.sky[0]![0], GROUND_TOP + 1);
});
Deno.test("opening a hole to the sky remeshes the columns around it from the old sky height to the new", async () => {
const { level, remeshed } = await meshed_level();
level.break_block(8, ROOF, 8);
// the light reaches 15 blocks past where it changes, and a section's faces are lit from the block past them
const from = (GROUND_TOP + 1 - 16) >> 4;
const to = (ROOF + 1 + 16) >> 4;
const sections = Array.from({ length: to - from + 1 }, (_, i) => from + i).join(" ");
const chunks = [];
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) chunks.push(`${x},${z}: ${sections}`);
}
assertEquals(remeshed(), chunks.sort());
// a light block under the roof changes the light around it, in the sections next to its own
level.add_block({ id: "bworld:light", x: 4, y: 100, z: 4 });
assertEquals(remeshed(), chunks.map((chunk) => chunk.replace(sections, "5 6 7")));
});