Procedural Maps
With a seeded generator and fractal noise you can build whole worlds from one number. This lesson puts the Randomness section together into three classic techniques: a noise heightmap turned into an island, cellular automata caves, and object scattering with minimum spacing. Each produces the same map for the same seed, every time.
Heightmap islands
- Sample fBm noise at every tile to get a height.
- Subtract a falloff that grows toward the map's edge, so land sinks into the sea near the border and the map becomes an island.
- Turn heights into tiles with thresholds: deep water, shallow water, sand, grass, forest, mountain.
#include <iostream>
#include <cstdint>
#include <cmath>
#include <string>
#include <algorithm>
uint32_t hash(int x, int y, uint32_t seed) {
uint32_t h = seed * 3266489917u + uint32_t(x) * 374761393u + uint32_t(y) * 668265263u;
h ^= h >> 16; h *= 0x85EBCA6Bu; h ^= h >> 13; h *= 0xC2B2AE35u;
return h ^ (h >> 16);
}
double fade(double t) { return t * t * t * (t * (t * 6 - 15) + 10); }
double lerp(double a, double b, double t) { return a + (b - a) * t; }
double grad(int ix, int iy, double dx, double dy, uint32_t seed) {
static const double G[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0.7071, 0.7071}, {-0.7071, 0.7071}, {0.7071, -0.7071}, {-0.7071, -0.7071}};
const double* g = G[hash(ix, iy, seed) & 7];
return g[0] * dx + g[1] * dy;
}
double perlin(double x, double y, uint32_t seed) {
int x0 = int(std::floor(x)), y0 = int(std::floor(y));
double fx = x - x0, fy = y - y0, u = fade(fx), v = fade(fy);
return lerp(lerp(grad(x0, y0, fx, fy, seed), grad(x0 + 1, y0, fx - 1, fy, seed), u),
lerp(grad(x0, y0 + 1, fx, fy - 1, seed), grad(x0 + 1, y0 + 1, fx - 1, fy - 1, seed), u), v);
}
double fbm(double x, double y, uint32_t seed, int octaves) {
double sum = 0, amp = 1, freq = 1, norm = 0;
for (int i = 0; i < octaves; i++) {
sum += amp * perlin(x * freq, y * freq, seed + i);
norm += amp; amp *= 0.5; freq *= 2;
}
return sum / norm; // roughly -0.7..0.7
}
int main() {
const int W = 56, H = 20;
const uint32_t seed = 2024;
for (int y = 0; y < H; y++) {
std::string row;
for (int x = 0; x < W; x++) {
double nx = x / double(W) * 2 - 1, ny = y / double(H) * 2 - 1; // -1..1 across the map
double h = fbm(x * 0.09, y * 0.18, seed, 4) * 1.4 + 0.5; // ~0..1
double edge = std::max(std::fabs(nx), std::fabs(ny)); // 0 at center, 1 at border
h -= edge * edge * 1.0; // falloff
char c = h < 0.0 ? '~' : h < 0.12 ? '-' : h < 0.2 ? '.' : h < 0.42 ? ',' : h < 0.55 ? 'T' : '^';
row += c;
}
std::cout << row << "\n";
}
std::cout << "~ deep water - shallows . sand , grass T forest ^ mountain\n";
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~----...-~~~-.--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~-.,,..--~~-..,,.~~~~~~~~~~~~~--,,-~~~~~~~~~~~
~~~~~~~~~~~-.,,..---~.,,T,.--~~~~~~---,,TTT,,,-~~~~~~~~~
~~~~~~~~~~~~-,,...,...,,,,,.,,,...,,,,,T^^T,.-..~~~~~~~~
~~~~~~~~~~-.,,,,,,,.,,,,TTTTTT,,,,,TTTT^^T,..----~-~~~~~
~~~~~~~-~--.,,,TT,,,TTTT^^T^^^TTTTTTT^T^^T,,..--..-.-~~~
~~~~~~~---.,,,T^^^^^^^^^^^^TTT,,TT,TTT^^^^,,..----~~--~~
~~-----.,.,,,,,TTT^^^T^^^TT,,,,,TTT,,,T^^^,,,.-~~~~~~~~~
~~~~~~~~~~~.,TTTTT^TTTTTT,,,,,,,T^^^^^^^^T,,,-.-~~~~~~~~
~~~~~~~~~~~-,T^^^^T^^^T,....,,,,,,^^^^^^T,,,,---~~~~~~~~
~~~~~~~~~~-.,T^^^^^^^^T,...,,,TTTT^^^^^T,.----~~~~~~~~~~
~~~~~~~~~~--..,T^T,,,,,,.,,...,,,,TTT^^T,..~~~~~~~~~~~~~
~~~~~~~~~.,,,,,TTT,,,--.---~~~-,TTTT^^^^T,,,.~~~~~~~~~~~
~~~~~~~..,,,,,,,,,,..----~~~~~~.,,,,^^^^T,,,,-~~~~~~~~~~
~~~~~~~-...,.--~~...,..--~~~~~~~--,,^^TT,,,.-~~~~~~~~~~~
~~~~~~~~---~---~~-.----~~~~~~~~~~~.,,,,,,,.-.--~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-.--...---~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--~~~~~~~~~~~~
~ deep water - shallows . sand , grass T forest ^ mountain
Change the seed and you get a new island; keep it and you get this one forever. The two noise scales (0.09 for x, 0.18 for y) compensate for terminal characters being about twice as tall as they are wide.
Cellular automata caves
Noise makes good outdoor terrain; caves and dungeons often use a cellular automaton instead:
- Fill a grid randomly: each cell is a wall with probability ~45%.
- Repeat a few times: a cell becomes a wall if 5 or more of its 8 neighbors are walls, and a floor otherwise.
The random static clumps into smooth, organic caverns in 4 or 5 steps.
#include <iostream>
#include <cstdint>
#include <string>
#include <vector>
struct Pcg32 {
uint64_t state, inc;
Pcg32(uint64_t seed, uint64_t stream = 54) : state(0), inc((stream << 1) | 1) { next(); state += seed; next(); }
uint32_t next() {
uint64_t old = state;
state = old * 6364136223846793005ULL + inc;
uint32_t x = uint32_t(((old >> 18) ^ old) >> 27), rot = uint32_t(old >> 59);
return (x >> rot) | (x << ((-rot) & 31));
}
double nextDouble() { return next() / 4294967296.0; }
};
int main() {
const int W = 48, H = 16;
std::vector<std::string> g(H, std::string(W, '.'));
Pcg32 rng(77);
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
if (x == 0 || y == 0 || x == W - 1 || y == H - 1 || rng.nextDouble() < 0.45) g[y][x] = '#';
for (int step = 0; step < 4; step++) {
auto next = g;
for (int y = 1; y < H - 1; y++)
for (int x = 1; x < W - 1; x++) {
int walls = 0;
for (int dy = -1; dy <= 1; dy++)
for (int dx = -1; dx <= 1; dx++)
if ((dx || dy) && g[y + dy][x + dx] == '#') walls++;
next[y][x] = walls >= 5 ? '#' : (walls <= 3 ? '.' : g[y][x]);
}
g = next;
}
for (const auto& row : g) std::cout << row << "\n";
}################################################
################################################
###.....#####.......#########....###############
###......##..........#######.....####..#########
###...................####........##....########
######.................................#########
#######................................#########
########...............................####.####
##########..........##....###..........###...###
####..#####.........##...#####................##
####...####..............######...............##
####...###................#####...............##
####...##..................####...............##
####..###.................#####...###....###.###
#######################..#############..########
################################################
The rule here is the common "4-5" variant: 5+ wall neighbors makes a wall, 3 or fewer makes floor, and exactly 4 keeps the cell as it was. Real games then flood-fill to find the separate caves and either connect them with tunnels or keep only the largest.
Scattering objects: minimum spacing
Placing trees or enemies with plain random positions creates ugly clumps and gaps. Poisson-disk sampling places points randomly but never closer than a minimum distance. The simplest version, "dart throwing", tries random positions and rejects any that land too close to an existing point:
#include <iostream>
#include <cstdint>
#include <string>
#include <vector>
struct Pcg32 {
uint64_t state, inc;
Pcg32(uint64_t seed, uint64_t stream = 54) : state(0), inc((stream << 1) | 1) { next(); state += seed; next(); }
uint32_t next() {
uint64_t old = state;
state = old * 6364136223846793005ULL + inc;
uint32_t x = uint32_t(((old >> 18) ^ old) >> 27), rot = uint32_t(old >> 59);
return (x >> rot) | (x << ((-rot) & 31));
}
double nextDouble() { return next() / 4294967296.0; }
};
int main() {
const int W = 48, H = 12;
const double minDist = 4.0;
Pcg32 rng(5);
struct P { double x, y; };
std::vector<P> trees;
for (int attempt = 0; attempt < 2000; attempt++) {
P p{rng.nextDouble() * W, rng.nextDouble() * H};
bool ok = true;
for (const P& t : trees) {
double dx = (p.x - t.x) * 0.5, dy = p.y - t.y; // x is squeezed: characters are tall
if (dx * dx + dy * dy < minDist * minDist / 4) { ok = false; break; }
}
if (ok) trees.push_back(p);
}
std::vector<std::string> map(H, std::string(W, ' '));
for (const P& t : trees) map[int(t.y)][int(t.x)] = 'T';
for (const auto& row : map) std::cout << "|" << row << "|\n";
std::cout << trees.size() << " trees placed\n";
}| T T T T T T T|
| T T |
|T T |
| T T T T T T T T T |
| T |
| T T T T |
| T T T |
| T T T T T T T|
| T T T |
| T T T T T T |
| T T T |
|T T T T T T T|
54 trees placed
Dart throwing is fine for small maps. For large ones, Bridson's algorithm produces the same kind of distribution in linear time using a background grid. Combine it with the heightmap (only accept trees on grass tiles, more of them at higher moisture) and the world starts to look designed.
Making it a game
- Chunks: compute each chunk only from
(seed, chunkX, chunkY)with stateless hashing, so infinite worlds can be generated in any order and regenerated identically. - Validate: procedural levels must be playable. Check reachability with a flood fill or A*, and re-roll (seed + 1) if a level fails.
- Mix authored and procedural: most successful roguelikes stitch hand-made rooms together procedurally rather than generating every tile.
Tip: Show the seed in the pause menu or the debug overlay. When a tester reports "the map spawned me inside a wall", the seed lets you reproduce the exact map.
Test yourself
Why subtract a falloff that grows toward the edges of a heightmap?
- To make mountains taller
- So land sinks into water near the border, forming an island
- To reduce noise octaves
- To make the map tileable
In the cave automaton, what happens to a cell with 6 wall neighbors?
- It becomes floor
- It becomes a wall
- It stays as it was
- It is removed from the map
What does Poisson-disk sampling guarantee?
- Points on a regular grid
- Random points that are never closer than a minimum distance
- Exactly N points
- Points only on grass