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

//********************************************************
//* GetUnitCollisionSize (Standalone version)
//* --------------------
//*   If you need it, use it.
//*
//* To implement it just create a custom text 'trigger'
//* called GetUnitCollisionSize, and paste this there.
//*
//* To copy from one map to another just copy the trigger
//* holding this code to the target map.
//*
//*********************************************************

//========================================================
library_once GetUnitCollisionSize

    globals
        private constant integer ITERATIONS         = 10    //too much, slow, too short "innacurate", let it be bigger than 0...
                                                            //I *think* 10 is enough...
        public  constant real    MAX_COLLISION_SIZE = 300.0 //should be THE max collision size in the map,
                                                            //well, not really, just make sure it is greater than it
                                                            //few maps should have collision sizes bigger than 300.0...
    endglobals

//========================================================
function GetUnitCollisionSize takes unit u returns real
local integer i=0
local real x=GetUnitX(u)
local real y=GetUnitY(u)
local real hi
local real lo
local real mid


    set hi=MAX_COLLISION_SIZE
    set lo=0.0
    loop
        set mid=(lo+hi)/2.0
        exitwhen (i==ITERATIONS)
        if (IsUnitInRangeXY(u,x+mid,y,0)) then
            set lo=mid
        else
            set hi=mid
        endif
        set i=i+1
    endloop
return mid
endfunction

endlibrary


Table version
Code (jass) Select

library_once GetUnitCollisionSize initializer init requires Table
//********************************************************
//* GetUnitCollisionSize (Table version)
//* --------------------
//*   If you need it, use it.
//*
//* To implement it just create a custom text 'trigger'
//* called GetUnitCollisionSize, and paste this there.
//*
//* To copy from one map to another just copy the trigger
//* holding this code to the target map.
//*
//*********************************************************

//=========================================================
    globals
        private constant integer ITERATIONS         = 10    //too much, slow, too short "innacurate", let it be bigger than 0...
                                                            //I *think* 10 is enough...
        public  constant real    MAX_COLLISION_SIZE = 300.0 //should be THE max collision size in the map,
                                                            //well, not really, just make sure it is greater than it
                                                            //few maps should have collision sizes bigger than 300.0...

        private Table memo
    endglobals



//=============================================================================
function GetUnitCollisionSize takes unit u returns real
local integer i=0
local real x=GetUnitX(u)
local real y=GetUnitY(u)
local integer typ=GetUnitTypeId(u)
local real hi
local real lo
local real mid

    if (memo.exists(typ) ) then
        return I2R(memo[typ])
    endif

    set hi=MAX_COLLISION_SIZE
    set lo=0.0
    loop
        set mid=(lo+hi)/2.0
        exitwhen (i==ITERATIONS)
        if (IsUnitInRangeXY(u,x+mid,y,0)) then
            set lo=mid
        else
            set hi=mid
        endif
        set i=i+1
    endloop
    set memo[typ]=R2I(mid+0.500000001)
return mid
endfunction

    private function init takes nothing returns nothing
     set memo=Table.create()
    endfunction

endlibrary
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.