[AI] Trying to Make Function That Outputs Specific Units

Level 22
Joined
Mar 16, 2008
Messages
1,013
Trying to make a function that targets 4 preplaced Castles 'hcas' on map in AI script. Would be easy if we could access udg variables but we can't for AI. Do the functions below look OK? for some reason they don't seem to work. But I haven't used debug codes yet. I'm just wondering if anyone more familiar with JASS notices any blatant problems.

JASS:
//===========================================================================
// Picks a player that isn't the AI and is playing (has gold)
//===========================================================================
function Pick_Tar takes nothing returns player
    local player array p
    local integer array_count = 0
    local integer i
    if (gCond_Has_Gold_Red) and (ai_player !=Player(0)) then
        set array_count = array_count + 1
        set p[array_count] = Player(0)
    endif
    if (gCond_Has_Gold_Blu) and (ai_player !=Player(1)) then
        set array_count = array_count + 1
        set p[array_count] = Player(1)
    endif
    if (gCond_Has_Gold_Tea) and (ai_player !=Player(2)) then
        set array_count = array_count + 1
        set p[array_count] = Player(2)
    endif
    if (gCond_Has_Gold_Pur) and (ai_player !=Player(3)) then
        set array_count = array_count + 1
        set p[array_count] = Player(3)
    endif
    set i = GetRandomInt(1, array_count)
    return p[i]
endfunction

//===========================================================================
// Finds a unit of type
//===========================================================================
function FindUnitOfType takes player p returns unit
    local group g
    local unit u
    local unit found
    call GroupEnumUnitsOfPlayer(g, p, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null or found != null
        call GroupRemoveUnit(g, u)
        if GetUnitTypeId(u) == 'hcas' then
            set found = u
        endif
    endloop
    return found
endfunction
 
There may be more, but I think the script is going to quietly fail since you didn't create a group.
JASS:
function FindUnitOfType takes player p returns unit
    local group g // = CreateGroup()
    local unit u
    local unit found
    call GroupEnumUnitsOfPlayer(g, p, null)
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null or found != null
        call GroupRemoveUnit(g, u)
        if GetUnitTypeId(u) == 'hcas' then
            set found = u
        endif
    endloop
    return found
endfunction

Personally, I would do this:
JASS:
function CastleFilter takes nothing returns boolean
    return GetUnitTypeId( GetFilterUnit() ) == 'hcas'
endfunction

function FindPlayerCastle takes player p returns unit
    local group g = CreateGroup()
    call GroupEnumUnitsOfPlayer( g, p, Condition( function CastleFilter ) )
    return FirstOfGroup( g )
endfunction
 
Last edited:
Back
Top