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

Source Code for Module horizons.ai.aiplayer.mission.domestictrade

 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.ai.aiplayer.mission import ShipMission 
23  from horizons.ext.enum import Enum 
24  from horizons.util.python.callback import Callback 
25  from horizons.util.worldobject import WorldObject 
26 27 28 -class DomesticTrade(ShipMission):
29 """ 30 Given a ship and two settlement managers the ship is taken to the first one, 31 loaded with resources, and then moved to the other one to unload the resources. 32 """ 33 34 missionStates = Enum('created', 'moving_to_source_warehouse', 'moving_to_destination_warehouse') 35
36 - def __init__(self, source_settlement_manager, destination_settlement_manager, ship, success_callback, failure_callback):
37 super().__init__(success_callback, failure_callback, ship) 38 self.source_settlement_manager = source_settlement_manager 39 self.destination_settlement_manager = destination_settlement_manager 40 self.state = self.missionStates.created
41
42 - def save(self, db):
43 super().save(db) 44 db("INSERT INTO ai_mission_domestic_trade(rowid, source_settlement_manager, destination_settlement_manager, ship, state) VALUES(?, ?, ?, ?, ?)", 45 self.worldid, self.source_settlement_manager.worldid, self.destination_settlement_manager.worldid, self.ship.worldid, self.state.index)
46 47 @classmethod
48 - def load(cls, db, worldid, success_callback, failure_callback):
49 self = cls.__new__(cls) 50 self._load(db, worldid, success_callback, failure_callback) 51 return self
52
53 - def _load(self, db, worldid, success_callback, failure_callback):
54 db_result = db("SELECT source_settlement_manager, destination_settlement_manager, ship, state FROM ai_mission_domestic_trade WHERE rowid = ?", worldid)[0] 55 self.source_settlement_manager = WorldObject.get_object_by_id(db_result[0]) 56 self.destination_settlement_manager = WorldObject.get_object_by_id(db_result[1]) 57 self.state = self.missionStates[db_result[3]] 58 super().load(db, worldid, success_callback, failure_callback, WorldObject.get_object_by_id(db_result[2])) 59 60 if self.state == self.missionStates.moving_to_source_warehouse: 61 self.ship.add_move_callback(Callback(self._reached_source_warehouse_area)) 62 self.ship.add_blocked_callback(Callback(self._move_to_source_warehouse_area)) 63 elif self.state == self.missionStates.moving_to_destination_warehouse: 64 self.ship.add_move_callback(Callback(self._reached_destination_warehouse_area)) 65 self.ship.add_blocked_callback(Callback(self._move_to_destination_warehouse_area)) 66 else: 67 assert False, 'invalid state'
68
69 - def start(self):
70 self.state = self.missionStates.moving_to_source_warehouse 71 self._move_to_source_warehouse_area()
72
74 self._move_to_warehouse_area( 75 self.source_settlement_manager.settlement.warehouse.position, 76 Callback(self._reached_source_warehouse_area), 77 Callback(self._move_to_source_warehouse_area), 'First move not possible')
78
80 self.log.info('%s reached the source warehouse area', self) 81 if self.source_settlement_manager.trade_manager.load_resources(self): 82 self.log.info('%s loaded resources', self) 83 self.state = self.missionStates.moving_to_destination_warehouse 84 self._move_to_destination_warehouse_area() 85 else: 86 self.report_failure('No need for the ship at the source warehouse')
87
89 self._move_to_warehouse_area( 90 self.destination_settlement_manager.settlement.warehouse.position, 91 Callback(self._reached_destination_warehouse_area), 92 Callback(self._move_to_destination_warehouse_area), 'Second move not possible')
93
95 self.log.info('%s reached destination warehouse area', self) 96 self._unload_all_resources(self.destination_settlement_manager.settlement) 97 self.report_success('Unloaded resources')
98