Python to find UU for UnitClass - if any

Wesnjean

Chieftain
Joined
May 12, 2006
Messages
6
Location
Lincoln NE - USA
Greets,

I want to have the ability to create new units for a player that builds a wonder - similar to the Pact of the Nilhorn from FFH2. I know how to do what was done in there, but how do you allow for UU's if you are using them? I don't see any call in the API that looks like it will return what I am looking for.
Specifically, if I want to create a new UNITCLASS_WARRIOR, I want the code for UNIT_WARRIOR to be used unless there is a UU for the civilization that has built the wonder.
(I currently use the city built event because it is easy to test with new starts, not sure what final implementation will be.)

Spoiler :

def onCityBuilt(self, argsList):
'City Built'
city = argsList[0]
playerX = PyPlayer(city.getOwner())
pPlayer = gc.getPlayer(city.getOwner())
playerTraits = playerX.getTraitList()
iUnit = playerX.getBestGarrisonUnit()
'CyInterface().addImmediateMessage(("Best Garrison Unit ID = %d" %(iUnit)), "")'
if (city.getOwner() == CyGame().getActivePlayer()):
self.__eventEditCityNameBegin(city, False)
if gc.getInfoTypeForString('TRAIT_SUPREME') in playerTraits:
cityGarrison = pPlayer.initUnit(iUnit, city.getX(), city.getY(), UnitAITypes.NO_UNITAI)
CvUtil.pyPrint('City Built Event: %s' %(city.getName()))


Spoiler :

def getBestGarrisonUnit(self):
eTeam = gc.getTeam(self.player.getTeam())
if eTeam.isHasTech(gc.getInfoTypeForString('TECH_RIFLING')):
return gc.getInfoTypeForString('UNIT_RIFLEMAN')
elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_GUNPOWDER')) and (self.player.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_SULPHUR')) >= 1):
return gc.getInfoTypeForString('UNIT_MUSKETMAN')
elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_FEUDALISM')) and eTeam.isHasTech(gc.getInfoTypeForString('TECH_ARCHERY')):
return gc.getInfoTypeForString('UNIT_LONGBOWMAN')
elif eTeam.isHasTech(gc.getInfoTypeForString('TECH_ARCHERY')):
return gc.getInfoTypeForString('UNIT_ARCHER')
else:
return gc.getInfoTypeForString('UNIT_WARRIOR')


And it looks like I have no idea how to format the code in the spoiler windows so that the indentations are kept. :confused:

But ideally, I would like to return either the base unit ID or the UU unit ID from the call to getBestGarrisonUnit. If you can point me at the most useful API calls, that would be great. Worse case, I can probably try to read the civilization info, looking for the specific UNITCLASS and seeing if there is a UNIT entry in there - I think.

Thanks,
Wes
 
I also scratched my head about this for a little why ... the trick is a member function of CvCivilizationInfo that takes in a UnitClass and returns the UnitType for that class for the civ. Also, to keep indentation you can use
Code:
 brackets instead of spoiler ones ...

[CODE]                workerClass = CvUtil.findInfoTypeNum(gc.getUnitClassInfo,gc.getNumUnitClassInfos(),'UNITCLASS_WORKER')
                iWorker = gc.getCivilizationInfo( newPlayer.getCivilizationType() ).getCivilizationUnits(workerClass)
This code picks normal workers for most, fast workers for India. If you ever need to go from one UnitType to potentially a unique UnitType of the same class, you can get the UnitClass for a unit using:

unitClass = gc.getUnitInfo(unitID).getUnitClassType()
 
That's great. Answers to two questions in one post. Many thanks jdog5000. I'll try it out and see what happens.

And next time, I'll use the code brackets to keep the indentation.

Thanks again,
Wes
 
Back
Top Bottom