def rotate(self,packet): #continue any strike after checking it if self.current_strike is not None: if check_strike(packet,self.ball_prediction,self.current_strike) and abs(self.current_strike.slice_location.y) > abs(self.bot_car.location.y): self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return else: self.current_strike = None #aim at the post opposite where the ball is target_location = self.posts[self.bot_car.team][0] if self.ball.location.x < 0 else self.posts[self.bot_car.team][1] #wavedash to rotate quicker if self.bot_car.location.dist(target_location) > 3000 and self.bot_car.stable and self.bot_car.orientation.forward.ang_to(target_location-self.bot_car.location)<0.3 and 500<self.bot_car.velocity.length()<1600: self.active_sequence, self.controls = wavedash(packet) return #add something to avoid collisions later if self.bot_car.location.point_in_path(target_location - self.bot_car.location,self.ball.location): target_location = self.ball.location + Vec3(0,math.copysign(150,target_location.x),0) for ally in self.allies: if self.bot_car.location.point_in_path(target_location - self.bot_car.location,ally.location): target_location = ally.location + Vec3(0,math.copysign(150,target_location.x),0) #drive toward target self.controls.throttle = 1.0 self.steer_toward(self.bot_car,target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return
def support(self, packet, position:int): #continue any strike after checking it if self.current_strike is not None: if check_strike(packet,self.ball_prediction,self.current_strike) and self.current_strike.slice_time-packet.game_info.seconds_elapsed < 1.5: self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return else: self.current_strike = None target_location = Vec3(0,0,0) if position==1: target_location = self.ball.location.flat() + Vec3(-self.ball.location.x/2,(self.bot_car.team*2-1)*3000,0) else: target_location = self.ball.location.flat() + (Vec3(0,(self.bot_car.team*2-1)*6500,0) - self.ball.location.flat()).rescale(6000) target_location = self.point_in_field(target_location) #second man in net on defence, or if would drive up wall if self.ball.location.y *(2*self.bot_car.team-1)>0 and self.ball.velocity.y *(2*self.bot_car.team-1)>0 or abs(target_location.y)>4600: target_location = Vec3(0,5200*(2*self.bot_car.team-1),0) #if it needs to drive around posts, do it in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = target_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) facing_ang = self.bot_car.orientation.forward.ang_to(Vec3(1,0,1)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) travel_between_posts = post0_ang < std_ang_to_target < post1_ang facing_between_posts = post0_ang < facing_ang < post1_ang if not in_goal and not travel_between_posts: target_location = Vec3(0,5000*(2*self.bot_car.team-1),0) for ally in self.allies: if ally.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]): target_location = Vec3(-1*math.copysign(950,self.ball.location.x),4900,0) self.controls.throttle = min(self.bot_car.location.dist(target_location)**2/1000**2,1) if in_goal and self.bot_car.orientation.forward.y*(1-2*self.bot_car.team) >0.7 and abs(self.bot_car.location.y) <5220: self.controls.throttle = 0 if self.bot_car.velocity.length() <20 else -1*math.copysign(1,self.bot_car.velocity.dot(self.bot_car.orientation.forward)) self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return #exit goal if needed ##try: in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = target_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = min(self.bot_car.location.dist(target_location)**2/800**2+0.1,1) self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return ##except: ##stub="catch div 0 errors" self.controls.throttle = min(self.bot_car.location.dist(target_location)**2/1000**2,1) self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return
def defend(self,packet): #get the vector of the ball to where it will hit the goal ( ball_to_goal = self.ball.velocity.flat() if ball_to_goal.length()!=0: ball_to_goal = ball_to_goal.rescale(((5120-abs(self.ball.location.y))/abs(ball_to_goal.y))) #target 2/3 way between ball and goal target_location = self.ball.location + ball_to_goal/1.5 #get vector of ball from the cone of own net ideal_shot = self.ball.location.flat() - Vec3(0,-8000,0) if self.bot_car.team==0 else self.ball.location.flat() - Vec3(0,8000,0) #ideal shot is to corner if on other side of ball going_to_overtake_ball = (self.bot_car.location-Vec3(0,6000*(2*self.bot_car.team-1),0)).scalar_proj(self.ball.location-Vec3(0,6000*(2*self.bot_car.team-1),0))+100 > Vec3(0,6000*(2*self.bot_car.team-1),0).dist(self.ball.location) if going_to_overtake_ball: ideal_shot = Vec3(math.copysign(4096,(ball_to_goal+self.ball.location).x),(self.team*2-1)*5120,0) - self.ball.location #continue any strike after checking it if self.current_strike is not None: if check_strike(packet,self.ball_prediction,self.current_strike): #otherwise, continue on strike self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes,defence=True) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) #drive out of goal > priority in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = strike_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return return else: self.current_strike = None #try to find strikes if self.ball.velocity.length()!=0 and self.current_strike is None and self.bot_car.stable: strikes = find_strikes(packet,self.ball_prediction,self.bot_car,ideal_shot,defence=True) for strike in strikes: if strike.strike_type==strike_types.simple_linear: #linear, will check if line to strike location goes into net post0_ang = self.posts[self.bot_car.team][0].__sub__(self.ball.location).flat().ang_to(Vec3(1,0,0)) car_slice_ang = (strike.slice_location - self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.posts[self.bot_car.team][1].__sub__(self.ball.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < car_slice_ang < post1_ang if abs(self.bot_car.location.y)>abs(self.ball.location.y): #clear if on correct side if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif all(abs(ally.location.y-(1-ally.team*2)*6000) < abs(self.bot_car.location.y-(1-ally.team*2)*6000) or abs(ally.location.x)>2500 for ally in self.allies) and not between_posts: #clear to corner if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif strike.strike_type==strike_types.linear_jump or strike.strike_type==strike_types.linear_dblj: #linear, will check if line to strike location goes into net post0_ang = self.posts[self.bot_car.team][0].__sub__(self.ball.location).flat().ang_to(Vec3(1,0,0)) car_slice_ang = (strike.slice_location - self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.posts[self.bot_car.team][1].__sub__(self.ball.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < car_slice_ang < post1_ang if abs(self.bot_car.location.y)>abs(self.ball.location.y): #clear if on correct side if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif all(abs(ally.location.y-(1-ally.team*2)*6000) < abs(self.bot_car.location.y-(1-ally.team*2)*6000) or abs(ally.location.x)>2500 for ally in self.allies) and not between_posts: #clear to corner if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike #execute straight away if self.current_strike is not None: self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes,defence=True) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) #drive out of goal > priority in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = strike_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return return """ keep the rest for now, esp for defence """ #changed to if car is further from goal than ball if Vec3(0,6000*(2*self.bot_car.team-1),0).dist(self.bot_car.location) > Vec3(0,6000*(2*self.bot_car.team-1),0).dist(self.ball.location)-100: offset = self.bot_car.vec_to_ball.flat().cross(Vec3(0,0,1)).normalized() if offset.y>0 and self.ball.velocity.y <0 or offset.y<0 and self.ball.velocity.y >0: offset = -offset target_location = target_location + 150*offset self.controls.throttle=1.0 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return #Drive to the point, using atan to slow when close to the point (judged by d/v) est_time = -1 try: est_time = self.bot_car.location.dist(target_location) / self.bot_car.velocity.flat().scalar_proj((target_location - self.bot_car.location).flat()) except: stub="catch div 0" self.controls.throttle = (2*math.atan(est_time)/math.pi)*1.2-0.2 if est_time>0 else 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return
def clear(self,packet): #get vector of ball from the cone of own net ideal_shot = self.ball.location.flat() - Vec3(0,-8000,0) if self.bot_car.team==0 else self.ball.location.flat() - Vec3(0,8000,0) #find a future position based off the distance from the ball, using the current location as a backup future_location = self.ball.location future_velocity = self.ball.velocity #continue any strike after checking it if self.current_strike is not None and self.bot_car.stable: if check_strike(packet, self.ball_prediction, self.current_strike): self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls #drive out of goal > priority in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = self.current_strike.slice_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return else: self.current_strike = None #try to find strikes if self.ball.velocity.length()!=0 and self.current_strike is None: strikes = find_strikes(packet,self.ball_prediction,self.bot_car,ideal_shot) for strike in strikes: if strike.strike_type==strike_types.simple_linear: #linear if (strike.slice_location - Vec3(0,5500*(self.bot_car.team*2-1),0)).ang_to(self.bot_car.orientation.forward) < 2.2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif strike.strike_type==strike_types.linear_jump: #long jump if (strike.slice_location - Vec3(0,5500*(self.bot_car.team*2-1),0)).ang_to(self.bot_car.orientation.forward) < 2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif strike.strike_type==strike_types.linear_dblj: #double jump if (strike.slice_location - Vec3(0,5500*(self.bot_car.team*2-1),0)).ang_to(self.bot_car.orientation.forward) < 2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike #execute straight away if one was chosen: #execute if self.current_strike is not None: #drive out of goal > priority in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = self.current_strike.slice_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return #execute self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return #position for a better shot future_location, future_velocity = Vec3(self.ball.location), Vec3(self.ball.velocity) future_slice = find_slice_at_time(self.ball_prediction,packet.game_info.seconds_elapsed + 2) if future_slice is not None: future_location = Vec3(future_slice.physics.location) future_velocity = Vec3(future_slice.physics.velocity) self.renderer.draw_line_3d(self.ball.location, future_location, self.renderer.cyan()) target_location = self.point_in_field(future_location.flat()+ideal_shot.rescale(-500)) #drive out of goal > priority in_goal = self.bot_car.location.within(self.goal_corners[self.bot_car.team][0],self.goal_corners[self.bot_car.team][1]) post0_ang = self.collision_posts[self.bot_car.team][0].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) std_ang_to_target = target_location.__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) post1_ang = self.collision_posts[self.bot_car.team][1].__sub__(self.bot_car.location).flat().ang_to(Vec3(1,0,0)) between_posts = post0_ang < std_ang_to_target < post1_ang if not between_posts and in_goal: target_location = Vec3(0,(2*self.team-1)*5000,0) self.controls.throttle = 1 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return self.controls.throttle = 1.0 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return
def shoot(self,packet): #get vector of ball to the back of other net ideal_shot = Vec3(0,6000,0) - self.ball.location.flat() if self.bot_car.team==0 else Vec3(0,-6000,0) - self.ball.location.flat() #continue any strike after checking it if self.current_strike is not None: if check_strike(packet, self.ball_prediction, self.current_strike): self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return else: self.current_strike = None #try to find strikes if self.ball.velocity.length()!=0 and self.current_strike is None and self.bot_car.stable: strikes = find_strikes(packet, self.ball_prediction, self.bot_car, ideal_shot) for strike in strikes: if strike.strike_type==strike_types.simple_linear: #linear if (Vec3(0,6000*(1-self.bot_car.team*2),0) - strike.slice_location).ang_to(self.bot_car.orientation.forward) < 2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif strike.strike_type==strike_types.linear_jump: #long jump if (Vec3(0,6000*(1-self.bot_car.team*2-1),0) - strike.slice_location).ang_to(self.bot_car.orientation.forward) < 2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike elif strike.strike_type==strike_types.linear_dblj: #double jump if (Vec3(0,6000*(1-self.bot_car.team*2-1),0) - strike.slice_location).ang_to(self.bot_car.orientation.forward) < 2: #if linear strike is not a massive angle from the goal if self.current_strike is not None: self.current_strike = strike if strike.slice_time<self.current_strike.slice_time else self.current_strike else: self.current_strike = strike #execute straight away if one was chosen if self.current_strike is not None: self.active_sequence, strike_controls, strike_location, strike_time = execute_strike(packet,self.bot_car,self.current_strike,self.foes) self.controls = strike_controls self.renderer.draw_rect_3d(strike_location, 8, 8, True, self.renderer.red(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, strike_location, self.renderer.white()) self.renderer.draw_string_2d(20,20,3,3,f"throttle: {self.controls.throttle}",self.renderer.white()) return #position to get a shot on the ball future_location, future_velocity = Vec3(self.ball.location), Vec3(self.ball.velocity) future_slice = find_slice_at_time(self.ball_prediction,packet.game_info.seconds_elapsed + 2) if future_slice is not None: future_location = Vec3(future_slice.physics.location) future_velocity = Vec3(future_slice.physics.velocity) self.renderer.draw_line_3d(self.ball.location, future_location, self.renderer.cyan()) target_location = self.point_in_field(future_location.flat()+ideal_shot.rescale(-500)) self.controls.throttle = 1.0 self.steer_toward(self.bot_car, target_location) self.renderer.draw_rect_3d(target_location, 8, 8, True, self.renderer.cyan(), centered=True) self.renderer.draw_line_3d(self.bot_car.location, target_location, self.renderer.white()) return