def heading_closer_to_attacking_goal_or_ally(robot): try: angle_to_score = Util.get_turn_angle(robot.heading, robot.direction["attacking goal"]['angle']) angle_to_ally = Util.get_turn_angle(robot.heading, robot.direction["ally"]['angle']) except: return "goal" if min(abs(angle_to_score), abs(angle_to_ally)) == abs(angle_to_score): return "goal" else: return "ally"
def subtask_complete(self, subtask): if subtask == "turn to ally": angle_to_ally = Util.get_turn_angle(self.robot.heading, self.robot.direction["ally"]["angle"]) return self.robot.within_tolerance_angle(angle_to_ally) elif subtask == "move to receive pass": # if self.robot.desired_position == self.receive_point: angle_to_ally = Util.get_turn_angle(self.robot.heading, self.robot.direction["ally"]["angle"]) distance_to_centre = Util.get_direction(self.robot.position, self.robot.centre)["distance"] return self.robot.within_tolerance_angle(angle_to_ally) and distance_to_centre <= 200 # else: # return False elif subtask == "has ball": return self.has_ball elif subtask == "kicked": return self.kicked
def subtask_complete(self, subtask): """ Returns True if subtask is completed successfully """ if subtask == "turn to ally": angle_to_ally = Util.get_turn_angle(self.robot.heading, self.robot.direction["ally"]['angle']) return self.robot.tolerance_angle("ally") elif subtask == "move to receive pass": # if self.robot.desired_position == self.receive_point: angle_to_ally = Util.get_turn_angle(self.robot.heading, self.robot.direction["ally"]['angle']) distance_to_centre = Util.get_direction(self.robot.position, self.robot.centre)['distance'] return self.robot.tolerance_angle("ally") and distance_to_centre <= self.robot.TOLERANCE_DISTANCE elif subtask == "has ball": return self.has_ball elif subtask == "kicked": return self.kicked
def execution_step(self, subtask): self.subtask_started = self.subtask != subtask self.subtask = subtask try: self.has_ball = self.world_state.who_has_ball().role == "group09" except AttributeError: self.has_ball = False timestamp = time() heading = self.robot.heading position = self.robot.position defending_goal_midpoint = Util.get_midpoint(self.robot.defending_goal_line) self.go_to_desired_position = False if timestamp - self.timestamps["kicked"] >= 1: self.kicked = False if not self.grabbed and timestamp - self.timestamps["ungrabbed"] >= 5: self.grab_force() try: naughty = Util.point_in_defense_zone(self.robot.zones, self.robot.position, self.robot.attacking_goal) or ( Util.point_in_defense_zone(self.robot.zones, self.robot.position, self.robot.defending_goal) and Util.point_in_defense_zone(self.robot.zones, self.robot.ally.position, self.robot.defending_goal) ) except: naughty = False if naughty: self.robot.set_desired_position(self.robot.centre) self.go_to_desired_position = True # always grab the ball if possible, no matter what the subtask is elif self.robot.ball_in_range and not self.has_ball and not self.grabbed: self.grab() self.halt() # don't bash into walls elif self.collision is not None: if self.collision == "forward": self.moving = True self.com.move_backward(50) # TODO - calibrate elif self.collision == "left": self.moving = True self.com.turn_clockwise(50) # TODO - calibrate elif self.collision == "right": self.moving = True self.com.turn_anticlockwise(50) # TODO - calibrate self.collision = None # turn towards the ball, then ungrab and move towards it, grabbing and halting when it is within range elif self.subtask == "acquire ball": try: naughty = Util.point_in_defense_zone( self.robot.zones, self.world_state.ball_position, self.robot.attacking_goal ) or ( Util.point_in_defense_zone( self.robot.zones, self.world_state.ball_position, self.robot.defending_goal ) and Util.point_in_defense_zone( self.robot.zones, self.robot.ally.position, self.robot.defending_goal ) ) except: naughty = False if naughty: self.robot.set_desired_position(self.robot.centre) self.go_to_desired_position = True # print("[PLANNER GROUP 9] ball in a naughty place, retreating to centre") else: angle_to_ball = Util.get_turn_angle(heading, self.robot.direction["ball"]["angle"]) if self.robot.ball_in_left or self.robot.ball_in_right: self.grab() self.move(-50, position) # TODO - calibrate elif self.robot.ball_in_bottom: self.grab() self.move(50, position) # TODO - calibrate elif not self.robot.within_tolerance_grab_angle(angle_to_ball): self.grab() self.turn(angle_to_ball, heading) else: distance_to_ball = self.robot.direction["ball"]["distance"] if self.grabbed and self.robot.ball_in_range: self.move(-50, position) # TODO - calibrate elif not self.robot.ball_in_range: self.ungrab() self.move(distance_to_ball, position) elif self.subtask == "penalty shoot": if self.has_ball: if self.subtask_started or self.bearing is None: if self.robot.defending_goal == "left": self.bearing = round(uniform(5, 15)) if choice(["left", "right"]) == "left": self.bearing = -self.bearing else: self.bearing = round(uniform(175, 165)) if choice(["left", "right"]) == "left": self.bearing = -self.bearing angle_to_bearing = Util.get_turn_angle(heading, self.bearing) if not self.robot.within_tolerance_angle(angle_to_bearing): self.turn(angle_to_bearing, heading) else: self.kick_power(100) elif self.subtask == "defend penalty": # our robot cannot implement this subtask pass elif self.subtask == "kick off": if self.has_ball: if self.subtask_started or self.bearing is None: if self.robot.defending_goal == "left": self.bearing = round(uniform(23, 67)) if choice(["left", "right"]) == "left": self.bearing = -self.bearing else: self.bearing = round(uniform(113, 157)) if choice(["left", "right"]) == "left": self.bearing = -self.bearing angle_to_bearing = Util.get_turn_angle(heading, self.bearing) if not self.robot.within_tolerance_angle(angle_to_bearing): self.turn(angle_to_bearing, heading) else: self.kick_power(100) # turn our robot to face our ally elif self.subtask == "turn to ally": angle_to_ally = Util.get_turn_angle(heading, self.robot.direction["ally"]["angle"]) if not self.robot.within_tolerance_angle(angle_to_ally): self.turn(angle_to_ally, heading) elif self.subtask == "pass" and self.has_ball: self.kick_distance(self.robot.direction["ally"]["distance"]) elif self.subtask == "panic shoot" or self.subtask == "shoot": if self.has_ball: if self.robot.facing_attacking_goal: self.kick_power(100) else: angle_to_goal = Util.get_turn_angle(heading, self.robot.direction["attacking goal"]["angle"]) self.turn(angle_to_goal, heading) elif ( self.subtask == "move to receive pass" or self.subtask == "intercept shot" or self.subtask == "intercept pass" or self.subtask == "align with ball" or self.subtask == "defend goal from ball" or self.subtask == "defend enemy shot" ): if self.subtask == "move to receive pass": # if self.subtask_started: # self.receive_point = Util.set_receive_point(self.robot.zones, self.robot.attacking_goal) # desired_position = self.receive_point desired_position = self.robot.centre elif self.subtask == "intercept shot": if self.world_state.who_has_ball() is None: enemy = self.robot.enemy1 else: enemy = self.world_state.who_has_ball() if enemy.facing_attacking_goal: desired_position = Util.get_intercept_point_or_midpoint( position, [enemy.position, Util.intercept_goal(enemy)] ) # print("ENEMY CAN SCORE {}".format(desired_position)) else: desired_position = Util.get_intercept_point_or_midpoint( position, [enemy.position, defending_goal_midpoint] ) # print("ENEMY MIDPOINT {}".format(desired_position)) elif self.subtask == "intercept pass": desired_position = Util.get_intercept_point_or_midpoint(position, self.robot.intercept_line) else: desired_position = None # # elif self.subtask == "align with ball" or self.subtask == "defend goal from ball": # if self.world_state.ball_moving_to_defending_goal(self.robot.defending_goal): # desired_position = self.world_state.ball_intercept_point(self.robot.defending_goal) # if self.robot.defending_goal == "left": # desired_position[0] += self.robot.GOAL_X_OFFSET # else: # desired_position[0] -= self.robot.GOAL_X_OFFSET # else: # ball_y = self.world_state.ball_position[1] # if self.robot.defending_goal == "left": # top = self.robot.defending_goal_line[0][1] # bottom = self.robot.defending_goal_line[1][1] # x = defending_goal_midpoint[0] + self.robot.GOAL_X_OFFSET # else: # top = self.robot.defending_goal_line[2][1] # bottom = self.robot.defending_goal_line[3][1] # x = defending_goal_midpoint[0] - self.robot.GOAL_X_OFFSET # if ball_y <= top: # y = top # elif ball_y >= bottom: # y = bottom # else: # y = ball_y # desired_position = (x, y) # # else: # elif self.subtask == "defend enemy shot": # enemy = self.world_state.who_has_ball() # intercept = Util.intercept_goal(enemy) # if intercept is not None: # if self.robot.defending_goal == "left": # desired_position = (intercept[0] + self.robot.GOAL_X_OFFSET, intercept[1]) # else: # desired_position = (intercept[0] - self.robot.GOAL_X_OFFSET, intercept[1]) # else: # desired_position = defending_goal_midpoint # if desired_position is not None: self.robot.set_desired_position(desired_position) self.go_to_desired_position = True # ungrab and wait to receive the ball elif self.subtask == "receive pass": self.ungrab() if self.go_to_desired_position: distance_to_desired_position = self.robot.direction["desired position"]["distance"] if not self.robot.within_tolerance_distance(distance_to_desired_position): # get to the desired position forward_angle_to_desired_position = Util.get_turn_angle( heading, self.robot.direction["desired position"]["angle"] ) if abs(forward_angle_to_desired_position) <= 90: if not self.robot.within_tolerance_angle(forward_angle_to_desired_position): self.turn(forward_angle_to_desired_position, heading) else: self.move(distance_to_desired_position, position) else: backward_angle_to_desired_position = Util.get_turn_angle( Util.get_opposite_heading(heading), self.robot.direction["desired position"]["angle"] ) if not self.robot.within_tolerance_angle(backward_angle_to_desired_position): self.turn(backward_angle_to_desired_position, heading) else: self.move(-distance_to_desired_position, position) else: if self.subtask == "move to receive pass": # turn to ally angle_to_ally = Util.get_turn_angle(heading, self.robot.direction["ally"]["angle"]) if not self.robot.within_tolerance_angle(angle_to_ally): self.turn(angle_to_ally, heading) else: self.ungrab() elif ( self.subtask == "intercept shot" or self.subtask == "intercept pass" or self.subtask == "defend enemy shot" ): # turn to the enemy with the ball and ungrab if self.world_state.who_has_ball() is None: enemy_with_ball = "enemy1" else: enemy_with_ball = self.world_state.who_has_ball().role angle_to_enemy = Util.get_turn_angle(heading, self.robot.direction[enemy_with_ball]["angle"]) if not self.robot.within_tolerance_angle(angle_to_enemy): self.turn(angle_to_enemy, heading) else: self.ungrab() else: # self.subtask == "align with ball" or self.subtask == "defend goal from ball": # turn to ball and ungrab angle_to_ball = Util.get_turn_angle(heading, self.robot.direction["ball"]["angle"]) if not self.robot.within_tolerance_grab_angle(angle_to_ball): self.turn(angle_to_ball, heading) else: self.ungrab()