Auto-translated
Here's something I found on the Heroesportal forum a while ago, and I kept it in a text file. The author is Antonshka:
First, the unit with the highest speed always moves. If there are several, the attacker has priority. If there are several attackers, the leftmost stack has priority. Then, if the speeds are equal, the opponent always has priority, and again, the leftmost stack.
If a unit is waiting, the priority of the last move is in the same sequence, but from the lowest speed to the highest.
A unit that missed its turn has the lowest priority while waiting.
If an enemy unit has a higher speed than your main units, it will enter the attack range of your troops either by attacking or by waiting. The first one has priority. Other enemy units, if there is already one of their stacks in the attack range of your troops, will boldly run towards you, without waiting, even if they cannot reach anyone.
Next, enemy units always go to the nearest stack (calculated by the number of turns, not cells, and based on the current speed, taking into account applied spells). This is the basis for the AI's actions when casting spells. If the AI sees that it can cast a speed spell on the main stack and it will reach, it will cast it. Otherwise, it may cast a missile spell or Stone Skin. Thus, you can force it to cast certain spells by using a decoy.
If there are several stacks within reach, the AI chooses based on the following priorities:
1. Strength of attack (calculated based on the current stack, not on what will remain after the attack). It tries to choose the minimum.
2. It "calculates" whether it will win or lose. If it wins, it starts attacking from the bottom (right stacks, last in the queue). If it loses, it attacks the top stacks. An example of this is using cavalry to eliminate archers. At the beginning, all archers try to attack the bottom stack, and when the cavalry is almost defeated, they "suddenly" switch to the top, fully healthy stack.
3. It also tries to use the properties of units, for example, to block archers or attack with dragons. It casts spells during the attack, for example, Haste.
In general, there are many nuances. I have described the main ones.
There are clear algorithms:
1. A unit goes to the nearest (calculated not by cells, but by the number of turns). That is, if it takes 2 turns to reach one unit and 3 turns to reach another, it will go to the one that takes 2 turns.
2. If there are several units that it can reach in the minimum number of turns, then many factors come into play, which I don't want to explain in detail, such as the ratio of forces of the armies, the attack range of other units, the ratio of attack/defense, the influence of already inflicted damage, and spells. Also, spells have an original effect on two-cell units, such as Wall of Fire. Ballista has a very original effect on harpies, and so on.
P.S. The forum is glitching with the time, it seems like I created the topic, - that's funny :)