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

Source Code for Module horizons.world.production.productionline

  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  from horizons.constants import UNITS 
 23   
 24   
25 -class ProductionLine:
26 """Class that collects the production line data.""" 27
28 - def __init__(self, id, data):
29 """Inits self from yaml data""" 30 self.__data = data or {} 31 self.id = id 32 self.__init()
33
34 - def __init(self):
35 self._init_finished = False 36 # time in seconds that production takes: 37 self.time = self.__data.get('time', 1) 38 # whether this prodline influences animation: 39 self.changes_animation = self.__data.get('changes_animation', True) 40 # whether statistics about this production line should be kept: 41 self.save_statistics = self.__data.get('save_statistics', True) 42 43 # here we store all resource information. 44 # needed resources have a negative amount, produced ones are positive. 45 self.production = {} 46 self.produced_res = {} # contains only produced 47 self.consumed_res = {} # contains only consumed 48 self.unit_production = {} # Stores unit_id: amount entries, if units are to be produced 49 if 'produces' in self.__data: 50 for produced_object, amount in self.__data['produces']: 51 if produced_object < UNITS.DIFFERENCE_BUILDING_UNIT_ID: 52 self.production[produced_object] = amount 53 self.produced_res[produced_object] = amount 54 else: 55 self.unit_production[produced_object] = amount 56 if 'consumes' in self.__data: 57 for res, amount in self.__data['consumes']: 58 self.production[res] = amount 59 self.consumed_res[res] = amount 60 61 self._init_finished = True
62
63 - def __str__(self):
64 return "ProductionLineData(lineid={})".format(self.id)
65
66 - def alter_production_time(self, modifier):
67 """Sets time to original production time multiplied by modifier""" 68 self.time = self.__data.get('time', 1) * modifier
69
70 - def change_amount(self, res, amount):
71 """Alters an amount of a res at runtime. Because of redundancy, you can only change 72 amounts here.""" 73 self.production[res] = amount 74 if res in self.consumed_res: 75 self.consumed_res[res] = amount 76 if res in self.produced_res: 77 self.produced_res[res] = amount
78
79 - def save(self, db, for_worldid):
80 # we don't have a worldid, we load it for another world id 81 for res, amount in self.production.items(): 82 db("INSERT INTO production_line(for_worldid, type, res, amount) VALUES(?, ?, ?, ?)", 83 for_worldid, "NORMAL", res, amount) 84 for res, amount in self.consumed_res.items(): 85 db("INSERT INTO production_line(for_worldid, type, res, amount) VALUES(?, ?, ?, ?)", 86 for_worldid, "CONSUMED", res, amount) 87 for res, amount in self.produced_res.items(): 88 db("INSERT INTO production_line(for_worldid, type, res, amount) VALUES(?, ?, ?, ?)", 89 for_worldid, "PRODUCED", res, amount) 90 for unit, amount in self.unit_production.items(): 91 db("INSERT INTO production_line(for_worldid, type, res, amount) VALUES(?, ?, ?, ?)", 92 for_worldid, "UNIT", unit, amount) 93 94 db("INSERT INTO production_line(for_worldid, type, res, amount) VALUES(?, ?, ?, ?)", 95 for_worldid, "TIME", self.time, None)
96
97 - def load(self, db, for_worldid):
98 # we don't have a worldid, we load it for another world id 99 self.production = {} 100 self.consumed_res = {} 101 self.produced_res = {} 102 self.unit_production = {} 103 for t, res, amount in db.get_production_line_row(for_worldid): 104 if t == "TIME": 105 self.time = res 106 else: 107 {"NORMAL": self.production, 108 "CONSUMED": self.consumed_res, 109 "PRODUCED": self.produced_res, 110 "UNIT": self.unit_production}[t][res] = amount
111
112 - def get_original_copy(self):
113 """Returns a copy of this production, in its original state, no changes 114 applied""" 115 return ProductionLine(self.id, self.__data)
116