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
+3 -3
View File
@@ -79,10 +79,10 @@ export interface EntityLevel {
// the fluid there and how high it is, for fluids pushing things along
fluid_at(x: number, y: number, z: number): FluidInCell | undefined;
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined;
// the column's biome, like "bworld:plains"
biome_at(x: number, z: number): string;
// the biome at a block, like "bworld:plains"
biome_at(x: number, y: number, z: number): string;
// that biome's season now, see biome_season in common/worldgen/biomes.ts
season_at(x: number, z: number): Season;
season_at(x: number, y: number, z: number): Season;
add_entity(entity: Entity): void;
get_entity(id: string): Entity | undefined;
// entities whose feet are within the box around x, y, z
+4 -4
View File
@@ -116,12 +116,12 @@ export class EntityManager implements EntityLevel {
return this.#host.world.get_block_info(x, y, z);
}
biome_at(x: number, z: number) {
return this.#host.world.biome_at(x, z);
biome_at(x: number, y: number, z: number) {
return this.#host.world.biome_at(x, y, z);
}
season_at(x: number, z: number) {
return biome_season(this.biome_at(x, z), this.#host.world.time);
season_at(x: number, y: number, z: number) {
return biome_season(this.biome_at(x, y, z), this.#host.world.time);
}
fluid_at(x: number, y: number, z: number) {
+5 -6
View File
@@ -1607,8 +1607,8 @@ export class GameServer {
});
}
// like minecraft's /locate biome: columns LOCATE_STEP apart, in square rings out from x, z. the closest in the
// first ring that has one
// like minecraft's /locate biome: columns LOCATE_STEP apart, in square rings out from x, z, each from its ground
// down. the closest in the first ring that has one
#locate_biome(biome: string, x: number, z: number) {
for (let ring = 0; ring * LOCATE_STEP <= LOCATE_RADIUS; ring++) {
let best: { x: number; y: number; z: number; distance: number } | undefined;
@@ -1616,11 +1616,10 @@ export class GameServer {
for (let dz = -ring; dz <= ring; dz++) {
if (Math.max(Math.abs(dx), Math.abs(dz)) !== ring) continue;
const [cx, cz] = [x + dx * LOCATE_STEP, z + dz * LOCATE_STEP];
const found = this.world.estimate_biome(cx, cz);
const distance = Math.hypot(dx, dz);
if (found.biome === biome && (!best || distance < best.distance)) {
best = { x: cx, y: found.y, z: cz, distance };
}
if (best && distance >= best.distance) continue;
const y = this.world.locate_biome(biome, cx, cz);
if (y !== undefined) best = { x: cx, y, z: cz, distance };
}
}
if (best) return best;
+11 -8
View File
@@ -663,10 +663,10 @@ export class ModRuntime {
y,
z,
get biome() {
return game.world.biome_at(x, z);
return game.world.biome_at(x, y, z);
},
get season() {
return biome_season(game.world.biome_at(x, z), game.world.time);
return biome_season(game.world.biome_at(x, y, z), game.world.time);
},
get data() {
return game.world.get_tile(x, y, z)?.mod_data;
@@ -942,9 +942,12 @@ export class ModRuntime {
return { x: fx + 0, y: fy + 0, z: fz + 0 };
},
is_loaded: (x, z) => game().world.is_generated(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE)),
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
season_at: (x, z) =>
biome_season(game().world.biome_at(Math.floor(x), Math.floor(z)), game().world.time),
biome_at: (x, y, z) => game().world.biome_at(Math.floor(x), Math.floor(y), Math.floor(z)),
season_at: (x, y, z) =>
biome_season(
game().world.biome_at(Math.floor(x), Math.floor(y), Math.floor(z)),
game().world.time,
),
get min_y() {
return game().world.shape.min_y;
},
@@ -1507,7 +1510,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
};
},
get biome() {
return game.world.biome_at(Math.floor(player.x), Math.floor(player.z));
return game.world.biome_at(Math.floor(player.x), Math.floor(player.y), Math.floor(player.z));
},
get season() {
return biome_season(this.biome, game.world.time);
@@ -1676,10 +1679,10 @@ export function entity_ref(entity: Entity): EntityRef {
return { x: entity.x, y: entity.y, z: entity.z };
},
get biome() {
return entity.level.biome_at(Math.floor(entity.x), Math.floor(entity.z));
return entity.level.biome_at(Math.floor(entity.x), Math.floor(entity.y), Math.floor(entity.z));
},
get season() {
return entity.level.season_at(Math.floor(entity.x), Math.floor(entity.z));
return entity.level.season_at(Math.floor(entity.x), Math.floor(entity.y), Math.floor(entity.z));
},
get removed() {
return entity.removed;
+2 -2
View File
@@ -125,11 +125,11 @@ export class NaturalSpawner {
if (Math.hypot(world_spawn.x - fx, world_spawn.y - y, world_spawn.z - fz) < MIN_DISTANCE) continue;
if (!world.has_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE))) continue;
if (!spawn) {
spawn = this.#pick(world.biome_at(x, z));
spawn = this.#pick(world.biome_at(x, y, z));
if (!spawn) break;
size = spawn.min_count + this.#int(spawn.max_count - spawn.min_count + 1);
}
if (distance > MAX_DISTANCE || !spawns_in(spawn, world.biome_at(x, z))) continue;
if (distance > MAX_DISTANCE || !spawns_in(spawn, world.biome_at(x, y, z))) continue;
// in loaded chunks only, the light too, so trying never builds a chunk
if (!world.has_chunks_around(x, z, LIGHT_REACH)) continue;
if (!is_spawn_ground(world, x, y - 1, z)) continue;
+25 -14
View File
@@ -7,23 +7,31 @@
// u8 format, CHUNK_FORMAT
// i16 the bottom of the lowest section, the dimension's min_y when it was saved
// u32 palette size, then each entry: u8 id length, the id as utf-8, u16 state bits
// the biomes and the sections, bottom first, each block the index of its entry in the palette. see write_column in
// common/section.ts, which level_chunk uses too
// the sections' blocks and biomes, bottom first, each block the index of its entry in the palette. see
// write_column in common/section.ts, which level_chunk uses too
//
// blocks are saved by their string ids, so mods can come and go without a save's numbers meaning other blocks. every
// chunk the game generates is saved whole, like minecraft's, so a change to world generation only shows in chunks never
// generated before. a chunk loaded into a dimension with another height keeps the sections both have (see
// ServerWorld's #from_saved).
// formats 1 and 2 are still read. they're big-endian and from when the world was 0 to 256 high: after the palette, the
// formats 1, 2 and 3 are still read, with each column's biome all the way up. format 3 is this one from before biomes
// had heights, see read_column's flat_biomes. they're big-endian and from when the world was 0 to 256 high: after the palette, the
// index of every block from y 0 up, then z, then x, u8 each or u16 for more than 256 entries. format 2 then has a u8
// biome palette size, each biome as u8 length and utf-8, and a u8 biome index a column. format 1 has no biomes, but a
// bit per block for whether world generation made it, from when only chunks players changed were saved
import { CHUNK_AREA } from "@/common/constants.ts";
import { Reader, Writer } from "@/common/bytes.ts";
import { read_column, Section, SECTION_VOLUME, write_column } from "@/common/section.ts";
import {
column_biomes,
type ColumnBiomes,
read_column,
Section,
SECTION_VOLUME,
write_column,
} from "@/common/section.ts";
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
export const CHUNK_FORMAT = 3;
export const CHUNK_FORMAT = 4;
// how many blocks a chunk of formats 1 and 2 has
const FLAT_VOLUME = CHUNK_AREA * 256;
@@ -60,8 +68,8 @@ export interface ChunkBlocks {
min_y: number;
// the palette index of each block, a section at a time from min_y up
sections: Section[];
// each column's, see ServerWorld.biome_at. format 1 chunks don't have them
biomes?: string[];
// see ServerWorld.biome_at. format 1 chunks don't have them
biomes?: ColumnBiomes;
}
export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuffer> {
@@ -80,9 +88,11 @@ export function encode_chunk(chunk: Required<ChunkBlocks>): Uint8Array<ArrayBuff
// throws when the bytes aren't a chunk this can read
export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
const format = bytes[0];
if (format !== CHUNK_FORMAT && format !== 2 && format !== 1) throw new Error(`Unknown chunk format ${format}`);
const reader = new Reader(bytes.subarray(1), format === CHUNK_FORMAT);
const min_y = format === CHUNK_FORMAT ? reader.i16() : 0;
if (format < 1 || format > CHUNK_FORMAT) throw new Error(`Unknown chunk format ${format}`);
// formats 1 and 2 are big-endian, and flat
const flat = format <= 2;
const reader = new Reader(bytes.subarray(1), !flat);
const min_y = flat ? 0 : reader.i16();
if (min_y % SECTION_HEIGHT !== 0) throw new Error(`Bad min_y ${min_y}`);
const palette: [string, number][] = [];
const palette_size = reader.u32();
@@ -92,8 +102,8 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
return value;
};
if (format === CHUNK_FORMAT) {
const { sections, biomes } = read_column(reader, index);
if (!flat) {
const { sections, biomes } = read_column(reader, index, format === 3);
if (!reader.done) throw new Error("Wrong size");
return { palette, min_y, sections, biomes };
}
@@ -105,17 +115,18 @@ export function decode_chunk(bytes: Uint8Array): ChunkBlocks {
{ length: FLAT_VOLUME / SECTION_VOLUME },
(_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)),
);
let biomes: string[] | undefined;
let biomes: ColumnBiomes | undefined;
if (format === 1) {
reader.take(FLAT_VOLUME / 8);
} else {
const biome_palette: string[] = [];
const biome_palette_size = reader.u8();
for (let i = 0; i < biome_palette_size; i++) biome_palette.push(reader.string());
biomes = [...reader.take(CHUNK_AREA)].map((i) => {
const columns = [...reader.take(CHUNK_AREA)].map((i) => {
if (i >= biome_palette_size) throw new Error("Bad biome palette index");
return biome_palette[i];
});
biomes = column_biomes(columns, sections.length);
}
if (!reader.done) throw new Error("Wrong size");
return { palette, min_y, sections, biomes };
+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 };
}
+2 -2
View File
@@ -6,7 +6,7 @@
// server/worldgen_workers.ts). requests go nearest first, a worker has a couple at a time so priorities stay fresh, and what the
// game stops wanting is dropped before it starts
import type { RawChunk } from "@/common/generation.ts";
import { ChunkColumn, Section, type SectionData } from "@/common/section.ts";
import { ChunkColumn, type ColumnBiomes, Section, type SectionData } from "@/common/section.ts";
import { WorldShape } from "@/common/world_shape.ts";
import type { WorldgenBundle } from "@/common/worldgen_bundle.ts";
import { chunk_key } from "@/common/utils.ts";
@@ -19,7 +19,7 @@ export type WorkerToPool =
| { type: "ready" }
| { type: "failed"; error: string }
// a raw chunk's sections as their data, since what's sent between workers loses its class
| { type: "chunk"; x: number; z: number; sections: SectionData[]; spills: Int32Array; biomes: string[] }
| { type: "chunk"; x: number; z: number; sections: SectionData[]; spills: Int32Array; biomes: ColumnBiomes }
| { type: "error"; x: number; z: number; error: string };
// the game's end of a channel to a worker
+6 -1
View File
@@ -34,7 +34,12 @@ self.onmessage = (event: MessageEvent<{ port: MessagePort }>) => {
try {
const raw = (await loaded!).generate(x, z);
const sections = raw.column.sections.map((section) => section.data);
const buffers = sections.flatMap((data) => typeof data === "number" ? [] : [data.buffer]);
// sections can share their biomes' array
const buffers = new Set(
[...sections, ...raw.column.biomes.sections].flatMap((data) =>
typeof data === "number" ? [] : [data.buffer]
),
);
send({ type: "chunk", x, z, sections, spills: raw.spills, biomes: raw.column.biomes }, [
...buffers,
raw.spills.buffer,