Procedural Content Generation (AI)
Summary
Procedural content generation (PCG) is the process of using an AI system to author aspects of a game that a human designer would typically create — from terrain and levels to quests and items. The designer’s task shifts from crafting content directly to building a system that encodes design knowledge as rules, grammars, or fitness functions. Two primary motivations are replayability (fresh content each run) and adaptability (content fitted to player skill or preference) (Smith, Game AI Pro 2, see source-game-ai-pro-2).
PCG is closely related to procedural-generation (the game-design pattern perspective). This page focuses on the AI techniques and engineering trade-offs; the design-side framing lives there.
Key ideas
The control spectrum
PCG approaches sit on a spectrum from low control/high emergence to high control/low surprise:
| Approach | Control | Speed | Notes |
|---|---|---|---|
| Simulation-based | Lowest | Slow | Emergent, content history, can run live |
| Constructionist | Medium | Fast | Ad hoc algorithms, designer expertise baked in |
| Grammar-based | Medium–high | Fast | Formal rules; generate-and-test for soft constraints |
| Optimisation-based | Medium (soft constraints) | Slow | Evolutionary or human-in-loop; fitness function required |
| Constraint-driven | Highest (hard constraints) | Variable | Declarative; NP-complete in general, fast for small domains |
Approaches can be mixed at different abstraction layers — for example, a grammar generates room layouts while a constraint solver places items inside rooms.
Knowledge representation
The building blocks the generator assembles are as important as the algorithm:
- Experiential chunks — large preauthored pieces that can stand alone (e.g. Robot Unicorn Attack level segments). High artist control; players quickly recognise repeated chunks.
- Templates — preauthored structures with gaps for the AI to fill. Balance between control and variety; described as “the Mad Libs of PCG.”
- Components — pattern-level building blocks that require broader context (e.g. enemy archetypes). Reduces pattern recognition risk but makes the algorithm carry more weight.
- Subcomponents — raw assets (tile art, individual particles) with no semantic information. Rarely used alone; most generators need some semantic layer.
Simulation-based PCG
Start with an initial world state and run a set of operators forward in time. Dwarf Fortress generates entire planets this way: elevation fractal → temperature/rainfall/drainage/salinity layers → biome classification → river erosion → civilisation simulation (Adams, Game AI Pro 2, see source-game-ai-pro-2).
Advantages: produces a content history that can be replayed; can run during gameplay to react to player action. Disadvantage: no direct control over output; requires a generate-and-test layer if constraints must be met.
Four Dwarf Fortress simulation principles
- Don’t overplan. Get something running early; allow the simulation to surprise you.
- Break down the system. Model individual fields (temperature, rainfall, drainage) separately; let their interaction determine emergent properties like biomes.
- Don’t overcomplicate. Operate at the level of what the player sees, or one layer below. Complexity only where it matters.
- Base the model on real-world analogs. Reality provides ground truth for debugging; if the simulation deviates from reality, the deviation signals a modelling problem.
Grammar-based PCG
A formal grammar splits the generation problem into (a) production rules and (b) an interpreter. Rules define a generative space; the interpreter samples from it. Nonterminal symbols expand into terminals (leaf content) until fully resolved.
Grammars generate content quickly. The main failure modes are:
- Overgeneration — the grammar can produce content that was not intended. Fix: generate-and-test with acceptance criteria.
- Undergeneration — over-constraining the rules to avoid overgeneration narrows variety too much.
The Launchpad level generator for Mario-style platformers generates 10,000 candidate levels in seconds using an unoptimised grammar.
Optimisation-based PCG
A search process maximises an evaluation function over candidate content. Evolutionary algorithms are common in research. All optimisation-based systems require:
- A fitness function that numerically captures content quality.
- Careful knowledge representation (the representation shapes what the algorithm can evolve).
- A decision about human involvement: human-in-the-loop approaches (e.g. Galactic Arms Race using weapon-usage preference) avoid needing a purely mathematical fun definition.
Constraint-driven PCG
Constraints declaratively specify what the content must be; a solver finds solutions. Commonsense constraints (no two objects occupying the same position) must be specified alongside game-specific constraints. Answer Set Programming is one common substrate.
Runtime is unpredictable but scales better with more constraints (each constraint prunes the search space). Small-to-medium domains (mazes, grid puzzles, simple platformers) can solve in seconds.
In practice
Choosing an approach
Start from two questions:
- How tight are the design guarantees needed? — Hard guarantees (always playable) push toward constraint-driven or generate-and-test. Soft guarantees (usually about the right difficulty) suit grammar-based or optimisation-based.
- Online or offline? — Online (runs while the player plays) requires fast algorithms (constructionist, grammar-based). Offline permits slower approaches.
Work top-down (design extreme-case examples first, distil patterns into building blocks) and bottom-up (pick a technique and iterate rules) simultaneously.
Expressive range analysis
The primary debugging technique for any PCG system. Generate a large sample of content, measure key quality metrics on each piece, and plot histograms or heatmaps. This reveals:
- What extreme content the generator can produce.
- Whether the generator is biased toward particular regions of the content space.
- How changes to the generator shift the distribution.
Compare generator versions by overlaying their expressive range charts. Automated playtesting agents can supply richer metrics than hand-written formulae.
Unity integration notes
There is no Unity-native PCG framework. Common entry points:
// Minimal constructionist room generator — Unity C#
public class DungeonGenerator : MonoBehaviour {
[SerializeField] private GameObject[] roomPrefabs;
[SerializeField] private int roomCount = 10;
void Start() {
Vector3 position = Vector3.zero;
for (int i = 0; i < roomCount; i++) {
// Select and place a random room chunk
GameObject roomPrefab = roomPrefabs[Random.Range(0, roomPrefabs.Length)];
Instantiate(roomPrefab, position, Quaternion.identity);
position += new Vector3(roomPrefab.GetComponent<Room>().Width, 0, 0);
}
}
}For grammar-based systems, keep the rule set (a data structure or ScriptableObject list) separate from the interpreter (a MonoBehaviour or static class). Seed Random.InitState(seed) at start to enable reproducible debugging.
Trade-offs
| Simulation | Constructionist | Grammar | Optimisation | Constraint | |
|---|---|---|---|---|---|
| Design control | Low | Medium | Medium | Medium | High |
| Speed | Slow | Fast | Fast | Slow | Variable |
| Hard guarantees | No | No | No | No | Yes |
| Authoring cost | Low code, high tuning | High code | Medium | High (fitness fn) | High (constraints) |
| Debug difficulty | High | High | Medium | High | Medium–high |
Examples
- Elite (1984) — deterministic PCG as data compression for an 8-bit galaxy. The earliest notable PCG game.
- Rogue and roguelikes — constructionist dungeon generators; rooms connected by corridors.
- Civilization V — parameterised world generator; player sets age/temperature/landmass type. PCG frames player strategy, not the full experience.
- Borderlands — weapon component PCG producing vast item variety.
- Minecraft — simulation-based terrain with Perlin noise; also ongoing during play.
- Dwarf Fortress — multi-layer simulation PCG producing full world histories before play begins.
- Spelunky — grammar-based room generation with generate-and-test for playability.
Related
procedural-generation · genetic-algorithms · cellular-automata · ai-experience-management · player-modelling · game-analytics · second-order-design · dwarf-fortress · binding-of-isaac · spelunky · source-game-ai-pro-2