from Rebellion import CivilWar
from HistoricalCities import *
pCivRome = instance("Rome")
flipPopupMessage = "TXT_SENATE_FLIP_POPUP_MESSAGE"
colonyPopupMessage = "TXT_SENATE_COLONY_POPUP_MESSAGE"
tByzantiumCoords = (46, 27)
iByzantiumSelectionProbability = 33
iCultureRequiredForFlip = 25
lBooleanOptions = ["Yes", "No"]
class SenateOutcome:
lColonyList = None
def noOutcome(self, _):
print "No Senate Outcome"
def civilWar(self):
"""
Checks if the conditions for a civil war are met. In that case launches a rebellions.
"""
lCities = getCityList("Rome")
iNumCities = len(lCities)
lUnhappy = list()
for pCity in (city.GetCy() for city in lCities):
if Rebellion.CivilWar.checkUnhappiness(pCity):
lUnhappy.append(pCity)
iNumUnhappy = len(lUnhappy)
if ( Rebellion.CivilWar.isQuota(iNumUnhappy, iNumCities)
and iNumUnhappy >= iNumCities * iCivilWarUnhappyCitiesPercentage / 100.0 ):
Rebellion.CivilWar.initCivilWar(pCivRome, lUnhappy)
return True
return False
def grantCity(self):
"""
Selects a rival city and suggests player acquires it for free.
"""
pCity = self.preselectCity()
if pCity:
launchFlipPopup(pCity)
return True
else:
return False
def preselectCity(self):
pByzantiumPlot = getPlot(tByzantiumCoords)
if ( pByzantiumPlot.isCity()
and pByzatiumPlot.getOwner() != eRome ):
if isChance(iByzantiumSelectionProbability):
return pByzantiumPlot.getPlotCity()
pCity = self.getRandomCity()
if pCity:
return pCity
def getRandomCity():
iTreshold = iCultureRequiredForFlip
while iTreshold:
lCities = list()
for pCivPlayer in CivPlayer.getPlayers():
if pCivPlayer == pCivRome: continue
for lCityList in (player.getCityList() for player in pCivPlayer.get(PyPlayer)):
for pCity in (city.GetCy() for city in lCityList):
if pCity.getCulture(eRome) > iTreshold:
lCities.append(pCity)
if not lCities:
iTreshold -= 5
else:
iTreshold = 0
iRand = getRandNum(len(lCities), "flipping city")
return lCities[iRand]
def launchFlipPopup(self, pCity):
tCoords = getCoords(pCity)
Interface.doPing(tCoords[0], tCoords[1], eRome)
tData = pCity.getOwner(), pCity.getID()
showMultiOptionPopup(flipPopupMessage, (pCity.getName(),), "agreeTakeover", eRome, lBooleanOptions, tData)
def grantColony(self, tArea=None):
"""
Checks if there are historical settlement sites within the area. Launches a pop-up if at least 1 site is found.
"""
if not tArea:
tArea = ((0, 0), (Map.getGridWidth(), Map.getGridHeight()))
lHistoricalSites = self.getHistoricalSites(tArea)
if len(lHistoricalSites):
if len(lHistoricalSites) > iMaxColonySites:
lHistoricalSites = self.randomSelection(lHistoricalSites)
self.launchColonyPopup(lHistoricalSites)
return True
else:
return False
def getHistoricalSelection(self, tArea):
lSites = list()
for tCoords in getAreaCoords(tArea):
if cityNameDict.has_key(tCoords):
if ( isPlotOwner(tCoords, -1)
or isPlotOwner(tCoords, eRome) ):
if self.checkAdjacent(tCoords):
lSites.append((tCoords, cityNameDict[tCoords]))
return lSites
def checkAdjacent(self, tCoords):
for pPlot in getAdjacentPlots(tCoords):
if pPlot.isCity():
return False
return True
def randomSites(self, lSites):
lSiteHash = list()
iNumSites = len(lSites)
iRand = getRandNum(iNumSites, "historical sites")
for i in xrange(iMaxColonySites):
iIndex = i + iRand
if iIndex > iNumSites - 1:
iIndex -= iNumSites
lSiteHash.append(lSites[iIndex])
return lSiteHash
def launchColonyPopup(self, lSites):
self.lColonyList = lSites
lCityNames = list()
for tCoords, name in ((tSite[0], tSite[1]) for tSite in lSites):
lCityNames.append(name)
Interface.doPing(tCoords[0], tCoords[1], eRome)
showMultiOptionPopup(colonyPopupMessage, (), "selectColony", eRome, lCityNames)
tRewards = (
( changeGold, 100, 2 ),
( grantUnit, ePretorian, 1 ),
( grantGoldenAge, 6, 1 ),
( changeHappiness, 1, 3 ),
( grantUpgrade, None, 1 ),
( grantBuilding, None, 1 ),
( grantCivic, None, 1 ),
( grantTech, None, 1 )
( grantCity, None, 1 )
( grantColony, None, 1)
)
tPenalties = (
( changeGold, -200, 3 ),
( rebelUnit, eSwordsman, 2),
( changeHappiness, -1, 4),
( destroyBuilding, None, 2),
( anarchyTurns, 1, 2),
( civilWar, None, 1)
)
tEffects = (
)
lArrays = [tRewards, tPenalties, tEffects]
iRewards, iPenalties, iEffects = range(3)
arrayIndexSpanDict = dict()
def __init__(self):
self.setupWeighIndexes()
def getWeightSpan(self, tData):
iCount = 0
for iWeigh in (tValues[2] for tValues in tData):
iCount += iWeigh
return iCount
def setupWeighIndexes(self):
for tData in self.lArrays:
self.arrayIndexSpanDict[tData] = self.getWeightSpan(tData)
@classmethod
def getArray(cls, iArray):
return self.lArrays[iArray]
@classmethod
def indexArray(cls, tArray, iIndex):
if iIndex != -1:
return tArray[iIndex]
def senateOutcomeByIndex(self, iArray, iIndex, iArg=None):
tData = SenateOutcome.getArray(iArray)
if iArg == None:
iArg = SenateOutcome.indexArray(tData, iIndex)[1]
if not self.fireFunction(SenateOutcome.indexArray(tData, iIndex)[0], iArg):
self.randSenateOutcome(iArray)
def senateOutcomeByType(self, pOutcome, iArg=None):
iArray = pOutcome.getArrayID()
if iArg == None:
tData = SenateOutcome.getArray(iArray)
iArg = SenateOutcome.indexArray(tData, pOutcome.getID())[1]
if not self.fireFunction(pOutcome.getFunction(), iArg):
self.randSenateOutcome(iArray)
def randSenateOutcome(self, iArray):
tData = SenateOutcome.getArray(iArray)
iArrayIndexSpan = self.arrayIndexSpanDict[tData]
iRand = getRandNum(iArrayIndexSpan) + 1
iCount = 0
for tValues in tData:
function, iArgument, iWeigh = tValues
iCount += iWeigh
if iCount >= iRand:
if self.fireFunction(function, iArgument):
return
else:
self.randSenateOutcome(iArray)
def fireFunction(self, function, iArg):
if iArg == None:
return function()
else:
return function(iArg)
# inherited by sub-classes
def getID(self):
return self.iIndex
def getArray(self):
return self.iArray
def getFunction(self):
return self.function
def getArg(self):
return self.iArg
outcome = SenateOutcome()
class SenateRewardType(SenateOutcome):
def __init__(self, iIndex):
self.iArray = self.iRewards
self.iIndex = iIndex
if iIndex == -1:
self.function = self.noOutcome
self.iArg = None
else:
self.function = SenateOutcome.indexArray(tRewards, iIndex)[0]
self.iArg = SenateOutcome.indexArray(tRewards, iIndex)[1]
NUM_REWARDS = len(self.tRewards)
NO_REWARD = SenateRewardType(-1)
REWARD_GOLD = SenateRewardType(0)
REWARD_UNIT = SenateRewardType(1)
REWARD_GOLDEN_AGE = SenateRewardType(2)
REWARD_HAPPINESS = SenateRewardType(3)
REWARD_UPGRADE = SenateRewardType(4)
REWARD_BUILDING = SenateRewardType(5)
REWARD_CIVIC = SenateRewardType(6)
REWARD_TECH = SenateRewardType(7)
REWARD_CITY = SenateRewardType(8)
REWARD_COLONY = SenateRewardType(9)
class SenatePenaltyType(SenateOutcome):
def __init__(self, iIndex):
self.iArray = self.iPenalties
self.iIndex = iIndex
if iIndex == -1:
self.function = self.noOutcome
self.iArg = None
else:
self.function = SenateOutcome.indexArray(tPenalties, iIndex)[0]
self.iArg = SenateOutcome.indexArray(tPenalties, iIndex)[1]
NUM_PENALTIES = len(self.tPenalties)
NO_PENALTY = SenatePenaltyType(-1)
PENALTY_GOLD = SenatePenaltyType(0)
PENALTY_UNIT = SenatePenaltyType(1)
PENALTY_HAPPINESS = SenatePenaltyType(2)
PENALTY_BUILDING = SenatePenaltyType(3)
PENALTY_ANARCY = SenatePenaltyType(4)
PENALTY_CIVIL_WAR = SenatePenaltyType(5)
# not currently in use
class SenateEffectType(SenateOutcome):
def __init__(self, iIndex):
self.iArray = SenateOutcome.iEffects
self.iIndex = iIndex
if iIndex == -1:
self.function = self.noOutcome
self.iArg = None
else:
self.function = SenateOutcome.indexArray(tEffects, iIndex)[0]
self.iArg = SenateOutcome.indexArray(tEffects, iIndex)[1]
NUM_EFFECTS = len(self.tEffects)
NO_EFFECT = SenateEffectType(-1)
# add class attributes for effects here