Package horizons :: Package ai :: Package aiplayer :: Package behavior :: Module profile
[hide private]
[frames] | no frames]

Source Code for Module horizons.ai.aiplayer.behavior.profile

  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  import random 
 24   
 25  from horizons.ai.aiplayer.behavior import BehaviorManager 
 26  from horizons.ai.aiplayer.behavior.behaviorcomponents import BehaviorDebug  # pylint: disable=W0611 
 27  from horizons.ai.aiplayer.behavior.behaviorcomponents import ( 
 28          BehaviorAggressive, BehaviorAggressivePirate, BehaviorDoNothing, BehaviorEvil, BehaviorGood, 
 29          BehaviorNeutral, BehaviorRegular, BehaviorRegularPirate, BehaviorSmart) 
 30  from horizons.ai.aiplayer.strategy.condition import ConditionDebug  # pylint: disable=W0611 
 31  from horizons.ai.aiplayer.strategy.condition import ( 
 32          ConditionAllied, ConditionHostile, ConditionNeutral, ConditionPirateRoutinePossible, 
 33          ConditionSharingSettlement) 
34 35 36 -class BehaviorProfile:
37 - def __init__(self):
38 """ 39 Init actions and strategies with required types. 40 e.g. self.strategies is a dict of Enum => {}, each of such items is later filled by concrete BehaviorProfile. 41 """ 42 super().__init__() # TODO: check if this call is needed 43 self.actions = {action_type: {} for action_type in BehaviorManager.action_types} 44 self.strategies = {strategy_type: {} for strategy_type in BehaviorManager.strategy_types} 45 self.conditions = {}
46
47 48 -class BehaviorProfileAggressive(BehaviorProfile):
49
50 - def __init__(self, player):
51 super().__init__() 52 53 self.conditions = { 54 ConditionHostile(player): 1.1, 55 ConditionSharingSettlement(player): 1.0, 56 ConditionNeutral(player): 0.3, 57 ConditionAllied(player): 0.3, 58 } 59 60 self.actions[BehaviorManager.action_types.offensive][BehaviorRegular(player)] = 0.35 61 self.actions[BehaviorManager.action_types.offensive][BehaviorAggressive(player)] = 0.65 62 self.actions[BehaviorManager.action_types.idle][BehaviorDoNothing(player)] = 1.0 63 64 self.strategies[BehaviorManager.strategy_types.offensive][BehaviorRegular(player)] = 1.0 65 66 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorAggressive(player)] = 0.05 67 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorEvil(player)] = 0.75 68 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorNeutral(player)] = 0.2
69
70 71 -class BehaviorProfileBalanced(BehaviorProfile):
72
73 - def __init__(self, player):
74 super().__init__() 75 76 self.conditions = { 77 ConditionHostile(player): 1.1, 78 ConditionSharingSettlement(player): 1.0, 79 ConditionNeutral(player): 0.3, 80 ConditionAllied(player): 0.29, 81 } 82 83 self.actions[BehaviorManager.action_types.offensive][BehaviorRegular(player)] = 0.8 84 self.actions[BehaviorManager.action_types.offensive][BehaviorAggressive(player)] = 0.2 85 self.actions[BehaviorManager.action_types.idle][BehaviorDoNothing(player)] = 1.0 86 87 self.strategies[BehaviorManager.strategy_types.offensive][BehaviorRegular(player)] = 1.0 88 89 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorEvil(player)] = 0.05 90 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorNeutral(player)] = 0.9 91 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorGood(player)] = 0.05
92
93 94 -class BehaviorProfileCautious(BehaviorProfile):
95
96 - def __init__(self, player):
97 super().__init__() 98 99 self.conditions = { 100 ConditionHostile(player): 0.9, 101 #ConditionSharingSettlement(player): 1.0, # does not respond to enemy sharing a settlement 102 ConditionNeutral(player): 0.3, 103 ConditionAllied(player): 0.29, 104 } 105 106 self.actions[BehaviorManager.action_types.offensive][BehaviorRegular(player)] = 0.8 107 self.actions[BehaviorManager.action_types.idle][BehaviorDoNothing(player)] = 1.0 108 109 self.strategies[BehaviorManager.strategy_types.offensive][BehaviorRegular(player)] = 1.0 110 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorGood(player)] = 0.7 111 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorNeutral(player)] = 0.3
112
113 114 -class BehaviorProfilePirateRegular(BehaviorProfile):
115
116 - def __init__(self, player):
128
129 130 -class BehaviorProfileManager:
131 """ 132 BehaviorProfileManager is an object that defines the dictionary with BehaviorComponents for AIPlayer. 133 If it proves to be useful it will handle loading AI profiles from YAML. 134 """ 135 136 log = logging.getLogger("ai.aiplayer.behavior.profilemanager") 137 138 @classmethod
139 - def get_random_player_profile(cls, player, token):
141 142 @classmethod
143 - def get_random_pirate_profile(cls, player, token):
145 146 @classmethod
147 - def _get_random_profile(cls, player, token, profiles):
148 random_generator = random.Random() 149 random_generator.seed(token) 150 151 total, random_value = 0.0, random_generator.random() 152 153 probabilities_sum = sum([item[1] for item in profiles]) 154 155 assert probabilities_sum > 1e-7, "sum of BehaviorProfile probabilities is too low: {0!s}". \ 156 format(probabilities_sum) 157 158 random_value *= probabilities_sum 159 160 chosen_profile = None 161 for profile, probability in profiles: 162 if (total + probability) >= random_value: 163 chosen_profile = profile 164 break 165 total += probability 166 167 cls.log.debug("BehaviorProfileManager: Player %s was given %s", player.name, chosen_profile.__name__) 168 return chosen_profile(player)
169
170 171 -class BehaviorProfileDebug(BehaviorProfile):
172
173 - def __init__(self, player):
174 super().__init__() 175 176 self.conditions = { 177 #ConditionHostile(player): 1.1, 178 #ConditionSharingSettlement(player): 1.0, 179 ConditionNeutral(player): 0.4, 180 #ConditionAllied(player): 0.3, 181 #ConditionDebug(player): 1.0, 182 } 183 184 self.actions[BehaviorManager.action_types.offensive][BehaviorSmart(player)] = 1.0 185 186 self.strategies[BehaviorManager.strategy_types.offensive][BehaviorRegular(player)] = 1.0 187 #self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorAggressive(player)] = 0.02 188 #self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorDebug(player)] = 1.0 189 #self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorEvil(player)] = 1.0 190 #self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorGood(player)] = 1.0 191 self.strategies[BehaviorManager.strategy_types.diplomatic][BehaviorNeutral(player)] = 1.0
192
193 194 # Each AI player is assigned a Profile with certain probability. 195 # actions, strategies and conditions are encapsulated inside a profile itself. 196 -def get_available_player_profiles():
197 return ( 198 (BehaviorProfileCautious, 0.3), 199 (BehaviorProfileAggressive, 0.1), 200 (BehaviorProfileBalanced, 0.6), 201 #(BehaviorProfileDebug, 1.0), 202 )
203
204 205 -def get_available_pirate_profiles():
206 return ( 207 (BehaviorProfilePirateRegular, 1.0), 208 )
209