Editing Melee AI Difficulty with triggers (this is not about custom AI)

Level 6
Joined
Aug 28, 2019
Messages
83
Hi everyone, I have known the Hive for a long time but only recently signed on. I noticed that there were discussions years ago asking about modifying melee AI with triggers, and from what I can tell the code was not shared. So here it is. This is part of a simple tutorial I made many years ago, with significant help from my friend CHUNK whose knowledge of JASS is that of legend. Without further ado, here it is.


********
TUTORIAL
********


This simple command will allow you to check all the "Fixed Player Settings" boxes in Player Properties but still control the level of Melee AI in the game (Easy/Normal/Insane) by modifying this trigger. (To be totally clear, this is not about custom AI, it's about changing difficulty of the game's inborn AI).

Make this trigger:

Events
Map Initialization


Conditions
(nothing)


Actions
Player Group - Pick Every Player in (All players controlled by a Computer player) and do (Custom script: call ConvertAIDifficulty(2))

Custom Script: call DestroyTrigger( GetTriggeringTrigger() )



NOTE: As this is, it will make the computer AIs Insane. See that number "2" in there? That means "insane" melee AI. 1 is normal, 0 is easy.

Thus, in the custom script, the NUMBER in the () corresponds to difficulty.

0 = Easy
1 = Normal
2 = Insane


There you go, let me know what you think. I have not tried executing these actions in any other way, but I imagine you could have some kind of event within your map that would be able to modify the AI of computer players.

You could probably also modify the actions to target specific computer players, and with tweaking of upgrades and unit availability use this to change the strategy of the computer AI without having to ever mess with AI in the first place. Just a theory.

Best regards,
ThomasEddington
 
Level 11
Joined
Jun 15, 2016
Messages
470
While it is true that you can change the AI difficulty with triggers mid-game, it is worth noting that doing so will not completely change the AI.

TL;DR: Changing the AI difficulty mid-map will only make the computer build like a more advanced AI, and have shorter delays between attacking.

L;R: At the start of every melee AI there is a call to the StandardAI standard AI function. This call happens only once, at map initialization, and decides several aspects according to the difficulty level, for example whether AI heroes will buy items, whether AI units will prioritize heroes, etc. Here's the full list:

JASS:
function StandardAI takes code heroes, code peons, code attacks returns nothing

    local boolean isNewbie = (MeleeDifficulty() == MELEE_NEWBIE)

    call InitAI()

    call SetMeleeAI()

    call SetDefendPlayer(true)
    call SetGroupsFlee(not isNewbie)
    call SetHeroesBuyItems(not isNewbie)
    call SetHeroesFlee(true)
    call SetHeroesTakeItems(true)
    call SetIgnoreInjured(true)
    call SetPeonsRepair(true)
    call SetSmartArtillery(not isNewbie)
    call SetTargetHeroes(not isNewbie)
    call SetUnitsFlee(not isNewbie)
    call SetWatchMegaTargets(true)

    call CreateCaptains()

    call SetHeroLevels(heroes)

    call Sleep(0.1)
    call StartThread(peons)
    call StartThread(attacks)
endfunction

This function also starts the thread of a peons function to handle peon assignments and build order, and an attacks function to handle attacking. Both functions are infinite loops where AI difficulty decides on certain if/else blocks. In attacks an easy AI difficulty sets a 4 minute delay before any attack at the start of a game, and a 1 minute delay between each attack, and in peons the AI diffuclty decides whether second and third heroes are built, as well as other considerations (see the AI scripts themselves).
 
Top