Followers 0

# Reversing a procedural generation

## 31 posts in this topic

Procedurally generated data has a random seed and random values generated by the process. Here is an example of what I mean:

You have a row of windows you want to either be broken or unbroken. You set you seed and loop through the "windows" randomly assigning "broken" or "unbroken" to each according to the output of the random number generator.

This is simple and straight forward......

What if I wanted to do that in reverse? If I started with all the windows "unbroken" and I throw rocks randomly and broke a few of them. I would like to reverse the process until I get THE random seed that would result in generating the windows into that state.

I'm sure it CAN be done, but how?

This process could be very useful in games that have a huge amount of "object states". Like a planet with mineral deposits. If the player collects a certain amount of them, it would be silly for the player to be able to come back to the planet and get those deposits again. And saving the state of each mineral deposit on every planet in every solar system would be crazy!

0

##### Share on other sites

If I have a random seed of 12345 and with the process, the windows generate:

1,0,1,1,0,0,0,0

where 1=="broken" and 0=="unbroken"

Then every time I go to generate the windows with seed 12345 it will look exactly the same.

If I break a window:

1,0,1,1,0,1,0,0 then the seed 12345 won't generate this result.

There IS some seed that will generate 1,0,1,1,0,1,0,0..... I want to go from the result to the seed. It's not magic, it's logic I am not fully aware of.

0

##### Share on other sites

This is a SUPER SIMPLE random number generator:

int RNSeed=12345;
int randomnum(void){
RNSeed=abs(RNSeed*8786545643)%987654321;
return RNSeed;
}



With this initial seed, the first time it's called it results in 770159010, and the second time it results in 808599291.

This will happen EVERY time I start with 12345. The problem with reversing the calculation is the modulus of 987654321. Any seed value greater than that number will be some value that can't be found and therefore the previous seed us unknown. The solution would involve a random number generator that can be reversed-- I don't know how to make that (or if it can be made at all).

All of you are either think I don't know how random generators work or you are missing my point entirely.

-2

##### Share on other sites

You can design a PRNG that is easily reversible, by using just about any private-key cryptographic method (any block cipher would do).

Now that's positive thinking!

I thank you all for responding. After serious consideration, I think you may be right about one thing: the random number generator wouldn't have calculations that would repeat numbers. This means even if I had some linear values, I couldn't have all 0's or all 1's or any number. Otherwise it wouldn't be a PRNG.... The game state should have the ability to have any valid state for all parameters.

+1 to all......

0

##### Share on other sites

Generate random locations for all mineral deposits every time you visit a planet.

When a player visits a planet, typically they will land at a location and collect all the minerals within range of their ship.

So work out a region that has been mined out. Store the region in the save game.

Next time you come to the planet, generate all the minerals again and compare each one to the mined region.

When you leave the planet, rationalise the region list. I.E. if two regions are contiguous, merge them into a single region.

Eventually you will end up with a single region, the whole planet.

The save game storage would not be a major problem then, and a simple point / region intersection test will not slow down the game very much.

1

##### Share on other sites

Yet another example is MineCraft. The world is generated by a 32-bit seed, yet it is infinitely large (theoretically). Obviously, you can't reverse that back into a seed, as explained above.

I would like to point out that early versions of MineCraft used to save the differences between the original world generated by the seed and the modified world edited by the player. Alas, the longer the world existed and the more you mined, the larger the save-files became and the longer it took to apply the differences whenever you loaded the world.

I recommend saving absolute states and not relative states. You can optimize it in many ways, one way is only saving the sub-sections that are different than what was generated by the seed.

1

##### Share on other sites

At this point it's probably obvious - there is no encoding that will work like you want it to

That's correct.....

To everyone: I've moved away from the idea of using a seed as a save state.

Perhaps I should start a new post on saving the state of a galactic sized game.....

Any ideas on minimizing the save game size for really huge games?

1

##### Share on other sites

That sounds interesting. How about storing save data as data for an image like .png? The data would look like a picture (noise), but it would utilize the image's compression..... Don't know if that's what you were getting to, but it made me think of it anyway.

Some values will be actual bites and some will be stored/read as bites but actually have 8 yes/no values..... Sounds like a good way to encrypt it too-- don't know if anyone is using this method.

0

##### Share on other sites

How about storing save data as data for an image like .png? The data would look like a picture (noise), but it would utilize the image's compression..... Don't know if that's what you were getting to, but it made me think of it anyway.

Yeah, that might make sense if you were using a library that had png support and for some reason weren't able to use some other 3rd party compression library.

I don't know the details of png compression, but I wouldn't be surprised if it made some assumptions about what a typical image looks like (e.g. smooth gradients from pixel to pixel, etc...). Basically, it assumes the data is an image, and will probably compress that better than arbitrary data.

The point being, if you know something about your data, you can probably choose a compression scheme that will work better with it.

0

##### Share on other sites

I don't know a lot about data compression. I've never written anything that used it. Image files are compressed on the disk (except .bmp). I was simply implying I could use something that already had support through DirectX.

0

##### Share on other sites

You certainly can store your data in an image format, and may see some benefit from that format's compression -- because image formats are designed with images in mind, your non-image data may not get an optimal benefit from that compression -- you could get better compression with something designed specifically for the type of data you're storing.

0

##### Share on other sites

Voxel world chunks do tend to end up very image-like, though, enough so that schemes such as RLE can achieve pretty significant compression. The caveat, of course, is that the more types of blocks you include, and the more widespread and scattered the types are throughout the world, the less effective RLE will be. You wouldn't want to actually use an image compression library (they're optimized for the 2D case, usually) but you can look to them for ideas.

2

##### Share on other sites

Álvaro, the zlib link takes me to the first post in this thread

0

##### Share on other sites

Álvaro, the zlib link takes me to the first post in this thread

Just google zlib. It's pretty well-known.

0

## Create an account

Register a new account