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
+35 -11
View File
@@ -8,7 +8,8 @@ import { ChunkBlocks, decode_chunk, encode_chunk, SavedChunk } from "@/server/ga
import { list_chunks, read_chunk, region_file, write_chunks } from "@/server/regions.ts";
import { sent_block, test_game } from "./helpers.ts";
import { DEFAULT_SHAPE, WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
import { biome_in, ChunkColumn, column_biomes, section_index, to_flat } from "@/common/section.ts";
import { Writer } from "@/common/bytes.ts";
// a chunk at the height of the base game's dimension, which the test worlds play
function chunk_data(seed: number, palette_size = 3, shape = DEFAULT_SHAPE): Required<ChunkBlocks> {
@@ -16,12 +17,8 @@ function chunk_data(seed: number, palette_size = 3, shape = DEFAULT_SHAPE): Requ
for (let i = 0; i < palette_size; i++) palette.push([`test:block_${i}`, i % 5]);
const blocks = new Uint32Array(shape.volume).map((_, i) => (i * seed) % palette_size);
const biomes = Array.from({ length: CHUNK_AREA }, (_, i) => `test:biome_${(i * seed) % 4}`);
return {
palette,
min_y: shape.min_y,
sections: ChunkColumn.from_flat(0, 0, shape, blocks, biomes).sections,
biomes,
};
const { sections, biomes: column_biomes } = ChunkColumn.from_flat(0, 0, shape, blocks, biomes);
return { palette, min_y: shape.min_y, sections, biomes: column_biomes };
}
// the palette entry of a saved chunk's block
@@ -44,9 +41,14 @@ function old_format(format: 1 | 2, chunk: ChunkBlocks) {
if (format === 1) {
bytes.push(...new Uint8Array(flat.length / 8));
} else {
const biome_palette = [...new Set(chunk.biomes)];
// each column's biome at the bottom
const columns = Array.from(
{ length: CHUNK_AREA },
(_, i) => biome_in(chunk.biomes!, DEFAULT_SHAPE, i, 0, i >> 4),
);
const biome_palette = [...new Set(columns)];
bytes.push(biome_palette.length, ...biome_palette.flatMap(string));
bytes.push(...chunk.biomes!.map((biome) => biome_palette.indexOf(biome)));
bytes.push(...columns.map((biome) => biome_palette.indexOf(biome)));
}
return Uint8Array.from(bytes);
}
@@ -80,6 +82,28 @@ Deno.test("a chunk's blocks and biomes come back as they were saved, with more t
assertEquals(to_flat(old.sections), to_flat(chunk.sections));
assertEquals(old.biomes, format === 1 ? undefined : chunk.biomes);
}
// and format 3, from before biomes had heights: each column's biome, which goes all the way up
const columns = Array.from({ length: CHUNK_AREA }, (_, i) => i < 10 ? "test:a" : "test:b");
const writer = new Writer();
writer.u8(3);
writer.i16(0);
writer.u32(1);
writer.string("test:block_0");
writer.u16(0);
writer.u16(2);
writer.string("test:a");
writer.string("test:b");
for (const biome of columns) writer.u8(biome === "test:a" ? 0 : 1);
writer.u16(16);
// sections all of the one block
for (let i = 0; i < 16; i++) {
writer.u8(0);
writer.u32(0);
}
const three = decode_chunk(writer.bytes());
assertEquals(three.sections.map((section) => section.value), new Array(16).fill(0));
assertEquals(three.biomes, column_biomes(columns, 16));
});
Deno.test("region files keep chunks, including negative ones, and saving one again doesn't touch the others", async () => {
@@ -136,7 +160,7 @@ Deno.test("every chunk generated is saved whole, and saved again only when it ch
const chunk = decode_chunk(saved.chunks.find((c) => c.x === 0 && c.z === 0)!.data);
assertEquals(saved_block(chunk, 3, 120, 5), ["bworld:stone", 0]);
assert(saved_block(chunk, 3, 10, 5)[0] !== AIR_ID);
assertEquals(chunk.biomes![5 * 16 + 3], game.world.biome_at(3, 5));
assertEquals(biome_in(chunk.biomes!, DEFAULT_SHAPE, 3, 120, 5), game.world.biome_at(3, 120, 5));
const untouched = decode_chunk(saved.chunks.find((c) => c.x === 5 && c.z === 5)!.data);
assertEquals(
Uint32Array.from(to_flat(untouched.sections), (block) => {
@@ -192,7 +216,7 @@ Deno.test("chunks saved before they had biomes get them from generation, and are
at_0_0.data = old_format(1, decode_chunk(at_0_0.data));
const { game } = await test_game("mods", saved);
assertEquals(game.world.get_block_id(3, 121, 5), "bworld:stone");
assertEquals(game.world.biome_at(3, 5), biomes[5 * 16 + 3]);
assertEquals(game.world.biome_at(3, 120, 5), biome_in(biomes, DEFAULT_SHAPE, 3, 120, 5));
const again = game.save().chunks.find((c) => c.x === 0 && c.z === 0)!;
assertEquals(decode_chunk(again.data).biomes, biomes);
});