Need help with procedural generation

Hi guys. I’m making an open-world space game. The first step for me is making a procedural universe generator (infinite galaxies, filled with around 1 million stars each which have planet systems). I am definitely not going to create content for everything. So I want to use procedural generation.

I know some of the theory - only loading stars within a certain range and unloading when further away, using a pattern such as the Fibonacci sequence with seeds for the first number and using data from that to generate details such as position, type and name.

What I have currently:

  • System that generates positions and names for the galaxies and puts them in an array (will change to lists)
  • That’s it, really. I have set a galaxy count variable as I am unsure on how to do it infinitely like I want - so it often crashes or gives an out of memory exception when I set something like 1,000,000+.

Please can you suggest how I would only generate when closer without storing it in an array/list and how to make it infinite?

PS: I would also like suggestions/tips on how to make it work with multiplayer

Thanks,
Noxbuds

I have seen other people using Mersenne Twister. Would that be worth a try?

I suggest you take a look at the Minecraft world generation tutorials and topics. A lot of those ideals can be applied here.

Thanks. I will have a look into it

I just found out that the Random (Random Class (System) | Microsoft Learn) class is a psudo-random number generator o.o

For your map you should work with Chunks/Nested-lists and about the random thing there is only pseudo randomness

OK. I specifically want pseudo-random - it is going to be multiplayer, so it must generate the same thing every time.
I am working on a number generation extension where you provide it with multiple integers for different things.

You can’t have non-pseudo-random number generation on a computer, so you’re set there :slight_smile:

Only use the Mersenne Twister if you absolutely want to be sure that the numbers are uniformly distributed, as it’s slower.

If it’s multiplayer and you want to make absolutely sure the numbers generated are identical, I’d probably sync it, somehow. Depending on the amount of numbers you’re looking at generating, you could make a very long array of random numbers and send that to the client. Then, just check once in a while that you’re on the same index.

OK. Not sure how I would sync it though. I plan to have millions of stars and thousands of galaxies…

Just remember that the experts at Kerbal Space Program only added the local solar system(and while they are doing full simulation, and I assume you will not be), it was quite a feat. How about trying to get one solar system working right now?

1 Like

you dont need to sync any think

if you do

        Random.seed = 10;   
        Debug.Log (Random.value);

it should give you the same result every time you runn the code (maby different values with different OS like win7x64 or x84)

Yes, this works across the Random functionality too. So if you need values above outside the range of Random.value.

using UnityEngine;

public class PsuedoRandom : MonoBehaviour
{

    private void Start ()
    {
        for (int i = 0; i < 25; i++)
        {
            Random.seed = 15454;
            Debug.Log (Random.Range (0, 10));
        }
    }
}
/*
Output:
7 (25 times)
*/

The syncing was more in the context of generating millions of random numbers, and always being on the same index on client and server. If you play for an hour or something, there’s probably a chance that they’ll get out of sync, which would be “a bad thing”.

thats the point you dont need to sync it every client will generate the same “galaxy” you just need to sync the position what can be 2 int values i added a small image

Exactly. I will probably make it so the server only stores things like currency, positions, ships etc. - not even starports (unless I add player-made ones)
I did try to generate a list of galaxies… I crashed Unity.

First comes Galaxies/Stars. Then planets :slight_smile:
First I will make the galaxy map, then use similar methods for the planets. In-game, the player will only get to see/visit the contents of their current star system unless they Hyperspace to the next system.

I am thinking of doing it a bit like this:

By the way, I did some calculations. I want at least 5000 galaxies, at least 1 million stars each. An Int32 is 4 bytes AFAIK, so 5,000*1,000,000=5,000,000,000. Which means the total size is 5,000,000,000 * 4 bytes or 20,000,000,000 bytes. If we minimize the numbers (divide by 1024 multiple times) we find out that it would mean 18 gigabytes of just Int32s. On top of that, I need space for HD starship models, player avatars, textures, starport attachments, landing pads, space elevators and so on. If I did that, it could easily become over 50gb in size. I do not want that.

EDIT: I realise that 18gb would be on a server, but currently I do not have a server to do that with. I want server interactions to be kept minimal

EDIT EDIT: It cannot possibly get “out of sync”. It is generating a number pseudo-randomly based on the same seed. There is no updating the galaxy server-side. The only parts that will be saved will be custom overrides I set (which I probably will)

So I’m thinking for the chunks, I run them through my number generator to get positions for stars and multiply/divide based on the distance to the galactic bulge. I am not entirely sure about spirals for the galaxies, but I can think something out

And then for the galaxy, I create lines where stars should be. I’ll position them around these lines:

(yellow squares demonstrate chunks on a larger scale)

Something like this I think:

int x1 = 0;
int y1 = 0;
int current_rotation = 0;
int arm_length = 100;
GameObject arm_point;

for (int i = 0; i < arm_length; i+=10) {

    x1 += Mathf.Sin(current_rotation);
    y1 += Mathf.Cos(current_rotation);

    Instantiate(armpoint, new Vector3(x1,y1,0), new Quaternion(0,0,0,0));
    current_rotation += 50;

}

And then the generator will go from point A to point B, scattering the stars left and right of the arm.
Good idea?

That’s a lot of stars.

I thought you were going to continually generate the universe over time when playing. Then it would have been a good idea to sync up sometimes, because of race conditions and stuff like that.