108 lines
4.4 KiB
Markdown
108 lines
4.4 KiB
Markdown
# 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.now` or anything else that changes between runs.** Use the chunk's
|
||
`rng` and 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:
|
||
|
||
1. **Terrain.** The ground, water, caves and trees. This is built into the engine for now, see below.
|
||
2. **Ores.** Every mod's `worldgen/ores.json`, in load order.
|
||
3. **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`:
|
||
|
||
```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:
|
||
|
||
```ts
|
||
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.
|
||
|
||
```ts
|
||
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+ with the Terralith
|
||
datapack: continentalness, erosion and weirdness noises set every column's height and roughness, a 3D density is carved
|
||
into caves, and on top of that come terraced plateaus, shattered hills, river valleys and gorges, jagged peaks and rare
|
||
sky islands. Biome ids are the keys of `BIOMES` in `common/worldgen/overworld.ts`, like `bworld:yosemite_cliffs` or
|
||
`bworld:skylands`.
|
||
|
||
`deno run -A tools/worldgen_preview.ts [seed]` renders it to PNG files.
|
||
|
||
> [!NOTE]
|
||
> `register_terrain` exists 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.
|