Flixel Forums

development => releases => Topic started by: Raf on Thu, Mar 14, 2013

Title: Random dungeon generator
Post by: Raf on Thu, Mar 14, 2013
I really enjoy Roguelikes, and am planning on creating one sooner or later. As I had a bit of spare time, and since my brain was just itching to write this, I created the very core of Roguelikes: The random dungeon generator.

The class's algorithm is very simple, but gets the job done. It's fully extendable, if you wish to do so. All configuration is also placed at the top of the class, in constants, so you can easily adjust the parameters to your likings. I could have added them as parameters to the constructor, but as these settings'd be the same throughout the entire game, setting them as constants made more sense.

Right now, it only uses "wall" and "floor" tiles, as per FlxTilemap.

Code is in attachment.

It's used as follows:
Code: [Select]
            var dungeonMap:DungeonGenerator = new DungeonGenerator();
            var level:FlxTilemap = new FlxTilemap();
            level.loadMap(FlxTilemap.arrayToCSV(dungeonMap.map,40), FlxTilemap.ImgAuto, 0, 0, FlxTilemap.AUTO);
            var someRandomFloorTile:Array = dungeonMap.getRandomRoomTile();
Title: Re: Random dungeon generator
Post by: Leebo on Thu, Mar 14, 2013
Cool, I'll have to play around with it later.
Title: Re: Random dungeon generator
Post by: camasthecat on Thu, Mar 14, 2013
Cool, I'll have to play around with it later.

Ditto. XD
Title: Re: Random dungeon generator
Post by: snakebee on Thu, May 30, 2013
First of all, thank you so much for sharing this, I'm currently using this in my game! Also first post from a long time lurker :P

I couldn't get the get random floor tile functions to work right for the life of me, so I ended up coming up with my own fix.

For random room tiles:

Code: [Select]
protected function fillRect(startX:int, startY:int, width:int, height:int):void {
for (var x:int = 0; x < width; x++) {
for (var y:int = 0; y < height; y++) {

var index:int = (startY + y) + (startX + x) * TOTAL_COLS; // Thanks to wg/funstorm for this formula

map[index] = FLOOR;

// Store the coordinates in the rooms array, to keep track of all rooms' floors
storeRoom(index % TOTAL_ROWS, Math.floor(index / TOTAL_COLS));

I don't know if I'm missing something simple but it works well enough for me,  8)

You can do the same kind of thing to the random corridor tiles, except it still doesn't work right since corridor paths start within a room, so there's a chance it will store coordinates inside a room. Not to mention another room can overlap a corridor path. You'd need a way to remove or ignore all the corridor points that are within room boundaries when they are stored.
edit2: I was able to fix the random corridor tile by simply splicing the elements that have the same coordinates as room coordinates using a nested for loop. Depending on the size of the dungeon it may take the game longer to load but it works.