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
+181 -60
View File
@@ -55,18 +55,93 @@ export class Section {
}
}
// a chunk's blocks, its dimension's whole height, and each column's biome
// biomes are one per block across, and one every BIOME_CELL_HEIGHT blocks up: a cell
export const BIOME_CELL_HEIGHT = 4;
export const SECTION_BIOMES = CHUNK_AREA * SECTION_HEIGHT / BIOME_CELL_HEIGHT;
// a column's biome palette can't be longer, since a cell's index in it is a byte
export const MAX_COLUMN_BIOMES = 256;
// a biome cell's place in its section's biomes: its layer of cells, then z, then x
export function biome_index(x: number, y: number, z: number) {
return (((y & 15) >> 2) << 8) | ((z & 15) << 4) | (x & 15);
}
// a section's biomes: one index into its column's biome palette for all of it, or SECTION_BIOMES of them. an array is
// never changed once it's made, see ChunkColumn.set_biome, so sections with the same biomes can share one: most of a
// column's when biomes don't change with height
export type SectionBiomes = number | Uint8Array;
// a chunk's biomes
export interface ColumnBiomes {
// every biome in it, at most MAX_COLUMN_BIOMES
palette: string[];
// by section from the bottom up, like its blocks
sections: SectionBiomes[];
}
// biomes from each cell's biome id, a layer of cells at a time from the bottom of the world up, then z, then x. or
// CHUNK_AREA of them, each column's biome all the way up. count is how many sections there are
export function column_biomes(cells: string[], count: number): ColumnBiomes {
const palette: string[] = [];
const indexes = new Map<string, number>();
const sections: SectionBiomes[] = [];
const section = new Uint8Array(SECTION_BIOMES);
// runs of the same biome are common, it's only looked up when it changes
let last: string | undefined;
let last_index = 0;
for (let s = 0; s < count; s++) {
let same = true;
for (let i = 0; i < SECTION_BIOMES; i++) {
const biome = cells[cells.length === CHUNK_AREA ? i & 255 : s * SECTION_BIOMES + i];
if (biome !== last) {
last = biome;
let index = indexes.get(biome);
if (index === undefined) {
if (palette.length === MAX_COLUMN_BIOMES) throw new Error(`More than ${MAX_COLUMN_BIOMES} biomes`);
index = palette.push(biome) - 1;
indexes.set(biome, index);
}
last_index = index;
}
section[i] = last_index;
if (last_index !== section[0]) same = false;
}
sections.push(same ? section[0] : section.slice());
}
share_same(sections);
return { palette, sections };
}
// sections whose biomes are the same as the one below's share its array
function share_same(sections: SectionBiomes[]) {
for (let s = 1; s < sections.length; s++) {
const below = sections[s - 1];
const here = sections[s];
if (typeof below === "number" || typeof here === "number" || below === here) continue;
let i = 0;
while (i < SECTION_BIOMES && below[i] === here[i]) i++;
if (i === SECTION_BIOMES) sections[s] = below;
}
}
// the biome at world coordinates in a chunk's biomes, see ChunkColumn.biome_at
export function biome_in(biomes: ColumnBiomes, shape: WorldShape, x: number, y: number, z: number): string {
y = Math.min(Math.max(y, shape.min_y), shape.max_y - 1);
const section = biomes.sections[(y - shape.min_y) >> 4];
return biomes.palette[typeof section === "number" ? section : section[biome_index(x, y, z)]];
}
// a chunk's blocks and biomes, its dimension's whole height
export class ChunkColumn {
readonly x: number;
readonly z: number;
readonly shape: WorldShape;
// from min_y up
readonly sections: Section[];
// x then z
readonly biomes: string[];
readonly biomes: ColumnBiomes;
constructor(x: number, z: number, shape: WorldShape, sections: Section[], biomes: string[]) {
if (sections.length * SECTION_HEIGHT !== shape.height) {
constructor(x: number, z: number, shape: WorldShape, sections: Section[], biomes: ColumnBiomes) {
if (sections.length * SECTION_HEIGHT !== shape.height || biomes.sections.length !== sections.length) {
throw new Error(`Chunk ${x}, ${z} has ${sections.length} sections, its dimension ${shape.height / 16}`);
}
this.x = x;
@@ -76,13 +151,39 @@ export class ChunkColumn {
this.biomes = biomes;
}
// from every block's value in one array, y from min_y then z then x, which is copied
// from every block's value in one array, y from min_y then z then x, which is copied, and the biomes of its cells,
// see column_biomes
static from_flat(x: number, z: number, shape: WorldShape, blocks: Uint32Array, biomes: string[]): ChunkColumn {
const sections: Section[] = [];
for (let start = 0; start < blocks.length; start += SECTION_VOLUME) {
sections.push(Section.of(blocks.subarray(start, start + SECTION_VOLUME)));
}
return new ChunkColumn(x, z, shape, sections, biomes);
return new ChunkColumn(x, z, shape, sections, column_biomes(biomes, sections.length));
}
// the biome at world coordinates. above and below the dimension it's the one at its top or bottom
biome_at(x: number, y: number, z: number): string {
return biome_in(this.biomes, this.shape, x, y, z);
}
// sets the biome of the cell y is in. does nothing outside the dimension's height
set_biome(x: number, y: number, z: number, biome: string) {
if (!this.shape.contains(y)) return;
const { palette, sections } = this.biomes;
let index = palette.indexOf(biome);
if (index === -1) {
if (palette.length === MAX_COLUMN_BIOMES) {
throw new Error(`More than ${MAX_COLUMN_BIOMES} biomes in a chunk`);
}
index = palette.push(biome) - 1;
}
const s = (y - this.shape.min_y) >> 4;
const section = sections[s];
if (section === index) return;
// a copy, since other sections and other chunks' copies can share the array
const changed = typeof section === "number" ? new Uint8Array(SECTION_BIOMES).fill(section) : section.slice();
changed[biome_index(x, y, z)] = index;
sections[s] = changed;
}
// the block value at world coordinates, air outside the dimension's height
@@ -99,7 +200,9 @@ export class ChunkColumn {
clone(): ChunkColumn {
const sections = this.sections.map((section) => new Section(section.blocks?.slice() ?? section.value));
return new ChunkColumn(this.x, this.z, this.shape, sections, [...this.biomes]);
// the sections' biome arrays are never changed, so they can be shared
const biomes = { palette: [...this.biomes.palette], sections: [...this.biomes.sections] };
return new ChunkColumn(this.x, this.z, this.shape, sections, biomes);
}
// every block's value in one array, like from_flat's
@@ -117,49 +220,51 @@ export function to_flat(sections: Section[]): Uint32Array {
return blocks;
}
// bits per block of a section's palette, the smallest that fits. more than 256 kinds are written whole
// bits per value of a paletted container's palette, the smallest that fits. more than 256 kinds are written whole
const PALETTE_BITS = [1, 2, 4, 8];
const DIRECT_BITS = 32;
// a column's biomes and sections, as level_chunk and saved chunks hold them (see common/chunk_streaming.ts and
// server/game/saves.ts). the network sends block values, a save the index of each block in its palette:
// a column's sections and biomes, as level_chunk and saved chunks hold them (see common/chunk_streaming.ts and
// server/game/saves.ts). the network sends block values, a save the index of each block in its own palette:
//
// u16 biome palette size, then each biome: u8 length and the id as utf-8
// u8 each column's biome palette index, x then z. left out when there's one biome
// u16 how many sections there are
// each section of 16x16x16 blocks, bottom first, a paletted container:
// u8 bits per block
// 0: u32 the value of every block
// 1, 2, 4, 8: u16 palette size, u32 each value, then each block's palette index packed from the low bits up
// 32: u32 each block's value
export function write_column(writer: Writer, sections: Section[], biomes: string[]) {
if (biomes.length !== CHUNK_AREA) throw new Error(`${biomes.length} biomes in a chunk`);
const biome_palette = [...new Set(biomes)];
writer.u16(biome_palette.length);
for (const biome of biome_palette) writer.string(biome);
if (biome_palette.length > 1) {
for (const biome of biomes) writer.u8(biome_palette.indexOf(biome));
// each section, bottom first:
// its 16x16x16 blocks, a paletted container of their values
// its biome cells (see biome_index), a paletted container of their biome palette indexes
//
// a paletted container, like minecraft's:
// u8 bits per value
// 0: u32 every value
// 1, 2, 4, 8: u16 palette size, u32 each value, then each one's palette index packed from the low bits up
// 32: u32 each value
export function write_column(writer: Writer, sections: Section[], biomes: ColumnBiomes) {
if (biomes.sections.length !== sections.length || biomes.palette.length > MAX_COLUMN_BIOMES) {
throw new Error("Bad biomes");
}
writer.u16(biomes.palette.length);
for (const biome of biomes.palette) writer.string(biome);
writer.u16(sections.length);
for (const section of sections) write_section(writer, section);
sections.forEach((section, i) => {
write_paletted(writer, section.blocks ?? section.value);
write_paletted(writer, biomes.sections[i]);
});
}
function write_section(writer: Writer, section: Section) {
const blocks = section.blocks;
if (blocks === null) {
function write_paletted(writer: Writer, values: number | Uint32Array | Uint8Array) {
if (typeof values === "number") {
writer.u8(0);
writer.u32(section.value);
writer.u32(values);
return;
}
// each block's palette index. runs of the same block are common, so it's looked up when the block changes
const indexes = new Uint16Array(SECTION_VOLUME);
// each value's palette index. runs of the same value are common, so it's looked up when the value changes
const indexes = new Uint16Array(values.length);
const palette: number[] = [];
const index_of = new Map<number, number>();
let last_value = blocks[0] + 1;
let last_value = values[0] + 1;
let last_index = 0;
for (let i = 0; i < SECTION_VOLUME; i++) {
const value = blocks[i];
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (value !== last_value) {
last_value = value;
let index = index_of.get(value);
@@ -180,7 +285,7 @@ function write_section(writer: Writer, section: Section) {
const bits = PALETTE_BITS.find((bits) => palette.length <= 1 << bits);
if (bits === undefined) {
writer.u8(DIRECT_BITS);
for (const value of blocks) writer.u32(value);
for (const value of values) writer.u32(value);
return;
}
@@ -188,43 +293,59 @@ function write_section(writer: Writer, section: Section) {
writer.u16(palette.length);
for (const value of palette) writer.u32(value);
const per_byte = 8 / bits;
for (let i = 0; i < SECTION_VOLUME; i += per_byte) {
for (let i = 0; i < values.length; i += per_byte) {
let byte = 0;
for (let j = 0; j < per_byte; j++) byte |= indexes[i + j] << (j * bits);
writer.u8(byte);
}
}
// throws when it isn't what write_column writes. map turns each value read into what the sections hold, and can throw
// for a value that's wrong
export function read_column(reader: Reader, map: (value: number) => number): { sections: Section[]; biomes: string[] } {
const biome_palette: string[] = [];
const biome_count = reader.u16();
if (biome_count === 0) throw new Error("No biomes");
for (let i = 0; i < biome_count; i++) biome_palette.push(reader.string());
const biomes: string[] = new Array(CHUNK_AREA);
for (let column = 0; column < CHUNK_AREA; column++) {
const index = biome_count > 1 ? reader.u8() : 0;
if (index >= biome_count) throw new Error("Bad biome index");
biomes[column] = biome_palette[index];
// throws when it isn't what write_column writes. map turns each block value read into what the sections hold, and can
// throw for a value that's wrong. flat_biomes reads what came before biomes had heights (format 3 saves): after the
// biome palette, a u8 palette index per column (x then z) when there's more than one, and sections of blocks alone
export function read_column(
reader: Reader,
map: (value: number) => number,
flat_biomes = false,
): { sections: Section[]; biomes: ColumnBiomes } {
const palette: string[] = [];
const palette_size = reader.u16();
if (palette_size === 0 || palette_size > MAX_COLUMN_BIOMES) throw new Error("Bad biome palette");
for (let i = 0; i < palette_size; i++) palette.push(reader.string());
const biome = (index: number) => {
if (index >= palette_size) throw new Error("Bad biome index");
return index;
};
let columns: string[] | undefined;
if (flat_biomes) {
columns = Array.from({ length: CHUNK_AREA }, () => palette[palette_size > 1 ? biome(reader.u8()) : 0]);
}
const count = reader.u16();
if (count * SECTION_HEIGHT > HIGHEST_Y - LOWEST_Y) throw new Error(`Too many sections, ${count}`);
const sections: Section[] = [];
for (let i = 0; i < count; i++) sections.push(read_section(reader, map));
return { sections, biomes };
const cells: SectionBiomes[] = [];
for (let i = 0; i < count; i++) {
sections.push(new Section(read_paletted(reader, new Uint32Array(SECTION_VOLUME), map)));
if (!flat_biomes) cells.push(read_paletted(reader, new Uint8Array(SECTION_BIOMES), biome));
}
share_same(cells);
return { sections, biomes: columns ? column_biomes(columns, count) : { palette, sections: cells } };
}
function read_section(reader: Reader, map: (value: number) => number): Section {
// into values, unless they're all one, which is returned instead
function read_paletted<T extends Uint32Array | Uint8Array>(
reader: Reader,
values: T,
map: (value: number) => number,
): number | T {
const bits = reader.u8();
if (bits === 0) return new Section(map(reader.u32()));
const blocks = new Uint32Array(SECTION_VOLUME);
if (bits === 0) return map(reader.u32());
if (bits === DIRECT_BITS) {
for (let i = 0; i < SECTION_VOLUME; i++) blocks[i] = map(reader.u32());
return new Section(blocks);
for (let i = 0; i < values.length; i++) values[i] = map(reader.u32());
return values;
}
if (!PALETTE_BITS.includes(bits)) throw new Error(`Bad bits per block ${bits}`);
if (!PALETTE_BITS.includes(bits)) throw new Error(`Bad bits per value ${bits}`);
const palette: number[] = [];
const size = reader.u16();
@@ -232,15 +353,15 @@ function read_section(reader: Reader, map: (value: number) => number): Section {
for (let i = 0; i < size; i++) palette.push(map(reader.u32()));
const per_byte = 8 / bits;
const mask = (1 << bits) - 1;
for (let i = 0; i < SECTION_VOLUME; i += per_byte) {
for (let i = 0; i < values.length; i += per_byte) {
const byte = reader.u8();
for (let j = 0; j < per_byte; j++) {
const index = (byte >> (j * bits)) & mask;
if (index >= size) throw new Error("Bad palette index");
blocks[i + j] = palette[index];
values[i + j] = palette[index];
}
}
return new Section(blocks);
return values;
}
// per column of a chunk (z * 16 + x), the highest y that isn't air, min_y - 1 for an empty one, then the lowest y with