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

Source Code for Package horizons.ai.aiplayer.mission

  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.component.storagecomponent import StorageComponent 
 25  from horizons.constants import BUILDINGS 
 26  from horizons.util.shapes import Circle 
 27  from horizons.util.worldobject import WorldObject 
 28  from horizons.world.units.unitexeptions import MoveNotPossible 
29 30 31 -class Mission(WorldObject):
32 """ 33 This class describes a general mission that an AI seeks to fulfil. 34 """ 35 36 log = logging.getLogger("ai.aiplayer.mission") 37
38 - def __init__(self, success_callback, failure_callback, owner):
39 super().__init__() 40 self.__init(success_callback, failure_callback, owner)
41
42 - def __init(self, success_callback, failure_callback, owner):
43 self.success_callback = success_callback 44 self.failure_callback = failure_callback 45 self.owner = owner
46
47 - def report_success(self, msg):
48 self.log.info('%s mission success: %s', self, msg) 49 self.success_callback(self, msg)
50
51 - def report_failure(self, msg):
52 self.log.debug('%s mission failure: %s', self, msg) 53 self.failure_callback(self, msg)
54
55 - def save(self, db):
56 pass
57
58 - def load(self, db, worldid, success_callback, failure_callback, owner):
59 super().load(db, worldid) 60 self.__init(success_callback, failure_callback, owner)
61
62 - def cancel(self):
63 self.report_failure('Mission cancelled')
64
65 - def __str__(self):
66 return '{} {}({:d})'.format( 67 self.owner if hasattr(self, 'owner') else 'unknown player', 68 self.__class__.__name__, self.worldid)
69
70 - def end(self):
71 pass
72
73 74 -class ShipMission(Mission):
75 - def __init__(self, success_callback, failure_callback, ship):
76 super().__init__(success_callback, failure_callback, ship.owner) 77 self.__init(ship)
78
79 - def __init(self, ship):
80 self.ship = ship 81 self.ship.add_remove_listener(self.cancel)
82
83 - def load(self, db, worldid, success_callback, failure_callback, ship):
84 super().load(db, worldid, success_callback, failure_callback, ship.owner) 85 self.__init(ship)
86
87 - def report_success(self, msg):
88 self.ship.remove_remove_listener(self.cancel) 89 super().report_success(msg)
90
91 - def report_failure(self, msg):
92 self.ship.remove_remove_listener(self.cancel) 93 super().report_failure(msg)
94
95 - def cancel(self):
96 self.ship.stop() 97 super().cancel()
98 99 @classmethod
100 - def move_resource(cls, ship, settlement, resource_id, amount):
101 """Move up to amount tons of resource_id from the ship to the settlement.""" 102 if amount > 0: 103 missing = ship.get_component(StorageComponent).inventory.alter(resource_id, -amount) 104 overflow = settlement.get_component(StorageComponent).inventory.alter(resource_id, amount - missing) 105 ship.get_component(StorageComponent).inventory.alter(resource_id, overflow) 106 elif amount < 0: 107 missing = settlement.get_component(StorageComponent).inventory.alter(resource_id, amount) 108 overflow = ship.get_component(StorageComponent).inventory.alter(resource_id, missing - amount) 109 settlement.get_component(StorageComponent).inventory.alter(resource_id, overflow)
110
111 - def _unload_all_resources(self, settlement):
112 # copy the inventory because otherwise we would be modifying it while iterating 113 for res, amount in [item for item in self.ship.get_component(StorageComponent).inventory.itercontents()]: 114 self.move_resource(self.ship, settlement, res, amount)
115
116 - def _move_to_warehouse_area(self, position, success_callback, blocked_callback, failure_msg):
117 area = Circle(position.center, BUILDINGS.BUILD.MAX_BUILDING_SHIP_DISTANCE) 118 try: 119 self.ship.move(area, success_callback, blocked_callback=blocked_callback) 120 except MoveNotPossible: 121 self.report_failure(failure_msg)
122
123 - def __str__(self):
124 return super().__str__() + (' using {}'.format( 125 self.ship if hasattr(self, 'ship') else 'unknown ship'))
126