player.initUnit, with trait effects?

davidlallen

Deity
Joined
Apr 28, 2008
Messages
4,743
Location
California
In Dune Wars, we have added a screen which allows you to buy reinforcements. It is not a full-up mercenary system. The idea is that your civ has an offworld capitol, and if you pay the local space ship owners (the navigator's guild), you can bring in reinforcements from your homeworld. You can see the work in progress at this post, screenshot at this post.

When the new units appear, it is via python player.initUnit(). A playtester has reported a bug, which is, units purchased this way do not have the starting promotions which should be granted by their leaderhead's traits. For example, archers bought by a leader with the Protective trait should start with Drill, etc.

My question is, how can I tell what promotions are supposed to be added to new units? I do not want to hard code assumptions about what Aggressive does, what Protective does, etc. Is there a function like AddInitialPromotions()?

If I have to roll my own, how would I go about it? It looks like CvTraitInfo.isFreePromotion() could help, if I looped over all promotions, but I do not see how to get the traits of the current player.
 
I had a similar problem in Colonization, maybe it help:

Code:
for i in range(gc.getNumTraitInfos()):
	if player.hasTrait(i):
		if (gc.getTraitInfo(i).getEuropeTravelTimeModifier() != 0):
			....

Replace the last line by cycling through each promotion and make an isPromotion check:

Code:
for i in range(gc.getNumTraitInfos()):
	if player.hasTrait(i):
	       for k in range(gc.getNumPromotionInfos()):
                        if (gc.getTraitInfo(i).isPromotion(k)):
			    ....

Next test if unit can have that promotion and add it with CyUnit.promote(...)
 
Don't forget to first check that the promotions apply to the combat type of the unit:

Code:
[B]iCombatType = ...[/B]
for i in range(gc.getNumTraitInfos()):
    if player.hasTrait(i):
        [B]traitInfo = gc.getTraitInfo(i)
        if traitInfo.isFreePromotionUnitCombat(iCombatType):[/B]
            for k in range(gc.getNumPromotionInfos()):
                if traitInfo.isPromotion(k):
                    ...

Also, you want to use setHasPromotion() instead of promote() as it adds the promotion without changing the unit's level.
 
You might find it easier (now that you are into the DLL) to set up a function which applies build effects for you special in CvCity. It is a solution Kael implemented for much the same kind of concern, and will allow you to not only apply Promotions from traits, but also any special bonuses which the city might offer through buildings, or the player should also have through civics or other aspects of life.


To do that, you would go to CvCity::popOrder and comment out everything which applies bonuses to the unit in this function and instead call a new function where you will paste back in those same lines you commented out, then expose it to python so you can update a unit with a single line.
 
Top Bottom