Reputation: 155
I'm creating a text-based game, and trying to implement procedural world generation.
My initial plan was a bit haphazard: each universe has a 3D array of galaxies, of solar systems, which then in turn would be propagated with randomized celestial bodies. I was planning on generating the actual local area separately, but I'm not sure it's currently possible to complete the task as I have it.
Each universe is a Galaxy[10][10][10] (arbitrary number at the moment),
each galaxy is a randomly sized SolarSystem[50-150][50-150][50-150],
each SolarSystem is a randomly sized CelestialBody[5-20][5-20][5-20].
All of which would then be written out to a data file to be read later.
Looking at it now, if I'm not mistaken, this would require (((ClassSize^3)^3)^3) bytes, which is impossible to store even if the ClassSize were only 4 bytes.
My intent with arrays of arrays initially was to be able to efficiently group clusters together and better help identify where the player was in the universe.
My question is: how can I generate a world of such scale more efficiently?
Upvotes: 2
Views: 2056
Reputation: 33954
Rather than trying to store the resulting universe, simply create a unique, random seed value for each player, and use that to procedurally generate the world on the fly, as the player plays.
When you seed your generator with the same value each time, the random numbers will be the same every time. So, if my unique, random player ID is 654156475
, then throw that ID into the universe generator when I load up the game, and the generator will produce the same universe every time. A different player will get a different universe, because their seed is different than mine.
See the "video games" section of this article for a brief overview of how this technique is used in games.
Or, instead of writing it as a universe generator, write it as a solar system generator (or whatever is the smallest unit of space that the player will occupy). Then, store a random seed for each solar system, and use those seeds (which will be a relatively small amount of data) to generate (and re-generate the same stuff later) the play field on the fly as the player plays.
The major advantage to this approach is that you only have to store the seed values on disk, which is a very small amount of data, and you don't have to store the universe data at all. Not only that, regenerating just small parts of the universe on the fly can often be much faster than loading it from disk anyway.
Upvotes: 9
Reputation: 784
Having done something like you describe many, many years ago, (back when PCs had two 5.25" floppy drives) I wouldn't preallocate the entire game in memory. You should break it up so the game loads the portion of the universe the player is in, say in 3D blocks of 10x10x10. When the play wanders moves across the boundary of the loaded space, write that space out to disk and read in the space they've moved into.
Upvotes: 5