def what_to_do(self): # todo: add ball saving state print(self.state) self.bounce = self.choose_bounce_new_way() self.aim(self.to_local(self.bounce[0] - self.loc), speed_needed=self.bounce[1]) return if self.state == None: self.state = 'go for dribble' if self.state == 'kickoff': self.controller.boost = 1 self.controller.throttle = 1 ball_loc_ground = self.grounded(self.ball_loc) self.aim(self.to_local(ball_loc_ground - self.loc)) elif self.state == 'go for dribble' or self.state == 'going for dribble': if self.state == 'go for dribble' or self.bounce[0] == None: self.state = 'going for dribble' self.bounce = self.choose_bounce_new_way() if self.bounce[1] < self.time: self.state = 'go for dribble' if self.state == 'going for dribble' and la.norm(self.bounce[0] - self.loc) < 100: self.state = 'dribble' self.aim(self.to_local(self.bounce[0] - self.loc), speed_needed=self.bounce[1]) elif self.state == 'dribble': if (la.norm(self.ball_loc_loc) > (5 * self.max_current_radius)) and math.atan( self.to_local(self.bounce[0] - self.loc)[1] / self.to_local(self.bounce[0] - self.loc)[0]) > math.pi: self.state = 'go for dribble' self.bounce = self.choose_bounce_old_way() self.aim(self.to_local(self.bounce[0] - self.loc), speed_needed=self.bounce[1])
def aim(self, local_target, speed_needed=99999): print(local_target) self.renderer.begin_rendering('aim') self.renderer.draw_line_3d((self.to_world(local_target) + self.loc), self.loc, self.renderer.green()) self.renderer.end_rendering() alpha = math.atan(local_target[1] / local_target[0]) min_angle = 10 if math.sin(local_target[0] / la.norm(local_target)) < 0: alpha = -alpha if alpha < math.radians(-min_angle): # If the target is more than 10 degrees right from the centre, steer left self.controller.steer = -1 elif alpha > math.radians(min_angle): # If the target is more than 10 degrees left from the centre, steer right self.controller.steer = 1 else: # If the target is less than 10 degrees from the centre, steer straight self.controller.steer = math.degrees(alpha) / min_angle if speed_needed > 0: if speed_needed > self.max_speed[0]: self.controller.throttle = 1 self.controller.boost = 1 elif la.norm(self.vel) < speed_needed: self.controller.throttle = 1 self.controller.boost = 0 if la.norm(local_target) < 200: self.controller.throttle = la.norm(local_target) / 200 if speed_needed < la.norm(self.vel): self.controller.throttle = 0 self.controller.boost = 0 if la.norm(local_target) < 250: self.controller.throttle = -la.norm(local_target) / 250 if la.norm(local_target) < 100: self.controller.throttle = -la.norm(local_target) / 100 if self.sonic: self.controller.boost = 0 return
def choose_bounce_old_way(self): ball_goal_vector = la.vec2(self.enemy_goal_loc[0], self.enemy_goal_loc[1]) - \ la.vec2(self.ball_loc[0], self.ball_loc[1]) radius_multiplier = 1 / 1 # 0.8 num_bounce = len(self.ground_bounce[1]) # num_bounce = 3 path = [None] * num_bounce for bounce_n in range(num_bounce): bounce = self.ground_bounce[1][bounce_n] if ((self.ground_bounce[0][bounce_n]) - self.time) != 0: speed_needed = la.norm(bounce - self.loc) / ( (self.ground_bounce[0][bounce_n]) - self.time) else: speed_needed = self.max_speed[1] if 0 > speed_needed or speed_needed > self.max_speed[1]: continue else: return [ bounce + (self.g_vec3(la.normalize(ball_goal_vector)) * -40), speed_needed ] return [self.ball_loc, self.time + 0.01]
def update_values(self, packet): self.self = packet.game_cars[self.index] # Bot Physics self.loc = self.self.physics.location self.loc = la.vec3(self.loc.x, self.loc.y, self.loc.z) self.rot = self.self.physics.rotation self.rot = la.vec3(self.rot.pitch, self.rot.yaw, self.rot.roll) self.theta = la.euler_rotation(self.rot) self.vel = self.self.physics.velocity self.vel = la.vec3(self.vel.x, self.vel.y, self.vel.z) self.a_vel = self.self.physics.angular_velocity self.a_vel = la.vec3(self.a_vel.x, self.a_vel.y, self.a_vel.z) self.max_current_radius = 1 / sim.max_curvature(la.norm(self.vel)) # Other bot info self.demoed = self.self.is_demolished self.wheel_contact = self.self.has_wheel_contact self.sonic = self.self.is_super_sonic self.jumped = self.self.jumped self.d_jumped = self.self.double_jumped self.team = self.self.team self.boost = self.self.boost # Ball physics self.ball = packet.game_ball self.ball_loc = self.ball.physics.location self.ball_loc = la.vec3(self.ball_loc.x, self.ball_loc.y, self.ball_loc.z) self.ball_rot = self.ball.physics.rotation self.ball_rot = la.vec3(self.ball_rot.pitch, self.ball_rot.yaw, self.ball_rot.roll) self.ball_vel = self.ball.physics.velocity self.ball_vel = la.vec3(self.ball_vel.x, self.ball_vel.y, self.ball_vel.z) # Game info self.game = packet.game_info self.n_cars = packet.num_cars self.time = self.game.seconds_elapsed self.r_active = self.game.is_round_active self.in_kickoff = self.game.is_kickoff_pause self.ended = self.game.is_match_ended # Field info self.field = self.get_field_info() for goal in range(self.field.num_goals): if self.field.goals[goal].team_num == self.team: self.own_goal = self.field.goals[goal] self.own_goal_loc = la.vec3(self.own_goal.location.x, self.own_goal.location.y, self.own_goal.location.z) else: self.enemy_goal = self.field.goals[goal] self.enemy_goal_loc = la.vec3(self.enemy_goal.location.x, self.enemy_goal.location.y, self.enemy_goal.location.z) self.n_boost_pads = self.field.num_boosts # Normalize boost pads self.boost_pads = [] # [(is_active, location, is_full_boost, timer)] for pad in range(self.n_boost_pads): self.boost_pads = self.boost_pads + [ (packet.game_boosts[pad].is_active, self.field.boost_pads[pad].location, self.field.boost_pads[pad].is_full_boost, packet.game_boosts[pad].timer) ]