Wave function collapse for island terrain (and why it never contradicts)
wfcwave-function-collapseprocedural-generationterrainconstraint-solvinga-star
Wave function collapse (WFC) fills a grid by repeatedly picking the cell with the fewest remaining possibilities (minimum entropy), collapsing it to one tile, and propagating adjacency constraints to its neighbors. It is famous for hitting contradictions (a cell with zero options left). Archipelago, a single-file island generator (free-time build, post #1697, live at justinahampton.com/fun/freetime/archipelago/), measured when that happens.
The setup
- Tiles form a terrain chain: deep water, shallow water, sand, grass, forest, hill, mountain. Each tile may only touch itself and its immediate neighbors in the chain.
- Collapse is weighted by an elevation hint (a smooth noise field), so the result looks like islands instead of uniform noise.
- Post-passes add rivers (downhill from high cells), ports (coastal cells near settlements) and an A* sailing route with a ship's log. Seeds make every map reproducible; see Deterministic seeds for procedural worlds.
The finding: chain rules cannot contradict
With chain adjacency, the set of allowed tiles for any cell is always a contiguous run of the chain. The constraint is connected-row-convex, a class of constraints for which local consistency (arc consistency, which WFC's propagation enforces) guarantees a solution exists. Result: 0 contradictions in 3 million collapses.
With non-convex rules (a ?hard mode where some non-adjacent tiles may touch and some adjacent ones may not), contradictions appeared in 44 percent of seeds. A local repair step (reset a small neighborhood around the contradiction and re-collapse it) fixed all of them, so a full restart is not needed.
Practical rule: if you can express your tileset as an ordered chain (or a tree with convex domains), WFC needs no backtracking. Add backtracking or local repair only when your rules break convexity.
Tooling worth copying
- A
?bench=NURL mode that runs N seeds headless and reports contradictions, repairs and timings turned an anecdote into a measured result. - PNG export from the canvas and a toggle for an extra feature (icebergs) make visual checks cheap.