Is there any logic to an AI DoW?

I stumbled across this idagger and inonsense stuff yesterday, i kind of gave up on understanding these functions then :lol:. Anyway that strategy_hash function alone is longer than ai_dowar, i just list the return vars from it i think, see if they really matter.
 
Nice var name :lol: .....

iDagger is the "backstabbing" variable, right?

Well it seems many sincere and honest warlords with a high morale and enough military flavor are doing AI_STRATEGY_DAGGER. And of course Gandhi if his capital is located @X=20;Y=22 or something similar...

The whole function is kind of weird, I also think there is a bug in the following parts for AI_STRATEGY_PEACE:
Code:
[SIZE="3"]int iWarMemory = GET_PLAYER((PlayerTypes)iI).AI_getMemoryAttitude(getID(), MEMORY_DECLARED_WAR);
if (iWarMemory > 0)
{
	//they are a snake
	iParanoia += 50 + 20 * iWarMemory;
	break;							
}[/SIZE]
This would increase an AI Player's iParanoia if he declared war on the other Player iI. Who is the snake here??? I think it needs to be the other way around, further down it's the same problem for Player iJ.

Another WHAT??? in AI_doWar:
Code:
[SIZE="3"]int iDefensivePower = (GET_TEAM((TeamTypes)iI).getDefensivePower()[B][COLOR="Red"] * 2) / 3[/COLOR][/B];[/SIZE]

So to be safe from a close Monty we actually need double his power (130%*1.5 = 195%)
 
Ok it appears I was wrong about Mahatma--he can never be a Dagger/doDaggerStrategy (btw what is really meant by that?), no matter where Delhi is located on the map.
He just lacks the necessary features (high MaxWarRand, high flavor values for military, Attack UU) and often does MissionaryStrategy which excludes Dagger.

I've taken a closer look at AI_doWar and most of the relevant functions that are called there and tried to put the main checks and criteria for the AI's war planning into comprehensible(?) words (MSExcel). Some of them differ quite a lot from the descriptions in Niklas' post. For simplicity I've neglected the concept of several players forming one team (so 1 player = 1 team, note: Vassals do not join the Master's team, they will still be two separate teams with 1 player each).
The AI's current strategy and financial situation is pretty important for the likelihood of DoWs. Especially the fact that the AI will not even start thinking about any wars when it is following the GetBetterUnitsStrategy might be responsible for the much earlier DoWs on Deity compared to the lower difficulties. The AI needs to be able to build at least 2 offensive units (UNITAIs ATTACK_CITY, COUNTER, RESERVE and PILLAGE) in its capital. Units of the types Axeman, Spearman, Chariot qualify here (basically everything better than Warriors and Archers), so once it manages to hook up the necessary strategic resources it will drop that strategy and start rolling the dice. With the 2 starting settlers and discounts for training more plus the fast teching I think Deities will reach that point 1000 yrs earlier.

The other interesting factors are how Vassals and the financial situation affect the probabilities for wars. I don't really know what to think of the HUSP-factor (HighUnitSpendingPercentage). For boxed in unit spammers this should be huge (compact empire = low total expenses). E.g. checking the 990AD save of Sisiutil's ALC24 shows Mehmed with USP = 12:gold:/57:gold: = 24% (HUSP = (USP-7)/3 = 5) and 2-city-Pacal with 2:gold:/4:gold: = 50% (HUSP = 14) (note: foreign trade is not taken into account!). So they should consider a total war 6 / 15 times as often, but Pacal's power is probably much to low to actually find a valid victim.
It's also interesting how the raiders don't care about the attitude that much (NWR+10), so Boudica will declare at pleased if she is in financial trouble! (Edit: no she won't due to the capping at 99 and her NoWarProb=100)





The take home message (see also Niklas): Power doesn't mean anything to prevent a DoW unless you are over the relevant threshold! Attitude is much more important. If you are a Lonely Heart your are completely safe from Dogpiles and only have to fear LimitedWars from other Lonely Hearts. Have rather short borders. Don't be too close to yourself.

There is a (minor) BUG in AI_doWar as it checks bFinancesProMaxWar for limited raids instead of bFinancesProLimitedWar. There is a MAJOR (well known) BUG in CvPlayerAI::AI_playerCloseness, where the code determines the closeness of the victim to itself instead of the war planning AI's closeness to the victim, so that compact empires with many large cities make the juciest targets (AI_startWarVal). But it also affects the AI's strategy in the beginning of the game (AI_getStrategyHash). After making the first contact with a neighbor the AI will experience a sudden increase of paranoia due to the super (self-)close neighbor and drop Strategy_Peace and might directly jump to Strategy_Dagger. Daggers don't care about their money when it comes to starting a war... They also skip the whole war preparing thing (which others might drop again, see Saladin in ALC24).
The BetterAI guys have created a fix but AFAIK there is no guarantee yet, that the correct version doesn't bring up other unwanted side effects. (There are so many hard coded "x += (condition)? y : z"s and "q *= iNonsense"s in CIV's code that you just never really know what's gonna come out after killing a silly bug!)

To conveniently check and sort the several Leaders according to their WarRands and NoWarProbs or PowerRatios I've put their XML-values into an additional xls spreadsheet coarsely sorted by category. Columns are grouped and expandable, there is a "Game" column where you can put numbers in to filter everything for a current game. Leaders have their BTS-Colors.



I hope I didn't make too many mistakes here, after all this whole war thing is a pretty complex mess.


Edit: some minor corrections:
A) 4. AI is in financial trouble when (Inflated Costs + foreign trade dificit) > 60% of (beakers per turn + taxes income + foreign trade surplus), threshold increases by additional +8% when aiming for cultural victory, +12% when at war or preparing one, +10% when researching Future Techs

D)11. the +10 for NWR also applies for Daggers when AggAI is off (beware of Daggers in financial trouble--not only will they not oppose wars when broke, they will also care less about friends)

E) 2. MaxPlotDist is 72 for Standard Maps (with cylindrical world wrap)​
 

Attachments

  • Leaders_AI_doWar.zip
    62.6 KB · Views: 1,474
Nice work ( I was doing something similar... ).

... but not completely done ;) I still have to see how the AI handles diplo in events and AP votes ( both can lead to insta war and I'm not sure that diplo is taken in account when making that decision )
 
@DanF57771,:goodjob:. Looks very good, glancing over it it confirms what i found myself (and more). I've done some research myself mainly by writing vars like iwarrandthreshold, ihighunitspendpercentage ea to disk for monarch vs deity (save 2 of the ultimate challenge pt II in fact.). While on monarch this i warrandthreshold was always 0 (meaning the RNG has to throw a 0 for war to be considered). On deity iwarrandthreshold went up to 6 (meaning 0,1,2,3,4,5,6 means checking for war) some turns with Gilgamesh due to enormous spending on units(very high ihighunitspendpercentage). I think this answers the question as to why deity ais declare so early, they're able to raise the iwarrandthreshold by ihighunitspendcount, in simple english, they build so much units that going to war becomes a very attractive proposition for them.

There is this other check: getbetterunits, the ai deity begins to consider war earlier than monarch deity but monarch ai passed this check after ~2000 bc too, iwarrandthreshold is much more important in the simulations i've done.

Well the article has been written, i'll do some more simulations also on starts where everyone knows each other so the ai has more choice in picking a victim.

2 things stand out.

- Trying to keep up in power is completely useless for avoiding declaration
unless you can reach the threshold values. In this case you can defend
yourself easily btw.
- Diplomacy is everything, not only is war often canceled if they're pleased.
even if the ai is planning to goto war, and he can goto war to you because
inowarroll > attitude there comes a further check to see which is the
favored victim and there again attitude plays a major role.

So if we get declared on early on Deity it's 99% just bad luck and building axes and barracks is useless in this respect. Just restart or plan to please others right from the start.
 
I was also testing stuff with your ultimate challenge II-2 game. Gilgamesh delivers the worst case scenario here, with Copper + Iron in his superb capital BFC and the Ziggurat @ Priesthood (he build Oracle in 2800 BC for Monarchy and HR and Uruk was size 11 in 2000 BC in my tests!). He immediately switches to Dagger-mode after contacting you (because you are too self-close) and drops GetBetterUnits in ~3400 BC (went BW first). After Ziggurats his total costs drop fast and this is the problem in the early game. Spamming enough units will "raise" his unit costs to something like 2:gold: while his total costs are something like 5:gold:. Although the absolute values are ridiculous, the USP = UnitSpendingPercentage is 40%. So HUSP=11 will lead to a DoW-likelihood increased by factor 12:eek:!!! Maybe the absolute values should be taken into consideration here somehow.
Did you delete all units in the WB-file for your Monarch tests and let the game create them at the start according to the difficulty level (so that Monarch doesn't start with 2 settlers)?

Well the article has been written...
Has Niklas done that, or do you mean my pseudo code snippet collection spreadsheet? I don't consider this an article but more some material to start from writing one. IMHO this topic absolutely deserves to be framed into a nice article for the articles sub-forum. But I'm not much of a writer, so if you should feel inclined (or anybody else) to create one, please copy and paste what ever you like from it.

@r_rolo1: everything connected to the AP is still a mystery to me...
 
Niklas text was that only one post in the SGOTM 7 ( in spite of being better than much of the articles in the Strategy articles forum :p ) and only refered to Vanilla ( where the financial war does not exist, for a example of the diferences )........

Well, dan, i'm also completely off the AP code ;) That is why I asked :p
 
@DanF57771, your findings + Niklas's post provide enough material to write a good article on the subject. There are still things not entirely clear but my 2 majors questions
- does power help to avoid declaration
- why it that we get declared on more often on higher difficulties

have been answered.

There were many misconceptions especially about the first question. I'm glad this is solved now. I'd like to write the article one day but unfortunately i'm a very lousy writer myself.
 
@ Aelf. I like to play my games with a sort of realistic theme. Backstabbing "Friendly" AI's is very rare for me. There are only a few times i will do it.

Personally, I think that if you play a game without caring how you win, you don't have to attack a Friendly AI. You could even make the peace work to your advantage most of the time. If you made a friend accidentally without the least intention to or come to a position where you have to attack a friend to win, chances are something went wrong in the game. It could be luck, but you wouldn't let luck dictate your game, would you?
 
Very interesting.
I was always under the misconception that power rating of 0.5 somehow helps avoiding war. Apparently it is way too low and doesn't really matter if it is 0.5 of 0.1 at that stage.

It does make some games randomly un-winnable unfortunately.
There is also quite a lot of gamble with land blocking - essential on higher levels, but increases the chance of war.

I think the game should be modded so that AI does NOT get bonus on military units before 2000 BC. After that you can try to match them with chopping/whipping etc, so it's a more fair game. They should still get their bonus on non-military units, buildings, and techs, because all these gains can be overcome in the long term with a good play.
However, an early military advantage has just too much short term benefit denying the human player of any reasonable counter in most cases (well, we still have the power to reload but that's not fun).
 
I think the game should be modded so that AI does NOT get bonus on military units before 2000 BC. After that you can try to match them with chopping/whipping etc, so it's a more fair game. They should still get their bonus on non-military units, buildings, and techs, because all these gains can be overcome in the long term with a good play.
However, an early military advantage has just too much short term benefit denying the human player of any reasonable counter in most cases (well, we still have the power to reload but that's not fun).
That will be only possible if the Ai is taught to defend it self properly :p If you take archers for the AI ( not even removing extra units, just replace archers per warriors ) you can warrior rush a emperor AI easily.....
 
Very interesting.
I was always under the misconception that power rating of 0.5 somehow helps avoiding war. Apparently it is way too low and doesn't really matter if it is 0.5 of 0.1 at that stage.

It does make some games randomly un-winnable unfortunately.
There is also quite a lot of gamble with land blocking - essential on higher levels, but increases the chance of war.

I think the game should be modded so that AI does NOT get bonus on military units before 2000 BC. After that you can try to match them with chopping/whipping etc, so it's a more fair game. They should still get their bonus on non-military units, buildings, and techs, because all these gains can be overcome in the long term with a good play.However, an early military advantage has just too much short term benefit denying the human player of any reasonable counter in most cases (well, we still have the power to reload but that's not fun).
Sure agree that modding is needed, i'm slowly working at getting a grasp of C++ but i'm easily distracted by the temptation of just playing another game. It'd be very interesting to work on a team such as the one that's programming a better ai right now but it sure takes a lot of time too.

About the war algorithm I think that most importantly the ai should consider power as a very important factor (as we all thought it did actually) instead of only considering a cut off value.Something like for example:

1.0 we don't declare
0.9 0.1% chance of declaration
0.8 0.2
0,7 0.3
0.6 0.4
0.5 .05
0.4 .07
0.3 1
0.2 1.5
0.1 2.5

Totally neglecting power would hurt this way.I just thought up these numbers, you see where i'm getting at. Together with your idea of not giving the ai military bonuses before 2000 bc it gives the human a chance to compete even on deity, there's always the chance that you get so far behind in tech by building units that you can't win but at least on deity, this way you have a choice how to play.
 
The following changes would make the most sense for me:
  1. fix self-closeness bug, so that the neighbors don't get paranoid at first contact --> paranoia==0 prevents DaggerStrategy and prolongs PeaceStrategy (also target selection makes more sense)
  2. HUSP should be capped at UnitCost to prevent insane early increases of iWarRandThreshold;
    something like iHighUnitSpendingPercent += (std::min(GET_PLAYER((PlayerTypes)iI).calculateUnitCost(), std::max(0, iUnitSpendingPercent - 7) / 2));
  3. change the effect of power from threshold to chance
    so for instance for a TotalWar:
    Code:
    [SIZE="4"]int iDefensivePower = (GET_TEAM((TeamTypes)iI).getDefensivePower() * 2) / 3;
    int iOurPerceivedPower = (iOurPower * ((iPass > 1) ? AI_maxWarDistantPowerRatio() : AI_maxWarNearbyPowerRatio())) / 100;
    if (GC.getGameINLINE().getSorenRandNum(100, "AI Power Deterrence") >= (iDefensivePower * 100) / iOurPerceivedPower - 30)
    ... (valid target)[/SIZE]
This is just a suggestion and would look like this in a figure (red = old/threshold; blue = new/chance).



So if my defensive power (adjusted with *2/3 for TotalWar) is 80% of the perceived power of the AI, there is a 50% chance that the AI will not consider me a valid target (Deterrence Success). I need 130% to be completely safe, if I'm really weak (<=30%) the AI will just laugh at me...
This might make the game a bit more peaceful overall.

Maybe the power ratio should also be taken into account in AI_startWarVal to determine the best of the possible targets.
 
Hey, I'll add another :goodjob: to all who analyzed the AI DoW mechanics... especially DanF for the spreadsheet and details...

I guess almost important as when the AI declares war is how it selects the primary target city. I have some hypothesis, but honestly I don't even know where to look to find the real AI code for this. To me, understanding and countering this behavior can make a huge difference in the outcome of the war... if you can arrange your logistics trail to reinforce these citie(s) quickly, you stand a much better chance against the AI.

I've noticed that if you share a long border with the attacker, it seems that the AI has a fairly strong preference for alternately cycling attacks on the two farthest-most cities on that border... essentially it seems that the AI likes to try to stretch your defenses along long borders and hopes you eventually get enough troops out of position that they can take a city with their stack. Other times, they seem to beeline for a capital or major wonder like SoZ.

Any thoughts on where I can find where this is actually coded?
 
Any thoughts on where I can find where this is actually coded?
I think it's in CvPlayerAI::AI_targetCityValue, or it's at least a good point to start from.
 
Another thought: Has there been a look at the decision mechanics that cause an AI to call off a planned war? I notice that sometimes an AI goes into WHEOOHRN but does not follow up with actually declaring war, what is the cause of that?

Thumbs up for the work done so far.
 
WHEOOHRN means that the AI changes its war plan for another player from NO_WARPLAN to one of the following:
  • WARPLAN_PREPARING_LIMITED
  • WARPLAN_PREPARING_TOTAL
  • WARPLAN_LIMITED
  • WARPLAN_TOTAL
  • WARPLAN_DOGPILE
Normally it enters a phase of preparation (WARPLAN_PREPARING_LIMITED, WARPLAN_PREPARING_TOTAL). Then it also changes the battle plan that is somehow linked to its homeland (capital area) from AREAAI_NEUTRAL to AREAAI_MASSING or AREAAI_ASSAULT_MASSING depending on whether the victim is located on the same landmass or not. After a fixed number of turns it checks whether it has accomplished to spam enough offensive units to attack (I have also described it in this post to understand what was going on in Sisiutil's ALC#24). If yes the battle plans change to AREAAI_OFFENSIVE / AREAAI_ASSAULT and the war plans change to WARPLAN_LIMITED or WARPLAN_TOTAL which is followed by the actual DoW. If the AI's empire is too big (like Saladin's) and the army is not ready after the allowed number of turns of preparation it calls off all war plans and changes back to NO_WARPLAN (no more WHEOOHRN).
Note, if the AI is following the Dagger strategy at the moment it chooses a war, it skips the whole preparation for total and limited wars and will somehow be ready instantly and DoW shortly after.
 
DanF, thanks for the detailed explanation.
What is Dagger strategy exactly and how does the AI decide to use it? Do only specific AIs apply this strategy, or anyone can depending on some conditions?

Does dagger mean that there is no WHEOOH message before the war?
 
What is Dagger strategy exactly and how does the AI decide to use it? Do only specific AIs apply this strategy, or anyone can depending on some conditions?
Every AI player chooses a set of strategies according to its personality and the current situation in the game. There are 20 different strategies, iiuc they mainly determine what the AI chooses to build in its cities, how it adjusts its sliders and how it behaves when it comes to starting wars:
  1. AI_STRATEGY_DAGGER
  2. AI_STRATEGY_SLEDGEHAMMER
  3. AI_STRATEGY_CASTLE
  4. AI_STRATEGY_FASTMOVERS
  5. AI_STRATEGY_SLOWMOVERS
  6. AI_STRATEGY_CULTURE1
  7. AI_STRATEGY_CULTURE2
  8. AI_STRATEGY_CULTURE3
  9. AI_STRATEGY_CULTURE4
  10. AI_STRATEGY_MISSIONARY
  11. AI_STRATEGY_CRUSH
  12. AI_STRATEGY_PRODUCTION
  13. AI_STRATEGY_PEACE
  14. AI_STRATEGY_GET_BETTER_UNITS
  15. AI_STRATEGY_LAND_BLITZ
  16. AI_STRATEGY_AIR_BLITZ
  17. AI_STRATEGY_LAST_STAND
  18. AI_STRATEGY_FINAL_WAR
  19. AI_STRATEGY_OWABWNW
  20. AI_STRATEGY_BIG_ESPIONAGE
Everybody starts with AI_STRATEGY_PEACE in the beginning, but this actually has no effect on anything (nothing implemented in the code). As mentioned earlier in this thread there is some pseudo randomness in the form of the iNonsense variable which depends on the position of the AI's capital on the map (it's the sum of the capital's X and Y coordinates, so it is fixed for the whole game as long as the capital isn't moved). Its influence is mostly implemented in the form x = y + (iNonsense%z), with % being the modulo-operator, so it allows variation in the range [0...(z-1)].

The requirements for AI_STRATEGY_DAGGER are:

general:
  • not AI_STRATEGY_MISSIONARY
  • "early" in the game ( iCurrentEra <= (2+(iNonsense%2)) which means upto Medieval / Renaissance )
  • must have met others
  • must have iParanoia > 0 (due to the self-closeness bug this is always true after the AI has made contact with someone it is cautious or lower with)
Leader's personality:
  • MWR = MaxWarRand, FM = AI_FLAVOR_MILITARY, NF(NonsenseFactor) = (iNonsense % 11)/10 :
  • iDagger = 120 * 100/(MWR+50) * NF + 5 * Min(8, FM);
    Example: Ragnar MWR = 50, FM = 10, Nidaros @ (X=30,Y=31) --> iNonsense = 30+31 = 61; NF = (61 % 11) / 10 = 0.6
    iDagger = 120 * 100/(50+50) * 0.6 + 5 * Min(8, 10) = 72 + 40 = 112;​
iDagger is further modified for military Unique Units (all except Indian Fast Worker):
  • -10 for defensive UUs (Mali Skirmisher, Babylon Bowman)
  • Capital can build UU?:
    +40 (+10 for defensive UU)
    +20 for each :strength: the UU is stronger than the replaced unit (WarChariot, Vulture, Praetorian, Cataphract, East Indiaman, no penalty for Jaguar)
    +20 if UU has no PrereqTech (Incan Quechua)​
  • Capital cannot build UU?:
    +10 / +30 if PrereqTech is / is not known
    +20 if AI has all required resources to build UU​
AggAi off? +5 / +10 / +15 for Prince / Monarch / Emperor+

already at war (AREAAI_OFFENSIVE, AREAAI_DEFENSIVE)?
  • +40 if capital can build offensive units (UnitAIs: RESERVE, ATTACK_CITY, COUNTER, PILLAGE)
  • +20 if capital cannot build offensive units (-->AI_STRATEGY_GET_BETTER_UNITS)

CHECK: iDagger >= AI_DAGGER_THRESHOLD (100) --> AI_STRATEGY_DAGGER!

So all the warmongers with strong offensive UUs are likely to follow Dagger strategy until medieval/renaissance era.

Example1: Ragnar from above can build Berserkers in Nidaros, AggAI off Immortal difficulty, not at war
iDagger = 112 + 40 + 20*0 + 15 = 167 >= 100 --> AI_STRATEGY_DAGGER

Example2: Willem (MWR=100, FM=0), Amsterdam @(44,12) can build East Indiaman, AggAI off Deity difficulty, not at war
iDagger = 120 * 100/(100+50) * 0.1 + 5 * Min(8, 0) = 8 + 0 = 8; for personality
iDagger = 8 + 40 + 20*2 + 15 = 103 >= 100 --> AI_STRATEGY_DAGGER

Example3: Gandhi (MWR=400, FM=0), Delhi @(22,21), AggAI off Prince difficulty, currently fighting defensive war, can build Musketmen (UNITAI_RESERVE)
iDagger = 120 * 100/(400+50) * 1 + 5 * Min(8, 0) = 13 + 0 = 13; for personality
iDagger = 13 + 5 + 40 = 58 < 100 --> no Dagger

Does dagger mean that there is no WHEOOH message before the war?

When it comes to war, IMHO the main effect of AI_STRATEGY_DAGGER is the skipping of the preparation phase, which means that Dagger-AIs that changed war plans to Limited or Total Wars will never call those plans off again (so Saladin was no Dagger in ALC#24). I'm not really sure about what determines the interval between the war plan change and the actual DoW (=you get WHEOOHRN before the war). I guess it depends on where their SoD is located and how long it takes to march it to your borders (DoW = crossing borders). If there are any obstacles in the SoD's path (like impassable mountains) it could be that the AI is locked in WHEOOHRN forever.
 
Top Bottom