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 kick_ball_towards_target(self): # check alignment before kicking if has_ball_facing_target(self.game_state, self.player.id, self.target_position): self.next_state = self.halt self.status_flag = Flags.SUCCESS return Kick(self.game_state, self.player, 1) elif has_ball(self.game_state, self.player.id): self.next_state = self.halt self.status_flag = Flags.FAILURE return Idle(self.game_state, self.player) else: # returns error, strategy goes back to GoGetBall self.next_state = self.halt self.status_flag = Flags.FAILURE return Idle(self.game_state, self.player)
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
def halt(self): """ S'exécute lorsque l'état courant est *Halt* :return: l'action Stop crée """ stop = Idle(self.game_state, self.player_id) self.next_state = self.halt return stop
def halt(self) -> Idle: """ S'exécute lorsque l'état courant est *Halt*. Générique pour arrêter n 'importe quelles tactiques enfants :return: un nouvelle instance de l'action Idle pour le robot """ self.next_state = self.halt return Idle(self.game_state, self.player)
def halt(self): self.status_flag = Flags.SUCCESS if get_distance(self.player.pose.position, self.game_state.get_ball_position()) < \ POSITION_DEADZONE + ROBOT_RADIUS: self.next_state = self.halt else: self.next_state = self.move_to_ball return Idle(self.game_state, self.player)
def halt(self): self.status_flag = Flags.SUCCESS dist = self._get_distance_from_ball() if dist > POSITION_DEADZONE: self.next_state = self.get_behind_ball else: self.next_state = self.halt return Idle(self.game_state, self.player_id)
def halt(self, reset=False): stop = Idle(self.game_state, self.player_id) if get_distance( self.game_state.get_player_pose(self.player_id).position, self.game_state.get_ball_position() ) < POSITION_DEADZONE + BALL_RADIUS: self.next_state = self.halt else: self.next_state = self.move_to_ball return stop
def keep(self): if self._get_distance_from_ball() < 120: self.next_state = self.keep self.status_flag = Flags.SUCCESS elif self._is_player_towards_ball_and_target(-0.4): self.next_state = self.grab_ball self.status_flag = Flags.WIP else: self.next_state = self.get_behind_ball self.status_flag = Flags.WIP return Idle(self.game_state, self.player)
def handle_joystick(self): if self.status_flag is not Flags.FAILURE: self.status_flag = Flags.WIP pygame.event.pump() x, y = self.joy.get_left_axis_vector() _, t = self.joy.get_right_axis_vector() if self.joy.get_btn_value("X"): charge_kick = True else: charge_kick = False if self.joy.get_btn_value("A"): kick = 4 else: kick = 0 if self.joy.get_btn_value("B"): dribbler = 2 else: dribbler = 0 if self.joy.get_btn_value("Y"): self.next_state = self.halt x_speed = -y * self.inv_y y_speed = x * self.inv_x speed_pose = Pose(Position(x_speed, y_speed), t * 5) if kick == 0: next_action = AllStar( self.game_state, self.player, **{ "ai_command_type": AICommandType.MOVE, "pose_goal": speed_pose, "control_loop_type": AIControlLoopType.SPEED, "charge_kick": charge_kick, "kick_strength": kick, "dribbler_on": dribbler }) else: next_action = AllStar( self.game_state, self.player, **{ "ai_command_type": AICommandType.KICK, "kick_strength": kick }) else: next_action = Idle(self.game_state, self.player) return next_action
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 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 rotate_towards_ball(self): if player_grabbed_ball(self.game_state, self.player_id): self.next_state = self.halt self.status_flag = Flags.SUCCESS return Idle(self.game_state, self.player_id) else: # keep rotating current_position = self.game_state.get_player_position() ball_position = self.game_state.get_ball_position() rotation_towards_ball = get_angle(current_position, ball_position) pose_towards_ball = Pose(current_position, rotation_towards_ball) move_to = MoveTo(self.game_state, self.player_id, pose_towards_ball) self.next_state = self.rotate_towards_ball self.status_flag = Flags.WIP return move_to
def cover_zone(self): enemy_positions = self.get_enemy_in_zone() ball_pos = self.game_state.get_ball_position() if len(enemy_positions) == 0: self.next_state = self.support_other_zone return Idle(self.game_state, self.player) else: self.next_state = self.cover_zone mean_position = Position() for pos in enemy_positions: mean_position = mean_position + pos mean_position /= len(enemy_positions) destination = stayInsideSquare(mean_position, self.y_top, self.y_bottom, self.x_left, self.x_right) return GoBetween(self.game_state, self.player, ball_pos, destination, ball_pos, 2 * ROBOT_RADIUS)
def test_exec(self): next_ai_command = self.graph1.exec() expected_ai_command = Idle(self.game_state, self.a_player).exec() 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 = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() 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 = GoToPositionNoPathfinder( self.game_state, self.a_player, Pose(Position(500, 0), 0)).exec() self.assertEqual(self.empty_graph.current_node, 0) self.assertEqual(next_ai_command, expected_ai_command)
def halt(self): if self.status_flag == Flags.INIT: self.next_state = self.kick_charge else: self.status_flag = Flags.SUCCESS return Idle(self.game_state, self.player)
def halt(self): self.status_flag = Flags.SUCCESS return Idle(self.game_state, self.player)
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)
def test_idle(self): self.idle = Idle(self.game_state, self.player_id) current_pose = None current_pose_string = "AICommand(move_destination=" + str( current_pose) + ", kick_strength=0)" self.assertEqual(str(Idle.exec(self.idle)), current_pose_string)