13 years
edited 8 years
Gets a unit's collision size maybe?
Information:
Standalone
Table version
Information:
- ITERATIONS : This determines how many iterations it should use (it uses a binary search, 10 is fine, actually, 5 is fine as well, I don't think you need this to be ultra precise, but if you want, go ahead and have 30 iterations.
- Flavors: It comes in three flavors, I figured some people may not like the idea of doing the iterations so much freaking times when collision size is constant per unit type,
so using gamecache to memorize the values is possible, so one of the alternative versions uses CSSafeCache, while the other one uses Table. Please take note that I am not that sure gamecache is faster than the little 10 iterations this function does.
Standalone
1 2 //********************************************************3 //* GetUnitCollisionSize (Standalone version)4 //* --------------------5 //* If you need it, use it.6 //*7 //* To implement it just create a custom text 'trigger'8 //* called GetUnitCollisionSize, and paste this there.9 //*10 //* To copy from one map to another just copy the trigger11 //* holding this code to the target map.12 //*13 //*********************************************************14 15 //========================================================16 library_once GetUnitCollisionSize17 18 globals19 private constant integer ITERATIONS = 10 //too much, slow, too short "innacurate", let it be bigger than 0...
20 //I *think* 10 is enough...21 public constant real MAX_COLLISION_SIZE = 300.0 //should be THE max collision size in the map,
22 //well, not really, just make sure it is greater than it23 //few maps should have collision sizes bigger than 300.0...24 endglobals25 26 //========================================================27 function GetUnitCollisionSize takes unit u returns real
28 local integer i=0
29 local real x=GetUnitX(u)
30 local real y=GetUnitY(u)
31 local real hi
32 local real lo
33 local real mid
34 35 36 set hi=MAX_COLLISION_SIZE
37 set lo=0.0
38 loop39 set mid=(lo+hi)/2.0
40 exitwhen (i==ITERATIONS)
41 if (IsUnitInRangeXY(u,x+mid,y,0)) then
42 set lo=mid
43 else44 set hi=mid
45 endif46 set i=i+1
47 endloop48 return mid49 endfunction50 51 endlibrary52
Table version
1 2 library_once GetUnitCollisionSize initializer init requires Table
3 //********************************************************4 //* GetUnitCollisionSize (Table version)5 //* --------------------6 //* If you need it, use it.7 //*8 //* To implement it just create a custom text 'trigger'9 //* called GetUnitCollisionSize, and paste this there.10 //*11 //* To copy from one map to another just copy the trigger12 //* holding this code to the target map.13 //*14 //*********************************************************15 16 //=========================================================17 globals18 private constant integer ITERATIONS = 10 //too much, slow, too short "innacurate", let it be bigger than 0...
19 //I *think* 10 is enough...20 public constant real MAX_COLLISION_SIZE = 300.0 //should be THE max collision size in the map,
21 //well, not really, just make sure it is greater than it22 //few maps should have collision sizes bigger than 300.0...23 24 private Table memo25 endglobals26 27 28 29 //=============================================================================30 function GetUnitCollisionSize takes unit u returns real
31 local integer i=0
32 local real x=GetUnitX(u)
33 local real y=GetUnitY(u)
34 local integer typ=GetUnitTypeId(u)
35 local real hi
36 local real lo
37 local real mid
38 39 if (memo.exists(typ) ) then
40 return I2R(memo[typ])
41 endif42 43 set hi=MAX_COLLISION_SIZE
44 set lo=0.0
45 loop46 set mid=(lo+hi)/2.0
47 exitwhen (i==ITERATIONS)
48 if (IsUnitInRangeXY(u,x+mid,y,0)) then
49 set lo=mid
50 else51 set hi=mid
52 endif53 set i=i+1
54 endloop55 set memo[typ]=R2I(mid+0.500000001)
56 return mid57 endfunction58 59 private function init takes nothing returns nothing
60 set memo=Table.create()
61 endfunction62 63 endlibrary64