library Neartest
//*************************************************
//* _ _ _
//*| \ | | ___ __ _ _ __ ___ ___| |_
//*| \| |/ _ \/ _` | '__/ _ \/ __| __|
//*| |\ | __/ (_| | | | __/\__ \ |_
//*|_| \_|\___|\__,_|_| \___||___/\__|
//* by dhguardianes
//* this idea by Tom_Kazansky
//* -------------
//* Easiest and with more flexible way to retrieve the nearest unit from a point
//*
//* function GetNearestUnit( X,Y,Radius,Boolexpr)
//*
//* The extra integer argument skips that many units in the
//* list of near units.
//* Example: If takes "3" then it would return the second nearest
//* unit.
//* The extra real argument is the range to pick unit
//* return null if no unit is picked.
//* Example:
//*
//* set TempUnit = GetNearestUnit(GetLocationX(TempLoc),GetLocationY(TempLoc),800,Condition(function IsBuilding))
//*
//************************************************
//=========================================================================================
globals
private unit GetUnit
private real Distance
private real x
private real y
private real value
private constant group g = CreateGroup()
endglobals
//=========================================================================================
function IsHero takes nothing returns boolean
return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) == true )
endfunction
function IsBuilding takes nothing returns boolean
return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) == true )
endfunction
function IsBuildingPlayer0 takes nothing returns boolean
return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) and ( GetOwningPlayer(GetFilterUnit()) == Player(0) ) == true )
endfunction
function IsBuildingPlayer6 takes nothing returns boolean
return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) and ( GetOwningPlayer(GetFilterUnit()) == Player(6) ) == true )
endfunction
private function callback takes nothing returns nothing
set value = SquareRoot((GetUnitX(GetEnumUnit()) - x) * (GetUnitX(GetEnumUnit()) - x) + (GetUnitY(GetEnumUnit()) - y) * (GetUnitY(GetEnumUnit()) - y))
if ( value < Distance ) then
set Distance = value
set GetUnit = GetEnumUnit()
endif
endfunction
function GetNearestUnit takes real newX, real newY, real newR, boolexpr newB returns unit
set Distance = newR
set x = newX
set y = newY
set GetUnit = null
call GroupEnumUnitsInRangeOfLoc(g, Location(newX, newY), newR, newB)
call ForGroup(g, function callback)
call GroupClear(g)
return GetUnit
endfunction
endlibrary