View Full Version : New experimental BetterAI version : more human-like and warlike behaviour


klokwerk
Jun 07, 2007, 06:07 AM
While we wait for the new expansion (BtS), I thought I could mess with the latest BetterAI version (Warlords version only for now). It's far from completion but I already modified a few things and I will probably upload the DLL soon (this evening or tomorrow I think).

It's not at all an 'official' betterAI version, right now it's just an evolution of the latest BetterAI to something more warlike.

The idea is also to make the AI more human-like with things like :
- war declarations depending more on opportunities (closeness, city values) and a bit less on official attitudes
- being more conscious of who is about to win and what they can do about it
- more generally trying a bit more to win and a bit less to roleplay

Many people complained about the AI passivity in the latest builds, I fixed that and there are now many more wars.

The things I changed in this version :
*) bug fixes :
- minor bug where 'limited wars' were evaluated incorrectly (they used a 'total war' percentage instead)
- possible wars were not analyzed when the AI found no target for another kind of war
- corrected the way distances were evaluated in CvTeamAI::AI_calculateCapitalProximity according to Nacht's finding (kudos to him !) : this bug was making distant civ much more attractive as war targets.
- debug (chipotle) : several bugs in the war chances displayed with 'Alt'.

*) changes :
- more total wars now, especially when enough units are available
- many more dogpile wars : dogpile wars are now evaluated first, so they're now preferred over total wars
- slightly more limited wars
- 'attitude' weight reduced in deciding who AI should attack. A real player doesn't attack only if relations are 'furious', especially when there are better opportunities closer to him.
- 'closeness' weight greatly increased in deciding who AI should attack. With naval invasions still a bit difficult for the AI, it's often much smarter to attack a closer neighbour. It's also much quicker and better for finances.
- debug (chipotle) : added war chances 1st check (often very high) in the things displayed. Ignore this number.

*) known issues left (which were already in BetterAI latest official version) :
- sometimes an AI decides to attack and the war won't start even dozens of turns later. It's due to the impossibility to reach the chosen enemy to start the sneak attack. I'll try to fix this one soon, but it's probably hard.

The next things :
- continuing to tweak the agression level, depending on feedback
- giving the ability to start more than one war at a time when the first war will clearly be won.
- understanding other parts of the war code, including the 'ask for peace' code and the units evaluation (which decides partly total war chances), to see if I improve something.
- bug fixes
- thinking about giving goals like diplo and domination victory
- further AI vs AI testing (automating that too ?)

EDIT : Ok folks here is the first version. It's really in alpha stage, but the agression level should be much better

http://forums.civfanatics.com/uploads/99614/BetterAI_Warlords_Asserts_Unofficial_07-06-04.zip

Feel free to comment, give other ideas or remind me of other bugs in the latest BetterAI official version.

Many thanks to Firaxis for this great game and to Blake and Iustus for their awesome work on the AI.

scu98rkr
Jun 07, 2007, 08:33 AM
Sounds good to me.

Personally I like there to be plenty of wars and was slightly disappointed with the last Better AI release, in which the AI aggression seemed to have been toned down a lot.

Anyway Im looking forward to downloading it great work.

Roland Johansen
Jun 07, 2007, 08:36 AM
Good to hear that you've picked up the ball and are trying to improve the AI even further. Of course, you might have to redo all of this when Beyond the Sword arrives. Still, a good idea that works for Warlords is probably still a good idea for Beyond the Sword.

Two comments:

1) Don't completely remove the attitude part in war declarations. I think it is good that the AI will sooner attack a near civilization than a far away civilization, but if at some point the AI almost ignores the relations, then diplomacy will become a less meaningful element of the game and that is surely not something that you would desire.
By the way, I don't think you were planning to completely remove the attitude part from the war declarations.

2) I've read that the AI in Beyond the Sword would be improved and one of the few things that mentioned explicitly was naval invasions. But even if this were not the case, then still an AI should try it sometimes. What if an AI on a continents map were able to conquer an entire continent (which would be great). Then surely it would be immensely powerful compared to civilisations that share a continent. It should try a naval invasion of the weakest civilisation of another continent and go for world domination. If it is in the late game, it should quickly create airports on the enemy continent to transport its army to the other continent. Will it do this with your present ideas?

Good luck! :thumbsup:

klokwerk
Jun 07, 2007, 09:18 AM
Thank you for your comments. I'm note sure I can 'improve' BetterAI so much, Blake and Iustus made obviously a great work, and I'm even not a developer, but I can certainly change a few things according to the taste od some of us.

scu98rkr : I agree with that. Now the agression is maybe too high, but we'll see when we test it. I didn't change anything in the 'power check' right now, so the AIs won't be suicidal either, they won't attack if they're not strong enough to beat their enemy. They'll just evaluate possible wars more often.

Roland Johansen :

1) on the attitude in war declarations : I totally agree with you. Before my changes attacking a civ you were 'furious' with was valued 8 times more than attacking a civ you were 'pleased' with (and 16 times more than attacking a 'friendly' civ). This number is mixed with closeness, so basically you could go eight times more far to attack this civ, which was far too much I think (AI always chose the bad attitude civ by a wide margin), so I reduced that but certainly not eliminated this factor. The factors are now something like 1 / 2 / 3 / 4 / 6 (from friendly to furious) instead of 1 / 2 / 4 / 8 / 16 so there's still a nice attitude weight, especially considering the distances between civs don't vary so much. The results I get seem more human-like, but further tests are clearly needed.

2) Exactly. In BtS AI will be improved and we'll probably all restart our work from scratch. If Naval invasions work, the closeness weight should be reduced again. But even now with my changes, the AI will still try sometimes distant naval wars (at least that's what I saw in my tests on a late game). It's just that it will look at the closer opportunities more realistically...

Right now I made no changes to armies or buildings, but I'd surely like to work on this part too... I don't know if it's building airports correctly right now to help in its invasions, but I write down the idea immediately.

Roland Johansen
Jun 07, 2007, 10:02 AM
Thanks for the quick response.

Sometimes you see the AI do strange war declarations. It has someone it could easily beat and doesn't like but it attacks a stronger civilisation that it doesn't like. This is not realistic from a role playing point of view nor is it good from a smart human like point of view. I haven't looked at the code at all, but I guess it just has to do with random number rolls. The AI is more likely to attack the weaker civilisation than the stronger civilisation, but because of a bad random number roll with the weak civilisation and a good one with the stronger civilisation, it decides to go for the stronger one.

Now, we don't want to make the AI predictable. If two civilisations are both disliked and both near to the AI controlled civilisation and one is just slightly stronger, then it should not always go for the slightly weaker one. Some randomness is good.

The likeliness to attack is probably some number based on attitude, distance and strength of the opponents, the higher the number, the higher the likelihood of attacking that civilisation. I suggest that the AI doesn't declare war on civilisations which have a number twice or three times as low as the civilisation with the highest number. That way, there is still some randomness in who the AI declares war upon, but it won't accidentally declare war upon an unlikely opponent. Because in my opinion, it is these few strange war declarations that stop the AI from behaving 'realistic'.

Good luck. :thumbsup:

klokwerk
Jun 07, 2007, 10:57 AM
Exactly, the strange declarations you see are due to the rather strange system itself. Maybe it would be better to design another algorithm from scratch, but I won't do it for now. Maybe after BtS if it doesn't change.

The algorithm is :
- if I've got a war, don't declare another (I'll try to change that very soon, a human can easily dogpile a very small civ AND face a bigger one, now this system slows down the domination victory because of pointless small wars vs pushovers)
- otherwise must I declare a war ? First there's a general war check (often yes)
- Then a total war check (rand vs a number depending on personality and units / power). Yes -> choose enemy if possible, No -> next check
- Then a limited war check (rand vs a number depending on personality and units / power). Yes -> choose enemy if possible, No -> next check
- Then a dogpile war check (rand vs a number depending on personality and units / power). Yes -> choose enemy if possible, No -> no war

It's not very 'human-like', a human looks at the opportunities and then decides if he goes to war, here the AI does the opposite, and consider some difficult options (total war) before easy options (dogpile war)

The system is not so bad because the first check (total war) depends on the units available and it often gives acceptable results, but in my opinion, one of the things to do is to put the dogpile check on the first place, that's what a human would do... I'll probably put dogpile in first to give the wars a more 'human-like' feel. But even with my changes, dogpile wars and limited wars are still more unusual than total wars.

We must also consider the situation differently if a civ is about to win, because after all, which human would dogpile a pushover when his neighbour is about to reach a space victory ? I don't say AI must dogpile-war the civ about to win (god no !) but I say that the AI should try to win and know it's about to loose the game to weigh its decisions a bit differently.

On the randomness, I agree that it's good. It's the war declaration algorithm which can be improved.

The target civ chosen depends only on attitude and distance (both capital distance AND global distance between the 2 civs)

There's a final check though, the 'power check' which I didn't touch, and stops any war that the AI doesn't think is good, depending on the relative power (+ allies / vassals / defensive pacts) and on the leader personality. But if I remember correctly, this number is just a final check, it's not included in the target choice. But the AI will never attack a 'too strong' opponent, which is good. Now it can be a bad evaluation...

One dirty thing I don't like in the code too is that when 'agressive AI' is on, the AI believes its power is worth 4/3 of what it's really worth, so it starts wars it will potentially loose... I will maybe reduce that or even remove it completely, because there are other ways to make an AI agressive than fooling itself on its own capacities. On the other hand, it makes it optimistic ;)

Were you playing in 'agressive AI' mode when your AI attacked a too strong opponent ? Which leader was it ? What was the situation ?

Other than this 'agressive AI optimism', I can factor in the opponent's power before the target choice, but it has to be tested, I don't know if it's a good idea. As it is, it should attack the big nations (especially if it doesn't like them) when there are enough units, and the smaller ones when it can't, which doesn't look so bad.

As a summary, the bad war declarations you see are due to total war preferred first and power not considered as a factor for the choice as long as the attacker has enough power to challenge it. If agressive AI is on, it's also due to the optimism factor (4/3).

If I put dogpile wars first, along with my other changes to increase war declarations, I think the AI would choose better opportunities (vs slightly smaller opponents on average) and wage war much more often. I'd like to slightly decrease the 'agressive AI' optimism (since I don't think it's a good idea) factor (4/3) but I'm afraid to decrease agression too...

You'll tell me with the first version (I'll upload it tomorrow or earlier).

Roland Johansen
Jun 07, 2007, 05:44 PM
I always play with aggressive AI, because I'm used to it and in the normal game the non-aggressive AI is far too passive for my liking. In the latest version of betterAI that is the same for me. I'll use aggressive AI to get some more wars in the world.

I think the AI should pick the weakest, closest AI that it doesn't like because it should be able to steamroll such an opponent and thus become stronger and better prepared for the rest of the game, whether it goes for domination or a spaceship victory.
However, the AI will sometimes pick another AI to attack which isn't that weak or isn't as close. Some randomness is ok in the war declaration algorithm, however if one civilisation is a much easier better target, then it shouldn't even consider the other civilisation as a potential target for a war declaration.

A real world example where nations positions on the world map are shifted.
Lets assume that the US is a neighbour of Russia and North Korea and Iran and it dislikes all three of them equally and the US is aggressive and going for world domination. The US military is stronger than the Russian, the North Korean or the Iranian military, but there is a big difference in strength between the Russian and the North Korean or Iranian military strength. If the US would want to go for global domination, then it would be more useful to first quickly conquer North Korea or Iran to further increase its land size and thus potential power and only then consider other potential victims. The battle against Russia could destroy them both. It's in a real world perspective and in a game perspective just a smarter thing to attack North-Korea or Iran.

Now if the choice were between North Korea and Iran, then the difference in strength would be smaller between the potential victims and I wouldn't mind if the AI decided to attack either of them.

So if after an evaluation, the likelihood to attack North Korea would be 15%, the likelihood to attack Iran would be 10 % and the likelihood to attack Russia would be 1%, then I wouldn't want the AI to accidentally attack Russia. It should either attack North Korea or Iran and change the chance to attack Russia to 0%.

I hope that I have some time to play with your succession of the betterAI mod, but I don't know if I'll have the time and I'm not a fast player. But there are bound to be other players testing it. Especially if you promote it in the sticky 'latest version' thread. There are probably still a lot of civfanatics who have subscribed to that thread.

klokwerk
Jun 07, 2007, 06:04 PM
Yes, I agree it's often smarter to attack weaker nations, though it can also be a bad idea if we're already too late in the game. Maybe the enemy power should be included in the 'target score' before choosing the target, I'll try to test that. Right now if the bigger nation is closer and they hate them more than the small one, they'll probably choose the bigger nation, which is smart only in the late game... We could fix that relatively easily.

I already hear the fans complain though : 'all that your stupid AI does is dogpiling the weaker civs' :lol:

I discovered another bug on war declarations this evening : when the AI decides to make a total war and finds no good target for it, it doesn't look if it can make limited or dogpile wars, because it's an "elseif" in the code... It means that if the total war random roll succeeds, you won't start any dogpile or limited war this turn, even if you didn't find any target for the total war.

It's a minor bug (I don't think it's intentional), but it further slightly reduces the number of wars (especially dogpiles).

Quagga
Jun 07, 2007, 06:22 PM
Hello klokwerk. Thanks for picking up on the development. Other than a recent foray into playing Rhye's mod, I have been playing Better AI since it first appeared on CFC. I have missed its evolution.

After the last release, I started playing with Aggressive AI on. This made the games more entertaining, but I reluctantly concluded that it was a benefit to the human player and stopped using it. Now I see that the 4/3 strength valuation probably has something to do with why it is an advantage for the human.

I read your post mentioning the check/stop when the potential enemy is too strong. From what you're saying, this happens after target selection. I think you are indicating that it should be part of target selection, which seems logical to me.

I would like it if the AI could take into account any ongoing conflicts, so that a stronger civ might be considered a valid war candidate if it is already engaged in another conflict. This could be especially useful when trying to get one AI to fight against another, though this could work out too much in favor of the human. Still, I've often regretted recruiting an AI to join a war, especially when my enemy becomes that AI's vassal, so it might not be advantage human.

I'm going to give your modded mod a try as soon as it's available.

klokwerk
Jun 08, 2007, 07:02 AM
I agree with you. It would be nice if the AI could determine the best war opportunity. It would attack the smaller civs unless it's a good time to attack a bigger enemy, because this enemy is under attack by another powerful civ. I hope that my increase in the dogpile wars is a good step in this direction, but it's a small step.

I just uploaded the first version of the modded BetterAI. It's still in a very early stage, but any feedback on the wars would be nice (with and without 'agressive AI' option).

http://forums.civfanatics.com/uploads/99614/BetterAI_Warlords_Asserts_Unofficial_07-06-04.zip

phungus420
Jun 09, 2007, 12:32 PM
Thanks for continuing the work on this. Hopefully Iustus will get on here and give you the list of failed asserts to finish (he was looking for someone to finish those before he disapeared). The change in war declaration code will be nice to test. Also does this version include the distance declaration fixes?

ilteroi
Jun 10, 2007, 03:45 AM
hi!

did you also change something about the way a war ends? or do you intend to?

for me, it's often frustrating that the ai "refuses to talk" even though it is clearly losing the war, i on the other hand am crippled by rising war weariness ... which is why i would rather have peace than spend another ten turns conquering another remote city.

it even happens when the ai declared the war! i think it should have a way to judge if the war is going badly (difference in the number of units lost, difference in the number of cities lost, or maybe change in the power value from the stats) and if so, try to sue for peace.

i'm happy to see that you picked up development again, thank you!

klokwerk
Jun 10, 2007, 05:10 AM
phungus : yes, the distance fixes are included in this first version. You can see all the changes on the first post of this thread.

ilteroi : no, i didn't change the way a war ends. The AI already checks whether it's winning a war or not, but this 'war score' is maybe wrong. It's possible I'll work on that too, but actually I intend to work more on the AI after the new BtS version and the new SDK are released (and if possible with the help of other fans), because it will probably change much. It's only 43 days left after all.

phungus420
Jun 10, 2007, 07:11 AM
It's possible I'll work on that too, but actually I intend to work more on the AI after the new BtS version and the new SDK are released (and if possible with the help of other fans), because it will probably change much. It's only 43 days left after all.

Yeah, the AI is going to undergo alot of changes, the current better AI mod wol't be compatible, of this we can be sure. It does make sense to wait.

It'd be cool though if we had this up and running again, this was a cool project in CIV, and it was really fun to test it's progress, I hope it jump starts again after BtS comes out.

klokwerk
Jun 11, 2007, 02:42 AM
Yes, I hope there will be people working on the AI after the release. I surely will if they release the SDK.

wiglaff
Jun 11, 2007, 10:41 PM
i played a bit with this tonight. seems a big improvement. great work. I'll play a full game later but this seems like a big step for BetterAI.

TheOnlyDJCat
Jun 12, 2007, 01:31 AM
I played (well started and gave up) three games with the new dll on monarch with random leaders. With old dll I won around 90% of these games (with aggr. AI). Now I got smashed three times in a row (with normal aggression level). I would call the that a BIG improvement - or really bad luck with the leaders and starting positions ;)

scu98rkr
Jun 12, 2007, 03:06 AM
Hi I downloaded this at the weekend and played a bit.

I also used a new map script Earth2 which may have caused the problems. But I found I could not pillage sea based improvements such as fishing net.

It was a bit of an odd map with every one very spread out so I dont want to comment on the aggression I will try it again sometime this week.

klokwerk
Jun 12, 2007, 04:29 AM
Thanks a lot for the feedback ! It's nice to hear that you seem to like it. The AI is clearly much more agressive than ever before if it's working like intended, though it's still far from playing the way I'd like. Your comments make me want to continue to change things.

Please give me more detailed information if you can (and / or send me saved games). Were there early wars ? Were there many wars ? Who attacked ? Was it only the warmongers ? Were they distant sea wars or close land wars ? Did any AI get close to domination victory ? Did anybody betrail or dogpile anybody ? (they should betrail a bit more now and dogpile much more)

What do you think should be improved now about wars, agression or diplomacy ? (the parts I like working on the most...)

Once again, all I did is tweaking and fixing a few things, so let's thank Blake and Iustus for the awesome work they did. The parts I find most impressive are the cultural victory and the deadly crush attacks / dagger strategy.

My main goal after the BtS release (if the BtS AIs can't do that on its own) is to make the AI go for all kind of victories and to play 'like the humans' : they would know who is about to win (and who is second and so on), they would evaluate the danger (powerful close foes...) before deciding anything, and weight strategic goals depending of these numbers. The tactical decisions would be similar but they would depend on these strategic goals.

I read in Blake's posts that his intention before he stopped was to make the 'agressive AI' the switch which makes the AI play like a greedy human. It would certainly be nice to do that, and leave the no 'agressive AI' games for funnier and more different but less efficient AI personalities with complex strategies. War is often a major opportunity though. It probably had to be considered more, even by relatively peaceful AI in a normal game. In a way, if your neighbour is very weak, and you don't like him so much, it can sometimes be stupid to let him live in this game, even is you're a builder.

scu98rkr : could you send me your saved game ? I could try to look at this, if this part of the code is not too hard to understand.

darko82
Jun 14, 2007, 04:29 AM
The AI is not always using the cannons etc. in the proper way. I had a huge pile of units attacking a city. Inside the city it had many units, including cannons. The thing is it didn't make use of the cannon, which could destroy my pile, and defend its city becasue it would greatly weaken all my units. I would have no chances of destroying the city. The AI stayed in the city with all the units, thus couldn't protect itself. My point is the AI should distinguish between cannons and other types of units, also those to defend the cities. I hope and wish it could be improved in the future.

Offtopic - I would like to ask a programmer to implement the possibility to set the soundtrack music for each civilization. For instance, if I choose to play Indian Empire, the music that I choose for that civ will be played. If I choose Russia, then I should have a Russian music in diffrent eras etc. A small thing, but is missing, to my mind ! In Civ 3, we actually had such possibility.

More movies is also nice. For example, when founding a city ( or any event that is triggered in the game )

klokwerk
Jun 14, 2007, 06:32 AM
Yup, the problem is well known, the AI still doesn't use well its collateral units to defend its cities. Maybe I can fix that, it's probably not easy though.

Did you notice anything else ? Were there many wars and if so, when ? (if you played with my 'unofficial' version)

Yakk
Jun 14, 2007, 06:18 PM
Here is the function for collateral unit AI behaviour:

void CvUnitAI::AI_collateralMove()
{
PROFILE_FUNC();

if (AI_leaveAttack(1, 20, 100))
{
return;
}

if (AI_guardCity(false, true, 1))
{
return;
}

if (AI_heal(30, 1))
{
return;
}

if (AI_cityAttack(1, 35))
{
return;
}

if (AI_anyAttack(1, 45, 3))
{
return;
}

if (AI_anyAttack(1, 55, 2))
{
return;
}

if (AI_anyAttack(1, 35, 3))
{
return;
}

if (AI_anyAttack(1, 30, 4))
{
return;
}

if (AI_anyAttack(1, 20, 5))
{
return;
}

if (AI_heal())
{
return;
}

if (!noDefensiveBonus())
{
if (AI_guardCity(false, false))
{
return;
}
}

if (AI_anyAttack(2, 55, 3))
{
return;
}

if (AI_cityAttack(2, 50))
{
return;
}

if (AI_anyAttack(2, 60))
{
return;
}

if (AI_protect(50))
{
return;
}

// if (AI_group(UNITAI_ATTACK_CITY, -1, -1, -1, /*bIgnoreFaster*/ true))
// {
// return;
// }

if (AI_guardCity(false, true, 3))
{
return;
}

if (AI_retreatToCity())
{
return;
}

if (AI_safety())
{
return;
}

getGroup()->pushMission(MISSION_SKIP);
return;
}

The first bit:

if (AI_leaveAttack(1, 20, 100))
{
return;
}

is "try to find something to attack within 1 square".


bool CvUnitAI::AI_leaveAttack(int iRange, int iOddsThreshold, int iStrengthThreshold)


One problem is the last parameter: it only tries to attack if the collateral unit's plot strength is greater than the attacking plot strength.

Pratically, what "could" be done there is an attack and defense simulation: see if attacking leads to better results than defending, esp. if you are in a city.

Tricky.

As an aside, I found something interesting: CvUnit::currEffectiveStr should be changed, or the AI should use something different to approximate odds: one's ability to win a skirmish is closer to combat strength squared, not linear with combat strength. I'd have to be careful making sure that units don't use this value for actual combat, just for AI purposes... This seems to be the case: the only use of currEffectiveStr is in "*AI.cpp" files.

To be clear:

The strength of a unit, in a last-unit-standing fight, is roughly purportional to currHP * currCombat^2 -- a bit less than that, if they don't have time to heal up between fights.

Collateral units work differently. When attacking, their power is dependant on the defender having many units.

If the defender has "enough" units, a collateral unit does damage purportional to:
(TargetAvgCollateralCombatPower / CollateralDamage)^1.5 * number_of_targets
in the collateral phase: the exact value is tricky to work out.

1: Take # of targets with less than "MAX_COLL_DAMAGE" damage.
2: Then, roughly (CollateralStr/DefenderCollateralDefense)^1.5 * 28 damage is done to each one, on average. (that takes into account both an approximation of hit chance (roughly power ratio), and damage per collateral attack (roughly sqrt of power ratio, times 28).

...

If we factor in the effects of repeated combat from smaller stacks fighting larger ones, we can simplify this.

Have the power-estimate be equal to:
HP * Combat^1.5

where the units are "estimated power damage done to an enemy stack in a suicide-charge-until-dead".

Then collateral damage adds:
85 * CollateralStr^1.5
to this value.

So, we get:
HP * CombatStr^1.5 + 85 * (CollateralPercent * CollateralStr)^1.5
as our "better than current estimate" of collateral unit power contribution to an attacking stack. :)

So a full-HP catapult (str 5) with +50% collateral strength contributes:
1118 from direct-attack power.
1746 from collateral-damage power.
for a total of:
2864 attack-power.

A full-HP praetorian (str 9) with combat 1 (+10%) contributes:
3115 from direct-attack power.

A full-HP axeman (str 5) with combat 2 (+20%) contributes:
1470 from direct-attack power.

You will notice that catapults read out as rather powerful under this system -- this reflects the power of catapults in the real game against target stacks.

Ideally one would factor in the enemy stack size: a single enemy unit will result in catapults overestimating their importance, while a large stack will result in catapults underestimating their importance.

Roland Johansen
Jun 14, 2007, 07:08 PM
In the current implementation of BetterAI, I often see multiple defending artillery type units build by the AI. However, they typically attack single attackers that are just pillaging and they stay away from larger stacks. Just the opposite from what you'd want.

If a unit strength valuation system is going to be used like suggested above or something similar, then it is very important that the strength from artillery type units like catapults is estimated correctly and that means taking into account the stack size of the enemy. You'd want the AI to attacks single units and stacks of size 2 with the right counter units, while it values the collateral damage attack higher once the stack is larger.

Yakk
Jun 14, 2007, 07:53 PM
The problem is that 100 strength thing.

It will only attack if it thinks this stack is stronger than the target stack.

This makes catapults attack lone units...

The next test is AI_guardCity. This ends up saying "we should guard this city".

Not so good. :)

So, in english:

bool CvUnitAI::AI_leaveAttack(int iRange, int iOddsThreshold, int iStrengthThreshold)

1> We suicide charge if our stack is at least as strong as the target stack, and we have at least a 25% chance of victory. This is a bad idea -- this is something you do with cavalry and "get rid of pillagers" units.

2> If not, and we are in a city, we stop.

Also a bad idea. This prevents collateral units in cities from doing . .. .. .. ..

Next, we see if we need healing.

bool CvUnitAI::AI_cityAttack(int iRange, int iOddsThreshold, bool bFollow)

Range 1, Odds 35%

bool CvUnitAI::AI_anyAttack(int iRange, int iOddsThreshold, int iMinStack, bool bFollow)

Range 1, Odds 45%, MinStack 3
Range 1, Odds 55%, MinStack 2
Range 1, Odds 35%, MinStack 3
Range 1, Odds 30%, MinStack 4
Range 1, Odds 20%, MinStack 5

Is there a stack within 1 range that we have a 45% chance of winning with at least 3 units in it?

Then we heal up.

Then if we have a defensive bonus, we think about guarding a city.

Anyattack:
Range 1, Odds 55%, MinStack 3
CityAttack:
Range 2, Odds 50%
AnyAttack:
Range 2, Odds 60%

Then see about protecting other units (50% odds).
Then think about guarding a city.
Then think about retreating to a city.
Then think about finding a safe spot.
Then skip one's turn.

...

With the better approximation of "my strength vs yours" above, we can improve the AI_anyAttack. What if it did a check to see if I can defeat the target, rather than the odds of each unit?

Use leave-attack style code, but have it pay attention to the defender's stack size: collateral power can only sum to a certain percentage of the target's total power (based off of the max collateral damage %).

Ie, an AI_attackCrush test.

The AI_attackLeave can also take a "min target stack size" parameter, or take that into account, much like AI_anyAttack.

Hmm...

klokwerk
Jun 15, 2007, 10:53 AM
Good finding ! It's clear that something is wrong in the tactical evaluation before the fight... The good solution would clearly be to 'simulate' the fight (which we probably can't do except if you want to wait one hour between each turn :) ).

I agree with you, the collateral units need to be fixed ! Tell me if you work on that part so that I don't work on it simultaneously. ;)

Yakk
Jun 15, 2007, 01:12 PM
I figure a two-pass algorithm.

The first uses a better power approximation to see if it makes sense to try.

If that passes, and it is close to a toss-up, run a simulation of the fight. If we come out ahead, we chaaarge! with our entire stack.

Ie: we only run this simulation of the fight after we have decided who we want to crush, and just before we execute the unit-charge. The simulation acts as a check on our first-estimate attack guess.

Given that the first-estimate says "we should crush that stack":
If we have a P chance of winning the battle, we will attack with probability P.

So if we have a 90% chance of winning, we will attack 90% of the time:
10% we don't engage.
9% we engage and lose.
81% we engage and win.

If we have a 20% chance of winning, we will only attack 20% of the time.
80% we don't engage.
4% we engage and win
16% we engage and lose.

Admittedly, we probably make this decision every round -- so if the enemy sticks a stack outside of our city and the first-pass thinks we should try to crush it, eventually we will give it a go. :)

But the delay might be enough for some kind of "bring additional defenders to protect against a hostile stack" code to kick in.

The important part of doing the simulation before attacking is that the AI will almost never "crush attack" when it has no hope of winning against the defenders.

"Crush attack" AI code should only activate when the target is a stack and we are a stack of sufficient size.

...

But that's improving the AI. To make catapults stop acting like nimrods, I think you can just delete the first two checks:

if (AI_leaveAttack(1, 20, 100))
{
return;
}

if (AI_guardCity(false, true, 1))
{
return;
}


Then the siege unit will NOT try to attack weak attacking stacks with a 20% chance of winning, and it will NOT compusively guard cities.

Bursk
Jun 15, 2007, 01:16 PM
The thing that is putting me off playing the game is the non-use of collateral units. When you're outside an enemy city and they have a bunch of cats just sit there and do nothing, it makes the game feel too easy.

Roland Johansen
Jun 15, 2007, 02:06 PM
A human player will often use its defending collateral damage units to attack the threatening stack even if it doesn't intend to fully destroy the attacking stack (that turn). I've done this sometimes when the AI approaches my city with a stack that is too large to defend against. I hurt this attack stack with some artillery type units (for instance catapults), then attack with some good offensive units (say macemen, knights). But if the stack is far from being defeated, then I won't throw in my city defenders (say longbowmen). Often this will make it impossible for the attacking stack to take my city that turn and give me some time to get reinforcements to the threatened city. I couldn't destroy the threatening stack, but I've made it less threatening by hurting it with collateral damage attacks (and some strong attackers) and then waiting for reinforcements while defending my city.

The situation is very rare that the best move for a collateral damage type of unit is to stay in the city.
If the attacking stack is weak, then you can hurt them with collateral damage attacks and destroy the stack with attacking units.
If the attacking stack is too strong to defeat, then the collateral damage attack will often weaken them enough to get some reinforcements there before the stack can take the city.
If the attacking stack is far too strong and will capture the city anyway then the best move would be to evacuate the city and let the collateral damage units fight another day. But attacking the stack doesn't have to be a bad move. It will hurt the attackers and they will probably suffer higher losses when attacking the city because their units are weakened.
The only time when collateral damage units shouldn't attack is if the stack consists of very few units.

This is not perfect and a human player would not always use its collateral damage units to attack a threatening stack, but if the AI would attack every stack with collateral damage units, then that is hard to abuse by a human player.

Wodan
Jun 15, 2007, 02:37 PM
Right, Roland. That's what we want the AI to do, but it currently doesn't. Right now, Yakk etc are absolutely correct that the AI currently does some nonsense such as designate cats as garrison units. Whatever it's doing, it's wacked and needs to be fixed.

Wodan

Yakk
Jun 22, 2007, 12:38 PM
So I've been hacking my my copy of the source and making a few changes -- I've made the Power calculation more accurate, the effectiveStrength calculation more accurate, tweaked catapult changes, and patched the "on continent" war chance.

Klock, are your changes checked into sourceforge? I want to see if your changes and mine work together well.

klokwerk
Jun 26, 2007, 09:37 AM
Nice work, Yakk ! Did you test your changes to see if it felt better in the game ?

My own changes are not in Sourceforge right now, my subscription there didn't work (I don't know why right now)... I guess I could send you the source files I modified, I commented my changes.

As I said in the other thread though, right now I don't feel it's a good idea to work on the AI since Blake made a whole new AI for BtS and it's about to be released.

This feeling is being reinforced by the lack of interest for BetterAI nowadays (everybody waits for BtS, it's normal...). I had very few feedback on my own changes, so I really think the best is now to wait and to work together (not only us two, but any fan who knows dev a bit and wants in) on Blake's new AI after the release, maybe even after the patch.

TheOnlyDJCat
Jun 26, 2007, 10:20 AM
Well, your main question was, are there more wars now and are these wars 'reasonable'?
I need many games to give a proper answer;)
Now I have been through five monarch games with normal aggression level and there were many wars. On greater continents they were ok, but when the map fractal map generator leads to islands which can be reached with triremes, sometimes the AI declared on me (I'm absolutely sure without any bribing). Then once in a time a single galley made a crazy attempt to land... But all in all this version plays much better.

Yakk
Jun 26, 2007, 11:03 AM
Nice work, Yakk ! Did you test your changes to see if it felt better in the game ?

My own changes are not in Sourceforge right now, my subscription there didn't work (I don't know why right now)... I guess I could send you the source files I modified, I commented my changes.

Or just host them on CivFanatics. :)

As I said in the other thread though, right now I don't feel it's a good idea to work on the AI since Blake made a whole new AI for BtS and it's about to be released.

Bah -- Blake did the same thing with Warlords. :) While it was a leap in quality, it wasn't a complete reworking of how things went.

I figure that getting familiar with the code and making improvements now is a great time -- so they will be fresh in our minds when BTS-AI comes out, and we can fold them into BTS-AI quicker.

This feeling is being reinforced by the lack of interest for BetterAI nowadays (everybody waits for BtS, it's normal...). I had very few feedback on my own changes, so I really think the best is now to wait and to work together (not only us two, but any fan who knows dev a bit and wants in) on Blake's new AI after the release, maybe even after the patch.

*grin* -- I'm gonna be a curmudgeon, and release a custom-compiled and improved BetterAI for my IsoMod if I get it done pre-BTS.

I didn't notice -- happen to see what license BetterAI is released under?

... checking...

RPL:
c. Create Extensions to the Licensed Software consistent with the rights
granted by this License, provided that You make the Source Code to
any Extensions You Deploy available to all third parties under the
terms of this License, document Your Modifications clearly, and title
all Extensions distinctly from the Licensed Software.

So I gotta bundle up source code if I distribute the binary.

...

Actually, I gotta share my source code to anyone even if I don't distribute the binary. Neat. :)

I suspect we need rights to be able to check in changes to BetterAI on source forge.

Ralgar
Jun 30, 2007, 04:30 AM
So I've been hacking my my copy of the source and making a few changes -- I've made the Power calculation more accurate, the effectiveStrength calculation more accurate, tweaked catapult changes, and patched the "on continent" war chance.

I totally want a compiled version of this! Please!

If we have a 20% chance of winning, we will only attack 20% of the time.
80% we don't engage.
4% we engage and win
16% we engage and lose.

Im not sure how this will play out. I would cap it at around 40-50% or so. Because as a human I will brutally exploit this: Get a bigger stack near an enemy stack. Wait turn. Wait. Wait. Wait. Wait. Enemy stack attacks and commits suicide.

Yakk
Jul 03, 2007, 08:15 PM
The compiled version is in:
http://forums.civfanatics.com/showthread.php?t=229211
The DLL currently contains purely BetterAI changes -- the rest of the changes are in XML.

Source will follow -- sorry, was away for the weekend in the backwoods.

Amask
Jul 03, 2007, 11:23 PM
Yakk, I just discovered your mod from this thread, can't wait to try it out
sounds awesome
what a useless post by me this was
that's just how excited I am

Revolutionary
Jul 07, 2007, 08:27 AM
I've played your mod and its great! :D

Man can the AI conquer now, although it still does foolish suicidal attacks but its much better. There are a lot more wars between AIs now, but I still see a bias against the human player, AIs will declare war on me even though I'm much more powerful, it has other weaker neighbors, and/or are far away from me.

Also the AIs makes a fatal mistake when they're warring, they defortify their cities leaving only 1 unit to defend to send the other units in a stack to attack, while this can be a smart tactic it leaves them open to attack from a neighbor or naval invasion.

So when an AI declares war on you all you have to do is have a stack to counter theirs and flank them by building 1-2 galleys and a trireme or a gallon or 2 and invade them on the other side of their empire and capture and burn their cities to the ground.

This needs to be fixed.

Yakk
Jul 11, 2007, 12:19 PM
Units tied up with defending don't go to war. Note that in the mod linked, I reduce the AI's advantage against the player significantly: so try boosting your difficulty level, and see if you can deal with the enemy forces.

As an aside, the changes I made to defensive alloitment should be limited to:
A> Border cities.
B> Cities under threat. The number of extra units needed was changed to (threatening power/average defender power), instead of just the raw number of threatening units.

You could, under the standard BetterAI build, threaten a city with a stack of warriors, and cause most defenders to freeze up and not sally! With a few warrior stacks (or other trash troops) backed up with a strong defense unit or two, you could lock down the entire enemy army, even if it overpowered you significantly. :)

...

I should see about increasing the bias of the AI towards dog-piles and crushing weak neighbours, like klok did -- I think mine still has the standard "attack people who we hate much stronger than people we like" code.

Ideally, I'd like various AI nations to conquor and grow much like a player would: finding a nearby weak nation, expanding, and repeating -- and form de-facto defensive/offensive alliances by having a tendency to dogpile with friendly civilizations.

It should be standard for multiple civilizations to go to war with you at once, and going to war should be contingent on having allies sufficient to crush your target, and your target being worth conquoring.

Ie, when a civilization wants to go to war, it should look at your relative power points. It should then seek to gather an alliance at least 1.5 to 2.0 times as powerful as the target, and then go to war with the target.

To invite humans to such a dogpile, it would conditionally ask other AI "if I get the human to join the war, will you join in too?", produce a dogpile large enough to defeat the target with the human, ask the human to go to war -- if the human says yes, activate the conditional agreements and go to war. Otherwise, keep searching for another set of AIs who would agree to go to war with the target.

I'd call this "effective conquest". ;)

When attacked, it should try the same kind of dogpile tricks against the attacking alliance.

Another bug I found is that the AI will keep on MASSING even when it has an economic collapse going on. A massing strategy should keep track of how many offensive units it can afford, as well as how many units it has.

Should the AI prefer teaming up with the most powerful civilization, or prefer taking it down?

Quornix
Jul 13, 2007, 06:19 PM
I took your (Yakk's) game core out for a spin, with aggressive AI on the Earth map. Played as the Aztecs on Monarch, and found myself surprised to be the first to Optics, and the tech leader. Then I got a copy of someone's world map and found out why I was up on tech. Cyrus had wiped out Egypt, Arabia, and India. Frederick had wiped out the Russians, the Romans, and the French. Qin had taken out the Mongol hordes. With everyone willing to take more units out of their cities for combat, things had gone nuts over there.

I've been playing with the standard Better AI for a while, and while there have been plenty of wars, this has been the most successful warring I've seen. I'm pretty sure that the AI is now much more effective at leveraging a tech advantage.

Played a few other games, and I found out quickly that I needed to drop down a level when I had more than one neighbor. Big stacks have been decimated as I brought them to cities, and I've wished more than once that I had Chichen Itza:eek: . Having the AI commit more of their army to active rather than passive war is dangerous.

It's tons of fun.

Cryptoanarchist
Jul 15, 2007, 01:30 AM
Hmm, what about other human behaviors? It seems alot of players specialise a GPP farm a Science city,commerce city production cities or play to more specific strategies (from the start - I'm going for a cultural win).

Disclaimer: Havn't read up on much about better AI just thought I'd throw in 2c for people to do what they will with it.

oedali
Jul 17, 2007, 04:39 PM
Hey, I just tried out your mod and I love it so far! I usually play Monarch games w/ aggressive AI setting but I tried this one w/o aggressive AI. As soon as I built my second city Ragnar declared war on me with his units at the border and razed it, and then moved more units to take my capital. He did not succeed in the end but I fell down a great deal in score and still had only one city while other civs had 3-4. So I'm gonna start a new game now more aware of the Better AI's capabilities. I can definitely say that it's playing a lot more human-like & warlike!

DragonHawk
Jul 18, 2007, 12:24 PM
Just want to echo everyone's opinion and thank klokwerk for the great adjustments to BetterAI. I for once appreciate how the AI are now more opportunistic than ever :goodjob:

darko82
Jul 21, 2007, 04:46 AM
Since the BTS is about to be realised, it would be good to start implement the mod correctly.

Wodan
Jul 21, 2007, 06:03 AM
The mod isn't necessary with the new AI in BtS. Or, more properly, the mod should really be "reset" to ground zero and then new improvements started.

Wodan

PieceOfMind
Jul 21, 2007, 07:17 AM
The mod isn't necessary with the new AI in BtS. Or, more properly, the mod should really be "reset" to ground zero and then new improvements started.

Wodan

It's gonna take some skill to think up a better AI this time. This time we have to outdo Blake! The AI in BtS is impressive to say the least. Ignoring the occasional bug of course.

However, I wouldn't put it past klokwerk and Yakk. If they do go ahead with improvements it would be fantastic. But I wonder how many and how big the patches will be for BtS. If Blake makes AI improvements in patches it may be wise to avoid having two people making improvements in parallel.

Not meaning to sound like a total fanboy, but Blake's work has done wonders for the game.

Wodan
Jul 21, 2007, 03:14 PM
Not meaning to sound like a total fanboy, but Blake's work has done wonders for the game.
I totally and 100% agree. I uninstalled BetterAI2 and BetterHandicaps when I put in the patch the other day, and frankly was amazed at the improvements.

Wodan

klokwerk
Jul 23, 2007, 07:16 AM
Thanks for the feedback about my changes and Yakk's changes.

Yes, the mod will be started from 'ground zero', as you said. I hope we'll be able to improve things, but Blake did a awesome work in BtS and there aren't so many things to improve right now.

I still see a few glitches in BtS though :
- planes are still often destroyed on the ground when you capture a city (I destroyed 8 HRE zeppelins by taking 2 cities), they should flee before the city is captured
- the odds calculation before massive AI stack attacks doesn't seem always right. The AI sometimes suicides dozens of units before acknowledging he won't take your city, I don't know why I didn't read this part of the code.

There are probably more but that's what I see now, I only played half a game.

Overall, congrats to Blake, the BtS AI is really impressive. (And I'm pretty sure he'll make it even better in a future patch. )

Roland Johansen
Jul 23, 2007, 09:26 AM
Klokwerk and Yakk, Blake and Iustus, I hope you all will think a bit more about AI improvements (if you want to). Any improvements possible are always welcome. And I guess, that there will always be small weaknesses of the AI left to improve upon. Good luck!

Wodan
Jul 23, 2007, 04:53 PM
Klokwerk, here are a couple of things I've noticed so far:
-- building "Research", and also with emphasize research button (and no other buttons), the city governor assigns an Engineer? Something's up there.
-- this is a personal preference: the last versions of BetterAI had algorithms that when you forced a specialist type, and it needed to add another specialist, it would only choose to add more specialists of that same type (as possible).

Wodan

Yakk
Jul 23, 2007, 05:49 PM
-- building "Research", and also with emphasize research button (and no other buttons), the city governor assigns an Engineer? Something's up there.

That could easily result in more research than a scientist. Did you check by changing specialists manually, and seeing if the amount of research went up when you changed to a scientist?

Wodan
Jul 26, 2007, 07:11 AM
That could easily result in more research than a scientist. Did you check by changing specialists manually, and seeing if the amount of research went up when you changed to a scientist?
Yes.

Wodan