Exemple #1
0
    def test_GoBehind(self):
        # TODO: faire davantage de cas de test
        distance_behind = 500

        # test avec une droite quelconque
        self.go_behind = GoBehind(self.game_state, self.player_id, Position(1.5, 2.3), Position(18.3, 27.8),
                                  distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(self.player_id, AICommandType.MOVE,
                                **{"pose_goal": Pose(Position(-273, -415), 0.9882)})
        # AICommand(Pose(Position(-273, -415), 0.9882), 0)
        self.assertEqual(aicmd_obtenu, aicmd_cible)

        # test avec une droite verticale
        self.go_behind = GoBehind(self.game_state, self.player_id, Position(1000, 250.3), Position(1000, 725.8),
                                  distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(self.player_id, AICommandType.MOVE,
                                **{"pose_goal": Pose(Position(1000, -249), 1.5707)})
        # AICommand(Pose(Position(1000, -249), 1.5707), 0)
        self.assertEqual(aicmd_obtenu, aicmd_cible)

        # test avec une droite horizontale
        self.go_behind = GoBehind(self.game_state, self.player_id, Position(175.8, -200.34), Position(-276.8, -200.34),
                                  distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(self.player_id, AICommandType.MOVE,
                                **{"pose_goal": Pose(Position(675, -200), -3.1415)})
        self.assertEqual(aicmd_obtenu, aicmd_cible)
Exemple #2
0
    def get_behind_ball(self):
        self.status_flag = Flags.WIP

        player_x = self.player.pose.position.x
        player_y = self.player.pose.position.y

        ball_x = self.game_state.ball_position.x
        ball_y = self.game_state.ball_position.y

        vector_player_2_ball = np.array([ball_x - player_x, ball_y - player_y])
        vector_player_2_ball /= np.linalg.norm(vector_player_2_ball)

        if self._is_player_opposing_ball_and_target():
            self.next_state = self.push_ball
            self.last_ball_position = self.game_state.ball_position
        else:
            # self.debug.add_log(4, "Distance from ball: {}".format(dist))
            self.next_state = self.get_behind_ball
        return GoBehind(self.game_state,
                        self.player,
                        self.game_state.ball_position,
                        self.target.position,
                        120,
                        pathfinder_on=True,
                        orientation='back')
Exemple #3
0
    def go_between_ball_and_target(self):
        self.status_flag = Flags.WIP
        ball = self.game_state.get_ball_position()
        ball_velocity = self.game_state.get_ball_velocity().conv_2_np()
        if np.linalg.norm(ball_velocity) > 50:
            self.target = Pose(
                Position.from_np(ball.conv_2_np() - ball_velocity), 0)
            dist_behind = np.linalg.norm(ball_velocity) + 1 / np.sqrt(
                np.linalg.norm(ball_velocity))
        else:
            self.target = None
            dist_behind = 250
        if self.target is None:
            if self.game_state.get_our_team_color() == 0:  # yellow
                self.target = Pose(
                    self.game_state.const["FIELD_GOAL_BLUE_MID_GOAL"], 0)
            else:
                self.target = Pose(
                    self.game_state.const["FIELD_GOAL_YELLOW_MID_GOAL"], 0)
        if self._is_player_towards_ball_and_target():
            self.next_state = self.grab_ball
        else:
            self.next_state = self.go_between_ball_and_target

        return GoBehind(self.game_state, self.player, ball,
                        self.target.position, dist_behind)
Exemple #4
0
    def get_behind_ball(self):
        self.status_flag = Flags.WIP

        player_x = self.player.pose.position.x
        player_y = self.player.pose.position.y

        ball_x = self.game_state.get_ball_position().x
        ball_y = self.game_state.get_ball_position().y

        vector_player_2_ball = np.array([ball_x - player_x, ball_y - player_y])
        vector_player_2_ball /= np.linalg.norm(vector_player_2_ball)

        if self._is_player_towards_ball_and_target():
            self.last_time = time.time()
            self.next_state = self.grab_ball
        else:
            self.next_state = self.get_behind_ball
        return GoBehind(
            self.game_state,
            self.player,
            self.game_state.get_ball_position() + Position(
                vector_player_2_ball[0] * 70, vector_player_2_ball[1] * 70),
            self.target.position,
            self.game_state.const["DISTANCE_BEHIND"],
            pathfinding=True)
Exemple #5
0
    def test_GoBehind(self):
        # TODO: faire davantage de cas de test
        distance_behind = 500

        # test avec une droite verticale
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(1000, 250.3), Position(1000, 725.8),
                                  distance_behind)
        aicmd_obtenu = self.go_behind.exec()
        aicmd_expected = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(1000, -249.700), 1.5707)})
        self.assertEqual(aicmd_obtenu, aicmd_expected)

        # test avec une droite quelconque
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(1.5, 2.3), Position(18.3, 27.8),
                                  distance_behind)
        aicmd_obtenu = self.go_behind.exec()
        aicmd_expected = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(-273.579, -415.230), 0.9882)})

        self.assertEqual(aicmd_obtenu, aicmd_expected)

        # test avec une droite horizontale
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(175.8, -200.34),
                                  Position(-276.8, -200.34), distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(675.800, 99.660), -2.601)})
        self.assertEqual(aicmd_obtenu, aicmd_cible)
Exemple #6
0
    def go_behind_ball(self):
        ball_position = self.game_state.get_ball_position()

        if canGetBall(self.game_state, self.player_id, ball_position):
            self.next_state = self.grab_ball
        else:
            self.next_state = self.go_behind_ball

        return GoBehind(self.game_state, self.player_id, ball_position, Position(0, 0), DISTANCE_BEHIND)
Exemple #7
0
    def test_GoBehind(self):
        # TODO: faire davantage de cas de test
        distance_behind = 500

        # test avec une droite quelconque
        self.go_behind = GoBehind(self.game_state, self.player_id,
                                  Position(1.5, 2.3), Position(18.3, 27.8),
                                  distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(Pose(Position(-273, -415), 0.9882), 0)
        self.assertEqual(aicmd_obtenu, aicmd_cible)

        # test avec une droite verticale
        self.go_behind = GoBehind(self.game_state, self.player_id,
                                  Position(1000, 250.3), Position(1000, 725.8),
                                  distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(Pose(Position(1000, -249), 1.5707), 0)
        self.assertEqual(aicmd_obtenu, aicmd_cible)

        # test avec une droite horizontale
        self.go_behind = GoBehind(self.game_state, self.player_id,
                                  Position(175.8, -200.34),
                                  Position(-276.8, -200.34), distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(Pose(Position(675, -200), -3.1415), 0)
        self.assertEqual(aicmd_obtenu, aicmd_cible)
Exemple #8
0
    def go_behind_ball(self):
        ball_position = self.game_state.get_ball_position()
        if not self.game_state.game.field.is_inside_goal_area(
                ball_position, self.is_yellow):
            self.next_state = self.protect_goal
        else:
            if can_get_ball(self.game_state, self.player_id, ball_position):
                self.next_state = self.grab_ball
            else:
                self.next_state = self.go_behind_ball

        return GoBehind(self.game_state, self.player, ball_position,
                        Position(0, 0), DISTANCE_BEHIND)
Exemple #9
0
    def get_behind_ball(self):
        self.status_flag = Flags.WIP
        player = self.player.pose.position
        ball = self.game_state.ball_position
        target = self.game_state.get_player_position(self.target_id)

        vector_player_2_ball = ball - player
        vector_player_2_ball /= vector_player_2_ball.norm

        if self._is_player_towards_ball_and_target():
            self.next_state = self.grab_ball
        else:
            self.next_state = self.get_behind_ball
        return GoBehind(self.player, self.game_state.ball_position, target,
                        120)
Exemple #10
0
    def get_behind_ball(self):
        self.status_flag = Flags.WIP
        player = self.player.pose.position.conv_2_np()
        ball = self.game_state.get_ball_position().conv_2_np()
        target = self.game_state.get_player_position(self.target_id, True).conv_2_np()

        vector_player_2_ball = ball - player
        vector_player_2_ball /= np.linalg.norm(vector_player_2_ball)

        if self._is_player_towards_ball_and_target():
            self.next_state = self.grab_ball
        else:
            self.next_state = self.get_behind_ball
        return GoBehind(self.game_state, self.player, self.game_state.get_ball_position(),
                        Position.from_np(target), 120, pathfinder_on=True)
Exemple #11
0
    def get_behind_ball(self):
        self.status_flag = Flags.WIP

        player_x = self.player.pose.position.x
        player_y = self.player.pose.position.y

        ball_x = self.game_state.get_ball_position().x
        ball_y = self.game_state.get_ball_position().y

        vector_player_2_ball = np.array([ball_x - player_x, ball_y - player_y])
        vector_player_2_ball /= np.linalg.norm(vector_player_2_ball)

        if self._is_player_towards_ball_and_target():
            self.next_state = self.grab_ball
        else:
            self.next_state = self.get_behind_ball
        return GoBehind(self.game_state,
                        self.player,
                        self.game_state.get_ball_position(),
                        self.target.position,
                        120,
                        pathfinding=True)
Exemple #12
0
 def _generate_move_to(self):
     go_behind = GoBehind(self.game_state, self.player_id, self.game_state.get_ball_position(), self.target.position,
                          DISTANCE_BEHIND)
     destination = go_behind.exec().move_destination
     return GoToPosition(self.game_state, self.player_id, destination)
Exemple #13
0
class TestActions(unittest.TestCase):
    def setUp(self):
        # ToDo : Use mock instead of actual objects
        self.game_state = GameState()
        self.game = Game()
        self.game.set_referee(Referee())
        game_world = ReferenceTransferObject(self.game)
        game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW))
        self.game_state.set_reference(game_world)
        self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID)

    def test_move_to(self):
        A_CRUISE_SPEED = 0.1
        self.pose = Pose(Position(0, 0), 0.0)
        self.move = MoveToPosition(self.game_state, self.a_player, self.pose,
                                   False, A_CRUISE_SPEED)
        return_cmd = self.move.exec()
        expected_cmd = AICommand(
            self.a_player, AICommandType.MOVE, **{
                "pose_goal": self.pose,
                "pathfinder_on": False,
                "cruise_speed": A_CRUISE_SPEED
            })
        self.assertEqual(return_cmd, expected_cmd)

        self.pose = Pose(Position(0.5, 0.3), 3.2)
        self.move = MoveToPosition(self.game_state, self.a_player, self.pose,
                                   False, A_CRUISE_SPEED)
        self.assertEqual(
            MoveToPosition.exec(self.move),
            AICommand(
                self.a_player, AICommandType.MOVE, **{
                    "pose_goal": self.pose,
                    "pathfinder_on": False,
                    "cruise_speed": A_CRUISE_SPEED
                }))

    def test_idle(self):
        idle = Idle(self.game_state, self.a_player)
        expected_command = AICommand(
            self.a_player,
            AICommandType.STOP,
            control_loop_type=AIControlLoopType.POSITION)
        actual_command = Idle.exec(idle)
        self.assertEqual(actual_command, expected_command)

    def test_GrabBall(self):
        self.grab_ball = GetBall(self.game_state, self.a_player)
        self.game_state.set_ball_position(Position(5, 0), A_DELTA_T)
        ai_cmd = self.grab_ball.exec()
        ball_position = self.game_state.get_ball_position()
        destination_orientation = (ball_position -
                                   self.a_player.pose.position).angle()
        destination_pose = Pose(ball_position, destination_orientation)
        ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE,
                                    **{"pose_goal": destination_pose})
        self.assertEqual(ai_cmd, ai_cmd_expected)

        grab_pose = Pose(Position(-5, 5), 3 * pi / 4)
        self.game_state.set_ball_position(Position(-5, 5), A_DELTA_T)
        ai_cmd = self.grab_ball.exec()
        ai_cmd_expected = AICommand(self.a_player, AICommandType.MOVE,
                                    **{"pose_goal": grab_pose})
        self.assertEqual(ai_cmd, ai_cmd_expected)

    @unittest.skip("GoBetween does not actually go in between")
    def test_GoBetween(self):
        # test avec une droite verticale
        POS_TOP = Position(100, 100)
        POS_BOTTOM = Position(100, -100)
        POS_TARGET = Position(200, 0)
        POS_INBETWEEN = Position(100, 0)
        self.go_between = GoBetween(self.game_state, self.a_player, POS_TOP,
                                    POS_BOTTOM, POS_TARGET)
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(POS_INBETWEEN, 0)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test avec une droite horizontale
        self.go_between = GoBetween(self.game_state, self.a_player,
                                    Position(100, 100), Position(-100, 100),
                                    Position(0, 200))
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(Position(0, 100), pi / 2)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test avec une droite quelconque
        self.go_between = GoBetween(self.game_state, self.a_player,
                                    Position(0, 500), Position(500, 0),
                                    Position(-300, -300))
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(Position(250, 250), -3 * pi / 4)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test destination calculée derrière position1
        self.go_between = GoBetween(self.game_state, self.a_player,
                                    Position(1000, 75), Position(1500, -250),
                                    Position(0, 0), 0)
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(Position(1000, 75), -3.067)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test destination calculée derrière position2
        self.go_between = GoBetween(self.game_state, self.a_player,
                                    Position(-100, 50), Position(-50, 50),
                                    Position(-60.0 + sqrt(3), 51.0), 10)
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(Position(-60, 50), 0.5235)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test correction pour respecter la distance minimale
        self.go_between = GoBetween(self.game_state, self.a_player,
                                    Position(-500, 25), Position(1, 25),
                                    Position(-179, 0), 180)
        ai_cmd = self.go_between.exec().pose_goal
        ai_cmd_expected = Pose(Position(-179, 25), -pi / 2)
        self.assertEqual(ai_cmd, ai_cmd_expected)

        # test distance entre les positions insuffisantes
        self.assertRaises(AssertionError, GoBetween, self.game_state,
                          self.a_player, Position(1, 1), Position(-1, -1), 50)

    def test_GoBehind(self):
        # TODO: faire davantage de cas de test
        distance_behind = 500

        # test avec une droite verticale
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(1000, 250.3), Position(1000, 725.8),
                                  distance_behind)
        aicmd_obtenu = self.go_behind.exec()
        aicmd_expected = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(1000, -249.700), 1.5707)})
        self.assertEqual(aicmd_obtenu, aicmd_expected)

        # test avec une droite quelconque
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(1.5, 2.3), Position(18.3, 27.8),
                                  distance_behind)
        aicmd_obtenu = self.go_behind.exec()
        aicmd_expected = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(-273.579, -415.230), 0.9882)})

        self.assertEqual(aicmd_obtenu, aicmd_expected)

        # test avec une droite horizontale
        self.go_behind = GoBehind(self.game_state, self.a_player,
                                  Position(175.8, -200.34),
                                  Position(-276.8, -200.34), distance_behind)
        aicmd_obtenu = GoBehind.exec(self.go_behind)
        aicmd_cible = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(675.800, 99.660), -2.601)})
        self.assertEqual(aicmd_obtenu, aicmd_cible)

    def test_kick(self):
        # test avec la valeur 0 (nulle)
        target = Pose(Position(1, 1))
        ball_position = Position(5, 0)
        self.game_state.set_ball_position(ball_position, A_DELTA_T)
        expected_cmd = AICommand(
            self.a_player, AICommandType.MOVE, **{
                "pose_goal": Pose(ball_position, 0.785),
                "charge_kick": True,
                "kick": True,
                "pathfinder_on": True,
                "cruise_speed": 0.1,
                "end_speed": 0
            })
        return_cmd = Kick(self.game_state,
                          self.a_player,
                          force=0,
                          target=target).exec()
        self.assertEqual(expected_cmd, return_cmd)

        # test avec la valeur 1 (force maximale)
        expected_cmd.kick_strength = 1
        return_cmd = Kick(self.game_state, self.a_player, 1,
                          target=target).exec()
        self.assertEqual(return_cmd, expected_cmd)

        # test avec la valeur 0.3 (force intermediaire)
        expected_cmd.kick_strength = 0.3
        return_cmd = Kick(self.game_state, self.a_player, 0.3,
                          target=target).exec()
        self.assertEqual(return_cmd, expected_cmd)

    @unittest.skip("I got lazy, didn't want to review all of the protectgoal.")
    def test_ProtectGoal(self):
        # test de base
        self.game_state.game.friends.players[0].update(Pose(Position(4450,
                                                                     10)))
        self.game_state.game.ball.set_position(Position(0, 0), 0)
        self.protectGoal = ProtectGoal(self.game_state, 0)

        aicmd_obtenu = self.protectGoal.exec()
        aicmd_cible = AICommand(self.a_player, AICommandType.MOVE,
                                **{"pose_goal": Pose(Position(4000, 0), -pi)})
        self.assertEqual(aicmd_obtenu, aicmd_cible)

        # test distance max < distance min
        self.assertRaises(AssertionError, ProtectGoal, self.game_state, 0,
                          True, 50, 40)