class CollectBoost(BaseAction): action = None def get_output(self, info: Game) -> SimpleControllerState: car = info.my_car if not self.action: self.action = Drive(car) boost_pad = closest_available_boost(car.location, info.pads) if boost_pad is None: # All boost pads are inactive. return self.controls self.action.target = boost_pad.location self.action.step(info.time_delta) self.controls = self.action.controls return self.controls def get_possible(self, info: Game): return True def update_status(self, info: Game): if info.my_car.boost == 100: self.finished = True
class Kickoff: def __init__(self, car: Car, info: Game): self.car: Car = car self.info: Game = info self.controls = Input() self.drive = Drive(car) def step(self, dt): self.drive.target = self.info.ball.position self.drive.speed = 1500 self.drive.step(self.info.time_delta) self.controls = self.drive.controls
class Derevo(BaseAgent): """Main bot class""" def __init__(self, name, team, index): """Initializing all parameters of the bot""" super().__init__(name, team, index) Game.set_mode("soccar") self.game = Game(index, team) self.name = name self.team = team self.index = index self.drive = None self.dodge = None self.controls = SimpleControllerState() self.kickoff = False self.prev_kickoff = False self.kickoffStart = None self.step = None def initialize_agent(self): """Initializing all parameters which require the field info""" init_boostpads(self) self.drive = Drive(self.game.my_car) self.dodge = Dodge(self.game.my_car) def get_output(self, packet: GameTickPacket) -> SimpleControllerState: """The main method which receives the packets and outputs the controls""" self.game.read_game_information(packet, self.get_rigid_body_tick(), self.get_field_info()) update_boostpads(self, packet) self.prev_kickoff = self.kickoff self.kickoff = packet.game_info.is_kickoff_pause and norm( vec2(self.game.ball.location - vec3(0, 0, 0))) < 100 if self.kickoff and not self.prev_kickoff: init_kickoff(self) self.prev_kickoff = True elif self.kickoff or self.step is Step.Dodge_2: kick_off(self) else: self.drive.target = self.game.ball.location self.drive.speed = 1410 self.drive.step(self.game.time_delta) self.controls = self.drive.controls if not packet.game_info.is_round_active: self.controls.steer = 0 return self.controls
class Agent(BaseAgent): def __init__(self, name, team, index): self.game = Game(index, team) self.controls = SimpleControllerState() self.action = None def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.game.read_game_information(packet, self.get_rigid_body_tick(), self.get_field_info()) if not self.action: self.action = Drive(self.game.my_car) self.action.speed = 1400 self.action.target = self.game.ball.location self.action.step(self.game.time_delta) self.controls = self.action.controls return self.controls
class CustomDrive: def __init__(self, car): self.car = car self.target = vec3(0, 0, 0) self.speed = 2300 self.controls = SimpleControllerState() self.finished = False self.rlu_drive = RLUDrive(self.car) self.update_rlu_drive() self.power_turn = True # Handbrake while reversing to turn around quickly def step(self, dt: float): self.update_rlu_drive() self.rlu_drive.step(dt) self.finished = self.rlu_drive.finished car_to_target = (self.target - self.car.position) local_target = dot(car_to_target, self.car.orientation) angle = atan2(local_target[1], local_target[0]) self.controls = self.rlu_drive.controls reverse = (cos(angle) < 0) if reverse: angle = -invert_angle(angle) if self.power_turn: self.controls.throttle = (-self.controls.throttle - 1) / 2 angle *= -1 else: self.controls.throttle = -1 self.controls.steer = cap(angle * 3, -1, 1) self.controls.boost = False self.controls.handbrake = (abs(angle) > radians(70)) def update_rlu_drive(self): self.target = self.target self.rlu_drive.target = self.target self.rlu_drive.speed = self.speed
class GetToAirPoint: """Drive towards the point, jump and start hovering when near enough.""" def __init__(self, car: Car, info: Game): self.target: vec3 = None self.car: Car = car self.info = info self.hover = Hover(car) self.drive = Drive(car) self.controls: Input = Input() self.__time_spent_on_ground = 0.0 def step(self, dt): if self.car.on_ground and norm(self.car.position + self.car.velocity - xy(self.target)) > 2500: self.drive.speed = 1000 self.drive.target = self.target self.drive.step(dt) self.controls = self.drive.controls self.controls.handbrake = angle_between(self.car.forward(), self.target - self.car.position) > 1.2 return self.hover.target = self.target self.hover.up = normalize(self.car.position * -1) self.hover.step(dt) self.controls = self.hover.controls self.controls.throttle = not self.car.on_ground self.controls.jump = (self.car.position[2] < 30 or self.car.on_ground) and self.__time_spent_on_ground > 0.1 if self.info.round_active: self.__time_spent_on_ground += dt if not self.car.on_ground: self.__time_spent_on_ground = 0.0
class MyAgent(BaseAgent): def __init__(self, name, team, index): super().__init__(name, team, index) self.game = Game(index, team) self.name = name self.controls = SimpleControllerState() self.timer = 0.0 self.drive = Drive(self.game.my_car) self.navigator = None self.dodge = None self.turn = None self.state = State.RESET def get_output(self, packet: GameTickPacket) -> SimpleControllerState: self.game.read_game_information(packet, self.get_rigid_body_tick(), self.get_field_info()) self.controls = SimpleControllerState() next_state = self.state if self.state == State.RESET: self.timer = 0.0 self.set_gamestate_straight_moving() # self.set_gamestate_angled_stationary() # self.set_gamestate_straight_moving_towards() next_state = State.WAIT if self.state == State.WAIT: if self.timer > 0.2: next_state = State.INITIALIZE if self.state == State.INITIALIZE: self.drive = Drive(self.game.my_car) self.drive.target, self.drive.speed = self.game.ball.location, 2300 next_state = State.DRIVING if self.state == State.DRIVING: self.drive.target = self.game.ball.location self.drive.step(self.game.time_delta) self.controls = self.drive.controls can_dodge, simulated_duration, simulated_target = self.simulate() if can_dodge: self.dodge = Dodge(self.game.my_car) self.turn = AerialTurn(self.game.my_car) print("============") print(simulated_duration) self.dodge.duration = simulated_duration - 0.1 self.dodge.target = simulated_target self.timer = 0 next_state = State.DODGING if self.state == State.DODGING: self.dodge.step(self.game.time_delta) self.controls = self.dodge.controls if self.game.time == packet.game_ball.latest_touch.time_seconds: print(self.timer) if self.dodge.finished and self.game.my_car.on_ground: next_state = State.RESET self.timer += self.game.time_delta self.state = next_state return self.controls def simulate(self): ball_prediction = self.get_ball_prediction_struct() duration_estimate = math.floor( get_time_at_height(self.game.ball.location[2], 0.2) * 10) / 10 for i in range(6): car = Car(self.game.my_car) ball = Ball(self.game.ball) batmobile = obb() batmobile.half_width = vec3(64.4098892211914, 42.335182189941406, 14.697200775146484) batmobile.center = car.location + dot(car.rotation, vec3(9.01, 0, 12.09)) batmobile.orientation = car.rotation dodge = Dodge(car) dodge.duration = duration_estimate + i / 60 dodge.target = ball.location for j in range(round(60 * dodge.duration)): dodge.target = ball.location dodge.step(1 / 60) car.step(dodge.controls, 1 / 60) prediction_slice = ball_prediction.slices[j] physics = prediction_slice.physics ball_location = vec3(physics.location.x, physics.location.y, physics.location.z) dodge.target = ball_location batmobile.center = car.location + dot(car.rotation, vec3(9.01, 0, 12.09)) batmobile.orientation = car.rotation if intersect(sphere(ball_location, 93.15), batmobile) and abs( ball_location[2] - car.location[2] ) < 25 and car.location[2] < ball_location[2]: return True, j / 60, ball_location return False, None, None def set_gamestate_straight_moving(self): # put the car in the middle of the field car_state = CarState( physics=Physics(location=Vector3(0, -1000, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 2, 0), angular_velocity=Vector3(0, 0, 0))) # put the ball in the middle of the field ball_state = BallState( physics=Physics(location=Vector3(0, 1500, 93), velocity=Vector3(0, random.randint(-250, 800), random.randint(700, 800)), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_gamestate_straight_moving_towards(self): # put the car in the middle of the field car_state = CarState(physics=Physics( location=Vector3(0, 0, 18), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0), )) # put the ball in the middle of the field ball_state = BallState(physics=Physics( location=Vector3(0, 2500, 93), velocity=Vector3(0, -250, 700), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0), )) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_gamestate_angled_stationary(self): # put the car in the middle of the field car_state = CarState( physics=Physics(location=Vector3(-1000, -1500, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 8, 0), angular_velocity=Vector3(0, 0, 0))) # put the ball in the middle of the field ball_state = BallState( physics=Physics(location=Vector3(0, 0, 750), velocity=Vector3(0, 0, 1), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state}))
class MyAgent(BaseAgent): def __init__(self, name, team, index): super().__init__(name, team, index) Game.set_mode("soccar") self.game = Game(index, team) self.name = name self.controls = SimpleControllerState() self.timer = 0.0 self.drive = Drive(self.game.my_car) self.dodge = None self.turn = None self.state = State.RESET def get_output(self, packet: GameTickPacket) -> SimpleControllerState: # Update the game values and set the state self.game.read_game_information(packet, self.get_field_info()) self.controls = SimpleControllerState() next_state = self.state # Reset everything if self.state == State.RESET: self.timer = 0.0 # self.set_gamestate_straight_moving() # self.set_gamestate_straight_moving_towards() self.set_state_stationary_angled() # self.set_gamestate_angled_stationary() # self.set_state_stationary() next_state = State.WAIT # Wait so everything can settle in, mainly for ball prediction if self.state == State.WAIT: if self.timer > 0.2: next_state = State.INITIALIZE # Initialize the drive mechanic if self.state == State.INITIALIZE: self.drive = Drive(self.game.my_car) self.drive.target = self.game.ball.position self.drive.speed = 1400 next_state = State.DRIVING # Start driving towards the target and check whether a dodge is possible, if so initialize the dodge if self.state == State.DRIVING: self.drive.target = self.game.ball.position self.drive.step(self.game.time_delta) self.controls = self.drive.controls a = time.time() target = self.game.my_car.position + 1000000 * ( self.game.ball.position - self.game.my_car.position) can_dodge, simulated_duration, simulated_target = self.simulate() print(time.time() - a) if can_dodge: self.dodge = Dodge(self.game.my_car) self.turn = AerialTurn(self.game.my_car) self.dodge.duration = simulated_duration - 0.1 self.dodge.target = simulated_target self.dodge.preorientation = look_at(simulated_target, vec3(0, 0, 1)) self.timer = 0 next_state = State.DODGING # Perform the dodge if self.state == State.DODGING: self.dodge.step(self.game.time_delta) self.controls = self.dodge.controls T = self.dodge.duration - self.dodge.timer if T > 0: if self.dodge.timer < 0.2: self.controls.boost = 1 # self.controls.pitch = 1 else: xf = self.game.my_car.position + 0.5 * T * T * vec3( 0, 0, -650) + T * self.game.my_car.velocity delta_x = self.game.ball.position - xf if angle_between(vec2(self.game.my_car.forward()), self.dodge.direction) < 0.3: if norm(delta_x) > 50: self.controls.boost = 1 self.controls.throttle = 0.0 else: self.controls.boost = 0 self.controls.throttle = clip( 0.5 * (200 / 3) * T * T, 0.0, 1.0) else: self.controls.boost = 0 self.controls.throttle = 0.0 else: self.controls.boost = 0 # Great line # if self.game.time == packet.game_ball.latest_touch.time_seconds: # print(self.game.my_car.position) if self.dodge.finished and self.game.my_car.on_ground: next_state = State.RESET self.timer += self.game.time_delta self.state = next_state return self.controls # The miraculous simulate function # TODO optimize heavily in case I actually need it # Option one: estimate the time for the current height and look at that ball prediction. # If its heigher use that unless it gets unreachable and else compare with the lower one. # If duration_estimate = 0.8 and the ball is moving up there is not sense in even simulating it. # Might even lower it since the higher the duration estimate the longer the simulation takes. def simulate(self, global_target=None): lol = 0 # Initialize the ball prediction # Estimate the probable duration of the jump and round it down to the floor decimal ball_prediction = self.get_ball_prediction_struct() if self.game.my_car.boost < 6: duration_estimate = math.floor( get_time_at_height(self.game.ball.position[2]) * 10) / 10 else: adjacent = norm( vec2(self.game.my_car.position - self.game.ball.position)) opposite = (self.game.ball.position[2] - self.game.my_car.position[2]) theta = math.atan(opposite / adjacent) t = get_time_at_height_boost(self.game.ball.position[2], theta, self.game.my_car.boost) duration_estimate = (math.ceil(t * 10) / 10) # Loop for 6 frames meaning adding 0.1 to the estimated duration. Keeps the time constraint under 0.3s for i in range(6): # Copy the car object and reset the values for the hitbox car = Car(self.game.my_car) # Create a dodge object on the copied car object # Direction is from the ball to the enemy goal # Duration is estimated duration plus the time added by the for loop # preorientation is the rotation matrix from the ball to the goal # TODO make it work on both sides # Test with preorientation. Currently it still picks a low duration at a later time meaning it # wont do any of the preorientation. dodge = Dodge(car) prediction_slice = ball_prediction.slices[round( 60 * (duration_estimate + i / 60))] physics = prediction_slice.physics ball_location = vec3(physics.location.x, physics.location.y, physics.location.z) # ball_location = vec3(0, ball_y, ball_z) dodge.duration = duration_estimate + i / 60 if dodge.duration > 1.4: break if global_target is not None: dodge.direction = vec2(global_target - ball_location) target = vec3(vec2(global_target)) + vec3( 0, 0, jeroens_magic_number * ball_location[2]) dodge.preorientation = look_at(target - ball_location, vec3(0, 0, 1)) else: dodge.target = ball_location dodge.direction = vec2(ball_location) + vec2(ball_location - car.position) dodge.preorientation = look_at(ball_location, vec3(0, 0, 1)) # Loop from now till the end of the duration fps = 30 for j in range(round(fps * dodge.duration)): lol = lol + 1 # Get the ball prediction slice at this time and convert the location to RLU vec3 prediction_slice = ball_prediction.slices[round(60 * j / fps)] physics = prediction_slice.physics ball_location = vec3(physics.location.x, physics.location.y, physics.location.z) dodge.step(1 / fps) T = dodge.duration - dodge.timer if T > 0: if dodge.timer < 0.2: dodge.controls.boost = 1 dodge.controls.pitch = 1 else: xf = car.position + 0.5 * T * T * vec3( 0, 0, -650) + T * car.velocity delta_x = ball_location - xf if angle_between(vec2(car.forward()), dodge.direction) < 0.3: if norm(delta_x) > 50: dodge.controls.boost = 1 dodge.controls.throttle = 0.0 else: dodge.controls.boost = 0 dodge.controls.throttle = clip( 0.5 * (200 / 3) * T * T, 0.0, 1.0) else: dodge.controls.boost = 0 dodge.controls.throttle = 0.0 else: dodge.controls.boost = 0 car.step(dodge.controls, 1 / fps) succesfull = self.dodge_succesfull(car, ball_location, dodge) if succesfull is not None: if succesfull: return True, j / fps, ball_location else: break return False, None, None def dodge_succesfull(self, car, ball_location, dodge): batmobile = obb() batmobile.half_width = vec3(64.4098892211914, 42.335182189941406, 14.697200775146484) batmobile.center = car.position + dot(car.orientation, vec3(9.01, 0, 12.09)) batmobile.orientation = car.orientation ball = sphere(ball_location, 93.15) b_local = dot(ball.center - batmobile.center, batmobile.orientation) closest_local = vec3( min(max(b_local[0], -batmobile.half_width[0]), batmobile.half_width[0]), min(max(b_local[1], -batmobile.half_width[1]), batmobile.half_width[1]), min(max(b_local[2], -batmobile.half_width[2]), batmobile.half_width[2])) hit_location = dot(batmobile.orientation, closest_local) + batmobile.center if norm(hit_location - ball.center) > ball.radius: return None # if abs(ball_location[2] - hit_location[2]) < 25 and hit_location[2] < ball_location[2]: if abs(ball_location[2] - hit_location[2]) < 25: if closest_local[0] > 35 and -12 < closest_local[2] < 12: hit_check = True else: print("local: ", closest_local) hit_check = True else: hit_check = False # Seems to work without angle_check. No clue why though angle_car_simulation = angle_between(car.orientation, self.game.my_car.orientation) angle_simulation_target = angle_between(car.orientation, dodge.preorientation) angle_check = angle_simulation_target < angle_car_simulation or angle_simulation_target < 0.1 return hit_check """" State setting methods for various situations""" def set_gamestate_straight_moving(self): # put the car in the middle of the field car_state = CarState( physics=Physics(location=Vector3(0, -1000, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 2, 0), angular_velocity=Vector3(0, 0, 0))) # put the ball in the middle of the field ball_state = BallState( physics=Physics(location=Vector3(0, 1500, 93), velocity=Vector3(200, 650, 750), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_gamestate_straight_moving_towards(self): # put the car in the middle of the field car_state = CarState(physics=Physics( location=Vector3(0, 0, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 2, 0), angular_velocity=Vector3(0, 0, 0), ), boost_amount=50) # put the ball in the middle of the field ball_state = BallState(physics=Physics( location=Vector3(0, 2500, 93), velocity=Vector3(0, -250, 500), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0), )) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_gamestate_angled_stationary(self): # put the car in the middle of the field car_state = CarState( physics=Physics(location=Vector3(-1000, -2000, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 8, 0), angular_velocity=Vector3(0, 0, 0))) # put the ball in the middle of the field ball_state = BallState( physics=Physics(location=Vector3(0, 0, 600), velocity=Vector3(0, 0, 1), rotation=Rotator(0, 0, 0), angular_velocity=Vector3(0, 0, 0))) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_state_stationary(self): # put the car in the middle of the field car_state = CarState(physics=Physics( location=Vector3(0, -2500, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 2, 0), angular_velocity=Vector3(0, 0, 0), ), boost_amount=100) # put the ball in the middle of the field ball_state = BallState(physics=Physics( location=Vector3(0, ball_y, ball_z), velocity=Vector3(0, 0, 0), angular_velocity=Vector3(0, 0, 0), )) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state})) def set_state_stationary_angled(self): # put the car in the middle of the field car_state = CarState(physics=Physics( location=Vector3(0, -2500, 18), velocity=Vector3(0, 0, 0), rotation=Rotator(0, math.pi / 2, 0), angular_velocity=Vector3(0, 0, 0), ), boost_amount=100) # put the ball in the middle of the field ball_state = BallState(physics=Physics( location=Vector3(1500, 0, 93), velocity=Vector3(0, 0, 750), angular_velocity=Vector3(0, 0, 0), )) self.set_game_state( GameState(ball=ball_state, cars={self.game.id: car_state}))
class CustomDrive: def __init__(self, car): self.car = car self.target = vec3(0, 0, 0) self.speed = 2300 self.controls = SimpleControllerState() self.finished = False self.rlu_drive = RLUDrive(self.car) self.update_rlu_drive() self.power_turn = True # Handbrake while reversing to turn around quickly self.aerial_turn = AerialTurn(car) self.kickoff = False def step(self, dt: float): self.speed = abs(self.speed) car_to_target = (self.target - self.car.location) local_target = dot(car_to_target, self.car.rotation) angle = atan2(local_target[1], local_target[0]) vel = norm(self.car.velocity) in_air = (not self.car.on_ground) on_wall = (self.car.location[2] > 250 and not in_air) reverse = (cos(angle) < 0 and not (on_wall or in_air or self.kickoff)) get_off_wall = (on_wall and local_target[2] > 450) if get_off_wall: car_to_target[2] = -self.car.location[2] local_target = dot(car_to_target, self.car.rotation) angle = atan2(local_target[1], local_target[0]) max_speed = self.determine_max_speed(local_target) self.update_rlu_drive(reverse, max_speed) self.rlu_drive.step(dt) self.finished = self.rlu_drive.finished self.controls = self.rlu_drive.controls self.controls.handbrake = False if reverse: angle = -invert_angle(angle) if self.power_turn and not on_wall: angle *= -1 self.controls.handbrake = (vel > 200) self.controls.steer = cap(angle * 3, -1, 1) self.controls.boost = False if not self.controls.handbrake: self.controls.handbrake = (abs(angle) > radians(70) and vel > 500 and not on_wall) if self.controls.handbrake: self.controls.handbrake = (dot(self.car.velocity, car_to_target) > -150) if in_air: self.aerial_turn.target = look_at(xy(car_to_target), vec3(0, 0, 1)) self.aerial_turn.step(dt) aerial_turn_controls = self.aerial_turn.controls self.controls.pitch = aerial_turn_controls.pitch self.controls.yaw = aerial_turn_controls.yaw self.controls.roll = aerial_turn_controls.roll self.controls.boost = False def update_rlu_drive(self, reverse: bool = False, max_speed: float = 2200): self.target = self.target self.rlu_drive.target = self.target self.rlu_drive.speed = cap(self.speed * (-1 if reverse else 1), -max_speed, max_speed) def determine_max_speed(self, local_target): low = 100 high = 2200 if self.kickoff: return high for i in range(5): mid = (low + high) / 2 radius = (1 / RLUDrive.max_turning_curvature(mid)) local_circle = vec3(0, copysign(radius, local_target[1]), 0) dist = norm(local_circle - xy(local_target)) if dist < radius: high = mid else: low = mid return high