def execute(s, drone): if s.strategy == Strategy.KICKOFF: if drone.controller is None: drone.controller = GK_Control(drone, goal_pos*team_sign(s.team)) else: if drone.controller is None: drone.controller = GK_Control(drone, goal_pos*team_sign(s.team)) if drone.controller is not None: drone.controller.run()
def execute(s, drone): drone.role.timer += s.dt if s.strategy == Strategy.KICKOFF: distance = np.linalg.norm(drone.pos - s.ball.pos) time_to_hit = distance / np.linalg.norm(drone.vel) if time_to_hit <= KO_DODGE_TIME: drone.controller = Dodge(drone, local(drone.orient_m, drone.pos, s.ball.pos)) drone.role.timer = 0 if drone.controller is None: if drone.kickoff == 'r_back' or drone.kickoff == 'l_back': drone.controller = AB_Control(drone, a3l([0.0, -2816.0, 70.0])*team_sign(s.team)) drone.role.timer = 0 #drone.controller = LINE_PD_Control(drone, a3l([0,-6000,0])*team_sign(s.team), s.ball.pos) else: drone.controller = AB_Control(drone, s.ball.pos) elif isinstance(drone.controller,AB_Control): if drone.role.timer >= KO_PAD_TIME: AB_Control(drone, s.ball.pos) else: drone.controller = AB_Control(drone, s.ball.pos) if drone.controller is not None: if isinstance(drone.controller,Dodge): drone.controller.run(drone.role.timer) else: drone.controller.run()
def execute(s, drone): if s.strategy == Strategy.KICKOFF: if drone.controller is None: drone.controller = AB_Control(drone, best_boost[drone.kickoff]*team_sign(s.team)) if drone.controller is not None: drone.controller.run()
def execute(self, hive, drone): if hive.strategy == Strategy.KICKOFF: # Find estimated time to hit the ball. distance = np.linalg.norm(drone.pos - hive.ball.pos) time_to_hit = distance / np.linalg.norm( drone.vel) if np.linalg.norm(drone.vel) > 0 else 10 # If the estimated time to hit is small, dodge. if time_to_hit <= self.KO_DODGE_TIME: drone.controller = Dodge() # Use AngleBased controller if none is set. if drone.controller is None: drone.controller = AngleBased() # If using AngleBased controller. elif isinstance(drone.controller, AngleBased): # Drive towards the pad on r_left or l_left kickoffs. if drone.kickoff in ( 'r_back', 'l_back') and drone.role.timer <= self.KO_PAD_TIME: drone.controller.run( hive, drone, a3l([0.0, -2816.0, 70.0]) * team_sign(hive.team)) # Drive towards the ball. else: drone.controller.run(hive, drone, hive.ball.pos) # If using Dodge controller. elif isinstance(drone.controller, Dodge): drone.controller.run(hive, drone, hive.ball.pos) else: # Else if drone.controller is None: drone.controller = TargetShot() elif isinstance(drone.controller, TargetShot): drone.controller.run(hive, drone, goal_pos * team_sign((hive.team + 1) % 2)) super().execute(hive)
def think(s): if s.strategy == Strategy.KICKOFF: # exit condition. if not s.ko_pause: print("KICKOFF END") s.strategy = None elif s.strategy == Strategy.DEFENCE: # exit condition. if s.ball.pos[1]*team_sign(s.team) > 0: print("DEFENCE END") s.strategy = None elif s.strategy == Strategy.OFFENCE: # exit condition. if s.ball.pos[1]*team_sign(s.team) < 0: print("OFFENCE END") s.strategy = None if s.strategy is None: # Kickoff. if s.r_active and s.ko_pause: print("KICKOFF START") s.strategy = Strategy.KICKOFF for drone in s.drones: drone.role = None drone.controller = None closest = 'r_corner' for pos in kickoff_positions: current = np.linalg.norm(kickoff_positions[closest]*team_sign(s.team) - drone.pos) new = np.linalg.norm(kickoff_positions[pos]*team_sign(s.team) - drone.pos) if new < current: closest = pos drone.kickoff = closest for pos in kickoff_positions: if any([isinstance(drone.role,Attacker) for drone in s.drones]): break for drone in s.drones: if drone.kickoff == pos and drone.role is None: drone.role = Attacker() break for pos in goalie_positions: if any([isinstance(drone.role,Goalie) for drone in s.drones]): break for drone in s.drones: if drone.kickoff == pos and drone.role is None: drone.role = Goalie() break for drone in s.drones: if drone.role is None: drone.role = Support() # Defence. elif s.ball.pos[1]*team_sign(s.team) < 0: s.strategy = Strategy.DEFENCE print("DEFENCE START") potential_goalie = s.drones[0] for drone in s.drones: drone.role = None drone.controller = None if np.linalg.norm(potential_goalie.pos - goal_pos*team_sign(s.team)) > np.linalg.norm(drone.pos - goal_pos*team_sign(s.team)): potential_goalie = drone potential_goalie.role = Goalie() for drone in s.drones: if drone.role is None: drone.role = Attacker() # Offence. elif s.ball.pos[1]*team_sign(s.team) > 0: s.strategy = Strategy.OFFENCE print("OFFENCE START") for drone in s.drones: drone.role = None drone.controller = None drone.role = Attacker()
def plan(hive): """Decides on strategy for the hivemind and assigns drones to roles. Arguments: hive {BotHelperProcess (self)} -- The hivemind. """ if hive.strategy == Strategy.KICKOFF: # End this strategy if ball has moved and the kickoff pause has ended. if not hive.ko_pause and np.any(hive.ball.pos[:2] != np.array([0, 0])): hive.logger.info("KICKOFF END") hive.strategy = None elif hive.strategy == Strategy.DEFENCE: # End this strategy if ball goes on their side. if hive.ball.pos[1] * team_sign(hive.team) > 0 or hive.ko_pause: hive.logger.info("DEFENCE END") hive.strategy = None elif hive.strategy == Strategy.OFFENCE: # End this strategy if ball goes on our side. if hive.ball.pos[1] * team_sign(hive.team) < 0 or hive.ko_pause: hive.logger.info("OFFENCE END") hive.strategy = None # Pick a strategy else: # KICKOFF: At start of kickoff. if hive.r_active and hive.ko_pause: hive.logger.info("KICKOFF START") hive.strategy = Strategy.KICKOFF # Finds drones' kickoff positions. for drone in hive.drones: # Resets roles and controllers. drone.role = None drone.controller = None # Looks for closest match in kickoff positions. drone.kickoff = 'r_corner' for pos in kickoff_positions: if np.linalg.norm(drone.pos - kickoff_positions[pos] * team_sign(hive.team)) < 100: drone.kickoff = pos break print("Drone index {} on kickoff {} position.".format( drone.index, drone.kickoff)) # Assigning Attacker role, i.e. who takes the kickoff. for pos in kickoff_positions: if any([ isinstance(drone.role, Attacker) for drone in hive.drones ]): break for drone in hive.drones: if drone.kickoff == pos and drone.role is None: drone.role = Attacker() break # Assigning Goalie role. for pos in goalie_positions: if any( [isinstance(drone.role, Goalie) for drone in hive.drones]): break for drone in hive.drones: if drone.kickoff == pos and drone.role is None: drone.role = Goalie() break # The rest are assigned as Support. for drone in hive.drones: if drone.role is None: drone.role = Support() # TODO Find better definitions for OFFENCE and DEFENCE # TODO Pick roles more intelligently. # DEFENCE: When the ball is on our half. elif hive.ball.pos[1] * team_sign(hive.team) < 0: hive.logger.info("DEFENCE START") hive.strategy = Strategy.DEFENCE # Assigns Goalie role to closest drone to goal. potential_goalie = hive.drones[0] for drone in hive.drones: # Resets roles and controllers. drone.role = None drone.controller = None # Checks if drone is closer to goal than the previous potential goalie. if np.linalg.norm(potential_goalie.pos - goal_pos * team_sign(hive.team)) > np.linalg.norm( drone.pos - goal_pos * team_sign(hive.team)): potential_goalie = drone potential_goalie.role = Goalie() # The rest are assigned as Attacker. for drone in hive.drones: if drone.role is None: drone.role = Attacker() # OFFENCE: When the ball in on their half. elif hive.ball.pos[1] * team_sign(hive.team) > 0: hive.logger.info("OFFENCE START") hive.strategy = Strategy.OFFENCE # Assigns all drones to Attackers. for drone in hive.drones: # Resets roles and controllers. drone.role = None drone.controller = None drone.role = Attacker()