Results 1 to 20 of 37

Thread: AI Expansion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default AI Expansion

    AI Expansion

    Spoiler Alert, click show to read: 

    nb1 This is a work in progress.

    nb2 Originally i followed this process for my own modding, testing to 50 turns - which took ages. When i decided to repeat the process and write it up as a tute i couldn't handle doing dozens of 50 turn tests again so i started with 30 then finally 12. The first set of edits show an obvious improvement in AI performance within 12 turns but some of the later map edits e.g the Caffa land-bridge only show an improvement around 40-50 turns. This may be a little confusing later but the most important changes show within 12 turns.


    Introduction

    This tutorial will consist of a series of posts exploring techniques for improving the AI's campaign map performance with the least amount of modding effort. It's mainly aimed at people who've already modded a bit but maybe haven't looked at AI behaviour but i'll also detail the modding steps involved in some of the changes i make as a general modding tutorial. I'll use the vanilla campaign as the test case.

    The general pattern of the posts will be:
    1. Description
    2. Modding Details
    3. Effect

    The effect will be based on running automated tests after modding and seeing if the AI takes more or less regions than they did compared with the vanilla files.

    The list of areas covered will be:

    Intro
    Setting up

    Part 1

    Universal changes that show up in short tests i.e 12 turns
    --Movement points
    --AI movement trait
    --Brigands, Pirates and Revolts (part 1)
    --Brigands, Pirates and Revolts (part 2)
    --Pre-building
    --Rebel garrisons

    Changes relating to specific factions to help their individual expansion or edits to particular regions the AI has a problem with. These will be:

    --Scotland
    --Russia
    --Denmark
    --Portugal
    --Caffa region


    Part 2

    --New Baseline
    --England and Venice
    --Mystery Improvement
    --Modding for the AI (Rebels)
    --Modding for the AI (Missions, Money Shocks and Upkeep)
    --Unit Stats
    --Campaign AI


    +++

    Next post: Setting up
    Last edited by tupodawg999; February 03, 2010 at 02:44 PM.

  2. #2

    Default Re: AI Expansion

    Post 2: Setting Up

    The best way to get a feel for how the AI behaves is to start the game (or mod) as a faction that is out of the way and doesn't get bothered in the early game e.g egypt, then pick a faction to watch and click end turn while watching what that faction does. You'll soon see the problems (it helps to have a spy nearby to look into woods) but figuring out the solutions to those problems is mostly guesswork and trial and error.

    However, many of the AI's problems relate to random events such as army revolts which do not occur consistently for each faction at the same point in each game. To test to see if edits related to that sort of thing has a beneficial effect on the AI's expansion then i think you need to run sets of tests, average out the results and then compare them to a vanilla baseline.

    To do this we need to set up a mod in a way where we can automate the AI for a specified number of turns.

    Modding Details

    The first step is to get our testing mod set up and ready.

    Step 1

    1) Create a "mods" folder inside your main MTW2 folder.
    2) Create an "ExpansionTest" folder under the mods folder.
    3) Create a "data" folder under the ExpansionTest folder.
    4) Copy the "world" folder from your main MTW2 "data" folder and copy it into expansiontest/data.

    Step 2

    1) Make a copy of your medieval2.preferences.cfg file in your main MTW2 folder and rename it to "expansiontest.cfg".
    2) At the top of this new file add

    Code:
     
    [features]
    mod = mods/ExpansionTest
    Lower down find the section that should look something like:

    Code:
     
    [hotseat]
    autoresolve_battles = 0
    disable_console = 0
    disable_papal_elections = 0
    save_prefs = 0
    update_ai_camera = 0
    validate_diplomacy = 1
    and replace it with

    Code:
     
    [multiplayer]
    playable = true
    [hotseat]
    autoresolve_battles = 0
    disable_console = 0
    disable_papal_elections = 0
    save_prefs = 0
    update_ai_camera = 0
    validate_diplomacy = 1
    Step 3

    1) Create a new text file in the same main MTW2 folder, name it "ExpansionTest.bat" and write in

    medieval2.exe --io.file_first @ExpansionTest.cfg

    Step 4

    1) Run the .bat file

    If it's all working right what should happen is the game will load and if you select single player / grand campaign / england / start then you should see the hotseat screen for your first turn as england. You don't need to bother about the password just click start. You'll be in a standard vanilla campaign in hotseat mode running from a mod folder.

    Quit game.

    Step 5

    1) Go to the mods\expansiontest\data\world\campaign\imperial_campaign folder and open the campaign_script.txt file.
    2) Near the top of the file, just below where it says:
    Code:
     
    ;----------------------
    ; monitors
    add

    Code:
     
    monitor_event FactionTurnStart TrueCondition
    console_command control england
    terminate_monitor
    end_monitor
     
    monitor_event FactionTurnStart FactionType slave
    and I_TurnNumber = 10
    console_command control england
    terminate_monitor
    end_monitor
    What these scripts do is as soon as the game starts control of the england faction is handed over to the to the ai. After the number of turns determined by the value you put in the script (in this case 10) control is returned to the player.

    Effect

    1) Use your expansiontest.bat file to start the game again, select single player / grand campaign / england / start and when the hotseat screen comes up click through that. What should happen if everything is working right is the ai should immediately take control of the england faction as well as all the others and carry on that way for n turns at which point control of england is handled back to you.

    2) When the ai takes control hit the space bar once so the game uses fast movement.

    3) Open up the console - on my keyboard it's the @ key that brings up the console but i believe it varies - then type "toggle_fow" so you can see the whole map.

    nb The number of turns to use is tricky as you need to use different numbers to test different types of edit. The testing process is a lot faster if the number of turns is low so it's a good idea to test changes that show an effect within a few turns first with a low number of turns, then lengthen the test runs later if neccessary.

    4) Count something.

    What to count will depend on what you're trying to test. If you're testing general AI expansion and you've set the number of turns to be low then it's quicker to count the number of regions the AI factions have taken. If you've set the number of turns higher then it will eventually become quicker to test something else like the number of rebel regions remaining.

    5) Repeat the test a number of times and average the results to try and iron out random events.

    Baseline

    For my initial tests i'm going to set the number of automated turns low and count the number of regions taken by the AI factions.

    For the initial group of tests i set the number of automated turns to 10. As the game starts at turn 0 this returns control after 11 turns (i meant it to be 12 but counted wrong).

    Six tests using the vanilla files gave me a baseline total of 102 regions gained by the AI factions.

    nb The numbers per test were 18, 17, 20, 18, 12, 17. Generally in my testing the numbers tend to mostly cluster but with dramatic outliers. I think this is because in an average game maybe 2-3 factions have a major glitch. In some games maybe only one does and the expansion is particularly good. In other games maybe 4-5 factions have a major glitch and there's a big drop.

    Given the level of randomness in the game i don't think six tests is really enough but it takes too long to do more. However i think if a change produces an improvement of 10%+ then it's reasonable to assume there's some benefit.

    Lastly instead of adding each change on top of each other i'll make a change, test it individually, then remove it again before the next change so each test will be compared purely with the vanilla game. This is because the same problem can have multiple work-arounds. One fix might help with 10 AI problems and another might help with 30 but some of those problems will be the same. It's easier to see the general scale of improvement if it's just compared to vanilla.

    +++

    Next post: Movement points
    Last edited by tupodawg999; January 15, 2010 at 10:15 PM.

  3. #3

    Default Re: AI Expansion

    Post 3: Movement Points.

    The AI in MTW (and RTW) has major path-finding problems. One of the main reasons for slow AI expansion is the AI armies are constantly getting stuck trying to reach their targets.

    I believe the main problem is simply that the path-finding calcs take too long when there's a lot of units to move in the AI turn so the game uses a strong path-finding method to find a route to a target up to one turn's worth of movement points away and if it hasn't found the target within that range then the ai switchs to a weaker but faster method of path-finding. Therefore increasing the army movement points increases the range the game uses the strong path-finding method for each AI each turn.

    The downside is this can have a dramatic effect on the length of the AI turn to the point where the game becomes unplayable (at least to me) so there's a compromise to be made. Strict agent limits can help with this as the AI factions have a habit of clogging up the game spamming spies and other agents.

    The default value for army movement is 80 movement points. What value to increase it to will depend on map size, scale, region density etc and will have to be found by trial and error and tested late game when the map is swarming with AIs. From previous testing with the vanilla map i think 120 is a good value so i'll go with that.

    Modding Details

    Step 1

    Copy the vanilla descr_character.txt file from the vanilla mtw2 data folder and copy it to our mods/expansiontest/data folder.

    Step 2

    Open decr_character.txt and edit the values for movement points to be 120 for armies and 180 for admirals and agents:

    Code:
    starting_action_points 120 ; default value for all characters and pathfinding calculations
    Code:
     
    type named character
    actions moving_normal, moving_quickmarch, garrison, assault, attack, besiege, entrench, ambush, diplomacy, bribe, exchange, building_fort, building_watchtower
    wage_base 100
    starting_action_points 120
    Code:
     
    type general
    actions moving_normal, moving_quickmarch, garrison, assault, attack, besiege, entrench, ambush, exchange
    wage_base 0
    starting_action_points 120
    and for the agents and admirals

    Code:
     
    type spy
    actions moving_normal, spying
    wage_base 100
    starting_action_points 180
    Code:
     
    type admiral
    actions moving_normal, quick_sail, blockade, disembark, exchange
    wage_base 50
    starting_action_points 180
    repeat for all the others.

    Effect

    Run the ai-controlled tests and count the number of regions captured by the AI.

    In my case the total was 139 which is a nice improvement over 102.

    (20, 23, 27, 23, 23, 23)

    Increasing the base movement points will give the biggest improvement for the least modding effort.

    +++

    Next post: AIMovementBonus trait.
    Last edited by tupodawg999; January 15, 2010 at 09:25 PM.

  4. #4
    konny's Avatar Artifex
    Join Date
    Jul 2007
    Location
    Germania Inferior
    Posts
    3,631

    Default Re: AI Expansion



    A lot of interesting stuff here. I would suggest to move this thread to the scripting section.


    Quote Originally Posted by tupodawg999 View Post
    Another possibility might be to have just one level of the trait that gave a 100% bonus right away but with an anti-trait and triggers that removed the bonus. The anti-trait triggers might be:
    1) Post-battle - to hopefully reduce retreating too far
    2) Ended turn in settlement.
    3) Ended turn outside a settlement but with < 50 movement.

    I'm not that familiar with anti-traits so i did it this way with four incremental bonus movement levels to dilute any unintended consequences a bit.
    An antitrait reduces points of his opposing trait first before accumulating points itself. That is, when you have GoodCommander and BadCommander as antitraits and a character gains one level in GoodCommander for winning a battle, the next battle lost will not give him a level in BadCommander (what would result in him being a good and a bad commander at the same time) but take away the level acquired before in GoodCommander. Further battles lost would then give him points in BadCommander.

    For your purpose I would indeed suggest to use one level in AIMoveBonus and a trigger with a negative value for reset:

    Code:
     
    ;------------------------------------------
    Trait AIMoveBonus
    Characters family
    ;Hidden
     
    Level AIMoveBonus1
    Description AIMoveBonus1_desc
    EffectsDescription AIMoveBonus1_effects_desc
    Threshold 1 
    Effect MovementPoints 10
    ;===============================================================
    
    Trigger Reset_AIMoveBonus_trigger
    WhenToTest CharacterTurnEnd
     
    Condition Trait AIMoveBonus = 1
    
    Affects AIMoveBonus -1 Chance 100
    ;--------------------------------------------
    Trigger AIMoveBonus_trigger
    WhenToTest CharacterTurnEnd
     
    Condition IsGeneral
    and not EndedInSettlement
    and RemainingMPPercentage > 95
    and not FactionIsLocal
     
    Affects AIMoveBonus 1 Chance 100
    That way the bonus trait will be reseted for each character who has it and instantly afterwards given anew for anyone qualifing, allowing him to use the bonus movement the next turn.

    Team member of: Das Heilige Römische Reich, Europa Barbarorum, Europa Barbarorum II, East of Rome
    Modding help by Konny: Excel Traitgenerator, Setting Heirs to your preference
    dHRR 0.8 beta released! get it here
    New: Native America! A mini-mod for Kingdoms America

  5. #5

    Default Re: AI Expansion

    @Konny
    A lot of interesting stuff here. I would suggest to move this thread to the scripting section.
    Ty. I posted here because some of the changes will be map edits so the changes will be both text file and map.

    For your purpose I would indeed suggest to use one level in AIMoveBonus and a trigger with a negative value for reset:
    Yes on reflection i think you're right. I'll redo it.

  6. #6

    Default Re: AI Expansion

    Post 4: AIMovementBonus Trait

    (Hat-tip to Konny for suggesting an improvement to this which i've incorporated.)

    Most players will have seen the basis of the next change many times without realising the significance - an AI stack will join a crusade/ jihad and suddenly attack a nearby city that has nothing to do with the crusade / jihad target. A lot of players will have seen France do this with Bruges early in a vanilla game but it will happen all over the map at some point or other during a game.

    What happens is one of the most common pathing glitches involves an AI stack moving to attack a city gets stuck at a point more than one but less than two turns movement away from the city. However anything that brings the target city within one turn's movement of the bugged army fixs the problem and the stack moves again. Many things can do this like a road being built in the region but one of the most common in MTW2 is the stack joins a crusade and gets its movement points doubled, and it seems, because the army was stuck when it joined the crusade, the original target city takes precedence over the crusade target city.

    This will only happen with a general army and not a captain army obviously.

    The AI much prefers to have its generals as governors - unless it has spare generals it will usually move all of them into cities and use mostly captain armies. However one advantage the AI seems to have is it often gets captains promoted to generals on the move and the captains that get promoted seem to be almost always those armies that have been told to capture a city. What this means is the critical AI stacks - the ones that get stuck on the way to attack settlements are the ones most likely to get general promotions.

    This kind of promotion is not rare but it's not common either - it's somewhere in between. I've seen an AI captain getting promoted en route to attack a city many times while testing but the frequency overall doesn't seem to be more than one per faction at a time and my guess would be when the faction already has a governor in each of its cities. There's also either a random element to it or it's very specific i.e when the faction has exactly x generals in x cities and captain armies moving to attack cities. If there's already excess generals it may not happen at all.

    Regardless of the details there will be many times during an average game where an AI captain army will get promoted to general while stuck more than one but less than two turns of movement away from its target.

    This provides the opportunity for a trait fix to help the AI expansion.

    Modding Details

    Step 1

    1) Add a "text" folder in "mods/expansiontest/data".
    2a) Either copy the export_VnVs.txt.strings.bin file from the main mtw2 "data/text" folder into mods/expansiontest/data/text and use the strings_bin_converter from http://www.twcenter.net/forums/downl...o=file&id=1006 to convert it to export_VnVs.txt. Delete the .bin file afterwards.
    2b) Or download the text files http://www.twcenter.net/forums/downl...do=file&id=951 , then copy the export_VnVs.txt file and paste it into "mods/expansiontest/data/text".
    3) Add at the top:

    Code:
     
    ¬--------------------
    {AIMoveBonus1} AIMoveBonus1
    {AIMoveBonus1_desc}
    AIMoveBonus.
    {AIMoveBonus1_effects_desc}
    +100% to Movement Points
    Step 2

    1) Copy export_descr_traits.txt from the main mtw2/data folder into mods/expansiontest/data.
    2) Under

    Code:
     
    ;===============================================================
    ;== TRAIT DATA STARTS HERE ==
    ;===============================================================
    add

    Code:
     
    ;------------------------------------------
    Trait AIMoveBonus
    Characters family
    ;Hidden
     
    Level AIMoveBonus1
    Description AIMoveBonus1_desc
    EffectsDescription AIMoveBonus1_effects_desc
    Threshold 1 
    Effect MovementPoints 10
    3) Lower down the same file underneath

    Code:
     
    ;===============================================================
    ;== TRIGGER DATA STARTS HERE ==
    ;===============================================================
    add

    Code:
    ;------------------------------------------
    Trigger Reset_AIMoveBonus_trigger1
    WhenToTest CharacterTurnEnd
     
    Condition Trait AIMoveBonus = 1
    Affects AIMoveBonus -1 Chance 100
     
    ;------------------------------------------
    Trigger Reset_AIMoveBonus_trigger2
    WhenToTest PostBattle
     
    Condition Trait AIMoveBonus = 1
    Affects AIMoveBonus -1 Chance 100
     
    ;------------------------------------------
    Trigger AIMoveBonus_trigger
    WhenToTest CharacterTurnEnd
     
    Condition IsGeneral
    and not EndedInSettlement
    and RemainingMPPercentage > 95
    ;and not FactionIsLocal
     
    Affects AIMoveBonus 1 Chance 100
    nb1 The live version of the trait would be Hidden and AI only but those parts are commented out for testing.

    nb2 There seems to be an odd glitch with the "RemainingMPPercentage" condition. In the original version of this trait when i had "RemainingMPPercentage = 100" in the trigger it wouldn't fire with base movement points of 120 plus a previous 20% movement bonus - some kind of strange rounding error somewhere maybe - however setting it to "> 95" works. This may be useful info for anyone getting odd results with that condition.

    nb3 The reason for the postbattle trigger is one of the unintended consequences of the trait i already knew about was the common situation where an AI army goes move->siege->siege->assault. In this case the AI would get the movement bonus at the end of the turn of sieging and then if the assault on the next turn fails the stack would sometimes retreat the full distance given by the move bonus. This could then lead to situations where the AI gets stuck at the spot they retreated to but the move fix won't help them because they already have it. Removing the movement trait postbattle hopefully prevents that.

    nb4 Getting stuck after retreating is the second biggest AI movement problem.

    Effect

    I removed the previous changes to bring the mod folder back to the vanilla state, made this change and then ran the six tests.

    My result was 114 regions gained (compared to the baseline of 102).

    Actual values 18, 22, 16, 17, 17, 24

    +++

    Next post: Brigands, Pirates and Revolts (part 1)
    Last edited by tupodawg999; January 15, 2010 at 10:16 PM.

  7. #7

    Default Re: AI Expansion

    Any chance you could upload a version of your map with the extra settlements?

  8. #8
    Gorrrrrn's Avatar Citizen
    Join Date
    Jan 2007
    Location
    here
    Posts
    5,546

    Default Re: AI Expansion

    re Mission Shocks
    http://www.twcenter.net/forums/showt...51#post6709051

    As far as I'm aware the AI factions don't get missions as such.

    --

    re settlement levels I'd recommend starting with a minimum level of towns and no villages - the latter are a pain in public order terms.
    ie their population isn't big enough to allow for public order buildings, and being captured usually causes a population drop.
    the AI doesn't keep a big enough garrison to maintain order, and sooner or later, the place rebels, the population drops and the settlement remains a village.
    and so on and so on ...

  9. #9
    irishron's Avatar Cura Palatii
    Moderator Emeritus

    Join Date
    Feb 2005
    Location
    Cirith Ungol
    Posts
    47,023

    Default Re: AI Expansion

    I naven't gotten into the mechanics, but in my mod the ai will grow villages. In fact, I sieged one that was a village by the time I got there was a large town so it can be done.

    We are using BBB 4 and XAI if that helps.

  10. #10
    Methoz's Avatar Senator
    Join Date
    Jul 2005
    Location
    Bohemia (Czech: Čechy; German: Böhmen) (CZE)
    Posts
    1,281

    Default Re: AI Expansion

    can downlaod any file for new mod?
    TotalWarForum.cz
    Česko-Slovenské fórum o hrách ze série Total War

  11. #11
    VINC.XXIII's Avatar Retired
    Join Date
    Mar 2009
    Location
    Oxybian hills
    Posts
    4,644

    Default Re: AI Expansion

    Very interesant thread, I would know how modd the power of the loyalists, who sometimes push the conqueror out from the city.
    Thanks

  12. #12
    z3n's Avatar State of Mind
    Moderator Emeritus

    Join Date
    Aug 2011
    Posts
    4,636

    Default Re: AI Expansion

    Moved this thread here.

    Pretty interesting read.
    The AI Workshop Creator
    Europa Barbaroum II AI/Game Mechanics Developer
    The Northern Crusades Lead Developer
    Classical Age Total War Retired Lead Developer
    Rome: Total Realism Animation Developer
    RTW Workshop Assistance MTW2 AI Tutorial & Assistance
    Broken Crescent Submod (M2TW)/IB VGR Submod (BI)/Animation (RTW/BI/ALX)/TATW PCP Submod (M2TW)/TATW DaC Submod (M2TW)/DeI Submod (TWR2)/SS6.4 Northern European UI Mod (M2TW)

  13. #13
    bitterhowl's Avatar Campidoctor
    Join Date
    Feb 2011
    Location
    Russian Feodality
    Posts
    1,568

    Default Re: AI Expansion

    I already read this thread about 2-3 years, but couldn't realize it's wisdom for that time.

    Well, that's a very interesting and illustrative example of AI improvement whith such a small changes. And also an argument of how disbalanced original game is.

    My sister, do you still recall the blue Hasan and Khalkhin-Gol?
    Russian warship is winning. Proofs needed? Go find yourself!

  14. #14
    bitterhowl's Avatar Campidoctor
    Join Date
    Feb 2011
    Location
    Russian Feodality
    Posts
    1,568

    Default Re: AI Expansion

    One more old hint (I suppose scince Rome I) - in descr_strat if you set different faction_creator for settlement then faction it owns at start of the campaign seems that faction set as faction_creator will try to capture this settlement. Need more tests but first drafts says it's true.

    So we can set principal targets for factions this way.

    My sister, do you still recall the blue Hasan and Khalkhin-Gol?
    Russian warship is winning. Proofs needed? Go find yourself!

  15. #15
    bitterhowl's Avatar Campidoctor
    Join Date
    Feb 2011
    Location
    Russian Feodality
    Posts
    1,568

    Default Re: AI Expansion

    Similar research form Nikolai1962 here https://forums.totalwar.org/vb/showt...-Game-AI-stuff

    My sister, do you still recall the blue Hasan and Khalkhin-Gol?
    Russian warship is winning. Proofs needed? Go find yourself!

  16. #16
    Jadli's Avatar The Fallen God
    Citizen

    Join Date
    Dec 2013
    Location
    Czech Republic
    Posts
    8,508

    Default Re: AI Expansion

    Nice work! Wanted to rep but repped some other ppl recently

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •