Mxt-dot / BetterAI

Europa Universalis 4 Mod, improving the AI
7 stars 3 forks source link

AI Estate optimization #41

Open ryandaniels55 opened 3 years ago

ryandaniels55 commented 3 years ago

Hello its me dabiels from the steam page, this comment was too long to leave there due to the character limit :)

Okay so after many hours of learning how EU4 coding for the AI works and testing, I think I figured out the issue with the low crownland! In the common>estate_crown_land_interactions_00 part of the mod, on line 50, you set the minimum crownland for an AI with a subject to want to sell crownland at 35%, while an AI without a subject will only sell if it has 40% or more, staying above that critical 30% mark to avoid debuffs. I think this is an error in the code, and what you meant to put was 45% for an AI with subjects, as the code comment indicates you wanted the threshold to be higher for them as the autonomy penalty hits them harder.

HOWEVER, after looking at the actual debuffs, I think the AI is actually doing better so long as they stay above 20% crownland, 10% tax penalty in the early game isnt much, it works out to around 3% of a major nations income once they get their trade nodes secure and have subjects, and the sheer number of ducats received in the early ages makes up for it. Also, since they are selling at 35% it reduces them to 25%, meaning they are only spending half the time in the debuff zone as long as they are seizing land in between (haven't figured out how to get them to do that).

Additionally, I think increasing the base factor for the decision so drastically might make it so the AI is effectively selling titles on cooldown whenever it can, and the same might go for seizing land but I'm not sure. From what I observed, most nations immediately sold titles even if they weren't at war, and after I reverted the "seize title" modifiers to vanilla, they bought back titles on cooldown and spawned rebels, getting stuck in a loop. When I was looking on the wiki theres a modifier you can add, ai_importance, that determines how high the AI prioritizes taking a decision. Maybe this could help with nations never seizing land once they go below a certain value, and hopefully encourage them to stop selling titles during the age of absolutism unless necessary(like in a war).

Ultimately, I think the best solution would be to allow the AI to sell titles if they have 35% crownland, and further allow them to sell when they have 30% IF AND ONLY IF they are going through one of the situations in the section directly below line 50 (deficit, loans, disaster, etc.).

P.S. I also found another issue, for theocracies you increased the minimum loyalty to seize land by 10%, however in a theocracy the base loyalty for the clergy is increased by 5%, but the disloyalty threshold at 30% is unaffected.

ryandaniels55 commented 3 years ago

I found what I think is a solution to avoid the ai suffering penalties from not completing an agenda. For every agenda, there is an invalid_trigger section, which removes the agenda and all associated flags without penalty or reward, which is exactly what we want for the ai! I figure if we could add in an "or statement" to each of those sections saying basically if 7299 days have passed (one day less than the time to fail an agenda) and ai = true, then trigger the invalid effect, on_invalid, which like i said removes the agenda without penalty, allowing the ai to call a new diet and start a new mission. Here is a useful reddit link to someone trying to mod events to be ai only: https://www.reddit.com/r/eu4/comments/6as4pt/ai_event_modding/

ryandaniels55 commented 3 years ago

00_generic_agendas_V3.txt

001_brahmin_agendas_V3.txt

These prevent the AI from being penalized when not completing an agenda, for which they currently have no code written to encourage.

ryandaniels55 commented 3 years ago

02_noble_agendas.txt 003_vaisya_agendas.txt 03_burgher_agendas.txt 04_cossack_agendas.txt 05_nomadic_tribe_agendas.txt 06_dhimmi_agendas.txt 07_jain_agendas.txt XX_example.txt 00_generic_agendas.txt 001_brahmin_agendas.txt 01_church_agendas.txt 0002_rajput_agendas.txt 002_maratha_agendas.txt

Many hours later, here are all the estate agendas, fixed so that a check is put in place in the failing_effect section to bypass any negative estate loyalty or province unrest for an AI controlled nation, but still remove flags and claims given by the agenda. In my playtests, many AI would actually complete estate missions just by chance (hoorah!), and the best was when they received a conquer claim agenda, as the AI is already coded for that to complete the agenda requirements.

I believe the next step is to figure out what agendas the AI already has applicable behavior modifiers for, such as if they receive an "increase manpower" agenda, they will avoid declaring war until the agenda is completed, just as an example. Doesn't have to be done as this feature is functional as is, but it'd be cool to see the AI actually go after the agendas, just like a player!

Edit: Obviously the ai_will_do factor under "call diet" in "interactions_00" has to be changed, I put a 100 in there as a placeholder so they always call diet when an agenda is completed or expires, but it might be a good idea to include some checks so they don't call a diet when estate influence is over 90% to avoid a disaster.

ryandaniels55 commented 3 years ago

Want to add a clause where the AI can seize land if only one estate loyalty is at 40, since in my playtests every single AI had trouble with the Jains estate loyalty, but only if they are not at war and have army_size_percentage = 0.6 or army_size = 50, whichever is lower. I would like to know what the actual factor for the number of rebels spawned is, but the only thing in interactions_00 is "spawn_rebels_seize_land_switch = yes" which doesn't help me figure out the math behind how large the rebel army will be. I figure 60% of any nations forcelimit should be enough though, so we can keep that as a placeholder.

Mxt-dot commented 3 years ago

Good progress! As mentioned in the private chat, I'll have a look into it after the stream today or maybe check it out in the stream at the end. My solution to the estate loyalty issue seems to be the smaller change (= more robust) in terms of potentially having to change code when pdx updates, but overall yours seems to add another layer of AI functionality (= actually completing agendas) so yours is obviously preferrable unless it would be insanely difficult to maintain.

Want to add a clause where the AI can seize land if only one estate loyalty is at 40

I guess that's also easily doable in the defines.lua, there should be a tweakable value for that.

but only if they are not at war and have army_size_percentage = 0.6 or army_size = 50

and that would be the other condition in the actual seize_land interaction (separately for if ai = yes)

I would like to know what the actual factor for the number of rebels spawned is

I think seize_land rebels are super small. I dont think I've ever seen a stack being more than 5k. It does depend on your FL, but the actual scaling per province seems to be super low so in most cases it's only like 1-2k rebels. Doable for any nation 🗡️ I dont think we need to care that much about the rebel size if the country has more than 5k troops (and for small nations more than 3k). The most important bit though is for OPMs, they should never ever spawn rebels when seizing land since we cant guarantee that troop maintenance is on high, and perhaps they get stackwiped when not paying.

ryandaniels55 commented 3 years ago

00_interactions.txt

Added the clause so AI can use seize land if:

            is_at_war = no
            NOT = {
            num_of_rebel_armies = 1
            num_of_rebel_controlled_provinces = 1
            }
            OR = {
            army_size = 15
            army_size_percentage = 0.6
            }
            num_of_cities = 5
                estate_loyalty = {
                estate = all
                loyalty = 40

Additionally, added a rough scale to crownland desire overtime; for the first 30 years the AI desires 25% (early game cash infusion is more desirable than 10% extra tax), after 1475 they desire 30%, after 1600 they desire 45%, and when absolutism starts in 1610 they desire 60%.

Edit: not working properly, need to find a way to make the ai consider this acceptable despite loyalty being below 50%

ryandaniels55 commented 3 years ago

00_interactions.txt

Fixed, just needed to change the conditions around! AI will also now check if they can call diet BEFORE using seize land, avoiding the rebel spawn in the first place.