Ember is discontinued. It still works, but it gets no more updates or support. Back to uayi.org
Home · Terrified Gorillas · Glimmerwood · Starfall Hollow · ‹ Prev Next ›
Ember
Download

Scripting

Procedural generation

Procedural levels are built by a script when the game starts, so every play can be different, or exactly the same when you fix the seed. Everything here is deterministic: seed 42 gives the same result on Windows, Linux, macOS and the web.

Random numbers you can repeat

local rng = random.new(42)          -- or random.new("my world") - any text works as a seed; random.new() is new every time
local n = rng:int(1, 6)              -- whole number, both ends included
local x = rng:float(-5, 5)           -- rng:float() is 0..1
if rng:chance(0.25) then ... end     -- true a quarter of the time
local colour = rng:pick({ "red", "green", "blue" })
rng:shuffle(list)                    -- in place
local p = rng:point(40, 20)          -- a vec3 in a 40 x 20 m area around 0

Each random.new is its own stream, so the maze, the coins and the enemies can each have one and adding an enemy doesn't change the maze.

Noise

Smooth randomness for hills, clouds, wobbling and anything that shouldn't jump about:

noise.seed(n) choose the noise (a number or text)
noise.perlin(x, y[, z]) about -1..1; nearby points give nearby values
noise.fbm(x, y[, octaves, lacunarity, gain]) layers of Perlin: big shapes plus detail (hills)
noise.ridged(x, y[, ...]) 0..1 with sharp ridges (mountain ranges)
noise.worley(x, y) 0..1 distance to the nearest random point (cells, stones, caves)

Scale the input to choose the feature size: noise.fbm(x * 0.02, z * 0.02) changes over about 50 m.

Landscapes

terrain.generate{ shape = "mountains", seed = 7, feature_size = 120, amplitude = 1, roughness = 0.5,
                  erosion = 0.5, paint = true, trees = true }

Rebuilds the first terrain in the scene (or entity = e): shapes are flat, hills, mountains, islands, canyons and dunes; erosion carves valleys; paint puts grass, rock, sand and snow where they belong; trees scatters trees. During play it changes a copy, so stopping the game in the editor puts the terrain back. Then use terrain.height(x, z) to place things on the ground.

Mazes

local grid = procgen.maze(12, 10, seed, 0.15)   -- 12 x 10 cells; 0.15 opens some dead ends into loops
for y = 1, grid.height do
  for x = 1, grid.width do
    if grid[y][x] then -- a wall
    end
  end
end

The grid is 2 * cols + 1 by 2 * rows + 1: cell (cx, cy) is at grid[2 * cy][2 * cx] and the cells between are walls or passages. With braid 0 there is exactly one way between any two cells. The Maze Run template builds walls from it, merging each row of wall cells into one box.

Scattering

for _, p in ipairs(procgen.scatter(200, 200, 15, seed)) do   -- 200 x 200 m, points at least 15 m apart
  local h = terrain.height(p.x, p.z)
  if h then scene.spawn{ mesh = "models/props/rock_a.glb", position = vec3(p.x, h, p.z) } end
end

Points are spread evenly (Poisson-disc), never closer than the distance you give: better than math.random for trees, rocks, coins and enemies.

The templates

Ember 1.1 documentation. Found a mistake? The source of this page is site/docs/procedural.md in the Ember source.