View Single Post
Old 20 November 2021, 08:17   #12
Havie
Registered User
 
Havie's Avatar
 
Join Date: Mar 2012
Location: UK
Posts: 1,895
Quote:
Originally Posted by Master484 View Post
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.
Unless I have misunderstood - not sure this would work as if baddie A is closer than baddie B on the x axis but the opposite is true then initially A would be closer and then B would be considered closer?

I am interested in the concept of blitting at different times. At the moment I just have a blitting loop in my game (all my games) and blit everything over the same loop. Had never thought of blitting at different times! Not sure this would work for TR though as the Bobs are z-ordered to print from top to bottom so once I have z-ordered them the only thing left to do is Blit and I can do this out of sequence as I have to have updated them all first before z-ordering but this is something I will bare in mind for future games.

Thanks.
Havie is offline  
 
Page generated in 0.04351 seconds with 11 queries