Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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