<commit_before>import robocup
import single_robot_composite_behavior
import behavior
import role_assignment
import constants
import evaluation.ball
import skills
import main
import enum
import math


class Goalie(single_robot_composite_behavior.SingleRobotCompositeBehavior):

    MaxX = constants.Field.GoalWidth / 2.0
    RobotSegment = robocup.Segment(robocup.Point(-MaxX, constants.Robot.Radius),
                                    robocup.Point(MaxX, constants.Robot.Radius))

    class State(enum.Enum):
        defend = 1          # TODO: clarify difference between block and defend
        block = 2
        intercept = 3
        clear = 4           # kick/chip the ball out if it's in our goal zone
        setup_penalty = 5   # get in place for a penalty shot
        chill = 6           # if we can't see the ball, we just sit in the center in front of our goal


    def __init__(self):
        super().__init__(continuous=True)

        for substate in Goalie.State:
            self.add_state(substate, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
            Goalie.State.chill,
            lambda: True,
            "immediately")

        self.add_transition(Goalie.State.chill,
            Goalie.State.defend,
            lambda: main.ball().valid,
            "ball is valid")

        non_chill_states = [s for s in Goalie.State if s != Goalie.State.chill]

        # if ball is invalid, chill
        for state in non_chill_states:
            self.add_transition(state,
                Goalie.State.chill,
                lambda: not main.ball().valid,
                "ball is invalid")

        for state in non_chill_states:
            self.add_transition(state,
                Goalie.State.setup_penalty,
                lambda: main.game_state().is_their_penalty() and not main.game_state().playing(),
                "opponent penalty shot about to happen")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.intercept]:
            self.add_transition(state,
                Goalie.State.intercept,
                lambda: evaluation.ball.is_moving_towards_our_goal(),
                "ball coming towards our goal")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.clear]:
            self.add_transition(state,
                Goalie.State.clear,
                lambda: evaluation.ball.is_in_our_goalie_zone() and
                        not evaluation.ball.is_moving_towards_our_goal(),
                "ball in our goalie box, but not headed toward goal")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.defend]:
            self.add_transition(state,
                Goalie.State.defend,
                lambda: not evaluation.ball.is_in_our_goalie_zone() and
                        not evaluation.ball.is_moving_towards_our_goal() and
                        evaluation.ball.opponent_with_ball() == None,
                'not much going on')



    # note that execute_running() gets called BEFORE any of the execute_SUBSTATE methods gets called
    def execute_running(self):
        if self.robot != None:
            self.robot.face(main.ball().pos)


    def execute_chill(self):
        if self.robot != None:
            self.robot.move_to(robocup.Point(0, constants.Robot.Radius))


    def execute_setup_penalty(self):
        pt = Point(0, Field_PenaltyDist)
        penalty_kicker = min(main.opponent_robots(), key=lambda r: (r.pos - pt).mag())
        angle = math.pi * penalty_kicker.angle / 180.0
        shot_line = Line(penalty_kicker.pos,
                            Point(penalty_kicker.pos.x + math.cos(angle)),)

        dest = shot_line.intersection(Goalie.RobotSegment)
        if dest == None:
            self.robot.move_to(robocup.Point(0, constants.Robot.Radius))
        else:
            dest.x = max(-Goalie.MaxX + constants.Robot.Radius, dest.x)
            dest.y = min(Goalie.MaxX - constants.Robot.Radius, dest.x)<commit_msg>more goalie fixes<commit_after>import robocup
import single_robot_composite_behavior
import behavior
import role_assignment
import constants
import evaluation.ball
import skills
import main
import enum
import math


class Goalie(single_robot_composite_behavior.SingleRobotCompositeBehavior):

    MaxX = constants.Field.GoalWidth / 2.0
    RobotSegment = robocup.Segment(robocup.Point(-MaxX, constants.Robot.Radius),
                                    robocup.Point(MaxX, constants.Robot.Radius))

    class State(enum.Enum):
        defend = 1          # TODO: clarify difference between block and defend
        block = 2
        intercept = 3
        clear = 4           # kick/chip the ball out if it's in our goal zone
        setup_penalty = 5   # get in place for a penalty shot
        chill = 6           # if we can't see the ball, we just sit in the center in front of our goal


    def __init__(self):
        super().__init__(continuous=True)

        for substate in Goalie.State:
            self.add_state(substate, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
            Goalie.State.chill,
            lambda: True,
            "immediately")

        self.add_transition(Goalie.State.chill,
            Goalie.State.defend,
            lambda: main.ball().valid,
            "ball is valid")

        non_chill_states = [s for s in Goalie.State if s != Goalie.State.chill]

        # if ball is invalid, chill
        for state in non_chill_states:
            self.add_transition(state,
                Goalie.State.chill,
                lambda: not main.ball().valid,
                "ball is invalid")

        for state in non_chill_states:
            self.add_transition(state,
                Goalie.State.setup_penalty,
                lambda: main.game_state().is_their_penalty() and not main.game_state().playing(),
                "opponent penalty shot about to happen")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.intercept]:
            self.add_transition(state,
                Goalie.State.intercept,
                lambda: evaluation.ball.is_moving_towards_our_goal(),
                "ball coming towards our goal")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.clear]:
            self.add_transition(state,
                Goalie.State.clear,
                lambda: evaluation.ball.is_in_our_goalie_zone() and
                        not evaluation.ball.is_moving_towards_our_goal(),
                "ball in our goalie box, but not headed toward goal")

        for state in [s2 for s2 in non_chill_states if s2 != Goalie.State.defend]:
            self.add_transition(state,
                Goalie.State.defend,
                lambda: not evaluation.ball.is_in_our_goalie_zone() and
                        not evaluation.ball.is_moving_towards_our_goal() and
                        evaluation.ball.opponent_with_ball() == None,
                'not much going on')



    # note that execute_running() gets called BEFORE any of the execute_SUBSTATE methods gets called
    def execute_running(self):
        if self.robot != None:
            self.robot.face(main.ball().pos)


    def execute_chill(self):
        if self.robot != None:
            self.robot.move_to(robocup.Point(0, constants.Robot.Radius))


    def execute_setup_penalty(self):
        pt = Point(0, Field_PenaltyDist)
        penalty_kicker = min(main.opponent_robots(), key=lambda r: (r.pos - pt).mag())
        angle_rad = penalty_kicker.angle * constants.DegreesToRadians
        shot_line = Line(penalty_kicker.pos, penalty_kicker.pos + robocup.Point.direction(angle_rad))

        dest = shot_line.intersection(Goalie.RobotSegment)
        if dest == None:
            self.robot.move_to(robocup.Point(0, constants.Robot.Radius))
        else:
            dest.x = max(-Goalie.MaxX + constants.Robot.Radius, dest.x)
            dest.y = min(Goalie.MaxX - constants.Robot.Radius, dest.x)
{'fraction_non_alphanumeric': 0.08588118319661944, 'fraction_numerical': 0.005468555804126274, 'mean_word_length': 2.098575279168271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4616191', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2327, 'n_words': 568}