Creating a building when a city is founded

Seven05

Warmonger
Joined
Dec 5, 2005
Messages
2,056
Location
USA
What I'm doing is replacing the civilization specific unique units, sort of. Perhaps it's best described as removing them from the civilization definitions and having each unique unit require a unique building that is created when you found a city. This means several things will happen over the course of the game; First, you will be unable to produce your civilization's unique units at a city you didn't build (although this may be addressed by allowing some units to create the building if I can get the AI to build them). Second, you might be able to build the unique units from another civilization if the building survives when you conquer or "flip" the city. Third, you can produce both the "standard" unit (e.g. Spearman) and your unique unit (e.g. Phalanx). And finally, you may loose the ability to produce your unique unit if your city is conquered and you retake it since the building may be destroyed in the fighting. Essentially, it adds a little depth to conquests and gives you something new to play with if you destroy another civilization or conquer a number of their cities.

Right now I have all of the buildings, they are simple names like "BUILDINGCLASS_AMERICA" and "BUILDING_AMERICA" for one example. I also have removed the unique units from the civilization definitions and change the unique units so they can be built as long as the new building exists in the city. I have also done some minor adjustments to make the unique units slightly better and cost slightly more since you have the option of creating them now, they aren't forced on you.

So far everything works except the building will only be created automatically when you found your first city and then only if it is defined in your civilization definition. It basically works like the palace and creates one free building for you but only in your first city. Making the buildings "free era buildings" puts them in everybodies cities, not just for the appropriate civilization. This isn't a problem for the player if I allow the actual unique units to create the building in any city that doesn't already have it (like the great scientists' academies). Unfortunately, the AI won't build them, or at least not that I've seen so far in testing. I think I've also stumbled upon the ability to define unique buildings much like the current unique units, so basically I could have this new special building replace barracks and each civ would produce their own special barracks. It's a viable option but I'd have to test it more to see if it actually works and then it defeats part of my objective in that it would allow you to build your UUs in conquered cities and may not allow you to build the UUs from another civ.

So anyway, on to the request...

I'd like some help in creating the script to insert a building into each new city based on which civilization founded the city. Is it possible? I've only done some copy & paste merging in python so I have no idea where to start. Any help would be greatly appreciated.
 
Wow, python is good at giving me a headache...

I think I can figure out when a city is founded but I have no idea how to go about adding a building to it, are there any mods out there that add buildings to cities so I can see how that's done?
 
I'm beginning to thing the onCityBuilt event handle doesn't work, that or my python code is a mess and won't work. I've even tried simple message popups like the triggered even that prompts you for a city name and can't get that to work.

So far the XML code works, I have the buildings (one per civ) and they are required to build the unique units. That much of it works perfectly, I can add or remove buildings in the world builder and they work exactly as expected. If I define it as a free building for the civilization it spawns fine in the first city, I'll have to test it adding a free building in every city through XML but I'm pretty sure that'll have undesired results for me.

With python scripts I have a modified event manager with my "new" onCityBuilt event that calls a "free building" trigger. Here's the event manager code:

This is in a custom event manager with cvEventmanager.py defined as parent
Code:
	def onCityBuilt(self, argsList):
		city = argsList[0]
		self.wpUniqueUnit.wpUniqueUnitFreeBuilding(self, city)	
		self.parent.onCityBuilt(self, argsList)

wpUniqueUnit.py has the "guts" of the code. The first is the initValues which is called by the EventManager's __Init__ routine:
Code:
class wpUniqueUnit:

	def initValues(self):

		self.iAmerica = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_AMERICA')
		self.iArabia = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_ARABIA')
		self.iAztec = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_AZTEC')
		self.iChina = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_CHINA')
		self.iEgypt = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_EGYPT')
		self.iEngland = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_ENGLAND')
		self.iFrance = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_FRANCE')
		self.iGermany = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_GERMANY')
		self.iGreece = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_GREECE')
		self.iInca = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_INCA')
		self.iIndia = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_INDIA')
		self.iJapan = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_JAPAN')
		self.iMali = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_MALI')
		self.iMongol = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_MONGOL')
		self.iPersia = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_PERSIA')
		self.iRome = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_ROME')
		self.iRussia = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_RUSSIA')
		self.iSpain = CvUtil.findInfoTypeNum(gc.getBuildingInfo,gc.getNumBuildingInfos(),'BUILDING_SPAIN')

The next is the code that gets called by the onCityBuilt event, this is a sloppy implimentation I'm sure, but I don't know enough about python to streamline it:

Code:
	def wpUniqueUnitFreeBuilding(self, city):

		if (not self.iSpain):
			self.initValues(self)

		owner = PyPlayer(city.getOwner())
		race = owner.getCivilizationType()

		CyInterface().addImmediateMessage("City Created","")

		if (race == 0):
			city.setHasRealBuilding(self.iAmerica, True)
		elif (race == 1):
			city.setHasRealBuilding(self.iArabia, True)
		elif (race == 2):
			city.setHasRealBuilding(self.iAztec, True)
		elif (race == 3):
			city.setHasRealBuilding(self.iChina, True)
		elif (race == 4):
			city.setHasRealBuilding(self.iEgypt, True)
		elif (race == 5):
			city.setHasRealBuilding(self.iEngland, True)
		elif (race == 6):
			city.setHasRealBuilding(self.iFrance, True)
		elif (race == 7):
			city.setHasRealBuilding(self.iGermany, True)
		elif (race == 8):
			city.setHasRealBuilding(self.iGreece, True)
		elif (race == 9):
			city.setHasRealBuilding(self.iInca, True)
		elif (race == 10):
			city.setHasRealBuilding(self.iIndia, True)
		elif (race == 11):
			city.setHasRealBuilding(self.iJapan, True)
		elif (race == 12):
			city.setHasRealBuilding(self.iMali, True)
		elif (race == 13):
			city.setHasRealBuilding(self.iMongol, True)
		elif (race == 14):
			city.setHasRealBuilding(self.iPersia, True)
		elif (race == 15):
			city.setHasRealBuilding(self.iRome, True)
		elif (race == 16):
			city.setHasRealBuilding(self.iRussia, True)
		elif (race == 17):
			city.setHasRealBuilding(self.iSpain, True)

If I put a message popup in the onCityBuilt event I get the message, if I put it into my initValues or wpUniqueUnitFreeBuilding I never see it. I'm not getting any errors when I fire up the game, I had a few python errors before but they were fixed by adding the appropriate includes.

Looking though the original python scripts and those from the American Revolution mod I see that the onCityBuilt event calls the name edit popup trigger so it should call my trigger. The American Revolution mod uses the same "setHasRealBuilding" to add building to existing cities.

I'm lost, but I'm going to try moving the code to a different location to see if I can get it to work. I'd like to keep it tied to the onCityBuilt event if possible since that one should get fired off to frequently.
 
Back
Top Bottom