Warcraft III resources & community, 2003–2006 · archived

FindCampmateUnits

not rated
Submitted by AltSubmitterFunctions0 downloads
This function could also be called "FindContiguousUnits" or something like that, basically, starting from one unit, it finds all that units "mates", units owned by the same player, and less than threshold distance from another in the camp. This is designed for a roll-your-own creepcamp sytem.

As a test I made a string of 512 units across the map, when run the function caused only a very slight amount of lag and accurately determined that all 512 units were in the same camp.

It basically works much like a floodfill algorithm using a stack - it is not recursive.

Details:
It does NOT detect hidden units, in fact it exploits the fact that hidden units are not detected by "EnumUnitsInRange". The boolean parameter determines if it should unhide the units when done (true = yes, unhide, false = leave them hidden). For my own personal use I need the units to remain hidden; justifying the parameter.

Source

function FindCampmateUnitsHideEnum takes nothing returns nothing
    call ShowUnit(GetEnumUnit(),false)
endfunction

function FindCampmateUnitsShowEnum takes nothing returns nothing
    call ShowUnit(GetEnumUnit(),true)
endfunction

function FindCampmateUnitsCondition takes nothing returns boolean
    return (GetOwningPlayer(GetFilterUnit()) == bj_groupEnumOwningPlayer)
endfunction

function FindCampmateUnits takes unit whichUnit, real threshold, boolean unhide returns group
    local group taggedUnits = CreateGroup()
    local group camp = CreateGroup()
    local unit u    
    local group close
    local boolexpr filter = Condition(function FindCampmateUnitsCondition)
    call GroupAddUnit(taggedUnits,whichUnit)
    set bj_groupEnumOwningPlayer = GetOwningPlayer(whichUnit)
    loop
        set u = FirstOfGroup(taggedUnits)
        exitwhen u == null
        set close = CreateGroup()
        call GroupEnumUnitsInRange(close,GetUnitX(u),GetUnitY(u),threshold,filter)
        call ForGroup(close, function FindCampmateUnitsHideEnum)
        call GroupAddGroup(close,taggedUnits)
        call GroupAddGroup(close,camp)
        call DestroyGroup(close)
        call GroupRemoveUnit(taggedUnits,u)

    endloop
    set bj_groupEnumOwningPlayer = null
    set bj_groupRandomCurrentPick = null
    call DestroyGroup(taggedUnits)
    call DestroyBoolExpr(filter)
    if (unhide == true) then
        call ForGroup(camp, function FindCampmateUnitsShowEnum)
    endif

    return camp
endfunction

Comments

0

No comments.