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 exec(self): if self.check_success(): self.status_flag = Flags.SUCCESS else: self.status_flag = Flags.WIP next_action = MoveToPosition(self.game_state, self.player_id, self.target) return next_action.exec()
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}))
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}))
def rotate_around(self): if self._has_reached_pose(self.pose_list[self.index]): self.index += 1 if self.index == len(self.pose_list): # position finale atteinte self.status_flag = Flags.SUCCESS self.next_state = self.halt action = Idle(self.game_state, self.player) else: # position intermédiaire atteinte self.status_flag = Flags.WIP action = MoveToPosition(self.game_state, self.player, self.pose_list[self.index]) else: self.status_flag = Flags.WIP action = MoveToPosition(self.game_state, self.player, self.pose_list[self.index]) return action
def calculate_path(self): self.initial_position = self.player.pose.position self.initial_orientation = self.player.pose.orientation self.initial_distance = get_distance(self.origin, self.initial_position) self.initial_angle = get_angle(self.origin, self.initial_position) self.target_angle = get_angle(self.origin, self.target.position) self.delta_angle = (self.target_angle + m.pi - self.initial_angle) % (2 * m.pi) if self.delta_angle > m.pi: self.delta_angle -= 2 * m.pi if m.fabs(self.delta_angle) >= self.ANGLE_INCREMENT: self.num_points = int( m.fabs(self.delta_angle) // self.ANGLE_INCREMENT) # incrément d'environ pi/8 radians (22.5 degrés) self.angle_increment = self.delta_angle / self.num_points else: self.num_points = 1 self.angle_increment = self.delta_angle for i in range(self.num_points): pos = rotate_point_around_origin(self.initial_position, self.origin, (i + 1) * self.angle_increment) theta = self.initial_orientation + (i + 1) * self.angle_increment self.pose_list.append(Pose(pos, theta)) self.next_state = self.rotate_around return MoveToPosition(self.game_state, self.player, self.pose_list[self.index])
def exec(self): self.target_player = closest_player_to_point( self.game_state.get_ball_position(), our_team=False).player orientation_opponent = np.array([ math.cos(self.target_player.pose.orientation), math.sin(self.target_player.pose.orientation) ]) destination_position = self.target_player.pose.position + self.distance * orientation_opponent ball_to_player = self.game_state.get_ball_position( ) - self.player.pose.orientation destination_orientation = ball_to_player.angle() destination_pose = Pose(destination_position, destination_orientation) if self.check_success(): self.status_flag = Flags.SUCCESS else: self.status_flag = Flags.WIP return MoveToPosition(self.game_state, self.player, destination_pose, pathfinder_on=True, 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).exec()
def exec(self): if self.check_success(): self.status_flag = Flags.SUCCESS else: self.status_flag = Flags.WIP return MoveToPosition(self.game_state, self.player, self.target, cruise_speed=self.cruise_speed).exec()
def is_moving(self): if (self.player.pose.position - self.init_position).norm() > MOVING_THRESHOLD: self.start_moving_time = time.time() self.loop_delay = self.start_moving_time - self.send_command_time print('Delay of the AI loop is {:5.3f} second'.format( self.loop_delay)) self.next_state = self.halt else: self.next_state = self.is_moving return MoveToPosition(self.game_state, self.player, self.target)
def exec(self): if self.check_success(): self.status_flag = Flags.SUCCESS else: self.status_flag = Flags.WIP return MoveToPosition(self.game_state, self.player, self.target, pathfinder_on=True, cruise_speed=self.cruise_speed, collision_ball=self.collision_ball, charge_kick=self.charge_kick, end_speed=self.end_speed).exec()
def grab_ball(self): if self._get_distance_from_ball( ) < 120 and self._is_player_towards_ball_and_target(): self.next_state = self.halt self.status_flag = Flags.SUCCESS else: self.next_state = self.grab_ball ball_x = self.game_state.get_ball_position().x ball_y = self.game_state.get_ball_position().y angle_ball_2_target = np.arctan2(self.target.position.y - ball_y, self.target.position.x - ball_x) return MoveToPosition( self.game_state, self.player, Pose(Position(ball_x, ball_y), angle_ball_2_target))
def protect_goal(self): if not self.penalty_kick: if not self._is_ball_too_far and \ self.player == closest_player_to_point(self.game_state.get_ball_position()).player and\ self._get_distance_from_ball() < ROBOT_RADIUS *3: self.next_state = self.go_behind_ball else: self.next_state = self.protect_goal return ProtectGoal(self.game_state, self.player, self.is_yellow, minimum_distance=300, maximum_distance=self.game_state.game.field. constant["FIELD_GOAL_RADIUS"] / 2) else: our_goal = Position( self.game_state.const["FIELD_OUR_GOAL_X_EXTERNAL"], 0) opponent_kicker = player_with_ball(2 * ROBOT_RADIUS) ball_position = self.game_state.get_ball_position() if opponent_kicker is not None: ball_to_goal = our_goal.x - ball_position.x if self.game_state.field.our_side is FieldSide.POSITIVE: opponent_kicker_orientation = clamp( opponent_kicker.pose.orientation, -pi / 5, pi / 5) goalkeeper_orientation = wrap_to_pi( opponent_kicker_orientation - pi) else: opponent_kicker_orientation = clamp( wrap_to_pi(opponent_kicker.pose.orientation - pi), -pi / 5, pi / 5) goalkeeper_orientation = opponent_kicker_orientation y_position_on_line = ball_to_goal * tan( opponent_kicker_orientation) width = self.game_state.const["FIELD_GOAL_WIDTH"] y_position_on_line = clamp(y_position_on_line, -width, width) destination = Pose(our_goal.x, y_position_on_line, goalkeeper_orientation) else: destination = Pose(our_goal) return MoveToPosition(self.game_state, self.player, destination, pathfinder_on=True, cruise_speed=2)
def support_other_zone(self): enemy_positions = self.get_enemy_in_zone() if len(enemy_positions) == 0: self.next_state = self.support_other_zone else: self.next_state = self.cover_zone destination = stayInsideSquare(self.game_state.get_ball_position(), self.y_top, self.y_bottom, self.x_left, self.x_right) destination = self.game_state.game.field.stay_outside_goal_area( destination, our_goal=True) orientation = (self.game_state.get_ball_position() - destination).angle() return MoveToPosition(self.game_state, self.player, Pose(destination, orientation))
def move_to_catch_ball(self): ball_position = self.game_state.get_ball_position() if has_ball(self.game_state, self.player_id): self.next_state = self.halt self.status_flag = Flags.SUCCESS return Idle(self.game_state, self.player) else: # position the robot to be able to catch the ball current_position = self.player.pose.position next_ball_position = ball_position + self.game_state.get_ball_velocity() destination_position = get_closest_point_on_line(current_position, ball_position, next_ball_position) rotation_towards_ball = get_angle(destination_position, ball_position) pose_towards_ball = Pose(destination_position, rotation_towards_ball) self.next_state = self.move_to_catch_ball self.status_flag = Flags.WIP return MoveToPosition(self.game_state, self.player, pose_towards_ball)
def move_to_enemy(self): if self._is_player_between_ball_and_enemy(): self.next_state = self.move_to_enemy self.status_flag = Flags.SUCCESS else: self.next_state = self.go_between_ball_and_enemy self.status_flag = Flags.WIP player = self.player.pose.position.conv_2_np() enemy = self.player.pose.position.conv_2_np() ball = self.game_state.get_ball_position().conv_2_np() ball_to_enemy = enemy - ball player_to_ball = ball - player destination = enemy - 300 * ball_to_enemy / np.linalg.norm( ball_to_enemy) destination_orientation = np.arctan2(player_to_ball[1], player_to_ball[0]) return MoveToPosition( self.game_state, self.player, Pose(Position.from_np(destination), destination_orientation))
def exec(self): self.status_flag = Flags.WIP return MoveToPosition(self.game_state, self.player, self.player.pose, 1)
def move(self): self.status_flag = Flags.WIP self.send_command_time = time.time() self.next_state = self.is_moving self.init_position = self.player.pose.position return MoveToPosition(self.game_state, self.player, self.target)
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)
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()) self.game.ball = Ball() game_world = GameWorld(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") 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})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") 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) @unittest.skip("LAZY ME OH HELL") 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) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") 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) @unittest.skip("I don't know whuy the f**k it is broken here.") 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})) @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.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)
def exec(self): self.status_flag = Flags.WIP next_action = MoveToPosition(self.game_state, self.player_id, Pose(self.game_state.get_player_position(self.player_id), 3.14)) return next_action.exec()
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()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer @unittest.skip("I don't know whuy the f**k it is broken here.") 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})) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = AICommand(self.player_id, AICommandType.STOP) self.assertEqual(Idle.exec(self.idle), current_pose_string) @unittest.skip("I don't know what the f**k is happening here.") 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) @unittest.skip("LAZY ME OH HELL") 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) @unittest.skip("I don't know whuy the f**k it is broken here.") def test_GoBetween(self): # test avec une droite verticale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(100, -100), Position(200, 0)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand(self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(100, 0), 0)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test avec une droite horizontale self.go_between = GoBetween(self.game_state, self.player_id, Position(100, 100), Position(-100, 100), Position(0, 200)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(0, 500), Position(500, 0), Position(-300, -300)) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(1000, 75), Position(1500, -250), Position(0, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": Pose(Position(1150, -23), 3.1215)}) self.assertEqual(ai_cmd, ai_cmd_expected) # test destination calculée derrière position2 self.go_between = GoBetween(self.game_state, self.player_id, Position(-100, 50), Position(-50, 50), Position(-60.0 + sqrt(3), 51.0), 10) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(-500, 25), Position(1, 25), Position(-179, 0), 180) ai_cmd = self.go_between.exec() ai_cmd_expected = AICommand( self.player_id, AICommandType.MOVE, **{"pose_goal": 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.player_id, Position(1, 1), Position(-1, -1), 50) @unittest.skip("I don't know whuy the f**k it is broken here.") 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) @unittest.skip("I don't know whuy the f**k it is broken here.") 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})) @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.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)