• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[AI] Attempting an AI.

Status
Not open for further replies.

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
I'm trying to make an AI just as a change of pace, but it isn't working right. No matter how many bots I turn on in the lobby only one of them works.

JASS:
globals
    unit array uAI
    integer iAIIndex=0
endglobals

function initAIOrder takes nothing returns nothing
    local integer index=0
    loop
        exitwhen index>iAIIndex
        
        if IsPlayerAlly(GetOwningPlayer(uAI[index]),Player(0)) or GetOwningPlayer(uAI[index])==Player(0) then
            if GetUnitState(uAI[index],UNIT_STATE_LIFE)<1 then
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)<.5 then
                call IssuePointOrder(uAI[index],"move",-2101,-6908)
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)>.5 then
                call IssuePointOrder(uAI[index],"move",353,-703)
            endif
        else
            call BJDebugMsg(GetUnitName(uAI[index])+" is on the north team. "+GetPlayerName(GetOwningPlayer(uAI[index])))
            if GetUnitState(uAI[index],UNIT_STATE_LIFE)<1 then
                call BJDebugMsg(GetUnitName(uAI[index])+" is dead.")
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)<.5 then
                call BJDebugMsg(GetUnitName(uAI[index])+" being moved to north repair dock. "+GetPlayerName(GetOwningPlayer(uAI[index])))
                call IssuePointOrder(uAI[index],"move",57,6147)
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)>.5 then
                call BJDebugMsg(GetUnitName(uAI[index])+" being moved to middle of right lane. "+GetPlayerName(GetOwningPlayer(uAI[index])))
                call IssuePointOrder(uAI[index],"move",353,-703)
            endif
        endif
        
        set index=index+1
    endloop
endfunction

function initAIChoose takes nothing returns boolean
    return IsUnitType(GetFilterUnit(),UNIT_TYPE_HERO)
endfunction

function initAIA takes nothing returns nothing
    local integer index=2
    local group gHeroes=CreateGroup()
    local timer newOrder=CreateTimer()
    loop
        exitwhen index>11
        if GetPlayerController(Player(index))==MAP_CONTROL_COMPUTER then
            call GroupEnumUnitsOfPlayer(gHeroes,Player(index),Condition(function initAIChoose))
            set uAI[iAIIndex]=pickRandUnit(gHeroes)
            set iAIIndex=iAIIndex+1
            call GroupClear(gHeroes)
        endif
        set index=index+1
    endloop
    call DestroyGroup(gHeroes)
    call TimerStart(newOrder,5,true,function initAIOrder)
endfunction

function InitTrig_initAI takes nothing returns nothing
    set gg_trg_initAI=CreateTrigger()
    call TriggerRegisterTimerEvent(gg_trg_initAI,10,false)
    call TriggerAddAction(gg_trg_initAI,function initAIA)
endfunction
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
JASS:
library randomPick
    function pickRandUnit takes group whichGroup returns unit
        set bj_groupRandomConsidered = 0
        set bj_groupRandomCurrentPick = null
        call ForGroup(whichGroup,function GroupPickRandomUnitEnum)
        call DestroyGroup(whichGroup)
        return bj_groupRandomCurrentPick
    endfunction
endlibrary


It is supposed to be like that, because I want it to skip the first 2 players (red and blue, which are empires, this is battleships)
 

Cokemonkey11

Spell Reviewer
Level 30
Joined
May 9, 2006
Messages
3,537
Great, thanks. +reps ofc

Heres the script now, trying to get ships to stay shallow. They don't move at all now. What am I doing wrong?

JASS:
globals
    unit array uAI
    integer iAIIndex=0
endglobals

function aiOrderDeepestBack takes unit u returns location
    local unit favUnit
    local location start=GetUnitLoc(u)
    local group g=CreateGroup()
    local real multiplier
    local integer playerNum
    local boolean less=false
    if GetPlayerId(GetOwningPlayer(u))<7 then
        set multiplier=-1
        set playerNum=1
        set less=true
    else
        set multiplier=1
        set playerNum=0
        set less=false
    endif
    call GroupEnumUnitsOfPlayer(g,Player(playerNum),null)
    loop
        call BJDebugMsg("loop starting")
        exitwhen FirstOfGroup(g)==null

        if less then
            if GetUnitY(FirstOfGroup(g))<GetUnitY(favUnit) or favUnit==null then
                set favUnit=FirstOfGroup(g)
                call BJDebugMsg("set fav unit")
            endif
        else
            if GetUnitY(FirstOfGroup(g))>GetUnitY(favUnit) or favUnit==null then
                set favUnit=FirstOfGroup(g)
                call BJDebugMsg("set fav unit")            
            endif
        endif

        call GroupRemoveUnit(g,FirstOfGroup(g))
        call BJDebugMsg("loop returning")
    endloop
    call DestroyGroup(g)
    call BJDebugMsg("returning")
    return Location(GetUnitX(favUnit),GetUnitY(favUnit)+multiplier*500)
endfunction

function initAIOrder takes nothing returns nothing
    local integer index=0
    loop
        exitwhen index>=iAIIndex
        
        
        
        if GetPlayerId(GetOwningPlayer(uAI[index]))<7 then
            if GetUnitState(uAI[index],UNIT_STATE_LIFE)<1 then
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)<.5 then
                call IssuePointOrder(uAI[index],"move",-2101,-6908) //move to south repair
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)>.5 then
                //call IssuePointOrder(uAI[index],"move",353,-703) //move to middle of right lane
                call IssuePointOrderLoc(uAI[index],"move",aiOrderDeepestBack(uAI[index]))
            endif
        else
            if GetUnitState(uAI[index],UNIT_STATE_LIFE)<1 then
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)<.5 then
                call IssuePointOrder(uAI[index],"move",57,6147) //move to north repair
            elseif GetUnitState(uAI[index],UNIT_STATE_LIFE)/GetUnitState(uAI[index],UNIT_STATE_MAX_LIFE)>.5 then
                //call IssuePointOrder(uAI[index],"move",353,-703) //move to middle of right lane
                call IssuePointOrderLoc(uAI[index],"move",aiOrderDeepestBack(uAI[index]))
            endif
        endif
        
        
        
        set index=index+1
    endloop
endfunction

function initAIChoose takes nothing returns boolean
    return IsUnitType(GetFilterUnit(),UNIT_TYPE_HERO)
endfunction

function initAIA takes nothing returns nothing
    local integer index=2
    local group gHeroes=CreateGroup()
    local timer newOrder=CreateTimer()
    loop
        exitwhen index>11
        if GetPlayerController(Player(index))==MAP_CONTROL_COMPUTER then
            call GroupEnumUnitsOfPlayer(gHeroes,Player(index),Condition(function initAIChoose))
            set uAI[iAIIndex]=pickRandUnit(gHeroes)
            set iAIIndex=iAIIndex+1
            call GroupClear(gHeroes)
        endif
        set index=index+1
    endloop
    call DestroyGroup(gHeroes)
    call TimerStart(newOrder,5,true,function initAIOrder)
endfunction

function InitTrig_initAI takes nothing returns nothing
    set gg_trg_initAI=CreateTrigger()
    call TriggerRegisterTimerEvent(gg_trg_initAI,10,false)
    call TriggerAddAction(gg_trg_initAI,function initAIA)
endfunction

updated.

Edit: I fixed it with help from bengal tigger.
 
Last edited:
Status
Not open for further replies.
Top