Dresden
Emperor
- Joined
- Jul 10, 2008
- Messages
- 1,081
Splitting off from the Foreign Advisor info topic, here's a quick version of the Attitude class proposed by EF that I wrote up today before work. It has been very minimally tested so expect bugginess. However, since EF usually has a dozen suggestions to make it better, I figured I should post early.
Not all of the current AttitudeUtils functions are implemented; for now I wanted to get enough to continue working on the advisor info screen while getting feedback on how to improve this.
You may notice the dump() method. It's something I often do when developing complex data structures to easily peek at the values. Some samples:
Spoiler :
Code:
# Implementing the new Attitude class as discussed in
# http://forums.civfanatics.com/showpost.php?p=7176404&postcount=24
# Build the hardcoded part of the attitude modifier dict now
MODIFIER_KEYS = (
"TXT_KEY_MISC_ATTITUDE_LAND_TARGET",
"TXT_KEY_MISC_ATTITUDE_WAR",
"TXT_KEY_MISC_ATTITUDE_PEACE",
"TXT_KEY_MISC_ATTITUDE_SAME_RELIGION",
"TXT_KEY_MISC_ATTITUDE_DIFFERENT_RELIGION",
"TXT_KEY_MISC_ATTITUDE_BONUS_TRADE",
"TXT_KEY_MISC_ATTITUDE_OPEN_BORDERS",
"TXT_KEY_MISC_ATTITUDE_DEFENSIVE_PACT",
"TXT_KEY_MISC_ATTITUDE_RIVAL_DEFENSIVE_PACT",
"TXT_KEY_MISC_ATTITUDE_RIVAL_VASSAL",
"TXT_KEY_MISC_ATTITUDE_SHARE_WAR",
"TXT_KEY_MISC_ATTITUDE_FAVORITE_CIVIC",
"TXT_KEY_MISC_ATTITUDE_TRADE",
"TXT_KEY_MISC_ATTITUDE_RIVAL_TRADE",
"TXT_KEY_MISC_ATTITUDE_FREEDOM",
"TXT_KEY_MISC_ATTITUDE_EXTRA_GOOD",
"TXT_KEY_MISC_ATTITUDE_EXTRA_BAD",
)
MODIFIER_STRING_TO_KEY = {}
for sKey in MODIFIER_KEYS:
sStr = BugUtil.getPlainText(sKey)
if (sStr):
# These modifier strings contain the '%d: ' prefix so we
# need to extract just the portion in quotes.
pMatch = re.match("^.*(\".+\")", sStr)
if (pMatch):
MODIFIER_STRING_TO_KEY[pMatch.group(1)] = sKey
MEMORY_TYPES_ADDED = False
#BugUtil.debug(str(MODIFIER_STRING_TO_KEY))
# Adding the memory-type modifier keys must be delayed because of
# "unidentifiable C++ exceptions" when this is done on module load.
def addMemoryTypeModifiers():
global MEMORY_TYPES_ADDED
if (not MEMORY_TYPES_ADDED):
for iMemType in range(MemoryTypes.NUM_MEMORY_TYPES):
sKey = str(gc.getMemoryInfo(iMemType).getTextKey())
sStr = BugUtil.getPlainText(sKey)
MODIFIER_STRING_TO_KEY[sStr] = sKey
MEMORY_TYPES_ADDED = True
#BugUtil.debug(str(MODIFIER_STRING_TO_KEY))
class Attitude:
""" Holds summary of attitude that this player has toward target player.
If the two IDs are the same or the two players have not met, the
class will be filled with generic default values. We also check to see
that the active player has met both players, and the accessor functions
will react differently depending on if this is the case. """
def __init__(self, iThisPlayer, iTargetPlayer):
pActiveTeam = gc.getTeam(gc.getActivePlayer().getTeam())
iThisTeam = gc.getPlayer(iThisPlayer).getTeam()
pThisTeam = gc.getTeam(iThisTeam)
iTargetTeam = gc.getPlayer(iTargetPlayer).getTeam()
self.iThisPlayer = iThisPlayer
self.iTargetPlayer = iTargetPlayer
self.iAttitudeSum = 0
self.iAttitudeModifiers = {}
self.bHasActiveMetBoth = CyGame().isDebugMode() or (pActiveTeam.isHasMet(iThisTeam) and pActiveTeam.isHasMet(iTargetTeam))
self.eAttitudeType = AttitudeTypes.NO_ATTITUDE
# This might be better off being something descriptive such as
# "players have not met" or "players are the same"
self.sAttitudeString = ""
if (iThisPlayer != iTargetPlayer and pThisTeam.isHasMet(iTargetTeam)):
self.eAttitudeType = gc.getPlayer(iThisPlayer).AI_getAttitude(iTargetPlayer)
self.sAttitudeString = CyGameTextMgr().getAttitudeString(iThisPlayer, iTargetPlayer)
addMemoryTypeModifiers()
for sLine in self.sAttitudeString.split("\n"):
pMatch = re.match("^.*>([-\+]\d+): (\".+\")<.*$", sLine)
if (pMatch):
iValue = int(pMatch.group(1))
sString = pMatch.group(2)
if sString in MODIFIER_STRING_TO_KEY:
self.iAttitudeModifiers[MODIFIER_STRING_TO_KEY[sString]] = iValue
self.iAttitudeSum = self.iAttitudeSum + iValue
self.dump()
def dump(self):
""" Dumps string version of internal data to debug function """
BugUtil.debug("""Dumping Attitde:
iThisPlayer: %d
iTargetPlayer: %d
iAttitudeSum: %d
eAttitudeType: %d
bHasActiveMetBoth: %s
iAttitudeModifiers: %s """ % (self.iThisPlayer, self.iTargetPlayer, self.iAttitudeSum, self.eAttitudeType,
self.bHasActiveMetBoth, str(self.iAttitudeModifiers)))
def hasModifier(self, sKey):
""" Does the attitude contain given modifier? """
if self.bHasActiveMetBoth:
return (sKey in self.iAttitudeModifiers)
return False
def getModifier(self, sKey):
""" Returns integer value of given attitude modifer. """
if self.bHasActiveMetBoth:
if sKey in self.iAttitudeModifiers:
return self.iAttitudeModifiers[sKey]
return 0
def getTotal(self):
""" Returns total (visible) attitude value. """
if self.bHasActiveMetBoth:
return self.iAttitudeSum
return 0
def getAttitude(self):
""" Returns attitude type. """
if self.bHasActiveMetBoth:
return self.eAttitudeType
return AttitudeTypes.NO_ATTITUDE
def getFullText(self):
""" Returns full diplomacy text string. """
if self.bHasActiveMetBoth:
return self.sAttitudeString
return ""
def getIcon(self):
""" Returns smilie icon string based on attitude type. """
if self.bHasActiveMetBoth:
return unichr(ord(unichr(CyGame().getSymbolID(FontSymbols.POWER_CHAR) + 4)) + self.eAttitudeType)
return ""
Not all of the current AttitudeUtils functions are implemented; for now I wanted to get enough to continue working on the advisor info screen while getting feedback on how to improve this.
You may notice the dump() method. It's something I often do when developing complex data structures to easily peek at the values. Some samples:
Spoiler :
Code:
Dumping Attitde:
iThisPlayer: 8
iTargetPlayer: 6
iAttitudeSum: -11
eAttitudeType: 1
bHasActiveMetBoth: True
iAttitudeModifiers: {'TXT_KEY_MISC_ATTITUDE_LAND_TARGET': -1, 'TXT_KEY_MISC_ATTITUDE_WAR': -3, 'TXT_KEY_MEMORY_SPY_CAUGHT': -2, 'TXT_KEY_MEMORY_DECLARED_WAR_ON_FRIEND': -5}
...
Dumping Attitde:
iThisPlayer: 8
iTargetPlayer: 2
iAttitudeSum: 1
eAttitudeType: 3
bHasActiveMetBoth: True
iAttitudeModifiers: {'TXT_KEY_MISC_ATTITUDE_OPEN_BORDERS': 2, 'TXT_KEY_MISC_ATTITUDE_FAVORITE_CIVIC': 3, 'TXT_KEY_MISC_ATTITUDE_PEACE': 1, 'TXT_KEY_MISC_ATTITUDE_BONUS_TRADE': 1, 'TXT_KEY_MISC_ATTITUDE_RIVAL_TRADE': -4, 'TXT_KEY_MISC_ATTITUDE_RIVAL_VASSAL': -1, 'TXT_KEY_MEMORY_DECLARED_WAR_ON_FRIEND': -1}
...
Dumping Attitde:
iThisPlayer: 1
iTargetPlayer: 0
iAttitudeSum: 19
eAttitudeType: 4
bHasActiveMetBoth: True
iAttitudeModifiers: {'TXT_KEY_MISC_ATTITUDE_DEFENSIVE_PACT': 2, 'TXT_KEY_MISC_ATTITUDE_SAME_RELIGION': 6, 'TXT_KEY_MISC_ATTITUDE_SHARE_WAR': 4, 'TXT_KEY_MISC_ATTITUDE_OPEN_BORDERS': 2, 'TXT_KEY_MEMORY_DECLARED_WAR': -3, 'TXT_KEY_MISC_ATTITUDE_TRADE': 4, 'TXT_KEY_MISC_ATTITUDE_PEACE': 1, 'TXT_KEY_MEMORY_GIVE_HELP': 1, 'TXT_KEY_MEMORY_TRADED_TECH_TO_US': 1, 'TXT_KEY_MEMORY_LIBERATED_CITIES': 1, 'TXT_KEY_MISC_ATTITUDE_BONUS_TRADE': 2, 'TXT_KEY_MEMORY_DECLARED_WAR_ON_FRIEND': -2}