Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem
Module

@std/terrain

Heightmap terrain and noise caves the fast way: noise sampled for the whole chunk at once, the ground under the lowest column laid as whole slabs, and every other column run written in one call.

local terrain = require("@std/terrain")

Functions

terrain.caves(chunk: types.Chunk, options: terrain.Caves)
#

Hollows caves out of whatever the chunk holds between fromY and toY.

terrain.heights(chunk: types.Chunk, shape: terrain.Shape) -> { number }
#

The ground height of every column in the chunk, the column at local x, z at index x * 16 + z + 1. The table is reused by the next call.

local gen = require("@ember/gen")
local terrain = require("@std/terrain")

-- The fast way to the same kind of terrain as a hand-written generator: `heights` samples the whole
-- chunk at once and `layers` writes every column in one call. Caves go in last.
local blocks: terrain.Layers = {
	stone = gen.block("minecraft:stone"),
	under = gen.block("minecraft:dirt"),
	surface = gen.block("minecraft:grass_block"),
	water = gen.block("minecraft:water"),
	floor = gen.block("minecraft:bedrock"),
	sea = 62,
}
local ground = gen.noise("ground", 5)
local tunnels = gen.noise("caves", 2)

gen.generator(function(chunk)
	local columns = terrain.heights(chunk, { noise = ground, scale = 160, base = 64, amplitude = 24 })
	terrain.layers(chunk, columns, blocks)
	terrain.caves(chunk, { noise = tunnels, fromY = chunk.minY + 8, toY = 56 })
end)
terrain.layers(chunk: types.Chunk, columns: { number }, blocks: terrain.Layers)
#

Builds every column up to its height from heights: floor, stone, under, the surface, then water to the sea.

Types