- NewEnemyType=Choose("zombie","vampire","goblin","troll")

In this case each enemy has equal chances to spawn, lets say 100 probability points.

- zombie - 100 probability points

vampire - 100 probability points

goblin - 100 probability points

troll - 100 probability points

Now depending on player's progress in my game I want to dynamically change spawning chances of enemies. On early levels it could be:

- zombie - 300

vampire - 150

goblin - 100

troll - 0

Later in the game it could be like this:

- zombie - 30

vampire - 60

goblin - 170

troll - 200

So how do I do this?

Currently I'm using an algorithm which is quite complex.

- Code: Select all
`I put all probabilities into an array:`

Array=[30,60,170,200]

Then I calculate the total:

TotalProbability=460

Then I generate a random number from this total:

R=Random(TotalProbability)

Then I iterate the array, searching which array member is "hit" by R:

For i=0 to 3

set x=x+Array.At(i)

if R<=x then // "i" is our monster type

Function.Call("SpawnMonster", i)

Graphically it looks like this:

The whole thing is a bit messy and complex, I need to iterate the array several times.

Is there a better solution? Maybe a formula or a built-in functionality or a plugin for this?