3D biomes

This commit is contained in:
2026-11-07 12:13:43 -03:00
parent b726ce9695
commit 22191d260c
53 changed files with 874 additions and 316 deletions
+56 -3
View File
@@ -7,10 +7,13 @@ 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,
@@ -21,10 +24,12 @@ 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, new Array(CHUNK_AREA).fill("test:plains"));
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", () => {
@@ -54,7 +59,7 @@ Deno.test("a column's blocks are at their world coordinates, from min_y to max_y
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);
const again = ChunkColumn.from_flat(-2, 3, SHAPE, flat, PLAINS);
assertEquals(again.to_flat(), flat);
assertEquals(again.sections.filter((section) => section.blocks).length, 2);
@@ -112,7 +117,14 @@ Deno.test("sections are written and read back as they were, however many kinds o
);
});
sections.push(new Section(GLASS));
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${i % 3}`);
// 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);
@@ -126,3 +138,44 @@ Deno.test("sections are written and read back as they were, however many kinds o
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",
]);
});