Sutsuj
Chieftain
- Joined
- Apr 30, 2020
- Messages
- 42
It's not really a double-check, it's more like the normal check. That function is the standard way that the game determines if a given improvement is present in a given city. To process all improvements in a city, the game considers all improvements defined in the scenario data and calls that function to filter out only the ones that are present. That kind of thing is pretty common, for example when a city finishes a unit it checks all present improvements to see if there are any that give the unit a bonus experience level. It does the same thing when computing city production, commerce, corruption, happiness, pollution, defensive bonuses, available build options, and so on.
But what really concerns me is the cost of the unit AI. Imagine an AI battle where a large stack of units is attacking a city with a large stack of defenders. Each attacker must find the top defender in the city, which means evaluating the defense strength of each defending unit, which means finding the defensive bonus provided by the city, which means checking every present improvement. If checking every improvement requires checking every unit in the city, you'd be considering every defending unit, times every possible improvement, times every defender, times every attacker. That could easily be a drag on turn times. Though I should point out this is all speculative. Maybe the combat code doesn't work that way, or maybe the slowdown caused would be drowned out by some other factor. The only way to know would be to test an actual implementation.
Ugh, if that is indeed the case then that's unfortunate. It seems CivIII could really use more permanent state and/or some hash tables in its implementation. If looking up "is a unit of type X in the City?" is big-O(1) with a small constant factor, then the change shouldn't have too much of an impact.