Dungeon Generator
Apparently there is some interest in how I made my dungeon generator. As of yet it outputs pretty basic maps, but there is enough there to build off of (in my opinion).
The code is in C#. It is supposed to simulate a human picking up square tiles that are one of the following: passageways (dead end, straight, turns, Ts and intersections) or rooms (vaults, one exit, two exit, three exit, or four exit) and placing them in an n by m grid configuration so as to make a maze of passageways and rooms.
The call to generate a map looks like this:
MapBuilder mb = new MapBuilder();
mb.Build(6, 4, 1);
And the output looks like this:
That translates to this:
The input that I gave for this example was larger than I would usually give. if you cut off the right two columns and the bottom row:
│╔═╗┬═
├╝╦╬╬.
│$╬┼└┤
╠┴╬╬═┘
That translates to this:
The input that I gave for this example was larger than I would usually give. if you cut off the right two columns and the bottom row:
│╔═╗
├╝╦╬
│$╬┼
It gives a far more nethack like dungeon (7 rooms + a vault on a level would not be unusual.)
Having upstairs and downstairs placed would be simple (though in my ascii condensed maps I couldn't figure out how to both show that they were there and also show how many and where the exits were)
The algorithm that I use is as follows:
Given an x Size and a y Size:
1) determine a random room type (1 door, 2 door, 3 door or 4 door) and location (bounded by the size of the dungeon) for the up-stair.
2) take enough random tiles to fill the rest of the level (this is where you could easily add in a down stair tile)
Until the level is full:
3) place the current tile on the map, taking into consideration the surrounding tiles (basically, rotate the tile to make as many exits line up with the surrounding tiles as is possible).
4) If there are multiple possible orientations that would line up with a maximal number of inbound passageways, then pick the one that has the most outbound passageways pointing into unexplored territory. (If that fails, pick randomly)
5) record the outbound passageways that have no tiles connected to them so that you can fill them in later.
6) if there are no outbound passageways, then declare that you have found a 'secret passage' and arbitrarily choose a point that is outside of the discovered territory to begin exploring. The outputted maps do not have any indication of this (so you would currently have to just make a note as to where it went from and to, but that could easily be rectified)
7) determine a random room to place in the next available slot (either a outbound passage that has not been explored, or the tile at the end of a secret passage)
Here is the code (It is probably unreadable as it is. You should ask me if you do not understand anything.)
Comments
Post a Comment