How do you control spawn frequency of number

I have the next problem

I had a function to spawn four type of enemies, and i want to control the frequency they spawn in the game.

For example, if i have 5 enemies of type 1 and 0 enemies of type 2, i want to spawn more frequently type 2 than type 1.

But after this, when i had 5 enemies of type 1 and 7 enemies of type 2, i will to return the normal frequency to type 1.

So, for example, i had 70% of type 1, 20% of type 2 and 0% of type 3, i want to assign a little frequency to type 1, a big frequency to type 2 and more bigger on type 3.

How can i achieve this in code?

[import]uid: 55808 topic_id: 16633 reply_id: 316633[/import]

Imagine we have four different types of enemies and you want them to show with the following frequencies:

type 1: 10%
type 2: 15%
type 3: 25%
type 4: 50%

This can be achievedd with the following code:

[lua]local enemyType1Freq = 10
local enemyType2Freq = 15
local enemyType3Freq = 25
local enemyType4Freq = 50

local function getRandomEnemyType ( )
local percentage = math.random( 1, 100 )

if percentage <= enemyType1Freq then
return 1;
elseif percentage <= enemyType1Freq + enemyType2Freq then
return 2;
elseif percentage <= enemyType1Freq + enemyType2Freq + enemyType3Freq then
return 3;
elseif percentage <= enemyType1Freq + enemyType2Freq + enemyType3Freq + enemyType4Freq then
return 4;
end
end

local enemyCount = { 0, 0, 0, 0 }
for i = 1, 500 do
local enemyType = getRandomEnemyType()
print (‘enemyType’, enemyType)

enemyCount[enemyType] = enemyCount[enemyType] + 1
end

print (‘enemyCount type 1’, enemyCount[1])
print (‘enemyCount type 2’, enemyCount[2])
print (‘enemyCount type 3’, enemyCount[3])
print (‘enemyCount type 4’, enemyCount[4])[/lua]

Hope it helps.

Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16633 reply_id: 62148[/import]

Thanks Raul, but your code is exactly the code i had now in my game. A table with desired percentages in game.

The problem is not do a percentage random spawn, the goal is a dynamic percentage random spawn.

A sort of SpawnManager who controls the actual percentage of enemys in screen and change frequency dinamically.

For example, i had 5 enemyType1 in screen, 2 enemyType2 in screen, 2 enemyType3 in screen and 1 enemyType4 in screen.

So i had:

enemyType1 = 50%
enemyType2 = 20%
enemyType3 = 20%
enemyType4 = 10%

So i want a code that looks every spawn time at screen (i had now enemy count and a spawn population number) and change dynamically the frequency of the enemy if the enemyType is populating too much the screen.

In this example, i want the enemyType1 to spawn at 20% frequency, but if enemyType2 goes crazy, i want to change this frequency "in-game"

How can i do this in code?

[import]uid: 55808 topic_id: 16633 reply_id: 62277[/import]

I am not sure if I understood correctly the problem, but I think what you need is to simply re-calculate the desired percentages of frequency and decide how often you want to do it.

This re-calculation could be done right before each new spawn or maybe invoke it every few seconds:

[lua]-- initial values
local enemyType1Freq = 10
local enemyType2Freq = 15
local enemyType3Freq = 25
local enemyType4Freq = 50

local function recalculateSpawnFrequencies()
– very rough calculation of new spawn frequency
local function getNewSpawnFrequency( typeCount, totalCount )
local currentPercentage = typeCount * 100 / totalCount
return 100 - currentPercentage
end

– recalculate percentages
enemyType1Freq = getNewSpawnFrequency( enemyType1Count, totalEnemyCount )
enemyType2Freq = getNewSpawnFrequency( enemyType2Count, totalEnemyCount )
enemyType3Freq = getNewSpawnFrequency( enemyType3Count, totalEnemyCount )
enemyType4Freq = getNewSpawnFrequency( enemyType4Count, totalEnemyCount )
end

– recalculate frequencies every 5 seconds
timer.performWithDelay(5000, recalculateSpawnFrequencies, 0)[/lua]

I assume you already have the values of totalEnemyCount and every enemyTypeXCount
Raúl Beltrán
MIU Games [import]uid: 44101 topic_id: 16633 reply_id: 62452[/import]

Maybe this will help you or other members:
####main.lua####
[lua]local found
local min
–Here I define a table
–You can use what value you want for you
local t={3, -200, -91, 7, 4}

–the main function
function main()

–with this function you will found the elemeen that you want to increase his value
local function increaseMe()

–first we compose a basic function that find the minimun value between 2 parameters a, b
–we use this function later
local function minim(a, b)

if a **return a
else
return b
end

end

–we create a function here that look in a generic table to find the minimum element in it
–we use in it the minim function
local function foundMin(table1)
for i=1,#table1 do
found=table1[1]
for j=2,#table1 do
found=minim(found,table1[j])
end
return found
end
end

–here we apply this function to our table an print the minimum value from it
min=foundMin(t)

–this is the final result of the function increaseMe
print(“The min value is :”…min)

end

–we load this function
increaseMe()

–after you found the minimum value , you look in the defined table to this value
for i=1,#t do
if min==t[i] then

–Here I suppose is what you are looking for
print (“You must increase the value of t[”…i…"]= “…t[i]…” with 1 unit")
end
end

end

main()[/lua] [import]uid: 65047 topic_id: 16633 reply_id: 62478[/import]**