Game Internals › Nonograms Katana › Dungeon generation

Dungeon generation

Every dungeon run is built on your phone from a random seed. Rooms are dropped at random, joined by a minimum spanning tree with a few extra loops, and then filled with chests, pages, monsters and traps. The same seed always gives the same dungeon.

See it for yourself: the dungeon simulator rebuilds any dungeon from 1A to 5C, the forest F1 to F4, the caves 6A to 6D and the tombs EA to ED from the label shown at its top. It runs in your browser; you can also download it for offline use.

Seeds and repeats

When you enter a level, the game draws a random number and uses it as the seed.

next = state × 69069 + 1 (32-bit, wrapping) roll(n) = (low 15 bits of next) × n ÷ 32768, rounded down

Only 32,768 different maps per level. Every roll reads just the low 15 bits of the state. Those bits depend only on the low 15 bits of the previous state, so the whole map, retries included, is decided by the seed modulo 32,768. For a given level and quest state there are at most 32,768 distinct dungeons.

Play itself uses a second generator, seeded from the clock when the run starts. It decides critical hits, dodges, trap spotting and mimics, so those aren't predictable even when the map is.

A dungeon in progress is saved tile by tile, so it's restored from the save, not regenerated.

The steps

Generation runs in this order. Up to step 7 only the level and the seed matter; from step 8 your quests and buildings can change what's placed.

  1. Grid. Pick the level's map size; some layouts are randomly turned on their side.
  2. Rooms. Drop random rectangles until enough fit.
  3. Connections. Triangulate the room centres, keep a minimum spanning tree, add back some extra links as loops.
  4. Corridors. Carve each link with a path search, longest link first.
  5. Walls, around every floor cell.
  6. Entrance in the outer wall of a small room.
  7. Doors wherever a corridor meets a room.
  8. Special rooms: the secret mosaic room, the barrel puzzle, lever doors, basements and spiral staircases.
  9. Room purposes and decoration.
  10. Chests and ancient pages (see the loot page for counts).
  11. Monsters: the boss, quest and unique monsters, room occupants, then random monsters room by room.
  12. People: a quest NPC or a wandering traveller, then wild familiars.
  13. Traps in corridors.
  14. Key doors, with the key given to a random monster.
  15. Check: at least 4 objectives, or start again.

Map sizes

Each level uses one of these layouts. Rooms are added until the minimum count and attempt count are both reached, or the maximum of either.

LevelsMap sizeRoomsExtra loopsNotes
1A, 1B, 1C, 2A, 3A, 4A61 × 6116–2210%
2B, 2C, 3B, 3C, 4B, 5A71 × 7120–2810%
4C, 5B79 × 7930–3810%
5C79 × 7940–4810%
F1113 × 3535–4580%Long strip crossed by 2–4 rivers
F2147 × 3748–5880%3–4 rivers
F3177 × 3960–7180%3–5 rivers
F4219 × 4575–8880%4–6 rivers
6A93 × 7929–3710%Basements
6B103 × 8934–4210%Basements
6C119 × 10548–5610%Basements
6D187 × 7760–6810%A fixed central corridor with three rooms and levers
EA85 × 7120–2810%Basements
EB103 × 8934–4210%Basements
EC119 × 10548–5610%Basements
ED129 × 11555–6310%Basements
Citadel97 × 6943 fixed–Hand-made, seed always 0 (95 × 67 and 39 rooms before 22.11)

"Extra loops" is the share of leftover links added back after the spanning tree, so the forest is far more interconnected than the dungeons. The tombs EA–ED (game version 22.11) use four new layouts sized like the caves'.

Rooms and corridors

Rooms

  • Width and height are each 3, 5, 7, 9 or 11, with 3 × 3 and 11 × 11 rejected. Rooms sit on odd coordinates, so walls line up on a grid.
  • A room is placed only if its whole rectangle is empty. Rooms can share a wall line but never overlap.
  • Larger rooms often get a shape: pillars in the corners, cut corners, a pool or a moat, a chasm splitting it in two (sometimes with a bridge), water channels, columns, or an L shape.

Connections

  1. The room centres are joined with a Delaunay triangulation (Bowyer–Watson).
  2. A minimum spanning tree (Prim's algorithm) keeps the shortest set of links that reaches every room.
  3. Loops: 10% of the leftover triangulation links are added back (80% in the forest), picked from the middle of the length range (the 20th–60th percentile), so loops are neither the shortest nor the longest links.
  4. Each link is carved by an A* path search. Existing corridors are cheap to reuse, room floors cost more, walls a lot, and each turn adds a small penalty. Corridors may run straight through other rooms.

The forest is a long strip crossed by 3-cell-wide rivers that alternate from each side, making a winding route. A middle river has a single gap, the jade gate, guarded by a minor boss whose emerald key opens it.

Doors and special rooms

  • Doors appear where a corridor meets a room wall. 25% are locks that need mental power and 15% are closed doors; the rest are open doorways. The forest uses fewer, simpler doors.
  • Secret mosaic room (2C, 3C, 4C, 5C, 6C and EC): a hidden dead-end room beside a larger room with a mosaic floor pattern. The level's ring chest goes inside. Measured: 8–18% of maps on those levels, 27% on EC.
  • Barrel puzzle (2B, 3B, 4B, 5B, 6B and EB): a room with target tiles and barrels to push onto them. The game tries in 80% of maps, but it needs a suitable room, so 32–53% of maps have one (measured).
  • Lever door (from 2B, not the forest): one door that opens from a lever in another room; in the tombs, up to two. Measured: 15–33% of maps, 20–43% in the tombs.
  • Basements: rooms reached only by a hatch and ladder; some hatches are locked with mental power.
  • Spiral staircases: on some levels, a 60% chance of a linked pair in two distant rooms; taking one brings you out at the other. (The code treats them as teleporters.)

Monsters

  • The boss goes in a room of at least 25 cells (35 with followers), well away from the entrance: along at least one axis it must be about half the map's width or height away (half, minus 5 cells). The rule relaxes after repeated failures.
  • Unique monsters on certain levels get a room each.
  • Room occupants: a room's purpose can bring its own resident, sometimes a whole pack.
  • Random monsters come from a weighted list for the level. Tougher "special" monsters are capped per room and per map.

How many random monsters a room gets depends on the level's difficulty tier. "Big" rooms are at least 49 cells with no resident:

Tier0 monsters1234–5Levels
120%45%35%big rooms 10%–1A, 2A
215%25%40%20%4: big rooms 10%1B, 2B, 3A, 4A, F1
315%10%45%30%4: big rooms 15%1C, 2C, 3B, 4B, 5A, F2, 6A, EA
410%10%40%40%4: big rooms 20%3C, 4C, 5B, F3, F4, 6B–6D, EB–ED
510%10%40%20%4: 20%; 5: big rooms 10%5C

The percentages are for normal rooms. In a big room the "big" share comes off the top first, so it takes from the higher counts.

Traps and keys

  • Traps are hidden marks on plain corridor cells, never in rooms or next to doors, and never side by side. Each level has a range, from 3–5 on 1A up to 9–12 on F4, 6D and ED. Whether you spot a trap is rolled during play: 30%, rising to 60% and 90% with the Traps skill.
  • Key doors lock a room that has only one door, usually a chest room. A random ordinary monster (preferring tougher ones) carries the key instead of its normal drop.

What else changes the map

Rooms, corridors and doors depend only on the level and the seed. After that, some of your game state is read, and because each check uses up random numbers differently, it shifts everything placed afterwards:

  • Active quests decide the NPC, extra monsters, special drops, and on 2C–5C the jade-gate quest adds an exit and an emerald-key carrier.
  • Buildings and quests unlock some decorations and gatherables.
  • Your hero's stats, inventory and map fragments aren't read.

When generation fails

About twenty checks can reject a map: no path between two rooms, nowhere for the entrance, the exit or the boss, too few objectives, and so on. Any failure starts again with the next seed (seed × 69069 + 1), with no limit on attempts. The citadel is the exception: its layout is fixed, so a failure there is an error.

A fixed bug: up to version 21.4, if a quest NPC with followers couldn't be placed anywhere, the code that builds the error message read the missing monster and crashed. The retry loop caught the crash and moved on, so players never saw it, but it was reported to the developer's crash logging. Version 22.11 reads the followers from the level's tables instead, so the failure is reported as intended.

Sources and confidence

  • Read from the code (version 21.4, rechecked against 22.11): the whole pipeline, the layouts, the room and corridor algorithms, the monster counts and the retry rules. The 32,768 limit follows from the arithmetic of the random number generator. The rules for 1A–6D are unchanged in 22.11; the tombs and the bigger citadel are new.
  • Measured with the simulator (which matches the game's own code on every level, 60 maps per level): how often each special room appears.
  • Inferred: the meaning of some tile and object types, which have no names in the game's text.
  • Not yet checked in the game. The map sizes are the easiest thing to compare against a real level.