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

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

  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.component.storagecomponent import StorageComponent 
 24  from horizons.constants import RES 
 25  from horizons.ext.enum import Enum 
 26  from horizons.util.python.callback import Callback 
 27  from horizons.util.worldobject import WorldObject 
28 29 30 -class PrepareFoundationShip(ShipMission):
31 """ 32 Given a ship and a settlement manager it moves the ship to the warehouse and loads 33 it with the resources required to start another settlement. 34 """ 35 36 missionStates = Enum('created', 'moving') 37
38 - def __init__(self, settlement_manager, ship, feeder_island, success_callback, failure_callback):
39 super().__init__(success_callback, failure_callback, ship) 40 self.settlement_manager = settlement_manager 41 self.feeder_island = feeder_island 42 self.warehouse = self.settlement_manager.settlement.warehouse 43 self.state = self.missionStates.created
44
45 - def save(self, db):
46 super().save(db) 47 db("INSERT INTO ai_mission_prepare_foundation_ship(rowid, settlement_manager, ship, feeder_island, state) VALUES(?, ?, ?, ?, ?)", 48 self.worldid, self.settlement_manager.worldid, self.ship.worldid, self.feeder_island, self.state.index)
49 50 @classmethod
51 - def load(cls, db, worldid, success_callback, failure_callback):
52 self = cls.__new__(cls) 53 self._load(db, worldid, success_callback, failure_callback) 54 return self
55
56 - def _load(self, db, worldid, success_callback, failure_callback):
57 db_result = db("SELECT settlement_manager, ship, feeder_island, state FROM ai_mission_prepare_foundation_ship WHERE rowid = ?", worldid)[0] 58 self.settlement_manager = WorldObject.get_object_by_id(db_result[0]) 59 self.warehouse = self.settlement_manager.settlement.warehouse 60 self.feeder_island = db_result[2] 61 self.state = self.missionStates[db_result[3]] 62 super().load(db, worldid, success_callback, failure_callback, 63 WorldObject.get_object_by_id(db_result[1])) 64 65 if self.state == self.missionStates.moving: 66 self.ship.add_move_callback(Callback(self._reached_destination_area)) 67 self.ship.add_blocked_callback(Callback(self._move_to_destination_area)) 68 else: 69 assert False, 'invalid state'
70
71 - def start(self):
72 self.state = self.missionStates.moving 73 self._move_to_destination_area()
74
76 self._move_to_warehouse_area( 77 self.warehouse.position, Callback(self._reached_destination_area), 78 Callback(self._move_to_destination_area), 'Move not possible')
79
81 personality = self.owner.personality_manager.get('SettlementFounder') 82 if self.feeder_island: 83 max_amounts = {RES.BOARDS: personality.max_new_feeder_island_boards, RES.TOOLS: personality.max_new_feeder_island_tools} 84 else: 85 max_amounts = {RES.BOARDS: personality.max_new_island_boards, RES.FOOD: personality.max_new_island_food, RES.TOOLS: personality.max_new_island_tools} 86 87 for resource_id, max_amount in max_amounts.items(): 88 self.move_resource(self.ship, self.settlement_manager.settlement, resource_id, self.ship.get_component(StorageComponent).inventory[resource_id] - max_amount)
89
91 self.log.info('%s reached BO area', self) 92 self._load_foundation_resources() 93 94 success = False 95 if self.feeder_island: 96 success = self.settlement_manager.owner.settlement_founder.have_feeder_island_starting_resources(self.ship, None) 97 if success: 98 self.report_success('Transferred enough feeder island foundation resources to the ship') 99 else: 100 success = self.settlement_manager.owner.settlement_founder.have_starting_resources(self.ship, None) 101 if success: 102 self.report_success('Transferred enough foundation resources to the ship') 103 if not success: 104 self.report_failure('Not enough foundation resources available')
105
106 - def cancel(self):
107 self.ship.stop() 108 super().cancel()
109