function ClosestUnitLoop takes nothing returns nothing
local unit u = GetEnumUnit()
local real x = GetUnitX(u) - ExtraX
local real y = GetUnitY(u) - ExtraY
local real r = SquareRoot(x * x + y * y)
if r < ExtraReal then
set ExtraReal = r
set ExtraUnit = u
endif
set u = null
endfunction
function ClosestUnitLoopZ takes nothing returns nothing
local unit u = GetEnumUnit()
local real x = GetUnitX(u) - ExtraX
local real y = GetUnitY(u) - ExtraY
local real z
local real r
local location l = Location(x,y)
set z = GetLocationZ(l)
call RemoveLocation(l)
set l = null
set r = SquareRoot(x * x + y * y + z * z)
if r < ExtraReal then
set ExtraReal = r
set ExtraUnit = u
endif
set u = null
endfunction
function GetClosestUnitOfGroup takes group g,real x,real y returns unit
set ExtraReal = 100000
set ExtraX = x
set ExtraY = y
call ForGroup(g, function ClosestUnitLoop)
return ExtraUnit
endfunction
function GetClosestUnitOfGroupEx takes group g,unit u returns unit
set ExtraReal = 100000
set ExtraX = GetUnitX(u)
set ExtraY = GetUnitY(u)
call ForGroup(g, function ClosestUnitLoop)
return ExtraUnit
endfunction
function GetClosestUnitOfGroupZ takes group g,real x,real y,real z returns unit
set ExtraReal = 100000
set ExtraX = x
set ExtraY = y
set ExtraZ = z
call ForGroup(g, function ClosestUnitLoopZ)
return ExtraUnit
endfunction
function GetClosestUnitOfGroupZEx takes group g,unit u returns unit
local location l
set ExtraReal = 100000
set ExtraX = GetUnitX(u)
set ExtraY = GetUnitY(u)
set l = Location(ExtraX,ExtraY)
set ExtraZ = GetLocationZ(l)
call RemoveLocation(l)
set l = null
call ForGroup(g, function ClosestUnitLoopZ)
return ExtraUnit
endfunction