//Returns the angle from xy1 to xy2 in radians(Rad)/degrees(Deg)
//The returned value is between -180 and 180 for degrees and between -PI and PI for radians.
function AngleBetweenCoordinatesRad takes real x1, real y1, real x2, real y2 returns real
return Atan2(y2 - y1, x2 - x1)
endfunction
function AngleBetweenCoordinatesDeg takes real x1, real y1, real x2, real y2 returns real
return AngleBetweenCoordinatesRad(x1, y1, x2, y2) * RADTODEG
endfunction
//Returns a normalized angle from the given angle.
//Normalized angles are between 0 and 360 for degrees and 0 and TAU for radians.
function NormalizeAngleRad takes real angle returns real
local real modulus = angle - I2R(R2I(angle / TAU)) * TAU
if modulus < 0 then
set modulus = modulus + TAU
endif
return modulus
endfunction
function NormalizeAngleDeg takes real angle returns real
local real modulus = angle - I2R(R2I(angle / 360)) * 360
if modulus < 0 then
set modulus = modulus + 360
endif
return modulus
endfunction
//Deprecated function pointing at "NormalizeAngleDeg()" for backwards compatibility.
//Returns a denormalized angle from the given angle.
//Denomalized angles are between -180 and 180 for degrees and -PI and PI for radians.
function DenormalizeAngleRad takes real angle returns real
return NormalizeAngleRad(angle) - PI
endfunction
function DenormalizeAngleDeg takes real angle returns real
return NormalizeAngleDeg(angle) - 180
endfunction
//Groups all units in a cone facing "direction" degrees from position ("x", "y").
//"radius" is the length of the cone. "size" is the maximum difference in angle in radians(Rad)/degrees(Deg) from ("x", "y") towards the unit.
//Returns "whichGroup" for easy usage: "local group g = GroupEnumUnitsInCone(CreateGroup(), x, y, r, d, s)".
function GroupEnumUnitsInConeRad takes group whichGroup, real x, real y, real radius, real direction, real size returns group
local unit FoG
local group g = CreateGroup()
local real x2
local real y2
local real difference
call GroupEnumUnitsInRange(g, x, y, radius, null)
loop
set FoG = FirstOfGroup(g)
exitwhen FoG == null
call GroupRemoveUnit(g,FoG)
set difference = RAbsBJ(DenormalizeAngleRad(AngleBetweenCoordinatesRad(x, y, GetUnitX(FoG), GetUnitY(FoG)) - direction))
if difference > -size and difference < size then
call GroupAddUnit(whichGroup, FoG)
endif
endloop
call DestroyGroup(g)
set g = null
return whichGroup
endfunction
function GroupEnumUnitsInConeDeg takes group whichGroup, real x, real y, real radius, real direction, real size returns group
local unit FoG
local group g = CreateGroup()
local real x2
local real y2
local real difference
call GroupEnumUnitsInRange(g, x, y, radius, null)
loop
set FoG = FirstOfGroup(g)
exitwhen FoG == null
call GroupRemoveUnit(g,FoG)
set difference = DenormalizeAngleDeg(AngleBetweenCoordinatesDeg(x, y, GetUnitX(FoG), GetUnitY(FoG)) - direction)
if difference > -size and difference < size then
call GroupAddUnit(whichGroup, FoG)
endif
endloop
call DestroyGroup(g)
set g = null
return whichGroup
endfunction