DireAussie
Chieftain
- Joined
- Nov 12, 2005
- Messages
- 60
I wrote this code for someone else's mod
If you wanted to implement a leader unit and have all of the civ killed off when that leader is killed then this is the code for you
Put this in your CvEventManager class. Be sure to put in a "self.ignoreKill = false" entry in the init function of your CvEventManager class.
In the below code, units and cities are converted to barbarian when a maceman is killed. Currently no experience or promotions are carried over during unit conversion, but this could be added in.
If you wanted to implement a leader unit and have all of the civ killed off when that leader is killed then this is the code for you

Put this in your CvEventManager class. Be sure to put in a "self.ignoreKill = false" entry in the init function of your CvEventManager class.
In the below code, units and cities are converted to barbarian when a maceman is killed. Currently no experience or promotions are carried over during unit conversion, but this could be added in.
Code:
def onUnitKilled(self, argsList):
#when inside the loop, calling loopUnit.kill(true, player.getID()) will then call this onUnitKilled method, then we get infinite loop. So avoid this by not calling the function when killing units inside the loop
if (self.ignoreKill == false):
self.parent.onUnitKilled(self, argsList)
unitInfo = []
#get info from args given to us
unit, iAttacker = argsList
#get player from unit
player = gc.getPlayer(unit.getOwner())
#get the attacking player (dont really need it for this)
attacker = gc.getPlayer(iAttacker)
if (unit.getUnitType() == gc.getInfoTypeForString("UNIT_MACEMAN") and player.getID() != gc.getBARBARIAN_PLAYER()):
#loop through and store all info about the player's units
(loopUnit, iter) = player.firstUnit(false)
self.ignoreKill = true #ignore onUnitKilled event while in loop
while (loopUnit):
if ( not loopUnit.isNone()):
#store unit info
unitInfo.append((loopUnit.getUnitType(),loopUnit.getX(), loopUnit.getY()) )
#kill unit
loopUnit.kill(true, player.getID())
(loopUnit, iter) = player.nextUnit(iter, false)
CvUtil.pyPrint("did unitloop")
self.ignoreKill = false
#player.killUnits() #this seems to crash the game
#now loop through all cities and convert to barbarian
(loopCity, iter) = player.firstCity(false)
while(loopCity):
if ( not loopCity.isNone()):
#do conversion
gc.getPlayer(gc.getBARBARIAN_PLAYER()).acquireCity(loopCity, false, false)
(loopCity, iter) = player.nextCity(iter, false)
CvUtil.pyPrint("did cityloop")
#now create new units for barbarian
self.ignoreKill = true #not tested but creating a unit on the same square as an enemy unit might kill the other unit
for id, xpos, ypos in unitInfo:
#NOTE- not fully tested - what if player is allied with someone and unit in an ally's city and then a barbarian unit is created?
#CvUtil.pyPrint("creating unit as follows: id %d, xpos %d, ypos %d" % (id, xpos, ypos))
#unitTypeNum = CvUtil.findInfoTypeNum(gc.getUnitInfo, gc.getNumUnitInfos(), "UNIT_MACEMAN")
gc.getPlayer(gc.getBARBARIAN_PLAYER()).initUnit(id, xpos, ypos, UnitAITypes.NO_UNITAI)
CvUtil.pyPrint("did barbunitloop")
self.ignoreKill = false