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. Opera

    Opera Deity

    Joined:
    Sep 21, 2008
    Messages:
    4,643
    Oh, so this is what purity is? It means that if they have a new religion appearing, if they already have another religion widespread, they will consider the new one much less? It kinda makes sense. I'll take a look...

    Edit: Just checked. I don't quite understand it. Don't know why but the logic of this snippet of code eludes me.
    Code:
    	int iBestCount = getHasReligionCount(eBestReligion);
    	int iSpreadPercent = (iBestCount * 100) / std::max(1, getNumCities());
    	int iPurityPercent = (iBestCount * 100) / std::max(1, countTotalHasReligion());
    	if (iPurityPercent < 49)
    	{
    		if (iSpreadPercent > ((eBestReligion == eFavorite) ? 65 : 75))
    		{
    			if (iPurityPercent > ((eBestReligion == eFavorite) ? 25 : 32))
    			{
    				return eBestReligion;
    			}
    		}
    		return NO_RELIGION;
    	}
    This purity thing bugs me.
     
  2. Rystic

    Rystic Turtle Wizard

    Joined:
    Jun 28, 2008
    Messages:
    677
    Location:
    New Jersey
    Awesome work! I'm using it in the TweakMod :D
     
  3. Deon

    Deon Lt. of Mordor

    Joined:
    Jan 13, 2008
    Messages:
    2,956
    Location:
    St.Petersburg, Russian Federation
    What does "?" operand mean?
    If I knew it, it'd be much clearer for me.
     
  4. misterslack

    misterslack Chieftain

    Joined:
    Jul 21, 2007
    Messages:
    42
    The ?: ternary operator is a short form of an if-else statement:

    Code:
    <condition> ? <expression1> : <expression2>
    This is equvalent to:

    Code:
    if (<condition>)
    	<expression1>
    else
    	<expression2>
     
  5. lemonjelly

    lemonjelly Modding For Ethne

    Joined:
    Jan 5, 2008
    Messages:
    864
    Location:
    UK
    Is this compatable with the other AI mods?

    And can somebody try and make a FF one please?

    Thanks
     
  6. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    It can be combined quite easily, but needs some adjustments.

    Anyway new version is coming soon and the noble AI is getting scary. (As in: tech tree can get finished by turn 400 and the AI is a credible military threat.)
     
  7. Rystic

    Rystic Turtle Wizard

    Joined:
    Jun 28, 2008
    Messages:
    677
    Location:
    New Jersey
    I just wrote some code that helps the Lanun AI develop Pirate Coves. Do you want to add that to your build?

    Spoiler :
    On a water map, Hannah is a frightening tech monster.
     
  8. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Yeah that's one of the last big things I could not do. Would be great if you could post the code.
     
  9. Rystic

    Rystic Turtle Wizard

    Joined:
    Jun 28, 2008
    Messages:
    677
    Location:
    New Jersey
    Bear in mind, it's only simulated coving. Without ripping apart the .dll, there's no way for them to build work boats and actively build Pirate Coves. The AI doesn't understand spells enough.

    What I did was make it so when the Lanun found a city, they check their first ring for coastal tiles. If one is found, they check a two-square region around it, too see if there are any conflicting Pirate Coves. If there are, the tile is skipped. Same thing happens if they don't control the tile, or if the tile has a resource. Otherwise, a Cove is placed. Whenever a city expands, another check is made, this time within the first two rings of the city. Same conditions for placement as above, except in the second check, it also ignores lakes. This usually leads to cities having two coves, and occasionally having three (with lucky placement).

    The Lanun AI has to have Fishing before it kicks in, so they won't have an immediate rush out the gate. They'll start coving about the same time a human player would.

    This goes into CivEventManager, under the CityBuilt method:
    Code:
    		if pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_LANUN'):
    			eTeam = gc.getTeam(pPlayer.getTeam())
    			if not pPlayer.isHuman() and eTeam.isHasTech(gc.getInfoTypeForString('TECH_FISHING')):
    				iX = city.getX()
    				iY = city.getY()
    				iCoast = gc.getInfoTypeForString('TERRAIN_COAST')
    				for iiX in range(iX-1, iX+2, 1):
    					for iiY in range(iY-1, iY+2, 1):
    						pPlot = CyMap().plot(iiX,iiY)
    						if pPlot.getTerrainType() == iCoast and cf.isCoveValid(pPlot,True):
    							pPlot.setImprovementType(gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE'))
    
    This goes into CivEventManager, under the CultureExpansion method:
    Code:
    		if pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_LANUN'):
    			eTeam = gc.getTeam(pPlayer.getTeam())
    			if not pPlayer.isHuman() and eTeam.isHasTech(gc.getInfoTypeForString('TECH_FISHING')):
    				iX = pCity.getX()
    				iY = pCity.getY()
    				iCoast = gc.getInfoTypeForString('TERRAIN_COAST')
    				for iiX in range(iX-2, iX+3, 1):
    					for iiY in range(iY-2, iY+3, 1):
    						pPlot = CyMap().plot(iiX,iiY)
    						if pPlot.getTerrainType() == iCoast and cf.isCoveValid(pPlot, False):
    							pPlot.setImprovementType(gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE'))
    
    Finally, this goes anywhere in CustomFunctions:
    Code:
    	def isCoveValid(self, plot, LakeValid):
    		cove = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_COVE')
    		harb = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_HARBOR')
    		port = gc.getInfoTypeForString('IMPROVEMENT_PIRATE_PORT')
    		iX = plot.getX()
    		iY = plot.getY()
    		owner = plot.getOwner()
    		if owner == -1:
    			return False
    		player = gc.getPlayer(owner)
    		if player.getCivilizationType() != gc.getInfoTypeForString('CIVILIZATION_LANUN'):
    			return False
    		if plot.isLake() and LakeValid == False:
    			return False
    		if plot.getBonusType(-1) != -1:
    			return False 
    		for iiX in range(iX-2, iX+3, 1):
    			for iiY in range(iY-2, iY+3, 1):
    				pPlot = CyMap().plot(iiX,iiY)
    				imp = pPlot.getImprovementType()
    				if imp==cove or imp==harb or imp==port:
    					return False
    		return True
    
     
  10. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Ah so you basically let the AI cheat and they get the coves for free without sacrificing a workboat. That would not work for me.
    Getting the AI to build an (extra) workboat is pretty trivial. There is also some code in the Final Frontier mod for AIs to Construct Starbases by moving Constructor ships to a designated plot. I was hoping to adapt that for workboats by modifying AI_UnitUpdate.
    Should be possible with just Python.
     
  11. Sephi

    Sephi Deity

    Joined:
    Jan 25, 2009
    Messages:
    2,908
    AI_unitUpdate (the python callback) is run only for barbarian units
     
  12. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Ah that explains why I had no success doing anything. Are there more callback functions that are similarly modified?
     
  13. Rystic

    Rystic Turtle Wizard

    Joined:
    Jun 28, 2008
    Messages:
    677
    Location:
    New Jersey
    It's actually not as bad as it sounds. I played a game as the Lanun, with a Lanun AI, and they kept up with me cove-wise.

    You could check whenever the Lanun build a workboat, and hijack it. Then the code I wrote could still be used in it's entirety; you can either physically send the workboat to the first valid tile, or remove the workboat and place the cove. The second option would work most of the time, except when there's a two-tile or three-tile thick strip of land with water on both sides (likely to happen on an Archipelago map). You could easily modify the CityExpansion/CityBuilt code to queue up a workboat whenever a valid tile is located.
     
  14. Sephi

    Sephi Deity

    Joined:
    Jan 25, 2009
    Messages:
    2,908
    hopefully not. You can try to put the workboat stuff into the end player turn function. That one should be run after a workboat is constructed and before unitupdate kicks in to give move orders
     
  15. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    updated version uploaded in the first post. Lots of improvements, especially the Rhoanna Hippus AI is very scary now.

    I'd be very curious if anyone can beat the emperor+ AI on a Pangaea map with no Acheron.
    If you do, please post a save!

    edit: Aww why doesn't the text of the title get changed? Now I'm stuck at 0.5 forever :(
     
  16. PPQ_Purple

    PPQ_Purple Techpriest Engineer

    Joined:
    Oct 11, 2008
    Messages:
    3,577
    How will this work with FF and FF+?
    Will it need any modifications to fit into there?
     
  17. Turinturambar

    Turinturambar Prince

    Joined:
    Dec 19, 2005
    Messages:
    368
    Yes it will need adjustments.
    Combining the code should be pretty trivial, however the changes of FF probably make a lot of my changes not desirable.
     
  18. Monkeyfinger

    Monkeyfinger Deity

    Joined:
    Oct 24, 2006
    Messages:
    2,002
     
  19. Valkrionn

    Valkrionn The Hamster King

    Joined:
    May 23, 2008
    Messages:
    14,450
    Location:
    Crestview FL
    That's actually why I haven't attempted to merge either this or the AI mod... FF has already fixed some of the same issues, usually in different ways. Much simpler to start fresh than to attempt to merge them... Which Sephi is already doing. ;)
     
  20. retro V

    retro V Prince

    Joined:
    Jun 9, 2006
    Messages:
    421
    Location:
    Location: Location
    In case it hasn't already occurred to you, PM a forum moderator to change it for you; there's certainly enough interest in this modmod and it's thread to warrant an accurate title :goodjob:
     

Share This Page