CivPlayer is a Python module and a Python scripting utility application. Download here.
What does it do?
CivPlayer has two main features:
How does it work?
CivPlayer is self-contained in the CivPlayer.py file and there are three things you need to do in order to access the included features:
What are the player/team values, then?
In CivIV each player is a CvPlayer instance and those objects are inherited by the Python scripting environment as CyPlayer instances. In order to do just about anything concerning the players with Python, you need to fetch the CyPlayer instance. So you end up doing this often. Very often. Other times you need the player ID - also known at the PlayerType - for various tasks indirectly affecting a player. So you need to get that value also on-the-fly.
Now, this isn't all there is to handling players. Each player also has a Civilization attribute associated with it. And every player is part of a team - these are known as CvTeam instances or CyTeam instances in Python terms. The same goes here; sometimes you need to get hold of the player's CyTeam object, and other times you need to fetch the teamID/TeamType.
CivPlayer wraps all of this up in a easy-to-use package using something called CivPlayer instances. A CivPlayer is basically a collection of all the values and objects associated with one of game's players or Civilizations. As a bonus, the CivPlayer instance also holds a PyPlayer instance for easy-access. PyPlayer is "helper class" found in the PyHelpers module that ships with the game, and it adds some very useful methods not otherwise available with CivIV Python.
What is the advantage of a CivPlayer instance over a CyPlayer and a CyTeam instance?
It's a matter of convenience as it saves time and results in shorter scripts with less repetition. Because even if you wrap up the main methods of fetching the associated instances and index values, you end up calling these functions over and over again.
With a CivPlayer instance, you can just invoke the CivPlayer.get() method for fetching any value on-the-fly. And there are numerous different and convenient ways of getting hold of the CivPlayer instance itself.
Example of use
This is an example of a Python script that makes one player declare war on another. To make it even more delicate, we need to figure out what player has the CivilizationType.CIVILIZATION_RUSSIA. (We're gonna assume that there is only one player with this CivilizationType in the scenario, and that this player is currently alive.)
Installation
What does it do?
CivPlayer has two main features:
1. Making handling various player and team values and instances easy to handle.
2. Has built-in support for storing custom data - both player specific and global data.
2. Has built-in support for storing custom data - both player specific and global data.
How does it work?
CivPlayer is self-contained in the CivPlayer.py file and there are three things you need to do in order to access the included features:
1. Download and install the module in the \Assets\Python\ folder of your mod.
2. Initiate the setup code by adding function calls from the mod's Python event managing structure.
3. Import the module to any other module using it.
More details in the installation section.2. Initiate the setup code by adding function calls from the mod's Python event managing structure.
3. Import the module to any other module using it.
What are the player/team values, then?
In CivIV each player is a CvPlayer instance and those objects are inherited by the Python scripting environment as CyPlayer instances. In order to do just about anything concerning the players with Python, you need to fetch the CyPlayer instance. So you end up doing this often. Very often. Other times you need the player ID - also known at the PlayerType - for various tasks indirectly affecting a player. So you need to get that value also on-the-fly.
Now, this isn't all there is to handling players. Each player also has a Civilization attribute associated with it. And every player is part of a team - these are known as CvTeam instances or CyTeam instances in Python terms. The same goes here; sometimes you need to get hold of the player's CyTeam object, and other times you need to fetch the teamID/TeamType.
CivPlayer wraps all of this up in a easy-to-use package using something called CivPlayer instances. A CivPlayer is basically a collection of all the values and objects associated with one of game's players or Civilizations. As a bonus, the CivPlayer instance also holds a PyPlayer instance for easy-access. PyPlayer is "helper class" found in the PyHelpers module that ships with the game, and it adds some very useful methods not otherwise available with CivIV Python.
What is the advantage of a CivPlayer instance over a CyPlayer and a CyTeam instance?
It's a matter of convenience as it saves time and results in shorter scripts with less repetition. Because even if you wrap up the main methods of fetching the associated instances and index values, you end up calling these functions over and over again.
With a CivPlayer instance, you can just invoke the CivPlayer.get() method for fetching any value on-the-fly. And there are numerous different and convenient ways of getting hold of the CivPlayer instance itself.
Example of use
This is an example of a Python script that makes one player declare war on another. To make it even more delicate, we need to figure out what player has the CivilizationType.CIVILIZATION_RUSSIA. (We're gonna assume that there is only one player with this CivilizationType in the scenario, and that this player is currently alive.)
Spoiler :
Doing this the old-fashioned way would look something like this:
Phew! It would be possible to condence this script into fewer lines of code, but the logic would remain the same and the resulting script would be less readable.
Now, using CivPlayer it would be as easy as this to achieve the same thing:
This example uses the so-called Quick-Access Interface for fetching values, so there are no actual CivPlayer instances directly referenced in the code. The following script is another example where actual CivPlayer instances are assigned to local names:
Code:
def declareOnRussia(ePlayer):
# get the CyTeam of the attacker
pPlayer = gc.getPlayer(ePlayer)
eTeam = pPlayer.getTeam()
pTeam = gc.getTeam(eTeam)
# get the TeamType of the Russian player
eCivRussia = gc.getInfoTypeForString("CIVILIZATION_RUSSIA")
for eCurrentPlayer in gc.getMAX_CIV_PLAYERS():
pCurrentPlayer = gc.getPlayer(eCurrentPlayer)
if pCurrentPlayer.getCivilizationType() == eCivRussia:
ePlayerRussia = eCurrentPlayer
break
pPlayerRussia = gc.getPlayer(ePlayerRussia)
eTeamRussia = pPlayerRussia.getTeam()
# declare war
pTeam.declareWar(eTeamRussia, False, WarPlayTypes.NO_WARPLAN)
Now, using CivPlayer it would be as easy as this to achieve the same thing:
Code:
def declareOnRussia(ePlayer):
# get the CyTeam of the attacker
pTeam = getCyTeam(ePlayer)
# get the TeamType of the Russian player
eTeamRussia = pointer("Russia", teamID)
# declare war
pTeam.declareWar(eTeamRussia, False, WarPlayTypes.NO_WARPLAN)
Code:
def declareOnRussia(ePlayer):
# get the CyTeam of the attacker
pAttacker = instance(ePlayer)
pTeam = pAttacker.get(CyTeam)
# get the TeamType of the Russian player
pRussia = Civ("Russia")
eTeam = pRussia.get(teamID)
# declare war
pTeam.declareWar(eTeam, False, WarPlayTypes.NO_WARPLAN)
Installation
1. Download and unzip the CivPlayer.py file into your mod's \Assets\Python\ folder.
2a. With a standard Beyond the Sword setup, add the color coded code into the CvEventManager.py file:
2b. If you want to be able to use the built-in data storage features, also add these function calls:
3. Add this line into any module where you wanna be able to access the features of CivPlayer:
2a. With a standard Beyond the Sword setup, add the color coded code into the CvEventManager.py file:
Spoiler :
Code:
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
[COLOR="Red"]import CivPlayer[/COLOR]
gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo
Code:
def onLoadGame(self, argsList):
CvAdvisorUtils.resetNoLiberateCities()
[COLOR="Red"]CivPlayer.setupCivPlayer()[/COLOR]
return 0
Code:
def onGameStart(self, argsList):
'Called at the start of the game'
[COLOR="Red"]CivPlayer.setupCivPlayer()[/COLOR]
Spoiler :
Code:
def onPreSave(self, argsList):
"called before a game is actually saved"
[COLOR="Red"]CivPlayer.storeData()[/COLOR]
CvUtil.pyPrint('OnPreSave')
Code:
def onLoadGame(self, argsList):
CvAdvisorUtils.resetNoLiberateCities()
CivPlayer.setupCivPlayer()
[COLOR="Red"]CivPlayer.retrieveData()[/COLOR]
return 0
Spoiler :
Code:
[COLOR="Red"]from CivPlayer import *[/COLOR]