Legacy GM [solved] Procedural world generation

Psycho_666

Member
Hey.
I am making a game and I am reaching the point where I need to generate the world.
It's a strategy game, so I need to generate the overworld map and the rest will be taken care of by other algorithms I have already made.
My idea is, let's say I divide the world on 5x5 sectors and each sector is different area. That gives me 25 tile map.
Here's the tricky part.
I have let's say 10 dungeons and the other sectors id like to split between land and water.
Random won't work and my brain can't give me a decent algorithm of how to make sure I'm not putting two dungeons at the same location.
So basically, how to make sure I won't generate different dungeons in the same sector?
It's probably something really simple. I just can't think of it.
 

jo-thijs

Member
Hey.
I am making a game and I am reaching the point where I need to generate the world.
It's a strategy game, so I need to generate the overworld map and the rest will be taken care of by other algorithms I have already made.
My idea is, let's say I divide the world on 5x5 sectors and each sector is different area. That gives me 25 tile map.
Here's the tricky part.
I have let's say 10 dungeons and the other sectors id like to split between land and water.
Random won't work and my brain can't give me a decent algorithm of how to make sure I'm not putting two dungeons at the same location.
So basically, how to make sure I won't generate different dungeons in the same sector?
It's probably something really simple. I just can't think of it.
With sector, you mean a tile on the 5x5 map?

I can think of 2 main algorithms:
1) Create a list filled with every tile location, shuffle it and pick the first 10 locations:
Code:
var mapWidth = 5;
var mapHeight = 5;
var mapTotal = mapWidth * mapHeight;

var amount = 10;

var list = ds_list_create();

for(var i = 0; i < mapTotal; ++i) {
    ds_list_add(list, i);
}

ds_list_shuffle(list);

for(var i = 0; i < amount; ++i) {
    var tileX = i mod mapWidth;
    var tileY = i div mapWidth;
    
    // Some code to mark tile (tileX, tileY) as having a dungeon
}

ds_list_destroy(list);
2) Pick 10 random numbers out of a range corresponding to the amount of tiles that are left and transform those numbers to tiles (by keeping a sorted list of the previously picked tiles):
Code:
var mapWidth = 5;
var mapHeight = 5;
var mapTotal = mapWidth * mapHeight;

var amount = 10;

var list = ds_list_create();

for(var i = 0; i < amount; ++i) {
    var r = irandom(mapTotal - i - 1);
    
    for(var j = 0; j < i; ++j) {
        if r >= list[|j] {
            ++r;
        } else {
            break;
        }
    }
    ds_list_insert(list, j, r);
    
    var tileX = r mod mapWidth;
    var tileY = r div mapWidth;
    
    // Some code to mark tile (tileX, tileY) as having a dungeon
}

ds_list_destroy(list);
The latter algorithm is more performant for small values of "amount" with respect to "mapTotal", whereas the former algorithm is more performant for larger values of "amount".
In your case though, you shouldn't be concerned with performance, because your field is small enough that you won't notice a difference.
So, you should go with whichever one is more readable to you (probably the former algorithm).
 

Psycho_666

Member
In your case though, you shouldn't be concerned with performance
Yes, I will be doing this once in the beginning.
But here's my issue with both those methods:
I didn't mention the seed even though I mentioned procedural generation.
I don't want it random. I want to be able to generate the same map with the same seed for both consistency and bug testing...
Maybe I can use a list with some sort of calculated values for each tile, then sort them in a list and tale let's say the top 10 results for my 10 dungeons, then split the rest for water and empty land...
Randomisation will not work in any foem.

Also: merry Christmas everyone :)
 

jo-thijs

Member
Yes, I will be doing this once in the beginning.
But here's my issue with both those methods:
I didn't mention the seed even though I mentioned procedural generation.
I don't want it random. I want to be able to generate the same map with the same seed for both consistency and bug testing...
Maybe I can use a list with some sort of calculated values for each tile, then sort them in a list and tale let's say the top 10 results for my 10 dungeons, then split the rest for water and empty land...
Randomisation will not work in any foem.

Also: merry Christmas everyone :)
Merry Christmas!

Just before geneating the map, set the seed using random_set_seed.
Aterwards, you could use randomize again if you'd like.
 

jo-thijs

Member
Is it really that simple? I use the seed to determine the numbers that the random function will give me and then I can randomize as much as I want and it will be alright?
Sure.

Let me make one thing clear however, randomize will set a seed based on the current time.
If you use radomize too frequently, your numbers won't be quite as random anymore, because the time won't have changed enough to change the seed based on it.
If you only use randomize a couple of times in your project, this won't be an issue.

So, there's a bit of caution with "I can randomize as much as I want" if you mean using the function "randomize" with that.
 

Psycho_666

Member
Sure.

Let me make one thing clear however, randomize will set a seed based on the current time.
If you use radomize too frequently, your numbers won't be quite as random anymore, because the time won't have changed enough to change the seed based on it.
If you only use randomize a couple of times in your project, this won't be an issue.

So, there's a bit of caution with "I can randomize as much as I want" if you mean using the function "randomize" with that.
You are talking about the function "randomize", while I meant, that I can use the random function as much as I want and as long as I use the same seed there should always be the same results.
Honestly I don't mind overcomplicating the generation algorithm if that means I will get what I want.
But if setting the seed will give me always the same results, that's all I need.
 

jo-thijs

Member
You are talking about the function "randomize", while I meant, that I can use the random function as much as I want and as long as I use the same seed there should always be the same results.
Honestly I don't mind overcomplicating the generation algorithm if that means I will get what I want.
But if setting the seed will give me always the same results, that's all I need.
It will.
 

Psycho_666

Member
Thank you.
If I were to do it on my own I would have created a huge complex formula to calculate stuff, and all I need to do is one simple command.
Thank you again.
 

YanBG

Member
You also can set the seed to the chunk(or based on it's x and y). Because in a larger world i delete the tiles outside of view and place them again later but since the player can switch directions, the random would mess up.
 
Top