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

Source Code for Module horizons.ai.generic

 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 weakref 
23   
24  from horizons.constants import GAME_SPEED 
25  from horizons.ext.enum import Enum 
26  from horizons.scheduler import Scheduler 
27  from horizons.util.python.callback import Callback 
28  from horizons.world.player import Player 
29  from horizons.world.units.unitexeptions import MoveNotPossible 
30   
31   
32 -class GenericAI(Player):
33 """Class for AI players implementing generic stuff.""" 34 35 shipStates = Enum('idle', 'moving_random') 36
37 - def __init__(self, *args, **kwargs):
38 super().__init__(*args, **kwargs) 39 self.__init()
40
41 - def __init(self):
42 self.ships = weakref.WeakValueDictionary() # {ship : state}. used as list of ships and structure to know their state
43
44 - def _load(self, db, worldid):
45 super()._load(db, worldid) 46 self.__init()
47
48 - def send_ship(self, ship):
50
51 - def send_ship_random(self, ship):
52 """Sends a ship to a random position on the map. 53 @param ship: Ship instance that is to be used.""" 54 # find random position 55 point = self.session.world.get_random_possible_ship_position() 56 self.log.debug("%s %s: moving to random location %d, %d", self.__class__.__name__, self.worldid, point.x, point.y) 57 # move ship there: 58 try: 59 ship.move(point, Callback(self.ship_idle, ship)) 60 except MoveNotPossible: 61 self.log.info("%s %s: ship blocked", self.__class__.__name__, self.worldid) 62 # retry moving ship in 2 secs 63 Scheduler().add_new_object(Callback(self.ship_idle, ship), self, 64 GAME_SPEED.TICKS_PER_SECOND * 2) 65 return 66 self.ships[ship] = self.shipStates.moving_random
67
68 - def ship_idle(self, ship):
69 """Called if a ship is idle. Sends ship to a random place. 70 @param ship: ship instance""" 71 self.log.debug("%s %s: idle, moving to random location", self.__class__.__name__, self.worldid) 72 Scheduler().add_new_object(Callback(self.send_ship, ship), self)
73
74 - def end(self):
75 self.ships = None 76 super().end()
77