Skip to content

Build Rect Connections

Builds a sparse connection graph between rooms or regions using a Minimum Spanning Tree (MST) plus optional extra links.

Outputs pairs of rect indices (Vector2Int where x = A, y = B) that represent corridor connections.

What it does

Given a list of Rects (for example, from Build Random Rects), this action:

  1. Computes an MST over rect centers to ensure all rooms are connected with the shortest total path length.
  2. Optionally adds Extra Connections (short, non-duplicate links) to create loops and alternate routes.
  3. Outputs all connections as index pairs into Rect Connections for corridor or path generation.

The result is a clean, fully connected layout with optional branching or looping paths.

Inputs

Field Description
Rects Source list of Rects, for example, from Build Random Rects.
Extra Connections Number of additional non-MST links to add (creates loops and alternate paths).
Default: 1.
Max Extra Distance Maximum distance allowed for extra links. Prevents adding very long cross-map corridors.
A value of ≤ 0 disables the limit.

Outputs

Field Description
Rect Connections List of Vector2Int (A,B) pairs, where each value is a rect index from Rects.
Use these pairs to carve corridors.

How it works

Minimum Spanning Tree (Prim’s algorithm)

  • Starts from the first rect and connects each remaining rect to the nearest one not yet connected.
  • Ensures all rooms are reachable with minimal total corridor length.
  • Produces a single, loop-free network.

Extra Connections (shortcuts)

  • Finds all remaining room pairs not already connected.
  • Filters by Max Extra Distance if set.
  • Adds up to Extra Connections of the shortest remaining links.
  • Creates optional loops or shortcuts for a more natural layout.

Determinism: No randomness is used. Given the same Rects and settings, the output is identical each run.

Usage pattern

  1. Use Build Random Rects to create rooms.
  2. Use Build Rect Connections to generate connection pairs.
  3. Pass Rect Connections into corridor or path-building actions to carve or link the rooms visually.

Tips & pitfalls

  • Use Max Extra Distance to limit long or diagonal connections that cross the entire map.
  • The output stores indices, not positions - use the indices to fetch room centers or bounds.
  • The MST ensures connectivity, but if you want more loops, raise Extra Connections slightly.
  • Performance is O(n²), suitable for dozens or a few hundred rooms.

See Also