View Full Version : [BTS] AI Strategy - war


glider1
Jun 22, 2008, 10:55 PM
This sub forum of the "Better than BTS" project is for discussing AI behaviour regarding the strategy of warfare. This pertains to things like border war declarations, current problems and future possibilities for new war plan behaviours as well.

The discussion of "war tactics" is a huge area as well and has it's own sub-forum.

glider1
Jun 22, 2008, 11:00 PM
I looked into the chances for Friendly/Pleased war declarations a little more. In AI_doWar there's a odds check against AI_noWarAttitudeProb before the startWarVal is even computed ... each AI has their own % of the time they will not even consider a DOW for certain attitudes specified in XML under <NoWarAttitudeProbs>. None of the AIs will initiate a war when they are Friendly to you which is good. About half will also never start a war against you on Pleased, for the rest the odds are mostly 10% with a smattering of 20%.

The chances for a Pleased declaration of war on a neighbor if the 10-20% chance check passes are now much higher with the Closeness fixes, but the initial check makes this still a fairly rare event. On aggressive AI those 10-20% chances are more like 20-30% and the differentiation in startWarVal for close vs non-close civs means you really can't really trust your neighbors unless they're Friendly or engaged in other wars.

When the AI is Cautious towards you the odds of checking startWarVal range from 80% to only 10% for Sitting Bull. This is the scenario where most of the changes in behavior will occur I think, the AI will pick a war against a neighbor they're Cautious towards over a distant Annoyed much more often now. On Aggressive AI they will even be fairly likely to declare on a neighbor they're Cautious towards over a distant Furious.

This is an example of the type of content relevant to this sub forum. As far as I can perceive, this is all good news for Better BTS build 0.11 at least in the middle ages. A distant furious war declaration will still occur in terms of diplomacy objectives, but that doesn't override local border considerations it would seem. Of course it's not that simple once long range and cooperative warfare becomes possible late game.....

Sigh, there is still a lot of testing to do...
Cheers.

Yakk
Jun 23, 2008, 01:46 PM
One serious crippling part of the AI is the inability to actually gauge the relative power of two empires.

Currently, "power" that the AI uses is the sum of the "power" stat of units, technologies and buildings. This is ... absolutely horrid at guaging the military power of an empire.

We can:
A> Change the meaning of Power. This would impact in-game graphs, etc.
B> Produce a Munged Power value, and replace strategic evaluation of Power with this.
C> Other

Option <A> opens up more, new knowledge to players.
Option <B> requires more finness. Do we pay attention to the Espionage knowledge of a side? (I think currently the AI cheats, and knows power even if it doesn't have the Esp, right?)
Option <C> is wide open.

jdog5000
Jun 23, 2008, 04:37 PM
The AI definitely knows power regardless of espionage ... this isn't too much of a cheat to me, the human player is able to judge roughly how strong another empire is based on observed history, techs, size, etc even without the power graph. Personally I have never really used the power graph to decide ahead of time whether to declare war, if things aren't going my way quickly I might check to if my enemy's graph has dropped to help judge if things are headed for a stalemate or whether another offensive will break through.

I noticed that the perceived power and "asset" value of units was changed across the board in XML for 3.17 ... don't know quite why, didn't seem to be mentioned in the change list. What does the "asset" value do anyway?

mjs0
Jun 27, 2008, 07:27 AM
For those who are not familiar, there is a good description of how power and other demographics are currently calculated in this article (http://www.civfanatics.com/civ4/strategy/demographics.php)

mjs0
Jun 27, 2008, 08:00 AM
I have played several games recently using the excelllent tectonics map script from the new patch.
This script is particularly good at generating interesting landmasses with many choke points, unfortunately the AI seems to have no appreciation of either the value or danger of choke points.
This would be an area in the AI's strategic 'thinking' that would certainly benefit from some scrutiny.

For example, the mongolian hordes were decimated in attempts to take this fortress

http://forums.civfanatics.com/attachment.php?attachmentid=181341&stc=1&d=1214574448

and Tokugawa's samurai threw themselves in vain at this mountain pass (of course he created some great CG3/Guer2 longbowmen for me in the process!!)

http://forums.civfanatics.com/attachment.php?attachmentid=181344&stc=1&d=1214574915

Note that the fortress on his side of the pass was built on a stone resource outside any BFC, not because he recognised the strategic value of the location.

Yakk
Jun 27, 2008, 08:34 AM
There are AI algorithms for recognizing chokepoints. That would be useful defensively...

Offensively, chokepoints might be dealable with tactically. Ie:

Don't attack an enemy stack if the battle will almost certainly fail both tactically and economically using available resources.

mjs0
Jun 27, 2008, 08:49 AM
There are AI algorithms for recognizing chokepoints. That would be useful defensively...
Very Interesting. I wonder if that goes as far as settling cities to deliberately control those chokepoints and whether those checks are performed during peace as well as war and most importantly then influence worker/unit behaviour.
How sophisticated can the AI be in this area without a significantly improved capacity for long-term planning, coordination and goal setting.

Not directly related to my original point, but as a human player I often settle cities sub-optimally as psuedo-choke points (to neighbours with whom I refuse to open borders) to preserve a swathe of land for later settlement when I can afford the extra city costs. I have not observed the AI doing anything similar deliberately.

armand453
Jul 04, 2008, 12:02 PM
I'm looking for the function which calculate the power of a country or a stack. I didn't find it can someone give me it name please?

Armand

Yakk
Jul 05, 2008, 09:44 AM
I'm off to breakfast, but country power is tracked via an updated delta-function called something like CvPlayer::GetPower(). It doesn't do the calculation -- when things that add or subtract from a player's power happen, they just update the Power counter on the Player.

Stack... might be a plot power function, can't recall.

Iustus
Jul 05, 2008, 01:19 PM
(I am not looking at the code, but going from memory.)

We added something for BetterAI that would look at whole stack vs whole stack odds, when deciding whether to attack, rather than just look at the odds of one unit vs a stack. I am not sure how much of this survived into BtS, as I have not looked.

The way this works is a bit convoluted as I recall, going through several functions, so I would suggest studying it closely, or even stepping through it a few times to understand how it works.

I believe the function(s) are named something like getCombatOdds, or perhaps that is the name of the low level function that the one you care about is calling. Do a search for 'odds'

-Iustus

glider1
Jul 05, 2008, 05:43 PM
I have played several games recently using the excelllent tectonics map script from the new patch.

It's a great map isn't it? It almost feels like it was included by Fireaxis as a challenge for us here to improve the AI. The choke points and terrain structures are a wonderful testbed for building a truly clever intelligence (SIDE NOTE: from looking at the placement of bonuses it looks like the map script has been adjusted for better late game corporate balance *I think*)

In your example, it seems sad that the AI only built that fortress for the stone and not because of it's choke point promise...

You'd think there would be a fairly achievable way of getting the AI to understand choke points but it probably is not simple in reality. As a quick pass you could do this to implement something short term:
1) Once x number of border cities have been established to y-culture execute 2)
2) If not at war with no threat of war execute 3)
3) Discover the extent of "terrain holes" on my now well established borders
4) If terrain holes are few enough execute 5)
5) Decide how to plug the terrain holes (forts verses units verses treat hole with the same defense priority as a city)
6) Plug them quickly....

Without a strategic planning AI, once 6) has been executed the AI simply returns to generic behaviour but keeps a track of the "holes" and refills them as necessary throughout it's remaining existence whenever it is not at war. It will of course do potentially stupid and unneccessary things like "overdefend it's interior cities" but at least it covers for the possibility of invasion despite terrain security. The algorithm beyond step (2) above is only calculated once for every "unique" body of culture the AI possesses or if the body of culture has expanded by a certain amount where upon the algorithm is re-executed beyond step (2). The complexity of this algorithm is probably on the same par as "amphibious invasion" if not more.

As for terrain funnels, that is too much of a challenge for this existing AI I think. However all is not lost because perhaps terrain funnels produce interesting "generic" behaviour in the AI anyway.

This is unsatisfactory of course, but without strategic planning what does an AI do? In theory if it could plug all the holes and choke points, it could concentrate military at these points and areas then underdefend it's cities and concentrate on it's economy...

Cheers.

jdog5000
Jul 06, 2008, 11:33 PM
(I am not looking at the code, but going from memory.)

We added something for BetterAI that would look at whole stack vs whole stack odds, when deciding whether to attack, rather than just look at the odds of one unit vs a stack. I am not sure how much of this survived into BtS, as I have not looked.

The way this works is a bit convoluted as I recall, going through several functions, so I would suggest studying it closely, or even stepping through it a few times to understand how it works.

I believe the function(s) are named something like getCombatOdds, or perhaps that is the name of the low level function that the one you care about is calling. Do a search for 'odds'

-Iustus

Great, thanks for the clues ... I'll definitely dig that up.

Nor Me
Jul 08, 2008, 02:45 PM
The current power calculation is not all that good but I'd worry that the AI doesn't use it enough. For declaring war, there's a simple inequality that's weighted in their favour. Even the most cautious AI's will attack civs with considerably more power. Bts made them consider attacking with 2/3 the power they would have in Warlords. Would putting the ratio in AI_startWarVal be a good idea. A decent minimum might stop the weakest player being dogpiled but still make the AI more wary of stronger civs.

Nor Me
Jul 08, 2008, 04:12 PM
AI_startWarVal is basically the sum of:
1> How much closeness we have with them.
2> The value of the tiles near the border between us and them
3> How close our capitols are.


For some reason, along with 2), the AI counts the number of bonuses in our empire. That's with no qualification about distance or landmass. This means AIs like attacking big civs. With the generousness of the AI power comparison, this is a good recipe for suicidal wars.
Of course, the bug in 1) might make this effect smaller than I'd thought it was previously.

jdog5000
Jul 08, 2008, 04:56 PM
CvTeamAI::AI_calculatePlotWarValue is actually a bit deceiving ... while it seems to loop through and sum extra pieces for every bonus, GC.getBonusInfo(eBonus).getAIObjective() returns 0 for all bonuses. The iAIObjective XML tag for bonuses was introduced in Warlords but is always 0, even for oil. This was not changed in BTS. You can easily verify that this bonus code does nothing by looking at the values produced by CvTeamAI::AI_calculatePlotWarValue ... mouse over a civ's capital in debug mode and hold down CTRL and you'll see:


Opponents Name(6): city closeness (player closeness, team closeness)
String about war state: start war val (plot war value, capital Proximity, attitude and war check %)


The plot war values will make it clear that the AI is never adding 40 * anything for the other players resources ... it's only 4 * number of other teams' tiles adjacent to this player's territory.

So, as it stands right now the AI does not choose wars for resource reasons at all ... this is something that we should definitely change. I think we should try re-instituting the iAIObjective tag for a few strategic resources and then change the code in CvTeamAI::AI_calculatePlotWarValue so the AI will pick wars where it might pickup important resources it doesn't currently have. This function is only called when picking war victims so we can afford to spend a little more compute time to weigh enemy resources by need and proximity.

Frankly we might as well move the resource calculations to a new function so it's easy to see the new resource value. My sense is that for a really desirable target (say multiple nearby oil resources when this civ has none) the return value should be between 50 and 80 so as not to upset the balance between the existing components of startWarVal.

So, we'd need to assign iAIObjective values for each resource type plus determine good formulas for how badly the AI needs the resource and how close it is. Doing a quick search for getBonusType returned a bunch of places where the AI makes choices based on bonuses, there should certainly be some opportunities for adapting ideas from other places plus using what we come up with in more places as well. In particular, CvPlayerAI::AI_targetCityValue (as the name suggests, it is used to pick cities to attack) right now simply counts bonuses in the fat cross with no regard for whether it's the oil we don't have or yet another sources of cows ...

Nor Me
Jul 08, 2008, 06:57 PM
The iAIObjective XML tag for bonuses was introduced in Warlords but is always 0, even for oil.

That'll teach me to actually look at the XML some days. I presume it's for mods where resources might be more victory critical.

Opponents Name(6): city closeness (player closeness, team closeness)
String about war state: start war val (plot war value, capital Proximity, attitude and war check %)


Nice. I've not had a chance to play with your mod as I only revisited this forum today. As I posted in the other thread, I've been using a modified version of the better AI war information mouseover which didn't break down start war val. But then, I was trying to mod the overall chance for war.

so as not to upset the balance between the existing components of startWarVal.

Balance? If all of the changes to this function since the original CIV have either done nothing or nothing like what was intended, then you can reasonably do you want with it and not be too different.

armand453
Jul 11, 2008, 03:58 PM
There is something I often saw, when civ are at war they don't produce military unit sometime in their city, I mean that sometime 3/4 of their city are in research production.
Where does it come from?

Armand.

jdog5000
Jul 11, 2008, 07:32 PM
Depends greatly on the situation ... I've seen this mostly for intercontinental war and am currently working on improving/fixing exactly this issue (see the Amphibious thread).

If you've seen this for local wars, please post a save so we can check it out.

I don't know if the AI ever builds research if there's something else useful it could be doing, I'll check into that.

Nor Me
Jul 12, 2008, 06:06 AM
It stops building units for attack when it thinks its unit costs are too high. It stops building units for defence when it thinks it has enough. It will then build research when it runs out of buildings.

The same formula for unit costs is a major part of the AI's decision about whether to go to war.

This is not a bad idea in principle the implementation isn't perfect. It takes the unit cost and supply as a percentage of the total cost. So a small empire, which will have low maintenance, will build less units even as a proportion of the size of its empire than a large empire. As it thinks it has a lot of units, it will then go to war which might be suicidal. A large and underdeveloped empire with high costs will spam units like there's no tomorrow even if its economy is in trouble. On the other side, since Better AI, the upgrade discounts the AI gets are much lower than its unit cost and supply discounts especially on the higher difficulty levels. Then the AI may build so many units that it will fall behind in science trying to upgrade them all.

I've been experimenting with a formula which includes an a guess at the average upgrade cost over the game and takes the costs as a percentage of its economy (i.e. mostly science and gold per turn minus the costs) rather than costs. It seems to work better. Some exceptions to the maximum cost rule might also be a good idea.

Grames
Aug 13, 2008, 10:55 PM
Is it possible to get the AI to read trends on the graphs so it can make better decisions about staying competitive in pop., gnp, production, power etc? This would leverage a 'memory' which is built in to the save game.

I coud say more, but unless it is possible there is little point.

ocitalis
Aug 15, 2008, 06:07 PM
Is it possible to get the AI to read trends on the graphs so it can make better decisions about staying competitive in pop., gnp, production, power etc? This would leverage a 'memory' which is built in to the save game.

I coud say more, but unless it is possible there is little point.

IIRC int CvReplayInfo::getPlayerScore(int iPlayer, int iTurn) can be used during the game to retrieve the score from a given turn. From the values returned it would be possible to determine trends, although it would take some work to get "trend recognition" working, and even more to make the AI react to a given trend in a given situation.

jdog5000
Aug 16, 2008, 12:23 AM
Yes, you can get all of the info graph data in the way ocitalis mentioned ... food, power, score, GNP, etc. Finding the right formula to detect trends though might be tricky, many of these things tend to have sharp changes when a civ goes into/out of a golden age, loses/gains a vassal, etc.

Cybah
Aug 20, 2008, 11:32 AM
I would like to see temporary "war alliances" to defeat/decrease the power of a dominating civilization. atm it's only possible by religious votes.

Like: If Power(civ1)+Power(civ2) > Power(dominating civ) which is currently at war (so opponent+civ1+civ2 >>> domination civ) -> civ1 will 'ask' civ2 for a war vs dominating civ

jdog5000
Aug 20, 2008, 11:32 PM
That's a good idea. I haven't looked at all into how the AI chooses to ask for allies in war, I know that those joining a fight aren't as scared off by a powerful adversary as they would be if the potential enemy wasn't already engaged ... the power comparison rules for using a DOGPILE war plan (ie joining a war) are very different than for starting a LIMITED or TOTAL war. I'll report back what I find.

Dearmad
Aug 27, 2008, 10:07 PM
I think this idea is not one to be added with out a lot of thought if at all. It flattens out personalities in the game and makes the game more gamey... which isn't always fun. Grudges, past relationships, and trust are concerns here.

If civ 1 is winning, and civ 2 and 3 are weak and clearly going to lose militarily. What if Civ3 which has always been friendly with civ 1 took another route: "Hey let's ally with civ 1 and win together!" I see this sometimes in my game. Now if both civ 2&3 always sort of hated civ 1 and do still now... then sure, it makes sense for them to team up.

This already happens in the game relatively well, but maybe enforcing the direction they are ALREADY leading is would be ok. I don't want all my hard fought for diplomacy over hundreds of years to come for naught simply because I am more powerful.

Yakk
Aug 27, 2008, 11:39 PM
LoveScale = 0 at -5 or lower, 1.0 at 10 or higher (Ie, (Attitude+5)/15, capped)
HateScale = 0 at +5 or higher, 1.0 at -10 or lower (Ie, (-Attitude-5)/15, capped)

GlobalDangerHate = HateScale * ((Percent Of World Power Empire Controls)*(Number Of Known Empires) - 1) * 5, capped from below at 0 and above at 5.

Ie, if you are twice as strong as the average world-wide empire, this works out to HateScale * 5.

GlobalDangerLove = LoveScale * [Worst mutual GlobalDangerHate]

This generates a tendency to gang-up on leaders, but if they already like you, they won't consider you to be a global danger. In fact, they'll ally with you against the "real" global dangers.

It just means that if there IS a global danger, then Empires which have any disliking for that empire will start to like each other more. And they won't have extra fond feelings for the allies of the global danger, leading to a possible additional tendency for the world to divide into two opposing power blocks...

This doesn't include the "you keep on betraying your friends, I shouldn't trust you" effect.

phungus420
Aug 28, 2008, 02:05 AM
This doesn't include the "you keep on betraying your friends, I shouldn't trust you" effect.

This would be a good one to add. Currently the AI will trust and love any civ at friendly, wol't ever cross them. That's mostly good, I've only backstabbed a friendly AI a couple of times (only when going for conquest and they wol't ally, not much reason to otherwise). However not everyone plays like that, so it would be good for the AI to react if it sees a player attack, instigate a war, or otherwise backstab a "friend" and act accordingly. Hell it might not even be that bad an idea to make Mad Monty and maybe Peter play like an aggressive human player, have them consider attacking a friendly Civ if they are locked into a domination victory.

scu98rkr
Aug 28, 2008, 03:28 AM
What would also be good is personallities which evolve or expand.

For instance if a non-war mongering Civ is attacked(possibly back stapped) but comes out of the war in a better position the Leader should be more likely to start a war in the future.

For instance if particular aspects of there personality brings results this aspect should be strengthed.

This would lead to a war monger with a lot of vassels slowly becoming more and more meglomanical. So they would be more likely to go for conquest / domination.

All you'd have to do for this is run a check to see how there score changes from start to finish of a war.

If it improves they should be more likely to declare war.

If it lowers they should be less likely to declare war in future.

I think this would really help civ repeating the same mistakes again and again, often if an early war mongers rush fails they just need to give up on that idea.

I do like the idea there should be a "Backstap" rating for every one too.

Cybah
Aug 28, 2008, 04:30 AM
"Hey let's ally with civ 1 and win together!"

sorry, this is wrong. if there is a dominating civ, the other civ will be to WEAK to get into an alliance with the dominating civ (quote: "I don't need help." (or something like this)). there is absolutely no chance for a win for civ1 if civ1 won't start a dogpile war.

doktorstick
Aug 29, 2008, 07:42 AM
Should leader personalities be taken into account in making these sorts of global love/hate decisions?

Dearmad
Aug 29, 2008, 09:17 PM
sorry, this is wrong. if there is a dominating civ, the other civ will be to WEAK to get into an alliance with the dominating civ (quote: "I don't need help." (or something like this)). there is absolutely no chance for a win for civ1 if civ1 won't start a dogpile war.

Sorry, it's not wrong. It's happened again and again in my games. It depends on the relationship to that point- if it's been trusting, mutually religious, etc... they can join up.

Also depends on if you have permanent alliances running. Which can factor heavily into how the AI should play if it is to play smartly.