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

Source Code for Module horizons.ai.aiplayer.strategy.condition

  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.ai.aiplayer.behavior import BehaviorManager 
 25   
 26   
27 -class Condition:
28 """ 29 Condition's goal is to aid StrategyManager in figuring out what kind of strategy/mission 30 is applicable to given state of world, e.g. instead of having a really long method that decides what kind of reasoning 31 should be done at given point, we have a collection of Conditions (with priorities) that are easier to handle. 32 """ 33 34 log = logging.getLogger("ai.aiplayer.combat.condition") 35 default_certainty = 1.0 36
37 - def __init__(self, owner):
38 self.owner = owner 39 self.world = owner.world 40 self.session = owner.session 41 self.unit_manager = owner.unit_manager 42 43 # States whether given condition can be locked 44 self.lockable = True
45
46 - def check(self, **environment):
47 """ 48 Based on information contained in **environment, determine wheter given condition occurs. 49 @return: If the condition occurs: dictionary, else: None 50 """ 51 raise NotImplementedError("This is an abstract class")
52
53 - def get_identifier(self, **environment):
54 """ 55 Based on information contained in **environment return an Unique identifier for given condition. 56 User for checking whether given condition is already being resolved by a mission in progress. 57 @return: unique identifier 58 @rtype: str 59 """ 60 return self.__class__.__name__
61 62
63 -class ConditionSharingSettlement(Condition):
64 """ 65 States whether given player shares a settlement with AI. 66 Raises "attack_enemy_player" strategy 67 """
68 - def __init__(self, owner):
69 super().__init__(owner)
70
71 - def check(self, **environment):
72 other_player = environment['player'] 73 74 my_islands = set(self.unit_manager.get_player_islands(self.owner)) 75 enemy_islands = set(self.unit_manager.get_player_islands(other_player)) 76 77 # checks whether player share the same island 78 if my_islands & enemy_islands: 79 return {'player': other_player, 'certainty': self.default_certainty, 'strategy_name': 'player_shares_island', 'type': BehaviorManager.strategy_types.offensive} 80 else: 81 return None
82
83 - def get_identifier(self, **environment):
84 return super().get_identifier(**environment) + str(environment['player'].worldid)
85 86
87 -class ConditionHostile(Condition):
88 """ 89 States whether there is a hostile player that can be attacked (i.e. has ships that can be destroyed) 90 """
91 - def __init__(self, owner):
92 super().__init__(owner)
93
94 - def check(self, **environment):
95 player = environment['player'] 96 97 if not self.session.world.diplomacy.are_enemies(self.owner, player): 98 return None 99 100 hostile_ships = self.unit_manager.get_player_ships(player) 101 if hostile_ships: 102 return {'player': player, 'certainty': self.default_certainty, 'strategy_name': 'hostile_player', 'type': BehaviorManager.strategy_types.offensive} 103 else: 104 return None
105
106 - def get_identifier(self, **environment):
107 return super().get_identifier(**environment) + str(environment['player'].worldid)
108 109
110 -class ConditionNeutral(Condition):
111 """ 112 States whether given player is neutral. 113 What it aims to do is not only find if given player is neutral, but also sort them, 114 i.e. penalize if given neutral is ally with our enemies etc. 115 This way in case of any diplomatic actions it's possible to have a "safe" ally 116 """ 117
118 - def check(self, **environment):
119 player = environment['player'] 120 if self.session.world.diplomacy.are_neutral(self.owner, player): 121 return {'player': player, 'certainty': self.default_certainty, 'strategy_name': 'neutral_player', 'type': BehaviorManager.strategy_types.diplomatic} 122 else: 123 return None
124
125 - def get_identifier(self, **environment):
126 return super().get_identifier(**environment) + str(environment['player'].worldid)
127 128
129 -class ConditionAllied(Condition):
130 """ 131 States whether given player is ally. 132 """ 133
134 - def check(self, **environment):
135 player = environment['player'] 136 if self.session.world.diplomacy.are_allies(self.owner, player): 137 return {'player': player, 'certainty': self.default_certainty, 'strategy_name': 'allied_player', 'type': BehaviorManager.strategy_types.diplomatic} 138 else: 139 return None
140
141 - def get_identifier(self, **environment):
142 return super().get_identifier(**environment) + str(environment['player'].worldid)
143 144
145 -class ConditionDebug(Condition):
146 """ 147 For testing purposes, always happens 148 """ 149
150 - def check(self, **environment):
151 player = environment['player'] 152 return {'player': player, 'certainty': self.default_certainty, 'strategy_name': 'debug', 'type': BehaviorManager.strategy_types.diplomatic}
153 #return {'player': player, 'certainty': self.default_certainty, 'strategy_name': 'player_shares_island', 'type': BehaviorManager.strategy_types.offensive} 154
155 - def get_identifier(self, **environment):
156 return super().get_identifier(**environment) + str(environment['player'].worldid)
157 158
159 -class ConditionPirateRoutinePossible(Condition):
160 """ 161 Currently always occurs, when pirate has more conditions/strategies to work on, this may change. 162 """
163 - def __init__(self, owner):
164 super().__init__(owner) 165 self.lockable = False
166
167 - def check(self, **environment):
168 return {'certainty': self.default_certainty, 'strategy_name': 'pirate_routine', 'type': BehaviorManager.strategy_types.idle}
169
170 - def get_identifier(self, **environment):
171 return super().get_identifier(**environment)
172