@SimSmall -
I'd like to add something about short circuting. I believe it was IanM that showed this to me a long time ago...
Quote: "if a > 0 then if a < 99
`Do something
endif"
will short circuit the condition checks. If a is not greater than 0 then it won't do any other checks after the 'then'. You can string up as many as you like on one line and it remains nice and readable.
DBP's ABS function is slow. The code below demonstrates that - if test1 comes out faster than test2 or 3 then that means you have IanM's Matrix1 DLLs installed, and he's provided a replacement abs function that uses integers instead of floats. If you're using DBP's native version it will blow your mind how comparatively slow it is.
Something I think is somewhat interesting - with integers 0-number is marginally faster than -number, around 10 ms for 10,000,000 checks, but with floats 0.0-number# is noticeably faster than -number#. This is shown below as well.
Rem DBP Integer Check
time=timer()
for i=1 to 10000
for j=1 to 1000
a=abs(-10000)
next j
next i
time1=timer()-time
Rem Function 1 integer
time=timer()
for i=1 to 10000
for j=1 to 1000
a=absL(-100000)
next j
next i
time2=timer()-time
Rem Function 2 integer
time=timer()
for i=1 to 10000
for j=1 to 1000
a=absL2(-100000)
next j
next i
time3=timer()-time
time=timer()
for i=1 to 10000
for j=1 to 1000
a#=abs(-100000.0)
next j
next i
time4=timer()-time
time=timer()
for i=1 to 10000
for j=1 to 1000
a#=absF(-100000.0)
next j
next i
time5=timer()-time
time=timer()
for i=1 to 10000
for j=1 to 1000
a#=absF2(-100000.0)
next j
next i
time6=timer()-time
print "Time taken to perform 10,000,000 abs integer checks with DBP: ",time1
print "Time taken to perform 10,000,000 abs integer checks with function 0-number: ",time2
print "Time taken to perform 10,000,000 abs integer checks with function -number: ",time3
print
print "Time taken to perform 10,000,000 abs float checks with DBP: ",time4
print "Time taken to perform 10,000,000 abs float checks with function 0.0-number#: ",time5
print "Time taken to perform 10,000,000 abs float checks with function -number#: ",time6
wait key
end
function absL(number)
if number<0 then number=0-number
endfunction number
function absL2(number)
if number<0 then number=-number
endfunction number
function absF(number#)
if number#<0.0 then number#=0.0-number#
endfunction number#
function absF2(number#)
if number#<0.0 then number#=-number#
endfunction number#
Another thing that tends to plague code is slow distance checks. Distance checks need to be performed every loop and can bog a program down if too many are being done incorrectly.
One thing you should realize - use a 2D distance check whenever you can. For example, if you have two characters walking on an advanced terrain and you want to check the distance between them, a 2D check will almost certainly do the job and is faster than a 3D check.
Styx has a really fast distance function. If you don't have Styx then you should use vectors.
function getDistance2D(obj1 as integer,obj2 as integer)
rem "null=make vector2(2)" command must be run prior to calling function
x as float
z as float
result# as float
x=object position x(obj1)-object position x(obj2)
z=object position z(obj1)-object position z(obj2)
set vector2 2,x,z
result#=length vector2(2)
endfunction result#
function getDistance3D(obj1 as integer, obj2 as integer)
rem "null=make vector3(1)" command must be run prior to calling function
x as float
y as float
z as float
result# as float
x=object position x(obj1)-object position x(obj2)
y=object position y(obj1)-object position y(obj2)
z=object position z(obj1)-object position z(obj2)
set vector3 1,x,y,z
result#=length vector3(1)
endfunction result#
Now, here's the thing you should realize - most distance checks are comparisons. For instance, if
distance#<100.0 then 'whatever. You can cut out the middle step and just check the squared distance. These are the fastest, even faster than the Styx distance checks (not by much though)
function fastDistance3D(obj1,obj2,distanceCheck#)
x#=object position x(obj1)-object position x(obj2)
y#=object position y(obj1)-object position y(obj2)
z#=object position z(obj1)-object position z(obj2)
returnValue=(((x#*x#)+(y#*y#)+(z#*z#))<=(distanceCheck#*distanceCheck#))
endfunction returnValue
function fastDistance2D(obj1,obj2,distanceCheck#)
x#=object position x(obj1)-object position x(obj2)
z#=object position z(obj1)-object position z(obj2)
returnValue=(((x#*x#)+(z#*z#))<=(distanceCheck#*distanceCheck#))
endfunction returnValue
This will return a 1 if the objects are within the distanceCheck# range and 0 if they are not.
returnValue=(((x#*x#)+(z#*z#))<=(distanceCheck#*distanceCheck#)) is noticeably faster than
if (((x#*x#)+(z#*z#))<=(distanceCheck#*distanceCheck#)) then returnValue=1
And one final though about this - x#^2 is way slower than x#*x#. I haven't tested it with integers, but it's probably the same.
Come see the WIP!