Developed by Blade.dkThis 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.
RequiresJASS 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.
Notes- 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!
InitializationFirst 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:
function SetupSkills takes nothing returns nothing
local string h
// Create a local string variable // Paladin // Here we’ll initialise the Paladin’s skills, repeat this for all other heroes
set h =
UnitId2String(
'Hpal')
// Store the returned value of UnitId2String(‘Hpal’) in the local call StoreInteger(udg_GameCache, h,
"BaseSkill1",
'AHhb')
// One of his base skills is Holy Light, store it as “BaseSkill1” call StoreInteger(udg_GameCache, h,
"BaseSkill2",
'AHds')
// Store Divine Shield as “BaseSkill2” call StoreInteger(udg_GameCache, h,
"BaseSkill3",
'AHad')
// Store Devotion Aura as “BaseSkill3” call StoreInteger(udg_GameCache, h,
"UltimateSkill",
'AHre')
// Store Resurrection as his “UltimateSkill” … // Repeat for each Hero.
endfunction
Here’s my InitTrig_AI function:
function InitTrig_AI takes nothing returns nothing
loop
exitwhen i > 11
set i = i + 1
endloop
endfunction
Starting the AI for a heroTo 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:
function AILoop takes nothing returns nothing
endfunction
function StartAI
takes unit hero
returns nothing call AttachObject(m, "hero", hero)
set m = null
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:
function PlayerLeaves takes nothing returns nothing
endif
set p = null
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 somethingWhenever 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:
function AILoop takes nothing returns nothing
local unit h = GetTableUnit(a,
"hero")
…
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.
…
if l <= 0 then
set e = 1.5
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:
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).
function AIFilterEnemyConditions
takes nothing returns boolean endfunction
…
else
if ((o == "smart") or (o == "attack") or (o == "patrol") or (o == "move") or (o == "stop") or (o == "hold") or (o == null)) then
set b =
Condition(
function AIFilterEnemyConditions)
if f == null then
…
else
endif
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.
function AISetItem takes nothing returns nothing
endfunction
function AIItemFilter
takes nothing returns boolean endfunction
function AIHasEmptyInventorySlot
takes unit u
returns boolean endfunction
…
if f == null then
set i =
Rect(x-800, y-800, x+800, y+800)
else
…
endif
…
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.
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:
function AILearnSkill
takes unit h,
string a
returns nothing local integer i = GetTableInt(a,
"LearnSkillOrder")+1
if i == 1 or i == 4 or i == 8 then
elseif i == 2 or i == 5 or i == 9 then
elseif i == 3 or i == 7 or i == 10 then
elseif i == 6 then
endif
call SetTableInt(a, "LearnSkillOrder", i)
endfunction
…
call AILearnSkill(h, a)
endif
…
Now simply make the timer expire again after 'e' seconds:
Last we need to set the local variables to null:
…
set h = null
set i = null
set r = null
set g = null
set b = null
set f = null
set be = null
…
Final notesThis 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!
Blade.dk