- Joined
- Apr 3, 2010
- Messages
- 1,889
Worked on from Vexorian's system, GetunitCollisionSize.
His system was off by decimal places and I need it to be accurate to the collision size value that the user gave the unit. The way I came up with the exact value the unit has with collision size is kinda dumb, but from my testing it works without fault.
I also created a new GetUnitTypeIdCollisionSize function.
Versions:
- Standalone (calculates new value even when unit type has been calculated before).
- Table (calculates new value once for every unit type has been calculated before).
There is no Cache version as I don't know where I would get the CSSafeCache (or another Cache) library from.
//--
His system was off by decimal places and I need it to be accurate to the collision size value that the user gave the unit. The way I came up with the exact value the unit has with collision size is kinda dumb, but from my testing it works without fault.
I also created a new GetUnitTypeIdCollisionSize function.
Versions:
- Standalone (calculates new value even when unit type has been calculated before).
- Table (calculates new value once for every unit type has been calculated before).
There is no Cache version as I don't know where I would get the CSSafeCache (or another Cache) library from.
JASS:
library_once GetUnitCollisionSize
//********************************************************
//* GetUnitCollisionSize (Standalone version, exact)
//* --------------------
//* If you need it, use it.
//*
//* Originally by Vexorian,
//* edited by SA Dashie for 100% accuracy
//*
//* Gives out the correct number everytime from
//* 1.00 collision size to 1000.00
//*
//*
//* 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.
//*
//*
//* API:
//*
//* function GetUnitCollisionSize takes unit u returns real
//*
//* function GetUnitTypeIdCollisionSize takes integer i returns real
//*
//*********************************************************
//========================================================
globals
private constant integer ITERATIONS = 27 //DO NOT TOUCH; have not tested, although it may throw out the wrong number
//if the ITERATIONS variable is changed...
//too much, slow, too short "innacurate", let it be bigger than 0...
//27 iterations will always return the right value...
public constant real MAX_COLLISION_SIZE = 1000.0//DO NOT TOUCH; have not tested, although it may throw out the wrong number
//if the MAX_COLLISION_SIZE variable is changed...
//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 real array correction //starting from highest, if collision value is less than indexed
//value then the collision value get's the index*0.01 taken away
//from the value and corrected to only have 2 decimal places...
private constant integer CORRECTION_COUNT = 42 //how many indexes the correction array has...
endglobals
private module initCorrection
private static method onInit takes nothing returns nothing
set correction[1]=55.85
set correction[2]=26.66
set correction[3]=17.50
set correction[4]=13.06
set correction[5]=10.39
set correction[6]=8.66
set correction[7]=7.43
set correction[8]=6.50
set correction[9]=5.79
set correction[10]=5.20
set correction[11]=4.75
set correction[12]=4.35
set correction[13]=4.02
set correction[14]=3.75
set correction[15]=3.50
set correction[16]=3.29
set correction[17]=3.10
set correction[18]=2.94
set correction[19]=2.79
set correction[20]=2.66
set correction[21]=2.55
set correction[22]=2.44
set correction[23]=2.35
set correction[24]=2.26
set correction[25]=2.17
set correction[26]=2.10
set correction[27]=2.031
set correction[28]=1.971
set correction[29]=1.911
set correction[30]=1.861
set correction[31]=1.811
set correction[32]=1.761
set correction[33]=1.721
set correction[34]=1.672
set correction[35]=1.641
set correction[36]=1.602
set correction[37]=1.571
set correction[38]=1.541
set correction[39]=1.511
set correction[40]=1.481
set correction[41]=1.452
set correction[42]=1.431
endmethod
endmodule
private struct inits extends array
implement initCorrection
endstruct
//========================================================
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
local string convert
// At about 27 loops it is accurate enough to go to the next stage
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.0)) then
set lo=mid
else
set hi=mid
endif
set i=i+1
endloop
// The smaller the collision size, the bigger the added decimal places there are
// Let's fix that and remove those extra decimal places to make it 100% accurate to the original collision size (to 2 decimal places)
set i=CORRECTION_COUNT
loop
if (mid<correction[i]) then
set mid=mid-i*0.01
exitwhen true
endif
exitwhen (i==1)
set i=i-1
endloop
set convert=R2S(mid)
set mid=S2R(SubString(convert,0,StringLength(convert)-1))
return mid
endfunction
function GetUnitTypeIdCollisionSize takes integer i returns real
local unit u=CreateUnit(Player(14),i,0,0,0)
local real mid=GetUnitCollisionSize(u)
call RemoveUnit(u)
set u=null
return mid
endfunction
endlibrary
JASS:
library_once GetUnitCollisionSize initializer init requires Table // http://www.hiveworkshop.com/threads/snippet-new-table.188084/
//********************************************************
//* GetUnitCollisionSize (Table version, exact)
//* --------------------
//* If you need it, use it.
//*
//* Originally by Vexorian,
//* edited by SA Dashie for 100% accuracy
//*
//* Gives out the correct number everytime from
//* 1.00 collision size to 1000.00
//*
//*
//* 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.
//*
//*
//* API:
//*
//* function GetUnitCollisionSize takes unit u returns real
//*
//* function GetUnitTypeIdCollisionSize takes integer i returns real
//*
//*********************************************************
//========================================================
globals
private constant integer ITERATIONS = 27 //DO NOT TOUCH; have not tested, although it may throw out the wrong number
//if the ITERATIONS variable is changed...
//too much, slow, too short "innacurate", let it be bigger than 0...
//27 iterations will always return the right value...
public constant real MAX_COLLISION_SIZE = 1000.0//DO NOT TOUCH; have not tested, although it may throw out the wrong number
//if the MAX_COLLISION_SIZE variable is changed...
//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 real array correction //starting from highest, if collision value is less than indexed
//value then the collision value get's the index*0.01 taken away
//from the value and corrected to only have 2 decimal places...
private constant integer CORRECTION_COUNT = 42 //how many indexes the correction array has...
private Table memo
endglobals
private module initCorrection
private static method onInit takes nothing returns nothing
set correction[1]=55.85
set correction[2]=26.66
set correction[3]=17.50
set correction[4]=13.06
set correction[5]=10.39
set correction[6]=8.66
set correction[7]=7.43
set correction[8]=6.50
set correction[9]=5.79
set correction[10]=5.20
set correction[11]=4.75
set correction[12]=4.35
set correction[13]=4.02
set correction[14]=3.75
set correction[15]=3.50
set correction[16]=3.29
set correction[17]=3.10
set correction[18]=2.94
set correction[19]=2.79
set correction[20]=2.66
set correction[21]=2.55
set correction[22]=2.44
set correction[23]=2.35
set correction[24]=2.26
set correction[25]=2.17
set correction[26]=2.10
set correction[27]=2.031
set correction[28]=1.971
set correction[29]=1.911
set correction[30]=1.861
set correction[31]=1.811
set correction[32]=1.761
set correction[33]=1.721
set correction[34]=1.672
set correction[35]=1.641
set correction[36]=1.602
set correction[37]=1.571
set correction[38]=1.541
set correction[39]=1.511
set correction[40]=1.481
set correction[41]=1.452
set correction[42]=1.431
endmethod
endmodule
private struct inits extends array
implement initCorrection
endstruct
//=========================================================
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
local string convert
// At about 27 loops it is accurate enough to go to the next stage
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.0)) then
set lo=mid
else
set hi=mid
endif
set i=i+1
endloop
// The smaller the collision size, the bigger the added decimal places there are
// Let's fix that and remove those extra decimal places to make it 100% accurate to the original collision size (to 2 decimal places)
set i=CORRECTION_COUNT
loop
if (mid<correction[i]) then
set mid=mid-i*0.01
exitwhen true
endif
exitwhen (i==1)
set i=i-1
endloop
set convert=R2S(mid)
set mid=S2R(SubString(convert,0,StringLength(convert)-1))
set memo[typ]=mid
return mid
endfunction
function GetUnitTypeIdCollisionSize takes integer i returns real
local unit u=CreateUnit(Player(14),i,0,0,0)
local real mid=GetUnitCollisionSize(u)
call RemoveUnit(u)
set u=null
return mid
endfunction
private function init takes nothing returns nothing
set memo=Table.create()
endfunction
endlibrary
//--
JASS:
library_once GetUnitCollisionSize
//********************************************************
//* GetUnitCollisionSize (Standalone version, exact)
//* --------------------
//* If you need it, use it.
//*
//* Originally by Vexorian,
//* edited by SA Dashie for 100% accuracy
//*
//*
//* 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.
//*
//*
//* API:
//*
//* function GetUnitCollisionSize takes unit u returns real
//*
//* function GetUnitTypeIdCollisionSize takes integer i returns real
//*
//*********************************************************
//========================================================
globals
private constant integer ITERATIONS = 27 //too much, slow, too short "innacurate", let it be bigger than 0...
//27 iterations will always return the right value...
public constant real MAX_COLLISION_SIZE = 1000.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 real array correction //starting from highest, if collision value is less than indexed
//value then the collision value get's the index*0.01 taken away
//from the value and corrected to only have 2 decimal places...
endglobals
private module initCorrection
private static method onInit takes nothing returns nothing
set correction[1]=53.97
set correction[2]=26.31
set correction[3]=17.34
set correction[4]=12.95
set correction[5]=10.32
set correction[6]=8.58
set correction[7]=7.35
set correction[8]=6.41
set correction[9]=5.69
set correction[10]=5.10
set correction[11]=4.63
set correction[12]=4.23
set correction[13]=3.90
set correction[14]=3.61
set correction[15]=3.35
set correction[16]=3.14
set correction[17]=2.94
set correction[18]=2.77
set correction[19]=2.61
set correction[20]=2.47
set correction[21]=2.34
set correction[22]=2.23
set correction[23]=2.12
set correction[24]=2.02
set correction[25]=1.93
set correction[26]=1.85
set correction[27]=1.77
set correction[28]=1.70
endmethod
endmodule
private struct inits extends array
implement initCorrection
endstruct
//========================================================
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
local string convert
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.0)) then
set lo=mid
else
set hi=mid
endif
set i=i+1
endloop
if (mid<correction[28]) then
set mid=1.42
else
set i=27
loop
if (mid<correction[i]) then
set mid=mid-i*0.01
exitwhen true
endif
exitwhen (i==1)
set i=i-1
endloop
set convert=R2S(mid)
set mid=S2R(SubString(convert,0,StringLength(convert)-1))
endif
return mid
endfunction
function GetUnitTypeIdCollisionSize takes integer i returns real
local unit u=CreateUnit(Player(14),i,0,0,0)
local real mid=GetUnitCollisionSize(u)
call RemoveUnit(u)
set u=null
return mid
endfunction
endlibrary
JASS:
library_once GetUnitCollisionSize initializer init requires Table // http://www.hiveworkshop.com/threads/snippet-new-table.188084/
//********************************************************
//* GetUnitCollisionSize (Table version, exact)
//* --------------------
//* If you need it, use it.
//*
//* Originally by Vexorian,
//* edited by SA Dashie for 100% accuracy
//*
//*
//* 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.
//*
//*
//* API:
//*
//* function GetUnitCollisionSize takes unit u returns real
//*
//* function GetUnitTypeIdCollisionSize takes integer i returns real
//*
//*********************************************************
//=========================================================
globals
private constant integer ITERATIONS = 27 //too much, slow, too short "innacurate", let it be bigger than 0...
//27 iterations will always return the right value...
public constant real MAX_COLLISION_SIZE = 1000.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 real array correction //starting from highest, if collision value is less than indexed
//value then the collision value get's the index*0.01 taken away
//from the value and corrected to only have 2 decimal places...
private Table memo
endglobals
private module initCorrection
private static method onInit takes nothing returns nothing
set correction[1]=53.97
set correction[2]=26.31
set correction[3]=17.34
set correction[4]=12.95
set correction[5]=10.32
set correction[6]=8.58
set correction[7]=7.35
set correction[8]=6.41
set correction[9]=5.69
set correction[10]=5.10
set correction[11]=4.63
set correction[12]=4.23
set correction[13]=3.90
set correction[14]=3.61
set correction[15]=3.35
set correction[16]=3.14
set correction[17]=2.94
set correction[18]=2.77
set correction[19]=2.61
set correction[20]=2.47
set correction[21]=2.34
set correction[22]=2.23
set correction[23]=2.12
set correction[24]=2.02
set correction[25]=1.93
set correction[26]=1.85
set correction[27]=1.77
set correction[28]=1.70
endmethod
endmodule
private struct inits extends array
implement initCorrection
endstruct
//=========================================================
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
local string convert
if (memo.has(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.0)) then
set lo=mid
else
set hi=mid
endif
set i=i+1
endloop
if (mid<correction[28]) then
set mid=1.42
else
set i=27
loop
if (mid<correction[i]) then
set mid=mid-i*0.01
exitwhen true
endif
exitwhen (i==1)
set i=i-1
endloop
set convert=R2S(mid)
set mid=S2R(SubString(convert,0,StringLength(convert)-1))
endif
set memo[typ]=R2I(mid+0.500000001)
return mid
endfunction
function GetUnitTypeIdCollisionSize takes integer i returns real
local unit u=CreateUnit(Player(14),i,0,0,0)
local real mid=GetUnitCollisionSize(u)
call RemoveUnit(u)
set u=null
return mid
endfunction
private function init takes nothing returns nothing
set memo=Table.create()
endfunction
endlibrary
Last edited: