### Author Topic: Using Seeds to Generate Levels  (Read 9379 times)

#### Alex E

• Rogueliker
• Posts: 118
• Karma: +0/-0
##### Using Seeds to Generate Levels
« on: August 19, 2012, 03:26:23 AM »
E.g. Minecraft. It uses seeds to generate environments. Using the same seed will create the same environment. How would you translate this into roguelike where you are mainly picking a random point in the level and adding assets there?

I don't want to make any kind of wilderness or cave, since those generate mainly with fractals or algorithms. I'm talking about creating a wall at a random place. Or making a road go 20 tiles north and then go 8 tiles west.

So how would you use seeds to do that?

#### Pueo

• Rogueliker
• Posts: 263
• Karma: +0/-0
##### Re: Using Seeds to Generate Levels
« Reply #1 on: August 19, 2012, 03:41:18 AM »
Well, in C, you use the 'rand' function to generate random numbers (which is usually how you do everything random in a roguelike).  However, unless you use the 'srand' function, your results will always be the same, because the rand function needs to be "seeded" (I think that's what it's called).

This is were seeds come in.   The rand function plugs that seed into a pseudo-random number generating algorithm.  The srand function gives the rand function a seed to work with.  The most common use is "srand( time(NULL) )", which plugs the current time in as a seed (this ensures that the seed is always different).

You could, however, ask the player if they want to input a specific seed.  (Have them type in a string, called it char seed[10], then use "srand(seed)")  If they input the same seed twice, then the number generating algorithm will churn out the exact same numbers twice, giving you the same map.

I don't want to make any kind of wilderness or cave, since those generate mainly with fractals or algorithms. I'm talking about creating a wall at a random place. Or making a road go 20 tiles north and then go 8 tiles west.
The wilderness and cave structures can also be generated through seeds (I made a cellular automata dungeon with seeds and the rand function).  It's all about how creative you are.

Creating a wall at a random place: Seed the RNG with a specific string, tell it to pick a number between 1 and 100.  As long as you use that exact seed, you will always get the same number.  Instead of telling it to pick a number between 1 and 100, tell it to pick two numbers to use as coordinates, then place a wall there.

Making a road go 20 tiles north, then 8 tiles west: If you mean you want it to go exactly 20N then 8W, it's more reliable to do that manually.  However, if you mean just random directions/lengths but always the same due to seeding, then do the same as "creating a wall at a random place."  Tell it to pick a number between 1 and 4 (North, East, South, West), then a length.  Do that a couple times, and you get a road that goes in a few random directions.  Use the same seed again, and you get the same road.

Summary:
How would you translate this into roguelike where you are mainly picking a random point in the level and adding assets there?
Seeding is all about giving the RNG something to work with.  If you give it the same seed twice, you get the same results twice.  If you give it the same seed each time you start the program, all the random points you pick will be the same each time you start the program, due to the magic of seeds.
« Last Edit: August 19, 2012, 03:56:48 AM by Pueo »
{O.o}
|)__)
” ”   o RLY?

#### Alex E

• Rogueliker
• Posts: 118
• Karma: +0/-0
##### Re: Using Seeds to Generate Levels
« Reply #2 on: August 19, 2012, 06:36:54 AM »
Thanks for the help.

#### kraflab

• Rogueliker
• Posts: 454
• Karma: +0/-0
##### Re: Using Seeds to Generate Levels
« Reply #3 on: August 19, 2012, 06:51:50 AM »
Note that if you want to make sure certain things unfold the same way regardless of player input, you need to have separate instances of the rng with separate seeds.  In other words, if you gen each level only when the character gets there, but you also use the rng to generate damage and hitting, the number of attacks will lead to different levels later on.  If you split things into a level rng and a battle rng though then you can be sure the entire dungeon will be the same.

#### Pueo

• Rogueliker
• Posts: 263
• Karma: +0/-0
##### Re: Using Seeds to Generate Levels
« Reply #4 on: August 19, 2012, 07:31:03 AM »
In other words, if you gen each level only when the character gets there, but you also use the rng to generate damage and hitting, the number of attacks will lead to different levels later on.  If you split things into a level rng and a battle rng though then you can be sure the entire dungeon will be the same.
Thanks, I never noticed that with mine (probably because I generated the whole dungeon in the beginning), but that'll be good to know in the future.
{O.o}
|)__)
” ”   o RLY?