Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

Creating simple AIs for Hero Arena-type maps No New Posts Jass Tutorials

Started by

0 Members and 1 Guest are viewing this topic.

Creating simple AIs for Hero Arena-type maps
on: July 09, 2017, 01:42:59 AM

Developed by Blade.dk

This tutorial will help you add a simple, but cool, AI to hero arena type maps.

The AI you’ll learn how to make here will not be perfect. The one we will create here will attack other heroes, pick up items, learn and use spells, but it won’t be as effective as a human player.

However, when you’ve learned the basics you should be able to improve it yourself.


JASS Knowledge – This tutorial uses JASS examples, and JASS-only features, so you will need to know JASS to follow this tutorial and make the AI. Technically it can be done in the GUI, but I won’t recommend that due to memory leaks, tons of unneeded code, and because using return bug and game cache based systems isn’t possible in the GUI. If you don’t know JASS, check the JASS tutorials at The JASS Vault and Wc3Campaigns.
You also need to know what a rawcode are, if you don’t, just search the different Warcraft 3 sites to find out.
A game cache and return bug based system – This can be KaTTaNa’s Local Handle Variables, Vexorian’s CSCache module (a part of the Caster System) or any other similar system.
In this tutorial I’ll use the CSCache module.
This map – A small map I’ve created that shows a simple AI like the one we’ll make in action. It is important that you have this map, as the tutorial often refers to it.

- The AI we’ll make here is not as good as a human player, but better than nothing. And when you’ve learned the basics, you should be able to improve it yourself.
- A lot of numbers (player numbers, for example) starts from 1 in the GUI, but while they starts from 0 in JASS. As this is a JASS tutorial, they start from 0 here.
- You don’t have to do the things exactly like I do them; I do it in my way, but if your way is better or you just feel more comfortable with it, do it your own way. I’m not perfect, and this tutorial isn’t perfect either, but hopefully it will help somebody.
- You could use the AI from my demo map without creating your own (if you do so, give me credit), but I suggest making your own, as maps can be very different, and because you should be able to learn more from making it yourself.
- The demo map is probably not perfectly unbugged, and it isn’t the funniest map either. Remember that it was just a map I quickly created to show a simple AI, if you want to see a better map with a good AI, take a look at Azeroth’s Arcane Arena.
- I would like to specially thank Vexorian for encouraging me to make my first AI for my map, giving me tips on how to do it, and for showing me his AI, which helped me improve mine. Thanks!


First create a new trigger with the “Player - Player 1 (Red) leaves the game” event. Convert it to JASS. We need that trigger to register when a player leaves the game, so we can start the AI for that player. At the moment it will only register when player 0 leaves the game, so we’ll use a loop to make it register when any player from 0-11 leaves instead.

We want the AI to use abilities. This may sound hard, but it isn’t. We just have to make the Heroes learn the abilities, and they’ll cast them themselves.

NOTE: The situation where a computer-controlled hero will cast a spell is always the same situation as where it would cast the spell it’s based on. So if you have a custom spell based on Silence, it will cast it in situations where it would cast it in melee games. NEVER base your spells of the “Channel” ability, as the AI never will use them. Changing the OrderString field on a spell in the Object Editor does nothing, it will still be the same as on the base spell.

To know which spells the heroes have, we create a game cache to store it in.

In the example map my trigger creates a gamecache at map initialization and saves it in the udg_GameCache global variable. Note that the cache HAS to be initialized before we starts using it, so I will do that in the first InitTrig function of my map.

In my map I create a function called “SetupSkills”. In the AI trigger’s InitTrig function I use the ExecuteFunc native (read more about that native here) to execute that function in another thread. This is to prevent the map initialization thread from getting too long, and crash.

My SetupSkills function looks like this:

Code: jass
  1.  function SetupSkills takes nothing returns nothing
  2.     local string h // Create a local string variable
  3. // Paladin // Here we’ll initialise the Paladin’s skills, repeat this for all other heroes
  4.     set h = UnitId2String('Hpal') // Store the returned value of UnitId2String(‘Hpal’) in the local
  5.     call StoreInteger(udg_GameCache, h, "BaseSkill1", 'AHhb') // One of his base skills is Holy Light, store it as “BaseSkill1”
  6.     call StoreInteger(udg_GameCache, h, "BaseSkill2", 'AHds') // Store Divine Shield as “BaseSkill2”
  7.     call StoreInteger(udg_GameCache, h, "BaseSkill3", 'AHad') // Store Devotion Aura as “BaseSkill3”
  8.     call StoreInteger(udg_GameCache, h, "UltimateSkill", 'AHre') // Store Resurrection as his “UltimateSkill”
  9. // Repeat for each Hero.
  10. endfunction

Here’s my InitTrig_AI function:

Code: jass
  1.  function InitTrig_AI takes nothing returns nothing
  2.     local integer i = 0
  3.     set gg_trg_AI = CreateTrigger(  )
  4.     loop
  5.         exitwhen i > 11
  6.         call TriggerRegisterPlayerEventLeave( gg_trg_AI, Player(i) )
  7.         set i = i + 1
  8.     endloop
  9.     call TriggerAddAction( gg_trg_AI, function PlayerLeaves )
  10.     call ExecuteFunc("SetupSkills")
  11. endfunction

Starting the AI for a hero

To control the AI I will use a timer. I create a function called “StartAI” that takes a single unit argument: the hero (check the function in the example map). The function just creates a timer, "attaches" the hero to it, and starts it (just make the expiration function now, we will out some actions into it later, but you need the function and endfunction lines to prevent getting compile errors).

This is the empty AILoop function and the StartAI function from the example map:

Code: jass
  1. function AILoop takes nothing returns nothing
  2. endfunction
  4. function StartAI takes unit hero returns nothing
  5.     local timer m = CreateTimer()
  6.     call AttachObject(m, "hero", hero)
  7.     call TimerStart(m, 0, false, function AILoop)
  8.     set m = null
  9. endfunction

Note that I’m starting it as a “one-shot” timer, by using false as the 'periodic' boolean value (we’ll get back to that later).

Now just make your hero selection system call that function when a computer controlled player chooses a hero, and go to the function that is executed when a player leaves the game. Check if the player has a hero, if he/she has one, call the function that starts the AI on that hero. Example:

Code: jass
  1. function PlayerLeaves takes nothing returns nothing
  2.     local player p = GetTriggerPlayer()
  3.     call DisplayTextToForce(bj_FORCE_ALL_PLAYERS, GetPlayerName(p)+" has left the game.")
  4.     if udg_Hero[GetPlayerId(p)] != null then
  5.         call StartAI(udg_Hero[GetPlayerId(p)])
  6.     endif
  7.     set p = null
  8. endfunction

NOTE: This will make the AI take control of a leaving player's hero, this is not needed, if you want to do something else when a player leaves.

Making the AI do something

Whenever the timer expires there are some things we want it to do:
  •    If the hero is dead, wait until he/she/it is revived.
  •    If the hero is about to die, order him/her/it to move to the fountain at the map center.
  •    If the hero has a fine amount of health, check if an enemy is close. If true, order the Hero to attack it, else check for items close to the hero, if any, issue a smart order so the Hero will pick the up. if there isn’t any, just order the hero to patrol to a random point in the arena.
  •    If the hero is alive and has any unused skill points, learn a skill.

We’ll start with declaring all the variables.  Notice the real variable 'e' in my function, it defines how long time will elapse before the timer expires again, so we can wait shorter time if the hero is dead, or longer time if he/she/it is attacking. That variable is initialized with the value 5.

Declare the local variables:

Code: jass
  1. function AILoop takes nothing returns nothing
  2.     local string a = GetAttachmentTable(GetExpiredTimer())
  3.     local unit h = GetTableUnit(a, "hero")
  4.     local rect i
  5.     local location r
  6.     local real x = GetUnitX(h)
  7.     local real y = GetUnitY(h)
  8.     local group g
  9.     local boolexpr b
  10.     local boolexpr be
  11.     local unit f
  12.     local real l = GetUnitState(h, UNIT_STATE_LIFE)
  13.     local real e = 5

We start with checking if the hero is dead, if he/she/it is, set the real variable to 1.5 (because waiting 5 seconds after revival is too long time, we don’t want that).

The hero’s life ('l' is 0, just set e to 1.5 to make the timer check more frequently for the hero’s revival.

Code: jass
  1.     if l <= 0 then
  2.         set e = 1.5
  3.     endif

Next I check if the hero’s life is below 20% of it's max life. If it is low, order the hero to move to fountain and set the variable 'e' to 3.

The hero’s life is less than 20% of max life, so order the hero to move to the position of the fountain:

Code: jass
  1.     if l < GetUnitState(h, UNIT_STATE_MAX_LIFE)/5 then
  2.         call IssuePointOrder(h, "move", GetUnitX(gg_unit_nfoh_0001), GetUnitY(gg_unit_nfoh_0001))
  3.         set e = 3

If the hero isn’t weak, check if he/she/it has a common order (to prevent it from interrupting channel spells). If it is a standard order, we check if any enemies are within a radius of 500. If true, simply issue an attack order (don’t change the 'e' variable, 5 seconds is fine in this situation).

Code: jass
  1.  function AIFilterEnemyConditions takes nothing returns boolean
  2. endfunction
  4.     else
  5.         if ((o == "smart") or (o == "attack") or (o == "patrol") or (o == "move") or (o == "stop") or (o == "hold") or (o == null)) then
  6.             set g = CreateGroup()
  7.             set b = Condition(function AIFilterEnemyConditions)
  8.             call GroupEnumUnitsInRange(g, x, y, 500, b)
  9.             set f = FirstOfGroup(g)
  10.             if f == null then
  11.             else
  12.                 call IssueTargetOrder(h, "attack", f)
  13.             endif
  14.             call DestroyGroup(g)
  15.             call DestroyBoolExpr(b)
  16.         endif

If no enemies are found, check for items. If an item is found, check if it’s a powerup. If it isn’t, check if the hero has any empty inventory slots, and order the hero to pick it up.

Code: jass
  1.  function AISetItem takes nothing returns nothing
  2. endfunction
  4. function AIItemFilter takes nothing returns boolean
  5. endfunction
  7. function AIHasEmptyInventorySlot takes unit u returns boolean
  8.     return UnitItemInSlot(u, 0) == null or UnitItemInSlot(u, 1) == null or UnitItemInSlot(u, 2) == null or UnitItemInSlot(u, 3) == null or UnitItemInSlot(u, 4) == null or UnitItemInSlot(u, 5) == null
  9. endfunction
  11.             if f == null then
  12.                 set i = Rect(x-800, y-800, x+800, y+800)
  13.                 set be = Condition(function AIItemFilter)
  14.                 set bj_lastRemovedItem=null
  15.                 call EnumItemsInRect(i, be, function AISetItem)
  16.                 if bj_lastRemovedItem != null and (GetItemType(bj_lastRemovedItem) == ITEM_TYPE_POWERUP or AIHasEmptyInventorySlot(h)) then
  17.                     call IssueTargetOrder(h, "smart", bj_lastRemovedItem)
  18.                 else
  19.                 endif
  20.                 call RemoveRect(i)
  21.                 call DestroyBoolExpr(be)

If the hero has items in all slots, or no items existed, order him/her/it to patrol to a random location in the map, to find new targets.

Code: jass
  1.                 else
  2.                     set r = GetRandomLocInRect(bj_mapInitialPlayableArea)
  3.                     call IssuePointOrderLoc(h, "patrol", r)
  4.                     call RemoveLocation(r)

Now let’s check if the hero has any unused skill points (keep this separated from the attack/item pickup/patrol block).

If he/she/it has, call a function that learns a skill to the hero. In my example I’ve used a function that stores the number it has taught the hero an ability, to keep a special pattern in the ability learning:

Code: jass
  1. function AILearnSkill takes unit h, string a returns nothing
  2.     local integer i = GetTableInt(a, "LearnSkillOrder")+1
  3.     if i == 1 or i == 4 or i == 8 then
  4.         call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill1"))
  5.     elseif i == 2 or i == 5 or i == 9 then
  6.         call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill2"))
  7.     elseif i == 3 or i == 7 or i == 10 then
  8.         call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "BaseSkill3"))
  9.     elseif i == 6 then
  10.         call SelectHeroSkill(h, GetStoredInteger(udg_GameCache, UnitId2String(GetUnitTypeId(h)), "UltimateSkill"))
  11.     endif
  12.     call SetTableInt(a, "LearnSkillOrder", i)
  13. endfunction
  15.     if GetHeroSkillPoints(h) > 0 and l > 0 then
  16.         call AILearnSkill(h, a)
  17.     endif

Now simply make the timer expire again after 'e' seconds:

Code: jass
  1.     call TimerStart(GetExpiredTimer(), e, true, function AILoop)

Last we need to set the local variables to null:

Code: jass
  1.     set h = null
  2.     set i = null
  3.     set r = null
  4.     set g = null
  5.     set b = null
  6.     set f = null
  7.     set be = null

Final notes

This is the basics of it, it can be way better, but this should help you get started. Feel free to ask questions here or pm me.

It shouldn’t be complicated at all, but if you have just checked the tutorial it can be so. The map was made to give a better demonstration, so please check it.

When you have finished making your simple AI, try to add one or more of the following things to imrove it:

-   Try to make it find the weakest enemy close.
-   Try to make different AI players work together on killing a specific unit.
-   When most battles becomes centered about the fountain, make heroes run away from it when they’re fleeing.
-   Make the AI post text messages that varies depending on the situation (for example, an AI player can say “Die, sucker!” before killing you).

I hope this will help somebody!



Started by Purgeandfire

Replies: 0
Views: 2646
Tutorial Zone

Started by moyack

Replies: 1
Views: 3232
Site Discussion

Started by REDSEW

Replies: 2
Views: 17969
Site Discussion

Started by Callahan

Replies: 0
Views: 1469
Warcraft III Models

Started by olofmoleman

Replies: 0
Views: 1976
Warcraft III Models
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...