[NOOB Python] Why won't they attack damn it!

Discussion in 'Civ4 - SDK/Python' started by Feeder74, Nov 13, 2007.

  1. Feeder74

    Feeder74 Chieftain

    Joined:
    Oct 31, 2007
    Messages:
    57
    I am trying to make a total war situation at 50 turns of play, where everybody is forced to suddenly be at war with one another.
    I have written this into the CvEventManager.py
    Code:
    	def onBeginPlayerTurn(self, argsList):
    		'Called at the beginning of a players turn'
    		iGameTurn, iPlayer = argsList
    		if (iGameTurn==50):
    			team=gc.getTeam(gc.getPlayer(iPlayer).getTeam())
    			if (team.isAlive()):
    				for i in range(gc.getMAX_PLAYERS()):
    					team2=gc.getTeam(gc.getPlayer(i).getTeam())
    					if (i!=iPlayer and team2.isAlive()):
    					team.declareWar(team2,false);	
    It doesn't work, could someone with a bit of python exp tell me what I am doing wrong here?
     
  2. Sto

    Sto Should i code today ...

    Joined:
    Dec 15, 2005
    Messages:
    1,144
    Location:
    Marseille (France)
    There is no tabulation with this test , team2 must be an integer in declareWar , the ";" is a synthax error i think ... you're code doesn't prevent 2 players of the same team to declare war each other (don't know what happen in this case ) . You should enable the debugger to check python error .

    Tcho !
     
  3. Feeder74

    Feeder74 Chieftain

    Joined:
    Oct 31, 2007
    Messages:
    57
    Well I changed it to this...
    Code:
    		if (iGameTurn==50):
    			team=gc.getTeam(gc.getPlayer(iPlayer).getTeam())
    			if (team.isAlive()):
    				for i in range(gc.getMAX_PLAYERS()):
    					team2=gc.getTeam(gc.getPlayer(i).getTeam())
    					if (i!=iPlayer and team2.isAlive()):
    						team.declareWar(i,false)	
    and it still isn't working for me, but thanks sto, on the note of poor indenting and syntax, is there a good IDE somewhere for this stuff, I am just using notepad at the moment.
    I have Code::Blocks already installed as of last night, but since I can't get the SDK to compile, I am wondering now if it can be configured to do python, anyhow will have a look straight after this ;)
    I have the debugger enabled - just can't follow the error message it spit out last time - I will have a look again in a few minutes to find out what it says this time :)

    Well I had a good look through the error messages, looks 1,688KB of this message to me...
    Code:
    Traceback (most recent call last):
    
      File "CvEventInterface", line 23, in onEvent
    
      File "CvEventManager", line 164, in handleEvent
    
      File "CvEventManager", line 464, in onUnitMove
    
    ValueError: too many values to unpack
    ERR: Python function onEvent failed, module CvEventInterface
    ...anyhow this means diddly to me, as it isn't even refering to the edits I made :(
     
  4. Sto

    Sto Should i code today ...

    Joined:
    Dec 15, 2005
    Messages:
    1,144
    Location:
    Marseille (France)
    Here the python tutorial where you can find python an how to debug ( if not just search python with google ) . This is a strange bug , you have made no change in onUnitMove ? and what is the line where you get an error ? . Notepad is not a good editor because it can cause some tab problems ( a mix of spaces and tab ) . Anyway , the code will not work in a game with a team . You should make a loop with teams and not players (perhaps place this function in onBeginGameTurn ) . You should check if the team canDeclareWar also .

    If you have some problems , do not hesitate to post your file with all the changes you've made in it , the version you play .

    Tcho !
     
  5. PeteT

    PeteT Warlord

    Joined:
    Jan 2, 2002
    Messages:
    273
    Location:
    Winterpeg
    This can't be right:

    Code:
    team2=gc.getTeam(gc.getPlayer(i).getTeam())
    
     
  6. Feeder74

    Feeder74 Chieftain

    Joined:
    Oct 31, 2007
    Messages:
    57
    thanks for the link to the tut - I hadn't seen that one - and it is very helpfull for where I am at ;)
    As for the 1,688KB of error messages I mentioned above - I think this may hve been caused by using Vanilla file instead of BTS3.13

    Anyhow after some mucking around I ended up with this...
    Code:
    	def onBeginGameTurn(self, argsList):
    		'Called at the beginning of the end of each turn'
    		iGameTurn = argsList[0]
    		CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
    		#***Start of edit!***
    		if (iGameTurn==50):
    			for iPlayerLoop in range(gc.getMAX_PLAYERS()):
    				pPlayer=gc.getPlayer(iPlayerLoop)
    				for iPlayerLoop2 in range(gc.getMAX_PLAYERS()):
    					pPlayer2=gc.getPlayer(iPlayerLoop2)
    					if (pPlayer.getTeam()!=pPlayer2.getTeam()):
    						gc.getTeam(iPlayerLoop).declareWar(gc.getPlayer(iPlayerLoop2).getTeam(),false,0)	
    it does what was intended, and I'd just like to say thanks heaps for getting me started with this - I sometimes find it hard to come to a new API and get the layout of it the first few time, hopefully next time, I might have something a bit more elaborate for you to wrap your minds around ;)
    PS: If you see anyways of optimizing that last line of code - I'd love to hear it - to me it seems - well ugly, and that isn't good, and prolly isn't bad either since it does do what it was meant too, but I like my stuff to look nice too ;)
     

Share This Page