6 Replies Latest reply: Feb 22, 2010 12:29 PM by 796262

Map generating algorithm

I'm making a 2D RPG and I want the level's to be (more or less) randomly generated, but I have no clue where to start.

I'm not asking for code, just a point in the right direction.
• 1. Re: Map generating algorithm
1 - you need to decide on a tile scheme: square, hex, or other.
2 - you need to make tile graphics to fix the terrain you want to support
3 - you need to learn to generate random numbers 0 through N-1 where N is the number of distinct terrain types

Once you have the planning done, you can think about how you want to implement.
• 2. Re: Map generating algorithm
I already have the graphics, and as for the tile scheme, it's square - and at your third point, you kinda lost me...
• 3. Re: Map generating algorithm
Since you are going to be generating a "random board", you will be assigning random numbers ranging from 0 to N-1 to each of your tiles. For instance, if you have 8 tiles, your N will be 8--the number of your tiles, you'll assign a number ranging between 0 and 7 to each of your tiles for your board. For a 10x10 board you'd be doing something along this line:
``````  //warning untested code
Tile[][] tile = new Tile[][];
Tile t = null;
for(int i=0; i<10; i++){
for(int j=0; j<10; j++){
t = new Tile();
t.type = myRandom.nextInt(8);
//what ever assignments you want to give to your tiles
tile[i][j] = t;
}
}``````
• 4. Re: Map generating algorithm
Of course in practice it won't be quite that simple as each possible section will no doubt need rules about which other sections can fit on either side of it.
If you don't do that, you quickly run into trouble with totally unrealistic maps.
For example you might have a section that has a river running through it from top to bottom border on something like a mountain or desert with no well, spring, or lake for that river to flow out of or in to.
• 5. Re: Map generating algorithm
Wow... Sorry, somehow I failed to mention that I'm trying to generate a bunch of randomly generated connecting rooms.

What I've decided to do is create 5-10 pre-built rooms (doors and all) which can all connect to each other (when lined up properly). So I generate a random amount of rooms, then lay them out next to each other. Then afterward creating several randomly generated rooms on the outside of those rooms (so there isn't any doors leading out into an endless void).
• 6. Re: Map generating algorithm
b1nary wrote:
Wow... Sorry, somehow I failed to mention that I'm trying to generate a bunch of randomly generated connecting rooms.
Ok, and why won't the approach already given to you work in that case?