1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Lead your forces to battle in the 15th Techtree Contest. The call is yours, commander!
    Dismiss Notice
  4. The reforging of the races is complete. Come see the 14th Techtree Contest Results.
    Dismiss Notice
  5. It's time to choose your horse in the race - the 32nd Modeling Contest Poll is up!
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Computer AI vjass

Discussion in 'Triggers & Scripts' started by dardas, Mar 10, 2011.

  1. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    hey.. i finised my gui code of the computer ai, which went terribly wrong...
    so i tried remaking it in vjass.. somehow i managed to remember some old stuff :p
    anyways, it managed to create heroes, store data, etc.. but it wont work correctly ;/
    it uses 2 libraries, 1 i made, and the second is grouputils.
    the problem:
    it wont go thru the loop i supose.. cant really find it out =]
    it creates hero, and wont do anything else, move, attack, etc.

    heres the code:
    Code

    Code (vJASS):

    library ComputerAI initializer init requires GroupUtils, GetClosestUnit
       
        globals
            //Declared Data
            private timer tim = null
            private integer array index
            private integer array heroList
           
            private integer count
            //Used inloop data
            private unit array botHero
            private player array botOwner
            private string array botState
            private unit array botTarget
            private group array botGroup
            private real array botHealSpotX
            private real array botHealSpotY
            //
        endglobals
       
        //List of indexes:
        //Index[0] = Hero List Count
        //Index[1] = Bots In Total
        //Index[2] = counting units in group
        //
       
        public function AddHeroToList takes integer unitid returns integer
            set index[0] = index[0] + 1
            set heroList[index[0]] = unitid
            return index[0]
        endfunction
       
        public function RemoveHeroFromList takes integer index returns nothing
            set heroList[index] = 0
        endfunction
       
        public function LocateUnitsBoolexpr takes nothing returns boolean
            return GetWidgetLife(GetFilterUnit()) > 0.405 and IsUnitEnemy(GetFilterUnit(), botOwner[count]) and IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) == false
        endfunction
       
        public function CreateNewBot takes player p, location startLoc, location healLoc returns integer
            local boolean pickedHero = false
            local integer num = 0
           
            set index[1] = index[1] + 1
           
            loop
                set num = GetRandomInt(1, index[0])
                if heroList[num] != 0 then
                    set pickedHero = true
                endif
            exitwhen pickedHero == true
            endloop
           
            set botHero[index[1]] = CreateUnit(p, heroList[num], GetLocationX(startLoc), GetLocationY(startLoc), 0)
            set botOwner[index[1]] = p
            set botHealSpotX[index[1]] = GetLocationX(healLoc)
            set botHealSpotY[index[1]] = GetLocationY(healLoc)
           
            set botGroup[index[1]] = NewGroup()
           
            return index[1]
        endfunction
       
        private function HandleActions takes nothing returns nothing
            if botState[count] == "moving to heal" then
                call IssuePointOrder(botHero[count], "move", botHealSpotX[count], botHealSpotY[count])
                if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) < 98 then
                    set botState[count] = "healing"
                endif
            else
                if botState[count] == "attacking enemy" then
                    call IssueTargetOrder(botHero[count], "attack", botTarget[count])
                endif
            endif
        endfunction
       
        private function PrepareActions takes nothing returns nothing
            if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) <= 30 or botState[count] == "healing" then
                set botState[count] = "moving to heal"
                set botTarget[count] = null
                if GetUnitStatePercent(botHero[count], UNIT_STATE_LIFE, UNIT_STATE_MAX_LIFE) >= 98 then
                    set botState[count] = "no actions"
                endif
            else
                if GetUnitState(botHero[count], UNIT_STATE_LIFE) <= 0 or index[2] <= 0 then
                    if index[2] <= 0 then
                        call GroupEnumUnitsInRect(botGroup[count], bj_mapInitialPlayableArea, function LocateUnitsBoolexpr)
                        call GroupRefresh(botGroup[count])
                       
                        set index[2] = CountUnitsInGroup(botGroup[count])
                    endif
                   
                    if GetUnitState(botHero[count], UNIT_STATE_LIFE) <= 0 then
                        set index[2] = index[2] - 1
                        set botTarget[count] = FindClosestUnit(botGroup[count], GetUnitX(botHero[count]), GetUnitY(botHero[count]))
                        set botState[count] = "attacking enemy"
                    endif
                endif
            endif
        endfunction
       
        private function ComputerAIActions takes nothing returns nothing
            set count = 0
            if index[1] != 0 then
                loop
                set count = count + 1
                    if GetUnitState(botHero[count], UNIT_STATE_LIFE) > 0 then
                        call PrepareActions()
                        call HandleActions()
                    endif
                exitwhen count == index[1]
                endloop
            endif
        endfunction
       
        private function init takes nothing returns nothing
            set tim = CreateTimer()
            call TimerStart(tim, 1, true, function ComputerAIActions)
           
        endfunction
    endlibrary
     

     
  2. Sevion

    Sevion

    Joined:
    Dec 4, 2006
    Messages:
    105
    Resources:
    2
    Tools:
    1
    JASS:
    1
    Resources:
    2
    Fill it with debug messages and post results, please.
     
  3. baassee

    baassee

    Joined:
    Nov 14, 2008
    Messages:
    3,220
    Resources:
    17
    Spells:
    14
    Tutorials:
    3
    Resources:
    17
    Have no idea, the script confuses me.
     
  4. Sevion

    Sevion

    Joined:
    Dec 4, 2006
    Messages:
    105
    Resources:
    2
    Tools:
    1
    JASS:
    1
    Resources:
    2
    As I said earlier, fill it with debug messages. I.E. output the variable values when you check them/use them. Output flags like "A1", "A2", etc to find where the script stops.
     
  5. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    hmm.. i tried remaking with Struct after school, i realised you wrote this too late(lightning went offline, so inet had strange difficulties..)
    well.. if struct thingy will fail, ill put lots of debug messages, and see where it fails..

    btw, last code managed to work,
    but i realised it works once, only after around the 2 minutes of waiting.. after creeps show up..
    and only 3/4 heroes work.
     
  6. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,212
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    GetUnitState(botHero[count], UNIT_STATE_LIFE) > 0

    >

    GetWidgetLife(botHero[count]) > 0.405

    Code (vJASS):

    globals
        private timer tim = null
    endglobals
    private function init takes nothing returns nothing
        set tim = CreateTimer()
        call TimerStart(tim, 1, true, function ComputerAIActions)
    endfunction
     


    >

    Code (vJASS):

    private function init takes nothing returns nothing
        call TimerStart(CreateTimer(), 1, true, function ComputerAIActions)
    endfunction
     


    GetUnitState(botHero[count], UNIT_STATE_LIFE) <= 0

    >

    IsUnitType(botHero[count], UNIT_TYPE_DEAD)

    public function CreateNewBot takes player p, location startLoc, location healLoc returns integer

    >

    public function CreateNewBot takes player p, real startX, real startY, real healX, real healY returns integer
     
  7. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    yeah i actually had a reason to use locations in here

    public function CreateNewBot takes player p, location startLoc, location healLoc returns integer

    since this was meant for gui initializing.. too lazy to use it otherwise =]
    thx for pointing those stuff out tho :D

    btw.. is there a way to make a loop, which goes thru a index from index[0] to index[1],
    and arrange the code from biggest to lowest?
    for example, i got a list of integers:

    1, 5, 7, 2, 3, 9, 4
    i want it to give me this at the end:
    9, 7, 5, 4, 3, 2, 1

    is it possible? i've tried finding it that way.. (trying to arrange the levels in a array,
    from lowest to highest, and from highest to lowest.)

    thanks =]
     
  8. Sevion

    Sevion

    Joined:
    Dec 4, 2006
    Messages:
    105
    Resources:
    2
    Tools:
    1
    JASS:
    1
    Resources:
    2
    You can arrange from high to low. You simply have a loop with two variables.

    Code (vJASS):
    local integer i = begin
    local integer j = end
    loop
        set array1[i] = array2[j]
        set i = i + 1
        set j = j - 1
        exitwhen ( i == end or j == begin )
    endloop


    Where array1 is the high-low array and array2 is the low-high array.
     
  9. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    yea.. my problem is to take 1, 3, 5, 2, 0, and make it
    5, 3, 2, 1, 0
    and not make last the begining, and begining the last :p
     
  10. Sevion

    Sevion

    Joined:
    Dec 4, 2006
    Messages:
    105
    Resources:
    2
    Tools:
    1
    JASS:
    1
    Resources:
    2
    Then you have to throw in a few if/then/else checks.
     
  11. Admiral Jack

    Admiral Jack

    Joined:
    May 23, 2011
    Messages:
    171
    Resources:
    0
    Resources:
    0
    CAn you make a map out of this Jass AI you make?
     
  12. dardas

    dardas

    Joined:
    Sep 12, 2008
    Messages:
    649
    Resources:
    0
    Resources:
    0
    It was for an old map, but why?
    the code isn't done, got messed up a bit, i supose i can fix it,
    but it'll be very annoying right now..