def calculate_ship_dispersion(cls, ship_group): """ There are many solutions to solve the problem of caculating ship_group dispersion efficiently. We generally care about computing that in linear time, rather than having accurate numbers in O(n^2). We settle for a diagonal of a bounding box for the whole group. @return: dispersion factor @rtype: float """ positions = [ship.position for ship in ship_group] bottom_left = Point(min(positions, key=lambda position: position.x).x, min(positions, key=lambda position: position.y).y) top_right = Point(max(positions, key=lambda position: position.x).x, max(positions, key=lambda position: position.y).y) diagonal = bottom_left.distance_to_point(top_right) return diagonal
def __create_unit(self): """Create the produced unit now.""" productions = self._productions.values() for production in productions: assert isinstance(production, UnitProduction) self.on_building_production_finished( production.get_produced_units()) for unit, amount in production.get_produced_units().iteritems(): for i in xrange(0, amount): radius = 1 found_tile = False # search for free water tile, and increase search radius if none is found while not found_tile: for coord in Circle(self.position.center(), radius).tuple_iter(): point = Point(coord[0], coord[1]) if self.island.get_tile(point) is None: tile = self.session.world.get_tile(point) if tile is not None and tile.is_water and coord not in self.session.world.ship_map: CreateUnit(self.owner.worldid, unit, point.x, point.y).execute(self.session) found_tile = True break radius += 1
def __init__(self, radius, coords_list, random): """ Create a BuildingIndexer @param radius: int, maximum required radius of the buildings @param coords_list: list of tuples, the coordinates of the island @param random: the rng of the session """ self._offsets = Circle(Point(0, 0), radius).get_coordinates() self._map = {} for coords in coords_list: self._map[coords] = BuildingIndex(coords, random) self._add_set = set() self._remove_set = set() self._changed = False
def __iter__(self): """Return all coordinates in the shape as points.""" for x, y in self.tuple_iter(): yield Point(x, y)