Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From \$16.50/m

# Create a Procedurally Generated Dungeon Cave System

Difficulty:IntermediateLength:LongLanguages:

For many, procedural generation is a magical concept that is just out of reach. Only veteran game developers know how to build a game that can create its own levels... right? It may seem like magic, but PCG (procedural content generation) can be learned by beginner game developers. In this tutorial, I'll show you how to procedurally generate a dungeon cave system.

## What We're Going to Cover

Here's a SWF demo that shows the kind of level layouts that this technique can generate:

Learning the basics usually means a lot of Google searching and experimentation. The problem is, there are very few simple guides on how to get started. For reference, here are some excellent sources of information on the subject, which I have studied:

Before getting into the details, it's a good idea to consider how we're going to solve the problem. Here are some easy-to-digest chunks that we will use to keep this thing simple:

1. Randomly place your created content into the game world.
2. Check that the content is placed in a spot that makes sense.
3. Check that your content is reachable by the player.
4. Repeat these steps until your level comes together nicely.

Once we've worked through the following examples, you should have the necessary skills to experiment with PCG in your own games. Exciting, eh?

## Where Do We Place Our Game Content?

The first thing we are going to do is randomly place the rooms of a procedurally generated dungeon level.

In order to follow along, it's a good idea to have a basic understanding of how tile maps work. In case you need a quick overview or a refresher, check out this tile map tutorial. (It is geared towards Flash but, even if you aren't familiar with Flash, it's still good for getting the gist of tile maps.)

### Creating a Room to Be Placed in Your Dungeon Level

Before we get started we have to fill our tile map with wall tiles. All you need to do is iterate through every spot in your map (a 2D array, ideally) and place the tile.

We also need to convert the pixel coordinates of each rectangle to our grid coordinates. If you want to go from pixels to grid location, divide the pixel coordinate by the tile width. To go from grid to pixels, multiple the grid coordinate by the tile width.

For example, if we want to place our room's top left corner at (5, 8) on our grid and we have a tile width of 8 pixels, we would need to place that corner at (5 * 8, 8 * 8) or (40, 64) in pixel coordinates.

Let's create a Room class; it might look like this in Haxe code:

We have values for each room's width, height, center point position, and four corners' positions, and a function that tells us whether this room intersects another one. Also note that everything except for the x and y values are in our grid coordinate system. This is because it makes life much easier to use small numbers each time we access the room values.

Okay, we have the framework for a room in place. Now how do we procedurally generate and place a room? Well, thanks to built-in random number generators, this part isn't too difficult.

All we have to do is provide random x and y values for our room within the bounds of the map, and give random width and height values within a predetermined range.

## Does Our Random Placement Make Sense?

Since we are using random locations and dimensions for our rooms, we are bound to overlap with previously created rooms as we fill our dungeon. Well, we already coded up a simple intersects() method in order to help us take care of the problem.

Each time we attempt to place a new room, we simply call intersects() on each pair of rooms within the entire list. This function returns a Boolean value: true if the rooms are overlapping, and false otherwise. We can use that value to decide what to do with the room we just attempted to place.

The key here is the failed Boolean; it's set to the return value of intersects(), and so is true if (and only if) your rooms are overlapping. Once we break out of the loop, we check this failed variable and, if it's false, we can carve out the new room. Otherwise, we just discard the room and try again until we've hit our maximum number of rooms.

## How Should We Handle Unreachable Content?

The vast majority of games that use procedurally generated content strive to make all of that content reachable by the player, but there are a few people out there who believe that this isn't necessarily the best design decision. What if you had some rooms in your dungeon that the player could only rarely get to but could always see? This might add an interesting dynamic to your dungeon.

Of course, no matter which side of the argument you are on, it's probably still a good idea to make sure that the player can always progress through the game. It would be pretty frustrating if you got to a level of the game's dungeon and the exit was completely blocked off.

Considering that most games shoot for 100% reachable content, we'll stick with that.

### Let's Handle That Reachability

By now, you should have a tile map up and running and there should be code in place to create a variable number of rooms of varying sizes. Look at that; you already have some clever procedurally generated dungeon rooms!

Now the goal is to connect each room so that we can walk through our dungeon and eventually reach an exit that leads to the next level. We can accomplish this by carving out corridors between the rooms.

We will need to add a point variable to the code to keep track of the center of each room created. Whenever we create and place a room, we determine its center and connect it to the previous room's center.

First, we'll implement the corridors:

These functions act in nearly the same way, but one carves out horizontally and the other vertically.

We need three values in order to do this. For horizontal corridors we need the starting x value, the ending x value, and the current y value. For vertical corridors we need the starting and ending y values along with the current x value.

Since we are moving from left to right we need the two corresponding x values, but only one y value since we won't be moving up or down. When we move vertically we will need the y values. In the for loop at the beginning of each function, we iterate from the starting value (x or y) to the ending value until we have carved out the entire corridor.

Now that we have the corridor code in place, we can change our placeRooms() function and call our new corridor functions:

In the above image, you can follow the corridor creation from the first to the fourth room: red, green, then blue. You can get some interesting results depending on the placement of the rooms - for instance, two corridors next to each other make a double-wide corridor.

We added some variables for tracking the center of each room and we attached the rooms with corridors between their centers. Now there are multiple non-overlapping rooms and corridors that keep the entire dungeon level connected. Not bad.

## We're Done With Our Dungeon, Right?

You've come a long way building your first procedurally generated dungeon level, and I'm hoping you've realized that PCG isn't some magical beast that you will never have a chance to slay.

We went over how to randomly place content around your dungeon level with simple random number generators, and a few predetermined ranges for keeping your content the right size and roughly in the right place. Next, we discovered a very simple way to determine if your random placement made sense by checking for overlapping rooms. Lastly, we talked a little bit about the merits of keeping your content reachable and we found a way to ensure that your player can reach every room in your dungeon.

The first three steps of our four step process are finished, which means that you have the building blocks of a great dungeon for your next game. The final step is down to you: you must iterate over what you learned to create more procedurally generated content for endless replayability.

### There Is Always More to Learn

The method for carving out simple dungeon levels in this tutorial only scratches the surface of PCG and there are some other simple algorithms that you can easily pick up.

My challenge for you is to start experimenting with the beginnings of your game that you created here and to do some research into more methods to change up your dungeons.

One great method for creating cave levels is using cellular automata, which has infinite possibilities for customizing dungeon levels. Another great method to learn is Binary Space Partitioning (BSP), which creates some wicked looking gridlike dungeon levels.

I hope this gave you a good jump start into procedural content generation. Make sure to comment below with any questions you have, and I'd love to see some examples of what you are creating with PCG.

Related Posts