Files
bworld/docs/modding/worldgen/terrain.md
T
2026-11-06 22:49:43 -03:00

316 lines
20 KiB
Markdown

# Terrain
The shape of the world, which biome each column gets, what its ground is made of and where water goes are all data, in a
mod's `worldgen/` folders. The engine only knows how to evaluate them. The base game's overworld is in
`mods/bworld/worldgen/`, and a mod can add biomes to it with rules of its own.
It works like Minecraft's: noises feed **density functions**, a **noise settings** file puts them together into a
generator, a **biome source** picks biomes, and a **dimension** says which generator the world uses. The names and
fields follow Minecraft's where the idea is the same, so the [Minecraft Wiki](https://minecraft.wiki/w/Density_function)
explains most of them. Where bworld differs, it says so below.
These files are server only: they're left out of the copy of a `.cubi` players download.
```
my_mod/
worldgen/noise/<path>.json # noises
worldgen/density_function/<path>.json # named density functions
worldgen/noise_settings/<path>.json # generators
worldgen/biome_source/<path>.json # biome rules, or rules added to another mod's
worldgen/dimension/<path>.json # which generator the world uses
```
Ids can have folders, like `bworld:overworld/erosion` in `worldgen/density_function/overworld/erosion.json`.
## How a Chunk Is Made
1. The generator's `terrain` and `caves` density functions are sampled at the corners of cells, `cell_width` blocks
across and each one's `cell_height` up, and blended in between. A block is solid where both are above 0.
2. Each column's biome is the first biome rule that holds, from its climate, the height of its ground and whether it's
steep.
3. Each solid block goes through the `surface_rule`, which gives grass, sand, snow and so on, or `default_block`.
4. Each empty block below the surface of a `fluid` gets it, like water up to sea level.
5. Then [ores](index.md#ores) and [features](index.md#features), like the base game's trees, which grow as the biome's
`trees` say.
## Noises
`worldgen/noise/erosion.json`:
```json
{
"format_version": 1,
"noise": {
"id": "bworld:erosion",
"wavelength": 1280,
"amplitudes": [1, 1, 0, 1, 1],
"quantiles": [-0.917, -0.48, -0.393, "...", 0.917]
}
}
```
| Field | Meaning | Default |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| `wavelength` | The size in blocks of the first octave's features. | required |
| `amplitudes` | Each octave's strength. Each octave is twice as fine as the one before. 0 skips one. | required |
| `dimensions` | `2` for x and z, `3` for x, y and z. | `2` |
| `vertical_stretch` | For 3D noises, how much slower it changes up and down than across. | `1` |
| `quantiles` | The noise's percentiles, every 5%, for spreading it evenly from -1 to 1 (see `even` below). Or another noise's id to use its table. | none |
| `seed_name` | What it's seeded with, together with the world's seed. | the id's path |
Unlike Minecraft's, bworld's noises are simplex noise sized by `wavelength` instead of a first octave. Octave noise is
bunched up in the middle of its range. `deno run tools/noise_quantiles.ts '[1,1,0,1,1]'` measures the `quantiles` for a
set of amplitudes, so `"even": true` can spread it out.
## Density Functions
A density function gives a number for every block. A file in `worldgen/density_function/` is one with an `id`:
```json
{
"format_version": 1,
"density_function": {
"id": "bworld:overworld/erosion",
"type": "mul",
"argument1": {
"type": "noise",
"noise": "bworld:erosion",
"even": true,
"x": "bworld:overworld/warp_x",
"z": "bworld:overworld/warp_z"
},
"argument2": 1.05
}
}
```
Wherever a density function goes, a number is a constant and a string is a named density function's id.
| Type | Fields | Gives |
| ---------------------------------------- | ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `constant` | `argument` | The number. A bare number does the same. |
| `x`, `y`, `z` | | The block's position. bworld's own. |
| `noise` | `noise`, `even`, `x`, `y`, `z` | The noise at the block, or at the position `x`, `y` and `z` give. `even` spreads it by its quantiles. |
| `add`, `sub`, `mul`, `div`, `min`, `max` | `argument1`, `argument2` | The two put together. `sub` and `div` are bworld's own. |
| `abs` | `argument` | Without its sign. |
| `clamp` | `input`, `min`, `max` | `input`, kept from `min` to `max`. |
| `smoothstep` | `input`, `from`, `to` | 0 at `from`, 1 at `to`, a smooth curve in between. bworld's own. |
| `spline` | `spline`: `{ coordinate, points }` | A smooth curve through `points` (`{ location, value, derivative }`) along `coordinate`. A `value` can be another spline, like Minecraft's. `derivative` is 0 when left out. |
| `range_choice` | `input`, bounds, `when_in_range`, `when_out_of_range` | One or the other by whether `input` is within the bounds: `min_inclusive`, `min_exclusive`, `max_inclusive`, `max_exclusive`, any of them. A bound can be a density function. |
| `cache_2d` | `argument` | The same, worked out once per column. Only for functions that don't change with y. |
Named density functions that don't change with y are worked out once per column on their own, so a function used by
several others, like `bworld:overworld/continents`, costs the same as using it once.
Every operation takes two arguments, so how its numbers round is written down. Two generators that do the same sums in
the same order make the same world block for block. The base game's terrain was moved from code into these files that
way, and `tests/worldgen_golden_test.ts` checks it never changes by accident.
## Noise Settings
A generator. The base game's is `worldgen/noise_settings/overworld.json`:
```json
{
"format_version": 1,
"noise_settings": {
"id": "bworld:overworld",
"sea_level": 64,
"default_block": "bworld:stone",
"cell_width": 4,
"terrain": { "density": "bworld:overworld/terrain", "cell_height": 8 },
"caves": { "density": "bworld:overworld/caves", "cell_height": 4 },
"preliminary_surface": "bworld:overworld/height",
"climate": {
"continentalness": "bworld:overworld/continents",
"temperature": "bworld:overworld/temperature",
"humidity": "bworld:overworld/humidity",
"valley": "bworld:overworld/ridges_folded",
"flatness": "bworld:overworld/erosion_folded",
"jaggedness": "bworld:overworld/jaggedness"
},
"steep": 3.5,
"biome_source": "bworld:overworld",
"fluids": [
{ "block": "bworld:water", "below": 64, "where": "open_sky" },
{
"block": "bworld:water",
"below": 64,
"where": { "function": "bworld:overworld/underground_river", "min_exclusive": 0 }
}
],
"surface_rule": { "...": "see below" }
}
}
```
| Field | Meaning |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `sea_level` | Where the sea's surface is, for surface rules. |
| `default_block` | What solid ground is where the surface rule doesn't say otherwise. |
| `cell_width` | How far apart across `terrain` and `caves` are sampled, 1 to 16. |
| `terrain`, `caves` | Their density function, and how far apart up they're sampled. Solid where both are above 0. |
| `preliminary_surface` | The ground's height before 3D noise. Surface rules, `steep` and `/locatebiome` use it. |
| `climate` | Named values biome rules look at. Any names, except `surface` and `steep`. |
| `steep` | A column is steep when its preliminary surface is this much higher or lower than a neighbor's. |
| `biome_source` | The biome source that picks its biomes. |
| `fluids` | What empty space becomes, the first that applies: below `below`, where there's `open_sky` (nothing but air above) or where a density function is in a range. |
| `surface_rule` | What solid blocks become. |
The world uses the generator its [dimension](#dimensions) names, the base game's is `bworld:overworld`.
## Biome Sources
The rules that pick a column's biome. The first that holds wins, so they go from the most particular to the most
general. The base game's, `worldgen/biome_source/overworld.json`, starts with rivers and oceans under water and ends
with `bworld:dark_forest`, which holds everywhere:
```json
{
"format_version": 1,
"biome_source": {
"id": "bworld:overworld",
"rules": [
{
"id": "bworld:river_bed_frozen",
"when": {
"surface": { "max_exclusive": 63 },
"valley": { "max_exclusive": 0.06 },
"continentalness": { "min_exclusive": -0.2 },
"temperature": { "max_exclusive": -0.65 }
},
"biome": "bworld:frozen_river"
},
{ "id": "bworld:dark_forest", "biome": "bworld:dark_forest" }
]
}
}
```
`when` holds when all its conditions do: a range of one of the generator's `climate` values, a range of `surface` (the y
of the column's top solid block), or `"steep": true` or `false`. Rules without `when` always hold.
This is bworld's own, not Minecraft's multi-noise biome source: a rule can look at how high the ground really is and
whether it's a cliff, which is how beaches, peaks and stony shores are placed.
### Adding Biomes
A mod adds its biomes with a biome source that `extends` another, putting each rule `before` or `after` one of its
rules. Lavender fields on all the land above the beaches that isn't mountains, before the climate picks forests and
plains:
```json
{
"format_version": 1,
"biome_source": {
"id": "lavender:fields",
"extends": "bworld:overworld",
"rules": [{
"id": "lavender:fields",
"before": "bworld:snowy_taiga",
"when": { "surface": { "min_inclusive": 70, "max_inclusive": 120 } },
"biome": "lavender:fields"
}]
}
}
```
Rules from several mods go in in load order. The biome itself is a [biome file](biomes.md) as usual.
## Surface Rules
What a solid block becomes, like Minecraft's surface rules: a rule gives a block or nothing, and the first one in a
`sequence` that gives a block wins. Blocks no rule gives anything for are the `default_block`.
| Rule | Fields | Gives |
| --------------- | ------------------------ | -------------------------------------------------------------------------------------------- |
| `sequence` | `sequence` | The first block its rules give. |
| `condition` | `if_true`, `then_run` | What `then_run` gives, when `if_true` holds. |
| `block` | `result_state: { Name }` | That block. |
| `biome_surface` | `part` | The column's biome's `top`, `filler`, `underwater_top` or `underwater_filler`. bworld's own. |
| Condition | Fields | Holds |
| --------------------------- | ---------- | ----------------------------------------------------------------------------------- |
| `stone_depth` | `offset` | When the solid blocks above it before air are at most `offset`. 0 is the top block. |
| `filler_depth` | | Within the biome's `filler_depth` below the top. bworld's own. |
| `above_preliminary_surface` | `offset` | At or above the preliminary surface plus `offset`. |
| `surface_underwater` | | When the column's ground is below `sea_level` - 1. bworld's own. |
| `steep` | | When the column is steep, see the noise settings' `steep`. |
| `bare_cliffs` | | When the biome's surface has `bare_cliffs`. bworld's own. |
| `y_at_least` | `function` | At or above the height a density function gives, like a snow line. bworld's own. |
| `biome` | `biome_is` | In one of the biomes. |
| `not` | `invert` | When `invert` doesn't. |
The base game's: within 20 blocks of the ground, the top block is the biome's underwater cover under water, snow (or
stone on cliffs) above the snow line, stone on bare cliffs, and the biome's `top` otherwise. Below it, the biome's
filler down to its `filler_depth`. Everything else is stone.
## Dimensions
A dimension is a world to play and the generator that makes it, like NeoForge's `dimension/` files (a level stem). The
base game's is `worldgen/dimension/overworld.json`:
```json
{
"format_version": 1,
"dimension": {
"id": "bworld:overworld",
"generator": "bworld:overworld",
"default": true,
"min_y": 0,
"max_y": 256,
"sky_light": true
}
}
```
| Field | Meaning |
| ----------- | ------------------------------------------------------------------------------------------------------- |
| `generator` | The [noise settings](#noise-settings) that generate it. |
| `default` | `true` for the dimension the world plays. False if unset. |
| `min_y` | The lowest y that has blocks. 0 if unset. |
| `max_y` | The first y above the world: blocks go up to `max_y - 1`. 256 if unset. |
| `sky_light` | Whether light comes from the sky. `false` for a dimension that's underground everywhere. True if unset. |
### Height
A dimension's blocks go from `min_y` up to `max_y - 1`, so `"min_y": -64, "max_y": 320` is 384 blocks tall. Both have to
be multiples of 16, `max_y` at least 16 above `min_y`, and the world has to fit in -2048 to 2048. They also have to be
multiples of the generator's `terrain` and `caves` `cell_height`, since the density is sampled every `cell_height`
blocks up from `min_y`. `check-mods` reports any of these, and the server doesn't start with them.
Outside the height there are no blocks: reading one gives air and placing one does nothing. Things that fall 64 blocks
below `min_y` are out of the world. Scripts read the height as `ctx.world.min_y` and `ctx.world.max_y`, on the server
and the client, and features as `chunk.min_y` and `chunk.max_y`.
Chunks are saved with the height they were made with. For now a chunk saved before a dimension's height changed can't be
read, and is generated again.
### Sky Light
Without sky light, there's none anywhere, in the open and above the world too: only blocks that give off light, like
torches, light it, and day and night don't change how bright it is. The sky is still drawn as it is in the overworld.
The world plays the dimension with `"default": true`. Exactly one dimension in the loaded mods can be the default: with
none, or more than one, the server doesn't start, and the error names them. A mod can have dimensions that aren't the
default, but nothing plays them yet. Since a mod can't change another mod's files yet, a mod with a default dimension of
its own, like a flat world, can't be loaded with the base game's for now.
There's one dimension per world for now, there's no travelling between them yet.
New players spawn in the middle of the world, on the first column, in rings of chunks around 0, 0, whose top block has
collision, looking down from `max_y`. Water has none, so they never spawn in the sea, and neither do plants without
collision.
## Changing the Base Game's Terrain
A mod can add biomes to it, but can't replace its files yet: an id defined by two mods is an error. Anything that
changes the terrain changes land not generated yet in existing worlds, the same as adding a
[feature](index.md#features). Every chunk generated is saved, so what's been generated stays as it was.
[Engine access](../misc/engine.md) mods can hook `engine.common.NoiseTerrain`, whose `density` and `cave` are called for
every cell corner.
`deno run -A tools/worldgen_preview.ts [seed] [out dir] [x z]` renders the base game's generator to PNG files, around
chunk `x z`.