library Util
globals
real PI = 3.14159274
real DEG2RAD = PI / 180
real RAD2DEG = 180 / PI
real X = 0 // Used to mimic returning two reals at once
real Y = 0 // Used to mimic returning two reals at once
effect SFX = null // Helps avoid a memory leak
endglobals
// Angle between points
function AngleBetweenXY takes real x1, real y1, real x2, real y2 returns real
return 180 + (RAD2DEG * Atan2(y1 - y2, x1 - x2))
endfunction
function AngleBetweenXYRad takes real x1, real y1, real x2, real y2 returns real
return PI + Atan2(y1 - y2, x1 - x2)
endfunction
function AngleBetweenUXY takes unit u, real x2, real y2 returns real
local real x1 = GetUnitX(u)
local real y1 = GetUnitY(u)
return 180 + (RAD2DEG * Atan2(y1 - y2, x1 - x2))
endfunction
function AngleBetweenUXYRad takes unit u, real x2, real y2 returns real
local real x1 = GetUnitX(u)
local real y1 = GetUnitY(u)
return PI + Atan2(y1 - y2, x1 - x2)
endfunction
// Polar offset
function GetOffsetPosition takes real x, real y, real offset, real degrees returns nothing
set X = x + (offset * Cos(DEG2RAD * degrees))
set Y = y + (offset * Sin(DEG2RAD * degrees))
endfunction
function GetOffsetPositionRad takes real x, real y, real offset, real radians returns nothing
set X = x + (offset * Cos(radians))
set Y = y + (offset * Sin(radians))
endfunction
function GetOffsetPositionU takes unit u, real offset, real degrees returns nothing
set X = GetUnitX(u) + (offset * Cos(DEG2RAD * degrees))
set Y = GetUnitY(u) + (offset * Sin(DEG2RAD * degrees))
endfunction
function GetOffsetPositionURad takes unit u, real offset, real radians returns nothing
set X = GetUnitX(u) + (offset * Cos(radians))
set Y = GetUnitY(u) + (offset * Sin(radians))
endfunction
// Creates a special effect offset by a set distance in a direction based on the given coordinates:
public function AddSpecialEffectOffset takes string modelName, real x1, real y1, real x2, real y2, real offset returns effect
local real angle = AngleBetweenXY(x1, y1, x2, y2) // reverse parameters to reverse direction
call GetOffsetPosition(x1, y1, offset, angle) // sets the global X and Y variables
return AddSpecialEffect(modelName, X, Y)
endfunction
// Like AddSpecialEffectOffset, but also makes the special effect face the direction:
public function AddSpecialEffectOffsetEx takes string modelName, real x1, real y1, real x2, real y2, real offset returns effect
local real angle = AngleBetweenXY(x1, y1, x2, y2) // reverse parameters to reverse direction
call GetOffsetPosition(x1, y1, offset, angle) // sets the global X and Y variables
set SFX = AddSpecialEffect(modelName, X, Y)
call BlzSetSpecialEffectYaw(SFX, angle * DEG2RAD) // yaw uses radians so we need to convert
return SFX
endfunction
endlibrary