Package horizons :: Package world :: Package buildability :: Module potentialroadconnectivitycache
[hide private]
[frames] | no frames]

Source Code for Module horizons.world.buildability.potentialroadconnectivitycache

  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.constants import BUILDING_PURPOSE 
 23  from horizons.world.buildability.connectedareacache import ConnectedAreaCache 
 24   
 25   
26 -class PotentialRoadConnectivityCache:
27 """ 28 Query whether a toad connection between two sets of coordinates is possible. 29 30 This class is used by the AI to figure out whether it might be possible to build a 31 road between two sets of coordinates. Potentially because the area contains some part 32 of the AI's plan and the land it has a plan for may be either owned by the AI or not 33 yet owned by anyone. 34 """ 35
36 - def __init__(self, area_builder):
37 self._area_builder = area_builder 38 self._land_manager = area_builder.land_manager 39 self._settlement_ground_map = area_builder.settlement.ground_map 40 self._cache = ConnectedAreaCache() 41 self.area_numbers = self._cache.area_numbers # {(x, y): area id, ...}
42
43 - def modify_area(self, coords_list):
44 """ 45 Refresh the usability of the coordinates in the given list. 46 47 This function is called with a list of coordinates on which the possibility of 48 building a road may have changed. It figures out whether it is possible to build 49 a road on (x, y) and updates the underlying ConnectedAreaCache accordingly. 50 """ 51 52 add_list = [] 53 remove_list = [] 54 55 for coords in coords_list: 56 if coords not in self._settlement_ground_map: 57 if coords in self.area_numbers: 58 remove_list.append(coords) 59 elif coords in self._land_manager.coastline: 60 if coords in self.area_numbers: 61 remove_list.append(coords) 62 elif coords in self._land_manager.roads: 63 if coords not in self.area_numbers: 64 add_list.append(coords) 65 elif coords in self._area_builder.plan: 66 if self._area_builder.plan[coords][0] == BUILDING_PURPOSE.NONE: 67 if coords not in self.area_numbers: 68 add_list.append(coords) 69 else: 70 assert self._area_builder.plan[coords][0] != BUILDING_PURPOSE.ROAD 71 if coords in self.area_numbers: 72 remove_list.append(coords) 73 else: 74 if coords in self.area_numbers: 75 remove_list.append(coords) 76 77 if add_list: 78 self._cache.add_area(add_list) 79 if remove_list: 80 self._cache.remove_area(remove_list)
81
82 - def is_connection_possible(self, coords_set1, coords_set2):
83 """Return True if and only if it is possible to connect the two coordinate sets. 84 85 More specifically, it returns True if and only if it is possible to build a toad 86 from some (x1, y1) in coords_set1 to some (x2, y2) in coords_set2 entirely within 87 the area. This is done cheaply using the underlying ConnectedAreaCache. 88 """ 89 90 areas1 = set() 91 for coords in coords_set1: 92 if coords in self.area_numbers: 93 areas1.add(self.area_numbers[coords]) 94 for coords in coords_set2: 95 if coords in self.area_numbers: 96 if self.area_numbers[coords] in areas1: 97 return True 98 return False
99