Warcraft III resources & community, 2003–2006 · archived

FindClosestUnitLoc

not rated
Submitted by AltSubmitterFunctions0 downloads
Simply finds the closest unit to loc, in group g.

The "Loc" version is basically a fluff function to make GUI use easier, those who use common.j functions will probably prefer using plain FindClosestUnit.

This function uses the "bj_wantDestroyGroup" hack to determine whether or not to destroy the group passed to it.

Source

function FindClosestUnit takes group g, real x, real y returns unit
    local real dx
    local real dy
    local group tempGroup
    local real maxDist=999999.0
    local real dist
    local unit u = null
    local unit closest = null

    if (bj_wantDestroyGroup == true) then
        set tempGroup = g 
    else
        set tempGroup = CreateGroup()
        call GroupAddGroup(g, tempGroup)
    endif
    set bj_wantDestroyGroup = false
    
    loop
        set u = FirstOfGroup(tempGroup)
        call GroupRemoveUnit(tempGroup, u)
        exitwhen (u == null)
        set dx = GetUnitX(u) - x
        set dy = GetUnitY(u) - y
        set dist = SquareRoot(dx*dx+dy*dy)
        if (dist < maxDist) then
            set closest = u
            set maxDist = dist
        endif
    endloop
    call DestroyGroup(tempGroup)
    set tempGroup = null
    return closest
endfunction

function FindClosestUnitLoc takes group g, location loc returns unit
    return FindClosestUnit(g,GetLocationX(loc),GetLocationY(loc))
endfunction

Comments

0

No comments.