There are a lot of dungeon generators on the internet that are mainly used for D&D or other P&P RPGs. The bad part is there is no source code for any of them. I did find a link describing the process used in roguelikes to generate dungeons and was able to get a copy of the source for Angband so that was a good start. I ended up deciding try and implement a cell based algorithm that's used in roguelikes
It basically works like this
- Pick a cell size like 8x8 or 2x4
- Pick a map width and height and multiply each by the cell size and that will give you the total map size
- For each cell randomly decide to add a room that has a random width and height less than the size of the cell but greater than 1
- Loop through all the cells that have a room and find the closest cell with a room thats not already connected and add a corridor between the rooms.
- Mark the room as connected
- Repeat steps 4-5 until all the rooms are connected
I currently store the map in a 2-dimensional enum array that marks a block as None, Wall, Room, or Corridor. This helps with adding doors in a later pass.
Remember when I said Unity was a good tool to focus on the game and not the technology? This also makes it a VERY good prototyping tool. Here is a link to what I implemented in Unity. If you want to see a different dungeon just hit the refresh button to reload the plugin.
Unity isn't actually generating the dungeons. It's hitting a servlet on the Google app server it's hosted on that returns generated dungeon data. The generator servlet outputs both binary and html that I used to visually debug the code.
I may or may not continue working on generator but it was fun figuring out how to make it all work