Package horizons :: Package ai :: Package aiplayer :: Module unitbuilder
[hide private]
[frames] | no frames]

Source Code for Module horizons.ai.aiplayer.unitbuilder

 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  import logging 
23   
24  from horizons.command.production import AddProduction 
25  from horizons.constants import BUILDINGS, PRODUCTIONLINES 
26  from horizons.world.production.producer import Producer 
27 28 29 -class UnitBuilder:
30 """An object of this class builds the units of one player.""" 31 32 log = logging.getLogger("ai.aiplayer.unit_builder") 33
34 - def __init__(self, owner):
35 super().__init__() # TODO: is this call needed? 36 self.owner = owner
37
38 - def _get_boat_builders(self):
39 """Return a list of all boat builders owned by the player.""" 40 result = [] # [building, ...] 41 for settlement_manager in self.owner.settlement_managers: 42 result.extend(settlement_manager.settlement.buildings_by_id.get(BUILDINGS.BOAT_BUILDER, [])) 43 return result
44
45 - def build_ship(self):
46 """Build a new usable fishing boat.""" 47 boat_builder = self._get_boat_builders()[0] 48 AddProduction(boat_builder.get_component(Producer), PRODUCTIONLINES.HUKER).execute(self.owner.session) 49 production = boat_builder.get_component(Producer)._get_production(PRODUCTIONLINES.HUKER) 50 production.add_production_finished_listener(self._ship_built) 51 self.log.info('%s started building trading ship', self)
52
53 - def build_combat_ship(self):
54 """Build a new frigate ship""" 55 boat_builder = self._get_boat_builders()[0] 56 AddProduction(boat_builder.get_component(Producer), PRODUCTIONLINES.FRIGATE).execute(self.owner.session) 57 production = boat_builder.get_component(Producer)._get_production(PRODUCTIONLINES.FRIGATE) 58 production.add_production_finished_listener(self._ship_built) 59 self.log.info('%s started building combat ship', self)
60
61 - def _ship_built(self, production):
62 """Called when a new ship has been built.""" 63 self.log.info('%s ship building finished', self) 64 self.owner.refresh_ships()
65 66 @property
67 - def num_ships_being_built(self):
68 """Return the number of ships being built by all the boat builders of the player.""" 69 return sum(len(boat_builder.get_component(Producer).get_production_lines()) for boat_builder in self._get_boat_builders())
70
71 - def __str__(self):
72 return '{} UnitBuilder'.format(self.owner)
73