def __init__(self, index, team, field_info=None): self.time = 0 self.ball = Ball() self.pitch = Pitch() if field_info is None: self.my_goal = Goal(team) self.their_goal = Goal(1 - team) self.boost_pads = [ BoostPad(3, vec3(-3072.0, -4096.0, 73.0), True, 0.0), BoostPad(4, vec3(3072.0, -4096.0, 73.0), True, 0.0), BoostPad(15, vec3(-3584.0, 0.0, 73.0), True, 0.0), BoostPad(18, vec3(3584.0, 0.0, 73.0), True, 0.0), BoostPad(29, vec3(-3072.0, 4096.0, 73.0), True, 0.0), BoostPad(30, vec3(3072.0, 4096.0, 73.0), True, 0.0) ] self.small_boost_pads = [] else: self.my_goal = Goal(team, field_info) self.their_goal = Goal(1 - team, field_info) self.boost_pads = [] self.small_boost_pads = [] for i in range(field_info.num_boosts): current = field_info.boost_pads[i] if field_info.boost_pads[i].is_full_boost: self.boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) else: self.small_boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) self.team = team self.index = index self.cars = [] self.teammates = [] self.opponents = [] self.my_car = Car() self.ball_predictions = [] self.about_to_score = None self.about_to_be_scored_on = None self.time_of_goal = -1
def get_path( time, dt ): path = [] t, path1, path2=self.will_hit( ball, self.car, time ) newvel = self.predict_ball(ball, self.car) ball = Ball( self.info.ball ) swapped = False for i in range(int(time/dt)): ball.step(dt) path.append(vec3(ball.pos)) if i > t and not swapped: ball.vel = vec3(newvel[0].item(), newvel[1].item(), newvel[2].item()) swapped = True return path
def __init__(self, car: Car, ball_predictions, predicate: callable = None, backwards=False): self.ball: Ball = None self.is_viable = True #find the first reachable ball slice that also meets the predicate speed = 1100 if backwards else estimate_max_car_speed(car) for ball in ball_predictions: if estimate_time(car, ball.pos, speed, -1 if backwards else 1) < ball.t - car.time \ and (predicate is None or predicate(car, ball)): self.ball = ball break #if no slice is found, use the last one if self.ball is None: if not ball_predictions: self.ball = Ball() else: self.ball = ball_predictions[-1] self.is_viable = False self.time = self.ball.t self.ground_pos = ground(self.ball.pos) self.pos = self.ball.pos
def predict_ball(self, num_steps, dt): self.about_to_score = False self.about_to_be_scored_on = False self.time_of_goal = -1 self.ball_predictions = [Ball(self.ball)] prediction = Ball(self.ball) for i in range(0, num_steps): prediction.step(dt) self.ball_predictions.append(Ball(prediction)) if self.time_of_goal == -1: if self.my_goal.inside(prediction.pos): self.about_to_be_scored_on = True self.time_of_goal = prediction.t if self.their_goal.inside(prediction.pos): self.about_to_score = True self.time_of_goal = prediction.t
def get_stats(self): ball = Ball() ball.pos = vec3( self.pos[0].item(), self.pos[1].item(), self.pos[2].item() ) ball.vel = vec3( self.vel[0].item(), self.vel[1].item(), self.vel[2].item() ) ball.omega = vec3( self.avel[0].item(), self.avel[1].item(), self.avel[2].item() ) return ball
class GameInfo: DT = 0.01666 def __init__(self, index, team, field_info=None): self.time = 0 self.ball = Ball() self.pitch = Pitch() if field_info is None: self.my_goal = Goal(team) self.their_goal = Goal(1 - team) self.boost_pads = [ BoostPad(3, vec3(-3072.0, -4096.0, 73.0), True, 0.0), BoostPad(4, vec3(3072.0, -4096.0, 73.0), True, 0.0), BoostPad(15, vec3(-3584.0, 0.0, 73.0), True, 0.0), BoostPad(18, vec3(3584.0, 0.0, 73.0), True, 0.0), BoostPad(29, vec3(-3072.0, 4096.0, 73.0), True, 0.0), BoostPad(30, vec3(3072.0, 4096.0, 73.0), True, 0.0) ] self.small_boost_pads = [] else: self.my_goal = Goal(team, field_info) self.their_goal = Goal(1 - team, field_info) self.boost_pads = [] self.small_boost_pads = [] for i in range(field_info.num_boosts): current = field_info.boost_pads[i] if field_info.boost_pads[i].is_full_boost: self.boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) else: self.small_boost_pads.append( BoostPad( i, vec3(current.location.x, current.location.y, current.location.z), True, 0.0)) self.team = team self.index = index self.cars = [] self.teammates = [] self.opponents = [] self.my_car = Car() self.ball_predictions = [] self.about_to_score = None self.about_to_be_scored_on = None self.time_of_goal = -1 def read_packet(self, packet): self.time = packet.game_info.seconds_elapsed dyn = packet.game_ball.physics self.ball.pos = vec3(dyn.location.x, dyn.location.y, dyn.location.z) self.ball.vel = vec3(dyn.velocity.x, dyn.velocity.y, dyn.velocity.z) self.ball.omega = vec3(dyn.angular_velocity.x, dyn.angular_velocity.y, dyn.angular_velocity.z) self.ball.t = self.time self.ball.step(GameInfo.DT) for i in range(0, packet.num_cars): game_car = packet.game_cars[i] dyn = game_car.physics car = Car() if i < len(self.cars): car = self.cars[i] car.pos = vec3(dyn.location.x, dyn.location.y, dyn.location.z) car.vel = vec3(dyn.velocity.x, dyn.velocity.y, dyn.velocity.z) car.omega = vec3(dyn.angular_velocity.x, dyn.angular_velocity.y, dyn.angular_velocity.z) car.theta = euler_rotation( vec3(dyn.rotation.pitch, dyn.rotation.yaw, dyn.rotation.roll)) car.on_ground = game_car.has_wheel_contact car.supersonic = game_car.is_super_sonic car.jumped = game_car.jumped car.double_jumped = game_car.double_jumped car.boost = game_car.boost car.time = self.time car.extrapolate(GameInfo.DT) if len(self.cars) <= i: car.id = i car.team = game_car.team self.cars.append(car) if game_car.team == self.team: if i == self.index: self.my_car = car else: self.teammates.append(car) else: self.opponents.append(car) for i in range(0, len(self.boost_pads)): boost_pad = packet.game_boosts[self.boost_pads[i].index] self.boost_pads[i].is_active = boost_pad.is_active self.boost_pads[i].timer = boost_pad.timer for i in range(0, len(self.small_boost_pads)): boost_pad = packet.game_boosts[self.small_boost_pads[i].index] self.small_boost_pads[i].is_active = boost_pad.is_active self.small_boost_pads[i].timer = boost_pad.timer self.time += GameInfo.DT def predict_ball(self, num_steps, dt): self.about_to_score = False self.about_to_be_scored_on = False self.time_of_goal = -1 self.ball_predictions = [Ball(self.ball)] prediction = Ball(self.ball) for i in range(0, num_steps): prediction.step(dt) self.ball_predictions.append(Ball(prediction)) if self.time_of_goal == -1: if self.my_goal.inside(prediction.pos): self.about_to_be_scored_on = True self.time_of_goal = prediction.t if self.their_goal.inside(prediction.pos): self.about_to_score = True self.time_of_goal = prediction.t
from RLUtilities.Simulation import Car, Ball from RLUtilities.LinearAlgebra import vec3, dot from RLUtilities.Maneuvers import AerialTurn, Aerial c = Car() b = Ball() b.pos = vec3(0, 0, 1000) b.vel = vec3(0, 0, 0) b.omega = vec3(0, 0, 0) b.t = 0 action = Aerial(c, b.pos, b.t) b.step(0.0166) print(action.t_arrival) print(action.target)
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.info.read_packet(packet) self.controls = SimpleControllerState() if self.timer == 0.0: self.csign = random.choice([-1, 1]) # this just initializes the car and ball # to different starting points each time c_position = Vector3(random.uniform(-1000, 1000), random.uniform(-4500, -4000), 25) car_state = CarState(physics=Physics( location=c_position, velocity=Vector3(0, 1000, 0), rotation=Rotator(0, 1.5 * self.csign, 0), angular_velocity=Vector3(0, 0, 0) )) self.bsign = random.choice([-1, 1]) b_position = Vector3(random.uniform(-3500, -3000) * self.bsign, random.uniform(-1500, 1500), random.uniform( 150, 500)) b_velocity = Vector3(random.uniform( 1000, 1500) * self.bsign, random.uniform(- 500, 500), random.uniform( 1000, 1500)) ball_state = BallState(physics=Physics( location=b_position, velocity=b_velocity, rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0) )) self.set_game_state(GameState( ball=ball_state, cars={self.index: car_state}) ) self.increment_timer = True if self.timer < 0.3: self.controls.throttle = 1 * self.csign else: if self.action == None: # set empty values for target and t_arrival initially self.action = Aerial(self.info.my_car, vec3(0,0,0), 0) # predict where the ball will be prediction = Ball(self.info.ball) self.ball_predictions = [vec3(prediction.pos)] for i in range(150): prediction.step(0.016666) prediction.step(0.016666) self.ball_predictions.append(vec3(prediction.pos)) # if the ball is in the air if prediction.pos[2] > 100: self.action.target = prediction.pos self.action.t_arrival = prediction.t # check if we can reach it by an aerial if self.action.is_viable(): break nsteps = 30 delta_t = self.action.t_arrival - self.info.my_car.time prediction = Car(self.info.my_car) self.car_predictions = [vec3(prediction.pos)] for i in range(nsteps): prediction.step(Input(), delta_t / nsteps) self.car_predictions.append(vec3(prediction.pos)) r = 200 self.renderer.begin_rendering() purple = self.renderer.create_color(255, 230, 30, 230) white = self.renderer.create_color(255, 230, 230, 230) x = vec3(r,0,0) y = vec3(0,r,0) z = vec3(0,0,r) target = self.action.target future = self.action.target - self.action.A self.renderer.draw_polyline_3d(self.ball_predictions, purple) self.renderer.draw_line_3d(target - x, target + x, purple) self.renderer.draw_line_3d(target - y, target + y, purple) self.renderer.draw_line_3d(target - z, target + z, purple) self.renderer.draw_polyline_3d(self.car_predictions, white) self.renderer.draw_line_3d(future - x, future + x, white) self.renderer.draw_line_3d(future - y, future + y, white) self.renderer.draw_line_3d(future - z, future + z, white) self.renderer.end_rendering() self.action.step(1.0 / 60.0) self.controls = self.action.controls if self.action.finished or self.timer > 10.0: self.timer = 0.0 self.action = None self.increment_timer = False self.predictions = [] if self.increment_timer: self.timer += 1.0 / 60.0 self.info.my_car.last_input.roll = self.controls.roll self.info.my_car.last_input.pitch = self.controls.pitch self.info.my_car.last_input.yaw = self.controls.yaw self.info.my_car.last_input.boost = self.controls.boost return self.controls