Example #1
0
    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
                }))
Example #2
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)
Example #3
0
    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)
Example #4
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)
Example #5
0
    def test_move_to(self):
        self.pose = Pose(Position(0, 0, 0), orientation=0.0)
        self.move = MoveToPosition(self.game_state, self.player_id, self.pose)
        self.assertEqual(
            self.move.exec(),
            AICommand(1, AICommandType.MOVE, **{"pose_goal": self.pose}))

        self.pose = Pose(Position(0.5, 0.3, 0.2), orientation=3.2)
        self.move = MoveToPosition(self.game_state, self.player_id, self.pose)
        self.assertEqual(
            MoveToPosition.exec(self.move),
            AICommand(self.player_id, AICommandType.MOVE,
                      **{"pose_goal": self.pose}))
Example #6
0
 def exec(self):
     if self.approach:
         return AICommand(self.player,
                          AICommandType.MOVE,
                          pose_goal=self.generate_destination(),
                          pathfinder_on=self.pathfinder_on,
                          cruise_speed=self.approach_speed,
                          end_speed=self.tangential_speed)
     else:
         return AICommand(self.player,
                          AICommandType.MOVE,
                          pose_goal=self.generate_destination(),
                          pathfinder_on=self.pathfinder_on,
                          end_speed=self.tangential_speed)
Example #7
0
 def exec(self):
     """
     Exécute l'arrêt
     :return: Un tuple (None, kick) où None pour activer une commande de stop et kick est nul (on ne botte pas)
     """
     # un None pour que le coachcommandsender envoi une command vide.
     return AICommand(self.player, self.ai_command_type, **self.other_args)
Example #8
0
 def exec(self):
     ball = self.game_state.get_ball_position().conv_2_np()
     player = self.player.pose.position.conv_2_np()
     player_to_ball = ball - player
     player_to_ball = 0.3 * player_to_ball / np.linalg.norm(player_to_ball)
     speed_pose = Pose(Position.from_np(player_to_ball))
     return AICommand(self.player, AICommandType.MOVE, **{"pose_goal": speed_pose, "speed_flag": True})
Example #9
0
    def exec(self):
        if self.status_flag is not Flags.FAILURE and self.cmd_id < len(
                self.commands) - 1:
            self.status_flag = Flags.WIP

            # Creating speed pose
            cmd_vx, cmd_vy, cmd_vt = self.commands[self.cmd_id]
            speed_pose = Pose(Position(cmd_vx, cmd_vy), cmd_vt)
            self.cmd_id += 1

            # Saving data
            px = self.player.pose.position.x / 1000.0
            vx = self.player.velocity[0]
            py = self.player.pose.position.y / 1000.0
            vy = self.player.velocity[1]
            pt = self.player.pose.orientation
            vt = self.player.velocity[2]

            if self.cmd_id == 0:
                self.start_time = time.time()
            t = time.time() - self.start_time

            with open(self.output_filename, 'a') as f:
                f.write('{},{},{},{},{},{},{},{},{},{}\n'.format(
                    t, cmd_vx, px, vx, cmd_vy, py, vy, cmd_vt, pt, vt))

            next_action = AICommand(
                self.player, AICommandType.MOVE, **{
                    "pose_goal": speed_pose,
                    "control_loop_type": AIControlLoopType.OPEN
                })
        else:
            next_action = Idle(self.game_state, self.player)

        return next_action
Example #10
0
    def test_MoveWithBall(self):
        self.move_with_ball = MoveToDribblingBall(self.game_state,
                                                  self.player_id,
                                                  Position(100, 0))
        self.game_state.game.ball.set_position(Position(5, 0), 0)
        ai_cmd = self.move_with_ball.exec()
        ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE,
                                    **{"pose_goal": Pose(Position(100, 0), 0)})
        self.assertEqual(ai_cmd, ai_cmd_expected)

        self.game_state.game.ball.set_position(Position(5, 2), 0)
        ai_cmd = self.move_with_ball.exec()
        ai_cmd_expected = AICommand(
            self.player_id, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(100, 0), atan(2 / 5))})
        self.assertEqual(ai_cmd, ai_cmd_expected)
Example #11
0
 def exec(self):
     return AICommand(
         self.player, AICommandType.MOVE, **{
             "pose_goal": self.get_destination(),
             "cruise_speed": self.cruise_speed,
             "pathfinder_on": self.pathfinder_on
         })
Example #12
0
    def test_exec(self):
        self.node1.add_vertex(self.vertex1)
        self.node1.add_vertex(self.vertex2)
        next_ai_command, next_node = self.node1.exec()
        self.assertEqual(next_node, 0)
        expected_aicmd = AICommand(
            self.a_player, AICommandType.MOVE,
            **{"pose_goal": Pose(Position(-4000, 0), 0)})
        self.assertEqual(next_ai_command, expected_aicmd)

        self.node2.add_vertex(self.vertex2)
        expected_aicmd = AICommand(
            self.a_player, AICommandType.STOP
        )  #, **{"pose_goal": Pose(Position(-4000, 0), 0)})
        next_ai_command, next_node = self.node2.exec()
        self.assertEqual(next_ai_command, expected_aicmd)
        self.assertEqual(next_node, -1)
    def test_good_kwargs(self):
        ai_cmd = AICommand(self.player, **self.new_keys)
        self.assertEqual(self.player, ai_cmd.player)
        self.assertEqual(self.robot_id, ai_cmd.robot_id)
        self.assertEqual(self.STOP, ai_cmd.command)

        for key, val in self.new_keys.items():
            self.assertEqual(ai_cmd[key], val)
Example #14
0
 def exec(self):
     self.status_flag = Flags.WIP
     target_orientation = (self.target.position -
                           self.player.pose.position).angle()
     return AICommand(self.player,
                      AICommandType.MOVE,
                      pose_goal=Pose(self.player_position,
                                     target_orientation))
Example #15
0
 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)
Example #16
0
    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)
 def exec(self):
     return AICommand(self.player,
                      AICommandType.MOVE,
                      pose_goal=self.destination,
                      pathfinder_on=self.pathfinder_on,
                      cruise_speed=self.cruise_speed,
                      collision_ball=self.collision_ball,
                      charge_kick=self.charge_kick,
                      end_speed=self.end_speed,
                      dribbler_on=self.dribbler_on)
Example #18
0
 def exec(self):
     """
     Exécute le déplacement
     :return: Un tuple (Pose, kick)
                  où Pose est la destination du joueur
                     kick est faux (on ne botte pas)
     """
     return AICommand(self.player, AICommandType.MOVE, **{"pose_goal": self.destination,
                                                          "pathfinder_on": True,
                                                          "cruise_speed": self.cruise_speed})
Example #19
0
    def test_GrabBall(self):
        self.grab_ball = GetBall(self.game_state, self.player_id)
        self.game_state.game.ball.set_position(Position(5, 0), 0)
        ai_cmd = self.grab_ball.exec()
        ball_position = self.game_state.get_ball_position()
        destination_orientation = get_angle(
            self.game_state.get_player_pose(self.player_id).position,
            ball_position)
        destination_pose = {
            "pose_goal": Pose(ball_position, destination_orientation)
        }
        ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE,
                                    **{"pose_goal": destination_pose})
        self.assertEqual(ai_cmd, ai_cmd_expected)

        self.game_state.game.ball.set_position(Position(-5, 5), 0)
        ai_cmd = self.grab_ball.exec()
        ai_cmd_expected = AICommand(Pose(Position(-5, 5), 3 * pi / 4), 0)
        self.assertEqual(ai_cmd, ai_cmd_expected)
Example #20
0
 def exec(self):
     """
     Exécute le déplacement
     :return:
     """
     return AICommand(
         self.player, AICommandType.MOVE, **{
             "pose_goal": self.destination,
             "pathfinder_on": False,
             "cruise_speed": self.cruise_speed
         })
Example #21
0
 def exec(self):
     """
     Place le robot afin qu'il prenne le contrôle de la balle
     :return: Un tuple (Pose, kick) où Pose est la destination du joueur et kick est nul (on ne botte pas)
     """
     ball_position = self.game_state.get_ball_position()
     destination_orientation = get_angle(self.player.pose.position,
                                         ball_position)
     destination_pose = {
         "pose_goal": Pose(ball_position, destination_orientation)
     }
     return AICommand(self.player, AICommandType.MOVE, **destination_pose)
Example #22
0
 def exec(self):
     """
     Exécute le déplacement en tenant compte de la possession de la balle. Le robot se déplace vers la destination,
     mais s'oriente de façon à garder la balle sur le dribleur. C'est la responsabilité de la Tactique de faire les
     corrections de trajectoire nécessaire.
     :return:
     """
     # TODO: Améliorer le comportement en ajoutant l'intervalle d'anle correspondant à la largeur du dribbleur
     destination_orientation = get_angle(
         self.player.pose.position, self.game_state.get_ball_position())
     destination_pose = Pose(self.destination, destination_orientation)
     return AICommand(self.player, AICommandType.MOVE,
                      **{"pose_goal": destination_pose})
Example #23
0
    def test_exec(self):
        next_ai_command = self.graph1.exec()
        expected_ai_command = AICommand(1, AICommandType.STOP)
        self.assertEqual(self.graph1.current_node, 1)
        self.assertEqual(next_ai_command, expected_ai_command)

        self.assertRaises(EmptyGraphException, self.empty_graph.exec)

        self.empty_graph.add_node(self.node2)
        self.empty_graph.add_node(self.node1)
        self.empty_graph.add_vertex(0, 1, foo2)

        next_ai_command = self.empty_graph.exec()
        expected_ai_command = AICommand(
            0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))})
        self.assertEqual(self.empty_graph.current_node, 0)
        self.assertEqual(next_ai_command, expected_ai_command)

        next_ai_command = self.empty_graph.exec()
        expected_ai_command = AICommand(
            0, AICommandType.MOVE, **{"pose_goal": Pose(Position(500, 0))})
        self.assertEqual(self.empty_graph.current_node, 0)
        self.assertEqual(next_ai_command, expected_ai_command)
Example #24
0
    def test_kick(self):

        # test avec la valeur 0 (nulle)
        self.kick = Kick(self.game_state, self.player_id, 0)
        current_pose = self.game_state.get_player_pose(self.player_id)
        current_pose_string = AICommand(self.player_id, AICommandType.KICK,
                                        **{"pose_goal": current_pose})
        self.assertEqual(Kick.exec(self.kick), current_pose_string)

        # test avec la valeur 1 (force maximale)
        self.kick = Kick(self.game_state, self.player_id, 1)
        current_pose = self.game_state.get_player_pose(self.player_id)
        self.assertEqual(
            Kick.exec(self.kick),
            AICommand(self.player_id, AICommandType.KICK,
                      **{"pose_goal": current_pose}))

        # test avec la valeur 0.3 (force intermediaire)
        self.kick = Kick(self.game_state, self.player_id, 0.3)
        current_pose = self.game_state.get_player_pose(self.player_id)
        self.assertEqual(
            Kick.exec(self.kick),
            AICommand(self.player_id, AICommandType.KICK,
                      **{"pose_goal": current_pose}))
Example #25
0
    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.player_id, 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)
Example #26
0
 def exec(self):
     """
     Execute le kick
     """
     target = self.target.position.conv_2_np()
     player = self.player.pose.position.conv_2_np()
     player_to_target = target - player
     player_to_target = 0.3 * player_to_target / np.linalg.norm(
         player_to_target)
     self.speed_pose = Pose(Position.from_np(player_to_target))
     return AICommand(
         self.player, AICommandType.MOVE, **{
             "pose_goal": self.speed_pose,
             "speed_flag": True,
             "kick": True,
             "kick_strength": self.force
         })
Example #27
0
    def exec(self):
        """
        Execute the kick command
        :return: Un AIcommand
        """
        if self.target is not None:
            ball_position = self.game_state.get_ball_position()
            orientation = (self.target.position - self.player.pose.position).angle()
        else:
            ball_position = self.player.pose.position
            orientation = self.player.pose.orientation

        cmd_params = {"pose_goal": Pose(ball_position, orientation),
                      "kick": True,
                      "pathfinder_on": True,
                      "kick_strength": self.force,
                      "cruise_speed": 0.1,
                      "charge_kick": True,
                      "end_speed":self.end_speed}

        return AICommand(self.player, AICommandType.MOVE, **cmd_params)
    def _parse_ai_command(player: OurPlayer) -> List[Command]:
        """
        Transforme une ai_command en command d'envoi du RULEngine d'après certaines de ses caractéristiques
        :param player: (OurPlayer) instance du joueur
        :return: (Command) une command d'envoi du RULEngine correspondante
        """

        # TODO add a way to stop the dribbler! MGL 2017/03/14
        # TODO restraindre une seul commande de mouvement par robot
        if player.ai_command is not None:
            temp = []
            if player.ai_command.charge_kick:
                temp.append(StartChargingKick(player))

            if player.ai_command.dribbler_on:
                temp.append(Dribbler(player))

            if player.ai_command.kick:
                temp.append(Kick(player))


            if player.ai_command.command == AICommandType.MOVE:
                assert (isinstance(player.ai_command.speed, SpeedPose))
                temp.append(Move(player))
            elif player.ai_command.command == AICommandType.STOP:
                temp.append(Stop(player))
                player.ai_command.speed = SpeedPose()
            return temp
        # TODO FIXME T_T ugly hack kill me with fire
        # this is done so that when a player has no ai_command we can send the command to stop him
        # without having the parts that send the commands freak out because there is no ai_command to fetch some
        # informations they need. At this time it's for the grsim_command_sender when he get
        # command.player.ai_commands.speed, he finds aicommand as none breaking it. MGL 2017/06/26

        player.ai_command = AICommand(player, AICommandType.STOP)
        player.ai_command.speed = SpeedPose()
        return [Stop(player)]
Example #29
0
    def exec(self):
        """
        Calcul la pose que doit prendre le gardien en fonction de la position de la balle.
        :return: Un tuple (Pose, kick) où Pose est la destination du gardien et kick est nul (on ne botte pas)
        """
        goalkeeper_position = self.player.pose.position
        ball_position = self.game_state.get_ball_position()
        goal_x = self.game_state.const[
            "FIELD_X_RIGHT"] if self.is_right_goal else self.game_state.const[
                "FIELD_X_LEFT"]
        goal_position = Position(goal_x, 0)

        # Calcul de la position d'interception entre la balle et le centre du but
        destination_position = get_closest_point_on_line(
            goalkeeper_position, goal_position, ball_position)

        # Vérification que destination_position respecte la distance minimale
        destination_position = stayOutsideCircle(destination_position,
                                                 goal_position,
                                                 self.minimum_distance)

        # Vérification que destination_position respecte la distance maximale
        if self.maximum_distance is None:
            destination_position = self.game_state.game.field.stay_inside_goal_area(
                destination_position, self.is_right_goal)
        else:
            destination_position = stayInsideCircle(destination_position,
                                                    goal_position,
                                                    self.maximum_distance)

        # Calcul de l'orientation de la pose de destination
        destination_orientation = get_angle(destination_position,
                                            ball_position)

        destination_pose = Pose(destination_position, destination_orientation)
        return AICommand(self.player, AICommandType.MOVE,
                         **{"pose_goal": destination_pose})
Example #30
0
 def exec(self):
     """
     Execute le deplacement
     """
     return AICommand(self.player, AICommandType.MOVE, **{"pose_goal": self.speed_pose, "speed_flag": True})