Random Generation with Arrays

0 favourites
  • 2 posts
From the Asset Store
Template for dungeon/maze generation, using wave function collapse
  • Hi! I'm using Arrays to randomly generate planets in a solar system.

    Essentially, it's just a For Each loop that goes through each planet and randomly picks from traits stored in an Array. It'll start by picking the planet type (Ice Planet, Desert Planet, Etc.), then move onto how resource rich it is (Barren, Moderate, Abundant, etc.) and so and so on. To make certain traits more rare, all I've done is added more of the same options to the column (i.e. 'Super-Abundant' appears only once, so the chances of it being picked at random are lower).

    It's great fun but very simple and I'd like to be able to affect the probability of outcomes based on previous selections. For example, if it's a Desert Planet, I want the chances of it being resource rich to decrease. If it's has abundant resources, I want it to be more likely to be habited - or for the society to be more wealthy, etc.

    It's an interesting challenge and I'm not sure I know what the cleanest solution is. I'd love to hear how others would approach this and how you might solve it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • To make certain traits more rare, all I've done is added more of the same options to the column

    I suggest using the probability tables feature of AdvancedRandom plugin. You can define multiple values with different weights. To make things easier, you can store the entire probability table as a JSON string in the array. I prefer adjusting the weights so that they all add up to 100%. For example:

    [[5,"Super-Abundant"],[20,"Abundant"],[20,"Rich"],[30,"Common"],[25,"Barren"]]
    

    Use AdvancedRandom.weighted expression to return a random value. In the example above, "Super-Abundant" option will have 5% chance to be picked.

    You can store such probability strings for each planet type.

    Although I would suggest using a JSON or multuple JSONs instead of the array. In many cases JSON is much more convenient to work with in C3. You can come up with a good structure which will be easy to read and update. It can be something like this:

    {
     "Ice": {
     "RichnessChances": [[5,"Super-Abundant"],[20,"Abundant"],[20,"Rich"],[30,"Common"],[25,"Barren"]],
     "HabitableChance": 10,
     "Resources": "ice,water,glass"
     },
     "Desert": {
     "RichnessChances": [[1,"Super-Abundant"],[10,"Abundant"],[20,"Rich"],[40,"Common"],[29,"Barren"]],
     "HabitableChance": 30,
     "Resources": "sand,iron,copper"
     }
    }
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)