Roland Johansen
Deity
Returns the lower number of the two given numbers.
That's exactly what this does:
Code:int iLoop; int iNumDefenders = AI_getNumAIUnits(UNITAI_CITY_DEFENSE); for (CvCity* pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop)) { int iNewHappy = std::min(iNumDefenders / iNumCities, pLoopCity->[COLOR="Red"]AI_neededDefenders()[/COLOR]) //if iNumCities == 0, the loop will never run, so no division by zero. //this is a conservative estimate, change std::min to std::max to have a more liberal estimate }
Sorry. As said before, I'm not a programmer, but I do like to think about improving the AI by employing decent approximations.
I'd likely prefer an average over a minimum or a maximum. iiNumDefenders / iNumCities is an underestimate as the AI will distribute the units in a better way over the cities, in a way that it gets more happiness advantages. AI_neededDefenders is an overestimate as the AI will often not own the units to get that preferred number of defenders. An average seems to be a decent approximation of the actual happiness advantage.
That should make the AIs add more units to cities that currently need happiness if they're operating the Monarchy civic. Now then, moving back to basic functionality updates to the PlayerAI stuff that will have to do for now til we can get a full system working b/c I need to post an update to my mod and this is taking forever lol:
I'm not a programmer so I didn't try to decipher all of the code. But if this causes the AI under hereditary rule to continuously create more units to fight unhappiness then that might not be such a good idea. The cost of building and maintaining a unit plus the food required to create another citizen in a city (which could have been hammers or commerce) is often larger than the advantage of the temporary extra citizen during the period that the AI is using the hereditary rule civic. At some point the investment in temporary units for temporary happiness and the cost of creating temporary citizens is just not worth it. After the hereditary rule civic has been replaced by another civic, the extra defenders are superfluous and costly and the extra citizens can't be kept happy.
I typically don't use more than 3-4 units per city during hereditary rule and will often use even less units.
It's nice to get some happiness bonus from the defenders in your cities, but it's typically not a good tactic to start creating units in great numbers to create happiness.