If you recall, with my previous top down shooter project, Planet: Discovery, I was working on level generation. Of course, the level generation algorithm used then was a very simple tile-based solution. The former algorithm would create a starting point, then randomly choose from the four cardinal directions where to start adding tiles and so on and so forth. It was a very linear approach to level generation and definitely could be improved upon, which is what I did.
I started researching more roguelike-style procedural dungeon generation algorithm, but there wasn’t anything that I really liked, until I got to Rooms and Mazes, written by Bob Nystrom.
His solution was very simple, and I quote:
- Place a bunch of random non-overlapping rooms.
- Fill in the remaining solid regions with mazes.
- Connect each of the mazes and rooms to their neighbors, with a chance to add some extra connections.
- Remove all of the dead ends.
Bob Nystrom
Of course, this algorithm is very simple to understand, but when I tried to finally implement a solution in Unity, it didn’t turn out that well. The first major problem I encountered was in step 1: place a bunch of random non-overlapping rooms.
Sounds simple enough, right? Well, I couldn’t find a very simple and efficient solution to placing random rooms without them overlapping. I tried hard-coding in positions to make sure the generator didn’t generate rooms where rooms were already generated, but that gave it a “too perfect” look.
Eventually, I discarded storing the positions of each room, and to just generate random rooms in a certain radius based on the height and width of the dungeon. If the room overlapped, it would get destroyed.
Then it was just a matter of searching up some maze algorithms, and I used the growing tree algorithm. There’s a giant collection of algorithm’s here, which can be useful, depending on what you’re trying to do.
I ended up with something simple, as shown below. The next steps I have to take are to add props, different tiles, a beginning and end point to each floor of the dungeon, and finally spawns for enemies. But for now I’m going to tackle Enemy AI.
[gfycat data_id=”SmoothDirtyGrebe” data_controls=”false” data_autoplay=”true” data_expand=”true”]