Warcraft III resources & community, 2003–2006 · archived

GroupFindCenterLoc

not rated
Submitted by AltSubmitterPhysics0 downloads
Given a unit group, finds the center, like the center of gravity.

Source

function GroupFindCenterXEnum takes nothing returns nothing
    set bj_randomSubGroupChance = bj_randomSubGroupChance + GetUnitX(GetEnumUnit())
    set bj_randomSubGroupTotal = bj_randomSubGroupTotal + 1
endfunction

function GroupFindCenterYEnum takes nothing returns nothing
    set bj_randomSubGroupChance = bj_randomSubGroupChance + GetUnitY(GetEnumUnit())
    set bj_randomSubGroupTotal = bj_randomSubGroupTotal + 1
endfunction

function GroupFindCenterLoc takes group whichGroup returns location
    local real oldReal = bj_randomSubGroupChance
    local integer oldInt = bj_randomSubGroupTotal
    local real x
    local real y
    //Determine average x
    if (whichGroup == null) or (FirstOfGroup(whichGroup) == null) then
        return null
    endif
    set bj_randomSubGroupChance = 0.0
    set bj_randomSubGroupTotal = 0
    call ForGroup(whichGroup, function GroupFindCenterXEnum)
    set x = bj_randomSubGroupChance / (bj_randomSubGroupTotal)

    //Determine average y
    set bj_randomSubGroupChance = 0.0
    set bj_randomSubGroupTotal = 0
    call ForGroup(whichGroup, function GroupFindCenterYEnum)
    set y = bj_randomSubGroupChance / (bj_randomSubGroupTotal)
    
    set bj_randomSubGroupChance = oldReal
    set bj_randomSubGroupTotal = oldInt

    return Location(x,y)
endfunction

Comments

4
will this snippet work for making a unit stand upside down?
nice :lol:
so zelos? this one is able to be used in many different ways. what if someone didnt want the guys to attract?
k this sux, ive made 1 that even make unit get closer to each other like real gravity