HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

SuicideOnQueue (new function)

12-27-2003, 11:36 AM#1
Tommi
Hi folks,

I finished the first version of SuicideOnQueue function, which may come handy to others, too.

Code:
//===========================================================================
// SuicideOnQueue
//
// Description: Attacks units on a queue, prioritizing earlier targets on the
// queue over later ones. Will reattack the targets, if they are revived.
//
// Returns true when all units on the queue are destroyed or hidden
// Returns false when the attack group is destroyed
//===========================================================================
function SuicideOnQueue takes nothing returns boolean
    local unit target = null
    local integer i = 0

    loop
        //looks for a valid target, beginning from the start of the queue every 2 seconds

        set target = null
        set i = 0

        loop
            set target = TargetQueue[i]

            //Exit when all targets have been destroyed
            if target == null then
                return true
            endif

            //Exit when a target that is alive and not hidden is found
            exitwhen (UnitAlive(target) and not IsUnitHidden(target))

            set i = i + 1
        endloop

        //exit if the attack group has been destroyed
        if CaptainIsEmpty() then
            return false
        endif

        //Attack the target
        call AttackMoveKill(target)

        //Sleep for 2 seconds before performing any other actions
        call Sleep(2)

    endloop

    //Necessary to add because of the return check
    return false

endfunction

The function uses global unit array TargetQueue, which must be initialized with appropriate targets before this function is called. I used myself the following function:
Code:
//===========================================================================
// Initializes TargetQueue
//===========================================================================
function SetTargetUnits takes nothing returns nothing
    local group g = CreateGroup()
    local unit u = null
    local integer id

    call GroupEnumUnitsOfPlayer(g, Player(0), null)

    set u = FirstOfGroup(g)
    loop
        exitwhen u == null
        set id = GetUnitTypeId(u)
        if(id == 'h000') then
            set TargetQueue[0] = u
        elseif(id == 'nmgv') then
            set TargetQueue[1] = u
        endif

        call GroupRemoveUnit(g, u)
        set u = FirstOfGroup(g)
    endloop

    call DestroyGroup(g)

    set TargetQueue[2] = null

endfunction
Comments welcome.

Cheers,

Tommi