• We have updated our Community Code of Conduct. Please read through the new rules for the forum that are an integral part of Paradox Interactive’s User Agreement.

EU4 - Development Diary - 16th of October 2018

Welcome all to today’s dev diary, my name is Michael, and I've been on the EU4 team as a programmer for a bit more than a year now.

Many of you have noted that the AI has been showing some undesirable behavior after recent updates, especially when it comes to defending their homeland and when choosing which forts to siege. So for the upcoming "Spain" update we spent some time on improving those aspects of the army AI. In this dev diary I will be explaining some of the changes we made. In addition to the improved hard-coded AI behavior, with the upcoming update modders will be able to influence some parts of the army AI with scripts. I'll end this dev diary with a short explanation/tutorial of this.


First I'll go over the changes and improvements made so far. Since we are still working on this update all of this is of course subject to change.

- The selection of armies to go on invasions (overseas) does now consider potential threats to the homeland and will be smaller if necessary. There still is the option that the AI will decide to invade with the full force if it thinks it cannot win otherwise. The calculation of the required strength has been overhauled and should be better in determining how many troops are actually needed.

- Region assignment of armies used to be done about once per year. There was a random factor involved so in some cases it could take even longer. In addition to the random reassignments it is now recalculated whenever the threat to the homeland changes considerably, i.e. there are sieges, large amounts of hostile troops moving towards homeland, etc..

- If there is a threat to the homeland these regions will have a much higher priority when assigning AI agents to regions. Even if not threatened, the homeland has higher priority in region assignment, if enough armies are available. On the province level (Within the assigned region) provinces close to the own country are now preferred when looking at what to siege/where to defend and what war goals to go for first.

- Armies running away from a superior force will still try to find a safe place, but provinces that require using military access or long walks will have lower priority, and if reasonably safe, the homeland receives much higher priority. AI armies are more likely to stand their ground, especially if they are clearly inferior and there is no real chance of winning the ongoing war anymore. Previously the player was often forced to chase them around the world thanks to easily available military access.


Scriptable army AI
A central part of the EU4 army AI is the evaluation of provinces, which determines where and when armies move to. There are already many defines you can change to tweak the outcome of this evaluation, but starting with the coming update you will also be able to change or even overwrite the evaluation score by using any trigger you like.
You will find new script files that have some basic functionality in commmon/ai_army, but you can add as many of your own as you like.

Let's look at a simple example:

Code:
province = {
    war = {
        active = {
            is_at_war = yes
        }
        eval_multiply = {
            modifier = {
                factor = 0.1
                is_in_capital_area = yes
            }
        }
    }
}

For now the top-level scope is always "province". We might add different options later. The second level is just a name you can choose freely and it serves to separate different modifications you want to apply. Just like event probabilities the starting value for these is 1. The interesting scopes are "active", "eval_multiply" and (not shown) "eval_add" and "eval_overwrite". With those you can control the modifications that will be applied to the evaluated province score. In this case it will be applied whenever the country is at war.
It is important to note that the evaluation of provinces happens very frequently for all armies in the game, so this "active" trigger will not be checked every time a province is evaluated (Which might happen hundreds of times per daily tick).
It will be evaluated when any of the following happens:
- Whenever you gain or lose ownership or control of a province
- Whenever a war starts or ends
There are three ways to modify the score, you can multiply it, add to it or overwrite it. The factor can be negative and < 1.0 as well so the inverse operations are also possible.
Note that armies prefer the province with the lowest score.

While you can influence armies a lot with this, there are still some hard-coded things that will always take precedence and obviously armies will still always adhere to the rules of the game (e.g. shattered retreat). It is a nice option to have however and I'm looking forward to seeing if and how it will be used in mods. Of course it is also possible to break AI behavior with this, so I'd recommend being careful, especially with "eval_overwrite".

To debug your script you can use the AI mapmode. In this mapmode you can see the evaluation scores for every province if you select an army. You can enable this mapmode by running this command in the console:

Code:
mapmode aieval

Capture.PNG


That’s all for today! Next week we will be looking at new features of the upcoming immersion pack. In the meantime, I’ll be watching this thread, so feel free to ask any questions you might have.
 
Last edited:
Hmm, so how will the moddability of the AI affect rebels and such? There should be differences on how the different types of rebels "act". I'd expect separatist and pretender rebels to be the most organised and dangerous compared to say peasant tax dodgers.
 
Most likely related to this -
ACCEPTABLE_BALANCE_DEFAULT = 1.7, --AI wants this advantage to enter battles typically. (There are some exceptions, e.g. offensives.)

Though if I understand the changes correct, it should be easy to circumvent such behaviour (Ie could make AI take into account things such as defensive bonus of fort/province, military boni etc, so its not just limited to a flat 1.7 times larger force decider)

That would probably explain why it suddenly wants to fight when the siege is over. I'd say it cosniders the 10% morale boost for the attacker, but perhaps doesn't value potential roll penalties for the enemy as much (or at all), so it always views being the defender as a disadvantage.
 
Hmm, so how will the moddability of the AI affect rebels and such? There should be differences on how the different types of rebels "act". I'd expect separatist and pretender rebels to be the most organised and dangerous compared to say peasant tax dodgers.

Always been scriptable in the rebels own definition
 
@Groogy Yes I'm aware of those too. I guess the question I tried to ask is; How will the moddable AI influence the rebel types beyond or in relation to the old definitions. If that even makes sense anyway.

Also Pirates should be their own rebel type. Just for the fun of it.
 
Welcome all to today’s dev diary, my name is Michael, and I've been on the EU4 team as a programmer for a bit more than a year now.

Many of you have noted that the AI has been showing some undesirable behavior after recent updates, especially when it comes to defending their homeland and when choosing which forts to siege. So for the upcoming "Spain" update we spent some time on improving those aspects of the army AI. In this dev diary I will be explaining some of the changes we made. In addition to the improved hard-coded AI behavior, with the upcoming update modders will be able to influence some parts of the army AI with scripts. I'll end this dev diary with a short explanation/tutorial of this.


First I'll go over the changes and improvements made so far. Since we are still working on this update all of this is of course subject to change.

- The selection of armies to go on invasions (overseas) does now consider potential threats to the homeland and will be smaller if necessary. There still is the option that the AI will decide to invade with the full force if it thinks it cannot win otherwise. The calculation of the required strength has been overhauled and should be better in determining how many troops are actually needed.

- Region assignment of armies used to be done about once per year. There was a random factor involved so in some cases it could take even longer. In addition to the random reassignments it is now recalculated whenever the threat to the homeland changes considerably, i.e. there are sieges, large amounts of hostile troops moving towards homeland, etc..

- If there is a threat to the homeland these regions will have a much higher priority when assigning AI agents to regions. Even if not threatened, the homeland has higher priority in region assignment, if enough armies are available. On the province level (Within the assigned region) provinces close to the own country are now preferred when looking at what to siege/where to defend and what war goals to go for first.

- Armies running away from a superior force will still try to find a safe place, but provinces that require using military access or long walks will have lower priority, and if reasonably safe, the homeland receives much higher priority. AI armies are more likely to stand their ground, especially if they are clearly inferior and there is no real chance of winning the ongoing war anymore. Previously the player was often forced to chase them around the world thanks to easily available military access.


Scriptable army AI
A central part of the EU4 army AI is the evaluation of provinces, which determines where and when armies move to. There are already many defines you can change to tweak the outcome of this evaluation, but starting with the coming update you will also be able to change or even overwrite the evaluation score by using any trigger you like.
You will find new script files that have some basic functionality in commmon/ai_army, but you can add as many of your own as you like.

Let's look at a simple example:

Code:
province = {
    war = {
        active = {
            is_at_war = yes
        }
        eval_multiply = {
            modifier = {
                factor = 0.1
                is_in_capital_area = yes
            }
        }
    }
}

For now the top-level scope is always "province". We might add different options later. The second level is just a name you can choose freely and it serves to separate different modifications you want to apply. Just like event probabilities the starting value for these is 1. The interesting scopes are "active", "eval_multiply" and (not shown) "eval_add" and "eval_overwrite". With those you can control the modifications that will be applied to the evaluated province score. In this case it will be applied whenever the country is at war.
It is important to note that the evaluation of provinces happens very frequently for all armies in the game, so this "active" trigger will not be checked every time a province is evaluated (Which might happen hundreds of times per daily tick).
It will be evaluated when any of the following happens:
- Whenever you gain or lose ownership or control of a province
- Whenever a war starts or ends
There are three ways to modify the score, you can multiply it, add to it or overwrite it. The factor can be negative and < 1.0 as well so the inverse operations are also possible.
Note that armies prefer the province with the lowest score.

While you can influence armies a lot with this, there are still some hard-coded things that will always take precedence and obviously armies will still always adhere to the rules of the game (e.g. shattered retreat). It is a nice option to have however and I'm looking forward to seeing if and how it will be used in mods. Of course it is also possible to break AI behavior with this, so I'd recommend being careful, especially with "eval_overwrite".

To debug your script you can use the AI mapmode. In this mapmode you can see the evaluation scores for every province if you select an army. You can enable this mapmode by running this command in the console:

Code:
mapmode aieval

View attachment 411237

That’s all for today! Next week we will be looking at new features of the upcoming immersion pack. In the meantime, I’ll be watching this thread, so feel free to ask any questions you might have.
Best change ever. Makes me wanna start playing again.
 
- Armies running away from a superior force will still try to find a safe place, but provinces that require using military access or long walks will have lower priority, and if reasonably safe, the homeland receives much higher priority. AI armies are more likely to stand their ground, especially if they are clearly inferior and there is no real chance of winning the ongoing war anymore. Previously the player was often forced to chase them around the world thanks to easily available military access.

Praise be!
 
@mikesc What is the one thing that you would like to improve in the AI in the future? (doesn't matter if its possible at the moment)
 
This is a nice start, hopefully it will fix most serious problems.

There is lots of space for improvement:
- AI handling of rebels in war - AI tend(ed) to ignore rebels
- In general AI handling rebels that can't fight from popping up was bad... players would invest everything to stop this
- AI not removing War Exhaustion - whats the deal with ignoring this?!? Make them spend diplo to reduce WE, they literally die to WE and unrest when sieged
- AI sieging forts - teach AI to bring/build artillery please; teach them artillery breach (russian AI is btw using million of infantry and nothing else)
- AI in naval battles - retreat from naval battles when losing badly - and fix Morale bugs in naval battles..
- AI spamming force limit building and useless docks
- Maybe make allies AI cooperate a bit if possible... 2-3 small AIs could combine forces in 1 stack, that would be AI breakthrough..
- AI blindly accepting to enter hopeless wars and die. Smarter diplo AI please ?
- AI turning on player just because it got military leader (personality)... etc..

Those are just some of the things that first pop out to improve.
 
Last edited:
Can we have a summary of all the province changes after the last diary?
Patience is a virtue. Let us pray.

I agree with @navaluiki , this isn't neondt's dev diary, so I think we should wait until next week when Spain is in the focus again.

Unless @neondt doesn't mind it?
This.

On point, good overall changes, praying I won't have to chase ridiculously small stacks across Germany and Italy for 2 years as Austria anymore. Fingers crossed for next week.
 
Mixed bag for me, thank you for working on the AI, it is a thankless task (people will always complain about it) and improvements are always welcome. But the way it's described, I'm getting flashbacks to HOI4 where the German AI had a propensity to pull their full forces back from any blitzkrieg if a piddling division was "threatening" their territory.

In a world where communications are poor and supply lines that would have existed in EU4 time period can be cut off by the player's actions in enemy territory, I don't think the AI should be that responsive to attacks on their home forts.

lolada's suggestions might have more of an impact in keeping with the nature of the game...
 
I mean it's pretty funny to watch the AI's using there whole army to siege down the enemy vut totally ignoring their homeland, but if this really fixes this and the endless army runs through other countries then I appreciate it very much. Thank you :D
 
Perhaps this is a bit out of scope, but are there any changes coming to the endless waves of European coloniser dows against NW nations (even as big a a reformed Inca, for example)

That isn't a bug; the AI thinks you're weak [Especially with inferior tech; and Smallpox event] and wants your gold.

Balance of power gets even worse if they see you're at war with a major already. They'll dogpile.

If you look at most colonizer AI's; they'll mark all gold provinces as Vital Interest.
 
Great changes to the AI!
One thing I would like to see addressed as well is the fact that AI can be clairvoyant as if they permanently infiltrated administration. Sometimes the AI leaves a fort it almost sieged down, because it knows a superior force of mine is heading to that province. Fog of war means very little to the AI.
Worst is when I have a smaller force than the enemy. Outmaneuvering them becomes almost impossible as they always seem to know where I am hiding out.
 
Great changes to the AI!
One thing I would like to see addressed as well is the fact that AI can be clairvoyant as if they permanently infiltrated administration. Sometimes the AI leaves a fort it almost sieged down, because it knows a superior force of mine is heading to that province. Fog of war means very little to the AI.
Worst is when I have a smaller force than the enemy. Outmaneuvering them becomes almost impossible as they always seem to know where I am hiding out.

Eh no, the AI needs to be made stronger not weaker.
 
The changes to army AI moddability is absolutely awesome to see. Would it be possible to add a variable to the army troop count, that is basically like a 'ghost units' var. Basically if a modder does 'add_army_eval = { infantry = 10 }', then the AI would think it has 10 extra infantry units when evaluating the troop count against other nations? This way, an AI can account for any modded/extra units it doesn't have on the map, but does/will have on hand?

This would revolutionise the way we interface with the built-in diplomacy AI, allowing us to build custom militaries or scripted units, and have them account for when an AI is deciding to declare war or ally other nations due to threats. This would ideally be an 'add, set, check' set of commands, for a single variable per unit type, that is added onto any troop count total used in diplomacy.
 
Last edited:
Great to hear about AI improvements, specially the ones about "protecting homeland" that may be hinting towards a colonisation overhaul, but please:

Please, add a Mesta modification to wool production in the region of Castille. Please, is a great opportunity to rework Personal Unions and make them work in a progressive integration way, instead of the demi-random way of inheritation or vassal-like integration.

Please, make the tercios a unique unit type like cossacks, banners or streltsis. Please rework the council system and add some kind of "Valido", prime minister, or equivalent figure that boosts MP production. Those figures existed through all europe. Please rework colonisation so it increases development of colonies and reduce development of metropoles.

Thanks for Iberia at last.