[Solved] AI Script causes game to perma freeze

Level 28
Joined
Dec 3, 2020
Messages
962
So I have the following ai script:

JASS:
//==================================================================================================
//  Retribution -- dark green player (Undead Scourge - Player 11) -- AI Script
//==================================================================================================
globals
    player targ1 = PlayerEx(1) // Alliance Expedition (player)
    player targ2 = PlayerEx(7) // Kirin Tor
    player targ  = targ1
endglobals

//--------------------------------------------------------------------------------------------------
//  switch_targets
//--------------------------------------------------------------------------------------------------
function switch_targets takes nothing returns nothing
    loop
        exitwhen CommandsWaiting() > 0
        call Sleep(1)
    endloop

    set targ = targ2
endfunction

//--------------------------------------------------------------------------------------------------
//  main
//--------------------------------------------------------------------------------------------------
function main takes nothing returns nothing

    call CampaignAI(ZIGGURAT_1,null)

    //call DoCampaignFarms(false)
    set campaign_wood_peons = 0
    call SetReplacements(3,3,3)
    //call GroupTimedLife(true)
    call SetCaptainHome(DEFENSE_CAPTAIN,4209,5530)
    call SetCaptainHome(ATTACK_CAPTAIN,2462,204)
    call SetPeonsRepair(true)


    call SetBuildUnitEx( 1,1,1, NECROPOLIS_1      )
        call SetBuildUnitEx( 1,1,1, ACOLYTE           )
    call SetBuildUnit( 1, UNDEAD_MINE        )
        call SetBuildUnitEx( 2,2,2, CRYPT             )
    call SetBuildUnitEx( 1,1,1, GRAVEYARD             )
        call SetBuildUnitEx( 5,5,5, ZIGGURAT_1        )
    call SetBuildUnitEx( 1,1,1, NECROPOLIS_2      )
        call SetBuildUnitEx( 5,5,5, ACOLYTE           )
    call SetBuildUnitEx( 5,5,5, ZIGGURAT_2        )
    call SetBuildUnitEx( 1,1,1, DAMNED_TEMPLE        )
    call SetBuildUnitEx( 1,1,1, SLAUGHTERHOUSE        )
    call SetBuildUnitEx( 1,1,1, 'usap'        )
    call SetBuildUnitEx( 1,1,1, 'ubon'        )
    call SetBuildUnitEx( 1,1,1, NECROPOLIS_3      )

        call CampaignDefenderEx( 6, 6, 6, GHOUL       )
        call CampaignDefenderEx( 3, 3, 3, NECRO       )


    call WaitForSignal()

    call StartThread(function switch_targets)

    //*** WAVE 1 ***
    call InitAssaultGroup()
    call CampaignAttackerEx( 5,5,5, GHOUL        )
    call CampaignAttackerEx( 1,1,1, ABOMINATION  )
    call CampaignAttackerEx( 4,4,4, 'nska'  )
    call SuicideOnPlayerEx(M1,M1,M1,targ)

    //*** WAVE 2 ***
    call InitAssaultGroup()
    call CampaignAttackerEx( 10,10,10, GHOUL        )
    call CampaignAttackerEx( 2,2,2, ABOMINATION  )
    call CampaignAttackerEx( 4,4,4, 'unec'  )
    call SuicideOnPlayerEx(M4,M4,M4,targ)


    //*** WAVE 3 ***
    call InitAssaultGroup()
    call CampaignAttackerEx( 12,12,12, GHOUL        )
    call CampaignAttackerEx( 4,4,4, 'unec'  )
    call CampaignAttackerEx( 1,1,1, 'umtw'  )
    call CampaignAttackerEx( 4,4,4, 'ugar'  )
    call SuicideOnPlayerEx(M4,M4,M4,targ)


    //*** WAVE 4 ***
    call InitAssaultGroup()
    call CampaignAttackerEx( 10,10,10, GHOUL        )
    call CampaignAttackerEx( 3,3,3, ABOMINATION  )
    call CampaignAttackerEx( 6,6,6, 'nska'  )
    call CampaignAttackerEx( 2,2,2, 'ufro'  )
    call SuicideOnPlayerEx(M4,M4,M4,targ)

endfunction

Right when the AI is about to send their first attack wave, the game permanently freezes (doesn't crash but freezes).
Anyone knows why? I am pretty sure that it's not some triggers causing it and I do the following:

I activate the script for the AI when the map begins (as I always do on every other map of my campaign). The AI only rebuilds lost structures now and has campaignDefenders (works perfectly, doesn't freeze the game).
Then a few moments later I send it the command(0, 0) which gives a signal to the AI to begin attacking the player, the AI prepares its attack wave and as soon as it begins sending it to attack, the game permanently freezes.

This happens with all the other AI Scripts I use on the map too, but for testing purposes I removed them and only kept this one.
All the values are correct (player variables, attack and defend captain coordinates, the unit IDs etc...).
The AI has enough gold, lumber, food and all the necessary techtree. It is also an enemy to the player and the player's allies (and allied to the other undead bases).

I am doing this on the Frostmourne mission. I have removed 90% of the triggers though and I triple checked and didn't find any suspicious trigger that might cause it.
Furthermore, once I activated the AI and didn't send it the command(0, 0) to attack and the game never froze (I playtested it for like 25 min and had 0 issues).

PS: I can send a link to the campaign if anyone wants to waste his time to check the triggers on the final map but at least looking at the script I post here would suffice.
I simplified it as much as I could to see if it will still freeze and it does (for example I removed the looping attack waves etc...).

Basically it works perfectly up until the very first attack wave.
 
Last edited:
JASS:
//============================================================================
//  Human 02 -- teal player -- AI Script
//============================================================================
globals
   
    constant integer Archer = 'e600'
    integer MyTargetInt = 7
    player MyTarget = PlayerEx(1)
                  
endglobals

function CommandCheck takes nothing returns nothing
    //Loop waiting for a Command to be sent
    loop
        loop
            exitwhen CommandsWaiting() != 0
            call Sleep(2)
        endloop
       
    set MyTargetInt = GetLastData() //Set Integer equal to integer sent by command
    set MyTarget = PlayerEx(MyTargetInt) //Set Player target using integer given
    call PopLastCommand()
    endloop
   
endfunction

//============================================================================
//  main
//============================================================================

function main takes nothing returns nothing
    call CampaignAI(HOUSE,null)
    set campaign_wood_peons = 4
    call SetSlowChopping(false)

    call SetBuildUnitEx( 9,9,9, PEASANT           )
    call SetBuildUnitEx( 1,1,1, TOWN_HALL      )
    call SetBuildUnitEx( 2,2,2, BARRACKS         )
    call SetBuildUnitEx( 5,5,5, HOUSE        )
    call SetBuildUnitEx( 1,1,1, 'hlum'        )
    call SetBuildUnitEx( 3,3,3, 'hwtw'        )
    call SetBuildUnitEx( 3,3,3, 'hgtw'        )
   
    call CampaignDefenderEx (4,4,4, 'hfoo'  )
    call CampaignDefenderEx (2,2,2, 'e600'  )

    call WaitForSignal()

    call StartThread(function CommandCheck) //Start thread to check for player target changes

    call InitAssaultGroup()
    call CampaignAttackerEx(2,2,2,FOOTMAN)
    call CampaignAttackerEx(1,1,1,Archer)
    call SuicideOnPlayerEx(M3,M3,M3,MyTarget)

    call InitAssaultGroup()
    call CampaignAttackerEx(3,3,3,FOOTMAN)
    call CampaignAttackerEx(2,2,2,Archer)
    call SuicideOnPlayerEx(M3,M3,M3,MyTarget)

    call InitAssaultGroup()
    call CampaignAttackerEx(4,4,4,FOOTMAN)
    call CampaignAttackerEx(2,2,2,Archer)
    call SuicideOnPlayerEx(M3,M3,M3,MyTarget)

    loop
        call InitAssaultGroup()
        call CampaignAttackerEx(6,6,6,FOOTMAN)
        call CampaignAttackerEx(4,4,4,Archer)
        call SuicideOnPlayerEx(M3,M3,M3,MyTarget)
    endloop
   
endfunction

Here's a script that definitely works and doesn't freeze for switching targets, maybe give it a try. All you need to do is send the command and the integer in the data field for the player you want to target.
 
Level 28
Joined
Dec 3, 2020
Messages
962
Thank you!
I will change the values accordingly but nothing else. If it still freezes then it's some other thing causing the issue (a trigger causing the freeze; but as I said, the map never freezes as long as I don't send the AI command to make it attack, which made me almost certain that it's the script causing this freeze).
 
Level 28
Joined
Dec 3, 2020
Messages
962
Yeah let me know how it goes, AI scripts are always fun to work with lol the sheer amount of things that can break and go wrong.
So I tried it. The AI does function properly, but the game still freezes right before the first attack wave gets sent.
I literally kept only the resource related triggers and the AI initiating triggers on the map. I will try removing a few weather regions later but I don't think that's the issue.

I shall make a new empty map and just try to see if it works there... but it's strange because I literally have 6 previous maps where AI works as it should and the game doesn't freeze.
This is an edit of the final human mission, perhaps there is something there that is causing this error?

Worst case scenario I shall just make a new empty map and copy the terrain and doodads (it's something possible I assume).
 
Level 28
Joined
Dec 3, 2020
Messages
962
Update: even the original AI script causes the map to perma freeze when the AI sends an attack wave. A very weird bug. I shall create an empty map and just copy over the triggers and terrain (if it's possible) instead of using an already existing map. I literally only kept the ending cinematic trigger and the AI resourcing triggers (and the trigger that activates the AI).
 
Level 28
Joined
Dec 3, 2020
Messages
962
Map still crashes, I:
  • Removed ALL the variables
  • Removed ALL the regions
  • Removed ALL the triggers except these:
1739808536626.png

1739808547331.png

1739808560441.png

1739808574147.png

1739808582706.png

1739808590137.png

The rest are the same for gold and lumber for each player
Note that it still crashes without me using the "Custom Test AI 2nd Signal" trigger. The ai script is the same as of the purple undead of the original human RoC campaign but renamed to h10, instead of h9 (or h09) and the attacks are sent faster (M1, down from M8 for example; so I don't have to wait 8 min to see if the map will crash).
The undead (AI in general) will gather an attack group the way it should, but the moment its sent the game perma freezes.

I already began copying over the terrain and units on a new empty map. The doodads are kind of an issue though since thare are around 14 000 and it's hard to place them cause of the insane lag I get the moment I press "C+V".

The weirdest thing is that script that just tells the AI to suicide all of its units works perfectly, and a script that just tells it to rebuild lost units and structures and have CampaignDefenders, but the moment an attack wave is sent, it's like it enters into some magical infinite loop which causes the perma freeze. I cannot explain why, even after removing literally all the variables and regions and triggers.

PS: I think it's just the original map that somehow bugs it out... idk
I will try making a new mission, using the original map, but before adding new players (AI) and making it bigger, I will just replace the AI scripts with my own.
 
Last edited:
Level 28
Joined
Dec 3, 2020
Messages
962
Major update (not mean to spam):
Map no longer perma freezes, I removed some Boundary that was placed on a few places (for cinematic purposes).
I also removed the AI gold and food triggers (unlikely that it was the cause for the bug).

The boundary thing makes no sense tho! No way that it could interrupt the AIs attacks (path wise).

I did this on a 2nd PC, I will try ONLY removing the boundary on my main PC later, if that fixes it:
1 - I will be hugely surprised but also relieved
2 - Mark the thread as solved
 
Level 28
Joined
Dec 3, 2020
Messages
962
Yeah I'm keen to see what the result of this is, boundaries causing a freeze would be interesting. I know the AI searching for expansions is a big source of lag at times.
So I went back to my main PC and removed the boundary and nothing, the map still froze.
It isn't the boundary then (as expected).

Literally the only thing left then is the AI gold & lumber and food triggers!
I also removed ALL unnecessary variables (on my main PC). I did try removing literally every variable (earlier, on my 2nd PC) and it didn't fix it so that's not the issue.
How strange... I will keep testing tomorrow.

Worst case scenario I will use this working version and pass it onto my main PC and just start the rest of the triggers from scratch there.
 
Level 28
Joined
Dec 3, 2020
Messages
962
Could it be a pathing issue in the terrain of the mission itself?
Update: FIXED!!!
So I forgot that I made the map bigger (as in pixels; example: from 220x280 to 252x320) on my 2nd PC, and that's what actually fixed it!!!
I just opened the map on my main PC, made it as big as on my 2nd PC and it works perfectly!

I really have no idea how that made the AI break and freeze the game when attacking an enemy though. I did have some glacier and rock doodads outside of the map (in that black area where no units can enter), but I don't see how that would make the AI bug out when attacking.

A very weird bug, but it is now fixed and thank you Footman for your aid!
And that AI script you gave me too, although I think I just found a new way to write my scripts for my future campaigns (using the undead scripts from Frostmourne mission, so much can be done with the AI this way, idk how it never occurred to me!).
Thread marked as solved.
 
Level 28
Joined
Dec 3, 2020
Messages
962
What's special about the Undead scripts from the Frostmourne mission? I'm just curious in case there's something in there I can use myself :)
Well, when you call the AI Command(0, 0) trigger, the attack waves change.
This not limited only to attack waves of course, you can change the player targets for the AIs attacks, or even completely change everything.
For example: you have a normal orc AI that will change into chaos orcs. 1 part of the script is the normal orcs and the other part is the chaos orcs stuff (at least I assume, this is hypothetical, but the part about changing attack waves is not since a blizzard employee literally used it, therefore making me believe that what I said is indeed possible, just have to change more things than the attack waves)
And you can repeat this as many times in the script as you want. Example: AI attacks Player 1 by default, using Ghouls and Necromancers only, then when Player 1 dies, you order it to start attacking Player 2, but this time adding Abominations and Meat Wagons in the attack waves. Player 2 eventually dies, you order the AI to attack Player 3, but this time also adding Gargoyles and Frost Wyrms in the attack waves etc...

I shall tinker with this and put it into practice to see if it actually works (but it should as far as I know). When I get to see it work in practice, I can message you I guess if you are interested in knowing how and what to do in order to make it work.
 
Level 19
Joined
Apr 20, 2014
Messages
616
Update: FIXED!!!
So I forgot that I made the map bigger (as in pixels; example: from 220x280 to 252x320) on my 2nd PC, and that's what actually fixed it!!!
I just opened the map on my main PC, made it as big as on my 2nd PC and it works perfectly!

I really have no idea how that made the AI break and freeze the game when attacking an enemy though. I did have some glacier and rock doodads outside of the map (in that black area where no units can enter), but I don't see how that would make the AI bug out when attacking.

A very weird bug, but it is now fixed and thank you Footman for your aid!
And that AI script you gave me too, although I think I just found a new way to write my scripts for my future campaigns (using the undead scripts from Frostmourne mission, so much can be done with the AI this way, idk how it never occurred to me!).
Thread marked as solved.
So making the map bigger fixed the problem?
Sounds weird.

I thought you had to do the opposite though.

I also tried a script on a very big map, and if the path is too big, it crashes.
 
Well, when you call the AI Command(0, 0) trigger, the attack waves change.
This not limited only to attack waves of course, you can change the player targets for the AIs attacks, or even completely change everything.
For example: you have a normal orc AI that will change into chaos orcs. 1 part of the script is the normal orcs and the other part is the chaos orcs stuff (at least I assume, this is hypothetical, but the part about changing attack waves is not since a blizzard employee literally used it, therefore making me believe that what I said is indeed possible, just have to change more things than the attack waves)
And you can repeat this as many times in the script as you want. Example: AI attacks Player 1 by default, using Ghouls and Necromancers only, then when Player 1 dies, you order it to start attacking Player 2, but this time adding Abominations and Meat Wagons in the attack waves. Player 2 eventually dies, you order the AI to attack Player 3, but this time also adding Gargoyles and Frost Wyrms in the attack waves etc...

I shall tinker with this and put it into practice to see if it actually works (but it should as far as I know). When I get to see it work in practice, I can message you I guess if you are interested in knowing how and what to do in order to make it work.
Oh right that kinda stuff, yeah if you put the attack waves into functions sending AI commands to change them is pretty good/powerful/useful.
 
Top