Exemple #1
0
    def __init__(self):
        super().__init__(start_state=DoubleTouchTracker.State.start)

        for state in DoubleTouchTracker.State:
            self.add_state(state)

        # FIXME: is it only restart plays?
        self.add_transition(
            DoubleTouchTracker.State.start,
            DoubleTouchTracker.State.restart_play_began, lambda:
            (main.root_play().play != None and main.root_play().play.__class__.
             is_restart() and main.game_state().is_our_restart()),
            'we start running an offensive restart play')

        self.add_transition(
            DoubleTouchTracker.State.restart_play_began,
            DoubleTouchTracker.State.kicking,
            lambda: any(bot.has_ball() for bot in main.our_robots()),
            'one of our bots has the ball')

        self.add_transition(DoubleTouchTracker.State.kicking,
                            DoubleTouchTracker.State.kicker_forbidden,
                            lambda: not self.kicker_has_possession(),
                            'kicker kicks or fumbles ball')

        self.add_transition(DoubleTouchTracker.State.kicker_forbidden,
                            DoubleTouchTracker.State.other_robot_touched,
                            lambda: self.other_robot_touching_ball(),
                            'another robot has touched the ball')
    def __init__(self):
        super().__init__(start_state=DoubleTouchTracker.State.start)

        for state in DoubleTouchTracker.State:
            self.add_state(state)

        # FIXME: is it only restart plays?
        self.add_transition(
            DoubleTouchTracker.State.start,
            DoubleTouchTracker.State.restart_play_began,
            lambda: (main.root_play().play is not None and
                     main.root_play().play.__class__.is_restart() and
                     main.game_state().is_our_restart()),
            'we start running an offensive restart play')

        self.add_transition(
            DoubleTouchTracker.State.restart_play_began,
            DoubleTouchTracker.State.kicking,
            lambda: ((any(self._touching_ball(bot) for bot in main.our_robots())) or
                     main.game_state().is_playing()) and not main.game_state().is_placement(),
            'one of our bots has the ball or the ball was kicked')

        self.add_transition(DoubleTouchTracker.State.kicking,
                            DoubleTouchTracker.State.kicker_forbidden,
                            # The ball is no longer in restart, we have begun playing
                            (lambda: main.game_state().is_playing() or
                             # We aren't in a restart anymore
                             main.root_play().play is None or
                             not main.root_play().play.__class__.is_restart()),
                            'ball has moved and is now in play')

        self.add_transition(DoubleTouchTracker.State.kicker_forbidden,
                            DoubleTouchTracker.State.other_robot_touched,
                            lambda: self.other_robot_touching_ball(),
                            'another robot has touched the ball')
Exemple #3
0
def setup():
    global _has_setup_ui
    global _defense_checkbox

    if _has_setup_ui == True:
        logging.warn("ui setup() function called more than once")
        return

    win = getMainWindow()
    if win == None:
        raise AssertionError("Unable to get a reference to the main window")

    pcTab = win.findChild(QtWidgets.QTreeView, 'plays')

    # setup play config tab
    pcTab.setModel(main.play_registry())
    pcTab.expandAll()
    pcTab.resizeColumnToContents(0)

    logging.debug("Initialized PlayConfigTab")

    # bind the play label in the ui to the name of the current play
    play_name_label = win.findChild(QtWidgets.QLabel, 'current_play_name')
    _defense_checkbox = win.findChild(QtWidgets.QCheckBox,
                                      'useDefenseCheckBox')

    main.root_play().play_changed.connect(play_name_label.setText)

    _has_setup_ui = True
def setup():
    global _has_setup_ui

    if _has_setup_ui == True:
        logging.warn("ui setup() function called more than once")
        return

    win = getMainWindow()
    if win == None:
        raise AssertionError("Unable to get a reference to the main window")

    pcTab = win.findChild(QtWidgets.QTreeView, 'plays')

    # setup play config tab
    pcTab.setModel(main.play_registry())
    pcTab.expandAll()
    pcTab.resizeColumnToContents(0)

    logging.debug("Initialized PlayConfigTab")

    # bind the play label in the ui to the name of the current play
    play_name_label = win.findChild(QtWidgets.QLabel, 'current_play_name')
    main.root_play().play_changed.connect(play_name_label.setText)

    _has_setup_ui = True
Exemple #5
0
def setup():
    global _has_setup_ui

    if _has_setup_ui == True:
        logging.warn("ui setup() function called more than once")
        return

    win = getMainWindow()
    if win == None:
        raise AssertionError("Unable to get a reference to the main window")

    pcTab = ui.play_config_tab.PlayConfigTab()
    pcTab.setObjectName('play_config')

    tabs = win.findChild(QtGui.QTabWidget, 'tabWidget')
    tabs.insertTab(0, pcTab, 'Plays')
    tabs.setCurrentIndex(0)

    logging.debug("Inserted PlayConfigTab at index zero")

    # bind the play label in the ui to the name of the current play
    play_name_label = win.findChild(QtGui.QLabel, 'current_play_name')
    main.root_play().play_changed.connect(play_name_label.setText)

    _has_setup_ui = True
    def __init__(self):
        super().__init__(start_state=DoubleTouchTracker.State.start)

        for state in DoubleTouchTracker.State:
            self.add_state(state)

        # FIXME: is it only restart plays?
        self.add_transition(DoubleTouchTracker.State.start,
            DoubleTouchTracker.State.restart_play_began,
            lambda: (main.root_play().play != None
                and main.root_play().play.__class__.is_restart()
                and main.game_state().is_our_restart()),
            'we start running an offensive restart play')

        self.add_transition(DoubleTouchTracker.State.restart_play_began,
            DoubleTouchTracker.State.kicking,
            lambda: any(bot.has_ball() for bot in main.our_robots()),
            'one of our bots has the ball')

        self.add_transition(DoubleTouchTracker.State.kicking,
            DoubleTouchTracker.State.kicker_forbidden,
            lambda: not self.kicker_has_possession(),
            'kicker kicks or fumbles ball')

        self.add_transition(DoubleTouchTracker.State.kicker_forbidden,
            DoubleTouchTracker.State.other_robot_touched,
            lambda: self.other_robot_touching_ball(),
            'another robot has touched the ball')
Exemple #7
0
    def __init__(self, defender_priorities=[20, 19]):
        super().__init__(continuous=True)

        # we could make the Defense tactic have more or less defenders, but right now we only support two
        if len(defender_priorities) != 2:
            raise RuntimeError("defender_priorities should have a length of two")

        self.add_state(Defense.State.defending, behavior.Behavior.State.running)
        self.add_state(Defense.State.clearing, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start, Defense.State.defending, lambda: True, "immediately")
        self.add_transition(
            Defense.State.defending,
            Defense.State.clearing,
            lambda: self.should_clear_ball(),
            "when it is safe to clear the ball",
        )
        self.add_transition(
            Defense.State.clearing, Defense.State.defending, lambda: not self.should_clear_ball(), "done clearing"
        )

        goalie = tactics.positions.submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=False)

        # add defenders at the specified priority levels
        for num, priority in enumerate(defender_priorities):
            defender = tactics.positions.submissive_defender.SubmissiveDefender()
            self.add_subbehavior(defender, "defender" + str(num + 1), required=False, priority=priority)

        self.debug = True

        self.win_eval = robocup.WindowEvaluator(main.system_state())
Exemple #8
0
    def __init__(self):
        super().__init__(continuous=True)

        goalie = submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=True)

        self.add_state(WingerWall.State.defending,
                       behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
                            WingerWall.State.defending, lambda: True,
                            "immediately")

        self.aggression = 1
        self.kick_eval = robocup.KickEvaluator(main.system_state())
        self.wingers = []
        self.forwards = []
        self.max_wingers = 3
        self.max_wall = 3
        self.assigned_wingers = 0
        # List of tuples of (class score, robot obj)

        self.kick_eval.excluded_robots.clear()

        for bot in main.our_robots():
            self.kick_eval.add_excluded_robot(bot)
Exemple #9
0
    def execute_running(self):
        self.recalculate()

        goalie = self.subbehavior_with_name("goalie")
        goalie.shell_id = main.root_play().goalie_id
        if goalie.shell_id == None:
            print("WARNING: No Goalie Selected")
Exemple #10
0
    def execute_running(self):
        self.recalculate()

        goalie = self.subbehavior_with_name("goalie")
        goalie.shell_id = main.root_play().goalie_id
        if goalie.shell_id == None:
            print("WARNING: No Goalie Selected")
Exemple #11
0
    def __init__(self, defender_priorities=[20, 19]):
        super().__init__(continuous=True)


        # we could make the Defense tactic have more or less defenders, but right now we only support two
        if len(defender_priorities) != 2:
            raise RuntimeError("defender_priorities should have a length of two")


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

        goalie = tactics.positions.submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=False)


        # add defenders at the specified priority levels
        for num, priority in enumerate(defender_priorities):
            defender = tactics.positions.submissive_defender.SubmissiveDefender()
            self.add_subbehavior(defender, 'defender' + str(num+1), required=False, priority=priority)


        self.debug = True
Exemple #12
0
    def __init__(self, defender_priorities=[20, 19]):
        super().__init__(continuous=True)

        if len(defender_priorities) != 2:
            raise RuntimeError("defender_priorities should have a length of 2")

        self.add_state(Defense.State.defending,
                       behavior.Behavior.State.running)
        self.add_state(Defense.State.clearing, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
                            Defense.State.defending, lambda: True,
                            "immediately")
        self.add_transition(Defense.State.defending, Defense.State.clearing,
                            lambda: self.should_clear_ball(),
                            "Clearing the ball")
        self.add_transition(Defense.State.clearing, Defense.State.defending,
                            lambda: not self.should_clear_ball(),
                            "Done clearing")

        goalie = submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=False)

        # Add the defenders
        for num, priority in enumerate(defender_priorities):
            defender = submissive_defender.SubmissiveDefender()
            self.add_subbehavior(defender,
                                 'defender' + str(num + 1),
                                 required=False,
                                 priority=priority)

        self.debug = True

        self.kick_eval = robocup.KickEvaluator(main.system_state())
Exemple #13
0
    def __init__(self, defender_priorities=[20, 19]):
        super().__init__(continuous=True)

        # we could make the Defense tactic have more or less defenders, but right now we only support two
        if len(defender_priorities) != 2:
            raise RuntimeError(
                "defender_priorities should have a length of two")

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

        goalie = tactics.positions.submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=False)

        # add defenders at the specified priority levels
        for num, priority in enumerate(defender_priorities):
            defender = tactics.positions.submissive_defender.SubmissiveDefender(
            )
            self.add_subbehavior(defender,
                                 'defender' + str(num + 1),
                                 required=False,
                                 priority=priority)

        self.debug = True

        self.win_eval = robocup.WindowEvaluator(main.system_state())
Exemple #14
0
    def execute_running(self):
        goalie = self.subbehavior_with_name("goalie")
        goalie.shell_id = main.root_play().goalie_id

        if goalie.shell_id is None:
            print("WARNING: No Goalie Selected")

        self.find_and_set_defender_location()
    def execute_running(self):
        goalie = self.subbehavior_with_name("goalie")
        goalie.shell_id = main.root_play().goalie_id

        if goalie.shell_id is None:
            print("WARNING: No Goalie Selected")

        self.find_and_set_defender_location()
Exemple #16
0
    def __init__(self):
        super().__init__(start_state=DoubleTouchTracker.State.start)

        for state in DoubleTouchTracker.State:
            self.add_state(state)

        # FIXME: is it only restart plays?
        self.add_transition(
            DoubleTouchTracker.State.start,
            DoubleTouchTracker.State.restart_play_began, lambda:
            (main.root_play().play is not None and main.root_play().play.
             __class__.is_restart() and main.game_state().is_our_restart()),
            'we start running an offensive restart play')

        self.add_transition(
            DoubleTouchTracker.State.restart_play_began,
            DoubleTouchTracker.State.kicking, lambda: (
                (any(self._touching_ball(bot)
                     for bot in main.our_robots())) or main.game_state(
                     ).is_playing()) and not main.game_state().is_placement(),
            'one of our bots has the ball or the ball was kicked')

        self.add_transition(
            DoubleTouchTracker.State.kicking,
            DoubleTouchTracker.State.kicker_forbidden,
            # The ball is no longer in restart, we have begun playing
            (
                lambda: main.game_state().is_playing() or
                # We aren't in a restart anymore
                main.root_play().play is None or not main.root_play().play.
                __class__.is_restart()),
            'ball has moved and is now in play')

        self.add_transition(DoubleTouchTracker.State.kicker_forbidden,
                            DoubleTouchTracker.State.other_robot_touched,
                            lambda: self.other_robot_touching_ball(),
                            'another robot has touched the ball')
Exemple #17
0
def setup():
    global _has_setup_ui

    if _has_setup_ui == True:
        logging.warn("ui setup() function called more than once")
        return

    win = getMainWindow()
    if win == None: raise AssertionError("Unable to get a reference to the main window")

    pcTab = ui.play_config_tab.PlayConfigTab()
    pcTab.setObjectName('play_config')

    tabs = win.findChild(QtWidgets.QTabWidget, 'tabWidget')
    tabs.insertTab(0, pcTab, 'Plays')
    tabs.setCurrentIndex(0)

    logging.debug("Inserted PlayConfigTab at index zero")

    # bind the play label in the ui to the name of the current play
    play_name_label = win.findChild(QtWidgets.QLabel, 'current_play_name')
    main.root_play().play_changed.connect(play_name_label.setText)

    _has_setup_ui = True
Exemple #18
0
    def on_enter_start(self):
        # if we have too many robots isolate one of the robots so they don't help in the play
        goalie = main.root_play(
        ).goalie_id  #.system_state().game_state.get_goalie_id()
        print(goalie)
        numRobots = len(main.our_robots()) - 4
        if (goalie != None):
            numRobots = numRobots - 1

        for i in range(numRobots):
            iso = skills.move.Move(
                robocup.Point(-constants.Field.Width / 2 + self.bot_buffer * i,
                              0))
            self.add_subbehavior(iso,
                                 'iso' + str(i),
                                 required=True,
                                 priority=1)
    def __init__(self, defender_priorities=[20, 19]):
        super().__init__(continuous=True)

        if len(defender_priorities) != 2:
            raise RuntimeError("defender_priorities should have a length of 2")

        self.add_state(Defense.State.defending,
                       behavior.Behavior.State.running)
        self.add_state(Defense.State.clearing, behavior.Behavior.State.running)

        self.add_transition(behavior.Behavior.State.start,
                            Defense.State.defending, lambda: True,
                            "immediately")
        self.add_transition(
            Defense.State.defending,
            Defense.State.clearing, lambda: self.should_clear_ball(),
            "Clearing the ball")
        self.add_transition(
            Defense.State.clearing,
            Defense.State.defending, lambda: not self.should_clear_ball(),
            "Done clearing")

        goalie = submissive_goalie.SubmissiveGoalie()
        goalie.shell_id = main.root_play().goalie_id
        self.add_subbehavior(goalie, "goalie", required=False)

        # Add the defenders
        for num, priority in enumerate(defender_priorities):
            defender = submissive_defender.SubmissiveDefender()
            self.add_subbehavior(
                defender,
                'defender' + str(num + 1),
                required=False,
                priority=priority)

        self.debug = True

        self.kick_eval = robocup.KickEvaluator(main.system_state())
Exemple #20
0
    def execute_marking(self):
        # main.system_state().draw_line(robocup.Line(self._area.get_pt(0), self._area.get_pt(1)), (127,0,255), "Defender")
        self.block_robot = self.find_robot_to_block()
        if self.block_robot is not None:
            # self.robot.add_text("Blocking Robot " + str(self.block_robot.shell_id()), (255,255,255), "RobotText")
            pass
        if self.robot.pos.near_point(robocup.Point(0, 0), self._opponent_avoid_threshold):
            self.robot.set_avoid_opponents(False)
        else:
            self.robot.set_avoid_opponents(True)

        target = None
        if self.block_robot is None:
            target = main.ball().pos + main.ball().vel * 0.3
        else:
            target = self.block_robot.pos + self.block_robot.vel * 0.3

        goal_line = robocup.Segment(
            robocup.Point(-constants.Field.GoalWidth / 2.0, 0), robocup.Point(constants.Field.GoalWidth / 2.0, 0)
        )

        self._win_eval.excluded_robots = [self.robot]

        # TODO defenders should register themselves with some static list on init
        # TODO make this happen in python-land
        # BOOST_FOREACH(Defender *f, otherDefenders)
        # {
        # 	if (f->robot)
        # 	{
        # 		_winEval.exclude.push_back(f->robot->pos);
        # 	}
        # }

        windows = self._win_eval.eval_pt_to_seg(target, goal_line)[0]

        best = None
        goalie = main.our_robot_with_id(main.root_play().goalie_id)

        if goalie is not None and self.side is not Defender.Side.center:
            for window in windows:
                if best is None:
                    best = window
                elif (
                    self.side is Defender.Side.left
                    and window.segment.center.x < goalie.pos.x
                    and window.segment.length > best.segment.length
                ):
                    best = window
                elif (
                    self.side is Defender.Side.right
                    and window.segment.center.x > goalie.pos.x
                    and window.segment.length > best.segment.length
                ):
                    best = window
        else:
            best_dist = 0
            for window in windows:
                seg = robocup.Segment(window.segment.center(), main.ball().pos)
                new_dist = seg.dist_to(self.robot.pos)
                if best is None or new_dist < best_dist:
                    best = window
                    best_dist = new_dist

        shoot_seg = None
        if best is not None:
            if self.block_robot is not None:
                dirvec = robocup.Point.direction(self.block_robot.angle * (math.pi / 180.0))
                shoot_seg = robocup.Segment(self.block_robot.pos, self.block_robot.pos + dirvec * 7.0)
            else:
                shoot_seg = robocup.Segment(main.ball().pos, main.ball().pos + main.ball().vel.normalized() * 7.0)

        need_task = False
        if best is not None:
            winseg = best.segment
            if main.ball().vel.magsq() > 0.03 and winseg.segment_intersection(shoot_seg) != None:
                self.robot.move_to(shoot_seg.nearest_point(self.robot.pos))
                self.robot.face_none()
            else:
                winsize = winseg.length()

                if winsize < constants.Ball.Radius:
                    need_task = True
                else:
                    arc = robocup.Circle(robocup.Point(0, 0), self._defend_goal_radius)
                    shot = robocup.Line(winseg.center(), target)
                    dest = [robocup.Point(0, 0), robocup.Point(0, 0)]

                    intersected, dest[0], dest[1] = shot.intersects_circle(arc)

                    if intersected:
                        self.robot.move_to(dest[0] if dest[0].y > 0 else dest[1])
                        if self.block_robot is not None:
                            self.robot.face(self.block_robot.pos)
                        else:
                            self.robot.face(main.ball().pos)
                    else:
                        need_task = True
        if need_task:
            self.robot.face(main.ball().pos)

        backVec = robocup.Point(1, 0)
        backPos = robocup.Point(-constants.Field.Width / 2, 0)
        shotVec = robocup.Point(main.ball().pos - self.robot.pos)
        backVecRot = robocup.Point(backVec.perp_ccw())
        facing_back_line = backVecRot.dot(shotVec) < 0
        if not facing_back_line and self.robot.has_ball():
            if self.robot.has_chipper():
                self.robot.chip(255)
            else:
                self.robot.kick(255)
Exemple #21
0
    def execute_area_marking(self):
        if self.robot.pos.near_point(robocup.Point(0, 0), self._opponent_avoid_threshold):
            self.robot.set_avoid_opponents(False)
        else:
            self.robot.set_avoid_opponents(True)
        goal_target = robocup.Point(0, -constants.Field.GoalDepth / 2.0)
        goal_line = robocup.Segment(
            robocup.Point(-constants.Field.GoalWidth / 2.0, 0), robocup.Point(constants.Field.GoalWidth / 2.0, 0)
        )

        if self.side is Defender.Side.left:
            goal_line.get_pt(1).x = 0
            goal_line.get_pt(1).y = 0
        if self.side is Defender.Side.right:
            goal_line.get_pt(0).x = 0
            goal_line.get_pt(0).y = 0

        for robot in main.system_state().their_robots:
            self._win_eval.excluded_robots.append(robot)

        if main.root_play().goalie_id is not None:
            self._win_eval.excluded_robots.append(main.our_robot_with_id(main.root_play().goalie_id))

            # TODO (cpp line 186)
            # windows = self._win_eval.
        windows = []

        best = None
        angle = 0.0
        for window in windows:
            if best is None:
                best = window
                angle = window.a0 - window.a1
            elif window.a0 - window.a1 > angle:
                best = window
                angle = window.a0 - window.a1

        shootline = robocup.Segment(robocup.Point(0, 0), robocup.Point(0, 0))
        if best is not None:
            angle = (best.a0 + best.a1) / 2.0
            shootline = robocup.Segment(self._win_eval.origin(), robocup.Point.direction(angle * (math.pi / 180.0)))
            main.system_state().draw_line(shootline, (255, 0, 0), "Defender")

        need_task = False
        if best is not None:
            winseg = best.segment
            arc = robocup.Circle(robocup.Point(0, 0), self._defend_goal_radius)
            shot = robocup.Line(shootline.get_pt(0), shootline.get_pt(1))
            dest = [robocup.Point(0, 0), robocup.Point(0, 0)]
            intersected, dest[0], dest[1] = shot.intersects_circle(arc)
            if intersected:
                self.robot.move_to(dest[0] if dest[0].y > 0 else dest[1])
            else:
                need_task = True
        if need_task:
            self.robot.face(main.ball().pos)

        if main.ball().pos.y < constants.Field.Length / 2.0:
            self.robot.set_dribble_speed(255)

        backVec = robocup.Point(1, 0)
        backPos = robocup.Point(-constants.Field.Width / 2, 0)
        shotVec = robocup.Point(main.ball().pos - self.robot.pos)
        backVecRot = robocup.Point(backVec.perp_ccw())
        facing_back_line = backVecRot.dot(shotVec) < 0
        if not facing_back_line and self.robot.has_ball():
            if self.robot.has_chipper():
                self.robot.chip(255)
            else:
                self.robot.kick(255)
    def execute_marking(self):
        #main.system_state().draw_line(robocup.Line(self._area.get_pt(0), self._area.get_pt(1)), (127,0,255), "Defender")
        self.block_robot = self.find_robot_to_block()
        if self.block_robot is not None:
            # self.robot.add_text("Blocking Robot " + str(self.block_robot.shell_id()), (255,255,255), "RobotText")
            pass
        if self.robot.pos.near_point(robocup.Point(0, 0),
                                     self._opponent_avoid_threshold):
            self.robot.set_avoid_opponents(False)
        else:
            self.robot.set_avoid_opponents(True)

        target = None
        if self.block_robot is None:
            target = main.ball().pos + main.ball().vel * 0.3
        else:
            target = self.block_robot.pos + self.block_robot.vel * 0.3

        goal_line = robocup.Segment(
            robocup.Point(-constants.Field.GoalWidth / 2.0, 0),
            robocup.Point(constants.Field.GoalWidth / 2.0, 0))

        self._win_eval.excluded_robots = [self.robot]

        # TODO defenders should register themselves with some static list on init
        # TODO make this happen in python-land
        # for (Defender *f :  otherDefenders)
        # {
        # 	if (f->robot)
        # 	{
        # 		_winEval.exclude.push_back(f->robot->pos);
        # 	}
        # }

        windows = self._win_eval.eval_pt_to_seg(target, goal_line)[0]

        best = None
        goalie = main.our_robot_with_id(main.root_play().goalie_id)

        if goalie is not None and self.side is not Defender.Side.center:
            for window in windows:
                if best is None:
                    best = window
                elif self.side is Defender.Side.left and window.segment.center.x < goalie.pos.x and window.segment.length > best.segment.length:
                    best = window
                elif self.side is Defender.Side.right and window.segment.center.x > goalie.pos.x and window.segment.length > best.segment.length:
                    best = window
        else:
            best_dist = 0
            for window in windows:
                seg = robocup.Segment(window.segment.center(), main.ball().pos)
                new_dist = seg.dist_to(self.robot.pos)
                if best is None or new_dist < best_dist:
                    best = window
                    best_dist = new_dist

        shoot_seg = None
        if best is not None:
            if self.block_robot is not None:
                dirvec = robocup.Point.direction(self.block_robot.angle *
                                                 (math.pi / 180.0))
                shoot_seg = robocup.Segment(
                    self.block_robot.pos, self.block_robot.pos + dirvec * 7.0)
            else:
                shoot_seg = robocup.Segment(
                    main.ball().pos,
                    main.ball().pos + main.ball().vel.normalized() * 7.0)

        need_task = False
        if best is not None:
            winseg = best.segment
            if main.ball().vel.magsq() > 0.03 and winseg.segment_intersection(
                    shoot_seg) != None:
                self.robot.move_to(shoot_seg.nearest_point(self.robot.pos))
                self.robot.face_none()
            else:
                winsize = winseg.length()

                if winsize < constants.Ball.Radius:
                    need_task = True
                else:
                    arc = robocup.Circle(robocup.Point(0, 0),
                                         self._defend_goal_radius)
                    shot = robocup.Line(winseg.center(), target)
                    dest = [robocup.Point(0, 0), robocup.Point(0, 0)]

                    intersected, dest[0], dest[1] = shot.intersects_circle(arc)

                    if intersected:
                        self.robot.move_to(
                            dest[0] if dest[0].y > 0 else dest[1])
                        if self.block_robot is not None:
                            self.robot.face(self.block_robot.pos)
                        else:
                            self.robot.face(main.ball().pos)
                    else:
                        need_task = True
        if need_task:
            self.robot.face(main.ball().pos)

        backVec = robocup.Point(1, 0)
        backPos = robocup.Point(-constants.Field.Width / 2, 0)
        shotVec = robocup.Point(main.ball().pos - self.robot.pos)
        backVecRot = robocup.Point(backVec.perp_ccw())
        facing_back_line = (backVecRot.dot(shotVec) < 0)
        if not facing_back_line and self.robot.has_ball():
            if self.robot.has_chipper():
                self.robot.chip(255)
            else:
                self.robot.kick(255)
    def execute_area_marking(self):
        if self.robot.pos.near_point(robocup.Point(0, 0),
                                     self._opponent_avoid_threshold):
            self.robot.set_avoid_opponents(False)
        else:
            self.robot.set_avoid_opponents(True)
        goal_target = robocup.Point(0, -constants.Field.GoalDepth / 2.0)
        goal_line = robocup.Segment(
            robocup.Point(-constants.Field.GoalWidth / 2.0, 0),
            robocup.Point(constants.Field.GoalWidth / 2.0, 0))

        if self.side is Defender.Side.left:
            goal_line.get_pt(1).x = 0
            goal_line.get_pt(1).y = 0
        if self.side is Defender.Side.right:
            goal_line.get_pt(0).x = 0
            goal_line.get_pt(0).y = 0

        for robot in main.system_state().their_robots:
            self._win_eval.excluded_robots.append(robot)

        if main.root_play().goalie_id is not None:
            self._win_eval.excluded_robots.append(
                main.our_robot_with_id(main.root_play().goalie_id))

        # TODO (cpp line 186)
        # windows = self._win_eval.
        windows = []

        best = None
        angle = 0.0
        for window in windows:
            if best is None:
                best = window
                angle = window.a0 - window.a1
            elif window.a0 - window.a1 > angle:
                best = window
                angle = window.a0 - window.a1

        shootline = robocup.Segment(robocup.Point(0, 0), robocup.Point(0, 0))
        if best is not None:
            angle = (best.a0 + best.a1) / 2.0
            shootline = robocup.Segment(
                self._win_eval.origin(),
                robocup.Point.direction(
                    angle * (math.pi / 180.0)))  # FIXME :no origin.
            main.system_state().draw_line(shootline, (255, 0, 0), "Defender")

        need_task = False
        if best is not None:
            winseg = best.segment
            arc = robocup.Circle(robocup.Point(0, 0), self._defend_goal_radius)
            shot = robocup.Line(shootline.get_pt(0), shootline.get_pt(1))
            dest = [robocup.Point(0, 0), robocup.Point(0, 0)]
            intersected, dest[0], dest[1] = shot.intersects_circle(arc)
            if intersected:
                self.robot.move_to(dest[0] if dest[0].y > 0 else dest[1])
            else:
                need_task = True
        if need_task:
            self.robot.face(main.ball().pos)

        if main.ball().pos.y < constants.Field.Length / 2.0:
            self.robot.set_dribble_speed(255)

        backVec = robocup.Point(1, 0)
        backPos = robocup.Point(-constants.Field.Width / 2, 0)
        shotVec = robocup.Point(main.ball().pos - self.robot.pos)
        backVecRot = robocup.Point(backVec.perp_ccw())
        facing_back_line = (backVecRot.dot(shotVec) < 0)
        if not facing_back_line and self.robot.has_ball():
            if self.robot.has_chipper():
                self.robot.chip(255)
            else:
                self.robot.kick(255)