View Single Post
Old 17 November 2021, 17:53   #8
Master484
Registered User
 
Master484's Avatar
 
Join Date: Nov 2015
Location: Vaasa, Finland
Posts: 525
I looked at my old notes, and reading a value from a single dimension array is indeed faster than doing a single multiply.

But reading a value from a two dimensional array ( which looks like: MyArray (X,Y) ), is slower than multiply or even a division.

So if you make a LUT, then a single dimensional array is recommended.

And there is of course that old Basic trick to make single dimensional arrays behave like two dimensional arrays. So for example if you need store values for a 160*100 grid then simply make a single dimensional array with 16000 elements in it, and then use -1 and +1 to move in the X dimension and -100 and +100 to move in the Y dimension.

---

Also if I was doing this sort of "find the closest enemy" routine, then most likely I would do something like this:

Before the enemy collection is processed I would first reset these variables:
Code:
CurrentClosestXY = 999
TheClosestEnemyID = 999
And then, as the enemy collection is processed, I would have something like this:

Code:
Count distanceX between ThisEnemy X and player X.

If distanceX is smaller than CurrentClosestXY
 CurrentClosestXY = distanceX
 TheClosestEnemyID = ThisEnemy
End if

Count distanceY between ThisEnemy Y and player Y.

If distanceY is smaller than CurrentClosestXY
 CurrentClosestXY = distanceY
 TheClosestEnemyID = ThisEnemy
End if
And so, after all enemies have been processed, you'll know the ID number of the closest enemy. No need to sort enemies in a list or something like that.

And of course those distance values could be useful for other purposes too, like when should enemies make their attack moves, and so on. So to optimize things all of that should be integrated with general enemy AI behaviour.

And also, if possible, integrate enemy blitting commands into the enemy collection browsing, so that the code follows a cycle like this:

handle enemy 1 logic
Draw enemy 1
handle enemy 2 logic
Draw enemy 2
handle enemy 3 logic
Draw enemy 3

This way the Blitter is constantly drawing the enemies in the background, while the CPU is calculating that distance stuff and AI logic. This is one of the biggest speed tricks in Blitz.
Master484 is offline  
 
Page generated in 0.04331 seconds with 11 queries