Пример #1
0
def _position_optimally_objective_goalie(state: PlayerState):
    if "kick_off" in state.world_view.game_state:
        # If already close to starting position, do idle orientation
        if state.position.get_value().euclidean_distance_from(
                state.get_global_start_pos()) < 2:
            return Objective(state, lambda: actions.idle_orientation(state),
                             lambda: True)
        return Objective(
            state, lambda: actions.jog_to(state, state.get_global_start_pos()),
            lambda: True)

    optimal_position = _optimal_goalie_pos(state)

    current_position: Coordinate = state.position.get_value()
    dist = current_position.euclidean_distance_from(optimal_position)

    if dist > 6.0:
        target = optimal_position
        return Objective(state, lambda: actions.rush_to(state, target),
                         lambda: True)
    if dist > 0.6:
        difference = optimal_position - current_position
        return Objective(
            state, lambda: actions.positional_adjustment(state, difference),
            lambda: True)
    else:
        return Objective(state, lambda: actions.idle_orientation(state),
                         lambda: True)
Пример #2
0
    def should_recalculate(self, state: PlayerState):
        # Don't recalculate if there are any un-executed urgent commands
        if self.has_urgent_commands() or self.has_processed_see_update:
            return False

        deadline_reached = self.deadline <= state.now()
        return deadline_reached or self.completion_criteria()
Пример #3
0
    def test_get_non_offside_forward_team_mates_03(self):
        # Initialise myself
        state: PlayerState = PlayerState()
        state.world_view.side = "r"
        state.team_name = "Team1"
        state.position.set_value(Coordinate(1.94, -2.54), 0)

        # Set opponents
        op1 = PrecariousData(
            ObservedPlayer("Team2", 1, 4.47, 45, 0, 0, 0, 0, False,
                           Coordinate(-2.06, -4.54)), 0)
        op2 = PrecariousData(
            ObservedPlayer("Team2", 2, 5.55, -45, 0, 0, 0, 0, False,
                           Coordinate(-2.2, 1.15)), 0)
        state.world_view.other_players.append(op1)
        state.world_view.other_players.append(op2)

        # Set team_mate who is offside
        tm = PrecariousData(
            ObservedPlayer("Team1", 2, 6.08, 0, 0, 0, 0, 0, False,
                           Coordinate(-4.06, -1.54)), 0)
        state.world_view.other_players.append(tm)

        non_offside_team_mates = state.world_view.get_non_offside_forward_team_mates(
            "Team1",
            state.world_view.side,
            state.position.get_value(),
            max_data_age=1,
            min_distance_free=2,
            min_dist_from_me=1)

        self.assertEqual(
            len(non_offside_team_mates), 0,
            "In this scenario, there should be no free non-offside players")
Пример #4
0
 def test_calculate_kick_power_01(self):
     self.skipTest("Revised calculate_power function")
     ball = Ball(0.7, 180, Coordinate(0, 0), 0)
     ps = PlayerState()
     ps.world_view.ball.set_value(ball, 0)
     distance = 17
     self.assertTrue(_calculate_kick_power(ps, distance) > 100, "Should not be able to kick 17 meters with worst "
                                                               "case ball position")
Пример #5
0
 def test__calculate_ball_global_dir(self):
     self.skipTest("Ball no longer has 'last_position' attribute")
     state: PlayerState = PlayerState()
     state.body_angle.set_value(90, 1)
     ball: Ball = Ball(0, 0, Coordinate(-1.5, 1.5), 0)
     ball.last_position.set_value(Coordinate(0, 0), 0)
     state.world_view.ball.set_value(ball, 1)
     stop_ball(state)
Пример #6
0
 def test_calculate_kick_power_02(self):
     self.skipTest("Revised calculate_power function")
     ball = Ball(0, 0, Coordinate(0, 0), 0)
     ps = PlayerState()
     ps.world_view.ball.set_value(ball, 0)
     distance = 43
     self.assertTrue(_calculate_kick_power(ps, distance) < 100, "Should be able to kick 43 meters with optimal ball "
                                                               "position")
Пример #7
0
 def test_calculate_kick_power_03(self):
     self.skipTest("Revised calculate_power function")
     ball = Ball(0.4, 90, Coordinate(0, 0), 0)
     ps = PlayerState()
     ps.world_view.ball.set_value(ball, 0)
     distance = 30
     self.assertTrue(_calculate_kick_power(ps, distance) < 100, "Should be able to kick 30 meters with"
                                                               " decent ball position")
Пример #8
0
 def test_parse_hear01(self):
     ps = PlayerState()
     parsing._parse_hear("(hear 0 referee kick_off_l)", ps)
     self.assertEqual(
         ps.world_view.game_state, "kick_off_l",
         "Game state in the player state should update according to msg")
     self.assertEqual(
         ps.world_view.sim_time, 0,
         "Sim time in the player state should update according to msg")
Пример #9
0
def _optimal_goalie_pos(state: PlayerState):
    if state.team_name in configurations.GOALIE_MODEL_TEAMS:
        if state.goalie_position_strategy is not None:
            optimal_coord = Coordinate(state.goalie_position_strategy.pos_x,
                                       state.goalie_position_strategy.pos_y)
            state.goalie_position_strategy = None
            return optimal_coord
        else:
            ball: Ball = state.world_view.ball.get_value()

            y_value = clamp(ball.coord.pos_y * 0.8, -5, 5)

            return Coordinate(state.get_global_start_pos().pos_x, y_value)
    else:
        ball: Ball = state.world_view.ball.get_value()

        y_value = clamp(ball.coord.pos_y * 0.8, -5, 5)

        return Coordinate(state.get_global_start_pos().pos_x, y_value)
Пример #10
0
def determine_objective_goalie_positioning_striker(state: PlayerState):
    # If lost orientation -> blind orient
    if _lost_orientation(state):
        return Objective(state, lambda: actions.blind_orient(state),
                         lambda: True, 1)

    # If ball unknown -> locate ball
    if _ball_unknown(state):
        return Objective(state, lambda: actions.locate_ball(state),
                         lambda: True, 1)

    side = 1 if state.world_view.side == "l" else -1
    if state.world_view.sim_time > 75 and len(state.coach_commands) > 0:
        # First check for dribble, and dribble if needed
        dribble_in_commands: bool = False
        for command in state.coach_commands:
            cmd = command.get_value()
            if "dribble" in cmd and not state.goalie_position_strat_have_dribbled:
                dribble_in_commands = True
                dribble_dir = int(str(cmd).replace("(dribble ", "")[:-1])
                state.goalie_position_strat_have_dribbled = True
                return Objective(
                    state, lambda: actions.dribble(
                        state, int(dribble_dir), dribble_kick_power=20),
                    lambda: True, 1)

        # If already dribble or should not dribble
        if not dribble_in_commands or state.goalie_position_strat_have_dribbled:
            if not state.is_near_ball():
                return _rush_to_ball_objective(state)

            if state.world_view.sim_time > 75:
                for command in state.coach_commands:
                    cmd = command.get_value()
                    if "striker_target_y" in cmd:
                        target_y_value = int(cmd[cmd.index(" ") + 1:-1])

                return Objective(
                    state, lambda: actions.shoot_to(
                        state, Coordinate(55 * side, target_y_value), 75),
                    lambda: True, 1)

    return Objective(state, lambda: [], lambda: True, 1)
Пример #11
0
 def __init__(self,
              state: PlayerState,
              command_generator,
              completion_criteria=lambda: True,
              maximum_duration=1) -> None:
     self.command_generator = command_generator
     self.completion_criteria = completion_criteria
     self.deadline = state.now() + maximum_duration
     self.commands_executed = 0
     self.planned_commands: [Command] = []
     self.last_command_update_time = -1
     self.has_processed_see_update = False
Пример #12
0
def determine_objective_biptest(state: PlayerState):
    # If lost orientation -> blind orient
    if _lost_orientation(state):
        return Objective(state, lambda: actions.blind_orient(state),
                         lambda: True, 1)

    # If ball unknown -> locate ball
    if _ball_unknown(state):
        return Objective(state, lambda: actions.locate_ball(state),
                         lambda: True, 1)

    if state.position.is_value_known():
        side: int = 1 if state.world_view.side == "l" else -1
        lower_goal: Coordinate = Coordinate(-25 * side, -33)
        upper_goal: Coordinate = Coordinate(-25 * side, 33)
        if not state.is_near(upper_goal, 2):
            debug_msg("Going to left goal", "BIPTEST")
            return Objective(state, lambda: actions.rush_to(state, upper_goal),
                             lambda: state.is_near(upper_goal, 0.5), 1000)
        else:
            debug_msg("Going to right goal", "BIPTEST")
            return Objective(state, lambda: actions.rush_to(state, lower_goal),
                             lambda: state.is_near(lower_goal, 0.5), 1000)
Пример #13
0
def _position_optimally_objective(state: PlayerState):
    if "kick_off" in state.world_view.game_state:
        # If already close to starting position, do idle orientation
        if state.position.get_value().euclidean_distance_from(
                state.get_global_start_pos()) < 2:
            return Objective(state, lambda: actions.idle_orientation(state),
                             lambda: True)
        return Objective(
            state, lambda: actions.jog_to(state, state.get_global_start_pos()),
            lambda: True)

    if state.player_type == "defender":
        optimal_position = _optimal_defender_pos(state)
    elif state.player_type == "midfield":
        optimal_position = _optimal_midfielder_pos(state)
    elif state.player_type == "goalie":
        optimal_position = _optimal_goalie_pos(state)
    else:  # Striker
        optimal_position = _optimal_striker_pos(
            state)  # _optimal_attacker_pos(state)

    current_position: Coordinate = state.position.get_value()
    dist = current_position.euclidean_distance_from(optimal_position)

    if dist > 6.0:  # todo: Should they sprint if dist > 10?
        target = optimal_position
        return Objective(state, lambda: actions.rush_to(state, target),
                         lambda: True)
    if dist > 2.0:
        difference = optimal_position - current_position
        return Objective(
            state, lambda: actions.positional_adjustment(state, difference),
            lambda: True)
    else:
        return Objective(state, lambda: actions.idle_orientation(state),
                         lambda: True)
Пример #14
0
def _optimal_striker_pos(state: PlayerState) -> Coordinate:
    side = 1 if state.world_view.side == 'l' else -1
    if not state.world_view.ball.is_value_known():
        return state.get_global_play_pos()
    ball: Coordinate = state.world_view.ball.get_value().coord
    play_position = state.get_global_play_pos()
    ball_delta_y = ball.pos_y - play_position.pos_y

    if side * ball.pos_x > 0:
        # Attacking
        x_offset = ball.pos_x + side * 5
        optimal_x = clamp(play_position.pos_x + x_offset, -45, 45)

        # Used to make players position themselves closer to the goal on the y-axis when far up/down the field
        y_goal_factor = 0.982888 + 0.002871167 * abs(
            optimal_x) - 0.0000807057 * pow(optimal_x, 2)

        optimal_y = clamp(
            play_position.pos_y + ball.pos_y * 0.2 + ball_delta_y * 0.4, -30,
            30) * y_goal_factor

        if state.world_view.team_has_ball(state.team_name, max_data_age=4):
            opt_coord = Coordinate(optimal_x + (10 * side), optimal_y)
            free_pos = state.get_closest_free_position(opt_coord)
            if state.is_test_player():
                debug_msg("Free position:{0}".format(free_pos),
                          "FREE_POSITION")
            return opt_coord if free_pos is None else free_pos

        return Coordinate(optimal_x, optimal_y)
    else:
        # Defending
        optimal_x = -state.get_global_play_pos().pos_x + ball.pos_x * 0.4
        optimal_y = state.get_global_play_pos().pos_y + ball_delta_y * 0.2

        if state.world_view.team_has_ball(state.team_name, max_data_age=4):
            opt_coord = Coordinate(optimal_x + (10 * side), optimal_y)
            free_pos = state.get_closest_free_position(opt_coord)
            if state.is_test_player():
                debug_msg("Free position:{0}".format(free_pos),
                          "FREE_POSITION")
            return opt_coord if free_pos is None else free_pos

        return Coordinate(optimal_x, optimal_y)
Пример #15
0
    def test_get_non_offside_forward_team_mates_02(self):
        # Initialise myself
        state: PlayerState = PlayerState()
        state.world_view.side = "l"
        state.team_name = "Team1"
        state.position.set_value(Coordinate(-1.94, -2.54), 0)

        # Set opponents
        op1 = PrecariousData(
            ObservedPlayer("Team2", 1, 4.47, 45, 0, 0, 0, 0, False,
                           Coordinate(2.06, -4.54)), 0)
        op2 = PrecariousData(
            ObservedPlayer("Team2", 2, 9.63, -45, 0, 0, 0, 0, False,
                           Coordinate(6.4, 2.27)), 0)
        state.world_view.other_players.append(op1)
        state.world_view.other_players.append(op2)

        # Set team_mate who is offside
        tm = PrecariousData(
            ObservedPlayer("Team1", 2, 6.08, 0, 0, 0, 0, 0, False,
                           Coordinate(4.06, -1.54)), 0)
        state.world_view.other_players.append(tm)

        non_offside_team_mates = state.world_view.get_non_offside_forward_team_mates(
            "Team1",
            state.world_view.side,
            state.position.get_value(),
            max_data_age=1,
            min_distance_free=2,
            min_dist_from_me=1)

        non_off_side_player: [ObservedPlayer] = non_offside_team_mates[0]

        self.assertEqual(non_off_side_player.num, 2,
                         "Assert that the correct player is found")
        self.assertEqual(
            len(non_offside_team_mates), 1,
            "In this scenario, there should be 1 non-offside player")
Пример #16
0
def _dribble_objective(state: PlayerState):
    side = 1 if state.world_view.side == "l" else -1
    if not state.is_nearest_ball(1):
        state.mode = DEFAULT_MODE
        return determine_objective(state)

    if not state.is_near_ball(KICKABLE_MARGIN):
        return _rush_to_ball_objective(state)

    pos: Coordinate = state.position.get_value()
    if pos.euclidean_distance_from(Coordinate(52.5 * side, 0)) < 24:
        return Objective(
            state,
            lambda: actions.shoot_to(state, Coordinate(55 * side, 0), 100),
            lambda: True, 1)

    should_dribble = state.received_dribble_instruction.get_value() \
                     and state.received_dribble_instruction.last_updated_time >= state.now() - 100

    if not should_dribble:
        target = _choose_pass_target(state)
        if target is not None:
            return Objective(state,
                             lambda: actions.pass_to_player(state, target),
                             lambda: True, 1)
    else:
        state.received_dribble_instruction.set_value(False, state.now())

    if state.is_near_ball(
    ):  # todo temp: and state.action_history.has_looked_for_targets:
        target_coord: Coordinate = Coordinate(52.5 * side, 0)
        opposing_goal_dir = math.degrees(
            calculate_full_origin_angle_radians(target_coord,
                                                state.position.get_value()))
        state.action_history.has_looked_for_targets = False
        return Objective(
            state, lambda: actions.dribble(state, int(opposing_goal_dir)),
            lambda: False, 1)
    return _rush_to_ball_objective(state)
Пример #17
0
def _choose_pass_target(state: PlayerState, must_pass: bool = False):
    print("choose pass target")
    """
    If uppaal has been generated recently -> Follow strat if applicable
    If free targets forward -> Pass forward
    If no free targets forward, but i am not marked -> dribble forward
    If no free targets forward, but free targets behind, and i am marked -> Pass back
    If no free targets and i am marked -> Try to dribble anyway
    :return: Parse target or None, if dribble
    """

    # For pass chain model, if an existing target is seen by player, pass ball
    if len(state.passchain_targets) > 0:
        print("passchain longer than 0")
        for target in state.passchain_targets:
            target: PrecariousData
            if target.last_updated_time > state.now() - 40:
                print("if target update time is later than 40 seconds ago")
                target = state.find_teammate_closest_to(target.get_value(),
                                                        max_distance_delta=8.0)
                if target is not None:
                    print("TORGET ACQUIRED : ", target)
                    return target

    debug_msg(str(state.now()) + "Choosing pass target", "DRIBBLE_PASS_MODEL")
    # Act according to Possession model
    if state.dribble_or_pass_strat.is_value_known():
        if state.dribble_or_pass_strat.is_value_known(state.now() - 8):
            debug_msg(
                "Following uppaal DribbleOrPass strategy :" +
                str(state.dribble_or_pass_strat.get_value()),
                "DRIBBLE_PASS_MODEL")
            strat = state.dribble_or_pass_strat.get_value()
            state.dribble_or_pass_strat = PrecariousData.unknown()
            if DRIBBLE_INDICATOR in strat:
                if not must_pass:
                    state.statistics.use_possession_strategy()
                    debug_msg(
                        str(state.now()) + " Dribble!", "DRIBBLE_PASS_MODEL")
                    return None

            else:
                match = re.match(r'.*\(([^,]*), ([^)]*)\)', strat)
                x = float(match.group(1))
                y = float(match.group(2))
                target = state.find_teammate_closest_to(Coordinate(x, y),
                                                        max_distance_delta=3.0)
                if target is not None:
                    debug_msg(
                        str(state.now()) +
                        " DRIBBLE_PASS_MODEL : Playing to :" +
                        str(Coordinate(x, y)), "DRIBBLE_PASS_MODEL")

                    # If target is outside the no no square then return target
                    i = -1 if state.world_view.side == "l" else 1
                    is_too_far_back = True if (state.world_view.side == "l" and target.coord.pos_x < -36) \
                                              or (state.world_view.side == "r" and target.coord.pos_x > 36) else False

                    if (not is_too_far_back) and (not is_offside(
                            state, target)) and (target.coord.pos_y > -20
                                                 or target.coord.pos_y > 20):
                        state.statistics.use_possession_strategy()
                        return target
                else:
                    debug_msg(
                        str(state.now()) + "No teammate matched :" +
                        str(Coordinate(x, y)) + " Visible: " +
                        str(state.world_view.get_teammates(
                            state.team_name, 10)), "DRIBBLE_PASS_MODEL")

        # Discard strategy
        state.statistics.discard_possession_strategy()
        state.dribble_or_pass_strat = PrecariousData.unknown()

    side = state.world_view.side
    """ # TODO : TESTING ONLY ----------------------------------------------------------
    teammates = state.world_view.get_teammates(state.team_name, 2)
    if len(teammates) is not 0:
        return choice(teammates)
    # todo -------------------------------------------------------------------------- """

    am_i_marked = state.world_view.is_marked(team=state.team_name,
                                             max_data_age=4,
                                             min_distance=4)

    # If free targets forward -> Pass forward
    forward_team_mates = state.world_view.get_non_offside_forward_team_mates(
        state.team_name,
        side,
        state.position.get_value(),
        max_data_age=4,
        min_distance_free=2,
        min_dist_from_me=2)

    if len(forward_team_mates) > 0:
        # If free team mates sort by closest to opposing teams goal
        opposing_team_goal: Coordinate = Coordinate(
            52.5, 0) if side == "l" else Coordinate(-52.5, 0)
        debug_msg("forward_team_mates: " + str(forward_team_mates),
                  "PASS_TARGET")
        good_target = list(
            sorted(forward_team_mates,
                   key=lambda p: p.coord.euclidean_distance_from(
                       opposing_team_goal),
                   reverse=False))[0]
        return good_target

    # If no free targets forward, but i am not marked -> dribble forward
    if len(forward_team_mates) < 1 and not am_i_marked:
        debug_msg("No free targets forward -> Dribble!", "PASS_TARGET")
        if must_pass:
            tms = state.world_view.get_teammates(state.team_name, 5)
            if len(tms) > 0:
                return random.choice(tms)
        return None

    # If no free targets forward, but free targets behind, and i am marked -> Pass back
    behind_team_mates = state.world_view.get_free_behind_team_mates(
        state.team_name,
        side,
        state.position.get_value(),
        max_data_age=3,
        min_distance_free=3,
        min_dist_from_me=3)
    if len(behind_team_mates) > 0:
        # Get the player furthest forward and free
        opposing_team_goal: Coordinate = Coordinate(
            52.5, 0) if side == "l" else Coordinate(-52.5, 0)
        debug_msg("Behind_team_mates: " + str(behind_team_mates),
                  "PASS_TARGET")
        good_target = list(
            sorted(behind_team_mates,
                   key=lambda p: p.coord.euclidean_distance_from(
                       opposing_team_goal),
                   reverse=False))[0]
        return good_target

    # If no free targets and i am marked -> Try to dribble anyway
    debug_msg("No free targets forward and i am marked -> Dribble anyway!",
              "PASS_TARGET")
    if must_pass:
        tms = state.world_view.get_player_observations(state.team_name, 3)
        if len(tms) > 0:
            return random.choice(tms)
    return None
Пример #18
0
def _update_dribble_or_pass_model(state: PlayerState, model: UppaalModel):
    # This function edits the UPPAAL file to represent the current state of the game

    # Amount of ticks to forecast game state
    # This should be the expected time that it takes to generate the strategy
    FORECAST_TICKS = 6

    team_mates = state.world_view.get_teammates_precarious(state.team_name,
                                                           10,
                                                           min_dist=5)
    opponents = state.world_view.get_opponents_precarious(state.team_name,
                                                          10,
                                                          min_dist=0)

    # Forecast position of the ball possessor
    if state.get_y_north_velocity_vector() is not None:
        possessor_forecasted = (
            state.position.get_value().vector() +
            state.get_y_north_velocity_vector() * FORECAST_TICKS).coord()
    else:
        possessor_forecasted = state.position.get_value()

    # Forecast position of other players
    forecasted_team_positions = list(
        map(
            lambda p: p.get_value().forecasted_position(
                (state.now() - p.last_updated_time) + FORECAST_TICKS),
            team_mates))
    forecasted_opponent_positions = list(
        map(
            lambda p: p.get_value().forecasted_position(
                (state.now() - p.last_updated_time) + FORECAST_TICKS),
            opponents))
    if state.players_close_behind > 0:
        forecasted_opponent_positions.append(
            Coordinate(
                possessor_forecasted.pos_x - 1,
                possessor_forecasted.pos_y,
            ))

    # Convert to textual format understandable by uppaal
    team_pos_value = _to_3d_double_array_coordinate(forecasted_team_positions)
    opponent_pos_value = _to_3d_double_array_coordinate(
        forecasted_opponent_positions)
    possessor_val = _to_2d_double_array_coordinate(possessor_forecasted)

    model.set_global_declaration_value("TEAM_MATES",
                                       len(forecasted_team_positions))
    model.set_global_declaration_value("OPPONENTS",
                                       len(forecasted_opponent_positions))
    model.set_global_declaration_value("team_pos[TEAM_MATES][2]",
                                       team_pos_value)
    model.set_global_declaration_value("opponent_pos[OPPONENTS][2]",
                                       opponent_pos_value)
    model.set_global_declaration_value("possessor[2]", possessor_val)

    if state.is_test_player():  # Debugging
        all_posis = list(
            map(lambda p: p.get_value().coord, state.world_view.other_players))
        debug_msg(
            str(state.now()) + " All positions: " + str(all_posis),
            "DRIBBLE_PASS_MODEL")
        debug_msg(
            str(state.now()) + " Forecasted team positions: " +
            str(team_pos_value), "DRIBBLE_PASS_MODEL")
        debug_msg(
            str(state.now()) + " Forecasted opponent positions: " +
            str(opponent_pos_value), "DRIBBLE_PASS_MODEL")
        debug_msg(
            str(state.now()) + " Forecasted possessor position: " +
            str(possessor_val), "DRIBBLE_PASS_MODEL")

    return forecasted_team_positions
Пример #19
0
def _find_applicable_strat_player(state: PlayerState) -> _StrategyGenerator:
    # Matches the current state of the game to a uppaal model, if applicable

    # Check if dribble strategy model can be applied
    if state.team_name in DRIBBLE_OR_PASS_TEAMS and state.needs_dribble_or_pass_strat(
    ):
        print(state.now(), " DRIBBLE STRAT - Player : ", state.num)

        possession_dir = Path(__file__).parent / "models" / "possessionmodel"
        if not possession_dir.exists():
            os.makedirs(possession_dir)

        original_pos_model = Path(
            possession_dir).parent / "PassOrDribbleModel.xml"
        model_name = "possessionmodel{0}{1}.xml".format(
            state.world_view.side, state.num)
        new_pos_file = possession_dir / model_name

        if not new_pos_file.exists():
            f = open(new_pos_file, "x")
            copyfile(original_pos_model, new_pos_file)
            f.close()

        return _StrategyGenerator(
            "/possessionmodel/possessionmodel{0}{1}".format(
                state.world_view.side, state.num),
            _update_dribble_or_pass_model, _extract_pass_or_dribble_strategy)

    # Check if stamina strategy model can be applied
    if state.team_name in STAMINA_MODEL_TEAMS and (
            state.now() % 121) == (state.num + 1) * 10:
        print(state.num, state.team_name)
        return _StrategyGenerator(
            "/staminamodel/staminamodel{0}{1}".format(state.world_view.side,
                                                      state.num),
            _update_stamina_model_simple, _extract_stamina_solution_simple)

    # Check if the goalie defence model can be applied
    if state.team_name in GOALIE_MODEL_TEAMS and state.player_type == "goalie":
        ball_possessor = state.get_ball_possessor()

        if ball_possessor is not None and ball_possessor.coord is not None:
            side = 1 if state.world_view.side == "r" else -1
            steps_per_meter = goalie_strategy.STEPS_PER_METER
            # Convert coordinate to fit the squares from the strategy
            possessor_new_x = math.floor(ball_possessor.coord.pos_x * side /
                                         steps_per_meter) * steps_per_meter
            possessor_new_y = math.floor(
                ball_possessor.coord.pos_y / steps_per_meter) * steps_per_meter
            goalie_new_x = math.floor(state.position.get_value().pos_x * side /
                                      steps_per_meter) * steps_per_meter
            goalie_new_y = math.floor(state.position.get_value().pos_y /
                                      steps_per_meter) * steps_per_meter

            if abs(state.position.get_value().pos_x) > 52.5:
                goalie_new_x = 52 * side

            # Example: "(36.5, -19.5),(47.5, -8.5)" -> "(goalie_x, goalie_y),(player_x, player_y)"
            key = "({0}.0, {1}.0),({2}.0, {3}.0)".format(
                str(goalie_new_x), str(goalie_new_y), str(possessor_new_x),
                str(possessor_new_y))
            if key in state.goalie_position_dict.keys():
                result = state.goalie_position_dict[key]
                optimal_x = int(goalie_new_x * side + result[0] * side)
                optimal_y = int(goalie_new_y + result[1])
                optimal_coord = Coordinate(optimal_x, optimal_y)
                state.goalie_position_strategy = optimal_coord

    return None
Пример #20
0
def determine_objective_field_default(state: PlayerState):
    state.intercepting = False
    # If lost orientation -> blind orient
    if _lost_orientation(state):
        return Objective(state, lambda: actions.blind_orient(state),
                         lambda: True, 1)

    opponent_side = "r" if state.world_view.side == "l" else "l"
    # If game not started or other team starting -> Idle orientation
    if state.world_view.game_state == 'before_kick_off' or state.world_view.game_state == "kick_off_{0}".format(
            opponent_side) or "goal" in state.world_view.game_state:
        return Objective(state, lambda: actions.idle_orientation(state),
                         lambda: True, 1)

    # If some fault has been made by our team -> position optimally
    if "fault_{0}".format(
            state.world_view.side) in state.world_view.game_state:
        return _position_optimally_objective(state)

    # If we have a free kick, corner_kick, kick_in or kick_off
    #   If closest -> Go to ball and pass, else position optimally
    if state.world_view.game_state == "free_kick_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "corner_kick_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "kick_in_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "kick_off_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "offside_{0}".format(opponent_side):
        if _ball_unknown(state):
            return _locate_ball_objective(state)
        if state.is_near_ball(KICKABLE_MARGIN):
            if state.world_view.sim_time - state.action_history.last_look_for_pass_targets > 2:
                return Objective(state,
                                 lambda: actions.look_for_pass_target(state),
                                 lambda: True, 2)
            if _choose_pass_target(state, must_pass=True) is not None:
                return Objective(
                    state, lambda: actions.pass_to_player(
                        state, _choose_pass_target(state, must_pass=True)),
                    lambda: True, 1)
            else:
                return Objective(state,
                                 lambda: actions.look_for_pass_target(state),
                                 lambda: True, 2)
        elif state.is_nearest_ball(1):
            return _jog_to_ball_objective(state)
        else:
            return _position_optimally_objective(state)

    # If in dribbling mode -> Dribble
    if state.mode is DRIBBLING_MODE:
        return _dribble_objective(state)

    # If in possession mode -> Pass
    if state.mode is POSSESSION_MODE:
        return _pass_objective(state)

    # If position known, but ball not -> Locate ball
    if _ball_unknown(state):
        return _locate_ball_objective(state)

    # If in possession of ball -> dribble!
    if state.is_near_ball() and state.is_nearest_ball(1):
        state.mode = DRIBBLING_MODE
        return _dribble_objective(state)

    required_degree = calculate_required_degree(state)
    if state.is_nearest_ball(required_degree):
        intercept_actions = actions.intercept_2(state)
        state.intercepting = True
        if intercept_actions is not None:
            return Objective(state, lambda: intercept_actions)
        else:
            return _rush_to_ball_objective(state)

    # If ball not incoming -> Position optimally while looking at ball
    if not state.ball_incoming() and not configurations.USING_PASS_CHAIN_STRAT:
        if state.is_test_player():
            debug_msg(str(state.now()) + " Position optimally!", "ACTIONS")
        return _position_optimally_objective(state)

    if state.is_test_player():
        debug_msg(str(state.now()) + " Idle orientation!", "ACTIONS")

    return Objective(state, lambda: actions.idle_orientation(state),
                     lambda: True, 1)
Пример #21
0
def determine_objective_goalie_default(state: PlayerState):
    opponent_side = "r" if state.world_view.side == "l" else "l"
    # If goalie and goal_kick -> Go to ball and pass
    if state.world_view.game_state == "goal_kick_{0}".format(
            state.world_view.side) and state.num == 1:
        debug_msg(
            str(state.now()) + " | goal kick -> got to ball and pass",
            "GOALIE")
        if state.is_near_ball(KICKABLE_MARGIN):
            return _pass_objective(state, must_pass=True)
        else:
            return _jog_to_ball_objective(state)

    # If game not started or other team starting -> Idle orientation
    if state.world_view.game_state == 'before_kick_off' or state.world_view.game_state == "kick_off_{0}".format(
            opponent_side) or ("goal_r" == state.world_view.game_state
                               or "goal_l" == state.world_view.game_state):
        debug_msg(
            str(state.now()) +
            " | If game not started or other team starting -> Idle orientation",
            "GOALIE")
        return Objective(state, lambda: actions.idle_orientation(state),
                         lambda: True, 1)

    # If lost_orientation -> blind orient
    if _lost_orientation(state):
        debug_msg(
            str(state.now()) + " | lost orientation -> blind orient", "GOALIE")
        return Objective(state, lambda: actions.blind_orient(state),
                         lambda: True, 1)

    # If ball unknown -> locate ball
    if _ball_unknown(state):
        debug_msg(
            str(state.now()) + " | ball unknown -> locate ball", "GOALIE")
        return Objective(state, lambda: actions.locate_ball(state),
                         lambda: True, 1)

    # If some fault has been made by our team -> Position optimally
    if "fault_{0}".format(
            state.world_view.side) in state.world_view.game_state:
        debug_msg(
            str(state.now()) + " | Team made fault -> position optimally",
            "GOALIE")
        return _position_optimally_objective_goalie(state)

    # If we have a free kick, corner_kick, kick_in, kick_off or goal_kick
    # If closest -> Go to ball and pass, else position optimally
    if state.world_view.game_state == "free_kick_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "corner_kick_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "kick_in_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "goal_kick_{0}".format(state.world_view.side) \
            or state.world_view.game_state == "offside_{0}".format(opponent_side):
        debug_msg(
            str(state.now()) +
            " | free_kick, corner_kick, kick_in, kick_off, goal_kick, offside -> go to ball or position optimally",
            "GOALIE")
        if _ball_unknown(state):
            return _locate_ball_objective(state)
        if state.is_near_ball(KICKABLE_MARGIN):
            if state.world_view.sim_time - state.action_history.last_look_for_pass_targets > 2:
                return Objective(state,
                                 lambda: actions.look_for_pass_target(state),
                                 lambda: True, 2)
            if _choose_pass_target(state, must_pass=True) is not None:
                return Objective(
                    state, lambda: actions.pass_to_player(
                        state, _choose_pass_target(state, must_pass=True)),
                    lambda: True, 1)
            else:
                return Objective(state,
                                 lambda: actions.look_for_pass_target(state),
                                 lambda: True, 2)
        elif state.is_nearest_ball(1):
            return _jog_to_ball_objective(state)
        else:
            return _position_optimally_objective_goalie(state)

    ball: Ball = state.world_view.ball.get_value()

    # If in possession of the ball -> Pass to team mate
    if state.is_near_ball(KICKABLE_MARGIN):
        pass_target = _choose_pass_target(state)
        if pass_target is not None:
            debug_msg(
                str(state.now()) +
                " | in possession, pass target found -> pass to teammate",
                "GOALIE")
            return Objective(
                state, lambda: actions.pass_to_player(
                    state, _choose_pass_target(state)), lambda: True, 1)
        # No suitable pass target
        debug_msg(
            str(state.now()) +
            " | in possession, pass target not found -> look for pass target",
            "GOALIE")
        return Objective(state, lambda: actions.look_for_pass_target(state),
                         lambda: True, 1)

    # If ball coming to goalie inside box -> Catch ball
    positions = ball.project_ball_position(2, 0)
    if positions is not None and state.is_inside_own_box():
        debug_msg(
            str(state.now()) + " | Ball incoming inside box -> Catch ball",
            "GOALIE")
        ball_pos_1_tick: Coordinate = positions[0]
        if ball_pos_1_tick.euclidean_distance_from(
                state.position.get_value()) < CATCHABLE_MARGIN:
            return Objective(
                state, lambda: actions.catch_ball(state, ball_pos_1_tick),
                lambda: True, 1)

    # If ball coming towards us or ball will hit goal soon -> Intercept
    if (ball.will_hit_goal_within(ticks=5)
            or (state.is_nearest_ball(1) and state.is_ball_inside_own_box())):
        debug_msg(
            str(state.now()) +
            " | ball coming towards us or ball will hit goal soon -> run to ball and catch!",
            "GOALIE")
        intercept_actions = actions.intercept_2(state, "catch")
        if intercept_actions is not None:
            return Objective(state, lambda: intercept_actions)
        else:
            return _rush_to_ball_objective(state)

    # If position not alligned with ball y-position -> Adjust y-position
    if state.position.is_value_known(
    ) and state.world_view.ball.is_value_known(
    ) and not configurations.USING_PASS_CHAIN_STRAT:
        debug_msg(
            str(state.now()) + " | Position not optimal -> adjust position",
            "GOALIE")
        return _position_optimally_objective_goalie(state)

    # If nothing to do -> Face ball
    debug_msg(str(state.now()) + " | Nothing to do -> Face ball", "GOALIE")
    return Objective(state, lambda: actions.face_ball(state), lambda: True, 1)
Пример #22
0
def generate_strategy(state: PlayerState):
    result = strategy.generate_strategy_player(state)
    state.strategy_result_list.append(result)
    state.is_generating_strategy = False