### Helper functions for Jamie's Rome Mod
from CvPythonExtensions import *
from CivPlayer import *
from eNums import *
from Popup import PyPopup
Interface = CyInterface()
Translator = CyTranslator()
Map = CyMap()
iNumMajorPlayers = 8
pHumanCiv = instance(Game.getActivePlayer())
eBarbarian = gc.getBARBARIAN_PLAYER()
pBarbarianCiv = CivPlayer(eBarbarian)
def showPopup(popupHeader, popupMessage):
"""
Launches a pop-up window with the header defined by the popupHeader (string)
argument and the text message defined by the popupMessage (string) argument.
"""
modPopup = PyPopup()
modPopup.setHeaderString(Translator.getText(popupHeader, ()))
modPopup.setBodyString(Translator.getText(popupMessage, ()))
modPopup.launch()
def isDate(iDate):
"""
Returns True if the current game date is the first game turn past the iDate
(integer) date. Otherwise returns False.
"""
return CyGame().getTurnYear(CyGame().getGameTurn() +1) > iDate and CyGame().getGameTurnYear() <= iDate <= iDate
def spawnUnits(ePlayer, eUnitType, tCoords, iNum):
"""
Initiates iNum (integer) number of CyUnit instances of the eUnitType (integer)
UnitType for the ePlayer (integer) PlayerType at the CyPlot defined by the pair of
integer coordinates contained in tCoords (tuple).
"""
iX, iY = tCoords
CivPlayer(ePlayer).get(PyPlayer).initUnit(eUnitType, iX, iY, iNum)
def isPlotOwner(tCoords, ePlayer):
"""
Returns True if ePlayer is the owner of the plot at tCoords. Otherwise returns
False. To check if the plot has no owner (at all) the ePlayer value should be -1.
"""
pPlot = getPlot(tCoords)
eOwner = pPlot.getOwner()
return eOwner == ePlayer
#Note to self: to ask the function if the plot is not owned use isPlotOwner(tCoords, -1) and True is for not owned
#Subsequently if you use: if not isPlotOwner(tCoords, -1) you are asking the function whether ANYBODY owns the plot!
##def isPlotNotOwned(tCoords):
## """
## Returns True if Plot is not owned by anybody (made by J_mie6) if plot is owned returns False
## """
## pPlot = getPlot(tCoords)
## eOwner = pPlot.getOwner()
## return eOwner != eHellenicRebels or eRome or eGreece or eGaul or eBrittania or eCarthage or eEgypt or eGermania or ePictia or eItalianRebels or eNubianRebels or eNumidianRebels or eGallicRebels or eBrythonRebels or eGermanicRebels or ePictishRebels or eIberia or eGreekColonies
## #Mostly the same as above just different method!
def checkCityProximity(tCoords, iDistance=4):
"""
Returns False if any city is within iDistance from the plot at tCoords. Otherwise
returns True.
"""
pPlot = getPlot(tCoords)
iX = pPlot.getX()
iY = pPlot.getY()
for iCurrentX in range(iX - iDistance, iX + iDistance + 1):
for iCurrentY in range(iY - iDistance, iY + iDistance + 1):
pCurrentPlot = Map.plot(iCurrentX, iCurrentY)
if pCurrentPlot.isCity():
return False
return True
def spawnCity(tCoords, ePlayer, name=None, iSize=2):
"""
Creates a new CyCity instance at tCoords belonging to ePlayer, of iSize and with
the optional name.
"""
iX, iY = tCoords
pPlayer = pPlayer = gc.getPlayer(ePlayer)
pCity = pPlayer.initCity(iX, iY)
pCity.setPopulation(iSize)
if name != None:
pCity.setName(name, True)
def getPlot(tCoords):
"""
Returns the CyPlot instance of the plot at tCoords.
"""
iX, iY = tCoords
return Map.plot(iX, iY)
def getCoords(pInstance):
"""
Used for fetching the map plot coordinates of any CyPlot or CyCity object, supplied
as the pInstance argument. The coordinates are returned as two integer values
wrapped up in a tuple.
"""
return (pInstance.getX(), pInstance.getY())
def isPlayerCity(pCity, ePlayer):
"""
Returns True if pCity (CyCity object) is owned by ePlayer (integer).
"""
return pCity.getOwner() == ePlayer
def getPlayerAreaCities(tCoords1, tCoords2, ePlayer):
"""
Returns a list of all CyCity instances corresponding to cities belonging to ePlayer
inside the map area defined by tCoords1 and tCoords2.
"""
lPlayerAreaCities = list()
for pCity in findAreaCities(tCoords1, tCoords2):
if isPlayerCity(pCity, ePlayer):
lPlayerAreaCities.append(pCity)
return lPlayerAreaCities
def findAreaCities(tCoords1, tCoords2):
"""
Used for looping. When supplied with the map coordinates of a area defined by its
lower left corner (tCoords1) and its upper right corner (tCoords2), the function
yields all cities (CyCity instances) in order.
"""
iX1, iY1 = tCoords1
iX2, iY2 = tCoords2
for iX in range(iX1, iX2 + 1):
for iY in range(iY1, iY2 + 1):
pPlot = Map.plot(iX, iY)
if pPlot.isCity():
yield pPlot.getPlotCity()
def getHighestCityCulturalRival(pCity):
"""
Returns the PlayerType (integer) of the rival with the highest amount of cultural
points in pCity (CyCity object).
"""
eOwner = pCity.getOwner()
eCulturalOwner = pCity.findHighestCulture()
if eCulturalOwner != eOwner:
return eCulturalOwner
else:
tCurrentLeader = -1, 0
for ePlayer in CivPlayer.getPlayers(index=playerID):
if ePlayer == eOwner or ePlayer == eCulturalOwner: continue
iCulture = pCity.getCulture(ePlayer)
if iCulture > tCurrentLeader[1]:
tCurrentLeader = ePlayer, iCulture
return tCurrentLeader[0]
def addMessage(tag, tValues, eColor, tCoords=(-1, -1)):
"""
Adds the tag (string) message to the game in eColor. (The preset pointers to valid
ColorTypes are eRed, eGreen and eWhite. A None value will disable the message.) If
the tag argument requires other values to be included in the message, then these
can be supplied with the tValues (tuple) argument. The optional tCoords (tuple)
argument is set to a pair of default -1 values that indicate that no arrow should
accompany the text message. (Valid map tile coordinates will enable the arrow.)
"""
if eColor == None: return
message = Translator.getText(tag, tValues)
bArrow = tCoords != (-1, -1)
Interface.addMessage(pHumanCiv.get(playerID), True, 20, message, "", eMinorEvent, "", eColor, tCoords[0], tCoords[1], bArrow, bArrow)
def getColor(bHuman, bAI, bOther=None):
"""
Used to translate the arguments (booleans) into one of the 3 preset ColorTypes values.
The booleans are evaluated in order and the first one pointing to a True value will
cause the function to return the corresponding ColorType. If all arguments are False
then the fuction returns a None value.
"""
if bHuman:
return eGreen
elif bAI:
return eRed
elif bOther:
return eWhite
def isChance(iProbability):
"""
Used to resolve random checks where the iProbability argument is a integer value
between 1 and 100. The probability is this value in 100 and the function returns True
or False depending on the outcome.
"""
return getRandNum(100, "isChance()") < iProbability
def getRandNum(iRange, debug="getRandNum()"):
"""
Returns a random number (integer) between 0 (zero) and iRange - 1. The debug (string)
argument is optional.
"""
return Game.getSorenRandNum(iRange, debug)
def getUnitClass(pUnit):
"""
Returns the UnitClassType (integer) of the UnitType of the CyUnit instance referenced
by the pUnit argument.
"""
return gc.getUnitInfo(pUnit.getUnitType()).getUnitClassType()
def isSpawnValid(pPlot):
""""
Returns False if the CyPlot instance referenced by the pPlot argument is a water or
a mountain tile. Otherwise returns True.
"""
return not ( pPlot.isWater() or pPlot.isPeak() )
def getRandomCoords(lPlots):
"""
Supplied with a list array (the lPlots argument) with CyPlot instances the function
will return a tuple containing the integer map coordinates of one randomly selected
plot.
"""
iRandNum = getRandNum(len(lPlots), "random plot")
pPlot = lPlots[iRandNum]
return getCoords(pPlot)
def cityKnownByHuman(pCity):
"""
Returns True if the CyCity instance referenced by the pCity argument is known to
the human player. Otherwise returns False.
"""
return pCity.isRevealed(pHumanCiv.get(teamID), False)
def getDistanceToCapital(pInstance, pPlayer):
"""
Returns a integer value corresponding to the distance (in map plots) between the
CyCity or CyPlot instance referenced by the pInstance argument and the capital of
the player referenced by the pPlayer (CyPlayer object) argument.
"""
iCityX, iCityY = getCoords(pInstance)
iCapitalX, iCapitalY = getCoords(pPlayer.getCapitalCity())
return max(abs(iCityX - iCapitalX), abs(iCityY - iCapitalY))
def getCityUnits(pCity):
"""
Returns a list array of all units in pCity (CyCity object) belonging to the city's
owner.
"""
eOwner = pCity.getOwner()
pPlot = pCity.plot()
lUnits = list()
for pUnit in (pPlot.getUnit(iUnit) for iUnit in range(pPlot.getNumUnits())):
if pUnit.getOwner() != eOwner: continue
lUnits.append(pUnit)
return lUnits
def setCapital(pCity):
"""
Spawns the Palace National Wonder in pCity (CyCity object) - thus making it the
capital.
"""
pCity.setNumRealBuilding(ePalace, 1)
def setAtWar(pCivPlayer1, pCivPlayer2):
"""
Makes the team of the player referenced by the pCivPlayer1 (CivPlayer object)
argument declare war on the team of the player referenced by the pCivPlayer2
(CivPlayer object) argument.
"""
pTeam1 = pCivPlayer1.get(CyTeam)
team2ID = pCivPlayer2.get(teamID)
if not pTeam1.isAtWar(team2ID):
pTeam1.declareWar(team2ID, True, -1)