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
+18 -11
View File
@@ -3,7 +3,7 @@
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { SECTION_HEIGHT, type WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, column_heights, Section, SECTION_VOLUME } from "@/common/section.ts";
import { biome_in, ChunkColumn, column_heights, type ColumnBiomes, Section, SECTION_VOLUME } from "@/common/section.ts";
import { BlockRegistry, EverythingRegistry, light_tables } from "@/common/everything_registry.ts";
import { generate_raw_chunk, RawChunk, WorldgenSetup } from "@/common/generation.ts";
import { AIR_ID } from "@/common/protocol.ts";
@@ -114,7 +114,7 @@ export class ServerWorld {
#raw = new LruMap<number, RawChunk>(RAW_CACHE_SIZE);
#chunks = new LruMap<number, LoadedChunk>(CHUNK_CACHE_SIZE, (key, chunk) => this.#evict(key, chunk));
// what generation decided each column's biome is, and what it put in neighbors, kept for longer than the rest
#biomes = new LruMap<number, string[]>(SPILL_CACHE_SIZE);
#biomes = new LruMap<number, ColumnBiomes>(SPILL_CACHE_SIZE);
#spills = new LruMap<number, Int32Array>(SPILL_CACHE_SIZE);
// raw chunks generated on this thread because nothing had made them yet: with a worldgen pool, what still waits
// on generation. see GameServer's /tps
@@ -238,16 +238,17 @@ export class ServerWorld {
return nid === AIR ? undefined : EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid);
}
// like "bworld:plains", what generation decided
// a column's biome without generating its chunk, a little off near coasts and cliffs, see NoiseTerrain
estimate_biome(x: number, z: number): { biome: string; y: number } {
return this.worldgen.generator.for_seed(this.seed).estimate_biome(x, z);
// the highest y in a column where it's this biome without generating its chunk, a little off near coasts and
// cliffs, see NoiseTerrain.locate_biome
locate_biome(biome: string, x: number, z: number): number | undefined {
return this.worldgen.generator.for_seed(this.seed).locate_biome(biome, x, z, this.shape.min_y);
}
biome_at(x: number, z: number): string {
// like "bworld:plains", what generation decided. one per block across and every 4 blocks up, see ChunkColumn
biome_at(x: number, y: number, z: number): string {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.#chunk_biomes(chunk_x, chunk_z)[column_index(x, z)];
return biome_in(this.#chunk_biomes(chunk_x, chunk_z), this.shape, x, y, z);
}
// a saved chunk's are the ones saved with it, which loads it. other chunks' come from generation without building
@@ -704,13 +705,19 @@ export class ServerWorld {
}
return section;
});
let biomes = saved.biomes;
if (!biomes) {
let saved_biomes = saved.biomes;
if (!saved_biomes) {
// saved before chunks had them. saved again with them
const key = chunk_key(chunk_x, chunk_z);
biomes = [...this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes];
saved_biomes = this.#biomes.get(key) ?? this.#get_raw(chunk_x, chunk_z).column.biomes;
this.#mark_unsaved(chunk_x, chunk_z);
}
// past what the save has, its top or bottom section's go on
const last = saved_biomes.sections.length - 1;
const biomes = {
palette: [...saved_biomes.palette],
sections: sections.map((_, s) => saved_biomes.sections[Math.min(Math.max(offset + s, 0), last)]),
};
return { column: new ChunkColumn(chunk_x, chunk_z, this.shape, sections, biomes), unknown };
}