// 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, assertThrows } from "@std/assert"; import { Alea } from "@paulaboks/rng"; import { AIR, CHUNK_AREA } from "@/common/constants.ts"; import { Reader, Writer } from "@/common/bytes.ts"; import { ChunkColumn, column_heights, read_column, Section, SECTION_VOLUME, to_flat, write_column, } 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"); }); Deno.test("sections are written and read back as they were, however many kinds of block they hold", () => { const random = new Alea("paletted"); // one kind, then the most and one more than each palette size holds, past what a palette is for const kinds = [1, 2, 3, 4, 5, 16, 17, 256, 257, 4096]; const sections = kinds.map((count) => { // big values too, with state bits const values = Array.from({ length: count }, (_, i) => (i * 7919 + (i % 3) * 0x8000_0000) >>> 0); return Section.of( Uint32Array.from( { length: SECTION_VOLUME }, (_, i) => values[i < count ? i : Math.floor(random.next() * count)], ), ); }); sections.push(new Section(GLASS)); const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${i % 3}`); const writer = new Writer(); write_column(writer, sections, biomes); const bytes = writer.bytes(); const read = read_column(new Reader(bytes), (value) => value); assertEquals(to_flat(read.sections), to_flat(sections)); assertEquals(read.biomes, biomes); assertEquals(read.sections.at(-1)!.blocks, null, "a section of one block stays one value"); // the values as the reader numbers them assertEquals(read_column(new Reader(bytes), (value) => value + 1).sections.at(-1)!.value, GLASS + 1); assertThrows(() => read_column(new Reader(bytes.subarray(0, bytes.length - 1)), (value) => value)); });