Code not working

Jarlaxe Baenre

Emperor
Joined
Feb 17, 2010
Messages
1,959
Location
Calgary, Alberta, Canada
I didn't write this code, and the person who either wrote it or copied it from somewhere and modified it has not responded to a private message.
It is supposed to create a civ with a city which is determined through a random event. When I select the option which results in this happening, however, nothing happens.
Code:
def applyCivilWar(argsList):
	kTriggeredData = argsList[1]
	DeclareWar = True
	dice = gc.getGame().getMapRand()

	lAlive = []
	del lAlive[:]

	iMaxCiv = gc.getMAX_CIV_PLAYERS()
	for i in range(iMaxCiv):
		pPlayer = gc.getPlayer(i)
		lAlive.append(pPlayer.getCivilizationType())
		lAlive.append(-1)

	iAllCivs = gc.getNumPlayableCivilizationInfos()

	pPlayer = gc.getPlayer(kTriggeredData.ePlayer)
	iDerivative = gc.getCivilizationInfo(pPlayer.getCivilizationType()).getDerivativeCiv()
	if not iDerivative in lAlive:
		iNewCiv = iDerivative
	else:
		lNotAlive = []
		del lNotAlive[:]
		for i in range (iAllCivs):
			if i in lAlive:
				continue
			lNotAlive.append(i)

		iTempCiv = dice.get(len(lNotAlive), "Civil War Civilization")
		iNewCiv = lNotAlive[iTempCiv]

	CurCiv = gc.getCivilizationInfo(iNewCiv)
	NumLeaders = CurCiv.getNumLeaders()
	LeaderNum = dice.get(NumLeaders, "Civil War Leader")
	LeaderCounter = 0
	for iLeaders in range(gc.getNumLeaderHeadInfos()):
		if CurCiv.isLeaders(iLeaders):
			if NumLeaders==1:
				NewLeaderID = iLeaders
				break
			else:
				if LeaderCounter == LeaderNum:
					NewLeaderID = iLeaders
					break
		LeaderCounter += 1

	# Find ID for new civ
	iNewID = -1
	for iID in range(iMaxCiv):
		pTestPlayer = gc.getPlayer(iID)
		if not pTestPlayer.isAlive():
			iNewID = iID
			break
		else:
			return false

	# Add the player to the game
	CyGame().addPlayer(iNewID, NewLeaderID, iNewCiv)

	pNewPlayer = gc.getPlayer(iNewID)
	pNewTeam = gc.getTeam(pNewPlayer.getTeam())
	pPlayer = gc.getPlayer(kTriggeredData.ePlayer)
	pCity = pPlayer.getCity(kTriggeredData.iCityId)
	pTriggerTeam = gc.getTeam(pPlayer.getTeam())

	# Give new team open borders tech (for unit handover)
	iMaxTech = gc.getNumTechInfos()
	for OBtech in range(iMaxTech):
		pTech = gc.getTechInfo(OBtech)
		if pTech.isOpenBordersTrading():
			pNewTeam.setHasTech(OBtech, True, iNewID, false, false)
			break
	pNewTeam.signOpenBorders(pTriggerTeam.getID())

	# Remove open borders tech
	pNewTeam.setHasTech(OBtech, false, iNewID, false, false)

	# Add techs to new player
	iMaxTech = gc.getNumTechInfos()
	for counter in range(iMaxTech):
		if (pTriggerTeam.isHasTech(counter) == True) and (pNewTeam.isHasTech(counter) == false):
			pNewTeam.setHasTech(counter, True, iNewID, false, false)

	# Hand over cities
	iX = pCity.getX()
	iY = pCity.getY()
	pNewPlayer.acquireCity(pCity, false, true)

	# Hand over units
	pPlot = CyMap().plot(iX, iY)
	iMaxNumUnits = pPlot.getNumUnits()
	for iUnits in range(iMaxNumUnits, -1, -1):
		pUnit = pPlot.getUnit(iUnits)
		if pUnit.getOwner() == pPlayer.getID():
			pUnit.doCommand(CommandTypes.COMMAND_GIFT, -1, -1)

	for i in range(2):
		pNewPlayer.initUnit(pCity.getConscriptUnit(), iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION)

	# Declare war
	if DeclareWar:
		pNewTeam.declareWar(pTriggerTeam.getID(), false, WarPlanTypes.WARPLAN_LIMITED)

	return True
 
:confused: that's my code, and I haven't got any PM from you.

Whatever.
If you have a savegame right before it, you should turn on Python exceptions (In Documents\My Games\BtS\Civilization4.ini, change
PHP:
; Set to 1 for no python exception popups
HidePythonExceptions = 1
to 0), then try it again and see if any error message comes up.
If so, then please post it here.


Mmhh...under the assumption that the indentation is correctly displayed, then we have a problem here though:
PHP:
	for iID in range(iMaxCiv):
		pTestPlayer = gc.getPlayer(iID)
		if not pTestPlayer.isAlive():
			iNewID = iID
			break
		else:
			return false

The else: and the following line should be one tab back, and not at this position.
 
I actually recognized it, because I remember you PMing it to me back in 2010. However, it was modified by Xyth or someone else because I found it in the bottom of the cvrandomeventinterface.py for History Rewritten and since your code worked, but this didn't, I figured I'd PM the person who likely modified it.


I am getting no error messages, and I have python exceptions enabled. I'm going to leave my house in just 20 minutes, so I'll try moving the else and return false back in about 8 hours.

Thank you.

Edit: Started up Civ, loaded the game...

NewLeaderID referenced before assignment. That's strange because it's referred to right above that. If you look at the code, the problem is with adding the player to the game. Cygame()addplayer and all that. It's in that line that it's telling me the local variable NewLeaderID was referenced before assignment. It's clearly referenced right above that.
 
Code:
	for iLeaders in range(gc.getNumLeaderHeadInfos()):
		if CurCiv.isLeaders(iLeaders):
			if NumLeaders==1:
				NewLeaderID = iLeaders
				break
			else:
				if LeaderCounter == LeaderNum:
					NewLeaderID = iLeaders
					break
		LeaderCounter += 1
Looks suspicious. Indentation wrong.

Assuming there are 3 leaders in that Civ, NumLeaders = 3
LeaderNum will then roll a number 0, 1 or 2 I believe.

Then when the loop starts, whether or not the leader is one of those 3, the leadercounter is increasing.
If the first 3 leaders are not from that civ, then leadercounter would have gone to 4, 5, 6 and so on.

The if NumLeaders==1: statement is never going to be true for this case.
And when leadercounter goes past 3, if LeaderCounter == LeaderNum: is also never true.
Then NewLeaderID is never going to be assigned.

Shift Leadercounter line by 1 tab
 
Code:
			[COLOR="Red"]if NumLeaders==1:
				NewLeaderID = iLeaders
				break
			else:[/COLOR]
				if LeaderCounter == LeaderNum:
					NewLeaderID = iLeaders
					break
Actually the red part looks redundant to me also.
If there is just one leader, LeaderNum is going to be 0 anyway.
Then the if LeaderCounter == LeaderNum is still going to work anyway, when it finds the first and only leader of the civ
 
Back
Top Bottom