Making a City in Python change to A Different Owner

GUSTAVUS II

Chieftain
Joined
Oct 12, 2007
Messages
44
Location
Malmo,Minnesota
I need some help with making a city change to a different owner(civ) in python.

I want cities to change owners instead of just making units appear to settle like
I was doing.


The code I am trying to modify would be similar to this:

Code:
def onBeginGameTurn(self, argsList):
		iGameTurn, = argsList
   

   if(iGameTurn == 8):
		#INFO
	          iNewPlayer = 12
                        iPlayer = 3
                        iPlayer2 = 4
                        iPlayer3 = 5
                        iPlayer4 = 6
                        self.CreateNewCivWhenAnotherisGoneMulti(iNewPlayer,iPlayer,iPlayer2                       ,iPlayer3,iPlayer4, 86, 40)

     def CreateNewCivWhenAnotherisGoneMulti(self, iNewPlayer,iPlayer,iPlayer2,iPlayer3     ,iPlayer4, iX, iY):
                pNewPlayer = gc.getPlayer(iNewPlayer)
                ploopPlayer = gc.getPlayer(iPlayer)
                ploopPlayer2 = gc.getPlayer(iPlayer2)
                ploopPlayer3 = gc.getPlayer(iPlayer3)
                ploopPlayer4 = gc.getPlayer(iPlayer4)
                if not ploopPlayer.isHuman() and not ploopPlayer2.isHuman() and not ploopPlayer3.isHuman() and not ploopPlayer4.isHuman() and not pNewPlayer.isAlive():
			#Create new Player
			pNewPlayer.setPlayable(True)
			g.addPlayer(iNewPlayer, pNewPlayer.getTeam(),      pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
			#Create a settler
			pNewPlayer.initUnit(gc.getInfoTypeForString ("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        
                        #Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return
 
You can use:

pPlayer.acquireCity(pCity,false,false)


Just figure out who you want to own the city at the end of the function (pPlayer) and which city you want them to own (pCity). The booleans state if it was acquired via Conquest or Trade (respectively). Probably also a good idea to give the city some culture under the new owner, maybe even remove the culture under the old owner
 
How do you indicate what city PCity reprsents?


Example:
Code:
def CreateNewCivWhenAnotherisGoneMulti(self, iNewPlayer,iPlayer,iPlayer2,iPlayer3,iPlayer4, iX, iY):
                [COLOR="Red"]pCity = ?[/COLOR]
                pNewPlayer = gc.getPlayer(iNewPlayer)
                ploopPlayer = gc.getPlayer(iPlayer)
                ploopPlayer2 = gc.getPlayer(iPlayer2)
                ploopPlayer3 = gc.getPlayer(iPlayer3)
                ploopPlayer4 = gc.getPlayer(iPlayer4)
                if not ploopPlayer.isHuman() and not ploopPlayer2.isHuman() and not ploopPlayer3.isHuman() and not ploopPlayer4.isHuman() and not pNewPlayer.isAlive():
		        #Create new Player
	                pNewPlayer.setPlayable(True)
		        g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
	                #Create a worker
		        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_WORKER"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        [COLOR="SeaGreen"]pNewPlayer.acquireCity(pCity,false,false)[/COLOR]
                        #Add a message on screen
	                CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

How do identify the city?
 
For the function I stated you need a city to already exist before you do it. So popping it up when a civ has been wipped out isn't possible (they don't have any cities left now, and if they did, there is a hostile unit standing where it used to be, about to decide between keeping or razing the city you want to convert).


There are many cases where a city is located in python, just search for pCity and you'll find some examples. Quite a few ways to approach it depending on what information you are starting with (player, plot, team, area, unit...)
 
I'm Sorry I am not very experienced with pyhon code. I only
understand a few basic lines of it. I am asking a question here
because I could not find in the search what a city identification
looks like.(Example: PCity = "Washington"? ) I know how players
and teams are identified by number,but have no clue how to
identify a peticular city on a turn even though I searched
a long time under terms like "PCity" and "acquireCity".
I would only use this code for standing cities, not destroyed cities.
 
For the code you have posted so far, you really can't generate a pointer to any existing city, unless you take one of the players you have a number for and steal one of their cities. The pointer to the city should be generated wherever you call this function from most likely. Unfortunately it is VERY dependant on what information you already have available and how you are going about deciding which city to use, so more details would be needed to be of much more help.

Here is a loop over all cities belonging to a player:

Code:
			for pyCity in PyPlayer(iPlayer).getCityList():
				pCity2 = pyCity.GetCy()

So pCity2 will point at each city which belongs to iPlayer, one after the other.
 
Sorry..I didn't supply the whole code to begin with. Here it is:
Code:
# When a turn begins :
	def onBeginGameTurn(self, argsList):
		iGameTurn, = argsList
#TURN 10 : MAKING A CIV RISE WHEN ANOTHER HAS FALLEN
		elif(iGameTurn == 8):
			#INFO
			iNewPlayer = 12
                        iPlayer = 3
                        iPlayer2 = 4
                        iPlayer3 = 5
                        iPlayer4 = 6
                        self.CreateNewCivWhenAnotherisGoneMulti(iNewPlayer,iPlayer,iPlayer2,iPlayer3,iPlayer4, 86, 40)
def CreateNewCivWhenAnotherisGoneMulti(self, iNewPlayer,iPlayer,iPlayer2,iPlayer3,iPlayer4, iX, iY):
                pNewPlayer = gc.getPlayer(iNewPlayer)
                ploopPlayer = gc.getPlayer(iPlayer)
                ploopPlayer2 = gc.getPlayer(iPlayer2)
                ploopPlayer3 = gc.getPlayer(iPlayer3)
                ploopPlayer4 = gc.getPlayer(iPlayer4)
                if not ploopPlayer.isHuman() and not ploopPlayer2.isHuman() and not ploopPlayer3.isHuman() and not ploopPlayer4.isHuman() and not pNewPlayer.isAlive():
		        #Create new Player
	                pNewPlayer.setPlayable(True)
		        g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
	                #Create a settler
		        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.acquireCity(pCity,false,false)
                        #Add a message on screen
	                CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return
 
In this case, just seeing your code is absolutely useless.

What does each player represent?

Why are so many values hardcoded?

The third line is an elif, so what other if checks came before it?

86/40 is the plot you want to work with, so can we assume that this is the plot you want the city to be on? If there is already a city there, just use: CyMap().plot(iX,iY).getPlotCity()

Is "g" CyGame()?

Why does the comment say to create a settler, but you actually create an infantry?
 
CyMap().plot(iX,iY).getPlotCity() is exactly what I needed.
Thanks, this is great because I can finish my ALL HISTORY MOD!
(A Larger and Improved Rhye's and Fall Mod)

By the way, the numeral system to identify civs is in the Worldbuilder file
line called "TeamID" which is typically 0-18 civs but I compiled
a 119 civ DLL.(not all Civs for play all at once of course)
(0) is always hardcoded as the first Civ in the WorldBuilder File,
(1) is the next civ in order and so on.

The reason the third line is "ELIF" instead of "IF" is because I made a
seperate code from my mod to test different things, but I originally didn't
wan't to bore anyone with trying to figure out this long code when only
a certain few lines actually mattered.
Here's the whole code (-Most of my code I learned from FABRYSSE)
Code:
from CvPythonExtensions import *
import CvEventManager
import CvUtil
import PyHelpers	
import Popup as PyPopup
import pickle		
import random
import CvTranslator
import sys
import CvAlertManager





# globals
gc = CyGlobalContext()
g = CyGame()
alert = CvAlertManager.CvAlertManager()
		


#the main class

class CvGustavusEventManager:
	def __init__(self, eventManager):
		# initialize base class
		eventManager.addEventHandler("BeginGameTurn", self.onBeginGameTurn)
		

				

	# When a turn begins :
	def onBeginGameTurn(self, argsList):
		iGameTurn, = argsList
		
		#BE CAREFULL : 
		#The Map I used to test the Mod is not so big :
		# X : from 0 to 103
		#Y : from 0 to 63
		#
		# I tested with create Germany at turn 1 and France + Spain at turn 4 => IT WORKS!
		
		###############################################################
		#
		#   ENTER YOUR INFOS HERE
		#
		###############################################################
		
		#TURN 1 : Germany
		if(iGameTurn == 1):
			#Germany have to start
			iNewPlayer = 7    # Player ID (= player's position in list) in your Map
			self.CreateNewCivGermany(iNewPlayer, 87, 40)    # 87 (X) and 40 (Y) are coordonates of the plot where the unit will be created
                
		#TURN 4 : France
		elif(iGameTurn == 4):
			#France have to start
			iNewPlayer = 8    # Player ID (= player's position in list) in your Map
			self.CreateNewCivFrance(iNewPlayer, 90, 17)    # 90 (X) and 17 (Y) are coordonates of the plot where the unit will be created
			
                #TURN 8 : Spain
		elif(iGameTurn == 5):
			#Spain have to start too
			iNewPlayer = 9    # Player ID (= player's position in list) in your Map
			self.CreateNewCivSpain(iNewPlayer, 86, 19)
                 
                #TURN 9 : Birth of Christ
		elif(iGameTurn == 6):
			#Tech Christianity to be Granted
			iNewPlayer = 1    
			self.CreateNewFaith(iNewPlayer, 80, 45)
                        iTechHappy28 = CvUtil.findInfoTypeNum(gc.getTechInfo,gc.getNumTechInfos(),'TECH_THEOLOGY')

                        pPlayer = gc.getPlayer(1)
                        pTeam = gc.getTeam(pPlayer.getTeam())
                        pTeam.setHasTech(iTechHappy28, true, 19, false, false)

                #TURN 10 : MAKING A CIV RISE WHEN ANOTHER HAS FALLEN
		elif(iGameTurn == 8):
			#INFO
			iNewPlayer = 12
                        iPlayer = 3
                        iPlayer2 = 4
                        iPlayer3 = 5
                        iPlayer4 = 6
                        self.CreateNewCivWhenAnotherisGoneMulti(iNewPlayer,iPlayer,iPlayer2,iPlayer3,iPlayer4, 86, 40)
                        
                #TURN 20 : MAKING A CIV RISE WHEN ANOTHER HAS FALLEN
		elif(iGameTurn == 20):
			#INFO
			iNewPlayer = 11
                        iPlayer = 2
                        self.CreateNewCivWhenAnotherisGone(iNewPlayer,iPlayer, 49, 45)
                 
                #TURN 25 : in reverse
		elif(iGameTurn == 25):
			#INFO
			iNewPlayer = 2
                        iPlayer = 11
                        self.CreateNewCivWhenAnotherisGone(iNewPlayer,iPlayer, 49, 45)

                #INSERT THE OTHER TURNS/CIVS HERE...
		#elif(iGameTurn == ??):
			#Germany have to start
			#iNewPlayer = ??
			#self.CreateNewCiv(iNewPlayer, ??, ??)
		
		###############################################################
		#
		#   END YOUR INFOS HERE
		#
		###############################################################
				
				
	# My function that creates the new unit :
	def CreateNewCivGermany(self, iNewPlayer, iX, iY):
		pNewPlayer = gc.getPlayer(iNewPlayer)
		if(not pNewPlayer.isAlive()):
			#Create new Player
			pNewPlayer.setPlayable(True)
			g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
			#Create a settler
			pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_FOUNDER"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SPEARMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
			#Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

        def CreateNewCivFrance(self, iNewPlayer, iX, iY):
		pNewPlayer = gc.getPlayer(iNewPlayer)
		if(not pNewPlayer.isAlive()):
			#Create new Player
			pNewPlayer.setPlayable(True)
			g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
			#Create a settler
			pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_FOUNDER"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SPEARMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_SWORDSMAN"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
			#Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return
        
        def CreateNewCivSpain(self, iNewPlayer, iX, iY):
		pNewPlayer = gc.getPlayer(iNewPlayer)
		if(not pNewPlayer.isAlive()):
			#Create new Player
			pNewPlayer.setPlayable(True)
			g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
			#Create a settler
			pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_FOUNDER"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
			#Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

	def CreateNewCivWhenAnotherisGone(self, iNewPlayer,iPlayer, iX, iY):
		pNewPlayer = gc.getPlayer(iNewPlayer)
                ploopPlayer = gc.getPlayer(iPlayer)
                if not ploopPlayer.isHuman() and not pNewPlayer.isAlive():
			#Create new Player
			pNewPlayer.setPlayable(True)
			g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
			#Create a settler
			pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_RAIDERTANK"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        #Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

        def CreateNewCivWhenAnotherisGoneMulti(self, iNewPlayer,iPlayer,iPlayer2,iPlayer3,iPlayer4, iX, iY):
                pCity = CyMap().plot(87,40).getPlotCity()
                pNewPlayer = gc.getPlayer(iNewPlayer)
                ploopPlayer = gc.getPlayer(iPlayer)
                ploopPlayer2 = gc.getPlayer(iPlayer2)
                ploopPlayer3 = gc.getPlayer(iPlayer3)
                ploopPlayer4 = gc.getPlayer(iPlayer4)
                if not ploopPlayer.isHuman() and not ploopPlayer2.isHuman() and not ploopPlayer3.isHuman() and not ploopPlayer4.isHuman() and not pNewPlayer.isAlive():
		        #Create new Player
	                pNewPlayer.setPlayable(True)
		        g.addPlayer(iNewPlayer, pNewPlayer.getTeam(), pNewPlayer.getPersonalityType(), pNewPlayer.getCivilizationType())
	                #Create a settler
		        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_INFANTRY"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
                        pNewPlayer.acquireCity(pCity,true,false)
                        pCity.changeCulture(iNewPlayer, 300, True)
                        #Add a message on screen
	                CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: A new Civilization (%s) has been created !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

        def CreateNewFaith(self, iNewPlayer, iX, iY):					
		pNewPlayer = gc.getPlayer(iNewPlayer)
		if(pNewPlayer.isAlive()):
			#Rome is Alive
                        #Create a unit
                        pNewPlayer.initUnit(gc.getInfoTypeForString("UNIT_WORKER"), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_NORTH)
			#Add a message on screen
			CyInterface().addMessage(g.getActivePlayer(),true,15,"Year %s: The Catholic Church (%s) has been born !" %(g.getGameTurnYear(), pNewPlayer.getCivilizationDescription(pNewPlayer.getCivilizationType())),'',0,'',ColorTypes(11), 1, 1, True,True)
			
		return

I used Infantry in places where settlers were usually made, because at the time
it was the only way I could transfer city ownership in historical order.
The entire code including the "g" lines work perfectly!
 
Ah, working out of a worldbuilder explains it. Normally you would use things like g.getInfoTypeFor("CIVILIZATION_BRITISH") or something along those lines to find the civ you want, then loop over the players to find which one matches. But if you have it forced already to be "34 shall always be Mayan" you can hardcode without too much issue.
 
Top Bottom