Wooooorld files

This commit is contained in:
2026-11-06 23:41:32 -03:00
parent 1fa47c027d
commit 8e14e9842a
9 changed files with 501 additions and 383 deletions
+41 -2
View File
@@ -2,10 +2,19 @@
// 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 { assert, assertEquals, assertThrows } 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 { 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 });
@@ -87,3 +96,33 @@ Deno.test("column heights are the highest block and the lowest y that sees the s
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));
});