Pyhton help needed - Comparing techs

kipkuhmi

Warlord
Joined
Aug 8, 2007
Messages
131
Hi everybody,

I want Python to check if the Player's civilization already owns the technology that a given other faction is currently researching.

Does anyone know how to write the if-condition for that?

Thanks for any help!
 
The existing code makes for great examples. The ExoticForeignAdvisor has code that does much the same thing, finding techs that you have that they don't and vice versa.

Here's a function that should tell you if one player ("the Player's civilization") has the tech that another player ("faction") is researching.

Code:
gc = CyGlobalContext()
def isHasTechOtherIsResearching(ePlayer, eOtherPlayer):
	player = gc.getPlayer(ePlayer)
	otherPlayer = gc.getPlayer(eOtherPlayer)
	eTech = otherPlayer.getCurrentResearch()
	if eTech != -1:
		team = gc.getTeam(player.getTeam())
		return team.isHasTech(eTech)
	else:
		return False

Note that this code doesn't check that the players are valid or alive. If eOtherPlayer doesn't have a tech in their research queue, the function returns False.
 
Wow, that was a quick response! :) Thanks a lot!

However, it still doesn't work ... I feel I'm missing out something incredibly trivial here.

The py-file I'm working with is "CvMainInterface.py".
The effect should be that the interface only shows you the tech of another player if you yourself already have discovered it. In the other case it just says "future technology".

All the rest is working fine, there's just something wrong with that if-clause

Code:
checkVar = self.isHasTechOtherIsResearching(gc.getGame().getActivePlayer(), ePlayer)
if (checkVar == true):
    szTempBuffer = u"-" "%s (%d)" %(gc.getTechInfo(gc.getPlayer(ePlayer).getCurrentResearch()).getDescription(), gc.getPlayer(ePlayer).getResearchTurnsLeft(gc.getPlayer(ePlayer).getCurrentResearch(), True))
else:
    szTempBuffer = u"-" "%s (%d)" %(gc.getTechInfo(gc.getInfoTypeForString("TECH_FUTURE_TECH")).getDescription() , gc.getPlayer(ePlayer).getResearchTurnsLeft(gc.getPlayer(ePlayer).getCurrentResearch(), True))
                                                    szBuffer = szBuffer + szTempBuffer

[. . .]

def isHasTechOtherIsResearching(ePlayer, eOtherPlayer):
    return true
    player = gc.getPlayer(ePlayer)
    otherPlayer = gc.getPlayer(eOtherPlayer)
    eTech = otherPlayer.getCurrentResearch()
    if eTech != -1:
        team = gc.getTeam(player.getTeam())
        return team.isHasTech(eTech)
    else:
        return false

I suspect there's sth wrong with the player vars gc.getGame().getActivePlayer() (Active Player) and ePlayer (the other player). However, that's how those two players are adressed earlier in the code, so it should be correct.

?!?
 
You need to add "self, " as the first parameter. Is its "def" at the same level of indentation of the other functions?

For one thing you have the first line of the function as "return true" which should result in the normal display of techs the AI is researching whether or not you know it already.

Your method of calling the function and it's parameters (the player IDs) looks correct.

To help figure out future problems, turn on the logging system in CivilizationIV.ini:

Code:
; Enable the logging system
LoggingEnabled = [B]1[/B]

Then remove the "return true" line and try again. Then post the contents of Logs/PythonErr.log and I'll take a look if you can't find the problem.

BTW, True and False are the correct Python boolean literals. Civ4 defines true (0) and false (1), but I recommend sticking with T/F. Also, you can simply say "if checkVar:" as that compares it to True.

And you can ditch the var completely:

Code:
if self.isHasTechOtherIsResearching(...):
    szTempBuffer = ...
else:
    szTempBuffer = ...
 
Cool, it works!

:):):):):):):)

The "self, " argument was the golden key.
Thanks a lot, I don't know how long it would have taken to figure that out by myself!
 
The error in the PythonErr.log file would have been something like this:

Code:
... CvMainInterface.py    xxxx (the line number with the call to the function)

Wrong number of arguments
isHasTechOtherIsResearching takes exactly 2 arguments 3 given.

Eventually you'd compare the definition to the definitions of the other functions and catch "self," as the first argument to them all. :)
 
Top Bottom