# 3. Seeded Graphs and Corridors
Part of [[Procedural Generation]]. Prev: [[2. Room Templates as ScriptableObjects]]. Next: [[4. Design Constraints and Beat Charts]].
## 3.1 Algorithm (room-and-corridor)
This is a design recipe, not a Unity API:
1. Seed the RNG (part 1).
2. Place an **entrance** at the origin (or a marked spawn).
3. Until you reach `roomCount`, pick an open socket, pick a compatible template (socket count / tags), place it so the new room's socket meets the old one, and add an edge.
4. If a placement **overlaps** an existing room AABB (use `size` plus a margin), try another socket or template; fail the attempt after N tries and backtrack or abort with a clear log (do not silently overlap).
5. Connect remaining required sockets with a **corridor** prefab (a straight or L piece you authored). If you cannot connect, fail - a pretty but unreachable room is a Level 9 bug.
6. Instantiate under a parent. In the Editor, register Undo ([[5. Worked Example Template Browser]]).
```csharp
using UnityEngine;
/// <summary>Factory only. Seed first. NOT compiled in Unity.</summary>
public static class DungeonGenerator
{
public static Transform Build(DungeonRecipe recipe, Transform parent)
{
if (recipe == null) throw new System.ArgumentNullException(nameof(recipe));
Random.InitState(recipe.seed);
// Walk the graph, Instantiate(template.prefab, position, rotation, parent)...
// Keep a list of placed AABBs and sockets. Return the parent or a new root.
return parent;
}
}
```
The method body is the exercise: keep it deterministic, no `Time.deltaTime`, no `FindObjectOfType` during generation.
## 3.2 Corridors
Corridors are rooms with a long, narrow prefab and two sockets. Do not stretch a cube in code unless you also update collision and NavMesh. A small set of 2 m / 4 m / 8 m corridor prefabs is easier to make fair than a dynamically scaled box that breaks metrics.
## 3.3 2D variant
The same graph can stamp **Tilemap** rooms or prefab chunks ([[Tilemaps and Tilesets]]). The recipe does not care if `size` is in XY or XZ - the generator must.
## Exercise
Generate 4 rooms at seed 1 and seed 2. Seed 1 twice must match. Log a failure when overlap cannot be resolved - do not emit two rooms in the same space.
## Sources
- `Random.InitState` (6000.5): https://docs.unity3d.com/6000.5/Documentation/ScriptReference/Random.InitState.html