I’ve been working on algorithms for randomly generating dungeons.
My project Caverns of Underkeep is a Roguelike, and one of the features of all roguelikes is that they have randomly generated dungeons. There are two different flavors of dungeon generation and I’ll explain how they work in this article – Surprisingly the simplest algorithms can often produce the best results.
Here is a screenshot from a typical Cave map
The way this type of dungeon is generated is as follows:
- Start with a completely unwalkable surface, then draw some randomly sized circles in random places – These circles are going to be water areas.
- Perform a couple of random walks from the centre of the map, if we fall off the side of the map stop walking.
- Locate tiles that are dead ends. (Any tile that has only one walkable tile as a neighbour) At these dead ends place a circular style room. Use a basic turbulence* algorithm to alter the shape of the room.
- Locate a few other tiles that are part of a corridor – And not part of a room (To do this just work out how many unwalkable tiles are in the area immediately surrounding the tile you selected – If the number is greater than 2, then the tile must be part of a room and not part of a corridor) and randomly place some more circular rooms at these locations.
*A basic turbulence algorithm for drawing a shape is if say – You are going to draw a rectangle, then draw it as normal but determine randomly if you are going to draw N more rectangles with slight x and y offsets to give a more interesting shape to your rooms. See the image below for an example.
The second type of map is a Citadel – A typical screenshot is bellow.
This algorithm is pretty much the exact opposite of the one above.
- Instead of starting with some random walks, I divide the map into 10×10 sections. Doing this gives it that more deliberate man made look to it.
- Determine if I want to place a room here – If I do, note it’s location in a big list of rooms.
- Draw a random rectangular room (with turbulence)
- Once all the rooms have been drawn, consult my list of rooms. For each room draw a corridor between that room and the nearest room. Once a corridor has been drawn from a room, remove it from the list of candidates for nearest neighbour (otherwise we could get stuck in an infinite loop)
Both algorithms are guaranteed to be completely connected – Which is a really important thing to have in a roguelike. There’s nothing worse than not being able to complete the game because of the random generator – Bad computer, no biscuit!
In Caverns of Underkeep I also take a hybrid approach to generate the sewer levels. Basically I combine the two algorithms to produce another style of dungeon – See if you can work this one out for yourself




#1 by Kristie on December 11, 2007 - 3:35 pm
Bad computer – no biscuit… Are you sure the second one is all biscuity? I don’t know if you got the screen shot I e-mailed, but I’ve had it happen one other time, where the stairs are by a dead end in a corridor, and if you come down them on the dead end side, you can’t get past them to explore the rest of the map. And if you go back up the stairs and come back down, oh horror of horrors, you always come out of the stairs on the same side!
#2 by admin on December 11, 2007 - 4:52 pm
Got the screenshot thanks, you were playing the game before I’d finished the generation algorithms
I’ve fixed stair placement by requiring each tile adjacent to the stairs is walkable. Which brings up a valid point – once your underlying enviroment is connected, don’t break it by placing stuff in dumb places…
#3 by Justin on January 28, 2008 - 4:54 pm
Cool approach to dungeon generation. The results are a lot more interesting than a lot of methods I’ve seen.
#4 by Dirk Kok on May 2, 2008 - 9:33 am
Hey Joshua
I really like your cave map. Looks really cool. I look forward to reading your detailed dungeon generation article.