Mathematical model of level generation
This document organizes the mathematics of the dungeon generator around one simple question: how do we build a map that is spatially valid, connected and interesting to explore?
The answer has three parts. First we define rooms and corridors in the plane. Then we represent the connections as a graph and measure its topology. Finally we choose which properties must be mandatory and which are only preferences.
This is the mathematical specification behind Tapiti's generator. Since the Rust implementation does not cover every idea of the model, the text distinguishes the reference model, the current behavior of the crate and the proposed extensions.
1. The problem in one sentence
A level is a network of rooms and passages, placed in space, that must respect geometric constraints and offer an exploration experience with a desired structure.
We use the following representation:
- โ is the navigable graph: vertices are rooms or junctions; edges are corridor stretches between those points.
- โ is the geometric realization: it says where the rooms are and where the corridors run.
- โ assigns roles to parts of the level, such as start, stairs, prize or ambush.
- โ gathers the generator's parameters, such as dimensions, clearances, shape weights and density.
The current generator builds a connected 2D floor. The representation separates topology โ what connects to what โ from geometry โ how those connections fit in the plane. This separation allows specifying the shape of a network without requiring it to already have coordinates.
Vocabulary and status
In this text, reference model identifies concepts preserved from the original mathematical specification; current crate indicates what exists in this Rust implementation; proposal identifies an extension not yet implemented. Topology profiles, keys, switches and exploration pacing are now part of the crate; multi-floor campaigns and mission objectives remain proposals.
2. Basic notation
| Symbol | Meaning |
|---|---|
| โ | the floor plane, in pixels |
| โ | region occupied by room โ |
| โ | outline of room โ |
| โ | smallest Euclidean distance between two sets |
| โ | corridor width |
| โ | minimum clearance between regions that do not connect |
| โ | minimum length of the straight stretch right after a door |
| โ | minimum length of a corridor segment |
| โ | desired minimum distance between connected rooms |
| โ | greatest integer less than or equal to โ |
A clearance is a distance between region borders, not between their centers. For example, requiring โ means there are at least โ pixels of space between the outlines of the two rooms.
3. Room geometry
3.1 Direction alphabet
Doors and corridor segments use a discrete set of directions:
With โ, the directions are horizontal and vertical. With โ, the 45ยฐ diagonals are added. We call the first case rectilinear and the second octilinear.
A free curve could join any two points, but discrete directions make passages more predictable and make it easier to test their width and intersections. This is a deliberate simplification of the model.
3.2 Shape classes
Each room occupies a region โ of a class โ:
| Class | Main parameters | Area or construction |
|---|---|---|
| Rectangle | width โ, height โ | โ |
| Disc | radius โ | โ |
| Octagon | circumradius โ | โ |
| L | box โ, notch โ, corner โ | โ |
| Truncated star | number of points โ, radii and top width โ | computed by the polygon (shoelace) formula |
A star has its points cut off. Without the cut, each point would be just a vertex and there would be no suitable stretch of edge to open a straight door.
In the reference model, a room is fitted into a usable rectangle โ by a function
where โ represents random choices such as proportions and orientation. In the crate, the blocks of the scaffold also limit where rooms can be placed.
To keep doors aligned with the alphabet, the reference specification restricts the rotations of shapes to โ. This is a modeling choice: continuous rotations would require also accepting walls whose normals do not belong to the corridor alphabet.
4. Doors and room capacity
A door is a pair โ: the point โ on the room's outline and the direction โ in which the corridor leaves.
On a polygon edge, not every point is admissible. The door must stay away from the corners and its exit must not cross the room itself. We define the inset
For an edge of length โ, the admissible points initially lie in the interval โ. In the mathematical model, any point of that interval can be chosen; the implementation samples positions in portStep steps and also adjusts doors when it resolves some intersections.
A door's exit strip can be represented as
where โ. Intuitively, โ is the rectangle the corridor occupies right after leaving the wall.
Two doors used in the same room must keep their strips apart:
We call capacity of the room the largest number of doors that satisfies this condition:
For an edge with an admissible interval of length โ, the limit is โ. On a disc of radius โ with equally spaced doors, the distance between neighboring positions is the chord โ; all โ doors fit if that chord is at least โ. This computation shows why the radius limits the number of doors, even when the parameter asks for more directions.
For example, on the same straight side, two doors cannot be closer than roughly โ. Capacity therefore depends both on the room's size and on the corridors' width.
5. Corridors
5.1 A corridor as an occupied region
A corridor is made of connected segments. Each segment follows a direction in โ and has a length of at least โ. The segment leaving a door must also keep the door's direction for at least โ.
If โ is the center line of a segment โ, its occupied strip is the neighborhood of that line with radius โ. A corridor's region is the union of the strips of its segments and of the caps at bends and junctions. Collision checks therefore compare regions, not just center lines.
5.2 Kinds of link
Between two doors, the specification considers three kinds of route:
| Type | Bends | Description |
|---|---|---|
| I | 0 | straight segment between aligned doors facing each other |
| L | 1 | two rays that meet in front of the doors |
| Z/U | 2 | two exit stretches joined by an intermediate segment |
For example, for a straight route between โ and โ, the doors must face each other, โ must be aligned with the exit direction, and the distance must allow the two minimum stretches:
L and Z routes impose similar conditions of alignment, direction and minimum length for each segment. In the Rust generator, direct routes follow the same I/L/Z family, with the bend limit controlled by maxBends.
For an L route, let โ, โ, โ. If โ, the rays meet at the parameters
The route is feasible only if โ and the bend formed by the directions is allowed. For Z, we choose an intermediate direction โ and solve
The minus sign appears because โ points out of the second room; the route's last segment reaches the door in the opposite direction. There are two vector equations in three lengths: for each intermediate direction there is a family of solutions. The specification fixes one length or a ratio between stretches and solves for the others.
5.3 Junctions
A junction connects three arms (T) or four arms (X). The arms share a meeting point, which is also a vertex of the navigable graph. This representation matters for counting paths and cycles: a T junction is a bifurcation, not a triangle of passages.
The reference specification also describes a conductance โ between the arms of a junction. It can model whether two arms behave as a single space or as partly coupled regions. The junction field appears in the parameters, but is marked in the code as unused by the generator; this interpretation is therefore an extension of the space model, not an effect the current crate produces.
5.4 Cost and weight of a route
A route can be geometrically valid and still be less desirable than another. To rank candidates, we use an aesthetic cost:
where โ is the number of bends, โ the length and โ the number of 45ยฐ bends. The indicator โ is 1 when the condition is true and 0 when it is not.
The cost does not decide validity. Among valid candidates, the weight is
where โ is the compatibility between the room classes and โ is the weight of the route type. A zero weight forbids the link; a larger weight raises its chance in a weighted choice. This is the mechanism used to vary choices without abandoning the geometric constraints.
6. Navigable graph
6.1 The representation used for topology
The refined graph of the level is
where โ is the set of rooms, โ the set of junctions, and each edge in โ is a corridor arm between a room and a junction, two junctions or two rooms. The graph may have parallel edges: two physically distinct passages between the same vertices remain two edges.
We use โ as the main representation for connectivity, cycles and independent routes. A projection that links every room of a junction to all the other rooms of that junction may be useful for certain "hops between rooms" measures, but it creates artificial connections if used to count cycles.
Example. Three rooms joined by a single T junction form a tree in โ: three arms and three rooms, no cycle. If we project the T as links between every pair of rooms, a triangle appears. That triangle does not correspond to three independent passages; all of them use the same junction. This is why the clique expansion must not be used to compute โ or edge-disjoint routes.
6.2 Connectivity
A connected level has a path between any pair of rooms. If โ is the number of connected components, the general condition is โ for a floor that contains a single playable region.
A bridge is an edge whose removal disconnects the graph. An articulation vertex is a vertex whose removal disconnects it. These two structures identify bottlenecks: places exploration has to pass through.
6.3 Number of cycles
The number of independent cycles, also called the cyclomatic number, is
For a connected floor, โ. A tree has โ. Adding a link between two already connected points raises โ by 1.
Example. The path room A โ room B โ room C has 3 vertices and 2 edges, so โ. Adding a passage between A and C gives 3 edges and 3 vertices; then โ, corresponding to one independent cycle.
If โ is planar and connected, Euler's formula gives โ. Hence โ: it is the number of bounded faces of the drawing. This interpretation depends on planarity; the cyclomatic number remains defined even when the face reading is not used.
6.4 Cycles do not guarantee alternative routes to the exit
The number of cycles describes the whole network, but not where the cycles are. To measure independent routes between start โ and stairs โ, we use
By Menger's theorem, this number is also the minimum size of a set of edges whose removal separates โ from โ.
So โ does not imply โ. For example, consider the path โ and a wing in a cycle โ. The graph has 5 vertices and 5 edges, so โ, but every route from โ to โ still uses the edge โ: โ. By contrast, two routes โ and โ have โ and โ. The level profile must specify "how many cycles exist" and "how many routes reach the exit" separately.
Other useful measures include:
| Measure | Definition | What it describes |
|---|---|---|
| Room distance โ | shortest path counting each arrival at a room as 1 and passing through a junction as 0 | length of the main route in rooms crossed |
| Geometric distance โ | smallest sum of corridor lengths between start and exit | minimum physical walk |
| Diameter | largest minimum distance between pairs of rooms | perceived global size |
| Girth โ | size of the smallest cycle (defined as โ if there is no cycle) | presence of short cycles |
| Degree โ | number of edges incident to โ | local number of options |
| Articulations and bridges | vertices/edges whose removal disconnects โ | structural bottlenecks |
| โ | edge-disjoint routes between start and exit | redundancy of the final route |
| โ | second smallest eigenvalue of the Laplacian โ | global connectivity, in aggregate |
6.5 When a level is valid
The constraints below separate different problems. Connectivity is about the network; clearance is about geometry; accessibility is about the game rules.
- V1 โ Connectivity. All relevant navigable vertices belong to the same component of โ.
- V2 โ Clearances. Rooms keep the minimum distance โ; a corridor stays at least โ from the rooms it does not connect to; two separate corridors keep the clearance, except at a valid junction. Between linked rooms, the direct distance โ is also required, so that rooms close to each other diagonally do not become nearly adjacent despite a long path.
- V3 โ Doors. Each used door is admissible for its room, and the exit strips of the doors of the same room are separated by at least โ.
- V4 โ Corridors. Directions, bends and lengths respect โ, โ, โ and the bend limit.
- V5 โ Start and exit. There is a start room and a distinct stairs room; the stairs respect the door limit and are at least
minHopsroom hops from the start. - V6 โ Furniture. In the original specification, stones and crystals must also stay out of the exit strips and keep their distance from each other. This step belongs to the game's furniture and is not implemented by this crate's geometric generator.
A level must satisfy all the constraints that apply to its scope. A lower aesthetic cost cannot compensate for a violation of V1โV5.
7. Cycle density
To normalize the number of cycles, the specification defines a reference โ and a density:
In the interface and in the code, this reference may appear under the name betaMax. The name must be read carefully: the value is obtained by a greedy strategy that adds the highest-weight valid actions until no options remain. This produces a maximal reference of the heuristic, not a proof of the global maximum among all possible configurations.
The target is rounded from โ. For example, if โ and โ, the target will be about 2 cycles. The realized density is โ when โ; it may differ from the target because of rounding, because an X action adds two cycles, or because โ comes from a heuristic, not a proven maximum. In some cases, โ can even exceed 1. The target controls the approximate quantity; topology profiles are needed to control the location and function of those cycles.
For a connected graph, the average degree satisfies
This identity relates the number of cycles to the average degree, but does not determine the layout of the network.
8. Generation in the reference model
The process can be understood as six steps. The original specification describes this sequence; the Rust crate implements a variant, summarized in section 12.
- Scaffold. Create a grid of โ cells and combine them into blocks. Some cells may be dropped, as long as the remaining blocks stay connected.
- Rooms. Draw a shape class for each block and fit the region into the usable space, respecting limits and clearances.
- Candidates. Choose pairs of rooms to consider; for each pair, look for feasible doors and routes.
- Conflicts. Mark pairs of candidates that cannot coexist because of geometric collision or doors that are too close.
- Tree and cycles. First connect the rooms with a spanning tree. Then add links between already connected points until approaching the โ target.
- Leaf rooms and roles. Use dropped cells for degree-1 rooms and assign roles such as start, stairs, prize or trap.
A tree guarantees connectivity without cycles. The second phase adds alternatives without removing existing connections.
8.1 Mixing room classes
A class distribution can consider both the block size and the classes of the neighboring blocks. Let โ be the set of blocks adjacent to โ, โ the number of cells of โ, and โ how many neighbors have class โ. The sampling rule is
The weight โ can, for example, forbid stars in small blocks. The parameter โ controls homophily: โ favors neighbors of the same class; โ favors different classes. In the crate, this rule is sampled in four sweeps and then the maxOf quotas are applied.
8.2 Candidate room pairs
The reference specification considers proximity graphs built from the room centers:
- RNG: a link between โ and โ is removed if another room is closer to both;
- Gabriel: a link is removed if another room falls inside the disc whose diameter is the segment between the centers of โ and โ;
- Bounded Delaunay: consider Delaunay neighbors up to a maximum distance.
If โ is the center of room โ and โ, the RNG test keeps โ when there is no โ with โ. The Gabriel test keeps the link when there is no โ with โ. The Near variant used in the crate keeps pairs with โ.
These filters control which pairs enter the search. They do not by themselves guarantee that the rooms can be connected: doors and corridors without collisions must still be found.
8.3 Conflict graph
Let โ be the set of candidate links. We build a conflict graph โ, in which two candidates are joined in โ when they cannot be chosen together.
A conflict-free selection is an independent set of โ. To form a connected level, we also require the selection to build a connected navigable graph. This formulation separates two questions:
- is each link valid on its own?
- can the chosen links coexist?
8.4 Tree, cycles and leaf rooms
The crate chooses a start room and grows a spanning tree. At each step, a candidate link must connect an already reached room to a new room. The choice is weighted by the candidates' weights. If the current list does not allow progress, the generator widens the set of proximity pairs and tries to continue.
After connecting the rooms, the generator builds a betaMax reference by filling a copy of the graph with the highest-weight valid actions. This search considers direct links and T/X junction actions. The reference is greedy: it depends on earlier choices and does not prove the global maximum.
In the real cycles phase, the target is โ, where delta is the crate's parameter. Valid actions are drawn until the target is reached or exceeded, or until no actions remain. A direct or T link adds one independent cycle; an X crossing can add two, since it closes a new path and splits the crossed corridor into two branches. So โ is a target, not a guarantee of exact equality.
Leaf chambers are added afterwards: each is a room linked by a single corridor. In the graph, the operation adds one vertex and one edge, so โ. It raises the number of dead ends without raising the number of cycles. The roles of start, stairs, boss, rest, ambush, trap, prize and empty room describe content. Keys, switches and locks are progression data, separate from the labels.
In the crate, the start is chosen among the live rooms. After the tree, the stairs are chosen among the leaves farthest from the start; if the minimum distance minHops is not reached, stage may try another seed up to the attempt limit.
9. Topology profiles
The topology parameter lets you ask explicitly for a topological intent. The crate implements mandatory requirements for branches, depth, independent routes to the exit, hub degree and minimum cycle length. If the geometry does not allow a requirement to be met, the generator records the limitation and the validator points out the violation.
We represent a profile as
where:
- โ is a set of mandatory conditions;
- โ is the vector of target values for the graph measures;
- โ defines the relative importance of each target.
Let โ be the vector of metrics, for example
The feasible set contains only levels that satisfy geometric validity and the coded mandatory conditions. The crate does not yet search for the best level according to a global scoring function. The expression below formalizes a possible extension to compare metrics with targets:
where โ penalizes the distance from the target. The mandatory constraints filter the levels; the function โ would rank the remaining options. This global optimization remains outside the current crate.
9.1 Example profiles
Spine with optional wings. We choose a main path โ from โ to โ. We remove the vertices of โ from โ and examine each remaining component โ. We define its neighborhood on the spine by
If โ, โ is a simple side branch: a single edge links the component to the spine. If โ, โ offers a detour that returns to the spine โ a shortcut or cycle, not a simple branch. We count edges, not just contact vertices: two parallel passages to the same vertex of โ already form a second link and must not be classified as a simple branch.
Two routes to the exit. Require โ. This guarantees that no single edge is enough to separate start and exit.
Explorable loop. Require a simple cycle โ with at least โ edges. The condition โ alone would allow the only cycle to be very small.
Hub. Require a room or junction โ with degree โ, or favor a degree distribution that concentrates several options in one region.
Intentional bottleneck. Require a bridge or articulation at a specified stretch of the main path. This can concentrate the level's pacing, as long as progression allows crossing it.
These requirements can coexist. For example, a profile can require a long spine, two branches with rewards and two alternative routes in the final stretch.
10. Progression with keys and switches
Topology answers "which places connect?". Progression answers "when can the player cross each connection?". To model locked doors and objectives, we extend the player's state:
where โ is the inventory, โ the set of activated mechanisms and โ the completed objectives.
An edge can be crossed only if its condition is satisfied in โ. The crate implements masks of up to eight keys and eight switches and checks the state space of location and inventory. A key is collected on entering the room; a switch is activated on entering it.
To keep each lock meaningful even when there are shortcuts, the generator splits the graph by successive cuts of the main path and applies the condition to all the edges that cross each cut. So it is not enough to place a key before a chosen door: every path that goes past that point must satisfy the condition. The validator walks states โ and requires the stairs to remain reachable.
The pacing.enabled field distributes rest, boss and ambush rooms along the main path and raises the preference for prizes in optional branches. These roles describe pacing and content; they do not create mission objectives.
The same model can extend to a multi-floor campaign: we assign each room a floor index and let stair or elevator edges connect vertices on different floors. Each floor has its own geometric realization โ, but connectivity and progression are checked on the joint graph. This extension is not yet implemented in this crate.
11. Model parameters
The parameters can be grouped by function. The names in parentheses are those used in the crate when there is a direct match.
| Group | Parameters | Effect |
|---|---|---|
| Scaffold | columns, rows, cell size, inset, large blocks, dropped fraction, irregularity and rotation (cols, rows, cellW, cellH, gap, big, drop, layoutJitter, rotateRooms) | scale, fragmentation and orientation of rooms; irregularity is limited to preserve the minimum size |
| Shapes | weights and quotas per class, minimum size for a star, homophily (weights, maxOf, starCells, homophily) | variety and distribution of rooms |
| Doors | allowed directions, number of doors on discs, sampling step (m, discPorts, portStep) | positions and directions of the connections |
| Corridors | width, clearance, exit stretch, minimum segment, minimum distance, maximum bends (width, clear, exit, seg, minLen, maxBends) | geometry of the passages |
| Weights | probabilities per route type, including the W detour, bend/length/diagonal costs (pI, pL, pZ, pW, pT, pX, costBend, costLen, cost45, costFacing) | preference among valid candidates |
| Topology | proximity, radius, density and profile (proximity, near, delta, topology) | pairs considered, cycle target and network requirements |
| Progression | number of key and switch locks (progression) | crossing conditions checked in the state graph |
| Pacing | roles distributed along the main path (pacing) | contrast between advancing, resting, combat and optional exploration |
| Start and exit | stairs door limit and minimum distance in hops (stairsDoors, minHops) | topological position of the stairs |
| Content | number of ambushes and leaf chambers; allowed classes and roles (ambush, leaves, leafClasses, leafRoles) | distribution of existing functions |
| Mixing | compatibility matrix between classes (mix) | allows or discourages connections between shapes |
| Junctions | optional conductance (junction) | reference parameter, not yet used by the generator |
The profile โ complements โ: the parameters control how the generator operates; the profile describes the structure you want to obtain.
12. What exists in the Rust crate
This table avoids confusing the reference specification with this repository's implementation.
| Concept | Status in the crate |
|---|---|
| Rooms and geometries | Rectangles, discs, octagons, L shapes and stars; placement based on a grid of blocks. |
| Directions and routes | Modes with 4 or 8 directions; I/L/Z candidates, T branches and X crossings. |
| Main connection | Tree growth over candidate room pairs; the generator tries other proximity relations if it runs out of connections. |
| Cycles | Later choice of cycle actions up to the target derived from delta and the betaMax reference. |
| Start and stairs | One start and one stairs room per floor; the stairs are chosen among distant rooms, usually leaves. |
| Room content | Marking of start, stairs, boss, rest, ambush, trap, prize, key, switch or empty. The labels are not a mission system. |
| Proximity | The Rust enum includes RNG, Gabriel and radius-based Near. Delaunay appears in the reference specification, but not in the current enum. |
| Topology profiles | Requirements for branches, depth, independent routes, hub and minimum cycle implemented; global profile scoring not yet implemented. |
| Stateful progression | Keys, switches, conditional doors and reachability checking implemented; mission objectives not yet implemented. |
| Exploration pacing | Rest, boss and ambush roles on the main path and a higher preference for prizes in optional branches. |
| Spatial flexibility | Irregular grid limited by minimum dimensions, shape rotation and W routes of up to six bends. |
| Connected multiple floors | Each current generation produces a single 2D map; the progression relationship between floors is not modeled here. |
In the crate, the geometric and topological checks are spread mainly across generate.rs, validate.rs and metrics.rs. The validator is the reference for deciding whether a concrete output satisfies the coded conditions.
13. Summary
The model separates three questions:
- Does the geometry fit? Do rooms, doors and corridors respect widths and clearances?
- Does the network have the desired shape? Do the cycles, branches, bottlenecks and routes to the exit match the profile?
- Can the level be completed? If there are progression rules, does the exit remain reachable in the right state?
This separation keeps the mathematics clear. Geometry defines what is physically possible; the graph defines the navigation structure; profiles define the level's intent. The next natural step is to express intent as conditions on โ, without confusing the global number of cycles with the availability of alternative routes at specific points.