1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

How Difficulty Bonuses work for AI

Discussion in 'General Balance' started by Gazebo, Nov 29, 2017.

  1. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    VP introduced a new difficulty bonus mechanic back when it launched. It isn't heavily documented, so here's information on it. First, the code:

    Code:
    int CvPlayer::DoDifficultyBonus()
    {
        int iEra = GetCurrentEra();
        if(iEra <= 0)
        {
            iEra = 1;
        }
        int iHandicap = 0;
        int iYieldHandicap = 0;
        CvHandicapInfo* pHandicapInfo = GC.getHandicapInfo(GC.getGame().getHandicapType());
        if(pHandicapInfo)
        {
            iHandicap = pHandicapInfo->getAIDifficultyBonus();
            iYieldHandicap = (iHandicap * iEra * 5);
        }
        if(iHandicap > 0)
        {              
            GetTreasury()->ChangeGold(iYieldHandicap);
            ChangeGoldenAgeProgressMeter(iYieldHandicap);
    
            if (getNumCities() > 1)
            {
                changeJONSCulture(iYieldHandicap);
            }
    
            int iLoop;
            CvCity* pLoopCity;
            for (pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop))
            {
                if (pLoopCity != NULL)
                {
                    pLoopCity->ChangeJONSCultureStored(iYieldHandicap);
                    pLoopCity->changeFood(iYieldHandicap);
                    pLoopCity->changeProduction(iYieldHandicap);
                }
            }
                   
            int iBeakersBonus = GetScienceYieldFromPreviousTurns(GC.getGame().getGameTurn(), iHandicap);
    
            TechTypes eCurrentTech = GetPlayerTechs()->GetCurrentResearch();
            if(eCurrentTech == NO_TECH)
            {
                changeOverflowResearch(iBeakersBonus);
            }
            else
            {
                GET_TEAM(getTeam()).GetTeamTechs()->ChangeResearchProgress(eCurrentTech, iBeakersBonus, GetID());
            }  
        }
        return iYieldHandicap;
    }
    The only external value you need is AIDifficultyBonus, which is called <DifficultyBonus> in the DifficultyMod.xml. On Deity, for example, it is 7.

    This difficulty bonus triggers on the following events:

    1. Founding a City
    2. Winning a war
    3. Starting a golden age
    4. Entering a new era

    The AI also gets a bonus when you (a human) kill one of their units:

    Code:
    //Human killed me?
                if (MOD_BALANCE_CORE_DIFFICULTY && !kPlayer.isHuman() && GET_PLAYER(ePlayer).isHuman())
                {
                    int iHandicap = GC.getGame().getHandicapInfo().getAIDifficultyBonus() * (getUnitInfo().GetPower());
                    iHandicap /= 25;
                    if (getOriginCity() != NULL && getOwner() == getOriginCity()->getOwner())
                    {
                        getOriginCity()->changeProduction(iHandicap);
                    }
                }
    And that's it.

    The other bonuses in the handicap info work as follows:


    • StartingMinorDefenseUnits: CS starting extra units.
    • CityProductionNumOptionsConsidered - number of building/unit options selected from top scorers
    • TechNumOptionsConsidered - number of tech options selected from top scorers
    • PolicyNumOptionsConsidered- number of policy options selected from top scorers
    • AIBarbarianBonus - extra CS/RCS % v. barbs
    • AIStartingDefenseUnits - warriors at game begin
    • AIStartingWorkerUnits - workers at game begin
    • AIStartingExploreUnits - explorers at game begin
    • AIDeclareWarProb - % extra desire to DOW humans
    • AIWorkRateModifier - additional tile improvement mod
    • AIUnhappinessPercent - reduction of AI unhappiness values %
    • AIGrowthPercent - reduction of food % needed to grow
    • AITrainPercent - reduction of production % needed to train a unit
    • AIConstructPercent - reduction of production % needed to construct a building
    • AIWorldConstructPercent - reduction of production % needed to construct a world wonder
    • AICreatePercent - reduction of production % needed to finish a project
    • AIBuildingCostPercent - reduction of gold % for building maintenance
    • AIUnitCostPercent - reduction of gold % for unit maintenance
    • AIUnitSupplyPercent - increase % of max supply cap
    • AIUnitUpgradePercent - decrease % of gold cost for unit upgrades
    • AIPerEraModifier - value, scaling with era, further reduces production needed for unit/building/project production. Also increases unit supply % and reduces city growth threshold %. So a value of 5 would further reduce production needed for a unit by 5% in classical, 10% in medieval, etc.
    • AIFreeXP - starting XP for units
    • AIFreeXPPercent - extra XP earned from combat (%)
    • DifficultyBonus - see above

    All other handicap values are irrelevant for difficulty values.

    With this information, you should have all the data you need to understand the difficulty system.

    Also, if you have logging enabled, you can see how much the DifficultyBonus value is giving the AI. Look in the HomelandAI log and search for: 'CBP AI DIFFICULTY BONUS FROM.' to see what it does.

    Cheers,
    G
     
    Last edited: Nov 29, 2017
  2. LukaSlovenia29

    LukaSlovenia29 Emperor

    Joined:
    Mar 13, 2016
    Messages:
    1,500
    Thank you for taking the time to present this valuable information, it's much appreciated!

    1. May I ask, please, what does the number of <DifficultyBonus> mean, i.e. what does 7 mean?

    2. Also, would it be possible for you to write where is it specified how much food/culture/.../ the AI gets upon founding a city, winning a war etc.?

    3. If I understand what you've written (I'm not good at reading code), that means that in the Classical era the AI gets the same discount to discovering new techs as it gets in the Industrial era? The only three AI bonuses that scale with era are production, unit supply % and city growth % (I suppose that means population, not culture borders?)?

    4. Where in the files is it specified how much of a discount the AI gets re: discovering new technologies, i.e. how much cheaper new techs are in beakers for the AI vs. the human player?

    5. Is there a discount/bonus for the AI percentage-wise-cost of new policies?

    Thank you very much for any further information!
     
  3. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    1. You can plug that value into the function above at
    'iHandicap = pHandicapInfo->getAIDifficultyBonus();'

    The value is then passed into the DLL for all the other yield values.

    2. That's all found in the HomelandAI during a game - take a look and see!

    3. No, as the 'GetScienceYieldFromPreviousTurns' gets the civ's science output over the past 7 turns (on Deity) and turns it into a science boost. So it scales with the civ's science output.

    4 & 5. AI civs do not get discounts on techs or policies.
     
  4. CrazyG

    CrazyG Deity

    Joined:
    Oct 14, 2016
    Messages:
    4,831
    Location:
    Beijing
    I'm curious what the logic for winning a war or founding a city being the source of bonuses is. Wouldn't these contribute to the AI doing better with larger empires than smaller, taller empires? It could explain some of the differences in player experience (many players saying they find tall cheating) and AI performance (wide often runsaway)

    Also, many players have stated they find AI performance to decrease in later eras, it could be because their founding a city bonus is wearing off
     
    Moi Magnus and Owlbebach like this.
  5. ElliotS

    ElliotS Warmonger

    Joined:
    Jun 13, 2013
    Messages:
    2,704
    Gender:
    Male
    Location:
    Chicago
    So correct me if I'm wrong:
    On Deity the AI will have a Handicap bonus of Era*35 :c5culture: empire-wide + :c5food:/:c5production:/:)c5culture: for border growth) per city and :c5science: Equal to 35*Era plus their previous turn?

    Then this bonus triggers on

    1. Founding a City (No Empire-wide culture on city #1)
    2. Winning a war
    3. Starting a golden age
    4. Entering a new era

    Correct? (Probably not.)

    Does the first turn count as entering an Era?
    Does entering an era give you thatr eras rewards or the last?
    how often AIs trigger Golden ages. Is it every 10 turns later or every 50?
    Does capturing a city in war count for the trigger?


    Thoughts:
    This explains the difficulty curve. Not only do you found more cities than do any other of these events in most games, but all the cities tend to be founded earlier. This means the AI will get a ton of bonuses on the first half and almost none on the second half. (Unless I'm underestimating Golden Ages.)

    Additionally Yields grow exponentially. Getting 70 :c5science:/:c5culture: in Classical isn't equal to 210 in modern. It's more like 500 in modern.

    The reason Wide beats Tall for AIs is because Tall triggers much few of these events. As far as I'm concerned both should trigger a similar amount.

    Suggestion&Tweaks:

    I have a suggestion I think is worth trying now that I have the numbers:
    #1-
    Change
    iYieldHandicap = (iHandicap * iEra * 5);

    To
    iYieldHandicap = (iHandicap * iEra * iEra *1.5);

    Since we're in the business of infographics:
    4fc949846658d52c95d57b270d15622b.png

    #2-
    Add a new trigger: Births a great Musician/Scientist(Maybe Writer and/or Artist.)
    This favors tall and late, bringing the balance closer to players.

    Tweaks:
    You don't let them get culture on their first city for obvious reasons. It might be better if they got no bonuses on their first city. (Then there's no issue with Deity needing more techs at the start.)

    Barbs and City states should go off lowest tech level. If they're more advanced than full nations shouldn't they be the full nation?

    Finally:
    @LukaSlovenia29 Here: http://civ-5-cbp.wikia.com/wiki/AI_and_Difficulty
     
    Legen and Owlbebach like this.
  6. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    Close - the Science is not 35 turns, but rather just the flat 7 handicap bonus (the two ints are different!).

    1. No
    2. Newest era
    3. About every 50, yeah.
    4. Yes
    Honestly? It has been a long time since I wrote this code - it played to the AI's strengths in the early days of the mod (AI was not so good at city building at that time, but loved to spam cities and whatnot). I could definitely see the AI getting less bonus per 'event,' but throwing Historic Events in the mix for them. The AI gets quite a few of those per game (also tracked in the HistoricEventLog file).

    Edit: also forgot to note the unit production boost above for humans killing units.

    G
     
    Last edited: Nov 29, 2017
    ashendashin, Owlbebach and IcyAngel like this.
  7. CrazyG

    CrazyG Deity

    Joined:
    Oct 14, 2016
    Messages:
    4,831
    Location:
    Beijing
    Keep in mind that tradition gets disporportionatally more historic events than the other two trees early on and it would cause snowballing off of wonders. We can test that, but it looks like stonehenge might be a litte absurd if the AI pulls 35 culture, science, food and hammers as a bonus yields from it. Would increasing the passive bonus be an option? Especially for later eras
     
  8. ElliotS

    ElliotS Warmonger

    Joined:
    Jun 13, 2013
    Messages:
    2,704
    Gender:
    Male
    Location:
    Chicago
    Yeah I feel like in fine-tuning we've been balancing apples to oranges sometimes and this is why. (Or at least a large part of it.)

    Also G consider running tests with no bonus events and see how tall/wide balance goes.
     
  9. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    Which passive bonus? The AIPerEraModifier? You have to be careful with that. For example, on Deity, AIUnitCostPercent is 70. By Renaissance it is at 50, Industrial 40...Information at 10. You can see why that's dangerous - any lower and the AI starts pumping out stuff wicked fast or just breaks entirely.

    Re: 35 culture, that's what I'm saying, though - if we include more events, we can lower the value from 7 to, say, 4 or 3.

    Eh, if by 'we' you mean' not Gazebo' then yes - I actually do pay a lot of attention to this stuff, but I also don't debug in Deity very often at all.

    G
     
  10. ElliotS

    ElliotS Warmonger

    Joined:
    Jun 13, 2013
    Messages:
    2,704
    Gender:
    Male
    Location:
    Chicago
    But you test with AIs only. I'm saying that a lot of tall/wide balance concerns have been affected because of how advantageous AI bonuses are towards wide.

    I want to make sure it isn't a case of confirmation bias for me though, so I'm asking if the tall/wide balance shifts among AI with these handicap events removed completely.
     
  11. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    Mmm, but I always disable bonuses for one of them.

    G
     
    LukaSlovenia29 likes this.
  12. ElliotS

    ElliotS Warmonger

    Joined:
    Jun 13, 2013
    Messages:
    2,704
    Gender:
    Male
    Location:
    Chicago
    Huh? Please expound so I don't make assumptions.
     
  13. Gazebo

    Gazebo Lord of the Community Patch Supporter

    Joined:
    Sep 26, 2010
    Messages:
    17,848
    Gender:
    Male
    Location:
    Little Rock
    I put bonus-less AI in the games for control. Not every game, but most.

    G
     
  14. LukaSlovenia29

    LukaSlovenia29 Emperor

    Joined:
    Mar 13, 2016
    Messages:
    1,500
    Thank you Gazebo and Elliot for your replies!
     
  15. Owlbebach

    Owlbebach Emperor

    Joined:
    Jun 5, 2016
    Messages:
    1,728
    Location:
    Moscow
    Overall thanks a lot. While i was wrong about exact mechanics, i was right in that AI gets more bonuses from wide play, which explains why experienced players were asking for wide buff and you were observing wide snowballing.

    Also here is the thing: Wide play, especially warmongering, should be stronger that Tall. Why? It requires investments, it is more risky than Tall. You build army and go to war, if you loose, you're screwed, because Tall was building infrastructure while you spent you hammers for army. So there should be higher reward to justify risks

    This actually means "the more science AI has - the more bonuses it gets", which perfectly explains ridiculous runaways that happen from time to time. The whole idea leads to the situation that we have right now: every AI starts about the same, but by the time you reach Industrial - only 2 or 3 of them are competitive, all others are behind and have zero chances to catch up.

    Maybe it should be replaced by flat bonus, like cost reduction? This will lead to more AIs remaining competitive in later stages of the game. Right now is rich gets richer, poor remains poor (or smarter).

    In my most recent game i had Korea (that was actually a TALL Korea) that was 4th in science in Industrial , but then it took Rationalism and literally, without any exaggeration, researched 25 techs in LESS than 25 turns. I was checking it every turn, he was researching 1 tech per turn, sometimes 2 (probably because of Great Scientists). He slowed down only in Informational and started to research 1 tech every 2 or 3 turns.
     
  16. Deadstarre

    Deadstarre Expert

    Joined:
    Nov 1, 2015
    Messages:
    960
    Location:
    New York
    thats also a problem with yield %bonuses in general. thats literally all it does, if youre doing well the %bonus gives you a big bonus, if youre doing poorly it gives you garbage.

    vanilla had %bonuses that were problematic, then CBP came along and remove virtually all of them, and then as time went on in VP they kept getting added back in. not just to wonders here and there but now to basic buildings and policies all over the place as well. its no wonder only 2-3 AI remain competitive at industrial and the others will never catch up no matter what resolutions get passed.
     
    Last edited: Nov 30, 2017
  17. tu_79

    tu_79 Deity

    Joined:
    Feb 11, 2016
    Messages:
    6,938
    Location:
    Malaga (Spain)
    Such bonuses are usually limited to national wonders. It helps tall civs, where the few cities are strong, specially the capital.

    I don't agree with owlbebach in that wide needs more help. Staying small is difficult when you have warmongers nearby, supply limits hurt here.
     
  18. Owlbebach

    Owlbebach Emperor

    Joined:
    Jun 5, 2016
    Messages:
    1,728
    Location:
    Moscow
    Absolutely not. Defense is much much much harder for Wide because your border is much bigger. When you play Tall - you have small border and you don't need more units to defend. Moreover you have more money per city and your cities are stronger (if you play Tall - than you play Tradition). If you struggle with defense playing Tall - you should revise your settling criteria or pay more attention to how you fight. In my expirience 3-4 units + Tradition city can stop 15 units at least, but usually unlimited amount of them
     
  19. tu_79

    tu_79 Deity

    Joined:
    Feb 11, 2016
    Messages:
    6,938
    Location:
    Malaga (Spain)
    Defending, yes. Intervening, no. Because you are small, you can't reach many city states, you can't send many trade routes, once someone declares war on you. Great people is your best weapon.
    You can hit your neighbour and retreat, but if some other civ is snowballing farther, you can't do anything.
     
    Legen, Txurce and IcyAngel like this.
  20. Grabbl

    Grabbl Prince

    Joined:
    May 25, 2016
    Messages:
    429
    If the problem is that boni are too wide-focussed, couldn't the bonus for city founding be replaced by a bonus for city growth (new citizen), that scales with the number of citizens already in the city?

    This would be tunable to be more or less tall/wide-independent, also if it has two drawbacks that I see:
    First, there would be much more Handicap triggers and calculations, possibly decreasing performance (no idea if the duration of these calculations matters or not). Second, possible chain reactions as city growth triggers a food bonus, depending on era and city size.
     

Share This Page