SpelunkyFan wiki

Level generation

Verified against the spelunky.ai port v0.1.10 on .

This page describes a secret.

Level generation in Spelunky Classic builds each level as it loads: a random path of rooms from entrance to exit, the side rooms, then monsters and treasure.

No two games are the same: each level is made when the player enters it, from rolls of the game's random number generator. The path comes first, so that the exit can be reached from the entrance; then each of its sixteen rooms is picked from its area's layouts; then a pass over every block adds the monsters, traps and treasure. A level may also be a special one, a dark level or a snake pit, with odds the game sets once for all, on the levels of the areas that can have it. The areas' own rules are on their pages: the Mines, the Jungle, the Ice Caves, the Temple and Olmec's Lair.

How it works

#

When a level is made

#

A level is made when its room starts with a game in progress: the object that runs the level calls the generator from its create event [1]. The screen between levels picks the room: the Ice Caves have a room of their own, a flooded level another, Olmec's Lair a third, and every other level shares one [2]. The area comes from the level's number: levels 1–4 are the Mines, 5–8 the Jungle, 9–12 the Ice Caves, 13–15 the Temple and 16 Olmec's Lair [3]. The Tutorial and custom levels from the level editor are laid out by hand, and get no monsters or treasure from it [4].

A level is four rooms by four, each 10 tiles wide and 8 high, a tile being 16 pixels [5]. Around them runs a border of blocks that nothing destroys; the Ice Caves' and Olmec's Lair's have no bottom, and a flooded level's is lower, to make room for its lake [6]. Olmec's Lair is a hall laid out by hand, with only the rooms above it made this way (Olmec's Lair).

The path

#

The generator first lays a path through the grid [7] [8]:

  1. It starts in a random room of the top row, which will hold the entrance.
  2. From each room it rolls a move. In the two middle columns, the move is left two times in five, right two in five and down one in five; at the left or right edge, it's away from the edge two times in three and down one in three.
  3. A move sideways takes the path into the next room. A move toward a room the path already has goes the other way instead if that room is free, or down if it isn't; at an edge it does nothing, and the walk rolls again (Quirks).
  4. A move down marks the room it leaves as a drop and the one it enters as dropped into. A move down from the bottom row ends the path: the room it leaves holds the exit.

The path is there so that the exit can be reached, "generally no bombs required", as the code's comment says [9]. The rooms it doesn't take are side rooms. Each room's place on the path decides its layouts, which leave a way on along the path; a side room's needn't lead anywhere [10]:

RoomWhat its layouts are
The start roomThe entrance, with an open floor, or a hole in it if the path drops at once[11]
The end roomThe exit, open from above if the path dropped into it[12]
On the path, crossedOpen to both sides[13]
On the path, dropped intoOpen from above[14]
On the path, a dropA gap in the floor to the room below[15]
Side roomLedges, walls, tunnels; or Kali's altar, 1 in 16 from level 2, or a Golden Idol's room, once a level each[16]
A shopOne of the shops, beside the path[17]

That table is the Mines' room script; each area has its own, with its own layouts, and the special levels with them [18]. A layout has slots for chunks of 5 tiles by 3, picked at random (a platform, a mound with Spikes, an overhang), and the start and end rooms' slot for the door picks one with the door in it [19]. Then each tile is made: a block of the area's earth, or, one time in ten in the Mines, a stone block; some tiles are a block only half the time [20].

Shops

#

From level 2 to level 15, a level rolls for a shop with a chance of 2 in its level's number: the roll always passes on level 2, 2 times in 5 on level 5, 2 in 15 on level 15. The shop's room is a side room beside the path, picked at random from those that qualify, and a level with none gets no shop; its kind is the room script's roll [21] [17]. Once the game has had the Black Market, no level has a shop again (Quirks).

Special levels

#

Some rolls turn rooms of the grid into something else, before or after the path is laid [22]. The graveyard's roll comes next, before the rooms are built, and the dark level's after [23]:

Special levelWhereOdds
Snake pitThe Mines: a column of three side rooms from the top two rows1 in 8 for each such column, once a level[24]
Dark levelFrom level 2, outside the Ice Caves and Olmec's Lair; once an area1 in 12, unless the level before it was the title screen or was finished 20 seconds or less past a whole minute[25]
Black MarketThe Jungle: the level after the player takes its hidden doorAlways[26]
Graveyard levelThe Jungle1 in 10[27]
Flooded levelThe Jungle, from the screen between levels, until the Black Market's door is taken1 in 10[28]
MoaiThe Ice Caves, once a gameBelow[29]
MothershipThe Ice Caves, after the Moai's level1 in 10[30]
Yeti lairThe Ice Caves, after the Moai's level1 in 6, once the mothership's roll fails[31]
Sacrifice pitThe Temple1 in 8[32]
City of GoldLevel 15, after the player takes level 14's gold doorAlways[33]

The Black Market can be a graveyard level as well, and the City of Gold a dark level or a sacrifice pit, as their pages say.

Once a game

#

Four things are made once a game, each on one of a few levels, until it's made:

WhatLevels
The Udjat Eye's locked chest and its keyLevel 2 one time in 3, level 3 one time in 2, otherwise level 4[34]
The Black Market's hidden doorLevel 5 one time in 3, level 6 one time in 2, otherwise level 7[35]
The MoaiLevel 9 one time in 4, level 10 one time in 3, level 11 one time in 2, otherwise level 12[29]
The gold door to the City of GoldLevel 14[36]

A game started from one of the Tunnel Man's shortcuts skips the levels before it, and with them the chances of the first ones. Reaching the Black Market and Reaching the City of Gold walk through finding and using the two doors.

Monsters, traps and treasure

#

Once the rooms are built, a last pass goes over every block outside the shops, in the area's own way [37]:

  • Monsters hang under a block with open space below it (in the Mines, a Bat 1 in 60, a Spider 1 in 80) or stand on one with open space above (a Snake 1 in 60), each roll made once the ones before it fail. In the Mines, none start in the start room [38].
  • Treasure goes on top of a block with nothing over it: a Rock 1 in 100, or a Jar 1 in 40; failing those, in a nook one block high, a Web, the Udjat Eye's chest, a Crate, a Chest, the Damsel (once a level), bones, gold or a big gem; in a tunnel, a Web, gold, bones or a big gem; on open ground, gold or bones. Nothing goes within two blocks of the entrance or the exit [39].
  • Gold in the rock is the blocks' own: a block of the Mines' earth shows gold 29 times in 100, and one that doesn't may hold a big gem or, 1 time in 1,200 once those fail, an item [40].
  • Traps: in the Mines, one stone block in four that has open space on one side and a block on the other becomes an Arrow Trap facing the open space, unless it's close to the entrance [41].

The areas' Inhabitants sections have their monsters and odds. The pass ends with a crate of flares beside the entrance of a dark level; then the player is put at the entrance, a thief or murderer finds an angry Shopkeeper at the exit, and any treasure inside a block that can't be destroyed is removed [42] [43] [44].

The random number generator

#

Every roll comes from the game's one random number generator, which play draws from too: monsters, items and even the particles each take numbers from it, so a level depends on everything that happened before it. The game never seeds the generator; GameMaker seeds it at random when the game starts [45]. A test such as a and rand(1, n) == 1 draws its number even when a is false, which moves the generator on for rolls that can't come true [46] [47]. The same starting value and the same keys make the same levels again, which this wiki's replays rely on: the clip below comes from one that starts the generator at 2 [48].

Numbers

#
ValueOdds
A move down, from a middle column1 in 5[49]
A move down, from the left or right edge1 in 3[50]
A shop, from level 22 in the level's number[51]
Kali's altar, for each side room from level 2 (the Mines)1 in 16[52]
A snake pit, for each column that can hold one1 in 8[53]
A dark level1 in 12[54]
A graveyard level1 in 10[55]
A flooded level1 in 10[56]
A mothership1 in 10[57]
A Yeti lair1 in 6[58]
A sacrifice pit1 in 8[59]

Examples

#
Down a level's path. In the first level of the Mines, the player runs right from the entrance along the top row of rooms, drops into the room below at the level's right edge, runs back left along the second row, and drops again at its left edge.
  • A path. On the first level with seed 2, the path starts in the top row's leftmost room, crosses the row to its right end and drops, crosses the second row back to the left and drops again. The player follows it: right along the top row, down at the right edge, left along the second row and down at the left [60].
  • A move that goes the other way. On the first level with seed 1, the walk rolls a move left twice in the third row, each time toward a room it has already taken, and moves right instead [61].
  • No room for a shop. Level 2 with seed 5 rolls its shop, but its only side rooms are on the bottom row, one not beside the path and one with the path on its left, so it gets none [62].
  • A dark level. Level 2 with seed 2 loads dark, with the crate of flares by the entrance [63].
A dark level. Level 2 of the Mines loads dark, with the message "I CAN'T SEE A THING! I'D BETTER USE THESE FLARES!" A crate of flares glows by the entrance, and the player climbs down through the dark by the small light around him.
  • A snake pit. A shaft of side rooms down a Mines level, with Snakes and two big rubies at its bottom [64].
Down a snake pit. On a Mines level with a snake pit, the player drops down the pit's shaft to its bottom room, collects the two big rubies there, and the snakes around him bite him three times.
  • A flooded level. A Jungle level whose bottom row is a lake, with the exit just above the water [65].
Across a flooded level. A Jungle level loads flooded, with the message "I CAN HEAR RUSHING WATER..." The player crosses it above the lake that fills its bottom, and takes the exit, which stands on a strip of earth just above the water.
  • The Moai. An Ice Caves level with the Moai's room on it [66]. With seed 2, the Moai comes on level 9, and level 11 has a mothership [67].
The Moai. On an Ice Caves level with the Moai, the player makes his way down from a ledge, past a Yeti, to the rock the Moai stands on: a stone head three blocks wide and four tall. He stops beside it, then drops off the ledge and heads back left.
  • A sacrifice pit. A Temple level with the pit's column of rooms, a Damsel and a Golden Idol at its top [68].
Arriving at a sacrifice pit. A Temple level loads with the message "I CAN HEAR PRAYERS TO KALI!" The player walks left from the entrance to the top of the sacrifice pit, where a damsel and a golden idol stand side by side on four skull blocks, and stops beside the idol.

Quirks

#

Level generation keeps several bugs of the original's code, each a row of docs/quirks.md:

  • The path walk's moves go the other way. Each else of the walk's moves belongs to the if nearest it, not to the edge check its indentation pairs it with, so a move toward a room the path already has goes the other way, or down, rather than being rolled again. The path runs on across a row more than it would otherwise [47] [61].
  • A shop faces left only from the rightmost column. A side room with the path only on its left is a shop only in the last column, so a level whose shop roll passes can still have none [47] [62].
  • No shops after the Black Market. Once the Black Market is made, the shop roll never passes again that game [47] [69].
  • No flooded level after the Black Market's door. The lake is only rolled until the market's door is taken, so the Jungle levels after it are never flooded [47].
  • No mothership or Yeti lair before the Moai. Their rolls are the else of the Moai's, which every Ice Caves level takes until the Moai is made [47].
  • A dark level needs the clock past 20 seconds. It's ruled out after a level finished 20 seconds or less past a whole minute, and a dark roll on the Black Market's level, which stays lit, uses up the Jungle's dark level [47].
  • Webs eleven times as often. A misplaced bracket makes the web odds always take the branch meant for near a Giant Spider [47].
  • Sealed in the rock. The Udjat Eye's key, the gold door and the City of Gold's Lady Xoc statue can each land where no way leads, which only bombs open [47].
  • Blocks under the Ice Caves. A row of blocks lies under every Ice Caves level, out of reach, with treasure on it [47].
  • Places. The Black Market can be a graveyard level that never says so (Black Markets on graveyard levels), can have two General Stores (Two General Stores), and its door's fallback can make several doors; a graveyard level can have several Ash graves (Several Ash graves); the City of Gold's stone blocks give no gold (Stone blocks without gold); and Olmec's Lair rolls a path and a shop that nothing uses [47].

FAQ

#
Are the levels in Spelunky Classic randomly generated?

Yes. Every level from 1 to 15 is made anew when the player enters it: a path of rooms from the entrance to the exit, the rooms around it, and the monsters, traps and treasure in them. Olmec's Lair, level 16, is a hall laid out by hand with rooms made above it, and the Tutorial and custom levels are laid out by hand.

Can a Spelunky Classic level be impossible to finish without bombs?

It can be. The path from the entrance to the exit is laid so that, in the code's own words, "generally no bombs [are] required", but some things placed after it can block it: the City of Gold's Lady Xoc statue can land on the path's drop, and only bombs get past it.

How often is a level dark in Spelunky Classic?

1 in 12 for each level that can be dark: from level 2, outside the Ice Caves and Olmec's Lair, once an area at most, never the first level after the title screen, and not after a level finished 20 seconds or less past a whole minute on its clock.

Can you choose the seed in Spelunky Classic?

No. The game never seeds its random number generator: GameMaker seeds it at random when the game starts, and level generation and play draw from it alike, so each level depends on everything before it.

See also

#

References

#
  1. objects/oGame/create.gml:27
  2. objects/oTransition/step.gml:17-27
  3. scripts/scrInitLevel.gml:24-29
  4. scripts/scrInitLevel.gml:197-198
  5. scripts/scrRoomGen.gml:24-45
  6. scripts/scrInitLevel.gml:66-128
  7. scripts/scrLevelGen.gml:25-33
  8. scripts/scrLevelGen.gml:89-133
  9. scripts/scrInitLevel.gml:57
  10. scripts/scrRoomGen.gml:48-239
  11. scripts/scrRoomGen.gml:53-69
  12. scripts/scrRoomGen.gml:70-84
  13. scripts/scrRoomGen.gml:122-151
  14. scripts/scrRoomGen.gml:152-172
  15. scripts/scrRoomGen.gml:219-239
  16. scripts/scrRoomGen.gml:85-121
  17. scripts/scrRoomGen.gml:173-204
  18. scripts/scrInitLevel.gml:150-165
  19. scripts/scrRoomGen.gml:242-315
  20. scripts/scrRoomGen.gml:317-357
  21. scripts/scrLevelGen.gml:239-297
  22. scripts/scrLevelGen.gml:35-237
  23. scripts/scrInitLevel.gml:147-176
  24. scripts/scrLevelGen.gml:138-185
  25. scripts/scrInitLevel.gml:167-176
  26. scripts/scrLevelGen.gml:44-69
  27. scripts/scrInitLevel.gml:147-148
  28. objects/oTransition/step.gml:18-24
  29. scripts/scrLevelGen.gml:212-221
  30. scripts/scrLevelGen.gml:222-233
  31. scripts/scrLevelGen.gml:234-237
  32. scripts/scrLevelGen.gml:71-87
  33. scripts/scrLevelGen.gml:135-136
  34. scripts/scrInitLevel.gml:178-184
  35. scripts/scrInitLevel.gml:186-192
  36. scripts/scrEntityGen.gml:387-388
  37. scripts/scrEntityGen.gml:30-525
  38. scripts/scrEntityGen.gml:44-71
  39. scripts/scrTreasureGen.gml:24-133
  40. objects/oBrick/create.gml:3-16
  41. scripts/scrEntityGen.gml:145-171
  42. scripts/scrInitLevel.gml:200-204
  43. scripts/scrEntityGen.gml:527-545
  44. scripts/scrInitLevel.gml:245-266
  45. Quirk 4 (docs/quirks.md)
  46. Quirk 2 (docs/quirks.md)
  47. Quirk 1 (docs/quirks.md)
  48. Replay fidelity/replays/mines-seed-2.txt
  49. scripts/scrLevelGen.gml:92-94
  50. scripts/scrLevelGen.gml:92-93
  51. scripts/scrLevelGen.gml:240
  52. scripts/scrRoomGen.gml:87-91
  53. scripts/scrLevelGen.gml:148
  54. scripts/scrInitLevel.gml:169
  55. scripts/scrInitLevel.gml:148
  56. objects/oTransition/step.gml:22
  57. scripts/scrLevelGen.gml:222
  58. scripts/scrLevelGen.gml:234
  59. scripts/scrLevelGen.gml:71
  60. Clip level-generation/path
  61. Replay fidelity/replays/mines-seed-1.txt
  62. Replay fidelity/replays/mines-seed-5.txt
  63. Clip dark-level/arrival
  64. Clip snake-pit/descent
  65. Clip flooded-level/crossing
  66. Clip moai/statue
  67. Replay fidelity/replays/ice-seed-2.txt
  68. Clip sacrifice-pit/arrival
  69. Replay fidelity/replays/city-of-gold-seed-9.txt

Source code

#