Experimental

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

Report a problem
Module

@ember/gen

World generation: block ids as numbers, seeded noise, and the bulk writes a generator makes per chunk.

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

Fields

gen.air: number
#
read only

The number of air, which every chunk starts as.

Functions

gen.block(id: string, state: { [string]: types.StateValue }?) -> number
#

The number standing for a block, for the chunk writes. Look blocks up once, at the top of the generator, and keep the numbers. state sets block state properties, as in world.setBlock.

gen.generator(build: (chunk: types.Chunk) -> ())
#

Sets the function that builds each chunk: it gets the chunk, all air, and writes it. Chunks generate side by side on several copies of the script, so keep nothing between calls. Only while the generator script first runs, and once.

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

-- A generator script, named by `generator = "terrain.luau"` in world.toml. Rolling grass hills:
-- look up block numbers and noise once, at the top, then write each chunk column by column.
local STONE = gen.block("minecraft:stone")
local DIRT = gen.block("minecraft:dirt")
local GRASS = gen.block("minecraft:grass_block")
local hills = gen.noise("hills", 4)

gen.generator(function(chunk)
	for x = 0, 15 do
		for z = 0, 15 do
			-- Noise changes about once per unit, so divide block positions to stretch the hills.
			local worldX, worldZ = chunk.x * 16 + x, chunk.z * 16 + z
			local height = 64 + math.floor(hills:at(worldX / 96, worldZ / 96) * 16)
			chunk:column(x, z, chunk.minY, height - 3, STONE)
			chunk:column(x, z, height - 3, height, DIRT)
			chunk:set(x, height, z, GRASS)
		end
	end
end)
gen.interpolate(grid: buffer, cells: buffer, height: number)
#

Fills cells from a coarse density grid, the cheap way to shape caves and overhangs in Luau: compute the grid, one f64 every 4 blocks across and 8 up, then let this fill the 32 blocks in between each. grid holds 5 * 5 * (height / 8 + 1) f64s, at ((gx * 5 + gz) * (height / 8 + 1) + gy) * 8, where gx and gz run 0-4 and gy 0-height/8. cells gets 256 * height f32s, the block at local x, z and y blocks up at ((x * 16 + z) * height + y) * 4. height is a multiple of 8.

Range: height: 8..4096

gen.noise(salt: string, octaves: number?, persistence: number?) -> types.Noise
#

Seeded value noise in [-1, 1]. The world's seed and salt decide it, so two noises with different salts don't line up. octaves (default 1) layers it at doubling frequency, each layer weighted by persistence (default 0.5) times the last. Only while the generator script first runs.

Range: octaves: 1..8, persistence: 0..1