LPlate2
Warlord
- Joined
- Dec 27, 2018
- Messages
- 299
Hi, Can someone tell me what is wrong with this code? The idea is that commanded units are going to follow their leader when he leads them in a charge, i.e. they immediately attack after him. The forced attack is working but the weakest unit is attacking first, rather than the strongest.
The messages I've included in the code show that the order of the pAttacker units in my trial is: Champion, Axeman and then Warrior but the combats are resolved in the order Warrior, Axeman, Champion. How do I fix this or what am I missing?
Edit: I managed to fix this issue. I moved the code that triggers the python from resolveCombat to updateCombat in CvUnit.cpp.
The messages I've included in the code show that the order of the pAttacker units in my trial is: Champion, Axeman and then Warrior but the combats are resolved in the order Warrior, Axeman, Champion. How do I fix this or what am I missing?
Spoiler :
Code:
def doLeadCharge(argsList):
caster, pTargetPlot = argsList
iAPlayer = caster.getOwner()
pAPlayer = gc.getPlayer(iAPlayer)
pPlot = caster.plot()
iX = pTargetPlot.getX()
iY = pTargetPlot.getY()
iPlayer = caster.getOwner()
pPlayer = gc.getPlayer(iPlayer)
iX = pPlot.getX()
iY = pPlot.getY()
eTeam = pPlayer.getTeam()
iAcceptableDamage = 25 # Don't want to force massively injured units to attack.
iMod = caster.getModifier() + caster.getLeadershipModifier()
lAttackers = []
lStr = []
for i in range(pPlot.getNumUnits()):
pLoopUnit = pPlot.getUnit(i)
if (caster.getOwner() == pLoopUnit.getOwner() and pLoopUnit.getCommandingUnit() == caster.getID()):
if pLoopUnit.movesLeft() > 0 and pLoopUnit.canAttack():
if not pLoopUnit.isMadeAttack() or pLoopUnit.isBlitz():
if pLoopUnit.getDamage() < iAcceptableDamage:
if pLoopUnit.canMoveOrAttackInto(pTargetPlot, False):
iLoopStr = (pLoopUnit.baseCombatStr() * (100 + pLoopUnit.getExtraCombatPercent()))
iLoopStr = (pLoopUnit.getExperience() * 15 + iLoopStr)
lAttackers.append(pLoopUnit)
lStr.append(iLoopStr)
lStr.sort()
lStr.reverse()
if len(lAttackers) > 0:
for k in range (len(lStr)):
# for k in range (len(lStr)-1, -1,-1): Alternative, if did not use lStr.reverse()
iStr = lStr[k]
CyInterface().addMessage(CyGame().getActivePlayer(),True,10,"iStr is %d" %(iStr),'',0,'',ColorTypes(8),-1,-1,True,True)
for m in range (len(lAttackers)):
pAttacker = lAttackers[m]
iAttackerStr = (pAttacker.baseCombatStr() * (100 + pAttacker.getExtraCombatPercent()) + pAttacker.getExperience() * 15)
sDesc = pAttacker.getName()
CyInterface().addMessage(CyGame().getActivePlayer(),True,10,"Attacking unit is %s" %(sDesc),'',0,'',ColorTypes(8),-1,-1,True,True)
CyInterface().addMessage(CyGame().getActivePlayer(),True,10,"iAttackerStr is %d" %(iAttackerStr),'',0,'',ColorTypes(8),-1,-1,True,True)
if iStr == iAttackerStr:
pAttacker.setFollowingCharge(True)
iCharging = pAttacker.getCharging()
pAttacker.changeExtraCombatPercent(iCharging)
pAttacker.attack(pTargetPlot, False)
pAttacker.changeExtraCombatPercent(-iCharging)
pAttacker.setFollowingCharge(False)
lAttackers.remove(pAttacker)
break
caster.setHasPromotion(gc.getInfoTypeForString('PROMOTION_LEADING_CHARGE'), False)
# Idea is that all of the able commanded units will follow the leader onto the attacked tile
Edit: I managed to fix this issue. I moved the code that triggers the python from resolveCombat to updateCombat in CvUnit.cpp.
Last edited: