OK. I believe the "merc function" is completed. Have been testing various scenarios for about 5 hours now, and haven't seen anything "break" yet.
The basic design criterion is related to the fact the AI simply does not buy units. In a way, that simplified the code, as the merc function needs to completely manage when AI spawns units, which units, where, and when to remove them.
For removing merc units already in the game: at turn 15 of a unit's lifespan, the function begins testing whether to remove or not. The test is ' lifespan * 2 '. This is the % chance to remove unit. So, if the unit is 15 turns old, there is a 30% chance to remove it. By the time it gets to age 50 turns, the unit will automatically be removed. From what I've seen, most of the units desert in their early 20's (turns, that is).
The current code gives a 5% chance per turn of an AI civ attempting to spawn a unit each turn. If the civ passes that test, the function will determine what are the possible/legal merc units available to that civ, based on their tech level, any overrides that are in place, etc. Once it has a list of possible units, it picks one of them on a random basis (i.e. each possible unit has an equal chance of being picked).
At that point, the function checks if the unit is a ship or not. If it is a boat, the function checks each city the civ has, and spawns the boat in the first coastal city it finds (almost always the earliest founded). If there are no coastal cities, no unit is spawned.
If the proposed unit is not a ship (land, air, hover), the function tests if the civ has a capital. If not, no unit is spawned and the function moves on to the next civ. The only time a civ doesn't have a capital is when it's Barbarian or when the game option "Complete Kills" is selected. In either of those cases, it doesn't seem right for a merc hiring action to happen.
If the civ has a capital, the merc unit will spawn there, and be available to the AI that turn.
Kind of brute force in the spawning/removing of units, but as most of this occurs out side of the human player sight, I think it will appear like the AI is using the units in a similar way to using other units it builds normally. That's probably good enough.
Some of the things I'll ask you to look for while you're testing: are the units spawning in a logical manner (too many, too few, too often, etc.), and are they being removed in a logical manner.
It doesn't have to be a perfect representation of how a human player might use mercs, and they will appear more often than they might for a human, simply because these units don't "cost" the AI anything - no gold is removed from their treasury. This is necessary, as we don't want a random merc unit or two to break any ongoing AI operations. It is a slight advantage to the AI, but given its overall effectiveness, they can probably use the help.
Will spend a couple of hours this morning examining the "desert & rebel" possibilities we discussed yesterday. Will set it up so it's a one line change to turn on/off in the mod, and default it to OFF.
After all this, I'll move on to the "hard part" this afternoon: folding the Merc Function code into the Exotic Units mod. Overall, it's not complicated, but there are usually a few gotchas hiding in code that is set up for testing, as opposed to code ready for the final mod. Am hoping to have a new version of Exotic Units posted today for you to test.