home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 January / Gamestar_80_2006-01_dvd.iso / Dema / Civilization4 / data1.cab / Civ4DemoComponent / Assets / Python / CvEventManager.py < prev    next >
Encoding:
Python Source  |  2005-11-09  |  31.3 KB  |  864 lines

  1. ## Sid Meier's Civilization 4
  2. ## Copyright Firaxis Games 2005
  3. ## 
  4. ## CvEventManager
  5. ## This class is passed an argsList from CvAppInterface.onEvent
  6. ## The argsList can contain anything from mouse location to key info
  7. ## The EVENTLIST that are being notified can be found 
  8.  
  9.  
  10. from CvPythonExtensions import *
  11. import CvUtil
  12. import CvScreensInterface
  13. import CvDebugTools
  14. import CvWBPopups
  15. import PyHelpers
  16. import Popup as PyPopup
  17. import CvCameraControls
  18. import CvTopCivs
  19. import sys
  20. import CvWorldBuilderScreen
  21. import CvAdvisorUtils
  22. import CvTechChooser
  23.  
  24. gc = CyGlobalContext()
  25. localText = CyTranslator()
  26. PyPlayer = PyHelpers.PyPlayer
  27. PyInfo = PyHelpers.PyInfo
  28.  
  29.  
  30. # globals
  31. ###################################################
  32. class CvEventManager:
  33.     def __init__(self):
  34.         #################### ON EVENT MAP ######################
  35.         #print "EVENTMANAGER INIT"
  36.         
  37.         self.bMultiplayer = False
  38.         self.bCtrl = False
  39.         self.bShift = False
  40.         self.bAlt = False
  41.         self.bAllowCheats = False
  42.         
  43.         # OnEvent Enums
  44.         self.EventLButtonDown=1
  45.         self.EventLcButtonDblClick=2
  46.         self.EventRButtonDown=3
  47.         self.EventBack=4
  48.         self.EventForward=5
  49.         self.EventKeyDown=6
  50.         self.EventKeyUp=7
  51.     
  52.         self.__LOG_MOVEMENT = 0
  53.         self.__LOG_BUILDING = 0
  54.         self.__LOG_COMBAT = 0
  55.         self.__LOG_CONTACT = 0
  56.         self.__LOG_IMPROVEMENT = 0
  57.         self.__LOG_CITYLOST = 0
  58.         self.__LOG_CITYBUILDING = 0
  59.         self.__LOG_TECH = 0
  60.         self.__LOG_UNITBUILD = 0
  61.         self.__LOG_UNITKILLED = 0
  62.         self.__LOG_UNITLOST = 0
  63.         self.__LOG_UNITPROMOTED = 0
  64.         self.__LOG_UNITSELECTED = 0
  65.         self.__LOG_GOODYRECEIVED = 0
  66.         self.__LOG_GREATPERSON = 0
  67.         self.__LOG_RELIGION = 0
  68.         self.__LOG_RELIGIONSPREAD = 0
  69.         self.__LOG_GOLDENAGE = 0
  70.         self.__LOG_ENDGOLDENAGE = 0
  71.         self.__LOG_WARPEACE = 0
  72.         
  73.         ## EVENTLIST
  74.         self.EventHandlerMap = {
  75.             'mouseEvent'            : self.onMouseEvent,
  76.             'kbdEvent'                 : self.onKbdEvent,
  77.             'Init'                    : self.onInit,
  78.             'Update'                : self.onUpdate,
  79.             'UnInit'                : self.onUnInit,
  80.             'OnSave'                : self.onSaveGame,
  81.             'OnPreSave'                : self.onPreSave,
  82.             'OnLoad'                : self.onLoadGame,
  83.             'GameStart'                : self.onGameStart,
  84.             'GameEnd'                : self.onGameEnd,
  85.             'plotRevealed'             : self.onPlotRevealed,
  86.             'BeginGameTurn'            : self.onBeginGameTurn,
  87.             'EndGameTurn'            : self.onEndGameTurn,
  88.             'BeginPlayerTurn'        : self.onBeginPlayerTurn,
  89.             'EndPlayerTurn'            : self.onEndPlayerTurn,
  90.             'endTurnReady'            : self.onEndTurnReady,
  91.             'combatResult'             : self.onCombatResult,
  92.           'combatLogCalc'             : self.onCombatLogCalc,
  93.           'combatLogHit'                : self.onCombatLogHit,
  94.             'improvementBuilt'         : self.onImprovementBuilt,
  95.             'routeBuilt'         : self.onRouteBuilt,
  96.             'firstContact'             : self.onFirstContact,
  97.             'cityBuilt'             : self.onCityBuilt,
  98.             'cityRazed'                : self.onCityRazed,
  99.             'cityAcquired'             : self.onCityAcquired,
  100.             'cityLost'                : self.onCityLost,
  101.             'cultureExpansion'         : self.onCultureExpansion,
  102.             'cityGrowth'             : self.onCityGrowth,
  103.             'cityDoTurn'             : self.onCityDoTurn,
  104.             'cityBuildingUnit'    : self.onCityBuildingUnit,
  105.             'cityBuildingBuilding'    : self.onCityBuildingBuilding,
  106.             'cityRename'                : self.onCityRename,
  107.             'unitMove'                 : self.onUnitMove,
  108.             'unitSetXY'             : self.onUnitSetXY,
  109.             'unitCreated'             : self.onUnitCreated,
  110.             'unitBuilt'             : self.onUnitBuilt,
  111.             'unitKilled'            : self.onUnitKilled,
  112.             'unitLost'                : self.onUnitLost,
  113.             'unitPromoted'            : self.onUnitPromoted,
  114.             'unitSelected'            : self.onUnitSelected, 
  115.             'UnitRename'                : self.onUnitRename,
  116.             'goodyReceived'            : self.onGoodyReceived,
  117.             'greatPersonBorn'          : self.onGreatPersonBorn,
  118.             'buildingBuilt'         : self.onBuildingBuilt,
  119.             'projectBuilt'             : self.onProjectBuilt,
  120.             'techAcquired'            : self.onTechAcquired,
  121.             'techSelected'            : self.onTechSelected,
  122.             'religionFounded'        : self.onReligionFounded,
  123.             'religionSpread'        : self.onReligionSpread, 
  124.             'goldenAge'                : self.onGoldenAge,
  125.             'endGoldenAge'            : self.onEndGoldenAge,
  126.             'chat'                     : self.onChat,
  127.             'victory'                : self.onVictory,
  128.             'changeWar'                : self.onChangeWar,
  129.             'setPlayerAlive'        : self.onSetPlayerAlive,
  130.             'windowActivation'        : self.onWindowActivation,
  131.             'gameUpdate'            : self.onGameUpdate,        # sample generic event
  132.         }
  133.  
  134.         ################## Events List ###############################
  135.         #
  136.         # Dictionary of Events, indexed by EventID (also used at popup context id)
  137.         #   entries have name, beginFunction, applyFunction [, randomization weight...]
  138.         #
  139.         # Normal events first, random events after
  140.         #    
  141.         ################## Events List ###############################
  142.         self.Events={
  143.             CvUtil.EventEditCityName : ('EditCityName', self.__eventEditCityNameApply, self.__eventEditCityNameBegin),
  144.             CvUtil.EventEditCity : ('EditCity', self.__eventEditCityApply, self.__eventEditCityBegin),
  145.             CvUtil.EventPlaceObject : ('PlaceObject', self.__eventPlaceObjectApply, self.__eventPlaceObjectBegin),
  146.             CvUtil.EventAwardTechsAndGold: ('AwardTechsAndGold', self.__eventAwardTechsAndGoldApply, self.__eventAwardTechsAndGoldBegin),
  147.             CvUtil.EventEditUnitName : ('EditUnitName', self.__eventEditUnitNameApply, self.__eventEditUnitNameBegin),
  148.             CvUtil.EventWBAllPlotsPopup : ('WBAllPlotsPopup', self.__eventWBAllPlotsPopupApply, self.__eventWBAllPlotsPopupBegin),
  149.             CvUtil.EventWBLandmarkPopup : ('WBLandmarkPopup', self.__eventWBLandmarkPopupApply, self.__eventWBLandmarkPopupBegin),
  150.             CvUtil.EventWBScriptPopup : ('WBScriptPopup', self.__eventWBScriptPopupApply, self.__eventWBScriptPopupBegin),
  151.             CvUtil.EventWBStartYearPopup : ('WBStartYearPopup', self.__eventWBStartYearPopupApply, self.__eventWBStartYearPopupBegin),
  152.         }    
  153. #################### EVENT STARTERS ######################
  154.     def handleEvent(self, argsList):
  155.         'EventMgr entry point'
  156.         # extract the last 6 args in the list, the first arg has already been consumed
  157.         self.origArgsList = argsList    # point to original
  158.         tag = argsList[0]                # event type string
  159.         idx = len(argsList)-6
  160.         self.bDbg, self.bMultiPlayer, self.bAlt, self.bCtrl, self.bShift, self.bAllowCheats = argsList[idx:]
  161.         ret = 0
  162.         if self.EventHandlerMap.has_key(tag):
  163.             fxn = self.EventHandlerMap[tag]
  164.             ret = fxn(argsList[1:idx])
  165.         return ret
  166.         
  167. #################### EVENT APPLY ######################    
  168.     def beginEvent( self, context, argsList=-1 ):
  169.         'Begin Event'
  170.         entry = self.Events[context]
  171.         return entry[2]( argsList )
  172.     
  173.     def applyEvent( self, argsList ):
  174.         'Apply the effects of an event '
  175.         context, playerID, netUserData, popupReturn = argsList
  176.         
  177.         if context == CvUtil.PopupTypeEffectViewer:
  178.             return CvDebugTools.g_CvDebugTools.applyEffectViewer( playerID, netUserData, popupReturn )
  179.         
  180.         entry = self.Events[context]
  181.                 
  182.         if ( context not in CvUtil.SilentEvents ):
  183.             self.reportEvent(entry, context, (playerID, netUserData, popupReturn) )
  184.         return entry[1]( playerID, netUserData, popupReturn )   # the apply function
  185.  
  186.     def reportEvent(self, entry, context, argsList):
  187.         'Report an Event to Events.log '
  188.         message = "DEBUG Event: %s (%s)" %(entry[0], gc.getActivePlayer().getName())
  189.         CyInterface().addImmediateMessage(message,"")
  190.         CvUtil.pyPrint(message)
  191.         return 0
  192.         
  193. #################### ON EVENTS ######################
  194.     def onKbdEvent(self, argsList):
  195.         'keypress handler - return 1 if the event was consumed'
  196.  
  197.         eventType,key,mx,my,px,py = argsList
  198.         game = CyGame()
  199.         self.bMultiplayer = CyGame().isNetworkMultiPlayer()
  200.         
  201.         if (self.bAllowCheats):
  202.             # notify debug tools of input to allow it to override the control
  203.             argsList = (eventType,key,self.bCtrl,self.bShift,self.bAlt,mx,my,px,py,self.bMultiplayer)
  204.             if ( CvDebugTools.g_CvDebugTools.notifyInput(argsList) ):
  205.                 return 0
  206.         
  207.         if ( eventType == self.EventKeyDown ):
  208.             theKey=int(key)
  209.             
  210.             CvCameraControls.g_CameraControls.handleInput( theKey )
  211.                         
  212.             # Shift - T (Debug - No MP)
  213.             if (self.bAllowCheats and theKey == int(InputTypes.KB_T)):
  214.                 if ( self.bShift ):
  215.                     self.beginEvent(CvUtil.EventAwardTechsAndGold)
  216.                     #self.beginEvent(CvUtil.EventCameraControlPopup)
  217.                     return 1
  218.                         
  219.             # Shift - ] (Debug - currently mouse-overd unit, health += 10
  220.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_LBRACKET) and self.bShift ):
  221.                 unit = CyMap().plot(px, py).getUnit(0)
  222.                 if ( not unit.isNone() ):
  223.                     d = min( unit.maxHitPoints()-1, unit.getDamage() + 10 )
  224.                     unit.setDamage( d, PlayerTypes.NO_PLAYER )
  225.                 
  226.             # Shift - [ (Debug - currently mouse-overd unit, health -= 10
  227.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_RBRACKET) and self.bShift ):
  228.                 unit = CyMap().plot(px, py).getUnit(0)
  229.                 if ( not unit.isNone() ):
  230.                     d = max( 0, unit.getDamage() - 10 )
  231.                     unit.setDamage( d, PlayerTypes.NO_PLAYER )
  232.                 
  233.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_F1)):
  234.                 if ( self.bShift ):
  235.                     CvScreensInterface.replayScreen.showScreen(False)
  236.                     return 1
  237.                 # don't return 1 unless you want the input consumed
  238.             
  239.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_F2)):
  240.                 if ( self.bShift ):
  241.                     import CvDebugInfoScreen
  242.                      CvScreensInterface.showDebugInfoScreen()
  243.                     return 1
  244.             
  245.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_F3)):
  246.                 if ( self.bShift ):
  247.                     CvScreensInterface.showDanQuayleScreen(())
  248.                     return 1
  249.                     
  250.             elif (self.bAllowCheats and theKey == int(InputTypes.KB_F4)):
  251.                 if ( self.bShift ):
  252.                     CvScreensInterface.showUnVictoryScreen(())
  253.                     return 1
  254.                     
  255.         return 0
  256.  
  257.     def onInit(self, argsList):
  258.         'Called when Civ starts up'
  259.         CvUtil.pyPrint( 'OnInit' )
  260.         
  261.     def onUpdate(self, argsList):
  262.         'Called every frame'
  263.         fDeltaTime = argsList[0]
  264.  
  265.         # allow camera to be updated
  266.         CvCameraControls.g_CameraControls.onUpdate( fDeltaTime )
  267.         
  268.     def onWindowActivation(self, argsList):
  269.         'Called when the game window activates or deactivates'
  270.         bActive = argsList[0]
  271.         
  272.     def onUnInit(self, argsList):
  273.         'Called when Civ shuts down'
  274.         CvUtil.pyPrint('OnUnInit')
  275.     
  276.     def onPreSave(self, argsList):
  277.         "called before a game is actually saved"
  278.         CvUtil.pyPrint('OnPreSave')
  279.     
  280.     def onSaveGame(self, argsList):
  281.         "return the string to be saved - Must be a string"
  282.         return ""
  283.  
  284.     def onLoadGame(self, argsList):
  285.         return 0
  286.  
  287.     def onGameStart(self, argsList):
  288.         'Called at the start of the game'
  289.         if (gc.getGame().getGameTurnYear() == gc.getDefineINT("START_YEAR")):
  290.             for iPlayer in range(gc.getMAX_PLAYERS()):
  291.                 player = gc.getPlayer(iPlayer)
  292.                 if (player.isAlive() and player.isHuman()):
  293.                     popupInfo = CyPopupInfo()
  294.                     popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
  295.                     popupInfo.setText(u"showDawnOfMan")
  296.                     popupInfo.addPopup(iPlayer)
  297.  
  298.     def onGameEnd(self, argsList):
  299.         'Called at the End of the game'
  300.         #print("Game is ending")            
  301.         return
  302.  
  303.     def onBeginGameTurn(self, argsList):
  304.         'Called at the beginning of the end of each turn'
  305.         iGameTurn = argsList[0]
  306.         CvTopCivs.CvTopCivs().turnChecker(iGameTurn)
  307.  
  308.     def onEndGameTurn(self, argsList):
  309.         'Called at the end of the end of each turn'
  310.         iGameTurn = argsList[0]
  311.         
  312.     def onBeginPlayerTurn(self, argsList):
  313.         'Called at the beginning of a players turn'
  314.         iGameTurn, iPlayer = argsList
  315.  
  316.     def onEndPlayerTurn(self, argsList):
  317.         'Called at the end of a players turn'
  318.         iGameTurn, iPlayer = argsList
  319.         
  320.         if (gc.getGame().getElapsedGameTurns() == 1):
  321.             if (gc.getPlayer(iPlayer).isHuman()):
  322.                 if (gc.getPlayer(iPlayer).canRevolution(0)):
  323.                     popupInfo = CyPopupInfo()
  324.                     popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_CHANGECIVIC)
  325.                     popupInfo.addPopup(iPlayer)
  326.         
  327.         CvAdvisorUtils.resetAdvisorNags()
  328.         CvAdvisorUtils.endTurnFeats(iPlayer)
  329.  
  330.     def onEndTurnReady(self, argsList):
  331.         iGameTurn = argsList[0]
  332.  
  333.     def onFirstContact(self, argsList):
  334.         'Contact'
  335.         iTeamX,iHasMetTeamY = argsList
  336.         if (not self.__LOG_CONTACT):
  337.             return
  338.         CvUtil.pyPrint('Team %d has met Team %d' %(iTeamX, iHasMetTeamY))
  339.     
  340.     def onCombatResult(self, argsList):
  341.         'Combat Result'
  342.         pWinner,pLoser = argsList
  343.         playerX = PyPlayer(pWinner.getOwner())
  344.         unitX = PyInfo.UnitInfo(pWinner.getUnitType())
  345.         playerY = PyPlayer(pLoser.getOwner())
  346.         unitY = PyInfo.UnitInfo(pLoser.getUnitType())
  347.         if (not self.__LOG_COMBAT):
  348.             return
  349.         if playerX and playerX and unitX and playerY:
  350.             CvUtil.pyPrint('Player %d Civilization %s Unit %s has defeated Player %d Civilization %s Unit %s' 
  351.                 %(playerX.getID(), playerX.getCivilizationName(), unitX.getDescription(), 
  352.                 playerY.getID(), playerY.getCivilizationName(), unitY.getDescription()))
  353.  
  354.     def onCombatLogCalc(self, argsList):
  355.         'Combat Result'    
  356.         genericArgs = argsList[0][0]
  357.         cdAttacker = genericArgs[0]
  358.         cdDefender = genericArgs[1]
  359.         iDefenderOdds = genericArgs[2]
  360.         CvUtil.combatMessageBuilder(cdAttacker, cdDefender)
  361.         combatMessage = "%s: %.1f%%" %(localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFENDER_ODDS", ()),iDefenderOdds/10.0,)
  362.         CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
  363.         CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
  364.         
  365.     def onCombatLogHit(self, argsList):
  366.         'Combat Message'
  367.         global gCombatMessages, gCombatLog
  368.         genericArgs = argsList[0][0]
  369.         cdAttacker = genericArgs[0]
  370.         cdDefender = genericArgs[1]
  371.         iIsAttacker = genericArgs[2]
  372.         iDamage = genericArgs[3]
  373.         
  374.         if (iIsAttacker == 0):
  375.             combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (gc.getPlayer(cdDefender.eOwner).getNameKey(), cdDefender.sUnitName, iDamage, cdDefender.iCurrHitPoints, cdDefender.iMaxHitPoints))
  376.             CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
  377.             CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
  378.             if (cdDefender.iCurrHitPoints <= 0):
  379.                 combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (gc.getPlayer(cdAttacker.eOwner).getNameKey(), cdAttacker.sUnitName, gc.getPlayer(cdDefender.eOwner).getNameKey(), cdDefender.sUnitName))
  380.                 CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
  381.                 CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
  382.         elif (iIsAttacker == 1):
  383.             combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_HIT", (gc.getPlayer(cdAttacker.eOwner).getNameKey(), cdAttacker.sUnitName, iDamage, cdAttacker.iCurrHitPoints, cdAttacker.iMaxHitPoints))
  384.             CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
  385.             CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
  386.             if (cdAttacker.iCurrHitPoints <= 0):
  387.                 combatMessage = localText.getText("TXT_KEY_COMBAT_MESSAGE_DEFEATED", (gc.getPlayer(cdDefender.eOwner).getNameKey(), cdDefender.sUnitName, gc.getPlayer(cdAttacker.eOwner).getNameKey(), cdAttacker.sUnitName))
  388.                 CyInterface().addCombatMessage(cdAttacker.eOwner,combatMessage)
  389.                 CyInterface().addCombatMessage(cdDefender.eOwner,combatMessage)
  390.  
  391.     def onImprovementBuilt(self, argsList):
  392.         'Improvement Built'
  393.         iImprovement, iX, iY = argsList
  394.         if (not self.__LOG_IMPROVEMENT):
  395.             return
  396.         CvUtil.pyPrint('Improvement %s was built at %d, %d'
  397.             %(PyInfo.ImprovementInfo(iImprovement).getDescription(), iX, iY))
  398.  
  399.     def onRouteBuilt(self, argsList):
  400.         'Route Built'
  401.         iRoute, iX, iY = argsList
  402.         if (not self.__LOG_IMPROVEMENT):
  403.             return
  404.         CvUtil.pyPrint('Route %s was built at %d, %d'
  405.             %(gc.getRouteInfo(iRoute).getDescription(), iX, iY))
  406.  
  407.     def onPlotRevealed(self, argsList):
  408.         'Plot Revealed'
  409.         pPlot = argsList[0]
  410.         iTeam = argsList[1]
  411.  
  412.     def onBuildingBuilt(self, argsList):
  413.         'Building Completed'
  414.         pCity, iBuildingType = argsList
  415.         game = CyGame()
  416.         if ((not self.bMultiPlayer) and (pCity.getOwner() == CyGame().getActivePlayer()) and isWorldWonderClass(gc.getBuildingInfo(iBuildingType).getBuildingClassType())):
  417.             # If this is a wonder...
  418.             popupInfo = CyPopupInfo()
  419.             popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
  420.             popupInfo.setData1(iBuildingType)
  421.             popupInfo.setData2(pCity.getID())
  422.             popupInfo.setData3(0)
  423.             popupInfo.setText(u"showWonderMovie")
  424.             popupInfo.addPopup(pCity.getOwner())
  425.  
  426.         CvAdvisorUtils.buildingBuiltFeats(pCity, iBuildingType)
  427.  
  428.         if (not self.__LOG_BUILDING):
  429.             return
  430.         CvUtil.pyPrint('%s was finished by Player %d Civilization %s' 
  431.             %(PyInfo.BuildingInfo(iBuildingType).getDescription(), pCity.getOwner(), gc.getPlayer(pCity.getOwner()).getCivilizationDescription(0)))
  432.     
  433.     def onProjectBuilt(self, argsList):
  434.         'Project Completed'
  435.         pCity, iProjectType = argsList
  436.         game = CyGame()
  437.         if ((not self.bMultiPlayer) and (pCity.getOwner() == CyGame().getActivePlayer())):
  438.             popupInfo = CyPopupInfo()
  439.             popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
  440.             popupInfo.setData1(iProjectType)
  441.             popupInfo.setData2(pCity.getID())
  442.             popupInfo.setData3(2)
  443.             popupInfo.setText(u"showWonderMovie")
  444.             popupInfo.addPopup(pCity.getOwner())
  445.     
  446.     def onUnitMove(self, argsList):
  447.         'unit move'
  448.         pPlot,pUnit = argsList
  449.         player = PyPlayer(pUnit.getOwner())
  450.         unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())
  451.         if (not self.__LOG_MOVEMENT):
  452.             return
  453.         if player and unitInfo:
  454.             CvUtil.pyPrint('Player %d Civilization %s unit %s is moving to %d, %d' 
  455.                 %(player.getID(), player.getCivilizationName(), unitInfo.getDescription(), 
  456.                 pUnit.getX(), pUnit.getY()))
  457.  
  458.     def onUnitSetXY(self, argsList):
  459.         'units xy coords set manually'
  460.         pPlot,pUnit = argsList
  461.         player = PyPlayer(pUnit.getOwner())
  462.         unitInfo = PyInfo.UnitInfo(pUnit.getUnitType())
  463.         if (not self.__LOG_MOVEMENT):
  464.             return
  465.         
  466.     def onUnitCreated(self, argsList):
  467.         'Unit Completed'
  468.         unit = argsList[0]
  469.         player = PyPlayer(unit.getOwner())
  470.         if (not self.__LOG_UNITBUILD):
  471.             return
  472.  
  473.     def onUnitBuilt(self, argsList):
  474.         'Unit Completed'
  475.         city = argsList[0]
  476.         unit = argsList[1]
  477.         player = PyPlayer(city.getOwner())
  478.  
  479.         CvAdvisorUtils.unitBuiltFeats(city, unit)
  480.         
  481.         if (not self.__LOG_UNITBUILD):
  482.             return
  483.         CvUtil.pyPrint('%s was finished by Player %d Civilization %s' 
  484.             %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))
  485.     
  486.     def onUnitKilled(self, argsList):
  487.         'Unit Killed'
  488.         unit, iAttacker = argsList
  489.         player = PyPlayer(unit.getOwner())
  490.         attacker = PyPlayer(iAttacker)
  491.         if (not self.__LOG_UNITKILLED):
  492.             return
  493.         CvUtil.pyPrint('Player %d Civilization %s Unit %s was killed by Player %d' 
  494.             %(player.getID(), player.getCivilizationName(), PyInfo.UnitInfo(unit.getUnitType()).getDescription(), attacker.getID()))
  495.  
  496.     def onUnitLost(self, argsList):
  497.         'Unit Lost'
  498.         unit = argsList[0]
  499.         player = PyPlayer(unit.getOwner())
  500.         if (not self.__LOG_UNITLOST):
  501.             return
  502.         CvUtil.pyPrint('%s was lost by Player %d Civilization %s' 
  503.             %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))
  504.     
  505.     def onUnitPromoted(self, argsList):
  506.         'Unit Promoted'
  507.         pUnit, iPromotion = argsList
  508.         player = PyPlayer(pUnit.getOwner())
  509.         if (not self.__LOG_UNITPROMOTED):
  510.             return
  511.         CvUtil.pyPrint('Unit Promotion Event: %s - %s' %(player.getCivilizationName(), pUnit.getName(),))
  512.     
  513.     def onUnitSelected(self, argsList):
  514.         'Unit Selected'
  515.         unit = argsList[0]
  516.         player = PyPlayer(unit.getOwner())
  517.         if (not self.__LOG_UNITSELECTED):
  518.             return
  519.         CvUtil.pyPrint('%s was selected by Player %d Civilization %s' 
  520.             %(PyInfo.UnitInfo(unit.getUnitType()).getDescription(), player.getID(), player.getCivilizationName()))
  521.     
  522.     def onUnitRename(self, argsList):
  523.         'Unit is renamed'
  524.         pUnit = argsList[0]
  525.         if (pUnit.getOwner() == CyGame().getActivePlayer()):
  526.             self.__eventEditUnitNameBegin(pUnit)
  527.  
  528.     def onGoodyReceived(self, argsList):
  529.         'Goody received'
  530.         iPlayer, pPlot, pUnit, iGoodyType = argsList
  531.         if (not self.__LOG_GOODYRECEIVED):
  532.             return
  533.         CvUtil.pyPrint('%s received a goody' %(gc.getPlayer(iPlayer).getCivilizationDescription(0)),)
  534.     
  535.     def onGreatPersonBorn(self, argsList):
  536.         'Unit Promoted'
  537.         pUnit, iPlayer, pCity = argsList
  538.         player = PyPlayer(iPlayer)
  539.         if pUnit.isNone() or pCity.isNone():
  540.             return
  541.         if (not self.__LOG_GREATPERSON):
  542.             return
  543.         CvUtil.pyPrint('A %s was born for %s in %s' %(pUnit.getName(), player.getCivilizationName(), pCity.getName()))
  544.     
  545.     def onTechAcquired(self, argsList):
  546.         'Tech Acquired'
  547.         iTechType, iTeam, iPlayer, bAnnounce = argsList
  548.         # Note that iPlayer may be NULL (-1) and not a refer to a player object
  549.         
  550.         # Show tech splash when applicable
  551.         if (iPlayer > -1 and bAnnounce):
  552.             if (gc.getGame().isFinalInitialized() and not gc.getGame().GetWorldBuilder2Mode()):
  553.                 if ((not self.bMultiPlayer) and (iPlayer == CyGame().getActivePlayer())):
  554.                     popupInfo = CyPopupInfo()
  555.                     popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
  556.                     popupInfo.setData1(iTechType)
  557.                     popupInfo.setText(u"showTechSplash")
  558.                     popupInfo.addPopup(iPlayer)
  559.                 
  560.         if (not self.__LOG_TECH):
  561.             return
  562.         CvUtil.pyPrint('%s was finished by Team %d' 
  563.             %(PyInfo.TechnologyInfo(iTechType).getDescription(), iTeam))
  564.     
  565.     def onTechSelected(self, argsList):
  566.         'Tech Selected'
  567.         iTechType, iPlayer = argsList
  568.         if (not self.__LOG_TECH):
  569.             return
  570.         CvUtil.pyPrint('%s was selected by Player %d' %(PyInfo.TechnologyInfo(iTechType).getDescription(), iPlayer))
  571.     
  572.     def onReligionFounded(self, argsList):
  573.         'Religion Founded'
  574.         iReligion, iFounder = argsList
  575.         player = PyPlayer(iFounder)
  576.         
  577.         iCityId = gc.getGame().getHolyCity(iReligion).getID()
  578.         if (gc.getGame().isFinalInitialized() and not gc.getGame().GetWorldBuilder2Mode()):
  579.             if ((not self.bMultiPlayer) and (iFounder == CyGame().getActivePlayer())):
  580.                 popupInfo = CyPopupInfo()
  581.                 popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
  582.                 popupInfo.setData1(iReligion)
  583.                 popupInfo.setData2(iCityId)
  584.                 popupInfo.setData3(1)
  585.                 popupInfo.setText(u"showWonderMovie")
  586.                 popupInfo.addPopup(iFounder)
  587.         
  588.         if (not self.__LOG_RELIGION):
  589.             return
  590.         CvUtil.pyPrint('Player %d Civilization %s has founded %s'
  591.             %(iFounder, player.getCivilizationName(), gc.getReligionInfo(iReligion).getDescription()))
  592.  
  593.     def onReligionSpread(self, argsList):
  594.         'Religion Has Spread to a City'
  595.         iReligion, iOwner, pSpreadCity = argsList
  596.         player = PyPlayer(iOwner)
  597.         if (not self.__LOG_RELIGIONSPREAD):
  598.             return
  599.         CvUtil.pyPrint('%s has spread to Player %d Civilization %s city of %s'
  600.             %(gc.getReligionInfo(iReligion).getDescription(), iOwner, player.getCivilizationName(), pSpreadCity.getName()))
  601.                 
  602.     def onGoldenAge(self, argsList):
  603.         'Golden Age'
  604.         iPlayer = argsList[0]
  605.         player = PyPlayer(iPlayer)
  606.         if (not self.__LOG_GOLDENAGE):
  607.             return
  608.         CvUtil.pyPrint('Player %d Civilization %s has begun a golden age'
  609.             %(iPlayer, player.getCivilizationName()))
  610.  
  611.     def onEndGoldenAge(self, argsList):
  612.         'End Golden Age'
  613.         iPlayer = argsList[0]
  614.         player = PyPlayer(iPlayer)
  615.         if (not self.__LOG_ENDGOLDENAGE):
  616.             return
  617.         CvUtil.pyPrint('Player %d Civilization %s golden age has ended'
  618.             %(iPlayer, player.getCivilizationName()))
  619.  
  620.     def onChangeWar(self, argsList):
  621.         'War Status Changes'
  622.         bIsWar = argsList[0]
  623.         iPlayer = argsList[1]
  624.         iRivalTeam = argsList[2]
  625.         if (not self.__LOG_WARPEACE):
  626.             return
  627.         if (bIsWar):
  628.             strStatus = "declared war"
  629.         else:
  630.             strStatus = "declared peace"
  631.         CvUtil.pyPrint('Player %d Civilization %s has %s on Team %d'
  632.             %(iPlayer, gc.getPlayer(iPlayer).getCivilizationName(), strStatus, eRivalTeam))
  633.     
  634.     def onChat(self, argsList):
  635.         'Chat Message Event'
  636.         chatMessage = "%s" %(argsList[0],)
  637.         
  638.     def onSetPlayerAlive(self, argsList):
  639.         'Set Player Alive Event'
  640.         iPlayerID = argsList[0]
  641.         bNewValue = argsList[1]
  642.         CvUtil.pyPrint("Player %d's alive status set to: %d" %(iPlayerID, int(bNewValue)))
  643.         
  644.     def onCityBuilt(self, argsList):
  645.         'City Built'
  646.         city = argsList[0]
  647.         if (city.getOwner() == CyGame().getActivePlayer()):
  648.             self.__eventEditCityNameBegin(city)    
  649.         CvUtil.pyPrint('City Built Event: %s' %(city.getName()))
  650.         
  651.     def onCityRazed(self, argsList):
  652.         'City Razed'
  653.         city, iPlayer = argsList
  654.         
  655.         owner = PyPlayer(city.getOwner())
  656.         razor = PyPlayer(iPlayer)
  657.         CvUtil.pyPrint('Player %d Civilization %s City %s was razed by Player %d' 
  658.             %(owner.getID(), owner.getCivilizationName(), city.getName(), razor.getID()))
  659.         CvUtil.pyPrint("City Razed Event: %s" %(city.getName(),))
  660.     
  661.     def onCityAcquired(self, argsList):
  662.         'City Acquired'
  663.         owner,playerType,city,bConquest,bTrade = argsList
  664.         CvUtil.pyPrint('City Acquired Event: %s' %(city.getName()))
  665.     
  666.     def onCityLost(self, argsList):
  667.         'City Lost'
  668.         city = argsList[0]
  669.         player = PyPlayer(city.getOwner())
  670.         if (not self.__LOG_CITYLOST):
  671.             return
  672.         CvUtil.pyPrint('City %s was lost by Player %d Civilization %s' 
  673.             %(city.getName(), player.getID(), player.getCivilizationName()))
  674.     
  675.     def onCultureExpansion(self, argsList):
  676.         'City Culture Expansion'
  677.         pCity = argsList[0]
  678.         iPlayer = argsList[1]
  679.         CvUtil.pyPrint("City %s's culture has expanded" %(pCity.getName(),))
  680.     
  681.     def onCityGrowth(self, argsList):
  682.         'City Population Growth'
  683.         pCity = argsList[0]
  684.         iPlayer = argsList[1]
  685.         CvUtil.pyPrint("%s has grown" %(pCity.getName(),))
  686.     
  687.     def onCityDoTurn(self, argsList):
  688.         'City Production'
  689.         pCity = argsList[0]
  690.         iPlayer = argsList[1]
  691.  
  692.         CvAdvisorUtils.cityAdvise(pCity, iPlayer)
  693.     
  694.     def onCityBuildingUnit(self, argsList):
  695.         'City begins building a unit'
  696.         pCity = argsList[0]
  697.         iUnitType = argsList[1]
  698.         if (not self.__LOG_CITYBUILDING):
  699.             return
  700.         CvUtil.pyPrint("%s has begun building a %s" %(pCity.getName(),gc.getUnitInfo(iUnitType).getDescription()))
  701.     
  702.     def onCityBuildingBuilding(self, argsList):
  703.         'City begins building a Building'
  704.         pCity = argsList[0]
  705.         iBuildingType = argsList[1]
  706.         if (not self.__LOG_CITYBUILDING):
  707.             return
  708.         CvUtil.pyPrint("%s has begun building a %s" %(pCity.getName(),gc.getBuildingInfo(iBuildingType).getDescription()))
  709.     
  710.     def onCityRename(self, argsList):
  711.         'City is renamed'
  712.         pCity = argsList[0]
  713.         if (pCity.getOwner() == CyGame().getActivePlayer()):
  714.             self.__eventEditCityNameBegin(pCity)    
  715.  
  716.     def onVictory(self, argsList):
  717.         'Victory'
  718.         iTeam, iVictory = argsList
  719.         if (iVictory >= 0 and iVictory < gc.getNumVictoryInfos()):
  720.             victoryInfo = gc.getVictoryInfo(int(iVictory))
  721.             CvUtil.pyPrint("Victory!  Team %d achieves a %s victory"
  722.                 %(iTeam, victoryInfo.getDescription()))
  723.     
  724.     def onGameUpdate(self, argsList):
  725.         'sample generic event, called on each game turn slice'
  726.         genericArgs = argsList[0][0]    # tuple of tuple of my args
  727.         turnSlice = genericArgs[0]
  728.     
  729.     def onMouseEvent(self, argsList):
  730.         'mouse handler - returns 1 if the event was consumed'
  731.         eventType,mx,my,px,py,interfaceConsumed,screens = argsList
  732.         print "onMouseEvent"
  733.         print eventType
  734.         if ( px!=-1 and py!=-1 ):
  735.             if ( eventType == self.EventLButtonDown ):
  736.                 if (self.bAllowCheats and self.bCtrl and self.bAlt and CyMap().plot(px,py).isCity() and not interfaceConsumed):
  737.                     # Launch Edit City Event
  738.                     self.beginEvent( CvUtil.EventEditCity, (px,py) )
  739.                     return 1
  740.                 
  741.                 elif (self.bAllowCheats and self.bCtrl and self.bShift and not interfaceConsumed):
  742.                     # Launch Place Object Event
  743.                     self.beginEvent( CvUtil.EventPlaceObject, (px, py) )
  744.                     return 1
  745.             
  746.         if ( eventType == self.EventBack ):
  747.             return CvScreensInterface.handleBack(screens)
  748.         elif ( eventType == self.EventForward ):
  749.             return CvScreensInterface.handleForward(screens)
  750.         
  751.         return 0
  752.         
  753.  
  754. #################### TRIGGERED EVENTS ##################    
  755.     def __eventEditCityNameBegin(self, city):
  756.         popup = PyPopup.PyPopup(CvUtil.EventEditCityName, EventContextTypes.EVENTCONTEXT_ALL)
  757.         popup.setUserData((city.getID(),))
  758.         popup.setHeaderString(localText.getText("TXT_KEY_NAME_CITY", ()))
  759.         popup.setBodyString(localText.getText("TXT_KEY_SETTLE_NEW_CITY_NAME", ()))
  760.         popup.createEditBox(city.getName())
  761.         popup.setEditBoxMaxCharCount( 15 )
  762.         popup.launch()
  763.     
  764.     def __eventEditCityNameApply(self, playerID, userData, popupReturn):    
  765.         'Edit City Name Event'
  766.         iCityID = userData[0]
  767.         player = gc.getPlayer(playerID)
  768.         city = player.getCity(iCityID)
  769.         cityName = popupReturn.getEditBoxString(0)
  770.         if (len(cityName) > 30):
  771.             cityName = cityName[:30]
  772.         city.setName(cityName, false)
  773.  
  774.     def __eventEditCityBegin(self, argsList):
  775.         'Edit City Event'
  776.         px,py = argsList
  777.         CvWBPopups.CvWBPopups().initEditCity(argsList)
  778.     
  779.     def __eventEditCityApply(self, playerID, userData, popupReturn):
  780.         'Edit City Event Apply'
  781.         if (getChtLvl() > 0):
  782.             CvWBPopups.CvWBPopups().applyEditCity( (popupReturn, userData) )
  783.  
  784.     def __eventPlaceObjectBegin(self, argsList):
  785.         'Place Object Event'
  786.         CvDebugTools.CvDebugTools().initUnitPicker(argsList)
  787.     
  788.     def __eventPlaceObjectApply(self, playerID, userData, popupReturn):
  789.         'Place Object Event Apply'
  790.         if (getChtLvl() > 0):
  791.             CvDebugTools.CvDebugTools().applyUnitPicker( (popupReturn, userData) )
  792.  
  793.     def __eventAwardTechsAndGoldBegin(self, argsList):
  794.         'Award Techs & Gold Event'
  795.         CvDebugTools.CvDebugTools().cheatTechs()
  796.     
  797.     def __eventAwardTechsAndGoldApply(self, playerID, netUserData, popupReturn):
  798.         'Award Techs & Gold Event Apply'
  799.         if (getChtLvl() > 0):
  800.             CvDebugTools.CvDebugTools().applyTechCheat( (popupReturn) )
  801.     
  802.     def __eventEditUnitNameBegin(self, argsList):
  803.         pUnit = argsList
  804.         popup = PyPopup.PyPopup(CvUtil.EventEditUnitName, EventContextTypes.EVENTCONTEXT_ALL)
  805.         popup.setUserData((pUnit.getID(),))
  806.         popup.setBodyString(localText.getText("TXT_KEY_RENAME_UNIT", ()))
  807.         popup.createEditBox(pUnit.getNameNoDesc())
  808.         popup.launch()
  809.  
  810.     def __eventEditUnitNameApply(self, playerID, userData, popupReturn):    
  811.         'Edit Unit Name Event'
  812.         iUnitID = userData[0]
  813.         unit = gc.getPlayer(playerID).getUnit(iUnitID)
  814.         newName = popupReturn.getEditBoxString(0)
  815.         if (len(newName) > 25):
  816.             newName = newName[:25]            
  817.         unit.setName(newName)
  818.  
  819.     def __eventWBAllPlotsPopupBegin(self, argsList):
  820.         CvScreensInterface.getWorldBuilderScreen().allPlotsCB()
  821.         return
  822.     def __eventWBAllPlotsPopupApply(self, playerID, userData, popupReturn):
  823.         if (popupReturn.getButtonClicked() >= 0):
  824.             CvScreensInterface.getWorldBuilderScreen().handleAllPlotsCB(popupReturn)
  825.         return
  826.  
  827.     def __eventWBLandmarkPopupBegin(self, argsList):
  828.         popup = PyPopup.PyPopup(CvUtil.EventWBLandmarkPopup, EventContextTypes.EVENTCONTEXT_ALL)
  829.         popup.createEditBox(localText.getText("TXT_KEY_WB_LANDMARK_START", ()))
  830.         popup.launch()
  831.         return
  832.  
  833.     def __eventWBLandmarkPopupApply(self, playerID, userData, popupReturn):
  834.         if (popupReturn.getEditBoxString(0)):
  835.             szLandmark = popupReturn.getEditBoxString(0)
  836.             if (len(szLandmark)):
  837.                 CvScreensInterface.getWorldBuilderScreen().setLandmarkCB(szLandmark)
  838.         return
  839.  
  840.     def __eventWBScriptPopupBegin(self, argsList):
  841.         popup = PyPopup.PyPopup(CvUtil.EventWBScriptPopup, EventContextTypes.EVENTCONTEXT_ALL)
  842.         popup.setHeaderString(localText.getText("TXT_KEY_WB_SCRIPT", ()))
  843.         popup.createEditBox(CvScreensInterface.getWorldBuilderScreen().getCurrentScript())
  844.         popup.launch()
  845.         return
  846.  
  847.     def __eventWBScriptPopupApply(self, playerID, userData, popupReturn):
  848.         if (popupReturn.getEditBoxString(0)):
  849.             szScriptName = popupReturn.getEditBoxString(0)
  850.             CvScreensInterface.getWorldBuilderScreen().setScriptCB(szScriptName)
  851.         return
  852.  
  853.     def __eventWBStartYearPopupBegin(self, argsList):
  854.         popup = PyPopup.PyPopup(CvUtil.EventWBStartYearPopup, EventContextTypes.EVENTCONTEXT_ALL)
  855.         popup.createSpinBox(0, "", gc.getGame().getStartYear(), 1, 5000, -5000)
  856.         popup.launch()
  857.         return
  858.  
  859.     def __eventWBStartYearPopupApply(self, playerID, userData, popupReturn):
  860.         iStartYear = popupReturn.getSpinnerWidgetValue(int(0))
  861.         print iStartYear
  862.         CvScreensInterface.getWorldBuilderScreen().setStartYearCB(iStartYear)
  863.         return
  864.