Modifying some python code help

Lib.Spi't

Overlord of the Wasteland
Joined
Feb 12, 2009
Messages
3,708
Location
UK
Hi guys, I just found this rather tasty bit of python for changing a unit production speed through civics: (in this case execs and missionaries)

Spoiler :
Code:
## Parlimentary Civic Start ##

		iGovernmentCivicOption = CvUtil.findInfoTypeNum(gc.getCivicOptionInfo,gc.getNumCivicOptionInfos(),'CIVICOPTION_GOVERNMENT')
		iParliment = CvUtil.findInfoTypeNum(gc.getCivicInfo,gc.getNumCivicInfos(),'CIVIC_PARLIMENTARY')

		pUnitInfo = gc.getUnitInfo(iUnit)
		pPlayer = gc.getPlayer(iPlayer)

		iExecutive1 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_1')
		iExecutive2 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_2')
		iExecutive3 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_3')
		iExecutive4 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_4')
		iExecutive5 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_5')
		iExecutive6 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_6')
		iExecutive7 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_EXECUTIVE_7')

		iMissionary1 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_JEWISH_MISSIONARY')
		iMissionary2 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_CHRISTIAN_MISSIONARY')
		iMissionary3 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_HINDU_MISSIONARY')
		iMissionary4 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_BUDDHIST_MISSIONARY')
		iMissionary5 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_CONFUCIAN_MISSIONARY')
		iMissionary6 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_TAOIST_MISSIONARY')
		iMissionary7 = CvUtil.findInfoTypeNum(gc.getUnitInfo,gc.getNumUnitInfos(),'UNIT_ISLAMIC_MISSIONARY')

		aiDiplomatList = [iExecutive1, iExecutive2, iExecutive3, iExecutive4, iExecutive5, iExecutive6, iExecutive7, iMissionary1, iMissionary2, iMissionary3, iMissionary4, iMissionary5, iMissionary6, iMissionary7]

		iGovernmentCivic = pPlayer.getCivics(iGovernmentCivicOption)

		if (iGovernmentCivic == iParliment):
			if (iUnit in aiDiplomatList):
				return 80	# 80% of normal cost

## Parlimentary Civic End ##

Is it Possible to change this code so that it does the same thing for units of a specific combat type like COMBAT_MELEE or COMBAT_GUN?

If not I can just reprogram the list to do all the units of this COMBAT type, I was just wondering if I could do it in a more general 'quick way' also so if I add more units they would be auto included.
 
Code:
if pUnitInfo.getUnitCombatType() == UnitCombatTypes.UNITCOMBAT_MELEE

or alternatively:

Code:
def getIndex(category, entry):
    """
    Returns the enumerated index value (integer) of the specified entry belonging to the
    specified category, as specified by the XML.
    """
    key = category.replace(" ", "") + "_" + entry.replace(" ", "_")
    return gc.getInfoTypeForString(key.upper())

def getUnitIndex(unit): return getIndex("Unit", unit)

##keep in mind iUnit is already defined!

dUnitCombat = { getUnitIndex("archer")    : UnitCombatTypes.UNITCOMBAT_ARCHER,
                getUnitIndex("swordsman") : UnitCombatTypes.UNITCOMBAT_MELEE}

if iGovernmentCivic == iParliment and dUnitCombat[iUnit] == UnitCombatTypes.UNITCOMBAT_MELEE:
    return 80 # 80% of normal cost

Hope I helped!
 
He already know how to get the unit info from iUnit.
You can compare getUnitCombatType() directly from CvUnitInfo without the need of CyUnit
 
I looked for a method in CvUnitInfo but I coundn't find one :confused:

in that case just use the top conditional
 
gc.getUnitInfo(iUnit).getUnitCombatType()
 
Back
Top Bottom