I'm having trouble making a fair algorithm that figures out how many enemies should spawn in an endless wave shooter game

Hello!

I’m trying to make a wave survival game and I want the waves to be endless so I’m having trouble figuring out a fair algorithm that calculates how many enemies to spawn in each wave

I want the number of players to affect it (fewer players = fewer enemies) and maybe (not necessarily) a difficulty value

any suggestions?

It really depends on how tough the enemy NPC are compared to the average player.

I’d recommend starting off with maybe 10 NPC for one player in wave one. Increasing by, maybe, 5-10% each wave. Then an additional 5% with each extra player.

A way to figure out how many NPC should spawn might be:

BaseNPCValue = 10
WaveMultiplier = 1
PlayerNumber = (1 + Players * 0.05) - 0.05
-- minus 0.05 from PlayerNumber to ensure it is only extra players that add 0.05
SpawnNPCValue = 10

-- Each wave end
WaveNumber = WaveNumber + 0.1
SpawnNPCValue = BaseNPCValue * WaveMultiplier * PlayerNumber

Next you just need to figure out if you want to round up or down.

BaseNPCValue = 10
WaveMultiplier = 1
PlayerNumber = (1 + 5 * 0.05) - 0.05
SpawnNPCValue = 10

WaveNumber = WaveNumber + 1
SpawnNPCValue = BaseNPCValue * WaveMultiplier * PlayerNumber

SpawnNPCValue = (SpawnNPCValue + 0.5) - (SpawnNPCValue + 0.5) % 1
print(SpawnNPCValue)

Hope this helps! IF you are not happy with the number of NPC being spawned, feel free to adjust the numbers in the variables!

Yea but now the wave number doesn’t affect the end result, it’s always 10 enemies and I’m not sure what to edit…

I tried looping through it

WaveNumber = 1
for I = 1, 20 do
	BaseNPCValue = 10
	WaveMultiplier = 1
	PlayerNumber = (1 + 1 * 0.05) - 0.05
	SpawnNPCValue = 10
	
	WaveNumber = WaveNumber + 1
	SpawnNPCValue = BaseNPCValue * WaveMultiplier * PlayerNumber
	
	SpawnNPCValue = (SpawnNPCValue + 0.5) - (SpawnNPCValue + 0.5) % 1
	print(SpawnNPCValue)
end

and the result in the output is

  10 (x20)

If you just want 10 enemies you can change damage and or health using the same concept as mentioned above.

Edit: You didn’t have the code posted and therefore this was taken out of context, still a good idea to think about though!

OK. This should fix it. :stuck_out_tongue:

BaseNPCValue = 10
WaveMultiplier = 1
PlayerNumber = (1 + 5 * 0.05) - 0.05
SpawnNPCValue = 10

WaveMultiplier = WaveMultiplier + 0.1
SpawnNPCValue = BaseNPCValue * WaveMultiplier * PlayerNumber

SpawnNPCValue = (SpawnNPCValue + 0.5) - (SpawnNPCValue + 0.5) % 1
print(SpawnNPCValue)
2 Likes

To explain the main part of the code:

The ‘SpawnNPCValue =’ line is just multiplying the base number by the extra players (0.05 for each extra player) and the wave (0.1 for each wave past one).

1 Like