[size=75]Continuing the Battle AI discussion threads (links to the other topics here). The rules presented here will be split in 3 categories:
**Basic **– should ensure a decent AI until better rules are put in place
**Advanced **– the minimum on the long term, so that VCMI AI is at least as good as the one in H3
**Expert **– sophisticated rules, which should make the computer an worthy opponent

[size=150]Battle AI Part 2 - ARROW TOWERS & WAR MACHINES[/size]

2.A. Arrow Towers
Basic rules (temporary - based on the OH3 logic)

I. First select target based on type: 1) Clones; 2) Shooters; 3) Flyers; 4) Walkers

II. Among each target type, shoot first the one placed on the lowest row on the battlefield. This is the weak H3 logic, so exploitable that I wonder if we should bother ever implementing it - the only argument I can think of being the fact that players are so used with this AI weakness, they might be frustrated it’s not there anymore.

Alternative 1 : Go for some generic rule of avoiding stacks which have less than 20 HP (or less than 1% of the HP of the attacking army), unless they are the only stacks which can inflict damage to the troops behind walls in the coming round.

Alternative 1 : Shoot the enemy stack which can potentially inflict the maximum damage against our troops the next round (based on their attack, damage & stack size) OR the enemy stack which is supposed to get their turn 1st (among those of same target type)

Advanced rules (replacing the above - or, if you prefer, they could be considered from the beginning)

I. General rules:
[ul]1. DO NOT waste the shot of the Main Building on the target, if a Side Turret is still up and can do the job. The damage inflicted by the Central Turret is always double of that of a Side Turret (i.e.: basic values 15 vs 7.5), so it would be a pity to waste the shot on a clone or on the last 2 HP of a 7 lvl creature, when a Side Turret could do the same thing.
2. Ignore stunned creatures (i.e.: Blinded/Petrified/Paralysed/Hypnotised?), if the effect of the spell is normally going to still keep them in place for the round to come (unless the shot could kill the remaining of the target stack  to be seen if this side condition is of notable added value)
3. If AI hero has Artillery, it can use the WAIT on the turrets, combined with using it also on creatures (when the attacker has no means of causing harm during the first half of the coming round). Like this first attacker moves closer (or uses Clone/Teleport/Dispel on creatures which didn’t pose a risk originally), than in the War Machines phase the AI turrets can better asses which of the enemy troops would make a better target. To be used if below conditions are met:
[list]a. Only if the attacker does not have Ballistics skill. So normally the AI should never use Wait on Turrets from the 1st round, because Catapult comes after Turrets and only then AI would be able to see if the attacker has double shots for Catapult or not.
b. Only if the attacker does not have more than 16 Spell Points (enough to cast Earthquake), UNLESS there is a chance to actually defeat the attacker (Turrets + AI’s higher speed defenders) before they get the chance to cast.
c. Only if none of the attacking creatures is in a position to cause AI harm in the coming round, i.e.: only walkers/fliers with no defending creature within range (shooters/casters who exhausted their shots included)[/ul][/list:u]

II. Priority rules. Select target in the following order, with specific rules for each target type:
[ul]1. Clones, but only if they are in position of either draining mana, or inflicting more damage in the coming round, as compared to the amount of creatures from the below categories which could be killed by that same Turret shot (i.e.: don’t waste the shot on 3 Cloned Imps if you can take the last remaining 10 HP of a Dragon).
2. Any high level (7 or more) attacker which is in a position to inflict damage to a defending creature in the coming round, ONLY IF the turret shot(s) is able to kill at least 1 creature in stack.
3. Mana drainers & restorers, in the following order:
[list]a. Familiars (who can drain as much as 6 mana AND transfer them to their hero), but ONLY if AI hero has enough mana to still cast at least 1 spell, or if it has Familiars (or other WoG mana drainers) himself, and enemy has mana to drain.
b. Wraiths (who drain only 2 mana, but don’t transfer to their hero), but ONLY if there’s a chance to destroy the stack this round (including towers still to shoot), otherwise we better go for shooters or flyers
c. Temple Guardian (WoG Commander – restores enemy hero mana), but ONLY if there’s a chance to kill it that round
d. Any other mana draining/restoring creature that might be added by future mods[/ul]
4. Shooters, in the following order:
[ul]a. 1st priority split: first shooters with no range/obstacle penalty, then shooters with penalty
b. 2nd priority split: first shooters with area attack, then shooters with no area attack
c. 3rd priority split: first shooters with no melee penalty, then shooters with melee penalty
d. 4th priority split: first shooters who are not blocked to use their range attack by a neighboring enemy[/ul]
5. Flyers, in the following order:
[ul]a. 1st priority split: first flyers who could make it past our walls in the next round
b. 2nd priority split: first flyers with area attacks, like fire breath or other (if introduced by mods, though don’t confuse these with Dracolich’s Death Cloud, which is actually a range attack, qualifying it as shooter)
c. 3rd priority split: first flyers with most retaliations available (e.g.: Griffin before Pegasus)
d. 4th priority split: first flyers with other abilities (e.g.: no retaliation, resurrect, etc – order to be defined later)[/ul]
6. Remaining creatures (order to be later defined, but for starters can be based on their proximity to defending creatures and speeds)[/list:u]

Expert rules

No others that I can think of for now (the Advanced should go in detail enough), but can be added if we identify situations where the human player could exploit the above rules with the help of a particular spell/creature/move.

Not regarding shooting from, but defending the Arrow Towers, we could think of Expert rules on how to correlate spells & creature attacks so that you can destroy the enemy Catapult and Cyclops, which can destroy your towers.

2.B. Ballista
Basic rules can actually follow the Arrow Tower rules, with one difference though – The Arrow Tower General rule I.1. should be replaced for Ballista with: DO NOT waste a shot of the ballista on creatures placed out of its full damage range, unless it can be used to kill a clone or high level (7+) creature. So if no notable damage can be inflicted on high risk enemy with a penalty shot, then consider only the enemies within full range (and among those, only those which can reach your troops in the coming round).

Advanced/Expert rules to follow.

2.C. Catapult
Rules to follow.

2.D. First Aid Tent
Basic/Advanced rules:
[size=75](I couldn’t draw a line, but you can see which can or cannot be implemented easily)[/size]

  1. Heal the creature which can take maximum HP benefit for Tent’s healing power.
  2. If there is a tie between 2 or more creatures on the above, then also count the number of negative spells which would be removed from the target.
  3. If there is a tie or no match on the above, then filter out the creatures which already wasted their turn and focus the heal on a creature which used Wait (or which were “frozen” by a spell which would not be removed), but ONLY IF it’s in a position to still reach an enemy troop which can still retaliate before the end of the turn.
  4. If a tie/no match on the above, then consider only the creatures which are still in a position of being attacked before the end of the round (i.e.: in the range or reachable distance of enemy which used Wait)
  5. If a tie/no match on the above, then take the creature which still didn’t waste its retaliation(s)
  6. If a tie on the above, take the creature with most retaliations left
  7. If still a tie, then further criteria can be applied: stack strength / size / level (see what makes more sense to you or it’s easier to code)

Light modding:
a. If no creature to be healed, Tent could restore HP to a War Machine. Currently this is already coded as such in VCMI. I’m just not sure what is the priority if more than 1 machine is damaged. Personally I would give 1st priority to the machine which can most benefit from the HP restoration, followed by a right-slot-rule (Catapult>Tent>Ballista>Cart) in case of a tie.
b. If also no machine to repair, then Tent should take a Defense stance. Actually I’m not even sure whether this didn’t actually exist already in H3 (hence it wouldn’t even be modding). If I’m not mistaken, we were able to use Defend on Ballista/Catapult, if we had control of them but didn’t want to use them. So it would generally make sense for a War Machine to go in Defense stance by default if it has nothing to do.

Expert rules will follow, and would probably tackle (among others) the following:

  • Which particular spells are on the creature, enhancing rule #2 above by having a hierarchy of spells we want to get rid of first. It can also be looked at the benefical spells on the creature (i.e.: heal the creature which would be most valuable, which often is the one we bestow more protective/boosting spells on)
  • We can see if certain creature types, stack sizes or positioning on the battlefield (close/vulnerable to the enemy) do not deserve a higher priority, which should override some of the above rules
  • Alignment could be perhaps taken into account (better to preserve the same alignment stacks, than the ‘alien’ one you may have picked to fill a slot, but which reduces army’s morale), in case hero has no Leadership.
  • When WoG secondary skills enhancements will be implemented, new rules will have to be drafted for Tent’s Resurrection ability.

2.E. Ammo Cart
Not Applicable (at least as long as no mod is implemented that would open the possibility of applicable rules)
Battle AI Part 2 - ARROW TOWERS & WAR MACHINES (original draft).7z (11.4 KB)

I always thought it works like it because AI tries to kill creatures with highest power rating first. A stack of 200 cloned enchanters may be more powerful than 4 of them who whill die with regular shot.

Calculate the possible damage that can be avoided:

Can 1 salve (mid damage) kill a dragon?
Can a dragon come into the castle with 1 move?
Is it better to kill weak ranged stacks (bow mans, crossbow mans)?

If the wall is breaked and a big gobo stack is near shot at the gobos. If the wall is ok, shoot the orks.

If a Titan is wounded kill him. If not shoot at the gremlins. Maybe we can create a situation dependent “Danger / Damage” Values (maybe with special abilitys) and shot at the target with the highest values.

@rohezal: very good points. I’ve updated the original post with improved rules. Let me know if you still see rules which leave room for big exploits.

Rules for First Aid Tent added as well now.