class SimpleCollectorAreaCache(object): """ A specialized PartialBinaryBuildabilityCache for keeping track of collector coverage. The AI uses instances of this class to figure out where it can place buildings such that at least some part of the building would be covered by a general collector. It is a simple version in that it doen't check whether a road to the corresponding collector would be possible. """ def __init__(self, terrain_cache): self.terrain_cache = terrain_cache self._area_cache = PartialBinaryBuildabilityCache(terrain_cache) self.cache = self._area_cache.cache # {(width, height): set((x, y), ...), ...} self._buildings = set() self._area_coverage = {} def add_building(self, building): """Take the the coverage area of the given building into account.""" self._buildings.add(building) new_coords_list = [] for coords in building.position.get_radius_coordinates( building.radius, True): if coords not in self.terrain_cache.land_or_coast: continue if coords not in self._area_coverage: self._area_coverage[coords] = 1 new_coords_list.append(coords) else: self._area_coverage[coords] += 1 for coords in new_coords_list: assert coords in self.terrain_cache.land_or_coast self._area_cache.add_area(new_coords_list) def remove_building(self, building): """Stop taking the coverage area of the given building into account.""" self._buildings.remove(building) removed_coords_list = [] for coords in building.position.get_radius_coordinates( building.radius, True): if coords not in self.terrain_cache.land_or_coast: continue if self._area_coverage[coords] == 1: removed_coords_list.append(coords) del self._area_coverage[coords] else: self._area_coverage[coords] -= 1 self._area_cache.remove_area(removed_coords_list)
class SimpleCollectorAreaCache(object): """ A specialized PartialBinaryBuildabilityCache for keeping track of collector coverage. The AI uses instances of this class to figure out where it can place buildings such that at least some part of the building would be covered by a general collector. It is a simple version in that it doen't check whether a road to the corresponding collector would be possible. """ def __init__(self, terrain_cache): self.terrain_cache = terrain_cache self._area_cache = PartialBinaryBuildabilityCache(terrain_cache) self.cache = self._area_cache.cache # {(width, height): set((x, y), ...), ...} self._buildings = set() self._area_coverage = {} def add_building(self, building): """Take the the coverage area of the given building into account.""" self._buildings.add(building) new_coords_list = [] for coords in building.position.get_radius_coordinates(building.radius, True): if coords not in self.terrain_cache.land_or_coast: continue if coords not in self._area_coverage: self._area_coverage[coords] = 1 new_coords_list.append(coords) else: self._area_coverage[coords] += 1 for coords in new_coords_list: assert coords in self.terrain_cache.land_or_coast self._area_cache.add_area(new_coords_list) def remove_building(self, building): """Stop taking the coverage area of the given building into account.""" self._buildings.remove(building) removed_coords_list = [] for coords in building.position.get_radius_coordinates(building.radius, True): if coords not in self.terrain_cache.land_or_coast: continue if self._area_coverage[coords] == 1: removed_coords_list.append(coords) del self._area_coverage[coords] else: self._area_coverage[coords] -= 1 self._area_cache.remove_area(removed_coords_list)
def setUp(self): super(TestPartialBinaryBuildabilityCache, self).setUp() coords_list = [] for x in xrange(10): for y in xrange(10): coords_list.append((x, y)) self.terrain_cache = MockTerrainBuildabilityCache(coords_list) self.buildability_cache = PartialBinaryBuildabilityCache(self.terrain_cache)
def __init__(self, terrain_cache): self.terrain_cache = terrain_cache self._area_cache = PartialBinaryBuildabilityCache(terrain_cache) self.cache = self._area_cache.cache # {(width, height): set((x, y), ...), ...} self._buildings = set() self._area_coverage = {}