Difference between revisions of "GetDistanceInTiles"
From UODemo Wiki
(Created page with 'integer '''getDistanceInTiles'''(location ''A'', location ''B'');<br> This function will return 9999 if A.x or B.x is greater or equal to 5120. Otherwise the absolute value of t…') |
m (Protected "GetDistanceInTiles" ([edit=sysop] (indefinite) [move=sysop] (indefinite))) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
integer '''getDistanceInTiles'''(location ''A'', location ''B'');<br> | integer '''getDistanceInTiles'''(location ''A'', location ''B'');<br> | ||
− | This function will | + | This function will calculate the absolute value of the difference between A and B (for both x and y) and the highest value of the two is returned. The Z value is ignored when calculating the difference. |
Pseudo-code (using signed integers):<pre> | Pseudo-code (using signed integers):<pre> | ||
− | integer | + | integer getDistanceInTiles(location A, location B) |
{ | { | ||
− | + | // NOTE TO SELF: I should explain this better when I'm not so sleepy | |
+ | Ax_is_invalid = A.x >= 5120 ? 1 : 0; | ||
+ | Bx_is_invalid = B.x >= 5120 ? 1 : 0; | ||
+ | if(Ax_is_invalid ^ Bx_is_invalid != 0) // Indeed, XOR is used here | ||
return 9999; | return 9999; | ||
Line 16: | Line 19: | ||
ydiff = -ydiff; | ydiff = -ydiff; | ||
− | if(A.x < 5120) | + | if(A.x < 5120) |
{ | { | ||
if(5120 - xdiff > xdiff) | if(5120 - xdiff > xdiff) |
Latest revision as of 22:44, 14 April 2010
integer getDistanceInTiles(location A, location B);
This function will calculate the absolute value of the difference between A and B (for both x and y) and the highest value of the two is returned. The Z value is ignored when calculating the difference.
Pseudo-code (using signed integers):integer getDistanceInTiles(location A, location B) { // NOTE TO SELF: I should explain this better when I'm not so sleepy Ax_is_invalid = A.x >= 5120 ? 1 : 0; Bx_is_invalid = B.x >= 5120 ? 1 : 0; if(Ax_is_invalid ^ Bx_is_invalid != 0) // Indeed, XOR is used here return 9999; Xdiff = A.x - B.x; ydiff = A.y - B.y; if(xdiff < 0) xdiff = -xdiff; if(ydiff < 0) ydiff = -ydiff; if(A.x < 5120) { if(5120 - xdiff > xdiff) xdiff = 5120 - xdiff; if(4096 - ydiff > ydiff) ydiff = 4096 - ydiff; } if(xdiff > ydiff) retval = xdiff; else retval = ydiff; return retval; }
Return to the Command List.