Starting a thread here to continue a discussion in a couple of other threads - how to limit the number of units built by the number of strategic resources available.
Baldyr and God Emperor posted this code which I imagine could be adapted:
I had a browse through the Python API for useful functions and found this one:
• CyCity.getNumBonuses (BonusType iBonus)
I'm not sure if this is just resources in city radius or via the network too. CyCity.hasBonus (BonusType iBonus) seems to be via the network so hopefully its counterpart is too. When I get some decent time I'll have a go at rewriting God Emperor's code.
It would be nice though to not have to maintain a 'list' in the code though so what I'm wondering is whether it's possible in Python to effectively 'look up' a unit's xml file for the necessary information - in this case <BonusType>, <PrereqBonuses> and <bPrereqBonuses>. I had a brief search through the Python API but didn't find anything obvious. Is something like this possible?
Baldyr and God Emperor posted this code which I imagine could be adapted:
Code:
def canTrain(self,argsList):
pCity = argsList[0]
eUnit = argsList[1]
bContinue = argsList[2]
bTestVisible = argsList[3]
bIgnoreCost = argsList[4]
bIgnoreUpgrades = argsList[5]
#city cap code with variable limits:
iDefault = 3
dLimits = { gc.getInfoTypeForString('UNITCLASS_ARCHER') : 4,
gc.getInfoTypeForString('UNITCLASS_CAVALRY') : 0.3334
} # add things to this dictionary that should not use the default value
eUnitClass = gc.getUnitInfo(eUnit).getUnitClassType()
pOwner = gc.getPlayer(pCity.getOwner())
iNumUnits = pOwner.getUnitClassCountPlusMaking(eUnitClass)
iNumCities = pOwner.getNumCities()
if eUnitClass in dLimits :
fUnitLimit = dLimits[eUnitClass]
else:
fUnitLimit = iDefault
iLimit = int(iNumCities * fUnitLimit)
return iNumUnits < iLimit
I had a browse through the Python API for useful functions and found this one:
• CyCity.getNumBonuses (BonusType iBonus)
I'm not sure if this is just resources in city radius or via the network too. CyCity.hasBonus (BonusType iBonus) seems to be via the network so hopefully its counterpart is too. When I get some decent time I'll have a go at rewriting God Emperor's code.
It would be nice though to not have to maintain a 'list' in the code though so what I'm wondering is whether it's possible in Python to effectively 'look up' a unit's xml file for the necessary information - in this case <BonusType>, <PrereqBonuses> and <bPrereqBonuses>. I had a brief search through the Python API but didn't find anything obvious. Is something like this possible?