Fixed a rare case in which a town-defender could become an indirect part of a troop-delivery-task by doing a sub-task like capturing a shipyard that takes less than 1 turn but leaves him out in the open.
It was happening when all slots were full but no unit even needed disbanding because the unit to be bought is part of the units that are inside of the existing slots.
(This commit contains excessive debugging but I don't want to remove it just yet incase another issue pops up.)
The score for buying army now scales with the cost of that army.
The cost of the units that need to be disbanded in order to hire the new units is subtracted from the army-hiring-score so the AI will prefer hiring armies where the amount of troops to disband is low or non-existent.
Fixed a bug that also disbanded troops when there still were free slots available.
When the AI cannot buy units in a city because all slots are blocked and the units in the slot are cheaper than the units it wants to buy, the AI will now get rid of the units that block that slot in order to be able to buy the better units.
Fixed that armyInvolvement was only filled in when the action involved a target to interact with rather than just a tile.
Since armyInvolvement was used for scoring actions such as retreating towards the closest town, this caused the AI to never retreat to their towns when they were supposed to.
These can happen when an enemy spawns ontop of an AI-hero. If the action would win, it wouldn't be executed anyways. So now AI does the next best thing instead, which likely what it wanted to do anyways.
AI will no longer skip turns with heroes that are waiting for a delivery that takes more than one turn. Instead they will do something until their delivery is close enough to get it at the same turn.
Fixed an issue where a bunch of heroes all tried to do the same tasks:
Tasks that involve no fighting will now always be performed by the closest eligible hero while all other heroes look for something else to do.
The AI is now a lot more likely to buy a hero early on when that hero's army is worth more than half the cost of the hero, regardless of other circumstances.
Instead of skipping the defense of other towns entirely when hiring a hero was considered as a way of defending, now only hiring the same hero twice will be avoided.
Just killing stuff even if there is no apparent reason now also is considered for the mere purpose of gaining XP.
This also helps the non-cheating AI to keep attacking enemies when they can't see anything worth exploring behind them.
Fixed an issue that made AI consider unpurchased troops as reinforcements for their armies which caused AI to visit cities over and over without actually doing anything there.
Buying units is handled by BuyArmyBehavior.
AI will now also garrison a hero as defender if the town to be defended has troops as long as the hero can merge their own troops with the town.
AI will no longer just dismiss existing troops in a town if a hero trying to garrison there can merge with it.