globals
private group ENUM_GROUP = CreateGroup()
private unit GetMostWounded = null
real MIN_HP_POSSIBLE = 1.0 //if a unit is detected with this much health, then it is automatically the unit with less health returned
endglobals
native UnitAlive takes unit u returns boolean
function FilterUnit takes unit u returns boolean
return UnitAlive(u) and IsUnitType(u, UNIT_TYPE_HERO)
endfunction
function GetUnitWithLessHealthInRect takes rect r returns unit
local integer i
local integer max
local real currentHealth
local real minHealth
local unit current
call GroupEnumUnitsInRect(ENUM_GROUP, r, null)
set max = BlzGroupGetSize(ENUM_GROUP)
if max == 0 then
return null
endif
set i = 0
set minHealth = 0.0
set GetMostWounded = null
loop
exitwhen i >= max
set current = BlzGroupUnitAt(ENUM_GROUP, i)
if FilterUnit(current) then
set currentHealth = GetUnitState(current, UNIT_STATE_LIFE)
if currentHealth <= MIN_HP_POSSIBLE then
set GetMostWounded = current
set current = null
call GroupClear(ENUM_GROUP)
return GetMostWounded
elseif currentHealth < minHealth or i == 0 then
set minHealth = currentHealth
set GetMostWounded = current
endif
endif
set i = i + 1
endloop
call GroupClear(ENUM_GROUP)
set current = null
return GetMostWounded
endfunction