library DestructableEnum // by Mr_Bean
globals
private destructable closest // Stores current closest destructable.
private real distance // Stores distance of current closest destructable.
private real unitX // Stores x co-ordinate of checked unit.
private real unitY // Stores y co-ordinate of checked unit.
endglobals
private function ClosestDestrEnum takes nothing returns nothing
local destructable d = GetEnumDestructable()
local real dx
local real dy
local real dist
if (GetWidgetLife(d) > 0.405) then
set dx = GetDestructableX(d) - unitX
set dy = GetDestructableY(d) - unitY
set dist = SquareRoot((dx * dx) + (dy * dy))
if (dist < distance) then
set closest = d
set distance = dist
endif
endif
set d = null
endfunction
function GetClosestDestructable takes unit whichUnit, rect whichRect returns destructable
set distance = 999999.0
set closest = null
set unitX = GetUnitX(whichUnit)
set unitY = GetUnitY(whichUnit)
call EnumDestructablesInRect(whichRect, null, function ClosestDestrEnum)
return closest
endfunction
endlibrary