Modifying python snippet for unit costing population

Lib.Spi't

Overlord of the Wasteland
Joined
Feb 12, 2009
Messages
3,708
Location
UK
So I have this code from Orion immigration mod, that makes an immigrant cost two pop. to build:

Spoiler :
Code:
def onUnitBuilt(self, argsList):
		pCity = argsList[0]
		unit = argsList[1]
		if (AutologOpt.isLogBuildCompleted()):
			
			if pCity.getOwner() == CyGame().getActivePlayer():
				message = BugUtil.getText("TXT_KEY_AUTOLOG_FINISH_UNIT", (pCity.getName(), gc.getUnitInfo(unit.getUnitType()).getDescription()))
				Logger.writeLog(message, vColor="Purple")
# Immigration Mod
		iOwner = pCity.getOwner()
		pPlayer = gc.getPlayer(iOwner)
		UnitType = unit.getUnitType()
		iImmigrant = gc.getInfoTypeForString("UNIT_IMMIGRANT")
		intCity = Immigration.getLeastPopulatedCity(iOwner)
		
		if UnitType == iImmigrant:
			#CyInterface().addImmediateMessage("B", "")
			if pPlayer.isAlive() and not pPlayer.isBarbarian():
				ReducedCityPopulation = pCity.getPopulation() - 2
				pCity.setPopulation(ReducedCityPopulation)				
				if intCity != -1:
					MigrationCity = pPlayer.getCity(intCity)
					Immigration.doImmigrantPlacementAI(unit, MigrationCity)					
# End Immigration Mod

What I would like is to expand this to do 2 extra things.

1. Make all Units cost 1 pop to build.

2. Have a list of exempted units that do not cost any pop to build preferably using UNITCOMBAT_ROBOT(which would be less of a list)

Then I need to adapt this next bit of code to stop a city building 'pop cost' units if it is below a certain pop. value say 3. This bit will be a bit more difficult because it is written in an immigaration specific bit of code, so I would also need to know where to place the modded code to get it working, i.e. under what callback.

Spoiler :
Code:
def cityCanTrainImmigrant(pCity):
	# Orion's Immigration Mod
	iOwner = pCity.getOwner()
	pPlayer = gc.getPlayer(iOwner)
	ImmigrantTech = gc.getInfoTypeForString("TECH_SAFE_HAVEN")
	
	# Does the Player have the required Tech
	if gc.getTeam(pPlayer.getTeam()).isHasTech(ImmigrantTech):
		# Does the city have a Population of at least 10
		if pCity.getPopulation() > 9:
			#CyInterface().addImmediateMessage("Can Train Immigrant", "")
			return True
			
	return False
	# Orion's Immigration Mod

I still don't speak codese, so please, please provide me with the actual complete code :)

Thanks in advance guys!
 
Hey Lib,

I think the first part could be done by stealing one of the xml attributes for our own purposes. Like <iMechanized> could define the units that do not cost any pop to build.
Otherwise we could probably define a new xml attribute for it, add an appropriate value to the unit class and make sure that this value is read correctly from the xml on load.

Let's say we use the mechanized attribute, then the code could be altered to this (only the last part):
Code:
if not unit.isMech():
			#CyInterface().addImmediateMessage("B", "")
			if pPlayer.isAlive() and not pPlayer.isBarbarian():
				ReducedCityPopulation = pCity.getPopulation() - 2
				pCity.setPopulation(ReducedCityPopulation)
(not tested)

the 2nd part would become:
Code:
def cityCanTrainImmigrant(pCity):
	# Orion's Immigration Mod
	iOwner = pCity.getOwner()
	pPlayer = gc.getPlayer(iOwner)

	if pCity.getPopulation() > 2:
			#CyInterface().addImmediateMessage("Can Train unit", "")
			return True
	return False
 
Top Bottom