Some strange AI code I found...

Ace of Spades

GEM Fanboy
Joined
Feb 18, 2003
Messages
210
Location
Munich, Germany
Hello AI modders,

while reading through CvPlayerAI.cpp for the first time, I noticed some really strange code.

It's rather far down in CvPlayerAI::AI_getStrategyHash() and CvPlayerAI::AI_getCultureVictoryStage().

In both methods, there is a variable named "iNonsense". If I understood the code correctly, this variable is determined by the coordinates of a civs capital on the map, and influences the AIs strategic behaviour - now did I get that correctly, that a civ starting in the lower right corner of a map might prefer different strategies than a civ starting in the center or the upper right corner?

This seems pretty strange to me... perhaps someone with more understanding of the AI code could verify it.

Best regards,
Ace
 
It's certainly a bit strange looking ... basically, it's a random number to modify chances of pursuing a particular strategy so that AIs aren't predictable from game to game. Because they want the strategy to be consistent from turn to turn they can't create a new random number each time the function is called, instead using:

(iX + iY)%7

So the x,y coordinates of the capital city mod 7 (remainder after dividing by 7). There are different mod values in other places depending on what kind of effect they're looking for. Because the two numbers are added together and then the remainder of a division is taken you won't get upper left civs always more likely to pursue one strategy or anything.

It certainly looks very strange though, that's for sure! Hope it makes a bit more sense now.
 
Thanks for explaining this! However, this might mean that the AI will probably change its strategy if their capital switches because they move it to another city, correct?

However, the effects will probably not be so decisive as to worry about this, I guess.

Best Regards,
Ace
 
this might mean that the AI will probably change its strategy if their capital switches because they move it to another city, correct?
Yes, there's only a 1/7 chance that the position of the new capital results in the same iNonsense number than the position of the former capital.

I don't think it's a problem at all, because whatever caused you to lose your capital, chances are that a strategy change is in order. ;)
 
Sure. But maybe the AI just constructed a palace somewhere? I think i've seen the AI do it sometimes, but I'm not completely sure. It might be a good idea to reduce maintenance in some cases.

However, I do agree that this is probably of minor importance.

Best Regards,
Ace
 
This is the problem that the code was intended to solve:

- we needed a way to make a random decision that would be remembered across turns and would be the same when you loaded a saved game
- BUT we could not modify the saved game file to make it incompatible with saves made without this mod

using the location of a capital was one way to do it. Perhaps a better way would be to just perform some sort of hash based on the (unchangable) terrain features of the map, or something else.

Of course, I later came up with the scheme we use to store some extra data in some unused spots in the save game file, but the decision was made to not change this code to do a regular random and save it to the extra spots.

-Iustus
 
Great to see you contribute to this AI project again, Iustus.
 
The palace building in BTS does change this. As the code is now, it breaks cultural victory.
 
Thats too bad they didnt bite the bullet and change the save game format for BtS when they had the chance.

--

(thanks guys! I dont have the free time to contributing coding right now, but I can at least post in my off time)

-Iustus
 
Back
Top Bottom