Files
bworld/docs/modding/worldgen/terrain.md
T

16 KiB

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, and a biome source picks biomes. The names and fields follow Minecraft's where the idea is the same, so the Minecraft Wiki 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 .bmod 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

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 and features, like the base game's trees, which grow as the biome's trees say.

Noises

worldgen/noise/erosion.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:

{
	"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:

{
	"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 bworld:overworld. The server doesn't start without it.

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:

{
	"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:

{
	"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 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.

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. Every chunk generated is saved, so what's been generated stays as it was.

Engine access mods can hook engine.common.OverworldTerrain, 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.