Combat Information

ruff_hi

Live 4ever! Or die trying
Joined
Oct 24, 2005
Messages
9,135
Location
an Aussie in Boston
Hi All - I am trying to work out if my unit is defending or attacking in the 'onCombatResult' part of CvEventManager. I can get the unit name, if the unit is the winner or loser, if the unit is human.

I think it comes down to 'CyGame().getActivePlayer()' - what does this return for a single player game?

Or - any advice or suggestions?
 
So if I test CyGame().getActivePlayer().isHuman() and find it is true then the unit was attacking but if I find it is false then the unit was defending?
 
Ooops - I was wrong. Misread your question.... and my answer was just plain wrong.

CyGame().getActivePlayer() gets the ID, not the pointer. gc.getActivePlayer() gets the pointer not the ID.

IsHuman isn't a very good function to use... because it will only work for humans. I would use:
Code:
 if unit.getOwner() == CyGame().getActivePlayer()
 
The Great Apple said:
Code:
 if unit.getOwner() == CyGame().getActivePlayer()
This will not work either as CyGame().getActivePlayer() is the same if my unit is attacking or defending.

I need to know if my unit is attacking (ie it is the human's turn) or defending (ie it is the machine's turn). I cannot seem to find the right combination of properties to tell me this.

Code:
if (gc.getPlayer(CyGame().getActivePlayer()).isHuman()):
	entry = "While attacking, "
else:
	entry = "While defending, "
I had this, but my message always starts "While attacking". Is there some flag (or similar) that tells me which player's turn it is?

I have this information - pWinner (pointer to who won), pLoser (pointer to who lost). I need to do something like this ...

Code:
if human was active (ie it was the human's turn) then
   message = "While attacking, "
else
   message = "While defending, "

if pwinner.owner = human then
   message = message + pwinner.getname + " beat the @#$ out of " + ploser.getname
else
   message = message + ploser.getname  " had the @#$ beat of him by " + pwinner.getname
 
gc.getActivePlayer() will return the player pointer for the active player... I would have guessed CyGame().getActivePlayer() did the same, but apparently it doesn't.

So;
Code:
if pWinner.getOwner() == gc.getActivePlayer().getID():
would be true if the owner of the winner was active, and if the owner of the winner is active then the winner must be attacking, right?
 
The Great Apple said:
gc.getActivePlayer() will return the player pointer for the active player... I would have guessed CyGame().getActivePlayer() did the same, but apparently it doesn't.
I liked the logic, but (sigh), 'gc.getActivePlayer().getID()' returns the human id all the time.
 
I've been thinking a bit more about this and I'm going to try something like this ...
Code:
def onBeginPlayerTurn(self, argsList):
HumanPlayerTurn = true
...

def onEndPlayerTurn(self, argsList):
HumanPlayerTurn = false
...

... and then test for HumanPlayerTurn when I see who won. However, I don't know how to set a global variable like this - or even if you can set a global variable.
 
ruff_hi said:
I've been thinking a bit more about this and I'm going to try something like this ...
Code:
def onBeginPlayerTurn(self, argsList):
HumanPlayerTurn = true
...

def onEndPlayerTurn(self, argsList):
HumanPlayerTurn = false
...

... and then test for HumanPlayerTurn when I see who won. However, I don't know how to set a global variable like this - or even if you can set a global variable.

That logic will also fail during simotaneous turns (effectivly always reporting as humanplayerturn = false after the first player ends his turn).
 
There must be some way for this to work, since the AI knows who is attacking and who is defending when doling out experience points.
 
I'm really thinking of a single player game and writing information to a log. This information does appear in the game. When you attack, you get a message in green if your unit wins and a message in red if he loses. While defending the message is the same color (green=win) but it starts with "While defending, your blah blah blah"

I'll probably need to test if the player whose turn it is isHuman. Is there a way to echo this stuff back to the screen? Or an inline immediate display where I can query the value of a variable?
 
I worked it out. See here. The basic idea was to set a variable to false during 'onEndPlayerTurn' and set it back to true during the game load or 'onEndGameTurn' then test that variable during combat. If it is true, then the human is attacking, if it is false, then the human is defending. It is unfortunate that the variables that appear to be the player id (see above) are always set at '0' - the human.

I also noticed that iPlayer (arguement from 'onBeginPlayerTurn' and others) does actually return the civ that is 'active'.
 
Perhaps a better way to do it, because it will work for simultanious turns:

set up a variable...
Code:
def __init__(self):

		# Store which unit is attacking
		self.AttackerOwner = -1

store it when combat begins...
Code:
def onCombatLogCalc(self, argsList):
		'Combat Result'	
		genericArgs = argsList[0][0]
		cdAttacker = genericArgs[0]
		cdDefender = genericArgs[1]
		iCombatOdds = genericArgs[2]
		self.AttackerOwner = cdAttacker.eOwner

Use it to determine if the attacker or defender won the combat...
Code:
def onCombatResult(self, argsList):
		'Combat Result'
		pWinner,pLoser = argsList
		playerX = PyPlayer(pWinner.getOwner())
		unitX = PyInfo.UnitInfo(pWinner.getUnitType())
		playerY = PyPlayer(pLoser.getOwner())
		unitY = PyInfo.UnitInfo(pLoser.getUnitType())
                if (self.AttackerOwner == pWinner.getOwner()):

The suggestion to use onCombatLogCalc came from Gerikes in a thread I made asking for help on this topic.

Roger Bacon
 
Back
Top Bottom