Too many enemies

Archives Forums/BlitzPlus Programming/Too many enemies

Zster(Posted 2004) [#1]
Hi All

Making a platform game and I'm quite far down the line as I have a working map, main charater, score and mini map working. I have a map thats 180x180 tiles big (that wraps) and the viewable area is 15x10. Problem is the game has +-100 bad guys scattered around the map that but I only want to worry about the ones close by (ie move them, see if they fall off platforms etc.) but as it stands I have to go through all hundered every frame. Is there an effecient method I can use to cull the guys I'm checking on so that the bad guys outside the viewable area are skipped?

Thanks in advance.

BlitzSupport(Posted 2004) [#2]
Just check the positions of all of them against the viewable area -- checking 100 sets of x/y locations will be utterly trivial, speed-wise.

skn3(Posted 2004) [#3]
Best idea would be to do a enter / leave routine. Each enemy would have a flag for "active", and when the enemey enters the scene it is flagged as active, then you just scan all active enemeis for further ai interaction.

When the enemey leaves the area, unflag the active flag, and voila. You could change the dimensions of the flag zone, so that the enemeis start to interact past the screen. That way you avoid them suddenly coming to life onscreen.

Zster(Posted 2004) [#4]
Thanks for the ideas. Should have given a bit more detail as I'm already flaging the bad guys as active within a one and a half screen radius which cuts down on going through every sub instruction for each bad guy. I still thought looping through 100 or so variables every frame to see if they're in the screen might be a bit excessive especially as there's hardly likely to be more than 4 bad guys on the screen at once. I think I may create a muliti dimensioned array that cycles through a subset depending on which area of the map they're on (the bad guys are terratorial) just wanted to know best practise.

semar(Posted 2004) [#5]
You could check one - or a small amount of - bad guy per frame, instead of going through the entire collection each frame.

If you are using array, you can use an index which increments at each frame, and point one bad guy each frame.

If you are using types, you can use the After dedicated type command, to point to the next type element - it wraps also automatically.

If your game is running at ca. 60 frame per second - which ist the most common scenario - that means that all your 100 bad guys are updated in something more than a second, which is in my opinion enough for most of the cases.


cbmeeks(Posted 2004) [#6]
Here is what I do for my Metroid Classic....this is my routine for the Zoomers

; UpdateZoomers() -- Updates all alive zoomers
Function UpdateZoomers()

ZoomersOnScreen = 0

For z.ZoomerType = Each ZoomerType

;only process Zoomers that are on screen...just like the original
If ((z\x - MAPX) > -ZoomerWidth) And ((z\x - MAPX) < SCR_WIDTH) Then

;Zoomer Timer
z\ZoomerTimer = z\ZoomerTimer - 1

;draw them
Select z\state
ZoomersOnScreen = ZoomersOnScreen + 1
End Select

If ZoomerDebug = 1 Then
Color 255,255,0
Text z\x - MAPX, (z\y + ZoomerHeight + 5) - MAPY, "sx: " +( z\x - MAPX)
Text z\x - MAPX, (z\y + ZoomerHeight + 15) - MAPY, "sy: " +( z\y - MAPY)
Text z\x - MAPX, (z\y + ZoomerHeight + 25) - MAPY, "wx: " +( z\x )
Text z\x - MAPX, (z\y + ZoomerHeight + 35) - MAPY, "wy: " +( z\y )
Text z\x - MAPX, (z\y + ZoomerHeight + 45) - MAPY, "d: " + z\direction
Text z\x - MAPX, (z\y + ZoomerHeight + 55) - MAPY, "f: " + z\frame
End If
End If

If ZoomerDebug = 2 Then
Color 255,0,255
Text VIEWPORT_X,20,"Zoomers On Screen: " + ZoomersOnScreen
End If

End Function


Zster(Posted 2004) [#7]
Thanks for the help. I shall try various implementations. cbmeeks I thought you'd moved development of Metroid Classic to C++? Definitely looking forward to that one.

cbmeeks(Posted 2004) [#8]
Well, I go back and forth.

B+ is simply doing it so well. B+ is SOO fast for 2D it's not even funny.