Everything regarding how the AI works, what is new, etc, will be posted here. Note that this all of this information is for Caster of Magic only, 1.50 AI generally only has a small portion of these upgrades.
Overland spell selection
Overland unit movement, part I - AI turn
Before any AI's turn, all AI players disband their obsolete units simultaneously if, and only if, the global max unit cap of 2000 is getting near. This was updated in 2.4, on top of disbanding units weaker than a certain threshold (I think it's set to one third of the average of the army), they also disband a fixed amount proportional to their army size: 1 unit for each 100 they own. Spirits, settlers, heroes, ships, engineers, etc cannot get disbanded this way, and higher level units and buffed units are worth more. This part of the code is not rocket science but should be enough to slowly replace crappy halberdiers with awesome sky drakes without ramming into the unit cap head first. In most games the cap will not be reached anyway.
The AI starts their turn with their spell being cast. If complete, they start a new spell. In 1.40 and below vanilla, they did not proceed with that spell, and left it for next turn even if they had enough skill to complete it. In CoM and 1.50 they proceed and keep casting while they can. Doing this first ensures anything they cast will be considered when deciding what to do.
(actually they do a few stuff prior, like evaluating continent values, selecting a target continent, updating hostility etc)
Then they decide how to distribute their power base to mana/power/reaseach, and use alchemy if needed.
Then they set tax rates and farmers: 2.5 will bring a major update to this part and makes the AI able to set these up to way that's reasonable and safe. Finally they set production in towns and then proceed to the main part, assigning orders to units.
Once the procedure to assign orders is completed, all units perform their assigned orders (move, attack, whatever) and the AI ends their turn, the next AI's turn starts. This is new for the mod and the 1.50 patch: In the vanilla game, first all 4 AI players assigned orders, then all 4 executed those moves, so they effectively had their turns simultaneously, without being aware of what the wizard earlier in the turn order did: resulting in attacking each other when not intended or failing to do so when intended, as the other wizard's units were moved.
Also only in the mod or 1.50, the AI starts by cancelling all existing orders on all units - except settlers and engineers, those would not work as intended otherwise - and assigns new orders, most likely to continue whatever they were doing, but if circumstances change they might not. In the original, once the AI assigned an order to a unit it was carried out, and could not be interrupted: A unit sent to attack an enemy city would still do so 20 turns later when reaching it even if the city is no longer there or peace was made.
Overland unit movement, part II - Assigning unit orders
This consists of several steps executed in a set order :
1. Any unit the AI cannot pay maintenance for are disbanded immediately. The AI disbands their worst units first. The AI is not subject to normal maintenance rules: if they fail to actually pay maintenance, they do not lose units - the game assumes this isn't possible in the first place since they disbanded any excess already. Insecticide improved this procedure by allowing the AI to keep their units if their income was negative but they had enough gold/mana to still pay for them.
2. AI stacks that are standing in a tower on the home plane of the AI switch to the other plane if able. The mod adds the ability to the AI to swap planes for units standing on an Astral Gate as well: in this case the city must have at least 6 units remaining, and the capital cannot be left this way. Furthermore the AI can swap planes with Plane Shifting or Planar Travelling units as well (item power not included). In all cases, movement happens from the home plane to the "other" plane. The AI goes with the assumption that they need more forces on the plane where they not started from, which is 99% correct and making it smarter would be close to impossible.
3. Any AI ships on land (usually produced in a city) are ordered to move onto the nearest sea tile.
4. Next all AI units patrolling near a poorly defended city cancel their orders. Since the orders are always cancelled on all units for the mod, this no longer serves any purpose, nor does any harm.
After this the following are done for each continent, one continent at a time :
5. The AI makes a list of stacks they have available on the continent, only idle units are considered in these but since they now reset orders each turn this is generally all of them.
6. Any melding, road building or outpost creating unit is sent out of any city.
7. (updated in 2.9) Any unmelded node occupied by own or no troops pulls the nearest idle spirit towards itself.
8. Any settlers are immediately ordered to build a town if able, otherwise they are sent towards a settling location of preference.
9. Then any units that can purify are sent to do so if needed.
10. Any engineers available are ordered to either move to the closest city, or to build a road from that tile to the nearest unconnected city. The engineers are now able to build around obstacles instead of trying to build through them.
11. Next, the AI builds a list of all available targets on the continent: enemy fortresses, cities, units, nodes, lairs, towers. in this order. Unfortunately this list is limited to 25 things, on very large continents the AI will be unable to add to the list and will overlook anything that would be near the end of the list: towers, lairs, etc. Fortunately, if their is enough enemy presence to fill the list, the AI should most likely ignore nodes and lairs and should not send their troops away to the other plane in towers, so this is not particularly bad. The only possible problem is when there are more than 25 stacks of enemy units and the AI might not notice, and fail to attack something that would be better as a target, still with 25 targets, it's very likely something will be attacked and killed, reducing the count and allowing other stuff to get on the list. Unfortunately, extending the size of the list is impossible.
12. The AI goes through any remaining stack that's still idle,
(new in 2.9) and checks if there is Jihad hostility towards anyone. If yes, the stack is at least as strong as 9 halberdiers, and the capital is on the continent and reachable, they are ordered to move there. During Jihad hostility, enemy troops are not considered roadblocks and the AI will try to move through them - though considers these tiles more expensive to enter than mountains. During any other level of hostility, enemy troops are considered roadblocks.
...and sends each to the highest (priority/distance) target that is weaker than 1.33 times the power of the AI's stack to attack it then erases the target from the list by setting its priority to -1. In the mod, the target is instead retained but set to the lowest priority of 1, also targets with (priority/distance)<1 will be ignored unless distance is less than 5 to avoid sending multiple stacks from far away locations to the same target, however multiple stacks from a nearby location are fine. This not only makes the AI able to attack the human player's city with multiple stacks which it wasn't able to do originally, as well as speeding up transport through towers, but if the units manage to actually reach the tile in the same turn, the two stacks merge together as a side effect, helping the AI to build up larger stacks in multiple locations without having to rewrite the entire stack building process.
13. Any stacks still left over will be sent towards the "main action continent" if they are able to move through water, from any continent that is not "under contest with an enemy". In the mod, directly prior to doing this, the stack will check if there are any viable military targets nearby: while "12." covered targets on the continent itself, doing this allows the AI to perform intercontinental or land to sea attacks which were impossible without this addition. This is probably the largest addition to the AI in terms of code size (and maybe even importance). In the 2.5 update this was changed to not only consider units as targets but cities as well, which does not matter here, but does if called from "18." instead. In 2.9 the stack first checks for Jihad hostility, if the capital of any player whose assigned hostility level is Jihad is on the same plane and the stack is at least as strong as 5 halberdiers, the stack will move to attack that capital. In 5.0 and later, this now uses identical target ratings as land at based attacks, including lairs.
14. Any remaining unit on a continent rated as "no target to attack" or "entirely owned" will go to shore and attempt to board a ship. If no ship is present, it'll mark the location as a "ship required point".
15. The AI will now look through any remaining, still idle stacks and makes a list of units that can be used for building a new stack. The best 9 such units are selected. This was originally done between 11 and 12, which is a major flaw, since the list is used in 16, so units on the list end up with conflicting orders: they'll both be sent to the stack building point and to attack targets/board ships/whatever. This can tear already built stacks apart by pulling some of them back to build a new one, too.
The AI will be able to select any amount of units from any stack not standing on a strategic point. If they're on a node, at least 3 units need to remain but the rest can be used. In cities, at least 6 units need to be present and at most 2 can be sent for the first 50 game turns. After that however, at most one unit can leave the city per turn and only if it has 9 units, also units in the capital can never leave. While this leaves the units on the way vulnerable, it ensures the city is not going to get lost, which is far more important. Always the 9 most valuable units (highest military rating) will be added to the list in order of value.
It's important to mention here that although it's not an actual stack building mechanism, when the AI produces units in a city that's full, always the weakest unit will be pushed outside to ensure the city defenses remain appropriate. However, if these units are not chosen later for stack building due to better units being available, they'll stay there and accumulate, effectively forming a stack on the spot automatically without AI interference, and these stacks are much less vulnerable to getting picked off one unit at a time prior to forming, since no actual movement is needed to be done by units.
16. The AI counts the number of units at the intended stack building tile, and sends as many there to fill up the count to 9 (or the intended lower amount). The mod also changed this, the original send units in excess of 9, which is a problem: if the two stacks cannot merge together due to having over 9 total units, they'll not move, and no unit will be added at all. The stack building tile is the center of their empire on the continent, or the shore if the AI intends to leave the continent.
17. Finally, the AI checks their own cities and nodes, and if there aren't enough defenders, sends any remaining, still idle units there. The AI considers nodes as needing more defenders up to 9 units even though they are also allowed to leave no more than 3 in their if those units are needed to build stacks: this ensures no valuable unit like a hero is stuck on eternal node guard duty, instead new units will be added to the guarding stack, and the valuable units will be able to leave it to build a new stack.
18. After all the continents are done, the AI checks for units over sea. If they are not capable of moving on water, they are disbanded (drown), since the original game did not contain a real drowning check. This is now unnecessary since they would drown on their own anyway but this way they cannot move and do stuff like attacking someone before drowning. Then, any remaining unit is sent to the main action continent. However, in the mod, units first check for a potential target, using the same procedure as "13.", allowing the AI to launch a sea to land attack on a nearby target.
19. Finally, any still remaining stacks on water are checked for ships. Stacks containing ships and units head towards the selected shore tile of the main action continent. Ships already on shore of that continent send the units to land as well as ships on shore of any continent marked "unoccupied by anyone/interesting" so passing by another interesting continent on the route might make the ship stop and unload the troops to explore a new continent. Stacks containing ships and settlers are sent to the "settling continent" instead the same way, if such is not available, they head to the main action continent instead. Stacks containing only ships look for the list of "ship request points" and send one ship to each such point. This procedure had a whole ton of bugs, safe to say at least two dozens in the vanilla game. Finally which the vanilla game did not do but the mod does: any remaining, still idle ship stacks will look for any valid naval target on see to attack. Those that can't find any, will more semi-randomly one tile to a direction based on turn order, which keeps the ships roughly at the same spot but without staying on the same tile. This is critically important since the AI insists on sending their ships to their selected unload points, but never recall them, blocking any further attempts to move ships there to unload. The ships have to be moved away randomly to ensure this does not happen. Another side effect of this is, if a shore is nearby the movement of the ships, held up by the land tiles, will despite being synchronized always moving the same way, result in the ships gathering up in a larger stack. This lets the AI "build" a massive stack of ships that can be used for naval battles very effectively.
20. Starting in 5.0, there is an additional step at the end - the AI will pull their best 9 water movement capable units together, excluding units that already have an attack order. This allows the AI to build a powerful intercontinental doomstack. More details in the thread where this feature was announced.
Other AI overland actions
Buying in cities
Design concept: Rush buying any specific building or unit over others is rarely a relevant difference - it merely allows the town to produce the next item (spent gold/2) production earlier. Since the AI selects what to build on weighted random priorities, speeding up a particular city over others is not providing a significant benefit. Even speeding up a major unit production center does not, as the AI lacks the ability to comprehend "I need more of these units" or "I don't need more of these units". For every case when the massive hordes of incoming Minotaurs or Paladins is helping the AI push ahead and win, there will be a case when it merely helps prolong a war between two AI of even strength, wasting both their economic potential, or worse, the units might be made in a long period of peace when they have nothing to do at all - and might not have even as long as the final war over Spell of Mastery.
Due to this, the AI should use the "BUY" button in two cases: 1. when they have an exorbitant amount of money they have absolutely no reason to keep around, or 2. if the bought building pays returns the investment by produced resources, or accelerates the production of the first few quality troops (either for the city or in general).
Currently defined rules :
-do not buy if can't pay the cost (obvious)
-If gold>10000 then buy
-Is sawmill then buy
-if gold>=50*cost to buy then buy
-if Sage's Guild, Amplifier Tower, Wizard's Guild, Alchemist's Guild or Fighter's Guild and gold>=2*the cost then buy.
This procedure was completely remade in 4.0 and is now more detailed. The AI will generally focus on military spending in adamantium/mithril cities, and cities present on mostly enemy controlled continents.
Alchemy
Design concept: Alchemy is a tool to convert resources in emergencies, unless the wizard has the Alchemy retort, in which case it can be used for generic purpose as well.
Rules:
-If casting the Spell of Return, convert all gold into mana every turn
-If alchemist, before turn 30, convert into having a ratio of 7 gold to 3 mana.
-If alchemist, after turn 30, convert into a ratio of 8 mana to 2 gold. If this would result in more than 24000 mana, make 24000 mana and all the rest as gold. If this would result in more than 30000 gold, put the leftover into mana again.
-if not alchemist and mana<gold/16, convert half of the available gold.
-If not alchemist, 10% chance to convert half of the gold if gold>4*mana. Skip if gold<100.
-If not alchemist, 10% chance to convert half of the mana if mana>8*gold. Skip if mana<100.
Power Distribution
-If time stop is being cast, set 100% mana, otherwise
-if sorcery conjuction and sufficient mana then set 90% skill 10% mana, otherwise 50% skill 50% mana, otherwise
-if spell of mastery is being researched, neither of the below conditions that would null research priority are true, and we could research it in fewer than 50 turns by that setting, set 80% research, 20% mana if sufficient mana, otherwise set 60% mana 40% research, and improve relations and peace interest with every other wizard. Otherwise
-if insufficient mana, set 100% mana, otherwise
-if casting spell of mastery already, set 90% mana, 10% skill
-if below turn 60, and researching Giant Spiders, Gargoyles, or Lycanthropy, set 80% research 20% mana
-if below turn 30, we have fewer than 250 mana, set 100% mana but if fewer than 26 casting skill, and over 100 mana, set 25% skill 75% mana instead.
Default priorities: 4 research, 2 mana, 4 skill
-if spell of mastery is being researched and someone else who is not banished has spell blast, lower research priority to 0
-if spell of mastery is being researched and we are at warlike hostility with the human player and we aren't in the top 25% of the historian chart, research priority to 0
-if theurgist, research priority +3
-if perfectionist, skill priority +3
-if militarist, mana priority +2
-if peaceful personality, research priority +3
-If time stop is known, mana priority +10
-if research behind the human player by 25% number of spells, or fewer than 15 spells known: research priority +3
-if casting skill does not exceed the human player by 50% or is below 80, skill priority +2, but only if the above wasn't true
-during war, both of the above are ignored, as they are overridden by the "more summoning needed" global tactic
-if sage master, research priority +3
-if archmage, skill priority +3
-if mana focusing, mana priority +2
-if chaneller, mana priority -1
-if alchemist, mana priority -2 (but can't go below zero)
-if own enlightenment in effect, halve research priority
-if spell of mastery is being researched and no one knows has blast, we are in the top 1/16 of the historian chart, it's past turn 200, and human player is below 10/16 height on the historian graph, research priority +5
-if casting skill>500, halve skill priority
-if not researching a spell, set research priority to zero
-if over 20000 mana crystals, set mana priority to zero
-set 20% mana, and distribute the remaining 80% randomly weighted by the above priorities.
Taxes
Design concept: The AI should aim for the highest tax rate they can get without losing too much production to rebels. Only the overall empire is considered, even if a single city or two starves, it's acceptable if the others can pay the high tax without rebels.
Rules :
-Overall rebel rate of empire population<10%: Increase tax rate by 1.
-Overall rebel rate of empire population>30%: Reduce tax rate by 1.
Food
Design concept: The AI does not actually have a desertion check - instead, they voluntarily disband any units they can not afford to maintain during their own turns. This means, the AI can never lose units due to conquered cities, and does not need to produce additional food, only the amount actually consumed.
Rules:
-go through each city one at a time. If total food is too low, add a farmer, assuming it produces 1 food. If total food is too high, remove a farmer and assume it has taken away 3 food. Change only once per city. If all cities are done, start from the first one again. Keep going until produced food equals needed amount+2.
Diplomacy
See the Diplomacy section in the wiki for MoM 1.50. CoM's diplomacy system is mostly identical. (some numbers, especially difficulty modifiers, have been tweaked)
There is one new addition: when the human player uses disjunction, spell binding or spell blast against an AI, it will trigger a Diplomatic Reaction with the appropriate warning message and penalty, proportional to the cost of the spell lost.
Overland spell selection
The AI first assigns weights to each category and decides on a category of spells to cast randomly based on these weights, then selects a spell from within the category, again by assigning each spell in the category a weight and selecting at random.
Summoning
All overland summoning spells belong to this category, plus summon hero, summon champion, summoning spirits, resurrection, incarnation, enchant item and create artifact. Each of these has a specific priority assigned to it, generally the better the creature the higher the priority. Heroes also check for having a free hero slot.
For spirits, the AI wants exactly 3 spirits on each plane, unless every node on the plane is already theirs. If they have 3 or more, the weight of spirits is zero.
For items and artifacts, the AI will never make any until turn 180 - early expansion is way more important and skill is not high enough to afford such an expensive spell. Past turn 180 the AI has a small chance of using Enchant Item and a somewhat higher but still small chance for Create Artifact. However if they have Artificier, they will never use Enchant Item, and will have a 4 times higher chance of using Create Artifact as other wizards.
The priority of this category is increased if the AI feels they need more units (their army power is inferior to others), and it is significantly increased if a very rare summoning spell is available. Having conjurer also increases the priority of summoning, as is having the summoning circle moved from the fortress, being Militarist or Expansionist.
The category always has a reasonably high base priority, even if the AI actually does not have any summoning spell they can use. For the 2.5 version if the AI selects summoning but has no such spell to cast - only possible for wizards playing heavy Life, as Guardian Spirit is limited to 1 per plane, others will always have at least some generic summoning spells - the AI will instead try the Unit buff category instead of not casting a spell that turn.
City Buff
All positive city enchantments belong to this category, as well as Transmute, Change Terrain, Move Fortress.
For casting any of these, the AI first checks if they can find a valid target, if not the assigned priority is zero.
The AI never casts Move Fortress, because it lacks the comprehension of the consequences, and it's generally impractical: the fortress already has the best defenses due to other mechanics.
The priority of this category is increased by being a Theurgist, Perfectionist, or having better city buff spells available.
Curse
This category can only be used if the AI is hostile towards at least one player. Priority is increased the lower the relation to that player is. Priority is greatly increased by Maniacal and Ruthless personalities and greatly reduced by lawful and peaceful. It's also somewhat increased by Expansionist.
The category contains all the city curses: Pestilece, Evil Presence, Chaos Rift, Famine, as well as Warp Node, Drain Power, Spell Blast, Time Stop, Earthquake, Blizzard, Fire Storm, Stasis, Black Wind, Corruption, Raise Volcano, Call the Void.
Each has its own priority assigned, going from lowest to highest: Stasis(1), Blizzard (3), Corruption (10), Spell Blast (10), Black Wind (10), Fire Storm (15), Warp Node (15), Drain Power (20), Raise Volcano (25), Earthquake (25), Famine (30), Evil Presence (30), Chaos Rift (40), Pestilence (60), Time Stop (150), Call the Void (200)
Casting Time Stop requires the wizard to have at least 5000 mana crystals and ofc not having the enchantment in effect already.
Spell Blast is slightly special. In addition to the default, if the AI notices an enemy wizard casting Spell of Mastery, the priority of the category increases tremendously (pretty much at least 90% chance of using it immediately) and Spell Blast will be selected from the category directly skipping the rest of the process. The same happens if the AI has Detect Magic in play, and an enemy wizard casts any spells on the list of "forbidden" spells. The latter doesn't look at the cost of the spell since it's not known how far the enemy progressed in casting it, it will assume the AI always has enough mana to blast the spell - if not they'll still try but nothing will happen. See the list of spells getting blasted in the last post.
The target for city curses is the "best" city the enemy has, which means the highest total of buildings and population, the fortress has a minor extra priority on top of that. (in vanilla AI the fortress received such a massive priority boost that nothing else could get targeted at all!)
Unit Buff
This category contains all unit enchantments as well as Nature's Cures, Conjure Roads.
The AI never casts Nature's Cures and Conjure Roads, since the former is too situational to just thrown around at random, and the latter may backfire by allowing enemies to move faster.
The AI does not check if they have a valid target for any of these spells, they assume they do - which is usually correct since they produce a lot of units.
As usual, each spell has its own priority, the stronger spells have higher.
The priority of the category depends on the strongest such spell available, having 3 tiers: weak, medium and powerful, with a priority of 25, 60 and 200. Additional large priority is added if the wizard is a perfectionist and some if they're militarist.
Unless the spell has additional conditions, the priority to target a unit is proportional to the unit's calculated military power plus a quarter of the power of other units in the same stack, plus 100 for the fortress and 50 for heroes.
Spell of Mastery
If the wizard has SoM this "category" gets a priority of 500. SoM is the only thing in the "category".
Summoning Circle
The AI has a timer of ~10-30 turns and alternates the location of the circle between their fortress, and their most remove city they can find. This "category" which only contains this one spell has a priority of 200 if the timer expired.
Disjunction
Each global enchantment owned by other wizards adds to the priority of this, each type of spell having a specific priority. The numbers are currently being reworked.
Allied spells are ignored. However, at the time of targeting a spell, allied spells will be considered if Spell Binding was cast - not if Disjunction.
Disjunction and Spell Binding belong to this group.
Disenchant Area
Each city curse or warped node adds to the priority of this.
Contains Disenchant Area only.
Global Enchantment
Each uncast global enchantment available adds to the priority of this. If chosen, each enchantment has its own priority to be the first to get cast, better enchantments have higher. Enchantments already in effect have 0 priority.
Also contains Great Unsummoning and Final Wave, these can be used at any time and have no special conditions.
Divine Order is special. It'll be only cast if the wizard or their allies are having enough Life magic books or enemy wizards have enough Chaos or Death, in particular if the sum of these exceed a certain threshold.
Theurgists have an increased chance to pick this category.
Note : This list is incomplete and outdated since version 4.0 when the AI overland spell selection was completely remade. While the generic concept is the same, there are more spell categories, such as "water walking movement enabling buff" and more details in it.
Summoning
All overland summoning spells belong to this category, plus summon hero, summon champion, summoning spirits, resurrection, incarnation, enchant item and create artifact. Each of these has a specific priority assigned to it, generally the better the creature the higher the priority. Heroes also check for having a free hero slot.
For spirits, the AI wants exactly 3 spirits on each plane, unless every node on the plane is already theirs. If they have 3 or more, the weight of spirits is zero.
For items and artifacts, the AI will never make any until turn 180 - early expansion is way more important and skill is not high enough to afford such an expensive spell. Past turn 180 the AI has a small chance of using Enchant Item and a somewhat higher but still small chance for Create Artifact. However if they have Artificier, they will never use Enchant Item, and will have a 4 times higher chance of using Create Artifact as other wizards.
The priority of this category is increased if the AI feels they need more units (their army power is inferior to others), and it is significantly increased if a very rare summoning spell is available. Having conjurer also increases the priority of summoning, as is having the summoning circle moved from the fortress, being Militarist or Expansionist.
The category always has a reasonably high base priority, even if the AI actually does not have any summoning spell they can use. For the 2.5 version if the AI selects summoning but has no such spell to cast - only possible for wizards playing heavy Life, as Guardian Spirit is limited to 1 per plane, others will always have at least some generic summoning spells - the AI will instead try the Unit buff category instead of not casting a spell that turn.
City Buff
All positive city enchantments belong to this category, as well as Transmute, Change Terrain, Move Fortress.
For casting any of these, the AI first checks if they can find a valid target, if not the assigned priority is zero.
The AI never casts Move Fortress, because it lacks the comprehension of the consequences, and it's generally impractical: the fortress already has the best defenses due to other mechanics.
The priority of this category is increased by being a Theurgist, Perfectionist, or having better city buff spells available.
Curse
This category can only be used if the AI is hostile towards at least one player. Priority is increased the lower the relation to that player is. Priority is greatly increased by Maniacal and Ruthless personalities and greatly reduced by lawful and peaceful. It's also somewhat increased by Expansionist.
The category contains all the city curses: Pestilece, Evil Presence, Chaos Rift, Famine, as well as Warp Node, Drain Power, Spell Blast, Time Stop, Earthquake, Blizzard, Fire Storm, Stasis, Black Wind, Corruption, Raise Volcano, Call the Void.
Each has its own priority assigned, going from lowest to highest: Stasis(1), Blizzard (3), Corruption (10), Spell Blast (10), Black Wind (10), Fire Storm (15), Warp Node (15), Drain Power (20), Raise Volcano (25), Earthquake (25), Famine (30), Evil Presence (30), Chaos Rift (40), Pestilence (60), Time Stop (150), Call the Void (200)
Casting Time Stop requires the wizard to have at least 5000 mana crystals and ofc not having the enchantment in effect already.
Spell Blast is slightly special. In addition to the default, if the AI notices an enemy wizard casting Spell of Mastery, the priority of the category increases tremendously (pretty much at least 90% chance of using it immediately) and Spell Blast will be selected from the category directly skipping the rest of the process. The same happens if the AI has Detect Magic in play, and an enemy wizard casts any spells on the list of "forbidden" spells. The latter doesn't look at the cost of the spell since it's not known how far the enemy progressed in casting it, it will assume the AI always has enough mana to blast the spell - if not they'll still try but nothing will happen. See the list of spells getting blasted in the last post.
The target for city curses is the "best" city the enemy has, which means the highest total of buildings and population, the fortress has a minor extra priority on top of that. (in vanilla AI the fortress received such a massive priority boost that nothing else could get targeted at all!)
Unit Buff
This category contains all unit enchantments as well as Nature's Cures, Conjure Roads.
The AI never casts Nature's Cures and Conjure Roads, since the former is too situational to just thrown around at random, and the latter may backfire by allowing enemies to move faster.
The AI does not check if they have a valid target for any of these spells, they assume they do - which is usually correct since they produce a lot of units.
As usual, each spell has its own priority, the stronger spells have higher.
The priority of the category depends on the strongest such spell available, having 3 tiers: weak, medium and powerful, with a priority of 25, 60 and 200. Additional large priority is added if the wizard is a perfectionist and some if they're militarist.
Unless the spell has additional conditions, the priority to target a unit is proportional to the unit's calculated military power plus a quarter of the power of other units in the same stack, plus 100 for the fortress and 50 for heroes.
Spell of Mastery
If the wizard has SoM this "category" gets a priority of 500. SoM is the only thing in the "category".
Summoning Circle
The AI has a timer of ~10-30 turns and alternates the location of the circle between their fortress, and their most remove city they can find. This "category" which only contains this one spell has a priority of 200 if the timer expired.
Disjunction
Each global enchantment owned by other wizards adds to the priority of this, each type of spell having a specific priority. The numbers are currently being reworked.
Allied spells are ignored. However, at the time of targeting a spell, allied spells will be considered if Spell Binding was cast - not if Disjunction.
Disjunction and Spell Binding belong to this group.
Disenchant Area
Each city curse or warped node adds to the priority of this.
Contains Disenchant Area only.
Global Enchantment
Each uncast global enchantment available adds to the priority of this. If chosen, each enchantment has its own priority to be the first to get cast, better enchantments have higher. Enchantments already in effect have 0 priority.
Also contains Great Unsummoning and Final Wave, these can be used at any time and have no special conditions.
Divine Order is special. It'll be only cast if the wizard or their allies are having enough Life magic books or enemy wizards have enough Chaos or Death, in particular if the sum of these exceed a certain threshold.
Theurgists have an increased chance to pick this category.
Note : This list is incomplete and outdated since version 4.0 when the AI overland spell selection was completely remade. While the generic concept is the same, there are more spell categories, such as "water walking movement enabling buff" and more details in it.
Before any AI's turn, all AI players disband their obsolete units simultaneously if, and only if, the global max unit cap of 2000 is getting near. This was updated in 2.4, on top of disbanding units weaker than a certain threshold (I think it's set to one third of the average of the army), they also disband a fixed amount proportional to their army size: 1 unit for each 100 they own. Spirits, settlers, heroes, ships, engineers, etc cannot get disbanded this way, and higher level units and buffed units are worth more. This part of the code is not rocket science but should be enough to slowly replace crappy halberdiers with awesome sky drakes without ramming into the unit cap head first. In most games the cap will not be reached anyway.
The AI starts their turn with their spell being cast. If complete, they start a new spell. In 1.40 and below vanilla, they did not proceed with that spell, and left it for next turn even if they had enough skill to complete it. In CoM and 1.50 they proceed and keep casting while they can. Doing this first ensures anything they cast will be considered when deciding what to do.
(actually they do a few stuff prior, like evaluating continent values, selecting a target continent, updating hostility etc)
Then they decide how to distribute their power base to mana/power/reaseach, and use alchemy if needed.
Then they set tax rates and farmers: 2.5 will bring a major update to this part and makes the AI able to set these up to way that's reasonable and safe. Finally they set production in towns and then proceed to the main part, assigning orders to units.
Once the procedure to assign orders is completed, all units perform their assigned orders (move, attack, whatever) and the AI ends their turn, the next AI's turn starts. This is new for the mod and the 1.50 patch: In the vanilla game, first all 4 AI players assigned orders, then all 4 executed those moves, so they effectively had their turns simultaneously, without being aware of what the wizard earlier in the turn order did: resulting in attacking each other when not intended or failing to do so when intended, as the other wizard's units were moved.
Also only in the mod or 1.50, the AI starts by cancelling all existing orders on all units - except settlers and engineers, those would not work as intended otherwise - and assigns new orders, most likely to continue whatever they were doing, but if circumstances change they might not. In the original, once the AI assigned an order to a unit it was carried out, and could not be interrupted: A unit sent to attack an enemy city would still do so 20 turns later when reaching it even if the city is no longer there or peace was made.
Overland unit movement, part II - Assigning unit orders
This consists of several steps executed in a set order :
1. Any unit the AI cannot pay maintenance for are disbanded immediately. The AI disbands their worst units first. The AI is not subject to normal maintenance rules: if they fail to actually pay maintenance, they do not lose units - the game assumes this isn't possible in the first place since they disbanded any excess already. Insecticide improved this procedure by allowing the AI to keep their units if their income was negative but they had enough gold/mana to still pay for them.
2. AI stacks that are standing in a tower on the home plane of the AI switch to the other plane if able. The mod adds the ability to the AI to swap planes for units standing on an Astral Gate as well: in this case the city must have at least 6 units remaining, and the capital cannot be left this way. Furthermore the AI can swap planes with Plane Shifting or Planar Travelling units as well (item power not included). In all cases, movement happens from the home plane to the "other" plane. The AI goes with the assumption that they need more forces on the plane where they not started from, which is 99% correct and making it smarter would be close to impossible.
3. Any AI ships on land (usually produced in a city) are ordered to move onto the nearest sea tile.
4. Next all AI units patrolling near a poorly defended city cancel their orders. Since the orders are always cancelled on all units for the mod, this no longer serves any purpose, nor does any harm.
After this the following are done for each continent, one continent at a time :
5. The AI makes a list of stacks they have available on the continent, only idle units are considered in these but since they now reset orders each turn this is generally all of them.
6. Any melding, road building or outpost creating unit is sent out of any city.
7. (updated in 2.9) Any unmelded node occupied by own or no troops pulls the nearest idle spirit towards itself.
8. Any settlers are immediately ordered to build a town if able, otherwise they are sent towards a settling location of preference.
9. Then any units that can purify are sent to do so if needed.
10. Any engineers available are ordered to either move to the closest city, or to build a road from that tile to the nearest unconnected city. The engineers are now able to build around obstacles instead of trying to build through them.
11. Next, the AI builds a list of all available targets on the continent: enemy fortresses, cities, units, nodes, lairs, towers. in this order. Unfortunately this list is limited to 25 things, on very large continents the AI will be unable to add to the list and will overlook anything that would be near the end of the list: towers, lairs, etc. Fortunately, if their is enough enemy presence to fill the list, the AI should most likely ignore nodes and lairs and should not send their troops away to the other plane in towers, so this is not particularly bad. The only possible problem is when there are more than 25 stacks of enemy units and the AI might not notice, and fail to attack something that would be better as a target, still with 25 targets, it's very likely something will be attacked and killed, reducing the count and allowing other stuff to get on the list. Unfortunately, extending the size of the list is impossible.
12. The AI goes through any remaining stack that's still idle,
(new in 2.9) and checks if there is Jihad hostility towards anyone. If yes, the stack is at least as strong as 9 halberdiers, and the capital is on the continent and reachable, they are ordered to move there. During Jihad hostility, enemy troops are not considered roadblocks and the AI will try to move through them - though considers these tiles more expensive to enter than mountains. During any other level of hostility, enemy troops are considered roadblocks.
...and sends each to the highest (priority/distance) target that is weaker than 1.33 times the power of the AI's stack to attack it then erases the target from the list by setting its priority to -1. In the mod, the target is instead retained but set to the lowest priority of 1, also targets with (priority/distance)<1 will be ignored unless distance is less than 5 to avoid sending multiple stacks from far away locations to the same target, however multiple stacks from a nearby location are fine. This not only makes the AI able to attack the human player's city with multiple stacks which it wasn't able to do originally, as well as speeding up transport through towers, but if the units manage to actually reach the tile in the same turn, the two stacks merge together as a side effect, helping the AI to build up larger stacks in multiple locations without having to rewrite the entire stack building process.
13. Any stacks still left over will be sent towards the "main action continent" if they are able to move through water, from any continent that is not "under contest with an enemy". In the mod, directly prior to doing this, the stack will check if there are any viable military targets nearby: while "12." covered targets on the continent itself, doing this allows the AI to perform intercontinental or land to sea attacks which were impossible without this addition. This is probably the largest addition to the AI in terms of code size (and maybe even importance). In the 2.5 update this was changed to not only consider units as targets but cities as well, which does not matter here, but does if called from "18." instead. In 2.9 the stack first checks for Jihad hostility, if the capital of any player whose assigned hostility level is Jihad is on the same plane and the stack is at least as strong as 5 halberdiers, the stack will move to attack that capital. In 5.0 and later, this now uses identical target ratings as land at based attacks, including lairs.
14. Any remaining unit on a continent rated as "no target to attack" or "entirely owned" will go to shore and attempt to board a ship. If no ship is present, it'll mark the location as a "ship required point".
15. The AI will now look through any remaining, still idle stacks and makes a list of units that can be used for building a new stack. The best 9 such units are selected. This was originally done between 11 and 12, which is a major flaw, since the list is used in 16, so units on the list end up with conflicting orders: they'll both be sent to the stack building point and to attack targets/board ships/whatever. This can tear already built stacks apart by pulling some of them back to build a new one, too.
The AI will be able to select any amount of units from any stack not standing on a strategic point. If they're on a node, at least 3 units need to remain but the rest can be used. In cities, at least 6 units need to be present and at most 2 can be sent for the first 50 game turns. After that however, at most one unit can leave the city per turn and only if it has 9 units, also units in the capital can never leave. While this leaves the units on the way vulnerable, it ensures the city is not going to get lost, which is far more important. Always the 9 most valuable units (highest military rating) will be added to the list in order of value.
It's important to mention here that although it's not an actual stack building mechanism, when the AI produces units in a city that's full, always the weakest unit will be pushed outside to ensure the city defenses remain appropriate. However, if these units are not chosen later for stack building due to better units being available, they'll stay there and accumulate, effectively forming a stack on the spot automatically without AI interference, and these stacks are much less vulnerable to getting picked off one unit at a time prior to forming, since no actual movement is needed to be done by units.
16. The AI counts the number of units at the intended stack building tile, and sends as many there to fill up the count to 9 (or the intended lower amount). The mod also changed this, the original send units in excess of 9, which is a problem: if the two stacks cannot merge together due to having over 9 total units, they'll not move, and no unit will be added at all. The stack building tile is the center of their empire on the continent, or the shore if the AI intends to leave the continent.
17. Finally, the AI checks their own cities and nodes, and if there aren't enough defenders, sends any remaining, still idle units there. The AI considers nodes as needing more defenders up to 9 units even though they are also allowed to leave no more than 3 in their if those units are needed to build stacks: this ensures no valuable unit like a hero is stuck on eternal node guard duty, instead new units will be added to the guarding stack, and the valuable units will be able to leave it to build a new stack.
18. After all the continents are done, the AI checks for units over sea. If they are not capable of moving on water, they are disbanded (drown), since the original game did not contain a real drowning check. This is now unnecessary since they would drown on their own anyway but this way they cannot move and do stuff like attacking someone before drowning. Then, any remaining unit is sent to the main action continent. However, in the mod, units first check for a potential target, using the same procedure as "13.", allowing the AI to launch a sea to land attack on a nearby target.
19. Finally, any still remaining stacks on water are checked for ships. Stacks containing ships and units head towards the selected shore tile of the main action continent. Ships already on shore of that continent send the units to land as well as ships on shore of any continent marked "unoccupied by anyone/interesting" so passing by another interesting continent on the route might make the ship stop and unload the troops to explore a new continent. Stacks containing ships and settlers are sent to the "settling continent" instead the same way, if such is not available, they head to the main action continent instead. Stacks containing only ships look for the list of "ship request points" and send one ship to each such point. This procedure had a whole ton of bugs, safe to say at least two dozens in the vanilla game. Finally which the vanilla game did not do but the mod does: any remaining, still idle ship stacks will look for any valid naval target on see to attack. Those that can't find any, will more semi-randomly one tile to a direction based on turn order, which keeps the ships roughly at the same spot but without staying on the same tile. This is critically important since the AI insists on sending their ships to their selected unload points, but never recall them, blocking any further attempts to move ships there to unload. The ships have to be moved away randomly to ensure this does not happen. Another side effect of this is, if a shore is nearby the movement of the ships, held up by the land tiles, will despite being synchronized always moving the same way, result in the ships gathering up in a larger stack. This lets the AI "build" a massive stack of ships that can be used for naval battles very effectively.
20. Starting in 5.0, there is an additional step at the end - the AI will pull their best 9 water movement capable units together, excluding units that already have an attack order. This allows the AI to build a powerful intercontinental doomstack. More details in the thread where this feature was announced.
Other AI overland actions
Buying in cities
Design concept: Rush buying any specific building or unit over others is rarely a relevant difference - it merely allows the town to produce the next item (spent gold/2) production earlier. Since the AI selects what to build on weighted random priorities, speeding up a particular city over others is not providing a significant benefit. Even speeding up a major unit production center does not, as the AI lacks the ability to comprehend "I need more of these units" or "I don't need more of these units". For every case when the massive hordes of incoming Minotaurs or Paladins is helping the AI push ahead and win, there will be a case when it merely helps prolong a war between two AI of even strength, wasting both their economic potential, or worse, the units might be made in a long period of peace when they have nothing to do at all - and might not have even as long as the final war over Spell of Mastery.
Due to this, the AI should use the "BUY" button in two cases: 1. when they have an exorbitant amount of money they have absolutely no reason to keep around, or 2. if the bought building pays returns the investment by produced resources, or accelerates the production of the first few quality troops (either for the city or in general).
Currently defined rules :
-do not buy if can't pay the cost (obvious)
-If gold>10000 then buy
-Is sawmill then buy
-if gold>=50*cost to buy then buy
-if Sage's Guild, Amplifier Tower, Wizard's Guild, Alchemist's Guild or Fighter's Guild and gold>=2*the cost then buy.
This procedure was completely remade in 4.0 and is now more detailed. The AI will generally focus on military spending in adamantium/mithril cities, and cities present on mostly enemy controlled continents.
Alchemy
Design concept: Alchemy is a tool to convert resources in emergencies, unless the wizard has the Alchemy retort, in which case it can be used for generic purpose as well.
Rules:
-If casting the Spell of Return, convert all gold into mana every turn
-If alchemist, before turn 30, convert into having a ratio of 7 gold to 3 mana.
-If alchemist, after turn 30, convert into a ratio of 8 mana to 2 gold. If this would result in more than 24000 mana, make 24000 mana and all the rest as gold. If this would result in more than 30000 gold, put the leftover into mana again.
-if not alchemist and mana<gold/16, convert half of the available gold.
-If not alchemist, 10% chance to convert half of the gold if gold>4*mana. Skip if gold<100.
-If not alchemist, 10% chance to convert half of the mana if mana>8*gold. Skip if mana<100.
Power Distribution
-If time stop is being cast, set 100% mana, otherwise
-if sorcery conjuction and sufficient mana then set 90% skill 10% mana, otherwise 50% skill 50% mana, otherwise
-if spell of mastery is being researched, neither of the below conditions that would null research priority are true, and we could research it in fewer than 50 turns by that setting, set 80% research, 20% mana if sufficient mana, otherwise set 60% mana 40% research, and improve relations and peace interest with every other wizard. Otherwise
-if insufficient mana, set 100% mana, otherwise
-if casting spell of mastery already, set 90% mana, 10% skill
-if below turn 60, and researching Giant Spiders, Gargoyles, or Lycanthropy, set 80% research 20% mana
-if below turn 30, we have fewer than 250 mana, set 100% mana but if fewer than 26 casting skill, and over 100 mana, set 25% skill 75% mana instead.
Default priorities: 4 research, 2 mana, 4 skill
-if spell of mastery is being researched and someone else who is not banished has spell blast, lower research priority to 0
-if spell of mastery is being researched and we are at warlike hostility with the human player and we aren't in the top 25% of the historian chart, research priority to 0
-if theurgist, research priority +3
-if perfectionist, skill priority +3
-if militarist, mana priority +2
-if peaceful personality, research priority +3
-If time stop is known, mana priority +10
-if research behind the human player by 25% number of spells, or fewer than 15 spells known: research priority +3
-if casting skill does not exceed the human player by 50% or is below 80, skill priority +2, but only if the above wasn't true
-during war, both of the above are ignored, as they are overridden by the "more summoning needed" global tactic
-if sage master, research priority +3
-if archmage, skill priority +3
-if mana focusing, mana priority +2
-if chaneller, mana priority -1
-if alchemist, mana priority -2 (but can't go below zero)
-if own enlightenment in effect, halve research priority
-if spell of mastery is being researched and no one knows has blast, we are in the top 1/16 of the historian chart, it's past turn 200, and human player is below 10/16 height on the historian graph, research priority +5
-if casting skill>500, halve skill priority
-if not researching a spell, set research priority to zero
-if over 20000 mana crystals, set mana priority to zero
-set 20% mana, and distribute the remaining 80% randomly weighted by the above priorities.
Taxes
Design concept: The AI should aim for the highest tax rate they can get without losing too much production to rebels. Only the overall empire is considered, even if a single city or two starves, it's acceptable if the others can pay the high tax without rebels.
Rules :
-Overall rebel rate of empire population<10%: Increase tax rate by 1.
-Overall rebel rate of empire population>30%: Reduce tax rate by 1.
Food
Design concept: The AI does not actually have a desertion check - instead, they voluntarily disband any units they can not afford to maintain during their own turns. This means, the AI can never lose units due to conquered cities, and does not need to produce additional food, only the amount actually consumed.
Rules:
-go through each city one at a time. If total food is too low, add a farmer, assuming it produces 1 food. If total food is too high, remove a farmer and assume it has taken away 3 food. Change only once per city. If all cities are done, start from the first one again. Keep going until produced food equals needed amount+2.
Diplomacy
See the Diplomacy section in the wiki for MoM 1.50. CoM's diplomacy system is mostly identical. (some numbers, especially difficulty modifiers, have been tweaked)
There is one new addition: when the human player uses disjunction, spell binding or spell blast against an AI, it will trigger a Diplomatic Reaction with the appropriate warning message and penalty, proportional to the cost of the spell lost.