Files
bworld/tests/section_test.ts
T
2026-11-06 23:13:34 -03:00

90 lines
3.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// chunks' blocks in sections of 16x16x16, see common/section.ts
import { assert, assertEquals } from "@std/assert";
import { Alea } from "@paulaboks/rng";
import { AIR, CHUNK_AREA } from "@/common/constants.ts";
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
import { WorldShape } from "@/common/world_shape.ts";
const SHAPE = new WorldShape({ min_y: -64, max_y: 320, sky_light: true });
const [STONE, GLASS, LEAVES] = [1, 2, 3];
// light goes through air and glass, leaves and stone stop it
const OPACITY = Uint8Array.of(0, 15, 0, 1);
function empty_column(x = 0, z = 0) {
const sections = Array.from({ length: SHAPE.height / 16 }, () => new Section(AIR));
return new ChunkColumn(x, z, SHAPE, sections, new Array(CHUNK_AREA).fill("test:plains"));
}
Deno.test("a section is one value until a block in it differs", () => {
const section = new Section(STONE);
section.set(5, STONE);
assertEquals(section.blocks, null);
section.set(5, GLASS);
assertEquals(section.get(5), GLASS);
assertEquals(section.get(6), STONE);
assertEquals(section.blocks?.length, SECTION_VOLUME);
assertEquals(Section.of(new Uint32Array(SECTION_VOLUME).fill(GLASS)).data, GLASS);
});
Deno.test("a column's blocks are at their world coordinates, from min_y to max_y - 1", () => {
const column = empty_column(-2, 3);
column.set(-32, -64, 63, STONE);
column.set(-17, 319, 48, GLASS);
column.set(-20, 320, 50, STONE);
assertEquals(column.get(-32, -64, 63), STONE);
assertEquals(column.get(-17, 319, 48), GLASS);
assertEquals(column.get(-20, 320, 50), AIR, "above the world");
assertEquals(column.get(-20, -65, 50), AIR, "below the world");
assertEquals(column.sections.filter((section) => section.blocks).length, 2);
// flat, y from min_y, then z, then x
const flat = column.to_flat();
assertEquals(flat[0 * CHUNK_AREA + 15 * 16 + 0], STONE);
assertEquals(flat[383 * CHUNK_AREA + 0 * 16 + 15], GLASS);
const again = ChunkColumn.from_flat(-2, 3, SHAPE, flat, column.biomes);
assertEquals(again.to_flat(), flat);
assertEquals(again.sections.filter((section) => section.blocks).length, 2);
const copy = column.clone();
copy.set(-32, -64, 63, AIR);
assertEquals(column.get(-32, -64, 63), STONE, "a clone's blocks are its own");
});
Deno.test("column heights are the highest block and the lowest y that sees the sky, block by block", () => {
const random = new Alea("section-test");
const column = empty_column();
// stone to y 0, then scattered blocks up to 200, and a glass section in the air
for (let y = -64; y <= 0; y++) {
for (let i = 0; i < CHUNK_AREA; i++) column.set(i % 16, y, i >> 4, STONE);
}
for (let n = 0; n < 2000; n++) {
const block = [STONE, GLASS, LEAVES][Math.floor(random.next() * 3)];
column.set(
Math.floor(random.next() * 16),
Math.floor(random.next() * 200),
Math.floor(random.next() * 16),
block,
);
}
// y 240 to 255, all glass, which light passes over whole
column.sections[(240 - SHAPE.min_y) / 16] = new Section(GLASS);
const heights = column_heights(column, OPACITY);
for (let i = 0; i < CHUNK_AREA; i++) {
const [x, z] = [i % 16, i >> 4];
let y = SHAPE.max_y - 1;
while (y >= SHAPE.min_y && column.get(x, y, z) === AIR) y--;
assertEquals(heights[i], y, `surface of ${x}, ${z}`);
while (y >= SHAPE.min_y && OPACITY[column.get(x, y, z)] === 0) y--;
assertEquals(heights[CHUNK_AREA + i], y + 1, `sky of ${x}, ${z}`);
}
const nothing = column_heights(empty_column(), OPACITY);
assertEquals([nothing[0], nothing[CHUNK_AREA]], [SHAPE.min_y - 1, SHAPE.min_y]);
assert(heights.some((height, i) => i < CHUNK_AREA && height === 255), "the glass is the top");
});