# 3. Deeper Procgen Constraints Presets and Generate-and-Test
Part of [[Deeper Procedural Generation]]. Previous: [[2. Deeper Procgen Wave Function Collapse from Scratch]]. Next: [[4. Deeper Procgen Tile Sets Builders and Bake vs Runtime]].
## 3.1 Two kinds of rule
| Kind | Example | How it's enforced |
|---|---|---|
| **Local** | "road edges meet road edges" | sockets: WFC guarantees it for every pair of neighbours |
| **Fixed cell** | "the border is grass", "a crossroads at the centre", "the entrance is at (0, 5)" | **presets**: `Restrict` a cell's options before solving |
| **Global** | "every road connects", "at least 20 road tiles", "the exit is reachable from the entrance" | **generate-and-test**: solve, check, retry if it fails |
WFC is excellent at the first two and blind to the third, because propagation only ever looks at neighbours. No local rule can say "this road must eventually connect to that one".
## 3.2 Presets
`Restrict(x, y, mask)` ANDs a cell's options with a mask and propagates. Use `WfcModel.MaskOf` to build masks by predicate:
```csharp
var grass = model.MaskOf(t => t.id == "grass");
var anyRoad = model.MaskOf(t => t.walkable);
var cross = model.MaskOf(t => t.id == "cross");
MapGenerator.Preset preset = solver =>
{
for (var x = 0; x < 16; x++) { solver.Restrict(x, 0, grass); solver.Restrict(x, 9, grass); }
for (var y = 0; y < 10; y++) { solver.Restrict(0, y, grass); solver.Restrict(15, y, grass); }
solver.Restrict(8, 5, cross); // a landmark crossroads
solver.Restrict(1, 5, anyRoad); // something walkable next to the west entrance
};
```
Presets run at the start of **every** attempt, after `Reset`. Because each `Restrict` propagates immediately, a grass border already rules out every tile with a road edge facing outward in the next ring of cells, before the first collapse.
A preset that can't be satisfied (say, a cross next to the grass border, which would need a road edge facing grass) fails in `Restrict`. The attempt is marked `Failed`, and because every attempt applies the same preset, all of them fail. `Generate` then returns `null` rather than a broken map.
## 3.3 Generate-and-test
`MapGenerator.Generate` loops up to `maxAttempts` times. Each attempt:
1. Makes a `SeededRandom` from `Derive(seed, attempt)`.
2. Resets the solver and applies the preset.
3. Runs WFC. A contradiction means "next attempt".
4. Copies the result into a `GeneratedMap` and calls `validate`. If it passes, return it, recording which attempt succeeded.
Because attempt *n* always uses the same derived seed, **the whole process is deterministic**. Seed 42 always needs exactly 4 attempts and always gives the map shown in the hub.
`WalkableConnected` is the validator for roads. It flood-fills from the first walkable cell, but only steps through edges whose socket is the join socket (`r`), so two parallel straights that touch side to side don't count as connected. If the fill reaches every walkable cell, the network is one piece.
## 3.4 What the numbers say
Measured over seeds 0–199 on the 16 × 10 bordered road set, outside Unity:
| Setup | Solved | Mean attempts | Roads all connected |
|---|---|---|---|
| WFC only | 200 / 200 | 1.00 | 67 / 200 |
| WFC + connectivity check | 200 / 200 | 2.99 | 200 / 200 |
So two thirds of raw WFC maps have separate road islands, which is fine for a city's decoration and wrong for a level the player has to traverse. Generate-and-test fixes it for roughly three times the cost, which is still a few milliseconds on a map this size.
If a validator rejects almost everything (mean attempts in the dozens), don't just raise `maxAttempts`. Change the inputs so valid maps are common: adjust weights (fewer dead ends), add presets (force a road through the middle), or reduce the map size.
## 3.5 Other validators worth writing
- **Minimum content:** count walkable tiles and reject maps with fewer than *n*.
- **Entrance to exit:** flood-fill from one preset cell and check you reach the other.
- **No huge empty areas:** reject maps where any 6 × 6 square is all grass.
- **Reachability for agents:** for 3D levels, build the NavMesh after placement and check paths. That's slower, so do it at bake time, not at runtime (part 4).
## Related
- [[Game Programming Patterns (Review)]]: presets and validators are callbacks passed in, so the solver never learns your game's rules. The book's *Decoupling Patterns* section makes the same argument.
- [[Level Up! (Review)]]: authored intent in generated levels; the presets are where the designer's hand goes.
- [[Procedural Generation]] for the room-and-corridor approach, which guarantees connectivity by construction instead of by testing.