// 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 { BIOME_CELL_HEIGHT, ChunkColumn, column_biomes, column_heights, read_column, Section, SECTION_BIOMES, 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); // every column's biome all the way up const PLAINS: string[] = new Array(CHUNK_AREA).fill("test:plains"); 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, column_biomes(PLAINS, sections.length)); } 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, PLAINS); 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)); // three biomes in the lowest 5 sections, one in the rest const biomes = column_biomes( Array.from( { length: sections.length * SECTION_BIOMES }, (_, i) => i < 5 * SECTION_BIOMES ? `test:biome_${i % 3}` : "test:one", ), sections.length, ); 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)); }); Deno.test("a column's biomes are one per block across and every 4 blocks up, and set a cell at a time", () => { // a cave biome from y 0 to 3 under one column, the rest plains const cells = Array.from( { length: SHAPE.height / BIOME_CELL_HEIGHT * CHUNK_AREA }, (_, i) => i === (0 - SHAPE.min_y) / BIOME_CELL_HEIGHT * CHUNK_AREA + 5 * 16 + 4 ? "test:cave" : "test:plains", ); const column = ChunkColumn.from_flat(0, 0, SHAPE, new Uint32Array(SHAPE.volume), cells); assertEquals([0, 3].map((y) => column.biome_at(4, y, 5)), ["test:cave", "test:cave"]); assertEquals([-1, 4].map((y) => column.biome_at(4, y, 5)), ["test:plains", "test:plains"]); assertEquals(column.biome_at(5, 0, 5), "test:plains"); // above and below the world, the top's and the bottom's assertEquals([column.biome_at(4, 1000, 5), column.biome_at(4, -1000, 5)], ["test:plains", "test:plains"]); // sections with the same biomes all the way up share theirs const plains = column_biomes(PLAINS.map((biome, i) => i < 5 ? "test:desert" : biome), 24); assert(plains.sections.every((section) => section === plains.sections[0])); column.set_biome(9, 100, 9, "test:desert"); for (let y = 100; y < 104; y++) assertEquals(column.biome_at(9, y, 9), "test:desert"); for (const y of [99, 104]) assertEquals(column.biome_at(9, y, 9), "test:plains"); assertEquals(column.biome_at(9, 100, 10), "test:plains"); // nothing outside the world's height column.set_biome(9, 320, 9, "test:desert"); assertEquals(column.biome_at(9, 319, 9), "test:plains"); // a copy's biomes are its own, even where sections shared theirs const sharing = ChunkColumn.from_flat( 0, 0, SHAPE, new Uint32Array(SHAPE.volume), PLAINS.map((biome, i) => i < 5 ? "test:desert" : biome), ); const copy = sharing.clone(); copy.set_biome(9, 0, 0, "test:cave"); assertEquals([copy.biome_at(9, 0, 0), sharing.biome_at(9, 0, 0), copy.biome_at(9, 16, 0)], [ "test:cave", "test:plains", "test:plains", ]); });