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

Source Code for Module horizons.world.buildability.settlementcache

 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.world.buildability.binarycache import BinaryBuildabilityCache 
23   
24   
25 -class SettlementBuildabilityCache(BinaryBuildabilityCache):
26 """A specialized BinaryBuildabilityCache for settlements.""" 27
28 - def __init__(self, terrain_cache, settlement_ground_map):
29 super().__init__(terrain_cache) 30 self.settlement_ground_map = settlement_ground_map
31
32 - def add_area(self, coords_list):
33 """Add a list of new coordinates to the area.""" 34 land_or_coast = self.terrain_cache.land_or_coast 35 add_list = [] 36 for coords in coords_list: 37 if coords in land_or_coast: 38 add_list.append(coords) 39 if add_list: 40 super().add_area(add_list)
41
42 - def modify_area(self, coords_list):
43 """ 44 Refresh the usability of the coordinates in the given list. 45 46 This function is called with a list of coordinates on which the possibility of 47 building a building may have changed to update the underlying BinaryBuildabilityCache. 48 """ 49 50 land_or_coast = self.terrain_cache.land_or_coast 51 52 add_list = [] 53 remove_list = [] 54 for coords in coords_list: 55 assert isinstance(coords, tuple) 56 if coords not in land_or_coast or coords not in self.settlement_ground_map: 57 continue 58 59 object = self.settlement_ground_map[coords].object 60 if object is None or object.buildable_upon: 61 if coords not in self.coords_set: 62 add_list.append(coords) 63 elif coords in self.coords_set: 64 remove_list.append(coords) 65 66 if remove_list: 67 self.remove_area(remove_list) 68 if add_list: 69 super().add_area(add_list)
70