1. We calculate the hero's power: Hero_Power = sqrt((1 + 0.05 * Attack) * (1 + 0.05 * Defense));
2. We calculate the hero's troop strength, Creatures_Power: we sum the AI values of all stacks;
3. The hero's army strength is Army_Power = floor(Hero_Power * Creatures_Power);
4. Similarly (see point 2), we calculate the strength of neutral troops, Neutrals_Power (we ignore the appearance of an upgraded unit);
5. k = Army_Power / Neutrals_Power;
6. We calculate the Power_Factor:
Power_Factor = 11 if k >= 7,
Power_Factor = floor(2 * (k - 1)), if 1 < k < 7,
Power_Factor = -1, if 0.5 < k <= 1,
Power_Factor = -2, if 0.333 < k <= 0.5,
Power_Factor = -3, if k <= 0.333
Notes:
1. The number 0.333 = 333/1000 (not 1/3!)
2. The number k is calculated with a small error (division), which may manifest itself at the boundaries of the above intervals.
7. We calculate "sympathy" (0 <= Sympathy <= 2):
Sympathy = 0, if the hero's army does not have the same creatures as the neutrals (or their [up, down]grades),
Sympathy = 1, if the hero's army has at least one such creature (or [up, down]grade),
Sympathy = 2, if the number of such creatures is strictly greater than all the others in the hero's army.
Note: here, magma, ice, and energy elementals are not considered upgrades of earth, water, and fire elementals, respectively, and vice versa.
8. We calculate X (the mood of the neutrals):
X = -4 for Compliant,
X = 1..7 (random integer) for Friendly,
X = 1..10 for Aggressive,
X = 4..10 for Hostile,
X = 10 for Savage.
9. We calculate the hero's "charisma" Ch:
Ch = Power_Factor + Diplomacy_Level + Sympathy
Note. If playing on Easy, Diplomacy_Level is increased by one in the calculations, but is still limited to 3.
a) if Ch < X, the neutrals will fight;
b) if Ch >= X, then
1) the neutrals will join for free if Sympathy + Diplomacy_Level + 1 >= X;
2) if they don't want to join for free (condition 1 is not met), they will join for money, but only if Sympathy + 2*Diplomacy_Level + 1 >= X;
3) if the hero rejects the offer to join, the neutrals:
- will be offended and will fight if Ch = X,
- will try to flee if Ch > X.
4) if conditions 1 and 2 are not met (they don't want to join), the neutrals:
- will fight if Ch = X,
- will try to flee if Ch > X.
P.S. Why is Diplomacy forbidden on random maps (all hostile)?
It is easy to calculate that the probability of neutrals joining for money can reach 86%!
In the archive - a table of AI values, a mini-program to determine the probability of a spell appearing in the magic guild, and a readme file for it.