## Sid Meier's Civilization 4
## Copyright Firaxis Games 2006
##
## CvEventManager
## This class is passed an argsList from CvAppInterface.onEvent
## The argsList can contain anything from mouse location to key info
## The EVENTLIST that are being notified can be found


from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import CvDebugTools
import CvWBPopups
import PyHelpers
import Popup as PyPopup
import CvCameraControls
import CvTopCivs
import sys
import CvWorldBuilderScreen
import CvAdvisorUtils
import CvTechChooser
import os.path
import BugPath
import codecs

# Updater Mod
import CvModUpdaterScreen
# Updater Mod END

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

#PBMod start
iPlayerOptionCheck = 0  # Triggers for == 1, decrements for >= 0
#PBMod end

# globals
###################################################
class CvEventManager:
    def __init__(self):
        self.bGameTurnProcessing = False
        self.pbServerChatMessage = ""
        #################### ON EVENT MAP ######################
        #print "EVENTMANAGER INIT"

        self.bCtrl = False
        self.bShift = False
        self.bAlt = False
        self.bAllowCheats = False

        # OnEvent Enums
        self.EventLButtonDown=1
        self.EventLcButtonDblClick=2
        self.EventRButtonDown=3
        self.EventBack=4
        self.EventForward=5
        self.EventKeyDown=6
        self.EventKeyUp=7

        self.__LOG_MOVEMENT = 0
        self.__LOG_BUILDING = 0
        self.__LOG_COMBAT = 0
        self.__LOG_CONTACT = 0
        self.__LOG_IMPROVEMENT =0
        self.__LOG_CITYLOST = 0
        self.__LOG_CITYBUILDING = 0
        self.__LOG_TECH = 0
        self.__LOG_UNITBUILD = 0
        self.__LOG_UNITKILLED = 1
        self.__LOG_UNITLOST = 0
        self.__LOG_UNITPROMOTED = 0
        self.__LOG_UNITSELECTED = 0
        self.__LOG_UNITPILLAGE = 0
        self.__LOG_GOODYRECEIVED = 0
        self.__LOG_GREATPERSON = 0
        self.__LOG_RELIGION = 0
        self.__LOG_RELIGIONSPREAD = 0
        self.__LOG_GOLDENAGE = 0
        self.__LOG_ENDGOLDENAGE = 0
        self.__LOG_WARPEACE = 0
        self.__LOG_PUSH_MISSION = 0

        ## EVENTLIST
        self.EventHandlerMap = {
            'mouseEvent'            : self.onMouseEvent,
            'kbdEvent'              : self.onKbdEvent,
            'ModNetMessage'                 : self.onModNetMessage,
            'Init'                  : self.onInit,
            'Update'                : self.onUpdate,
            'UnInit'                : self.onUnInit,
            'OnSave'                : self.onSaveGame,
            'OnPreSave'             : self.onPreSave,
            'OnLoad'                : self.onLoadGame,
            'GameStart'             : self.onGameStart,
            'GameEnd'               : self.onGameEnd,
            'plotRevealed'          : self.onPlotRevealed,
            'plotFeatureRemoved'    : self.onPlotFeatureRemoved,
            'plotPicked'            : self.onPlotPicked,
            'nukeExplosion'         : self.onNukeExplosion,
            'gotoPlotSet'           : self.onGotoPlotSet,
            'BeginGameTurn'         : self.onBeginGameTurn,
            'EndGameTurn'           : self.onEndGameTurn,
            'BeginPlayerTurn'       : self.onBeginPlayerTurn,
            'EndPlayerTurn'         : self.onEndPlayerTurn,
            'endTurnReady'          : self.onEndTurnReady,
            'combatResult'          : self.onCombatResult,
          'combatLogCalc'           : self.onCombatLogCalc,
          'combatLogHit'                : self.onCombatLogHit,
            'improvementBuilt'      : self.onImprovementBuilt,
            'improvementDestroyed'      : self.onImprovementDestroyed,
            'routeBuilt'        : self.onRouteBuilt,
            'firstContact'          : self.onFirstContact,
            'cityBuilt'             : self.onCityBuilt,
            'cityRazed'             : self.onCityRazed,
            'cityAcquired'          : self.onCityAcquired,
            'cityAcquiredAndKept'   : self.onCityAcquiredAndKept,
            'cityLost'              : self.onCityLost,
            'cultureExpansion'      : self.onCultureExpansion,
            'cityGrowth'            : self.onCityGrowth,
            'cityDoTurn'            : self.onCityDoTurn,
            'cityBuildingUnit'  : self.onCityBuildingUnit,
            'cityBuildingBuilding'  : self.onCityBuildingBuilding,
            'cityRename'                : self.onCityRename,
            'cityHurry'             : self.onCityHurry,
            'selectionGroupPushMission'     : self.onSelectionGroupPushMission,
            'unitMove'              : self.onUnitMove,
            'unitSetXY'             : self.onUnitSetXY,
            'unitCreated'           : self.onUnitCreated,
            'unitBuilt'             : self.onUnitBuilt,
            'unitKilled'            : self.onUnitKilled,
            'unitLost'              : self.onUnitLost,
            'unitPromoted'          : self.onUnitPromoted,
            'unitSelected'          : self.onUnitSelected,
            'UnitRename'                : self.onUnitRename,
            'unitPillage'               : self.onUnitPillage,
            'unitSpreadReligionAttempt' : self.onUnitSpreadReligionAttempt,
            'unitGifted'                : self.onUnitGifted,
            'unitBuildImprovement'              : self.onUnitBuildImprovement,
            'goodyReceived'         : self.onGoodyReceived,
            'greatPersonBorn'       : self.onGreatPersonBorn,
            'buildingBuilt'         : self.onBuildingBuilt,
            'projectBuilt'          : self.onProjectBuilt,
            'techAcquired'          : self.onTechAcquired,
            'techSelected'          : self.onTechSelected,
            'religionFounded'       : self.onReligionFounded,
            'religionSpread'        : self.onReligionSpread,
            'religionRemove'        : self.onReligionRemove,
            'corporationFounded'    : self.onCorporationFounded,
            'corporationSpread'     : self.onCorporationSpread,
            'corporationRemove'     : self.onCorporationRemove,
            'goldenAge'             : self.onGoldenAge,
            'endGoldenAge'          : self.onEndGoldenAge,
            'chat'                  : self.onChat,
            'victory'               : self.onVictory,
            'vassalState'           : self.onVassalState,
            'changeWar'             : self.onChangeWar,
            'setPlayerAlive'        : self.onSetPlayerAlive,
            'playerChangeStateReligion'     : self.onPlayerChangeStateReligion,
            'playerGoldTrade'       : self.onPlayerGoldTrade,
            'windowActivation'      : self.onWindowActivation,
            'gameUpdate'            : self.onGameUpdate,        # sample generic event
        }

        ################## Events List ###############################
        #
        # Dictionary of Events, indexed by EventID (also used at popup context id)
        #   entries have name, beginFunction, applyFunction [, randomization weight...]
        #
        # Normal events first, random events after
        #
        ################## Events List ###############################
        self.Events={
            CvUtil.EventEditCityName : ('EditCityName', self.__eventEditCityNameApply, self.__eventEditCityNameBegin),
            CvUtil.EventEditCity : ('EditCity', self.__eventEditCityApply, self.__eventEditCityBegin),
            CvUtil.EventPlaceObject : ('PlaceObject', self.__eventPlaceObjectApply, self.__eventPlaceObjectBegin),
            CvUtil.EventAwardTechsAndGold: ('AwardTechsAndGold', self.__eventAwardTechsAndGoldApply, self.__eventAwardTechsAndGoldBegin),
            CvUtil.EventEditUnitName : ('EditUnitName', self.__eventEditUnitNameApply, self.__eventEditUnitNameBegin),
            CvUtil.EventWBAllPlotsPopup : ('WBAllPlotsPopup', self.__eventWBAllPlotsPopupApply, self.__eventWBAllPlotsPopupBegin),
            CvUtil.EventWBLandmarkPopup : ('WBLandmarkPopup', self.__eventWBLandmarkPopupApply, self.__eventWBLandmarkPopupBegin),
            CvUtil.EventWBScriptPopup : ('WBScriptPopup', self.__eventWBScriptPopupApply, self.__eventWBScriptPopupBegin),
            CvUtil.EventWBStartYearPopup : ('WBStartYearPopup', self.__eventWBStartYearPopupApply, self.__eventWBStartYearPopupBegin),
            CvUtil.EventShowWonder: ('ShowWonder', self.__eventShowWonderApply, self.__eventShowWonderBegin),
        }
#################### EVENT STARTERS ######################
    def handleEvent(self, argsList):
        'EventMgr entry point'
        # extract the last 6 args in the list, the first arg has already been consumed
        self.origArgsList = argsList    # point to original
        tag = argsList[0]               # event type string
        idx = len(argsList)-6
        bDummy = false
        self.bDbg, bDummy, self.bAlt, self.bCtrl, self.bShift, self.bAllowCheats = argsList[idx:]
        ret = 0
        if self.EventHandlerMap.has_key(tag):
            fxn = self.EventHandlerMap[tag]
            ret = fxn(argsList[1:idx])
        return ret

#################### EVENT APPLY ######################
    def beginEvent( self, context, argsList=-1 ):
        'Begin Event'
        entry = self.Events[context]
        return entry[2]( argsList )

    def applyEvent( self, argsList ):
        'Apply the effects of an event '
        context, playerID, netUserData, popupReturn = argsList

        if context == CvUtil.PopupTypeEffectViewer:
            return CvDebugTools.g_CvDebugTools.applyEffectViewer( playerID, netUserData, popupReturn )

        entry = self.Events[context]

        if ( context not in CvUtil.SilentEvents ):
            self.reportEvent(entry, context, (playerID, netUserData, popupReturn) )
        return entry[1]( playerID, netUserData, popupReturn )   # the apply function

    def reportEvent(self, entry, context, argsList):
        'Report an Event to Events.log '
        if (gc.getGame().getActivePlayer() != -1):
            message = "DEBUG Event: %s (%s)" %(entry[0], gc.getActivePlayer().getName())
            CyInterface().addImmediateMessage(message,"")
            CvUtil.pyPrint(message)
        return 0

#################### ON EVENTS ######################
    def onKbdEvent(self, argsList):
        'keypress handler - return 1 if the event was consumed'

        eventType,key,mx,my,px,py = argsList
        game = gc.getGame()

        if (self.bAllowCheats):
            # notify debug tools of input to allow it to override the control
            argsList = (eventType,key,self.bCtrl,self.bShift,self.bAlt,mx,my,px,py,gc.getGame().isNetworkMultiPlayer())
            if ( CvDebugTools.g_CvDebugTools.notifyInput(argsList) ):
                return 0

        if ( eventType == self.EventKeyDown ):
            theKey=int(key)

            CvCameraControls.g_CameraControls.handleInput( theKey )

            if (self.bAllowCheats):
                # Shift - T (Debug - No MP)
                if (theKey == int(InputTypes.KB_T)):
                    if ( self.bShift ):
                        self.beginEvent(CvUtil.EventAwardTechsAndGold)
                        #self.beginEvent(CvUtil.EventCameraControlPopup)
                        return 1

                elif (theKey == int(InputTypes.KB_W)):
                    if ( self.bShift and self.bCtrl):
                        self.beginEvent(CvUtil.EventShowWonder)
                        return 1

                # Shift - ] (Debug - currently mouse-overd unit, health += 10
                elif (theKey == int(InputTypes.KB_LBRACKET) and self.bShift ):
                    unit = CyMap().plot(px, py).getUnit(0)
                    if ( not unit.isNone() ):
                        d = min( unit.maxHitPoints()-1, unit.getDamage() + 10 )
                        unit.setDamage( d, PlayerTypes.NO_PLAYER )

                # Shift - [ (Debug - currently mouse-overd unit, health -= 10
                elif (theKey == int(InputTypes.KB_RBRACKET) and self.bShift ):
                    unit = CyMap().plot(px, py).getUnit(0)
                    if ( not unit.isNone() ):
                        d = max( 0, unit.getDamage() - 10 )
                        unit.setDamage( d, PlayerTypes.NO_PLAYER )

                elif (theKey == int(InputTypes.KB_F1)):
                    if ( self.bShift ):
                        CvScreensInterface.replayScreen.showScreen(False)
                        return 1
                    # don't return 1 unless you want the input consumed

                elif (theKey == int(InputTypes.KB_F2)):
                    if ( self.bShift ):
                        import CvDebugInfoScreen
                        CvScreensInterface.showDebugInfoScreen()
                        return 1

                elif (theKey == int(InputTypes.KB_F3)):
                    if ( self.bShift ):
                        CvScreensInterface.showDanQuayleScreen(())
                        return 1

                elif (theKey == int(InputTypes.KB_F4)):
                    if ( self.bShift ):
                        CvScreensInterface.showUnVictoryScreen(())
                        return 1

        return 0

    def onModNetMessage(self, argsList):
        'Called whenever CyMessageControl().sendModNetMessage() is called - this is all for you modders!'
        iData1, iData2, iData3, iData4, iData5 = argsList
        CvUtil.pyPrint( 'onModNetMessage' )

        # PBMod, assemble chat message.
        chatFlags = (iData5 >> 24) & 0x7F
        if (chatFlags & 0x70) == 0x70:
            try:
                # Convert sign flags back into 32th bit
                if iData1 < 0: iData1 = -iData1 | 0x80000000
                if iData2 < 0: iData2 = -iData2 | 0x80000000
                if iData3 < 0: iData3 = -iData3 | 0x80000000
                if iData4 < 0: iData4 = -iData4 | 0x80000000

                if (chatFlags & 0x04) == 0x04:
                    # Begin of new message
                    self.pbServerChatMessage = ""

                if iData1 != 0:
                    self.pbServerChatMessage += "%c%c%c%c" %(
                        chr((iData1 >> 0) & 0xFF),
                        chr((iData1 >> 8) & 0xFF),
                        chr((iData1 >> 16) & 0xFF),
                        chr((iData1 >> 24) & 0xFF))
                if iData2 != 0:
                    self.pbServerChatMessage += "%c%c%c%c" %(
                        chr((iData2 >> 0) & 0xFF),
                        chr((iData2 >> 8) & 0xFF),
                        chr((iData2 >> 16) & 0xFF),
                        chr((iData2 >> 24) & 0xFF))
                if iData3 != 0:
                    self.pbServerChatMessage += "%c%c%c%c" %(
                        chr((iData3 >> 0) & 0xFF),
                        chr((iData3 >> 8) & 0xFF),
                        chr((iData3 >> 16) & 0xFF),
                        chr((iData3 >> 24) & 0xFF))
                if iData4 != 0:
                    self.pbServerChatMessage += "%c%c%c%c" %(
                        chr((iData4 >> 0) & 0xFF),
                        chr((iData4 >> 8) & 0xFF),
                        chr((iData4 >> 16) & 0xFF),
                        chr((iData4 >> 24) & 0xFF))
                if (iData5 & 0x00FFFFFF) != 0:
                    self.pbServerChatMessage += "%c%c%c" %(
                        chr((iData5 >> 0) & 0xFF),
                        chr((iData5 >> 8) & 0xFF),
                        chr((iData5 >> 16) & 0xFF))

                if (chatFlags & 0x08) == 0x08:
                    # End of message
                    msg_u = self.pbServerChatMessage.rstrip().decode('utf-8')
                    # CvUtil.pyPrint('Final chat message: ' + msg_u)
                    self.pbServerChatMessage = ""

                    sounds = ["AS2D_CHAT",
                              "AS2D_PING",
                              "AS2D_WELOVEKING",  # ^^
                              "AS2D_DECLAREWAR"]
                    sound = sounds[chatFlags & 0x03]

                    if not CyGame().isPitbossHost():
                        # Use unicode msg_u with cp1252 charset
                        sColor = localText.getText("[COLOR_WARNING_TEXT]", ())
                        color_msg = u"%sPitboss:</color> %s" % (sColor, msg_u)
                        CyInterface().addImmediateMessage(color_msg, sound)

            except Exception, e:
                CvUtil.pyPrint("Chat message decoding failed. Error: %s" % (e,))

    def onInit(self, argsList):
        'Called when Civ starts up'
        CvUtil.pyPrint( 'OnInit' )

    # Note: Callback not called in this mod...
    def onUpdate(self, argsList):
        'Called every frame'
        fDeltaTime = argsList[0]

        # allow camera to be updated
        CvCameraControls.g_CameraControls.onUpdate( fDeltaTime )


    def onWindowActivation(self, argsList):
        'Called when the game window activates or deactivates'
        bActive = argsList[0]

        # Updater Mod
        if not hasattr(CvScreensInterface, "showModUpdaterScreen"):
            CvModUpdaterScreen.integrate()

        # Show ModUpdater screen after Window switch
        if bActive:
            CvScreensInterface.showModUpdaterScreen(True)
        # Updater Mod END

    def onUnInit(self, argsList):
        'Called when Civ shuts down'
        CvUtil.pyPrint('OnUnInit')
        if "__ee_whip_handle" in self.__dict__:
            CyAudioGame().Destroy2DSound(self.__ee_whip_handle)
            del self.__dict__["__ee_whip_handle"]

    def onPreSave(self, argsList):
        "called before a game is actually saved"
        CvUtil.pyPrint('OnPreSave')

    def onSaveGame(self, argsList):
        "return the string to be saved - Must be a string"
        return ""

    def onLoadGame(self, argsList):
        CvAdvisorUtils.resetNoLiberateCities()

        #PBMod start
        global iPlayerOptionCheck
        # Attention, for iPlayerOptionCheck = 1 you will check aggainst
        # the option values stored in the save file, but not the current one!
        iPlayerOptionCheck = 8   # 1 = 1/4 sec

        if "__ee_whip_handle" in self.__dict__:
            CyAudioGame().Destroy2DSound(self.__ee_whip_handle)
            del self.__dict__["__ee_whip_handle"]

        #PBMod end

        return 0

    def onGameStart(self, argsList):
        'Called at the start of the game'
        if (gc.getGame().getGameTurnYear() == gc.getDefineINT("START_YEAR") and not gc.getGame().isOption(GameOptionTypes.GAMEOPTION_ADVANCED_START)):
            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                if (player.isAlive() and player.isHuman()):
                    popupInfo = CyPopupInfo()
                    popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
                    popupInfo.setText(u"showDawnOfMan")
                    popupInfo.addPopup(iPlayer)
        else:
            CyInterface().setSoundSelectionReady(true)

        if gc.getGame().isPbem():
            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                if (player.isAlive() and player.isHuman()):
                    popupInfo = CyPopupInfo()
                    popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_DETAILS)
                    popupInfo.setOption1(true)
                    popupInfo.addPopup(iPlayer)

        CvAdvisorUtils.resetNoLiberateCities()

        logName = None
        if not CyGame().isGameMultiPlayer():  # For local debugging
            logName = BugPath.join(BugPath.getRootDir(), "CtHBalance.log")
            f = open(logName, "w")
            f.write("Players|")

            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                if (player.isAlive()):
                    f.write("%s||||||||||||||||||||||||||||||||||||||||||||||" %
                        (player.getCivilizationDescription(1).encode('utf-8')))
            f.write("\n")
            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                if (player.isAlive()):
                    f.write("|Score|GoldenAgeTurns|ActualGold|ActualScience|Food|FoodKept|CurrentProduction|TotalProduction|ModifiedProduction|TotalSpecialist|CivicProduction|TotalWhip|TotalChop|TotalCommerce|TotalCommerceGold|VirtualGold|VirtualScience|GoldPerTurn|City Count|Total Pop|Inflation|Financial Bonus Lighthouse|Financial Bonus|Financial BtS Bonus|Foreign Trade Routes|Foreign Trade Income|Domestic Trade Routes|Domestic Trade Income|Domestic 75 Protective Bonus|Domestic 100 Protective Bonus|Aggressive Maintenance Bonus|City Maintenance|Unit Cost|Unit Supply|Civic Maintenance|ORG|Gov Civic|Leg Civic|Lab Civic|Eco Civic|Rel Civic|Wonders|Great Person|Tech Tracking|WW|TotalEspionage")
            f.write("\n")
            f.close()

        combatLogName = None
        if not CyGame().isPitbossHost():
            combatLogName = BugPath.join(BugPath.getRootDir(), "Combat.log")
            f = open(combatLogName, "w")
            f.close()

    def onGameEnd(self, argsList):
        'Called at the End of the game'
        print("Game is ending")
        return

    def onBeginGameTurn(self, argsList):
        'Called at the beginning of the end of each turn'
        iGameTurn = argsList[0]
        CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
        self.bGameTurnProcessing = True

    def onEndGameTurn(self, argsList):
        'Called at the end of the end of each turn'
        iGameTurn = argsList[0]
        self.bGameTurnProcessing = False
        ''' #Logfunktion deactivated, seems to make problems in languages other than english
        # CtHBalance.log
        logName = None
        if CyGame().isPitbossHost():
            logName = os.path.join(gc.getAltrootDir(), "Logs", "CtHBalance.log")
        elif not CyGame().isGameMultiPlayer():  # For local debugging
            logName = BugPath.join(BugPath.getRootDir(), "CtHBalance.log")

        if logName:
            f = codecs.open(logName, "a", 'utf-8')

            # Add values here            
            f.write("Turn %d|" % (CyGame().getGameTurn()))
            for iPlayer in range(gc.getMAX_PLAYERS()):
                player = gc.getPlayer(iPlayer)
                team = gc.getTeam(player.getTeam())
                if (player.isAlive()):
                    #Score|GoldenAgeTurns|ActualGold|ActualScience|Food|FoodKept|CurrentProduction|TotalProduction|ModifiedProduction|TotalSpecialist|CivicProduction|TotalWhip|TotalChop|TotalCommerce|TotalCommerceGold|VirtualGold|VirtualScience|GoldPerTurn|City Count|Total Pop|Inflation|Financial Bonus Lighthouse|Financial Bonus|Financial BtS Bonus|Foreign Trade Routes|Foreign Trade Income|Domestic Trade Routes|Domestic Trade Income|Domestic 75 Protective Bonus|Domestic 100 Protective Bonus|Aggressive Maintenance Bonus|City Maintenance|Unit Cost|Unit Supply|Civic Maintenance|ORG|Gov Civic|Leg Civic|Lab Civic|Eco Civic|Rel Civic|Wonders|Great Person|Tech Tracking|WW|TotalEspionage
                    f.write("%d|" % (player.getScoreHistory(CyGame().getGameTurn(),True)))
                    f.write("%d|" % (player.getGoldenAgeTurns()))
                    f.write("%d|" % (player.getCommerceRate(CommerceTypes.COMMERCE_GOLD)))
                    f.write("%d|" % (player.getCommerceRate(CommerceTypes.COMMERCE_RESEARCH)))
                    if (player.calculateBaseNetFullGoldTracking() == 0):
                        f.write("0|")
                        f.write("0|")
                        f.write("|")
                        f.write("0|")
                        f.write("0|")
                        f.write("0|")
                        f.write("0|")
                    else:
                        f.write("%d|" % (player.calculateTotalFoodTracking())) 
                        f.write("%d|" % (player.calculateTotalFoodKeptTracking())) 
                        f.write("%s|" % (player.getCurrentProductionTracking()))
                        f.write("%d|" % (player.calculateTotalBaseProductionTracking())) 
                        f.write("%d|" % (player.calculateTotalProductionTracking()))
                        f.write("%d|" % (player.getSpecialistPopulation())) 
                        f.write("%d|" % (player.getCivicProduction()))
                    f.write("%d|" % (player.getTotalWhip()))
                    f.write("%d|" % (player.getTotalChop()))
                    if (player.calculateBaseNetFullGoldTracking() == 0):
                        f.write("0|")
                    else:
                        f.write("%d|" % (player.calculateTotalYield(2)))
                    f.write("%d|" % (player.calculateBaseNetCommerceGoldTracking()))
                    f.write("%d|" % (player.calculateBaseNetFullGoldTracking()))
                    f.write("%d|" % (player.calculateBaseNetFullResearchTracking()))
                    f.write("%d|" % (player.getGoldPerTurn()))                    
                    f.write("%d|" % (player.getNumCities()))
                    f.write("%d|" % (player.getTotalPopulation()))
                    f.write("%d|" % (player.calculateInflatedCosts()))
                    f.write("%d|" % (player.getTrackingFinancialBonusLighthouse()))
                    f.write("%d|" % (player.getTrackingFinancialBonus()))
                    f.write("%d|" % (player.getTrackingOriginalFinancialBonus()))
                    f.write("%d|" % (player.getTrackingForeignTradeRoutes()))
                    f.write("%d|" % (player.getTrackingForeignTradeRoutesCommerce()))
                    f.write("%d|" % (player.getTrackingDomesticTradeRoutes()))
                    f.write("%d|" % (player.getTrackingDomesticTradeRoutesCommerce()))
                    f.write("%d|" % (player.getTrackingDomesticProtectiveBonus()))
                    f.write("%d|" % (player.getBetterTrackingDomesticProtectiveBonus()))
                    f.write("%d|" % (player.calculateUnitCostTraitReduction()))                    
                    f.write("%d|" % (player.getTotalMaintenance()))
                    f.write("%d|" % (player.calculateUnitCost()))
                    f.write("%d|" % (player.calculateUnitSupply()))
                    f.write("%d|" % (player.getCivicUpkeep([], False)))
                    f.write("%d|" % (player.getCivicUpkeepBonusTracking([], False)))
                    f.write("%s|" %
                            (gc.getCivicInfo(player.getCivics(0)).getDescription().encode('utf-8')))
                    f.write("%s|" % (gc.getCivicInfo(player.getCivics(1)).getDescription().encode('utf-8')))
                    f.write("%s|" % (gc.getCivicInfo(player.getCivics(2)).getDescription().encode('utf-8')))
                    f.write("%s|" % (gc.getCivicInfo(player.getCivics(3)).getDescription().encode('utf-8')))
                    f.write("%s|" % (gc.getCivicInfo(player.getCivics(4)).getDescription().encode('utf-8')))
                    f.write("%s|" % (player.getWonderTracking()))
                    f.write("%s|" % (player.getGreatPersonTracking()))
                    f.write("%s|" % (player.getTechTracking()))
                    f.write("%s|" % (player.getWarWearinessPercentAnger()))
                    f.write("%s|" % (team.getEspionagePointsEver()))
            
            f.write("\n")    
            f.close()
        '''

    def onBeginPlayerTurn(self, argsList):
        'Called at the beginning of a players turn'
        iGameTurn, iPlayer = argsList

    def onEndPlayerTurn(self, argsList):
        'Called at the end of a players turn'
        iGameTurn, iPlayer = argsList

        if (gc.getGame().getElapsedGameTurns() == 1):
            if (gc.getPlayer(iPlayer).isHuman()):
                if (gc.getPlayer(iPlayer).canRevolution(0)):
                    popupInfo = CyPopupInfo()
                    popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_CHANGECIVIC)
                    popupInfo.addPopup(iPlayer)

        CvAdvisorUtils.resetAdvisorNags()
        CvAdvisorUtils.endTurnFeats(iPlayer)

    def onEndTurnReady(self, argsList):
        iGameTurn = argsList[0]

    def onFirstContact(self, argsList):
        'Contact'
        iTeamX,iHasMetTeamY = argsList
        if (not self.__LOG_CONTACT):
            return
        CvUtil.pyPrint('Team %d has met Team %d' %(iTeamX, iHasMetTeamY))

    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 (not self.__LOG_COMBAT):
            return
        if playerX and playerX and unitX and playerY:
            CvUtil.pyPrint('Player %d Civilization %s Unit %s has defeated Player %d Civilization %s Unit %s'
                %(playerX.getID(), playerX.getCivilizationName(), unitX.getDescription(),
                playerY.getID(), playerY.getCivilizationName(), unitY.getDescription()))

    def onCombatLogCalc(self, argsList):
        'Combat Result'
        genericArgs = argsList[0][0]
        cdAttacker = genericArgs[0]
        cdDefender = genericArgs[1]
        iCombatOdds = genericArgs[2]
        CvUtil.combatMessageBuilder(cdAttacker, cdDefender, iCombatOdds)

    def onCombatLogHit(self, argsList):
        'Combat Message'
        global gCombatMessages, gCombatLog
        genericArgs = argsList[0][0]
        cdAttacker = genericArgs[0]
        cdDefender = genericArgs[1]
        iIsAttacker = genericArgs[2]
        iDamage = genericArgs[3]

        if cdDefender.eOwner == cdDefender.eVisualOwner:
            szDefenderName = gc.getPlayer(cdDefender.eOwner).getNameKey()
        else:
            szDefenderName = localText.getText("TXT_KEY_TRAIT_PLAYER_UNKNOWN", ())
        if cdAttacker.eOwner == cdAttacker.eVisualOwner:
            szAttackerName = gc.getPlayer(cdAttacker.eOwner).getNameKey()
        else:
            szAttackerName = localText.getText("TXT_KEY_TRAIT_PLAYER_UNKNOWN", ())

        if (iIsAttacker == 0):              
            combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (szDefenderName, cdDefender.sUnitName, iDamage, cdDefender.iCurrHitPoints, cdDefender.iMaxHitPoints))
            CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
            CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
            if (cdDefender.iCurrHitPoints <= 0):
                combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (szAttackerName, cdAttacker.sUnitName, szDefenderName, cdDefender.sUnitName))
                CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
                CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
        elif (iIsAttacker == 1):
            combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (szAttackerName, cdAttacker.sUnitName, iDamage, cdAttacker.iCurrHitPoints, cdAttacker.iMaxHitPoints))
            CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
            CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
            if (cdAttacker.iCurrHitPoints <= 0):
                combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (szDefenderName, cdDefender.sUnitName, szAttackerName, cdAttacker.sUnitName))
                CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
                CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
        
        # CtHCombat.log
        combatLogName = None
        if not CyGame().isPitbossHost():
            isActivePlayer = gc.getGame().getActivePlayer() in [cdDefender.eOwner, cdAttacker.eOwner]

            if isActivePlayer:
                combatLogName = BugPath.join(BugPath.getRootDir(), "Combat.log")
                f = codecs.open(combatLogName, "a", 'utf-8')
                if (iIsAttacker == 0):
                    combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (szDefenderName, cdDefender.sUnitName, iDamage, cdDefender.iCurrHitPoints, cdDefender.iMaxHitPoints))
                    f.write(combatMessage + " \n")
                    if (cdDefender.iCurrHitPoints <= 0):
                        combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (szAttackerName, cdAttacker.sUnitName, szDefenderName, cdDefender.sUnitName))
                        f.write(combatMessage + " \n")
                elif (iIsAttacker == 1):
                    combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (szAttackerName, cdAttacker.sUnitName, iDamage, cdAttacker.iCurrHitPoints, cdAttacker.iMaxHitPoints))
                    f.write(combatMessage + " \n")
                    if (cdAttacker.iCurrHitPoints <= 0):
                        combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (szDefenderName, cdDefender.sUnitName, szAttackerName, cdAttacker.sUnitName))
                        f.write(combatMessage + " \n")
                f.close()

    def onImprovementBuilt(self, argsList):
        'Improvement Built'
        iImprovement, iX, iY = argsList
        if (not self.__LOG_IMPROVEMENT):
            return
        CvUtil.pyPrint('Improvement %s was built at %d, %d'
            %(PyInfo.ImprovementInfo(iImprovement).getDescription(), iX, iY))

    def onImprovementDestroyed(self, argsList):
        'Improvement Destroyed'
        iImprovement, iOwner, iX, iY = argsList
        if (not self.__LOG_IMPROVEMENT):
            return
        CvUtil.pyPrint('Improvement %s was Destroyed at %d, %d'
            %(PyInfo.ImprovementInfo(iImprovement).getDescription(), iX, iY))

    def onRouteBuilt(self, argsList):
        'Route Built'
        iRoute, iX, iY = argsList
        if (not self.__LOG_IMPROVEMENT):
            return
        CvUtil.pyPrint('Route %s was built at %d, %d'
            %(gc.getRouteInfo(iRoute).getDescription(), iX, iY))

    def onPlotRevealed(self, argsList):
        'Plot Revealed'
        pPlot = argsList[0]
        iTeam = argsList[1]

    def onPlotFeatureRemoved(self, argsList):
        'Plot Revealed'
        pPlot = argsList[0]
        iFeatureType = argsList[1]
        pCity = argsList[2] # This can be null

    def onPlotPicked(self, argsList):
        'Plot Picked'
        pPlot = argsList[0]
        CvUtil.pyPrint('Plot was picked at %d, %d'
            %(pPlot.getX(), pPlot.getY()))

    def onNukeExplosion(self, argsList):
        'Nuke Explosion'
        pPlot, pNukeUnit = argsList
        CvUtil.pyPrint('Nuke detonated at %d, %d'
            %(pPlot.getX(), pPlot.getY()))

    def onGotoPlotSet(self, argsList):
        'Nuke Explosion'
        pPlot, iPlayer = argsList

    def onBuildingBuilt(self, argsList):
        'Building Completed'
        pCity, iBuildingType = argsList
        game = gc.getGame()
        if ((not gc.getGame().isNetworkMultiPlayer()) and (pCity.getOwner() == gc.getGame().getActivePlayer()) and isWorldWonderClass(gc.getBuildingInfo(iBuildingType).getBuildingClassType())):
            # If this is a wonder...
            popupInfo = CyPopupInfo()
            popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
            popupInfo.setData1(iBuildingType)
            popupInfo.setData2(pCity.getID())
            popupInfo.setData3(0)
            popupInfo.setText(u"showWonderMovie")
            popupInfo.addPopup(pCity.getOwner())

        CvAdvisorUtils.buildingBuiltFeats(pCity, iBuildingType)

        if (not self.__LOG_BUILDING):
            return
        CvUtil.pyPrint('%s was finished by Player %d Civilization %s'
            %(PyInfo.BuildingInfo(iBuildingType).getDescription(), pCity.getOwner(), gc.getPlayer(pCity.getOwner()).getCivilizationDescription(0)))

    def onProjectBuilt(self, argsList):
        'Project Completed'
        pCity, iProjectType = argsList
        game = gc.getGame()
        if ((not gc.getGame().isNetworkMultiPlayer()) and (pCity.getOwner() == gc.getGame().getActivePlayer())):
            popupInfo = CyPopupInfo()
            popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
            popupInfo.setData1(iProjectType)
            popupInfo.setData2(pCity.getID())
            popupInfo.setData3(2)
            popupInfo.setText(u"showWonderMovie")
            popupInfo.addPopup(pCity.getOwner())

    def onSelectionGroupPushMission(self, argsList):
        'selection group mission'
        eOwner = argsList[0]
        eMission = argsList[1]
        iNumUnits = argsList[2]
        listUnitIds = argsList[3]

        if (not self.__LOG_PUSH_MISSION):
            return
        if pHeadUnit:
            CvUtil.pyPrint("Selection Group pushed mission %d" %(eMission))

    def onUnitMove(self, argsList):
        'unit move'
        pPlot,pUnit,pOldPlot = argsList
        player = PyPlayer(pUnit.getOwner())
        unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())
        if (not self.__LOG_MOVEMENT):
            return
        if player and unitInfo:
            CvUtil.pyPrint('Player %d Civilization %s unit %s is moving to %d, %d'
                %(player.getID(), player.getCivilizationName(), unitInfo.getDescription(),
                pUnit.getX(), pUnit.getY()))

    def onUnitSetXY(self, argsList):
        'units xy coords set manually'
        pPlot,pUnit = argsList
        player = PyPlayer(pUnit.getOwner())
        unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())
        if (not self.__LOG_MOVEMENT):
            return

    def onUnitCreated(self, argsList):
        'Unit Completed'
        unit = argsList[0]
        player = PyPlayer(unit.getOwner())
        if (not self.__LOG_UNITBUILD):
            return

    def onUnitBuilt(self, argsList):
        'Unit Completed'
        city = argsList[0]
        unit = argsList[1]
        player = PyPlayer(city.getOwner())

        CvAdvisorUtils.unitBuiltFeats(city, unit)

        if (not self.__LOG_UNITBUILD):
            return
        CvUtil.pyPrint('%s was finished by Player %d Civilization %s'
            %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))

    def onUnitKilled(self, argsList):
        'Unit Killed'
        unit, iAttacker = argsList
        player = PyPlayer(unit.getOwner())
        attacker = PyPlayer(iAttacker)
        if (not self.__LOG_UNITKILLED):
            return
        CvUtil.pyPrint('Player %d Civilization %s Unit %s was killed by Player %d'
            %(player.getID(), player.getCivilizationName(), PyInfo.UnitInfo(unit.getUnitType()).getDescription(), attacker.getID()))

    def onUnitLost(self, argsList):
        'Unit Lost'
        unit = argsList[0]
        player = PyPlayer(unit.getOwner())
        if (not self.__LOG_UNITLOST):
            return
        CvUtil.pyPrint('%s was lost by Player %d Civilization %s'
            %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))

    def onUnitPromoted(self, argsList):
        'Unit Promoted'
        pUnit, iPromotion = argsList
        player = PyPlayer(pUnit.getOwner())
        if (not self.__LOG_UNITPROMOTED):
            return
        CvUtil.pyPrint('Unit Promotion Event: %s - %s' %(player.getCivilizationName(), pUnit.getName(),))

    def onUnitSelected(self, argsList):
        'Unit Selected'
        unit = argsList[0]
        player = PyPlayer(unit.getOwner())
        if (not self.__LOG_UNITSELECTED):
            return
        CvUtil.pyPrint('%s was selected by Player %d Civilization %s'
            %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))

    def onUnitRename(self, argsList):
        'Unit is renamed'
        pUnit = argsList[0]
        if (pUnit.getOwner() == gc.getGame().getActivePlayer()):
            self.__eventEditUnitNameBegin(pUnit)

    def onUnitPillage(self, argsList):
        'Unit pillages a plot'
        pUnit, iImprovement, iRoute, iOwner = argsList
        iPlotX = pUnit.getX()
        iPlotY = pUnit.getY()
        pPlot = CyMap().plot(iPlotX, iPlotY)

        if (not self.__LOG_UNITPILLAGE):
            return
        CvUtil.pyPrint("Player %d's %s pillaged improvement %d and route %d at plot at (%d, %d)"
            %(iOwner, PyInfo.UnitInfo(pUnit.getUnitType()).getDescription(), iImprovement, iRoute, iPlotX, iPlotY))

    def onUnitSpreadReligionAttempt(self, argsList):
        'Unit tries to spread religion to a city'
        pUnit, iReligion, bSuccess = argsList

        iX = pUnit.getX()
        iY = pUnit.getY()
        pPlot = CyMap().plot(iX, iY)
        pCity = pPlot.getPlotCity()

    def onUnitGifted(self, argsList):
        'Unit is gifted from one player to another'
        pUnit, iGiftingPlayer, pPlotLocation = argsList

    def onUnitBuildImprovement(self, argsList):
        'Unit begins enacting a Build (building an Improvement or Route)'
        pUnit, iBuild, bFinished = argsList

    def onGoodyReceived(self, argsList):
        'Goody received'
        iPlayer, pPlot, pUnit, iGoodyType = argsList
        if (not self.__LOG_GOODYRECEIVED):
            return
        CvUtil.pyPrint('%s received a goody' %(gc.getPlayer(iPlayer).getCivilizationDescription(0)),)

    def onGreatPersonBorn(self, argsList):
        'Unit Promoted'
        pUnit, iPlayer, pCity = argsList
        player = PyPlayer(iPlayer)
        if pUnit.isNone() or pCity.isNone():
            return
        if (not self.__LOG_GREATPERSON):
            return
        CvUtil.pyPrint('A %s was born for %s in %s' %(pUnit.getName(), player.getCivilizationName(), pCity.getName()))

    def onTechAcquired(self, argsList):
        'Tech Acquired'
        iTechType, iTeam, iPlayer, bAnnounce = argsList
        # Note that iPlayer may be NULL (-1) and not a refer to a player object

        # Show tech splash when applicable
        if (iPlayer > -1 and bAnnounce and not CyInterface().noTechSplash()):
            if (gc.getGame().isFinalInitialized() and not gc.getGame().GetWorldBuilderMode()):
                if ((not gc.getGame().isNetworkMultiPlayer()) and (iPlayer == gc.getGame().getActivePlayer())):
                    popupInfo = CyPopupInfo()
                    popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
                    popupInfo.setData1(iTechType)
                    popupInfo.setText(u"showTechSplash")
                    popupInfo.addPopup(iPlayer)

        if (not self.__LOG_TECH):
            return
        CvUtil.pyPrint('%s was finished by Team %d'
            %(PyInfo.TechnologyInfo(iTechType).getDescription(), iTeam))

    def onTechSelected(self, argsList):
        'Tech Selected'
        iTechType, iPlayer = argsList
        if (not self.__LOG_TECH):
            return
        CvUtil.pyPrint('%s was selected by Player %d' %(PyInfo.TechnologyInfo(iTechType).getDescription(), iPlayer))

    def onReligionFounded(self, argsList):
        'Religion Founded'
        iReligion, iFounder = argsList
        player = PyPlayer(iFounder)

        iCityId = gc.getGame().getHolyCity(iReligion).getID()
        if (gc.getGame().isFinalInitialized() and not gc.getGame().GetWorldBuilderMode()):
            if ((not gc.getGame().isNetworkMultiPlayer()) and (iFounder == gc.getGame().getActivePlayer())):
                popupInfo = CyPopupInfo()
                popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
                popupInfo.setData1(iReligion)
                popupInfo.setData2(iCityId)
                popupInfo.setData3(1)
                popupInfo.setText(u"showWonderMovie")
                popupInfo.addPopup(iFounder)

        if (not self.__LOG_RELIGION):
            return
        CvUtil.pyPrint('Player %d Civilization %s has founded %s'
            %(iFounder, player.getCivilizationName(), gc.getReligionInfo(iReligion).getDescription()))

    def onReligionSpread(self, argsList):
        'Religion Has Spread to a City'
        iReligion, iOwner, pSpreadCity = argsList
        player = PyPlayer(iOwner)
        if (not self.__LOG_RELIGIONSPREAD):
            return
        CvUtil.pyPrint('%s has spread to Player %d Civilization %s city of %s'
            %(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pSpreadCity.getName()))

    def onReligionRemove(self, argsList):
        'Religion Has been removed from a City'
        iReligion, iOwner, pRemoveCity = argsList
        player = PyPlayer(iOwner)
        if (not self.__LOG_RELIGIONSPREAD):
            return
        CvUtil.pyPrint('%s has been removed from Player %d Civilization %s city of %s'
            %(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pRemoveCity.getName()))

    def onCorporationFounded(self, argsList):
        'Corporation Founded'
        iCorporation, iFounder = argsList
        player = PyPlayer(iFounder)

        if (not self.__LOG_RELIGION):
            return
        CvUtil.pyPrint('Player %d Civilization %s has founded %s'
            %(iFounder, player.getCivilizationName(), gc.getCorporationInfo(iCorporation).getDescription()))

    def onCorporationSpread(self, argsList):
        'Corporation Has Spread to a City'
        iCorporation, iOwner, pSpreadCity = argsList
        player = PyPlayer(iOwner)
        if (not self.__LOG_RELIGIONSPREAD):
            return
        CvUtil.pyPrint('%s has spread to Player %d Civilization %s city of %s'
            %(gc.getCorporationInfo(iCorporation).getDescription(), iOwner, player.getCivilizationName(), pSpreadCity.getName()))

    def onCorporationRemove(self, argsList):
        'Corporation Has been removed from a City'
        iCorporation, iOwner, pRemoveCity = argsList
        player = PyPlayer(iOwner)
        if (not self.__LOG_RELIGIONSPREAD):
            return
        CvUtil.pyPrint('%s has been removed from Player %d Civilization %s city of %s'
            %(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pRemoveCity.getName()))

    def onGoldenAge(self, argsList):
        'Golden Age'
        iPlayer = argsList[0]
        player = PyPlayer(iPlayer)
        if (not self.__LOG_GOLDENAGE):
            return
        CvUtil.pyPrint('Player %d Civilization %s has begun a golden age'
            %(iPlayer, player.getCivilizationName()))

    def onEndGoldenAge(self, argsList):
        'End Golden Age'
        iPlayer = argsList[0]
        player = PyPlayer(iPlayer)
        if (not self.__LOG_ENDGOLDENAGE):
            return
        CvUtil.pyPrint('Player %d Civilization %s golden age has ended'
            %(iPlayer, player.getCivilizationName()))

    def onChangeWar(self, argsList):
        'War Status Changes'
        bIsWar = argsList[0]
        iTeam = argsList[1]
        iRivalTeam = argsList[2]
        if (not self.__LOG_WARPEACE):
            return
        if (bIsWar):
            strStatus = "declared war"
        else:
            strStatus = "declared peace"
        CvUtil.pyPrint('Team %d has %s on Team %d'
            %(iTeam, strStatus, iRivalTeam))

    def onChat(self, argsList):
        'Chat Message Event'
        chatMessage = "%s" %(argsList[0],)

    def onSetPlayerAlive(self, argsList):
        'Set Player Alive Event'
        iPlayerID = argsList[0]
        bNewValue = argsList[1]
        CvUtil.pyPrint("Player %d's alive status set to: %d" %(iPlayerID, int(bNewValue)))

    def onPlayerChangeStateReligion(self, argsList):
        'Player changes his state religion'
        iPlayer, iNewReligion, iOldReligion = argsList

    def onPlayerGoldTrade(self, argsList):
        'Player Trades gold to another player'
        iFromPlayer, iToPlayer, iGoldAmount = argsList

    def onCityBuilt(self, argsList):
        'City Built'
        city = argsList[0]
        if (city.getOwner() == gc.getGame().getActivePlayer()):
            self.__eventEditCityNameBegin(city, False)
        CvUtil.pyPrint('City Built Event: %s' %(city.getName()))

    def onCityRazed(self, argsList):
        'City Razed'
        city, iPlayer = argsList
        iOwner = city.findHighestCulture()

        #Removed by Charriu, because the Partisans event was removed by Krill
        # Partisans!
        #if city.getPopulation > 1 and iOwner != -1 and iPlayer != -1:
        #   owner = gc.getPlayer(iOwner)
        #   if not owner.isBarbarian() and owner.getNumCities() > 0:
        #       if gc.getTeam(owner.getTeam()).isAtWar(gc.getPlayer(iPlayer).getTeam()):
        #           if gc.getNumEventTriggerInfos() > 0: # prevents mods that don't have events from getting an error
        #               iEvent = CvUtil.findInfoTypeNum(gc.getEventTriggerInfo, gc.getNumEventTriggerInfos(),'EVENTTRIGGER_PARTISANS')
        #               if iEvent != -1 and gc.getGame().isEventActive(iEvent) and owner.getEventTriggerWeight(iEvent) < 0:
        #                   triggerData = owner.initTriggeredData(iEvent, true, -1, city.getX(), city.getY(), iPlayer, city.getID(), -1, -1, -1, -1)

        CvUtil.pyPrint("City Razed Event: %s" %(city.getName(),))

    def onCityAcquired(self, argsList):
        'City Acquired'
        iPreviousOwner,iNewOwner,pCity,bConquest,bTrade = argsList
        CvUtil.pyPrint('City Acquired Event: %s' %(pCity.getName()))

    def onCityAcquiredAndKept(self, argsList):
        'City Acquired and Kept'
        iOwner,pCity = argsList
        CvUtil.pyPrint('City Acquired and Kept Event: %s' %(pCity.getName()))

    def onCityLost(self, argsList):
        'City Lost'
        city = argsList[0]
        player = PyPlayer(city.getOwner())
        if (not self.__LOG_CITYLOST):
            return
        CvUtil.pyPrint('City %s was lost by Player %d Civilization %s'
            %(city.getName(), player.getID(), player.getCivilizationName()))

    def onCultureExpansion(self, argsList):
        'City Culture Expansion'
        pCity = argsList[0]
        iPlayer = argsList[1]
        CvUtil.pyPrint("City %s's culture has expanded" %(pCity.getName(),))

    def onCityGrowth(self, argsList):
        'City Population Growth'
        pCity = argsList[0]
        iPlayer = argsList[1]
        CvUtil.pyPrint("%s has grown" %(pCity.getName(),))

    def onCityDoTurn(self, argsList):
        'City Production'
        pCity = argsList[0]
        iPlayer = argsList[1]

        CvAdvisorUtils.cityAdvise(pCity, iPlayer)

    def onCityBuildingUnit(self, argsList):
        'City begins building a unit'
        pCity = argsList[0]
        iUnitType = argsList[1]
        if (not self.__LOG_CITYBUILDING):
            return
        CvUtil.pyPrint("%s has begun building a %s" %(pCity.getName(),gc.getUnitInfo(iUnitType).getDescription()))

    def onCityBuildingBuilding(self, argsList):
        'City begins building a Building'
        pCity = argsList[0]
        iBuildingType = argsList[1]
        if (not self.__LOG_CITYBUILDING):
            return
        CvUtil.pyPrint("%s has begun building a %s" %(pCity.getName(),gc.getBuildingInfo(iBuildingType).getDescription()))

    def onCityRename(self, argsList):
        'City is renamed'
        pCity = argsList[0]
        if (pCity.getOwner() == gc.getGame().getActivePlayer()):
            self.__eventEditCityNameBegin(pCity, True)

    def onCityHurry(self, argsList):
        'City is renamed'
        pCity = argsList[0]
        iHurryType = argsList[1]

        # EE
        if (pCity.getOwner() == gc.getGame().getActivePlayer()
            and iHurryType == 0):
            if "__ee_whip_played" not in self.__dict__:
                # Check if previous instance is running
                bPlay = True
                if "__ee_whip_handle" in self.__dict__:
                    if CyAudioGame().Is2DSoundPlaying(self.__ee_whip_handle):
                        bPlay = False
                    else:
                        CyAudioGame().Destroy2DSound(self.__ee_whip_handle)
                        del self.__dict__["__ee_whip_handle"]

                if bPlay:
                    r = gc.getASyncRand().get(500, "Whip ASYNC")
                    # CvUtil.pyPrint("EE_WHIP random val: %i" % (r,))
                    if r == 0:
                        self.__ee_whip_played = True
                        self.__ee_whip_handle = CyAudioGame().Play2DSound("AS2D_MOD_EE_WHIP")

                    elif r <= 5:
                        # can trigger more than once
                        # self.__ee_whip_played = True
                        self.__ee_whip_handle = CyAudioGame().Play2DSound("AS2D_MOD_EE_WHIP_SHORT")


    def onVictory(self, argsList):
        'Victory'
        iTeam, iVictory = argsList
        if (iVictory >= 0 and iVictory < gc.getNumVictoryInfos()):
            victoryInfo = gc.getVictoryInfo(int(iVictory))
            CvUtil.pyPrint("Victory!  Team %d achieves a %s victory"
                %(iTeam, victoryInfo.getDescription()))

    def onVassalState(self, argsList):
        'Vassal State'
        iMaster, iVassal, bVassal = argsList

        if (bVassal):
            CvUtil.pyPrint("Team %d becomes a Vassal State of Team %d"
                %(iVassal, iMaster))
        else:
            CvUtil.pyPrint("Team %d revolts and is no longer a Vassal State of Team %d"
                %(iVassal, iMaster))

    def onGameUpdate(self, argsList):
        'sample generic event, called on each game turn slice'
        genericArgs = argsList[0][0]    # tuple of tuple of my args
        turnSlice = genericArgs[0]

        #PBMod start
        global iPlayerOptionCheck
        if iPlayerOptionCheck > 0:
            iPlayerOptionCheck -= 1
            if iPlayerOptionCheck == 0:
                check_show_ressources()
        #PBMod end
                
    def onMouseEvent(self, argsList):
        'mouse handler - returns 1 if the event was consumed'
        eventType,mx,my,px,py,interfaceConsumed,screens = argsList
        if ( px!=-1 and py!=-1 ):
            if ( eventType == self.EventLButtonDown ):
                if (self.bAllowCheats and self.bCtrl and self.bAlt and CyMap().plot(px,py).isCity() and not interfaceConsumed):
                    # Launch Edit City Event
                    self.beginEvent( CvUtil.EventEditCity, (px,py) )
                    return 1

                elif (self.bAllowCheats and self.bCtrl and self.bShift and not interfaceConsumed):
                    # Launch Place Object Event
                    self.beginEvent( CvUtil.EventPlaceObject, (px, py) )
                    return 1

        if ( eventType == self.EventBack ):
            return CvScreensInterface.handleBack(screens)
        elif ( eventType == self.EventForward ):
            return CvScreensInterface.handleForward(screens)

        return 0

#PBMod start
#################### TRIGGERED EVENTS ##################

    def __eventEditCityNameBegin(self, city, bRename):
        popup = PyPopup.PyPopup(CvUtil.EventEditCityName, EventContextTypes.EVENTCONTEXT_ALL)
        popup.setUserData((city.getID(), bRename))
        popup.setHeaderString(localText.getText("TXT_KEY_NAME_CITY", ()))
        popup.setBodyString(localText.getText("TXT_KEY_SETTLE_NEW_CITY_NAME", ()))
        popup.createEditBox(city.getName())
        popup.setEditBoxMaxCharCount( 15 )
        popup.launch()

    def __eventEditCityNameApply(self, playerID, userData, popupReturn):
        'Edit City Name Event'
        iCityID = userData[0]
        bRename = userData[1]
        player = gc.getPlayer(playerID)
        city = player.getCity(iCityID)
        cityName = popupReturn.getEditBoxString(0)
        if (len(cityName) > 30):
            cityName = cityName[:30]
        city.setName(cityName, not bRename)

    def __eventEditCityBegin(self, argsList):
        'Edit City Event'
        px,py = argsList
        CvWBPopups.CvWBPopups().initEditCity(argsList)

    def __eventEditCityApply(self, playerID, userData, popupReturn):
        'Edit City Event Apply'
        if (getChtLvl() > 0):
            CvWBPopups.CvWBPopups().applyEditCity( (popupReturn, userData) )

    def __eventPlaceObjectBegin(self, argsList):
        'Place Object Event'
        CvDebugTools.CvDebugTools().initUnitPicker(argsList)

    def __eventPlaceObjectApply(self, playerID, userData, popupReturn):
        'Place Object Event Apply'
        if (getChtLvl() > 0):
            CvDebugTools.CvDebugTools().applyUnitPicker( (popupReturn, userData) )

    def __eventAwardTechsAndGoldBegin(self, argsList):
        'Award Techs & Gold Event'
        CvDebugTools.CvDebugTools().cheatTechs()

    def __eventAwardTechsAndGoldApply(self, playerID, netUserData, popupReturn):
        'Award Techs & Gold Event Apply'
        if (getChtLvl() > 0):
            CvDebugTools.CvDebugTools().applyTechCheat( (popupReturn) )

    def __eventShowWonderBegin(self, argsList):
        'Show Wonder Event'
        CvDebugTools.CvDebugTools().wonderMovie()

    def __eventShowWonderApply(self, playerID, netUserData, popupReturn):
        'Wonder Movie Apply'
        if (getChtLvl() > 0):
            CvDebugTools.CvDebugTools().applyWonderMovie( (popupReturn) )

    def __eventEditUnitNameBegin(self, argsList):
        pUnit = argsList
        popup = PyPopup.PyPopup(CvUtil.EventEditUnitName, EventContextTypes.EVENTCONTEXT_ALL)
        popup.setUserData((pUnit.getID(),))
        popup.setBodyString(localText.getText("TXT_KEY_RENAME_UNIT", ()))
        popup.createEditBox(pUnit.getNameNoDesc())
        popup.launch()

    def __eventEditUnitNameApply(self, playerID, userData, popupReturn):
        'Edit Unit Name Event'
        iUnitID = userData[0]
        unit = gc.getPlayer(playerID).getUnit(iUnitID)
        newName = popupReturn.getEditBoxString(0)
        if (len(newName) > 25):
            newName = newName[:25]
        unit.setName(newName)

    def __eventWBAllPlotsPopupBegin(self, argsList):
        CvScreensInterface.getWorldBuilderScreen().allPlotsCB()
        return
    def __eventWBAllPlotsPopupApply(self, playerID, userData, popupReturn):
        if (popupReturn.getButtonClicked() >= 0):
            CvScreensInterface.getWorldBuilderScreen().handleAllPlotsCB(popupReturn)
        return

    def __eventWBLandmarkPopupBegin(self, argsList):
        CvScreensInterface.getWorldBuilderScreen().setLandmarkCB("")
        #popup = PyPopup.PyPopup(CvUtil.EventWBLandmarkPopup, EventContextTypes.EVENTCONTEXT_ALL)
        #popup.createEditBox(localText.getText("TXT_KEY_WB_LANDMARK_START", ()))
        #popup.launch()
        return

    def __eventWBLandmarkPopupApply(self, playerID, userData, popupReturn):
        if (popupReturn.getEditBoxString(0)):
            szLandmark = popupReturn.getEditBoxString(0)
            if (len(szLandmark)):
                CvScreensInterface.getWorldBuilderScreen().setLandmarkCB(szLandmark)
        return

    def __eventWBScriptPopupBegin(self, argsList):
        popup = PyPopup.PyPopup(CvUtil.EventWBScriptPopup, EventContextTypes.EVENTCONTEXT_ALL)
        popup.setHeaderString(localText.getText("TXT_KEY_WB_SCRIPT", ()))
        popup.createEditBox(CvScreensInterface.getWorldBuilderScreen().getCurrentScript())
        popup.launch()
        return

    def __eventWBScriptPopupApply(self, playerID, userData, popupReturn):
        if (popupReturn.getEditBoxString(0)):
            szScriptName = popupReturn.getEditBoxString(0)
            CvScreensInterface.getWorldBuilderScreen().setScriptCB(szScriptName)
        return

    def __eventWBStartYearPopupBegin(self, argsList):
        popup = PyPopup.PyPopup(CvUtil.EventWBStartYearPopup, EventContextTypes.EVENTCONTEXT_ALL)
        popup.createSpinBox(0, "", gc.getGame().getStartYear(), 1, 5000, -5000)
        popup.launch()
        return

    def __eventWBStartYearPopupApply(self, playerID, userData, popupReturn):
        iStartYear = popupReturn.getSpinnerWidgetValue(int(0))
        CvScreensInterface.getWorldBuilderScreen().setStartYearCB(iStartYear)
        return

def check_show_ressources():
    iPlayer = gc.getGame().getActivePlayer()
    if (iPlayer != -1
        and gc.getPlayer(iPlayer).isOption(
            PlayerOptionTypes.PLAYEROPTION_MODDER_1)
       ):
        CvUtil.pyPrint('toggle resource symbols on')
        bResourceOn = ControlTypes.CONTROL_RESOURCE_ALL + 1001
        CyGame().doControlWithoutWidget(bResourceOn)  # Ctrl+r

#PBMod end
