Goal: Create algorithm that intelligently spawns zombies based on their strength. Example, at Wave 1 most zombies should be strength 1 with a few strength 2, and at wave 5 most zombies should be strength 5 with some lower and some higher. like in Zombie Attack the zombies seamlessly become progressively stronger as the wave number goes up
Is there a standard type of algorithm that I can use? Meanwhile here is what I came up with. Tell me if it’s good or bad or needs any improvements.
Each Zombie has a property called Cost and each Wave has a property called Budget. Wave also has a property called TotalEnemies. Then I have a basic algorithm that spawns the right zombies by using up the entire Budget and making sure to spawn as many zombies as the TotalEnemies.
So my question is:
How do I make a basic algorithm that will do this?
Have int values called (I’ll call it Wave Strength for this example) and set it to 1, when a new zombie spawns it’s Strength Value will be equal to Wave Strength Value.
At the end of each Wave you simply increase Wave Strength by whatever number you want
I want all different level zombies to spawn in the same wave. Just like in Zombie Attack on Wave 1 only weak zombies spawn but by Wave 30 all different zombies spawn weak ones and strong ones.
If it’s wave 1 it looks something like this :
local WaveStrength = 1
local Zombie = your model
local NewZombie = Zombie:Clone()
NewZombie.Strength.Value = math.random(1,WaveStrength)
If it’s wave 30 then it looks like this :
local WaveStrength = 30
local Zombie = your model
local NewZombie = Zombie:Clone()
NewZombie.Strength.Value = math.random(1,WaveStrength)
You can create a weighted system or just a simple random range for that. You’d have the level as a variable and get zombie levels based on the level:
--// Random Range
local currentLevel = 1
local function getZombieLevel()
return math.random(
math.clamp(currentLevel - 1, 0, math.huge), -- make sure the minimum level doesn't go under 1
currentLevel + 1
)
end
The code above makes the zombies have a level of 1 level higher to 1 level lower than the current level.
But to make it so where you don’t have to put the script in every zombie do:
local strength = 0
local zombie = nil
if zombie == nil then
strength = math.random(1,5) -- random strength
zombie = script.Parent.Parent:WaitForChild("Zombie") -- gets the zombie
zombie.Strength = strength -- applies the variable "strength" to the zombie's strength
return -- returns
end
You’d need to either determine the exact number of Zombies with x amount of strength or create an algorithm that gives more weight to choosing stronger zombies when at higher wave numbers.
Here is an example:
local waveStrengths = {}
waveStrengths.Wave1 = {.3, .3, .2, .1, .1} --Total percentage must be 100% or 1
waveStrengths.Wave5 = {.1, .1, .2, .3, .3} --More stronger ones in wave 5
local totalZombies = 50
print("Zombies at strength 1: ", totalZombies * waveStrengths.Wave1[1]) --15 Zombies at strength lvl 1
print("Zombies at strength 2: ", totalZombies * waveStrengths.Wave1[2]) --15 Zombies at strength lvl 2
print("Zombies at strength 3: ", totalZombies * waveStrengths.Wave1[3]) --10 Zombies at strength lvl 3
print("Zombies at strength 4: ", totalZombies * waveStrengths.Wave1[4]) --5 Zombies at strength lvl 4
print("Zombies at strength 5: ", totalZombies * waveStrengths.Wave1[5]) --5 Zombies at strength lvl 5
Firstly, I’m not changing the strength of the zombie. I am selecting zombies with different strengths.
math.random(1,waveNumber) can work but it doesn’t ensure that a high wave will get strong zombies. It’s leaving it to chance, obviously there is more of a chance it will pick stronger zombies but still. That’s why I thought of making a budget that has to get used.
if you want to get non integer values use Random.new():NextNumber(min,max) it should work flawlessly,
This returns ANY number randomly and clamped from min to max including decimal numbers.
Okay… but you said you wanted an algorithm that would adjust zombie strengths depending on the wave. That’s exactly what this is doing. You choose the percentage of zombies with certain strengths depending on the wave. I don’t understand how this is not precisely what you asked for.
I don’t want to hardcode it per each wave because there is an indefinite number of waves. I want a system that will spawn stronger and stronger zombies each wave.
That’s why I thought of the budget. On wave 1 the budget will be 50, then the algorithm spawns zombies of varying costs but it must equal up to 50. Then on wave 2 the budget increases with a formula (for now I just have it, budget *= 1.1) then the algorithm spawns zombies of varying costs but it must equal up to the new budget (55 in this case) and so on.
It also has to be smart for example on wave 1 I want it to spawn 50 zombies that cost 1 (or 40 that cost 1 and 5 that cost 2) but I don’t want it to spawn 1 super strong zombie that costs 50. And on wave 50 I want it to spawn 100 zombies that cost 50 and not 99 zombies that cost 1 and 1 zombie that costs 500.
Also if I didn’t make it clear I make the stronger zombies “cost” more.