Author Topic: [Resource] Simple dungeon layout generator  (Read 770 times)

This is a simple dungeon layout generator I wrote (only 207 LOC expanded) which creates simple arrangements of "rooms" as well as letting you place additional special rooms afterwards on leaf rooms. A special thing about it is that it doesn't take a width and height, rather dynamically expands from [0, 0]. It's also guaranteed to give you the requested number of rooms. Here's an example of using it:

==>$x = Dungeon();
==>$x.generate();
==>$x.visualize();
Visualizing Dungeon() with 15 rooms.
 R    
RR R  
 RRSR  
   R  
  RRRRR
==>$x.reset();
==>$x.generate(30,"end");
==>$x.visualize();
Visualizing Dungeon() with 30 rooms.
     R  
 RR  R  
  R RRR
 RR S  E
  RRRRRR
RRR R  R


Source code (dungeon.cs): https://gist.github.com/portify/6203660

Use it for whatever you want.

Additionally, it's possible to iterate over rooms as follows:

%count = %dungeon.getCount();

for (%i = 0; %i < %count; %i++) {
   %room = %dungeon.getObject(%i);
   echo("Room (" @ %room.type @ ") at " @ %room.x @ ", " @ %room.y);
}


You can also change the type of a room at a position easily:

%dungeon.setRoom(2, 4, "shop");

Wow. This is really useful.

Function reference

Dungeon()
Returns a new Dungeon (ScriptGroup) object.


Dungeon::generate([size=15[, specials=""]])
Generates rooms until the total number of rooms is size. When done, if specials is not empty, it will iterate over it's fields and replace leaf rooms with each individual field. Returns true when both operations succeed, false otherwise.

E.g. Dungeon::generate(30, "boss" TAB "shop") will generate 30 rooms, then place a boss room and a shop room.
If the dungeon already has some rooms generated (or set), you will need to call Dungeon::reset() before this method.


Dungeon::setRoom(x, y, type)
Creates a room at the given coordinates if there is no room there, or, using the already existing room, sets its type to the given type.
The first thing the generator does is run Dungeon::setRoom(0, 0, "start").


Dungeon::reset()
Removes all rooms and room[x, y] mappings from the object, making it ready for generating again.


Dungeon::visualize()
For debugging purposes, this function iterates over all rooms to get the boundaries of the dungeon and displays them as a top-down view in the console.


Dungeon::getCandidates()
Used internally for room generation - returns all points in the dungeon where a room is allowed to be placed.


Dungeon::getLeaves()
Used internally for special room placing - returns all points in the room which contain leaf rooms.


Dungeon::getNeighbors(x, y[, rooms])
Used internally for Dungeon::getCandidates - returns all points around the coordinates that do not contain a room, unless rooms is true, in which case it returns all points around the coordinates that do contain a room.