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

Better economic AI (V. 0.5)

Discussion in 'FfH2 Modmods, Scenarios, and Maps' started by Turinturambar, Jun 7, 2009.

  1. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    This mod fixes most of the bad AI economic behavior by forcing them to make good decisions. It consists 4 updated files: CvGameUtils.py, CvSpellInterface.py, CIV4SpellInfos.xml and PythonCallBackDefines.xml .

    Installation Instructions: Simply unzip the zip file into your beyond the sword/mods folder. If you have modified your game, you should make a backup copy of your CvGameUtils.py, CvSpellInterface.py, CIV4SpellInfos.xml and your PythonCallBackDefines.xml files first.

    The mod is compatible with FFH2 patch 041g and earlier.

    Suggested settings:
    For best AI performance I recommend playing with aggressive AI, no Acheron and a Continents-, Lakes- or Pangeae- style map.

    Features:
    AI will efficiently spend its available hammers.
    AI will research Religion Founding techs if appropriate/worthwile.
    AI will optimally research worker techs in the early game.
    AI will use efficient civics/ is fully capable of running an aristocracy economy.
    AI will be forced to research midgame economic techs when appropriate.
    AI will no longer build forts or build workshops.
    AI will build a sufficient amount of warriors to defend itself in the very early game.
    AI will not suffer an economic crash anymore.
    AI will intelligently use advanced religion beelines to late game heroes.
    AI will be better able to capture cities
    AI will better use unique buildings ( Warrens and Governors Manors in particular)
    Luchuirp tech and build AI improved (hello fireball spamming golems)
    AI will build mana nodes somewhat intelligently

    New in 0.7
    AI world spell casting improved
    AI will somewhat adjust its research/production decisions if they are at war.
    AI can now cast most spells properly, most notably: Crew promotions, feasting, Pirate Coves
    AI research decisions greatly improved
    AI production improved, AI will now build heroes and wonders very quickly.
    AI military production improved, AI will now build one tier2 enabling building in each city
    AI will build appropriate numbers of adepts (depending on available spells and traits)
    AI worker building tweaked, AI improves its land now very quickly.
    AI will aggressively spread and adopt religions of which it has the holy city

    Civ specific:
    Illian AI understands when to build their temple of the hand
    Kuriotates AI will not build settlers after they have reached their max number of allowed cities, drastically improving their AI.

    It should be easy to use the changes for other mods as I have only modified the following functions in CvGameUtils.py:

    1. canbuild
    2. cannotResearch
    3. cannotDoCivic
    4. cannotTrain
    5. cannotConstruct
    6. cannotCreate
    7. AI_chooseTech
    8. AI_chooseProduction (new in 0.6)

    The changes to the Spellfiles should be easily findable with winmerge. The main part of the mod is in CvGameutils.py anyway.
    Of course I also needed to activate the canbuild and cannotDoCivic functions in PythonCallBackDefines.xml.


    How much do these changes improve the AI? Check out this screenshots of a Pangeae noble game with aggressive AI, all unique features and no Acheron enabled:

    Without Changes:
    Spoiler :



    With Changes:
    Spoiler :



    Note the religious diversity (Sandolphon follows Esus), the rather scary Calabim stack and the scores of the top3 civs.
     

    Attached Files:

  2. cyther

    cyther Lord of the Dance

    Joined:
    Jun 9, 2008
    Messages:
    1,033
    Location:
    Fane of Lessers
    I just tested out a game and the AI looks pretty good. Could I use this in Rise of Darkness?
     
  3. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Sure! :)
    I just looked over the changelog for your mod and I couldn't see any compatibilty issues with the changes I made.
     
  4. [to_xp]Gekko

    [to_xp]Gekko QCT junkie

    Joined:
    Dec 16, 2005
    Messages:
    7,942
    Location:
    Seyda Neen, Vvardenfell
    awesome! :goodjob:
     
  5. Skyre Noktis

    Skyre Noktis Warlord

    Joined:
    Apr 28, 2009
    Messages:
    271
    Extremely good work, especially as I was going to be looking at this next (early worker techs in particular) and was trying to figure out a good way to do it. Looks like your results are rather impressive (though it would be nice to be able to see the leaders' scores on the right hand side).
     
  6. Senethro

    Senethro Overlord

    Joined:
    Mar 18, 2007
    Messages:
    4,634
    Location:
    The cutest of cephalopods
    Monarch, Normal Speed, turn 200:
    Top 3 are Beeri Bawl: 1350
    Decius of Bannor: 925
    Ethne: 800

    They're so good at getting gold I would suggest making a custom difficulty level which has raised unit maintenance and unit upgrade costs (i.e. 50% instead of 25%) just to soak up some of their excess.
     
  7. Skyre Noktis

    Skyre Noktis Warlord

    Joined:
    Apr 28, 2009
    Messages:
    271
    A minor criticism:

    Code:
    if eBonus == gc.getInfoTypeForString('BONUS_GOLD'):
    	imining = imining +1
    if eBonus == gc.getInfoTypeForString('BONUS_GEMS') :
    	imining = imining +1
    if eBonus == gc.getInfoTypeForString('BONUS_DYE') :
    	icalendar = icalendar +1
    if eBonus == gc.getInfoTypeForString('BONUS_INCENSE') :
    	icalendar = icalendar +1		
    Why are only dye and incense considered for calendar resources? Why not cotton, for example? This seems somewhat limited. Also, if you have wine in your initial fat cross, there's no real need to rush for either tech for the commerce benefits.

    You're also not taking into account whether the bonus is blocked by a terrain feature. If you're not an elven civ and you have a calendar resource in a forest, you'll need mining first regardless. If the resource is in a jungle square (common with both dyes and gems), you'll need bronze working before it's useful.
     
  8. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Only Incense and Dye are included because the others are either marginal (Cotton) or
    covered by Forest (Silk). Elven civs default to researching calendar before mining anyway (unless gold/gems are present).

    Since the code only checks for the Capital and the Capital gets dejunglified in standard mapscripts I don't need to check for features.

    Wine is also only a marginal resource commercewise. +2 commerce does not make a noticeable difference, however I could see that beelining crafting could be sensible for 2+ wine. However crafting is forced very early anyway in the mining beeline, so it's not as if it goes unimproved for long.

    Edit: Come to think of it beelining Crafting makes sense as both a food and a commerce tech, so I will include a check for wine resources and force it as first tech if there are 2+ wine resources.
     
  9. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    Very interesting changes. I'm eager to start a game with those :p
    (as Cyther, I may end up using them in my own modmod... which, being Orbis, will need a bit of tweak; I hope you wouldn't mind?)
     
  10. Skyre Noktis

    Skyre Noktis Warlord

    Joined:
    Apr 28, 2009
    Messages:
    271
    Why is cotton marginal? If there's only one, I agree that it's going to be relatively low priority, but they often come in clumps, in my experience, and it's worth getting hooked up if there are two or three near your capital. Provided you have a decent food supply, the +1 happiness can be extremely useful as well.

    Also, from a brief look at your code, only the Ljosalfar are hardcoded to pick calendar over mining, not the Svarts (unless I'm missing something obvious).

    Isn't it possible for the AI to move its initial settler in FfH? I'm sure I've seen it do that, so there's an outside chance of jungle in the fat cross, although it's probably not too likely on most maps.

    The low commerce bonus is offset by the fact that crafting is so cheap and that it also gives a food bonus. And almost any resource is worth it if there's at least two of them.

    This sounds sensible.
     
  11. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Sure go ahead and only take the parts you like/modify it to your hearts content.

    It gets hooked up rather early anyway, calendar is one of the first techs regardless of resources since it unlocks festivals and the agrarianism civic. The only decision is whether to go for mining or claendar first. An improved cotton only adds 2 commerce to the tile. Compare this to a mine on a standard hill, which adds +2 hammers and possible chop boni.

    If you farm the cotton resources before aquiring calendar and chop forests/build mines earlier, you will have get your settlers/warriors/workers out faster and work more and better tiles than 2/0/3 cottons.

    Good catch, I must have forgotten to include them.

    I have never seen this and I think I read somewhere that the AI is hardcoded to settle its first city immediately. Regardless it would be such a rare case and would not have especially bad consequences if it happens that I don't think adding more checks is sensible.
     
  12. Quetz

    Quetz The Tallest Lilliputian

    Joined:
    Aug 21, 2006
    Messages:
    663
    Location:
    Heavensent
    I see no improvement in Life Expectancy with these changes! Useless!

    I mean, great work XD

    Going to dump this into an FFPlus install and see what happens.
     
  13. Skyre Noktis

    Skyre Noktis Warlord

    Joined:
    Apr 28, 2009
    Messages:
    271
    Agreed it's an edge case. Just to clear this up, though, I've seen the AI move its initial settler quite often, although it always settles on the first turn.
     
  14. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    Can I suggest that you comment your changes? It's a bit hard to see what's part of vanilla and what's yours. Of course, using WinMerge, for example, is useful but... commenting is handy too :p
     
  15. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    Line 303: SHEIAM instead of SHEAIM
    In fact, it seems you did this mistake more than once... I begin to doubt it's a mistake, now...

    Why only prevent some civs to build disciples? I mean, if you have a religion Holy City, even though you're not Sheaim, Bannor or whatever, you should spread it, no?

    Okay, done reading thoroughly your code. Really interesting things in it. It will be a bit tricky to adapt it to Orbis but I'll do it.

    Congratulations for that, you thought of many things :goodjob:
     
  16. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Hah I just noticed that that part of the code is still messed up. Thanks for pointing it out.

    I wanted to make it so that only civs with the Holy cities or the civs which are attuned to the religion build disciples, so that everyone else doesn't waste hammers. In addition this should slow down the spread of religions, leading to more diversity.

    Corrected Code looks like this:
    Code:
    if eUnitClass == gc.getInfoTypeForString('UNITCLASS_DISCIPLE_THE_ASHEN_VEIL'):
    				if pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_DISCIPLE_THE_ASHEN_VEIL')) > 3:
    					return True
    				if pPlayer.hasHolyCity(gc.getInfoTypeForString('RELIGION_THE_ASHEN_VEIL')) == False :
    					if not pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_SHEAIM') :
    						return True
     
  17. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    Oh, right. So you want that no civ (apart from attuned ones and holy-city ones) manually spread religions? That's interesting. I remember this, manual spreading through disciples, being a huge factor in non-religiously-diverse games. Thanks for explaining that :)

    By the way, it's really hard to import this to a modmod which change many techs :lol: Cartography is much less valuable in Orbis (it's Writing that leads to Open Borders) and Exploration just doesn't exists!
     
  18. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    I've been tweaking your religions check in AI_chooseReligion and came up with that:
    Code:
    		####-----###
    		## Religion beelines
    		####-----###
    		iLeader = gc.getLeaderHeadInfo(pPlayer.getLeaderType())
    		iFlavor = ileader.getFlavorValue(gc.getInfoTypeForString('FLAVOR_RELIGION'))
    		iFoL = gc.getInfoTypeForString('RELIGION_FELLOWSHIP_OF_LEAVES')
    		
    		## FoL
    		if iLeader.getReligionWeightModifier(iFoL) >= 0:
    			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_MYSTICISM')) \
    			or eTeam.isHasTech(gc.getInfoTypeForString('TECH_TRACKING')) \
    			or pPlayer.getNumCities() > 3:
    				iDiceFoL = CyGame().getSorenRandNum(100, "FoL")
    				if pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_LJOSALFAR'):
    					iDiceFoL += 30
    				if iLeader.getFavoriteReligion() == iFoL:
    					iDiceFoL += 30
    				if iFlavor != 0:
    					iDiceFoL += iFlavor
    				if iDiceFoL <= iLeader.getReligionWeightModifier(iFoL):
    					iTech = gc.getInfoTypeForString('TECH_WAY_OF_THE_FORESTS')
    I tried doing something flexible, so you have bonus for things like being Ljos or having a high religion flavor, you're not just given the possibility of founding the religion only if you are Ljos. Not sure if it's worth it, though...
     
  19. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    I really like how religionfounding turned out in my mod. All civs have the possibility of founding religions, not just the attuned ones. In fact they regularly get beaten, but will still research the appropriate techs.

    It works like this:
    If the AI has researched the proper economic techs for its current economic situation and has researched Mysticism (Philosophy for the advanced religions) and has established initial settling (>= 3 cities), the AI will beeline one of the early religions under the following conditions:
    1) It does not have a holy city already (doing so under these circumstances would be wasteful and would lead to less religious diversity)
    2) The religion has not been founded yet. (again to promote diversity)
    3) The civ is not attuned to a specific religion (e. g. the Sheaim don't waste their research on chasing octopus overlords, because they will research Ashen Veil later on anyway.)
    4) The civ rolls a 5 or 6 on a six-sided die. (This ensures that different religions get chased by the nonattuned civs,leading to more efficiency and diversity)
    5) The alignment fits, meaning no civ will research a religion that shifts it alignment by 2 steps (from good to evil or vice versa) or shifts the alignment to neutral.

    Each religion has one civ that is attuned to it for flavour/gameplay reasons. Those civs can research the other religious techs, but they are never forced to do so. Once they pass the requirements for their preferred religion they will beeline it.

    Adding religious flavours to the random roll could be desirable, but I'm not sure if it will have a noticeable effect.
     
  20. UNIT 666

    UNIT 666 One of Many

    Joined:
    Mar 7, 2008
    Messages:
    340
    Location:
    Classified.
    I've just gotta say, Turin, you've done a GREAT job with this AI mod! Now I can finally get better at Civ while still staying at Noble difficulty!

    Thank you, Turin! :goodjob:
     

Share This Page