Пример #1
0
class TestPathing:
    """
    Test DocString
    """
    scenarios = [(f"Testing {md.bot.game_info.map_name}", {
        "map_data": md
    }) for md in get_map_datas()]

    def test_region_connectivity(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        region = map_data.where_all(base.position_tuple)[0]
        destination = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        all_possible_paths = map_data.region_connectivity_all_paths(
            start_region=region, goal_region=destination)
        for p in all_possible_paths:
            assert (destination in p), f"destination = {destination}"

        bad_request = map_data.region_connectivity_all_paths(
            start_region=region,
            goal_region=destination,
            not_through=[destination])
        assert (bad_request == [])

    def test_handle_out_of_bounds_values(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        assert (
            isinstance(reg_start, Region)
        ), f"reg_start = {reg_start},  base = {base}, position_tuple = {base.position_tuple}"
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        pts = []
        r = 10
        for i in range(50):
            pts.append(get_random_point(-500, -250, -500, -250))

        arr = map_data.get_pyastar_grid()
        for p in pts:
            arr = map_data.add_cost(p, r, arr)
        path = map_data.pathfind(p0, p1, grid=arr)
        assert (path is not None), f"path = {path}"

    def test_handle_illegal_weights(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        assert (
            isinstance(reg_start, Region)
        ), f"reg_start = {reg_start},  base = {base}, position_tuple = {base.position_tuple}"
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        pts = []
        r = 10
        for i in range(10):
            pts.append(get_random_point(20, 180, 20, 180))

        arr = map_data.get_pyastar_grid()
        for p in pts:
            arr = map_data.add_cost(p, r, arr, weight=-100)
        path = map_data.pathfind(p0, p1, grid=arr)
        assert (path is not None), f"path = {path}"

    def test_find_lowest_cost_points(self, map_data: MapData) -> None:
        cr = 7
        safe_query_radius = 14
        expected_max_distance = 2 * safe_query_radius

        influence_grid = map_data.get_air_vs_ground_grid()
        cost_point = (50, 130)
        influence_grid = map_data.add_cost(position=cost_point,
                                           radius=cr,
                                           grid=influence_grid)
        safe_points = map_data.find_lowest_cost_points(
            from_pos=cost_point, radius=safe_query_radius, grid=influence_grid)
        assert (
            safe_points[0][0], np.integer
        ), f"safe_points[0][0] = {safe_points[0][0]}, type {type(safe_points[0][0])}"
        assert isinstance(
            safe_points[0][1], np.integer
        ), f"safe_points[0][1] = {safe_points[0][1]}, type {type(safe_points[0][1])}"
        cost = influence_grid[safe_points[0]]
        for p in safe_points:
            assert (influence_grid[
                        p] == cost), f"grid type = air_vs_ground_grid, p = {p}, " \
                                     f"influence_grid[p] = {influence_grid[p]}, expected cost = {cost}"
            assert (map_data.distance(cost_point, p) < expected_max_distance)

        influence_grid = map_data.get_clean_air_grid()
        cost_point = (50, 130)
        influence_grid = map_data.add_cost(position=cost_point,
                                           radius=cr,
                                           grid=influence_grid)
        safe_points = map_data.find_lowest_cost_points(
            from_pos=cost_point, radius=safe_query_radius, grid=influence_grid)
        cost = influence_grid[safe_points[0]]
        for p in safe_points:
            assert (influence_grid[
                        p] == cost), f"grid type = clean_air_grid, p = {p}, " \
                                     f"influence_grid[p] = {influence_grid[p]}, expected cost = {cost}"
            assert (map_data.distance(cost_point, p) < expected_max_distance)

        influence_grid = map_data.get_pyastar_grid()
        cost_point = (50, 130)
        influence_grid = map_data.add_cost(position=cost_point,
                                           radius=cr,
                                           grid=influence_grid)
        safe_points = map_data.find_lowest_cost_points(
            from_pos=cost_point, radius=safe_query_radius, grid=influence_grid)
        cost = influence_grid[safe_points[0]]
        for p in safe_points:
            assert (influence_grid[
                        p] == cost), f"grid type = pyastar_grid, p = {p}, " \
                                     f"influence_grid[p] = {influence_grid[p]}, expected cost = {cost}"
            assert (map_data.distance(cost_point, p) < expected_max_distance)

        influence_grid = map_data.get_climber_grid()
        cost_point = (50, 130)
        influence_grid = map_data.add_cost(position=cost_point,
                                           radius=cr,
                                           grid=influence_grid)
        safe_points = map_data.find_lowest_cost_points(
            from_pos=cost_point, radius=safe_query_radius, grid=influence_grid)
        cost = influence_grid[safe_points[0]]
        for p in safe_points:
            assert (influence_grid[
                        p] == cost), f"grid type = climber_grid, p = {p}, " \
                                     f"influence_grid[p] = {influence_grid[p]}, expected cost = {cost}"
            assert (map_data.distance(cost_point, p) < expected_max_distance)

    def test_clean_air_grid_smoothing(self, map_data: MapData) -> None:
        default_weight = 2
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = Point2(reg_start.center)
        p1 = Point2(reg_end.center)
        grid = map_data.get_clean_air_grid(default_weight=default_weight)
        cost_points = [(87, 76), (108, 64), (97, 53)]
        cost_points = list(map(Point2, cost_points))
        for cost_point in cost_points:
            grid = map_data.add_cost(position=cost_point, radius=7, grid=grid)
        path = map_data.pathfind(start=p0, goal=p1, grid=grid, smoothing=True)
        assert (len(path) < 50)

    def test_clean_air_grid_no_smoothing(self, map_data: MapData) -> None:
        """
        non diagonal path should be longer,  but still below 250
        """
        default_weight = 2
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = Point2(reg_start.center)
        p1 = Point2(reg_end.center)
        grid = map_data.get_clean_air_grid(default_weight=default_weight)
        cost_points = [(87, 76), (108, 64), (97, 53)]
        cost_points = list(map(Point2, cost_points))
        for cost_point in cost_points:
            grid = map_data.add_cost(position=cost_point, radius=7, grid=grid)
        path = map_data.pathfind(start=p0, goal=p1, grid=grid, smoothing=False)
        assert (len(path) < 250)

    def test_air_vs_ground(self, map_data: MapData) -> None:
        default_weight = 99
        grid = map_data.get_air_vs_ground_grid(default_weight=default_weight)
        ramps = map_data.map_ramps
        path_array = map_data.pather.default_grid
        for ramp in ramps:
            for point in ramp.points:
                if path_array[point.x][point.y] == 1:
                    assert (grid[point.x][point.y] == default_weight
                            ), f"point {point}"

    def test_sensitivity(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        arr = map_data.get_pyastar_grid()
        path_pure = map_data.pathfind(p0, p1, grid=arr)
        path_sensitive_5 = map_data.pathfind(p0, p1, grid=arr, sensitivity=5)
        path_sensitive_1 = map_data.pathfind(p0, p1, grid=arr, sensitivity=1)
        assert (len(path_sensitive_5) < len(path_pure))
        assert (p in path_pure for p in path_sensitive_5)
        assert (path_sensitive_1 == path_pure)

    def test_pathing_influence(self, map_data: MapData,
                               caplog: LogCaptureFixture) -> None:
        logger.info(map_data)
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(
            map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        pts = []
        r = 10
        for i in range(50):
            pts.append(get_random_point(0, 200, 0, 200))

        arr = map_data.get_pyastar_grid()
        for p in pts:
            arr = map_data.add_cost(p, r, arr)
        path = map_data.pathfind(p0, p1, grid=arr)
        assert (path is not None)
Пример #2
0
class TestSanity:
    """
    Test DocString
    """
    scenarios = [(f"Testing {md.bot.game_info.map_name}", {
        "map_data": md
    }) for md in get_map_datas()]

    def test_polygon(self, map_data: MapData) -> None:
        for polygon in map_data.polygons:
            polygon.plot(testing=True)
            assert (polygon not in polygon.areas)
            assert (polygon.nodes == list(polygon.points))
            assert (polygon.width > 0)
            assert (polygon.area > 0)
            assert (polygon.is_inside_point(polygon.center))

            extended_pts = polygon.points.union(polygon.perimeter_points)
            assert (polygon.points == extended_pts)

            for point in extended_pts:
                assert (polygon.is_inside_point(point) is True)

                # https://github.com/BurnySc2/python-sc2/issues/62
                assert (point is not None)
                assert (type(point[0] == int))

            for point in polygon.corner_points:
                assert (point in polygon.corner_array)
            assert (polygon.buildables.free_pct is not None)

    def test_regions(self, map_data: MapData) -> None:
        for region in map_data.regions.values():
            for p in region.points:
                assert (
                    region in map_data.where_all(p)
                ), f"expected {region}, got {map_data.where_all(p)}, point {p}"
            assert (region == map_data.where(region.center))

            # coverage
            region.plot(testing=True)

    def test_ramps(self, map_data: MapData) -> None:
        for ramp in map_data.map_ramps:
            assert (len(ramp.regions) == 2), f"ramp = {ramp}"

    def test_chokes(self, map_data: MapData) -> None:
        for choke in map_data.map_chokes:
            for p in choke.points:
                assert (choke in map_data.where_all(p)), \
                    logger.error(f"<Map : {map_data}, Choke : {choke},"
                                         f" where :  {map_data.where(choke.center)} point : {choke.center}>")

    def test_vision_blockers(self, map_data: MapData) -> None:
        all_chokes = map_data.map_chokes
        for vb in map_data.map_vision_blockers:
            assert (vb in all_chokes)
            for p in vb.points:
                assert (vb in map_data.where_all(p)), \
                    logger.error(f"<Map : {map_data}, Choke : {vb},"
                                          f" where_all :  {map_data.where_all(vb.center)} point : {vb.center}>")
class TestPathing:
    """
    Test DocString
    """
    scenarios = [(f"Testing {md.bot.game_info.map_name}", {"map_data": md}) for md in get_map_datas()]

    def test_region_connectivity(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        region = map_data.where_all(base.position_tuple)[0]
        destination = map_data.where_all(map_data.bot.enemy_start_locations[0].position)[0]
        all_possible_paths = map_data.region_connectivity_all_paths(start_region=region,
                                                                    goal_region=destination)
        for p in all_possible_paths:
            assert (destination in p), f"destination = {destination}"

        bad_request = map_data.region_connectivity_all_paths(start_region=region,
                                                             goal_region=destination,
                                                             not_through=[destination])
        assert (bad_request == [])

    def test_handle_illegal_values(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        assert (isinstance(reg_start,
                           Region)), f"reg_start = {reg_start},  base = {base}, position_tuple = {base.position_tuple}"
        reg_end = map_data.where_all(map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        pts = []
        r = 10
        for i in range(50):
            pts.append(get_random_point(-500, -250, -500, -250))

        arr = map_data.get_pyastar_grid()
        for p in pts:
            arr = map_data.add_cost(p, r, arr)
        path = map_data.pathfind(p0, p1, grid=arr)
        assert (path is not None), f"path = {path}"

    def test_air_vs_ground(self, map_data: MapData) -> None:
        default_weight = 99
        grid = map_data.get_air_vs_ground_grid(default_weight=default_weight)
        ramps = map_data.map_ramps
        path_array = map_data.path_arr.T
        for ramp in ramps:
            for point in ramp.points:
                if path_array[point.x][point.y] == 1:
                    assert (grid[point.x][point.y] == default_weight)

    def test_sensitivity(self, map_data: MapData) -> None:
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        arr = map_data.get_pyastar_grid()
        path_pure = map_data.pathfind(p0, p1, grid=arr)
        path_sensitive_5 = map_data.pathfind(p0, p1, grid=arr, sensitivity=5)
        path_sensitive_1 = map_data.pathfind(p0, p1, grid=arr, sensitivity=1)
        assert (len(path_sensitive_5) < len(path_pure))
        assert (p in path_pure for p in path_sensitive_5)
        assert (path_sensitive_1 == path_pure)

    def test_pathing_influence(self, map_data: MapData, caplog: LogCaptureFixture) -> None:
        logger.info(map_data)
        base = map_data.bot.townhalls[0]
        reg_start = map_data.where_all(base.position_tuple)[0]
        reg_end = map_data.where_all(map_data.bot.enemy_start_locations[0].position)[0]
        p0 = reg_start.center
        p1 = reg_end.center
        pts = []
        r = 10
        for i in range(50):
            pts.append(get_random_point(0, 200, 0, 200))

        arr = map_data.get_pyastar_grid()
        for p in pts:
            arr = map_data.add_cost(p, r, arr)
        path = map_data.pathfind(p0, p1, grid=arr)
        assert (path is not None)