Package horizons :: Package component :: Module fieldbuilder
[hide private]
[frames] | no frames]

Source Code for Module horizons.component.fieldbuilder

 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.command.building import Build 
23  from horizons.component import Component 
24  from horizons.entities import Entities 
25 26 27 -class FieldBuilder(Component):
28 """ 29 Component for production buildings. It adds a hook `fill_range` to fill the 30 building range with a specified 1x1 field. The usual buildability constraints 31 for that field apply. `fill_range` will only succeed if the cost for filling 32 the entire range can be paid - there is no automated partial construction. 33 For GUI purposes, some more information is exposed: 34 - how many fields would be built at once 35 - total resource cost 36 - whether the build is affordable right now. 37 """ 38 NAME = 'FieldBuilder' 39
40 - def __init__(self, field):
41 super().__init__() 42 self.field = Entities.buildings[field]
43 44 @property
45 - def how_many(self):
46 return len(list(self.coords_in_range()))
47 48 @property
49 - def total_cost(self):
50 return {res: amount * self.how_many for res, amount in self.field.costs.items()}
51
52 - def check_resources(self):
53 return Build.check_resources({}, self.total_cost, self.instance.owner, 54 [self.instance.settlement])
55
56 - def coords_in_range(self):
57 where = self.instance.position.get_radius_coordinates(self.instance.radius) 58 for coords in where: 59 tile = self.instance.island.get_tile_tuple(coords) 60 if tile is None or tile.object is not None: 61 continue 62 if not self.field.check_build(self.instance.session, tile): 63 continue 64 yield coords
65
66 - def fill_range(self):
67 for (x, y) in self.coords_in_range(): 68 cmd = Build(self.field, x, y, self.instance.island) 69 cmd.execute(self.instance.session)
70