GetUnitCollisionSize

Scripts

13 years
edited 8 years
Gets a unit's collision size maybe?

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
Code (jass) Select
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 trigger
11
//* holding this code to the target map.
12
//*
13
//*********************************************************
14
15
//========================================================
16
library_once GetUnitCollisionSize
17
18
    globals
19
        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 it
23
                                                            //few maps should have collision sizes bigger than 300.0...
24
    endglobals
25
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
    loop
39
        set mid=(lo+hi)/2.0
40
        exitwhen (i==ITERATIONS)
41
        if (IsUnitInRangeXY(u,x+mid,y,0)) then
42
            set lo=mid
43
        else
44
            set hi=mid
45
        endif
46
        set i=i+1
47
    endloop
48
 return mid
49
endfunction
50
51
endlibrary
52


Table version
Code (jass) Select
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 trigger
12
//* holding this code to the target map.
13
//*
14
//*********************************************************
15
16
//=========================================================
17
    globals
18
        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 it
22
                                                            //few maps should have collision sizes bigger than 300.0...
23
24
        private Table memo
25
    endglobals
26
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
    endif
42
43
    set hi=MAX_COLLISION_SIZE
44
    set lo=0.0
45
    loop
46
        set mid=(lo+hi)/2.0
47
        exitwhen (i==ITERATIONS)
48
        if (IsUnitInRangeXY(u,x+mid,y,0)) then
49
            set lo=mid
50
        else
51
            set hi=mid
52
        endif
53
        set i=i+1
54
    endloop
55
    set memo[typ]=R2I(mid+0.500000001)
56
 return mid
57
endfunction
58
59
    private function init takes nothing returns nothing
60
     set memo=Table.create()
61
    endfunction
62
63
endlibrary
64
13 years
As always your spells/systems are well made and useful I'm gonna mass approve this all with my lovely 4/5 rating.
13 years
Moved from media so it can handle better the information.