Building a Dungeon Generator, Part 1. Introduction
Tags: csharp, gamedev, unity2d, procedural
Published: 2021-12-29 02:25:59

(Originally published on my blog on December 19, 2021)

Over the summer, myself and a few friends hatched an idea to work on a project together to practice programming. A bit of brainstorming later and we settled on a game idea that is sort of a hybrid roguelike / Legend of Zelda style game, and started hashing out some details; finally we had enough of a working name to put together a Unity project and a github repo, and started banging out some code. (I am sure Project Dungeon will not be the final name of this project.... I hope....)

## Layout Generation

[Ajesh Jhalli](https://github.com/AjeshJhalli) and [Denver S](https://github.com/frodosynthesis) collaborated to design and implement the layout generator part of the code, which produces layouts like these based on input variables.

With twenty rooms:

![Layout with twenty rooms](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq0zd3oqg8k3m9rmaqvo.png)

Seven rooms:

![Layout with seven rooms](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3894dwpbjtzpmqy7p4t.png)

Thirty-seven rooms:

![Layout with thirty-seven rooms](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ok3hq6qw173eqhieaf96.png)

The blue room is considered the "starting room" and the red room is considered the "boss room". This data at this stage isn't interactable in any way, but shows a proof of concept in the code to build the layout, correctly assign doors, and pathfind from a randomized starting room to the most distant dead end room in the dungeon, which we then assign as a boss room.

Originally this was implemented using `Random`, but after watching a few Game Developer Conference talks on using "random" functions vs using a seed and hashing, I convinced my co-developers that we should use hashing to create noise, instead of random number generators, to determine our "random" generation. This way, every dungeon generated from the same seed will have the same layout, monster placement, and so on. This will allow different players to start in the same world if they choose, and also increase the reproducibility of behavior for testing and bug hunting.

Ajesh adapted the C++ function that Squirrel Eiserloh shows at 46:33 in his GDC talk, [Math for Game Developers: Noise-Based RNG](https://www.gdcvault.com/play/1024365/Math-for-Game-Programmers-Noise) into C# for us, and swapped our implementation over to use the new function. It works great! There will be some interesting challenges to solve in the future using this (for one, if we have one world seed used to generate all hashes, how do we handle changing the dungeon layouts when a character dies and we want to generate new ones?), but I think overall it is the correct choice. Besides, it's fun to generate a layout, mess with the seed, generate a new layout, then switch back to the first one and see the first layout again!

## Tilemap generation

I volunteered to handle the tilemap generation end of things, the actual generating of the rooms described by the layout generator as in-game objects. I figured I would be best positioned to do this as I'm also planning to handle the majority of the art assets as well.

Following parts of [this video tutorial series on generating procedural dungeons in Unity](https://www.youtube.com/playlist?list=PLcRSafycjWFenI87z7uZHFv6cUG2Tzu9v) by [Sunny Valley Studio](https://www.youtube.com/c/SunnyValleyStudio), and using [this 32x32 dungeon tileset](https://stealthix.itch.io/dungeon-tileset-32x32-px) by [Stealthix](https://stealthix.itch.io/) for prototyping, I got the code into a situation where I could paint tiles into a tilemap programmatically:

![Room tilemap painted by code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0u2vcvj1qu7fjk1xmpo.png)

But then I basically ran into a wall. I was thinking I would have to generate each room as an object and create a prefab or something to pass back to an array of rooms for the game engine to pick from as needed, and I just didn't know enough about Unity. I had no idea how to do that. Honestly, I still don't. Most of my time on this project in the past couple months has been spent on other things, like setting up `SpriteLibraryAsset`s and animations, and trying to get a better handle on how the different parts of Unity work together and interact in general.

*But*... Saturday I had a different idea.

What if we use each floor of the dungeon as its own tilemap? This idea will probably come with optimization challenges, especially if we go for larger dungeons. But I can see a path to making this work! And any working prototype is better than none. Let's do this!

Next up in part 2: adapting the Room Generator code to take floor and wall tilemaps, room dimensions, and starting coordinates as arguments. Also work on assigning wall tiles manually, since trying to do it with a rule tile ... didn't turn out good, and probably even less so once I switch to using the assets I created that are 3 units deep for the complete height of a wall on-screen. Oh, yeah, and at some point I'll need to finish figuring out the doors and room transitions to be able to generate them with code. I'm sure it's doable.