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

Source Code for Module horizons.ai.aiplayer.strategy.mission.surpriseattack

  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.strategy.mission import FleetMission 
 23  from horizons.command.diplomacy import AddEnemyPair 
 24  from horizons.ext.enum import Enum 
 25  from horizons.util.python.callback import Callback 
 26  from horizons.util.shapes import Circle, Point 
 27  from horizons.util.worldobject import WorldObject 
 28  from horizons.world.units.unitexeptions import MoveNotPossible 
29 30 31 -class SurpriseAttack(FleetMission):
32 """ 33 This is a basic attack mission. 34 1. Send fleet to a Point (or Circle) A 35 2. Break diplomacy with enemy player P if he is not hostile, 36 3. Begin combat phase 37 4. Return home (point B). 38 """ 39 40 missionStates = Enum.get_extended(FleetMission.missionStates, 'sailing_to_target', 'in_combat', 'breaking_diplomacy', 'going_back') 41
42 - def __init__(self, success_callback, failure_callback, ships, target_point, return_point, enemy_player):
43 super().__init__(success_callback, failure_callback, ships) 44 self.__init(target_point, return_point, enemy_player)
45
46 - def __init(self, target_point, return_point, enemy_player):
47 self.target_point = target_point 48 self.return_point = return_point 49 self.enemy_player = enemy_player 50 51 self.combatIntermissions = { 52 self.missionStates.sailing_to_target: (self.sail_to_target, self.flee_home), 53 self.missionStates.in_combat: (self.go_back, self.flee_home), 54 self.missionStates.going_back: (self.go_back, self.flee_home), 55 self.missionStates.breaking_diplomacy: (self.break_diplomacy, self.flee_home), 56 self.missionStates.fleeing_home: (self.flee_home, self.flee_home), 57 } 58 59 # Fleet callbacks corresponding to given state 60 self._state_fleet_callbacks = { 61 self.missionStates.sailing_to_target: Callback(self.break_diplomacy), 62 self.missionStates.going_back: Callback(self.report_success, "Ships arrived at return point"), 63 self.missionStates.fleeing_home: Callback(self.report_failure, "Combat was lost, ships fled home successfully"), 64 }
65
66 - def save(self, db):
67 super().save(db) 68 db("INSERT INTO ai_mission_surprise_attack (rowid, enemy_player_id, target_point_x, target_point_y, target_point_radius, " 69 "return_point_x, return_point_y) VALUES(?, ?, ?, ?, ?, ?, ?)", self.worldid, self.enemy_player.worldid, self.target_point.center.x, 70 self.target_point.center.y, self.target_point.radius, self.return_point.x, self.return_point.y)
71
72 - def _load(self, worldid, owner, db, success_callback, failure_callback):
73 super()._load(db, worldid, success_callback, failure_callback, owner) 74 db_result = db("SELECT enemy_player_id, target_point_x, target_point_y, target_point_radius, return_point_x, return_point_y " 75 "FROM ai_mission_surprise_attack WHERE rowid = ?", worldid)[0] 76 enemy_player_id, target_point_x, target_point_y, target_point_radius, return_point_x, return_point_y = db_result 77 78 target_point = Circle(Point(target_point_x, target_point_y), target_point_radius) 79 return_point = Point(return_point_x, return_point_y) 80 enemy_player = WorldObject.get_object_by_id(enemy_player_id) 81 self.__init(target_point, return_point, enemy_player)
82
83 - def start(self):
84 self.sail_to_target()
85
86 - def sail_to_target(self):
87 self.log.debug("Player %s, Mission %s, 1/4 set off from point %s to point %s", self.owner.name, self.__class__.__name__, self.return_point, self.target_point) 88 try: 89 self.fleet.move(self.target_point, self._state_fleet_callbacks[self.missionStates.sailing_to_target]) 90 self.state = self.missionStates.sailing_to_target 91 except MoveNotPossible: 92 self.report_failure("Move was not possible when moving to target")
93
94 - def break_diplomacy(self):
95 self.state = self.missionStates.breaking_diplomacy 96 self.log.debug("Player %s, Mission %s, 2/4 breaking diplomacy with Player %s", self.owner.name, self.__class__.__name__, self.enemy_player.name) 97 if not self.session.world.diplomacy.are_enemies(self.owner, self.enemy_player): 98 AddEnemyPair(self.owner, self.enemy_player).execute(self.session) 99 self.in_combat()
100
101 - def in_combat(self):
102 self.combat_phase = True 103 self.log.debug("Player %s, Mission %s, 3/4 in combat", self.owner.name, self.__class__.__name__) 104 self.state = self.missionStates.in_combat
105
106 - def go_back(self):
107 self.log.debug("Player %s, Mission %s, 4/4 going back after combat to point %s", self.owner.name, self.__class__.__name__, self.return_point) 108 try: 109 self.fleet.move(self.return_point, self._state_fleet_callbacks[self.missionStates.going_back]) 110 self.state = self.missionStates.going_back 111 except MoveNotPossible: 112 self.report_failure("Move was not possible when going back")
113
114 - def flee_home(self):
115 if self.fleet.size() > 0: 116 try: 117 self.fleet.move(self.return_point, self._state_fleet_callbacks[self.missionStates.fleeing_home]) 118 self.state = self.missionStates.fleeing_home 119 except MoveNotPossible: 120 self.report_failure("Combat was lost, ships couldn't flee home") 121 else: 122 self.report_failure("Combat was lost, all ships were wiped out")
123 124 @classmethod
125 - def create(cls, success_callback, failure_callback, fleet, target_point, return_point, enemy_player):
126 return SurpriseAttack(success_callback, failure_callback, fleet, target_point, return_point, enemy_player)
127