def balance(self): s = Stockpile(allowNegative=True) for gainType in self.gain: s.deposit(self.gain[gainType].toList()) for spendType in self.spend: s.withdraw(self.spend[spendType].toList()) return s
def totalRequiredMaterials(self): stockpile = Stockpile() for ruleName, quantity in self.productionLines.items(): rule = getProductionRule(ruleName) consumed = self.requiredMaterials(rule) stockpile.deposit(consumed.toList()) return stockpile
def execute(self, game): maps = game.maps stockpile = Stockpile() mapProvince = maps.findProvince(self.province) if self.outputType == QUERY_RESOURCE_OUTPUT_ACTUAL: total = mapProvince.actualResourceOutput else: total = mapProvince.maxResourceOutput stockpile.deposit(total) return stockpile.toList()
def execute(self, game): maps = game.maps nation = game.getPlayerNation(self.player) stockpile = Stockpile() for province in nation.provinces: if not nation.capitalConnected[province]: continue mapProvince = maps.findProvince(province) if self.outputType == QUERY_RESOURCE_OUTPUT_ACTUAL: total = mapProvince.actualResourceOutput else: total = mapProvince.maxResourceOutput stockpile.deposit(total) return stockpile.toList()
def execute(self, game): player = self.player civilians = game.getPlayerCivilians(player) warehouse = game.getPlayerWarehouse(player) sp = warehouse.projectedStockpile(spendsUpTo=SPEND_DEVELOPMENT) devSpend = Stockpile() for id, assgn in civilians.allActiveAssignments: resourceCost = assgn.perTurnResourceCost(game, player) rc = ResourceConstraint(resourceCost) if rc.isSatisfied(sp): rc.satisfy(sp) devSpend.deposit(rc.resources) assgn.progressBlocked = False else: assgn.progressBlocked = True logging.info('Assignment %s blocked' % (assgn)) warehouse.assignSpend(SPEND_DEVELOPMENT, devSpend.toList())
def actualResourceOutput(self): stockpile = Stockpile() for terrainTile in self.mapTerrainTiles: resources = terrainTile.actualResourcesProduced stockpile.deposit(resources) return stockpile.toList()
class PlayerWarehouse(object): def __init__(self, player): self.player = player self.stockpile = Stockpile() self.gain = { GAIN_TRANSPORT: Stockpile(), GAIN_PRODUCTION: Stockpile(), GAIN_TRADE: Stockpile(), } self.spend = { SPEND_TRANSPORT: Stockpile(), SPEND_PRODUCTION: Stockpile(), SPEND_TRADE: Stockpile(), SPEND_RECRUITMENT: Stockpile(), SPEND_DEVELOPMENT: Stockpile(), SPEND_WORKER_UPKEEP: Stockpile(), } def projectedStockpile(self, gainsUpTo=None, spendsUpTo=None): gainList = WH_GAIN_PRIORITY[:WH_GAIN_PRIORITY.index( gainsUpTo)] if gainsUpTo else WH_GAIN_PRIORITY spendList = WH_SPEND_PRIORITY[:WH_SPEND_PRIORITY.index(spendsUpTo)] if spendsUpTo else WH_SPEND_PRIORITY stockpile = self.stockpile.clone() for gainType in gainList: stockpile.deposit(self.gain[gainType].toList()) for spendType in spendList: stockpile.withdraw(self.spend[spendType].toList()) return stockpile @property def balance(self): s = Stockpile(allowNegative=True) for gainType in self.gain: s.deposit(self.gain[gainType].toList()) for spendType in self.spend: s.withdraw(self.spend[spendType].toList()) return s def getGain(self, gainType): return self.gain[gainType] def getSpend(self, spendType): return self.spend[spendType] def assignGain(self, gainType, resources): self.gain[gainType].assign(resources) def assignSpend(self, spendType, resources): self.spend[spendType].assign(resources) def resetAllGains(self): for gainType in self.gain: self.resetGain(gainType) def resetAllSpends(self): for spendType in self.spend: self.resetSpend(spendType) def resetGain(self, gainType): self.gain[gainType].assign(Stockpile(allowNegative=True).toList()) def resetSpend(self, spendType): self.spend[spendType].assign(Stockpile(allowNegative=True).toList()) def postGain(self, gainType): self.stockpile.deposit(self.gain[gainType].toList()) def postSpend(self, spendType): self.stockpile.withdraw(self.spend[spendType].toList()) def getAmount(self, resourceName): return self.stockpile[resourceName] def listStockpile(self): return self.stockpile.toList() #accepts input of list of resources with quantities, returns lacking for each material #e.g. [(RESOURCE_IRON, 1)] #-> [] if sufficient, [(RESOURCE_IRON, 1)] if lacking def checkLacking(self, materialsWithQuants): lacking = [] for material, quantity in materialsWithQuants: if self.stockpile[material] < quantity: lacking.append((material, quantity-self.stockpile[material])) return lacking def deposit(self, materialsWithQuants): self.stockpile.deposit(materialsWithQuants) def withdraw(self, materialsWithQuants): self.stockpile.withdraw(materialsWithQuants)