resultUnit = null
for unit in group do
if (isEnemy(unit) == false) then
removeUnit(group)
endif
endfor
if (firstOf(group) != null) then
set resultUnit = firstOf(group)
set distX = GetUnitX(resultUnit) - x
set distY = GetUnitY(resultUnit) - y
set lastDistSquare = distX * distX + distY * distY
for unit in group do
set distX = GetUnitX(unit) - x
set distY = GetUnitY(unit) - y
set distSquare = distX * distX + distY * distY
if (distSquare < lastDistSquare) then
lastDistSquare = distSquare
resultUnit = unit
endif
endfor
endif