Package horizons :: Package world :: Package building :: Module production
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.building.production

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22   
 23  from horizons.command.building import Build 
 24  from horizons.component.storagecomponent import StorageComponent 
 25  from horizons.constants import BUILDINGS, PRODUCTION 
 26  from horizons.scheduler import Scheduler 
 27  from horizons.util.shapes import RadiusRect, Rect 
 28  from horizons.world.building.buildable import ( 
 29          BuildableSingle, BuildableSingleOnCoast, BuildableSingleOnDeposit, BuildableSingleOnOcean) 
 30  from horizons.world.building.building import BasicBuilding 
 31  from horizons.world.building.buildingresourcehandler import BuildingResourceHandler 
 32  from horizons.world.building.nature import Field 
 33  from horizons.world.production.producer import Producer 
34 35 36 -class ProductionBuilding(BuildingResourceHandler, BuildableSingle, BasicBuilding):
37 pass
38
39 40 -class PastryShop(ProductionBuilding):
41 - def get_providers(self):
42 reach = RadiusRect(self.position, self.radius) 43 resources = self.get_consumed_resources(include_inactive=True) 44 providers = self.island.get_providers_in_range(reach, reslist=resources) 45 return [provider for provider in providers]
46
47 48 -class Farm(ProductionBuilding):
49 - def get_providers(self):
50 reach = RadiusRect(self.position, self.radius) 51 resources = self.get_consumed_resources(include_inactive=True) 52 providers = self.island.get_providers_in_range(reach, reslist=resources) 53 return [provider for provider in providers if isinstance(provider, Field)]
54
55 56 -class CoastalProducer(BuildingResourceHandler, BuildableSingleOnOcean, BasicBuilding):
57 """E.g. salt ponds""" 58 pass
59
60 61 -class Fisher(BuildingResourceHandler, BuildableSingleOnCoast, BasicBuilding):
62 """ 63 Old selection workaround (only color fish) removed in b69c72aeef0174c42dec4039eed7b81f96f6dcaa. 64 """ 65
67 total = 0 68 productions = self.get_component(Producer).get_productions() 69 for production in productions: 70 if production.get_age() < PRODUCTION.STATISTICAL_WINDOW * 1.5: 71 return 1 72 state_history = production.get_state_history_times(True) 73 total += state_history[PRODUCTION.STATES.producing.index] 74 return total / float(len(productions))
75
76 77 -class Mine(BuildingResourceHandler, BuildableSingleOnDeposit, BasicBuilding):
78 - def __init__(self, inventory, deposit_class, *args, **kwargs):
79 """ 80 @param inventory: inventory dump of deposit (collected by get_prebuild_data()) 81 @param deposit_class: class num of deposit for later reconstruction (collected by get_prebuild_data()) 82 """ 83 # needs to be inited before super(), since that will call the _on_production_changed hook 84 super().__init__(*args, **kwargs) 85 self.__inventory = inventory 86 self.__deposit_class = deposit_class
87
88 - def initialize(self, deposit_class, inventory, **kwargs):
89 super().initialize(**kwargs) 90 self.__init(deposit_class=deposit_class) 91 for res, amount in inventory.items(): 92 # bury resources from mountain in mine 93 self.get_component(StorageComponent).inventory.alter(res, amount)
94 95 @classmethod
96 - def get_loading_area(cls, building_id, rotation, pos):
97 if building_id in [BUILDINGS.MOUNTAIN, BUILDINGS.MINE]: 98 if rotation == 45: 99 return Rect.init_from_topleft_and_size(pos.origin.x, pos.origin.y + 1, 1, 3) 100 elif rotation == 135: 101 return Rect.init_from_topleft_and_size(pos.origin.x + 1, pos.origin.y + pos.height - 1, 3, 1) 102 elif rotation == 225: 103 return Rect.init_from_topleft_and_size(pos.origin.x + pos.width - 1, pos.origin.y + 1, 1, 3) 104 elif rotation == 315: 105 return Rect.init_from_topleft_and_size(pos.origin.x + 1, pos.origin.y, 3, 1) 106 assert False 107 else: 108 return pos
109
110 - def __init(self, deposit_class):
111 self.__deposit_class = deposit_class 112 113 # setup loading area 114 # TODO: for now we assume that a mine building is 5x5 with a 3x1 entry on 1 side 115 # this needs to be generalized, possibly by defining the loading tiles in the db 116 self.loading_area = self.get_loading_area(deposit_class, self.rotation, self.position)
117 118 @classmethod
119 - def get_prebuild_data(cls, session, position):
120 """Returns dict containing inventory of deposit, which is needed for the mine build""" 121 deposit = session.world.get_building(position.center) 122 data = {} 123 data["inventory"] = deposit.get_component(StorageComponent).inventory.get_dump() 124 data["deposit_class"] = deposit.id 125 return data
126
127 - def remove(self):
128 # build the deposit back here after remove() is finished 129 deposit_build_data = {'inventory': self.get_component(StorageComponent).inventory.get_dump()} 130 build_cmd = Build(self.__deposit_class, self.position.origin.x, self.position.origin.y, 131 self.island, rotation=self.rotation, ownerless=True, data=deposit_build_data) 132 Scheduler().add_new_object(build_cmd, build_cmd, run_in=0) 133 134 super().remove()
135
136 - def save(self, db):
137 super().save(db) 138 db("INSERT INTO mine(rowid, deposit_class) VALUES(?, ?)", 139 self.worldid, self.__deposit_class)
140
141 - def load(self, db, worldid):
142 super().load(db, worldid) 143 deposit_class = db("SELECT deposit_class FROM mine WHERE rowid = ?", worldid)[0][0] 144 self.__init(deposit_class)
145