def move_agent(self, action_name, game_interface: HFOAttackingPlayer, features: BaseHighLevelState): """ Agent Moves/Dribbles in a specific direction """ # Get Movement type: action = DRIBBLE_TO if "UP" in action_name: action = (action, features.agent.x_pos, -0.9) elif "DOWN" in action_name: action = (action, features.agent.x_pos, 0.9) elif "LEFT" in action_name: action = (action, -0.8, features.agent.y_pos) elif "RIGHT" in action_name: action = (action, 0.8, features.agent.y_pos) else: raise ValueError("ACTION NAME is WRONG") attempts = 0 while game_interface.in_game() and attempts < self.action_num_episodes: status, observation = game_interface.step(action, features.has_ball()) features.update_features(observation) attempts += 1 return game_interface.get_game_status(), \ game_interface.get_observation_array()
def shoot_ball(self, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ Tries to shoot, if it fail, kicks to goal randomly """ attempts = 0 while game_interface.in_game() and features.has_ball(): if attempts > 3: break elif attempts == 3: # Failed to kick four times # print("Failed to SHOOT 3 times. WILL KICK") y = random.choice([0.17, 0, -0.17]) hfo_action = (KICK_TO, 0.9, y, 2) else: hfo_action = (SHOOT,) _, obs = game_interface.step(hfo_action, features.has_ball()) features.update_features(obs) attempts += 1 return game_interface.get_game_status(), \ game_interface.get_observation_array()
def pass_ball(self, game_interface: HFOAttackingPlayer, features: DiscreteFeatures1Teammate): """ Tries to use the PASS action, if it fails, Kicks in the direction of the teammate""" attempts = 0 while game_interface.in_game() and features.has_ball(): if attempts > 2: break elif attempts == 2: # Failed to pass 2 times # print("Failed to PASS two times. WILL KICK") y = random.choice([0.17, 0, -0.17]) hfo_action = (KICK_TO, 0.9, y, 2) else: hfo_action = (PASS, 11) _, obs = game_interface.step(hfo_action, features.has_ball()) features.update_features(obs) attempts += 1 return game_interface.get_game_status(), \ game_interface.get_observation_array()
def shoot_ball(self, game_interface: HFOAttackingPlayer, features: BaseHighLevelState): """ Tries to shoot, if it fail, kicks to goal randomly """ # Get best shoot angle: angles = [] goalie_coord = np.array([features.opponents[0].x_pos, features.opponents[0].y_pos]) player_coord = np.array(features.get_pos_tuple()) for goal_pos in self.shoot_possible_coord: angles.append(get_angle(goalie=goalie_coord, player=player_coord, point=goal_pos)) idx = int(np.argmax(np.array(angles))) best_shoot_coord = self.shoot_possible_coord[idx] # Action parameters: hfo_action = (KICK_TO, best_shoot_coord[0], best_shoot_coord[1], 2.5) # Step game: _, obs = game_interface.step(hfo_action, features.has_ball()) # Update features: features.update_features(obs) return game_interface.get_game_status(), \ game_interface.get_observation_array()
def move_agent(self, action_name, game_interface: HFOAttackingPlayer, features: DiscFeatures1Teammate): """ Agent Moves/Dribbles in a specific direction """ # print("move_agent!") if "SHORT" in action_name: num_repetitions = 10 elif "LONG" in action_name: num_repetitions = 20 else: raise ValueError("ACTION NAME is WRONG") # Get Movement type: if "MOVE" in action_name: action = MOVE_TO elif "DRIBBLE" in action_name: action = DRIBBLE_TO else: raise ValueError("ACTION NAME is WRONG") if "UP" in action_name: action = (action, features.agent_coord[0], -0.9) elif "DOWN" in action_name: action = (action, features.agent_coord[0], 0.9) elif "LEFT" in action_name: action = (action, -0.8, features.agent_coord[1]) elif "RIGHT" in action_name: action = (action, 0.8, features.agent_coord[1]) else: raise ValueError("ACTION NAME is WRONG") attempts = 0 while game_interface.in_game() and attempts < num_repetitions: status, observation = game_interface.step(action, features.has_ball()) features.update_features(observation) attempts += 1 return game_interface.get_game_status(), \ game_interface.get_observation_array()