Generic Pet AI

The "Generic Pet AI" is a monster AI that is intended to be used for a player character's pets. It provides a more responsive experience to the existing pet AIs while improving customizability.

Note that there is still some of that "Diablo 2 Feel TM", which is both on purpose and a result of the engine. Much of it was based on the existing shadow master and hireable AIs.

Modders: Any cases not in the vanilla game will not have been tested, so certain functionalities might not work. Also, while it is intended to be used with pets, there should be nothing stopping it from being used for normal monsters (this case was not tested, however).

Params

These params can be customized in monstats.txt#aip#

Behavior Flags

Some behaviors can be customized using aip2(H)/Behavior Flags. This is an integer value where each bit represents a boolean and is checked using the & operator. For example, if the value equals 5 (binary = 101) then that returns true for both the 4 (binary = 100) and 1 (binary = 1) bit field values.

Following

The pet chooses to follow the boss if either of the following conditions are met: The distance between it and its boss is greater than aip1/Leash Distance or the pet failed to choose a skill for combat.

The pet then chooses one of the following follow cases and performs it. The conditions are evaluated in order of appearance in the table.

Case Condition Behavior
Warp The distance between the pet and its boss is greater than aip1(N)/Warp Distance. Try to warp to a square just offscreen of the boss's location. Then try to warp to a square in the boss's room. Then try to warp directly onto the boss.
Close Distance The distance between the pet and its boss is greater than 10 squares (hardcoded) or the pet and its boss are in different logical rooms (a logical room is basically a tileset). Always run if the pet has that mode. Apply aip1(H)/Close Distance Velocity Bonus as a percentage bonus to velocity. Try to move to a square near the boss's predicted location. Then try to move to the boss's exact location.
Follow Cursor The pet's boss is moving. Always walk. Try to move to a square near the boss's predicted location, prefering squares in the direction the boss is moving. Then try to move to a square near the boss's exact location. Then try to move between the pet and the boss's exact location. Then try to move to our boss's exact location.
Boss Warped The distance between the pet and its boss's last warp location is less than 6 squares (hardcoded). Always walk. Try to move away from the boss's last warp location. Then try to move away from the boss's exact location.
Push The distance between the pet and its boss is less than or equal to 2 squares (hardcoded). Always walk. Try moving to a random square near the boss. Then try moving away from the boss. Then try moving towards the boss's predicted location.
Ambient The pet's aip2/Ambient Wander Chance is greater than 0. Always walk. Only move if a random percent is less than aip2/Ambient Wander Chance. Try moving to a random square near the boss. Then try moving to the boss's predicted location.

After choosing a follow case and performing it, an ai update is scheduled for 2 seconds in the future. This differs from other AIs which don't perform an ai update until the end of the path is reached. The result is a more responsive AI.

Combat

The pet chooses violence if the following condition is met: The distance between it and its boss is less than or equal to aip1/Leash Distance and it is not in town.

Targets

If the pet doesn't have a persisted target then it will find a new one. Target finding is a multi-step process. First, the pet will find a set of targets based on the skills it has. Second, the skills then choose a target from that set to use. Third, when a skill is chosen, its target becomes the pet's target. The set of target types is as follows:

Target Type Condition Behavior
Melee skills.txt#aitype set to Melee, Steal, or Move Attack. A target is always chosen, but when comparing two targets, the following conditions are used in order:
  1. Target is in sweet spot and is the boss's target.
  2. Target is in the sweet spot and is the highest priority target. See High Priority Targets.
  3. Target is the closest non-swarmed target. See Swarming.
Sweet spot: Distance between the pet and the target is between aip5/Melee Sweet Spot Min and aip5(N)/Melee Sweet Spot Max.
Ranged skills.txt#aitype set to Ranged. A target is always chosen, but when comparing two targets, the following conditions are used in order:
  1. Target is in sweet spot and is the boss's target.
  2. Target is in the sweet spot and is the highest priority target. See High Priority Targets.
  3. Target is the closest target in the sweet spot.
Sweet spot: Distance between the pet and the target is between aip6/Ranged Sweet Spot Min and aip6(N)/Ranged Sweet Spot Max..
Area skills.txt#aitype set to Summon or Area. Divide the targetable area into chunks. Target is the closest target in the closest chunk with the most total targets.
Corpse skills.txt#TargetCorpse is set. The closest unused corpse to the pet.
Near Boss skills.txt#aitype set to Teleport or Move Attack. The closest target to the boss.

Swarming

Swarming is an opt-in behavior that loosley manages how many pets can choose a unit as their target. Swarming only takes into account the pets that are using the Generic Pet AI and have aip2(N)/Swarm Cutoff greater than 0. A target is considered swarmed if the amount of pets targeting it is greater than aip2(N)/Swarm Cutoff.

High Priority Targets

Non-normal targets can be considered "high priority" and have a higher likelihood of being chosen as a target. This adds the target's elite type from the below table to the value in the target's monstats.txt#threat (or 14 if they are a player). This functionality can be disabled by enabling aip2(H)/Disable High Priority Targeting.

Elite Type Value
Normal 0
Champion 1
Unique 2
Super Unique 3
Boss or Prime Evil 4
Hostile Player 5

Skills

Gathering Skills

The set of skills the pet can use is gathered based on the value in aip3/Skill Gather Type. If aip3(N)/Allow Skill Bonuses is set, then level bonuses from items are included.

Gather Type Value Description
Monstats 0 Gather only the skills set in monstats.txt#Skill#.
All Skills 1 Gather all skills non-item skills on the unit. This is for skills added by other logic that are not present in monstats.txt#Skill#, commonly skills.txt#sumskill#.
All Skills with Items 2 Same as "All Skills" except it includes non-charged skills granted by items.
Mode Attacks

A "Mode Attack" is an attack not tied to a skill. They can be added by filling out aip4/Mode Attack Mode with a monster mode and aip4(N)/Mode Attack AI Type with a skills.txt#aitype. Only certain aitypes are supported to be mode attacks, see Rating Skills.

Rating Skills

A skill rating is generated for every gathered skill. The base rating is "(skills.txt#reqlevel / 4) + skill level + skills.txt#aibonus" or aip4(H)/Mode Attack Bonus Rating for Mode Attacks. The rest of this section describes all the modifications to that rating.

A skill can get set to "perform the skill now" or "skip" ratings. Internally this sets the skill rating to integer max and min respectively. There are also pre-set "rating boost" values that are mentioned throughout this section. They are:

Rating Boost Value
High 20
Medium 10
Low 5

If the skill is skills.txt#progressive and has max charges, skip it. Otherwise add a rating bonus of aip7(N)/Progressive Bonus Rating.

If the skill is skills.txt#finishing and has progressive charges, add a rating bonus of "total charges * (Low rating boost / 2)". Otherwise subtract a Medium rating boost.

Modify the rating by the amount of resistance the target has to skills.txt#EType.

Resist Value Rating Boost
>= 100 -High
>= 66 -Medium
>= 33 -Low
<= -33 +Low
<= -66 +Medium
<= -100 +High

Modify the rating based on the skill's skills.txt#aitype:

AI Type Target Type Behavior Mode Attack
Buff None Can only be chosen when finding a new target. Find an allied target that doesn't have skills.txt#aurastate or any other states in its states.html#group active. First check the pet. Then if skills.txt#TargetAlly is set check allied players, starting with the pet's boss. Then check all of the boss's pets if skills.txt#TargetPet is set. Perform the skill now if a target is found. No
Debuff None Skip if the pet doesn't have or is just aquiring a target. If the target doesn't have skills.txt#auratargetstate or any other states in its states.html#group active, perform the skill now. Otherwise skip. No
Summon Area Skip if the pet's monster class is equal to skills.txt#summon or its pet type is equal to skills.txt#pettype to prevent summoning itself. Skip if there are skills.txt#petmax amount of skills.txt#pettype pets, otherwise subtract a Medium rating boost if the amount of pets is half the max.
If there are less than three targets on screen, subtract a Medium rating boost, otherwise add a rating bonus of "(total targets - 3) * (Low rating boost / 2)". If there's a target in melee range, subtract a Medium rating boost.
Add a random bonus rating up to aip7/Random Bonus Rating.
Yes
Melee Melee If the target is within "aip5(N)/Melee Sweet Spot Max / 2", add a Medium rating boost. Add aip5(H)/Melee Bonus Rating.
Add a random rating bonus up to aip7/Random Bonus Rating.
Yes
Ranged Ranged If the target is within "aip5(N)/Melee Sweet Spot Max / 2", subtract a Medium rating boost. If there's a target in melee range, subtract a Low rating boost. Add aip6(H)/Ranged Bonus Rating.
Add a random rating bonus up to aip7/Random Bonus Rating.
Yes
Area Ranged Area Same as Ranged. Yes
Aura None Skip if the skill is equipped to the right hand, otherwise perform the skill now. No
Teleport Melee,
Ranged
When aquiring a target, calculate the rating for both the Melee and Ranged target. Try the Melee target first if the pet prefers melee ("aip5(H)/Melee Bonus Rating > aip6(H)/Ranged Bonus Rating"), otherwise try the Ranged target first.
If health is greater than 66%, try to get closer to the target. If the pet prefers melee, and the distance to the target is greater than "aip5(N)/Melee Sweet Spot Max / 2", then add a High rating boost. Otherwise if the pet doesn't prefer melee and the distance is greater than aip6(N)/Ranged Sweet Spot Max, add a High rating boost and target a square halfway between the pet and the target.
If health is lower than 66%, try to teleport away. If the pet has a boss, set the target to the boss. Otherwise, set the target to a random square within 20 squares. Add a Low rating boost. If health is less than 33%, add Medium rating boost.
Add a random rating bonus up to aip7/Random Bonus Rating.
Yes
Heal None Don't skip if health is less than 66%. Can target the boss under the same conditions if skills.txt#TargetAlly is set.
Add a High rating boost. Add a second one if health is less than 33%.
Add a random rating bonus up to aip7/Random Bonus Rating.
Yes
Steal Melee Skip if the target is a monster and its monstats.txt#Drain is less than 25%. Add a Medium rating boost if health is less than 50%, otherwise add a Low rating boost if health is below 75%. Add the same bonuses as Melee. Yes
Move Attack Melee,
Near Boss
When aquiring a target, use the Near Boss target and add a High rating boost if the boss has less than 4 targets near them and the pet's health is less than 50% or there are more than three targets near the pet. Otherwise use the Melee target.
Skip if the target is in melee range. Add a Medium rating boost if the target is outside of aip5(N)/Melee Sweet Spot Max.
Add a random rating bonus up to aip7/Random Bonus Rating.
Yes
Resurrect,
Passive
None Always skip. No

Performing a Skill

After all skills are rated, the pet chooses one to perform. How it chooses a skill is determined by aip3(H)/Skill Choose Type.

Skill Choose Type Value Behavior
Highest Rating 0 Try to perform the skill with the highest rating. Continue to the next highest until a skill is performed.
Remember that "perform the skill now" skills always have the highest rating. Some randomness can be introduced by using aip7/Random Bonus Rating.
Roll 1 Perform any "perform the skill now" skills. Otherwise, sum all of the ratings and roll a value between zero and the total. Try to perform the skill on that value. If that fails, try to perform the mode attack or the "attack" skill (the first skill in skills.txt), if either exists.

When a skill is chosen to be performed, first see if any repositioning is needed. If a repositioning occurs, then persist the skill and target until the next ai update (see Persisting Targets and Skills). This is based on the skill's skills.txt#aitype:

AI Type Behavior
Melee,
Steal
If the target is outside of melee range, move towards it. Run if the pet has the run mode and the distance is greater than 6 squares (hardcoded). Skip this skill if pathing failed.
Ranged,
Area Ranged
If the distance to the target is greater than aip6(N)/Ranged Sweet Spot Max or there is no line of sight to the target, move towards the target. Run if the pet has the run mode and the distance is greater than 6 squares (hardcoded).
If the distance to the target is less than aip6/Ranged Sweet Spot Min, move away from them.

If no repositioning is needed, then the skill is performed!

Persisting Targets and Skills

After a target is chosen and is an enemy, the pet focuses on it until it is no longer valid (dead, too far away, etc.). The target is saved off so it can persist into the pet's next AI update. Allies are not persisted.
The target can stay persisted up to 5 (hardcoded) times, upon expiry a full search is issued to reselect the target with the highest priority.

Skills can also be persisted. If a repositioning occurs the skill is saved off so the next AI update can try to use it again. Also, if the skill was just performed and has skills.txt#repeat set, it can be persisted up to 25 (hardcoded) times.