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

Source Code for Module horizons.world.buildability.freeislandcache

 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 FreeIslandBuildabilityCache:
26 """ 27 An instance of this class is used to keep track of the unclaimed area on an island. 28 29 Instances of this class can answer the same queries as BinaryBuildabilityCache. 30 It is specialized for keeping track of the unclaimed land on an island. That way it 31 is possible to use it in conjunction with the TerrainCache to find all available 32 warehouse positions on an island. 33 34 Note that the cache is initialized with all unclaimed tiles on the island and after 35 that it can only reduce in size because it is currently impossible for land to change 36 ownership after it has been claimed by the first player. 37 """ 38
39 - def __init__(self, island):
40 self._binary_cache = BinaryBuildabilityCache(island.terrain_cache) 41 self.cache = self._binary_cache.cache # {(width, height): set((x, y), ...), ...} 42 self.island = island 43 self._init()
44
45 - def _init(self):
46 land_or_coast = self._binary_cache.terrain_cache.land_or_coast 47 coords_list = [] 48 for (coords, tile) in self.island.ground_map.items(): 49 if coords not in land_or_coast: 50 continue 51 if tile.settlement is not None: 52 continue 53 if tile.object is not None and not tile.object.buildable_upon: 54 continue 55 coords_list.append(coords) 56 self._binary_cache.add_area(coords_list)
57
58 - def remove_area(self, coords_list):
59 """Remove a list of existing coordinates from the area.""" 60 clean_list = [] 61 for coords in coords_list: 62 if coords in self._binary_cache.coords_set: 63 clean_list.append(coords) 64 self._binary_cache.remove_area(clean_list)
65
66 - def add_area(self, coords_list):
67 """Add a list of coordinates to the area.""" 68 clean_list = [] 69 for coords in coords_list: 70 if coords not in self._binary_cache.coords_set: 71 clean_list.append(coords) 72 self._binary_cache.add_area(clean_list)
73