Smarter Procedural Roguelike Dungeon

4

Attached Files

The following files have been attached to this tutorial:

.capx

dungeon-base.capx

Download now 175.46 KB

Stats

7,771 visits, 11,838 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 17 Aug, 2014. Last updated 19 Feb, 2019

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

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.

.CAPX

dungeon-base.capx

Download now 175.46 KB
  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Thanks to make thing clear to me mate. ☺️👍

  • Even is this is an old tutorial its immensly usefull, thank you very much! I will try to extend it a bitso that the corridors are not to chaotic :D