4.7 KiB
World Generation
The server generates chunks from the world's seed, and players get the finished chunks. Mods add to what it generates with ores (data) and features (a worldgen script).
Determinism
A save only keeps what players changed on top of the generated terrain. Everything else is generated again whenever a chunk loads, so a chunk has to come out exactly the same every time. If it doesn't, blocks players broke reappear and their builds end up half buried.
- Never use
Math.random,Date,performance.nowor anything else that changes between runs. Use the chunk'srngand noise helpers, which are seeded from the world's seed. - Don't keep state between chunks. Chunks generate in any order.
- Adding or removing a mod's features is safe for chunks that haven't been generated yet, but changes existing worlds' untouched terrain the next time it loads.
The Passes
Each chunk goes through three passes:
- Terrain. The ground, water, caves and trees. This is built into the engine for now, see below.
- Ores. Every mod's
worldgen/ores.json, in load order. - Features. Every mod's registered features, in load order.
The world is 256 blocks tall and the sea is at y 64.
Ores
worldgen/ores.json:
{
"format_version": 1,
"ores": [
{
"id": "copper_tools:rich_copper_ore",
"replaces": "bworld:stone",
"min_y": 5,
"max_y": 40,
"scale": 0.05,
"threshold": 0.72
}
]
}
Every block in the chunk that is replaces and between min_y and max_y becomes the first ore whose 3D noise,
sampled at (x, y, z) × scale, is above threshold. A smaller scale makes bigger veins, and a higher threshold
makes rarer ones. Each ore's noise is seeded with its id, so ores don't line up with each other.
Features
Features are functions that run on every chunk after terrain and ores. The worldgen script registers them:
import type { WorldgenContext } from "bworld/worldgen";
export function setup(gen: WorldgenContext) {
gen.register_feature("copper_tools:boulders", (chunk) => {
if (chunk.rng.next() > 0.1) return;
const x = chunk.x * 16 + Math.floor(chunk.rng.next() * 16);
const z = chunk.z * 16 + Math.floor(chunk.rng.next() * 16);
chunk.set_block(x, chunk.height_at(x, z) + 1, z, "bworld:stone");
});
}
Feature ids are in the mod's namespace. Each feature's rng is seeded with the world's seed, the chunk and the
feature's id, so adding another mod's feature doesn't change what this one generates.
interface FeatureChunk {
readonly x: number; // chunk coordinates. blocks are chunk.x * 16 to chunk.x * 16 + 15
readonly z: number;
readonly seed: string;
readonly rng: { next(): number }; // 0 to 1
noise_2d(name: string): (x: number, z: number) => number;
noise_3d(name: string): (x: number, y: number, z: number) => number;
height_at(x: number, z: number): number; // surface height, inside this chunk only
biome_at(x: number, z: number): string; // like "bworld:plains", inside this chunk only
get_block(x: number, y: number, z: number): string | undefined; // inside this chunk only
set_block(x: number, y: number, z: number, id: string): void; // up to one chunk away
}
All coordinates are world coordinates. noise_2d and noise_3d return seeded noise functions, the same for the same
name in every chunk, so features can make patterns that continue across chunk borders.
set_block can write up to one chunk away, so a tree near the border can grow into the next chunk. Writes into another
chunk only fill air, so the result doesn't depend on which chunk generates first.
Generated blocks don't run on_create, and worldgen scripts can't use the server or client API.
Terrain
The terrain itself is built into the engine (common/worldgen/). It works like Minecraft 1.18+, shaped after the
Tectonic mod: continents, erosion and ridges noises set every column's
height and roughness, and a 3D density is carved into caves. Oceans wind between large continents, with islands out at
sea. Mountain ranges rise where erosion is low, and rivers run along the valleys between ridges, going underground where
they reach mountains. The flatter land is split into regions with their own shapes: valleys and plateaus (with badlands
ridges where it's hot), tiered mesas, rolling hills and jungle pillars, and wetlands and desert dunes.
Biome ids are the keys of BIOMES in common/worldgen/overworld.ts, like bworld:plains or bworld:jagged_peaks.
deno run -A tools/worldgen_preview.ts [seed] [out dir] [x z] renders it to PNG files, around chunk x z.
Note
register_terrainexists in the API for mods that replace the terrain generator, but it throws for now: the terrain hasn't moved out of the engine yet.