Smarter Procedural Roguelike Dungeon

Favourite 49 favourites
Tutorial written by SupremeRobotOriginally published on 17th, August 2014 - 9 revisions

Hi there! Welcome to my first tutorial!

I am working on a procedural death game and after looking through several tutorials I realized an interesting way to combine programming techniques.

Today I want to talk about combining procedural dungeons and the bitwise method of tile placement.

Procedural Dungeon

The basic dungeon generator I made was based on this post. As I found out, after a bit of searching, stemkoski has already ported it over to construct 2 and made a tutorial about it.

He translated the work very similarly to how I did with only a few differences. Go read his post and I will tell you what the differences are...

Back already; great. Here are the steps I used.

STEP 1. Define a grid and then set up 2 three dimensional arrays.

Arrays
DungeonMap (width of grid, height of grid, 2)
DungeonRoom (2,2,Number of Rooms)

*Pro tip: You can set the width and height of your DungeonMap array to be scalable by dividing the height and width of the layout by the size of your tiles and rounding down. That way whenever you set the size of your layout, your dungeon automatically fills up the space.

The equation would be floor(LayoutWidth / Gridsize)*

The DungeonMap array will be filled with zeros to start. The first plane will be filled with numbers for each cell that tell the game what to place there. 1-s for floors, 2-s for walls, etc...

The second plane will be filled with Cell Identifiers using the bitwise method. (That has its own section below.)

STEP 2. Define rooms.

The DungeonRooms array has planes that consist of four cells; two rows and two columns. There is one plane for each room we want to make.

The first row has the x position and the y position in each cell. The second row had the width and the height of the room.

Set the width and height first and THEN randomize the location. That way you can subtract the width and height from the maximum x and y positions so your rooms will not run off of the layout.

Set the corresponding cell values in DungeonMap to 1.

Here is how my event sheet looks:


PoAr stands for populate array.

RX, RY, RH, RW = room x, y, height, width

Output so far:


STEP 3. Define corridors.

I find a random point inside the first room in the DungeonRoom array and then a random point in the next room in the array depth and draw horizontal and veritical lines in the DungeonMap array with 1's to the points in the rooms.

I do this in a loop that stops 1 short of the number of rooms. Three rooms need only two corridors.

The event sheet.


Please ignore the StartX and StartY, those were variables I thought I needed but forgot to remove.

Output looks like this:


STEP 4. Define walls.

Anytime a "1" is next to a "0", place a "2" there instead.

This is simply running through each cell in the top plane of the DungeonMap array, seeing if it has a 1 in a cell, checking to see if any cell adjacent to it has a 0 and changing that to a 2 instead.

Here is my even sheet:


Output so far:


The Bitwise Method

The bitwise method for tiles is... oh heck.. zatyka already made a tutorial on it. But in short, it is assigning a cell a number based on what is around it. In this case, floor cells will have a different number depending on how many walls are around it and where they are placed.

Here is the best explanation I have found.

STEP 5. Assign values to the underlying cells on the second plane of the DungeonMap array.

Here is the way I assigned values:


And here is my event sheet:


Once all values are assigned, you can check the value of any coordinate on the first plane by reading the value in the matching cell on the second plane.

So if a cell has a wall to it's upper right. The value is 1.


If a cell is in a horizontal hallway the value is 221
Open full size image

As you can see this is VERY useful when placing things on your map. I used it to have traps that randomly spawn but only in certain places. I put certain types of tiles near walls and other kinds in the open. I was able to have power ups that only spawn in hallways.

STEP 6: Draw the map.

Now all we have to do is take the information in the DungeonMap and and use it to draw tiles on the screen.

Here is the event sheet portion that draws the map based on the content of DunegonMap


Conclusion

Other ways you can spice things up:

I placed the coordinates of my starting room in the DungeonRoom array so that the room is always where I want it to be. I am going to use this for other kinds of rooms as well. That way random rooms can exist side-by-side with planned out rooms and the game engine can fill in all the gaps.

I am still exploring and finding interesting ways to populate my dungeon.

I am attaching the capx file for the dungeon engine.
Here is a playable prototype level of a game I am working on.Maze of Cthulu
Space to shoot. Arrow keys to move.

This is the end of my first tutorial. Let me know if anything was unclear. Also, I just want to say thanks to Scirra for making such a great tool.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

0
LolindirLink 4,954 rep

indepth yet fairly easy for anyone. thanks!

Tuesday, March 29, 2016 at 8:24:24 PM
0
elitezore 3,423 rep

This is insanely useful!

Friday, October 14, 2016 at 1:11:28 AM
0
artheads 2,859 rep

Hi there was a capx with explanations - how to do it using tilemap... Any advices?

Monday, August 14, 2017 at 6:32:25 AM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.