Understanding Tactical AI

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
lukep
Sher'Tul Godslayer
Posts: 1712
Joined: Mon Mar 14, 2011 10:32 am
Location: Canada

Understanding Tactical AI

#1 Post by lukep »

I've looked through the tactical AI code, and I think I have a pretty good idea of how it works. It can be broken down into 3 basic steps:

1: calculating the tactical values of the talents
- This tells the NPC if the talent is available, its talent level, whether or not it hits foes and allies, and other calculations to determine a value for it.
2: choosing a tactic to use
- This step shows whether the NPC wants to escape, attack, heal, or anything else.
3: Choosing a talent for the tactic.
- This chooses the specific talent that will be used to fulfill the tactic.

Step #1 calculates which tactics are available, and how good each talent is at each of them. (Note: this is not quite the order it is done in code, but it's all multiplication, so it's fine)

- It first checks that the talent is available (enough resources, other requirements, in range, etc.).
- It then checks how many and which allies, foes, and enemies it hits
- If the tactic's value is a number, it uses that number, skipping the next step.
- Else if the tactic's value is a table, it checks for resistance and resistance penetration for damage types, or a simple yes/no on a single check of status immunity for status effects. All of the table's entries are calculated separately and then added together for each actor affected. This can affect the next step as well, making hitting an immune ally not matter at all.
- It then runs it through friendlyfire/selffire for how much ally and self compassion you have (default is 1 for ally, 5 for self, so if you hit 6 enemies and yourself, it is as good as hitting just 1 enemy)
- Multipliers are then added for talent level (+20% per level), if it is instant (x10), and if the player has modified the talent's weight (as in the control summon menu)
- If you are currently reloading, everything is set to 0.
- If it is above 0 so far, a random number up to 0.9 is added to the result.

The result is used to determine if a given tactic (eg. Heal, Attack) is available, for calculating the value of Attack, Attackarea, and Disable, and for selecting which talent to use at the end (step #3).

Step #2 chooses the tactic to use. There are 16 tactics, each with a unique way of calculating its weight. Most of them require that a talent is available with its tactic, with escape being the exception.

Heal: Value ranges from 3 (at <80% HP) to 10 (at <20% HP), modified by self_compassion. Even if you do not have any healing talents available, it is calculated and used for other tactics.
Mana: Value ranges from 0.5 (at <100%) to 4 (at <20%)
Stamina: Value ranges from 0.5 (at <100%) to 4 (at <20%)
Vim: Value ranges from 0.5 (at <100%) to 4 (at <20%)
Equilibrium: Looks like it is bugged. Value is 0.5 when fail rate >10%
Paradox: Looks like it is bugged. Value is 0.5 when fail rate >10%
Ammo: Value is 0 if currently reloading, or 0.5 if <100% ammo, or 10 if 0 ammo.
Protect: Only for summoned creatures. Value is same weight as Summoner's want for healing.
Closein: Must have Closein as a tactic, and only at range 3 or more. Value is 1 + (0.5 * distance)
Escape: Available with no talents. Value is (heal value * 0.5). If you have a safe_range in your AI, and the target is closer than that, add (your safe range * 0.5)
Surrounded: ??? value is number of foes in range 4?
Defend: Must be below 80% HP. Value is 1 + (0.5 * heal value) + (0.5 * foes seen)
Cure: Value is (number of detrimental status effects)
Attack: Value is the tactical AI value of your best attack talent, after it has been run through step #1.
Disable: Value is the tactical AI value of your best disable talent, after it has been run through step #1 added to (attack value)
Attackarea: Value is the tactical AI value of your best attackarea talent, after it has been run through step #1.
Buff: Only available if you have an attack available. Value is (0.5 + attack value)

Each of these 16 values are then slightly randomized ( multiplied by 2, then add a random number up to 0.9), and multiplied by the AI weight (eg, higher defend weight for tank, higher closein for melee etc.). The highest value is the tactic that will be used in step #3.

For step #3, the available talents are searched for the selected tactic, and the one with the highest value one is chosen. If Escape is chosen with no talents, it runs away (but doesn't break LoS?)

Examples of good tactical talent values (Note: the values of Attack, Attackarea, and Disable are the only three that affect which tactic is chosen, the others are simply compared to other talents in the same category)

Attack: {weapon = 1} for default melee attack, {FIRE = 2} for a medium spell, 10 for necromancer summoning
Cure: should usually be a function, as the tactic can not tell the type of detrimental status effects apart (physical, mental, magic).
Heal: 1 for a weak heal, 2 for a normal one, 3 for a very strong one.

The rest are basically 2-3, depending on how good it is at a particular thing.

Hope it helps.
Some of my tools for helping make talents:
Melee Talent Creator
Annotated Talent Code (incomplete)

nate
Wyrmic
Posts: 261
Joined: Fri Jan 18, 2013 8:35 am

Re: Understanding Tactical AI

#2 Post by nate »

Thanks lukep, that's exactly what I was looking for!

It seems like a lot of escapes request a target (for instance, controlled phase door)-- how does the AI handle that?

What about traits like direct_hit and requires_target? Those seem like AI flags, but I'm not sure.

And the resource flags-- those direct the AI to use those talents when they need resources?
Proud father of Fx4fx and Chronometer add-ons; proud mother of Fated add-on

lukep
Sher'Tul Godslayer
Posts: 1712
Joined: Mon Mar 14, 2011 10:32 am
Location: Canada

Re: Understanding Tactical AI

#3 Post by lukep »

nate wrote:It seems like a lot of escapes request a target (for instance, controlled phase door)-- how does the AI handle that?
I'm not sure, but my guess would be "poorly". It would still work well in cases such as Disengage, though.
nate wrote:What about traits like direct_hit and requires_target? Those seem like AI flags, but I'm not sure.
I'm not sure about direct_hit (it makes the AI treat it as a hit instead of a bolt, but that may already be covered by the targeting, and it may do other stuff as well), but requires_target is definitely more than just an AI flag. It makes it so that the talent only works when it targets an actor, for example, Disengage, or many melee attacks.
nate wrote:And the resource flags-- those direct the AI to use those talents when they need resources?
Yes. For example, manasurge runes have tactical info of { MANA = 1 }, meaning that they may be used when "mana" is selected as the tactic (in step #2)
Some of my tools for helping make talents:
Melee Talent Creator
Annotated Talent Code (incomplete)

Post Reply