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

Source Code for Module horizons.ai.aiplayer.goal.depositcoverage

  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.basicbuilder import BasicBuilder 
 23  from horizons.ai.aiplayer.constants import BUILD_RESULT, BUILDING_PURPOSE 
 24  from horizons.ai.aiplayer.goal.settlementgoal import SettlementGoal 
 25  from horizons.constants import BUILDINGS, RES 
 26  from horizons.entities import Entities 
27 28 29 -class DepositCoverageGoal(SettlementGoal):
30 """Build storage tents to get a resource deposit inside the settlement.""" 31 32 # the resource that has to be in the resource deposit 33 _deposit_resource_id = None # type: int 34
35 - def _have_reachable_deposit(self, resource_id):
36 """Returns True if there is a resource deposit outside the settlement that is not owned by another player.""" 37 for tile in self.land_manager.resource_deposits[resource_id]: 38 if tile.object.settlement is None: 39 return True 40 return False
41 42 @property
43 - def active(self):
44 return super().active and not self.production_builder.have_deposit(self._deposit_resource_id) and \ 45 self._have_reachable_deposit(self._deposit_resource_id)
46
48 """Get closer to having a resource deposit in the settlement.""" 49 if not self.production_builder.have_resources(BUILDINGS.STORAGE): 50 return BUILD_RESULT.NEED_RESOURCES 51 52 available_deposits = [] 53 for tile in self.land_manager.resource_deposits[self._deposit_resource_id]: 54 if tile.object.settlement is None: 55 available_deposits.append(tile.object) 56 if not available_deposits: 57 return BUILD_RESULT.IMPOSSIBLE 58 59 storage_class = Entities.buildings[BUILDINGS.STORAGE] 60 storage_spots = self.island.terrain_cache.get_buildability_intersection(storage_class.terrain_type, storage_class.size, 61 self.settlement.buildability_cache, self.production_builder.buildability_cache) 62 63 options = [] 64 for coords in sorted(storage_spots): 65 builder = BasicBuilder.create(BUILDINGS.STORAGE, coords, 0) 66 67 min_distance = None 68 for building in available_deposits: 69 distance = building.position.distance(builder.position) 70 if min_distance is None or min_distance > distance: 71 min_distance = distance 72 73 alignment = 0 74 for tile in self.production_builder.iter_neighbor_tiles(builder.position): 75 if tile is None: 76 continue 77 coords = (tile.x, tile.y) 78 if coords not in self.production_builder.plan or self.production_builder.plan[coords][0] != BUILDING_PURPOSE.NONE: 79 alignment += 1 80 81 value = min_distance - alignment * self.personality.alignment_coefficient 82 options.append((-value, builder)) 83 84 return self.production_builder.build_best_option(options, BUILDING_PURPOSE.STORAGE)
85
86 - def execute(self):
87 result = self._improve_deposit_coverage() 88 self._log_generic_build_result(result, 'deposit coverage storage') 89 return self._translate_build_result(result)
90
91 92 -class ClayDepositCoverageGoal(DepositCoverageGoal):
93 _deposit_resource_id = RES.RAW_CLAY 94
95 - def get_personality_name(self):
96 return 'ClayDepositCoverageGoal'
97
98 99 -class StoneDepositCoverageGoal(DepositCoverageGoal):
100 _deposit_resource_id = RES.STONE_DEPOSIT 101
102 - def get_personality_name(self):
103 return 'StoneDepositCoverageGoal'
104
105 106 -class MountainCoverageGoal(DepositCoverageGoal):
107 _deposit_resource_id = RES.RAW_IRON 108
109 - def get_personality_name(self):
110 return 'MountainCoverageGoal'
111