Package horizons :: Package ai :: Package aiplayer :: Package goal
[hide private]
[frames] | no frames]

Source Code for Package horizons.ai.aiplayer.goal

 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.constants import BUILD_RESULT, GOAL_RESULT 
25 26 27 -class Goal:
28 """An object of this class describes a goal that an AI player attempts to fulfil.""" 29 30 log = logging.getLogger("ai.aiplayer.goal") 31
32 - def __init__(self, owner):
33 super(Goal, self).__init__() 34 self.owner = owner 35 self.personality = self.owner.personality_manager.get(self.get_personality_name()) 36 37 if not hasattr(Goal, '_next_id'): 38 Goal._next_id = 1 # used to assign sequence numbers to goals to ensure consistent ordering 39 self.sequence_number = Goal._next_id 40 Goal._next_id += 1
41
42 - def get_personality_name(self):
43 """Return the name of the goal's personality module.""" 44 raise NotImplementedError('This function has to be overridden.')
45 46 @property
47 - def priority(self):
49 50 @property
51 - def active(self):
52 """Return True if and only if it is ok to execute this goal.""" 53 return self.can_be_activated
54 55 @property
56 - def can_be_activated(self):
57 """Return True if and only if it is ok to update this goal.""" 58 return self.personality.enabled and self.owner.settler_level >= self.personality.min_tier
59
60 - def execute(self):
61 """Do whatever is best to get closer to fulfilling the goal (usually involves building a building).""" 62 raise NotImplementedError("This function has to be overridden.")
63
64 - def update(self):
65 """Update the goal to find out whether it is currently active and what its current priority is.""" 66 pass
67 68 @classmethod
69 - def _translate_build_result(cls, result):
70 """Returns the goal execution state that corresponds to the given BUILD_RESULT constant.""" 71 if result == BUILD_RESULT.OK: 72 return GOAL_RESULT.BLOCK_ALL_BUILDING_ACTIONS 73 elif result == BUILD_RESULT.NEED_RESOURCES: 74 return GOAL_RESULT.BLOCK_SETTLEMENT_RESOURCE_USAGE 75 elif result in [BUILD_RESULT.IMPOSSIBLE, BUILD_RESULT.UNKNOWN_ERROR, BUILD_RESULT.ALL_BUILT, BUILD_RESULT.SKIP]: 76 return GOAL_RESULT.SKIP 77 assert False, 'Unable to translate BUILD_RESULT {:d} to a GOAL_RESULT'.format(result)
78
79 - def __lt__(self, other):
80 if self.priority != other.priority: 81 return self.priority < other.priority 82 return self.sequence_number < other.sequence_number
83
84 - def __str__(self):
85 return 'Goal({:d}): {}({:d})'.format( 86 self.priority, self.__class__.__name__, self.sequence_number)
87