def update(self): if self.agent.me.location[2] < 600: if self.agent.onSurface or self.agent.me.location[2] < 100: self.active = False controller_state = SimpleControllerState() if self.agent.me.rotation[2] > 0: controller_state.roll = -1 elif self.agent.me.rotation[2] < 0: controller_state.roll = 1 if self.agent.me.rotation[0] > self.agent.velAngle: controller_state.yaw = -1 elif self.agent.me.rotation[0] < self.agent.velAngle: controller_state.yaw = 1 if self.active: controller_state.throttle = 1 else: controller_state.throttle = 0 else: controller_state = SimpleControllerState() return controller_state
def exec(self, bot) -> SimpleControllerState: ct = time.time() - self._start_time controls = SimpleControllerState() controls.throttle = 1 car = bot.data.my_car # Target is allowed to be a function that takes bot as a parameter. Check what it is if callable(self.target): target = self.target(bot) else: target = self.target # To boost or not to boost, that is the question car_to_target = target - car.pos vel_p = proj_onto_size(car.vel, car_to_target) angle = angle_between(car_to_target, car.forward()) controls.boost = self.boost and angle < self._boost_ang_req and vel_p < self._max_speed # States of dodge (note reversed order) # Land on ground if ct >= self._t_finishing: self._almost_finished = True if car.on_ground: self.done = True else: bot.maneuver = RecoveryManeuver(bot) self.done = True return controls elif ct >= self._t_second_unjump: # Stop pressing jump and rotate and wait for flip is done pass elif ct >= self._t_aim: if ct >= self._t_second_jump: controls.jump = 1 # Direction, yaw, pitch, roll if self.target is None: controls.roll = 0 controls.pitch = -1 controls.yaw = 0 else: target_local = dot(car_to_target, car.rot) target_local.z = 0 direction = normalize(target_local) controls.roll = 0 controls.pitch = -direction.x controls.yaw = sign(car.rot.get(2, 2)) * direction.y # Stop pressing jump elif ct >= self._t_first_unjump: pass # First jump else: controls.jump = 1 return controls
def controller(self, agent): controller_state = SimpleControllerState() controller_state.pitch = 1 controller_state.throttle = -1 self.time_difference = agent.game_info.seconds_elapsed - self.start if self.time_difference <= 0.1: controller_state.jump = True elif 0.1 <= self.time_difference <= 0.15: controller_state.jump = False elif 0.15 <= self.time_difference <= 0.35: controller_state.jump = True elif 0.4 <= self.time_difference <= 1.75: controller_state.pitch = -1 if 1.2 <= self.time_difference: controller_state.throttle = 1 if 0.575 <= self.time_difference <= 1.2: controller_state.boost = True controller_state.roll = 1 if 0.7 <= self.time_difference <= 1.5: controller_state.yaw = .5 return controller_state
def fix_orientation(data: Data, point=None): controller = SimpleControllerState() strength = 0.22 ori = data.car.orientation if point is None and data.car.velocity.flat().length2() != 0: point = data.car.location + data.car.velocity.flat().rescale(500) pitch_error = -ori.pitch * strength controller.pitch = data.agent.pid_pitch.calc(pitch_error) roll_error = -ori.roll * strength controller.roll = data.agent.pid_roll.calc(roll_error) if point is not None: # yaw rotation can f up the other's so we scale it down until we are more confident about landing on the wheels car_to_point = point - data.car.location yaw_error = ori.front.ang_to_flat(car_to_point) * strength * 1.5 land_on_wheels01 = 1 - ori.up.ang_to(UP) / (math.pi * 2) controller.yaw = data.agent.pid_yaw.calc(yaw_error) * (land_on_wheels01 **6) # ! controller.throttle = 1 return controller
def get_output(self, packet: GameTickPacket) -> Optional[SimpleControllerState]: controller = SimpleControllerState() bot = PhysicsObject(packet.game_cars[self.agent.index].physics) if packet.game_info.seconds_elapsed > self.next_dodge_time: controller.jump = True # Calculate pitch and roll based on target and bot position # Correct yaw from (0 to pi to -pi to 0), to (0 to 2pi). # Then rotate circle by pi/2 degrees. Then flip circle vertically. yaw = bot.rotation.z if yaw < 0: yaw += 2 * math.pi yaw -= math.pi / 2 if yaw < 0: yaw += 2 * math.pi yaw = 2 * math.pi - yaw direction_to_target = (self.target - bot.location).normalised() angle_to_target = math.atan2(direction_to_target.y, direction_to_target.x) angle = angle_to_target - yaw controller.pitch = -math.cos(angle) controller.roll = math.sin(angle) if self.on_second_jump: return None else: self.on_second_jump = True self.next_dodge_time = packet.game_info.seconds_elapsed + self.dodge_time else: controller.jump = False return controller
def setHalfFlip(self): #_time = self.time #if _time - self.flipTimer >= 1.9: controls = [] timers = [] control_1 = SimpleControllerState() control_1.throttle = -1 control_1.jump = True controls.append(control_1) timers.append(0.125) controls.append(SimpleControllerState()) timers.append(self.fakeDeltaTime * 4) control_3 = SimpleControllerState() control_3.throttle = -1 control_3.pitch = 1 control_3.jump = True controls.append(control_3) timers.append(self.fakeDeltaTime * 4) control_4 = SimpleControllerState() control_4.throttle = -1 control_4.pitch = -1 control_4.roll = -.1 #control_4.jump = True controls.append(control_4) timers.append(0.5) self.activeState = Divine_Mandate(self, controls, timers) self.flipTimer = self.time
def get_controller_state_from_actions( action: np.ndarray) -> SimpleControllerState: controls = action.clip(-1, 1) controller_state = SimpleControllerState() controller_state.pitch = controls[0] controller_state.yaw = controls[1] controller_state.roll = controls[2] controller_state.boost = bool(controls[3] >= 0) return controller_state
def update(self): controller_state = SimpleControllerState() if not self.firstJump: controller_state.throttle = -1 controller_state.jump = True controller_state.pitch = 1 self.firstJump = True self.jumpStart = time.time() return controller_state elif self.firstJump and not self.secondJump: jumpTimer = time.time() - self.jumpStart controller_state.throttle = -1 controller_state.pitch = 1 controller_state.jump = False if jumpTimer < 0.12: controller_state.jump = True if jumpTimer > 0.15: controller_state.jump = True self.jumpStart = time.time() self.secondJump = True return controller_state elif self.firstJump and self.secondJump: timer = time.time() - self.jumpStart if timer < 0.15: controller_state.throttle = -1 controller_state.pitch = 1 else: controller_state.pitch = -1 controller_state.throttle = 1 controller_state.roll = 1 if timer > .8: controller_state.roll = 0 if timer > 1.15: self.active = False return controller_state else: print( "halfFlip else conditional called in update. This should not be happening" )
def update(self): if self.agent.onSurface or self.agent.me.location[2] < 100: self.active = False controller_state = SimpleControllerState() # if self.agent.me.rotation[1] > 0: # controller_state.pitch = 1 # # elif self.agent.me.rotation[1] < 0: # controller_state.pitch = -1 if self.agent.me.rotation[2] > 0: controller_state.roll = -1 elif self.agent.me.rotation[2] < 0: controller_state.roll = 1 if self.agent.me.rotation[0] > self.agent.velAngle: controller_state.yaw = -1 elif self.agent.me.rotation[0] < self.agent.velAngle: controller_state.yaw = 1 # if self.agent.me.avelocity[0] > 2: # controller_state.yaw = -1 # elif self.agent.me.avelocity[0] < 2: # controller_state.yaw = 1 # # if self.agent.me.avelocity[1] > 2: # controller_state.pitch= 1 # elif self.agent.me.avelocity[1] < 2: # controller_state.pitch = -1 # # if self.agent.me.avelocity[2] > 2: # controller_state.roll = -1 # elif self.agent.me.avelocity[2] < 2: # controller_state.roll = 1 if self.active: controller_state.throttle = 1 else: controller_state.throttle = 0 return controller_state
def turtle_controller(agent, target_object, target_speed): location = return_local_location(target_object, agent.me) controller_state = SimpleControllerState() angle_to_ball = math.atan2(location.data[1], location.data[0]) current_speed = velocity_2d(agent.me) #turtle if abs(agent.bot_roll) < math.pi / 2.5: controller_state.jump = True if agent.bot_roll >= 0: controller_state.roll = 1 else: controller_state.roll = -1 # steering controller_state.steer = steer(angle_to_ball) # throttle if target_speed > current_speed: controller_state.throttle = 1.0 if target_speed > 1400 and agent.start > 2.2 and current_speed < 2250: controller_state.boost = True elif target_speed < current_speed: controller_state.throttle = 0 # dodging time_difference = time.time() - agent.start if time_difference > 2.2 and distance_2d(target_object, agent.me) > ( velocity_2d(agent.me) * 2.5) and abs(angle_to_ball) < 1.3: agent.start = time.time() elif time_difference <= 0.1: controller_state.jump = True controller_state.pitch = -1 elif time_difference >= 0.1 and time_difference <= 0.15: controller_state.jump = False controller_state.pitch = -1 elif time_difference > 0.15 and time_difference < 1: controller_state.jump = True controller_state.yaw = controller_state.steer controller_state.pitch = -1 return controller_state
def getSensible_thingToCONTROL(magicWariable): ThisISTHE_controller = SimpleControllerState() ThisISTHE_controller.throttle = magicWariable[magicWariable[0][0]] ThisISTHE_controller.steer = magicWariable[magicWariable[0][1]] ThisISTHE_controller.pitch = magicWariable[magicWariable[0][2]] ThisISTHE_controller.yaw = magicWariable[magicWariable[0][3]] ThisISTHE_controller.roll = magicWariable[magicWariable[0][4]] ThisISTHE_controller.jump = magicWariable[magicWariable[0][5]] ThisISTHE_controller.boost = magicWariable[magicWariable[0][6]] ThisISTHE_controller.handbrake = magicWariable[magicWariable[0][7]] return ThisISTHE_controller
def recoveryController(agent, local): #accepts agent, the agent's controller, and a target (in local coordinates) controller_state = SimpleControllerState() turn = math.atan2(local.data[1],local.data[0]) up = toLocal(agent.me.location + Vector3([0,0,100]), agent.me) target = [math.atan2(up.data[1],up.data[2]), math.atan2(local.data[2],local.data[0]), turn] #determining angles each axis must turn controller_state.steer = steerPD(turn, 0) controller_state.yaw = steerPD(target[2],-agent.me.rvelocity.data[2]/5) controller_state.pitch = steerPD(target[1],agent.me.rvelocity.data[1]/5) controller_state.roll = steerPD(target[0],agent.me.rvelocity.data[0]/5) return controller_state
def to_simple_controller_state(self): controls = SimpleControllerState() controls.throttle = self.throttle controls.steer = self.steer controls.yaw = self.yaw controls.pitch = self.pitch controls.roll = self.roll controls.boost = self.boost controls.jump = self.jump controls.handbrake = self.handbrake return controls
def deltaC(agent, target): c = SimpleControllerState() target = target # - agent.me.velocity target_local = toLocal(agent.me.location + target, agent.me) angle_to_target = math.atan2(target_local.data[1], target_local.data[0]) pitch_to_target = math.atan2(target_local.data[2], target_local.data[0]) if agent.me.grounded: if agent.jt + 1.5 > time.time(): c.jump = True else: c.jump = False agent.jt = time.time() else: c.yaw = steerPD(angle_to_target, -agent.me.rvelocity.data[1] / 4) c.pitch = steerPD(pitch_to_target, agent.me.rvelocity.data[0] / 4) if target.magnitude() > 10: c.boost = True if abs(pitch_to_target) + abs(angle_to_target) > 0.9: c.boost = False else: top = toLocal([ agent.me.location.data[0], agent.me.location.data[1], agent.me.location.data[2] + 1000 ], agent.me) roll_to_target = math.atan2(top.data[1], top.data[2]) c.roll = steerPD(roll_to_target, agent.me.rvelocity.data[2] * 0.5) tsj = time.time() - agent.jt if tsj < 0.215: c.jump = True elif tsj < 0.25: c.jump = False elif tsj >= 0.25 and tsj < 0.27 and target.data[2] > 560: c.jump = True c.boost = False c.yaw = 0 c.pitch = 0 c.roll = 0 else: c.jump = False return c
def finalize_output(output): framework_output = SimpleControllerState() framework_output.throttle = output.throttle framework_output.steer = output.steer framework_output.yaw = output.yaw framework_output.pitch = output.pitch framework_output.roll = output.roll framework_output.boost = output.boost framework_output.handbrake = output.handbrake framework_output.jump = output.jump return framework_output
def get(self, data): c = SimpleControllerState() c.throttle = max(min(data[0], 1), -1) c.steer = max(min(data[1], 1), -1) c.pitch = max(min(data[2], 1), -1) c.yaw = max(min(data[3], 1), -1) c.roll = max(min(data[4], 1), -1) c.jump = data[5] c.boost = data[6] c.handbrake = data[7] c.use_item = data[8] return c
def copy_controls(obj_to: SimpleControllerState, obj_from: SimpleControllerState): """Copies the attribute of one SimpleControlerStates to another""" obj_to.steer = obj_from.steer obj_to.throttle = obj_from.throttle obj_to.pitch = obj_from.pitch obj_to.yaw = obj_from.yaw obj_to.roll = obj_from.roll obj_to.jump = obj_from.jump obj_to.boost = obj_from.boost obj_to.handbrake = obj_from.handbrake obj_to.use_item = obj_from.use_item
def array_to_scs(a): scs = SimpleControllerState() scs.throttle = a[0] scs.steer = a[1] scs.pitch = a[2] scs.yaw = a[3] scs.roll = a[4] scs.jump = a[5] scs.boost = a[6] scs.handbrake = a[7] return scs
def get_output(self): output = SimpleControllerState() output.throttle = self.throttle output.steer = self.steer output.pitch = self.pitch output.yaw = self.yaw output.roll = self.roll output.jump = self.jump output.boost = self.boost output.handbrake = self.handbrake return output
def nn_to_rlbot_controls(nn_controls): controller_state = SimpleControllerState() # steering if nn_controls[0] == 1: controller_state.steer = -1.0 elif nn_controls[1] == 1: controller_state.steer = 1.0 else: controller_state.steer = 0.0 # pitch if nn_controls[2] == 1: controller_state.pitch = -1.0 elif nn_controls[3] == 1: controller_state.pitch = 1.0 else: controller_state.pitch = 0.0 # throttle if nn_controls[4] == 1: controller_state.throttle = 1.0 elif nn_controls[5] == 1: controller_state.throttle = -1.0 else: controller_state.throttle = 0.0 controller_state.jump = (nn_controls[6] == 1) controller_state.boost = (nn_controls[7] == 1) controller_state.handbrake = (nn_controls[8] == 1) if controller_state.handbrake: controller_state.roll = controller_state.steer controller_state.yaw = 0.0 else: controller_state.roll = 0.0 controller_state.yaw = controller_state.steer return controller_state
def act(self, action): controller_state = SimpleControllerState() controller_state.throttle = action[0] # controller_state.steer = action[1] # controller_state.pitch = action[2] # controller_state.yaw = action[3] # controller_state.roll = action[4] # controller_state.jump = True if action[5] > 0.5 else False # controller_state.boost = True if action[6] > 0.5 else False # controller_state.handbrake = True if action[7] > 0.5 else False controller_state.steer = 0 controller_state.pitch = 0 controller_state.yaw = 0 controller_state.roll = 0 controller_state.jump = 0 controller_state.boost = 0 controller_state.handbrake = 0 self.manager.agent_action_queue.put(controller_state)
def exec(self, bot): controls = SimpleControllerState() dt = bot.info.dt car = bot.info.my_car relative_rotation = dot(transpose(car.rot), self.target) geodesic_local = rotation_to_axis(relative_rotation) # figure out the axis of minimal rotation to target geodesic_world = dot(car.rot, geodesic_local) # get the angular acceleration alpha = Vec3( self.controller(geodesic_world.x, car.ang_vel.x, dt), self.controller(geodesic_world.y, car.ang_vel.y, dt), self.controller(geodesic_world.z, car.ang_vel.z, dt) ) # reduce the corrections for when the solution is nearly converged alpha.x = self.q(abs(geodesic_world.x) + abs(car.ang_vel.x)) * alpha.x alpha.y = self.q(abs(geodesic_world.y) + abs(car.ang_vel.y)) * alpha.y alpha.z = self.q(abs(geodesic_world.z) + abs(car.ang_vel.z)) * alpha.z # set the desired next angular velocity ang_vel_next = car.ang_vel + alpha * dt # determine the controls that produce that angular velocity roll_pitch_yaw = AerialTurnManeuver.aerial_rpy(car.ang_vel, ang_vel_next, car.rot, dt) controls.roll = roll_pitch_yaw.x controls.pitch = roll_pitch_yaw.y controls.yaw = roll_pitch_yaw.z self._timer += dt if ((norm(car.ang_vel) < self.epsilon_ang_vel and norm(geodesic_world) < self.epsilon_rotation) or self._timer >= self.timeout or car.on_ground): self.done = True controls.throttle = 1.0 return controls
def exec(self, bot) -> SimpleControllerState: man_ct = bot.info.time - self.maneuver_start_time controls = SimpleControllerState() car = bot.info.my_car vel_f = proj_onto_size(car.vel, car.forward) # Reverse a bit if vel_f > -50 and man_ct < 0.3: controls.throttle = -1 self.halfflip_start_time = bot.info.time return controls ct = bot.info.time - self.halfflip_start_time # States of jump if ct >= self._t_finishing: self._almost_finished = True controls.throttle = 1 controls.boost = self.boost if car.on_ground: self.done = True else: bot.maneuver = RecoveryManeuver() self.done = True elif ct >= self._t_roll_begin: controls.pitch = -1 controls.roll = 1 elif ct >= self._t_second_jump_end: controls.pitch = -1 elif ct >= self._t_second_jump_begin: controls.jump = 1 controls.pitch = 1 elif ct >= self._t_first_jump_end: controls.pitch = 1 else: controls.jump = 1 controls.throttle = -1 controls.pitch = 1 return controls
def sevenC(agent, target, speed): c = SimpleControllerState() #target.data[2] = target.data[2]-90#futureZ(target.data[2],agent.ball.velocity.data[2], 0.2) angle = angle2(target, agent.me.location) x_speed = (math.cos(angle) * speed * 1.1) - (agent.me.velocity.data[0]) y_speed = (math.sin(angle) * speed * 1.1) - (agent.me.velocity.data[1]) #print(x_speed,y_speed) correction_vector = Vector3([x_speed / 2300, y_speed / 2300, speed / 2300]) target_local = toLocal(agent.me.location + correction_vector, agent.me) angle_to_target = math.atan2(target_local.data[1], target_local.data[0]) pitch_to_target = math.atan2(target_local.data[2], target_local.data[0]) if agent.me.grounded: if agent.jt + 1.5 > time.time(): c.jump = True else: c.jump = False agent.jt = time.time() else: if agent.jt > time.time() - 1.5: c.jump = True else: c.jump = False c.throttle, c.boost = altPD(target.data[2] - agent.me.location.data[2], agent.me.velocity.data[2]) c.yaw = steerPD(angle_to_target, -agent.me.rvelocity.data[1] / 5) c.pitch = steerPD(pitch_to_target, agent.me.rvelocity.data[0] / 4) if abs(pitch_to_target) + abs(angle_to_target) < 0.5: two = target loctwo = toLocal(two, agent.me) to_two = math.atan2(loctwo.data[1], loctwo.data[2]) c.roll = steerPD(to_two, agent.me.rvelocity.data[2] / 3) if abs(pitch_to_target) + abs(angle_to_target) > 0.45: c.boost = False return c
def sixC(agent, target, speed): controller_state = SimpleControllerState() target_relative = target.location - agent.me.location target_vel_relative = target.velocity - agent.me.velocity target_local = toLocal(target, agent.me) agent_local = toLocal(agent.me.velocity, agent.me) angle_to_target = math.atan2(target_local.data[1], target_local.data[0]) now = time.time() if agent.me.grounded: agent_speed = velocity2D(agent.me) controller_state.steer = steer(angle_to_target) controller_state.throttle, controller_state.boost = throttle( speed, agent_speed) if target_local.data[ 2] > target_local.data[0] * 3 and now > agent.jt + 1: controller_state.jump = True agent.jt = now return controller_state else: agent_speed = velocity2D(agent.me) pitch_to_target = math.atan2(target_local.data[2], target_local.data[0]) controller_state.jump = False controller_state.yaw = steerPD(angle_to_target, -agent.me.rvelocity.data[1] / 5) controller_state.pitch = steerPD(pitch_to_target, agent.me.rvelocity.data[0] / 4) controller_state.throttle, controller_state.boost = altPD( target_relative.data[2], agent.me.velocity.data[2]) if abs(pitch_to_target) > 0.4: controller_state.boost = False if abs(pitch_to_target) + abs(angle_to_target) < 0.5: two = agent.ball.location loctwo = toLocal(two, agent.me) to_two = math.atan2(loctwo.data[1], loctwo.data[2]) controller_state.roll = steerPD(to_two, agent.me.rvelocity.data[2] / 4) return controller_state
def step(self, dt) -> SimpleControllerState: ct = self.bot.info.time - self._start_time controls = SimpleControllerState() controls.throttle = 1 car = self.bot.info.my_car corner_debug = "ct: {}\n".format(ct) corner_debug += "_start_time: {}\n".format(self._start_time) corner_debug += "_t_finishing: {}\n".format(type(self._t_finishing)) self.bot.renderer.draw_string_2d(10, 200, 1, 1, corner_debug, self.bot.renderer.white()) if ct >= self._t_finishing: self._almost_finished = True if car.on_ground: self.finished = True # else: # self.bot.maneuver = RecoveryManeuver(self.bot) # self.done = True return controls elif ct >= self._t_second_unjump: # Stop pressing jump and rotate and wait for flip is done pass elif ct >= 0.08: if ct >= self._t_second_jump: controls.jump = 1 controls.roll = 0 controls.pitch = -1 controls.yaw = 0 # Stop pressing jump elif ct >= self._t_first_unjump: pass # First jump else: controls.jump = 1 return controls
def tick(self, packet: GameTickPacket) -> StepResult: if self.game_info is None: self.game_info = GameInfo(self.index, packet.game_cars[self.index].team) self.game_info.read_packet(packet) if self.aerial is None: self.aerial = Aerial( self.game_info.my_car, vec3(self.target.x, self.target.y, self.target.z), self.arrival_time) self.aerial.step(SECONDS_PER_TICK) controls = SimpleControllerState() controls.boost = self.aerial.controls.boost controls.pitch = self.aerial.controls.pitch controls.yaw = self.aerial.controls.yaw controls.roll = self.aerial.controls.roll controls.jump = self.aerial.controls.jump return StepResult(controls, packet.game_info.seconds_elapsed > self.arrival_time)
def pathController(agent, path): agent.renderer.begin_rendering() agent.renderer.draw_line_3d( path.lines[0].start, path.lines[0].end, agent.renderer.create_color(255, 255, 255, 255)) agent.renderer.draw_line_3d( path.lines[1].start, path.lines[1].end, agent.renderer.create_color(255, 255, 255, 255)) agent.renderer.end_rendering() c = SimpleControllerState() #this massive chain of if/elif handles all events that could have been triggered along the path if isinstance(path.Event, Event): event = path.Event if event.type == "drift": local = toLocal(path.lines[-1].end, agent.me) angle = math.atan2(local[1], local[0]) c.yaw = steerPD(angle, -agent.me.rvelocity.data[1] / 5) c.throttle = 1.0 c.handbrake = True if abs(angle) < 0.1: c.handbrake = False path.Event.done = True path.Event = None return c elif event.type == "dodge": elapsed = agent.current_time - agent.jump_time local = toLocal(event.target, agent.me).normalize() if elapsed > 2.2 and agent.me.grounded: agent.jump_time = agent.current_time c.jump = True elif elapsed <= 0.1: c.jump = True c.pitch = -local[1] c.yaw = -local[0] elif elapsed > 0.1 and elapsed <= 0.15: c.jump = False c.pitch = -local[1] c.yaw = -local[0] elif elapsed > 0.15 and elapsed <= 1: c.jump = True c.pitch = -local[1] c.yaw = -local[0] else: c.jump = False path.Event.done = True path.Event = None return c elif event.type == "aerial": elapsed = agent.current_time - agent.jump_time if elapsed > 2.2 and agent.me.grounded: agent.jump_time = agent.current_time c.jump = True elif elapsed < 0.25: c.jump = True time_left = event.time - agent.current_time target = backsolveFuture(agent.me.location, agent.me.velocity, path.lines[-1].end, time_left) if target.magnitude() < 100: local = tolocal(path.lines[-1].end, agent.me) else: local = toLocal(agent.me.location + target, agent.me) c.boost = True yaw_to = math.atan2(local[1], local[0]) pitch_to = math.atan2(local[2], local[0]) c.yaw = steerPD(yaw_to, -agent.me.rvelocity[1] / 4) c.pitch = steerPD(pitch_to, agent.me.rvelocity[0] / 4) if abs(pitch_to) + abs(angle_to) > 0.9: c.boost = False else: top = toLocal(agent.me.location + Vector3(0, 0, 500)) roll_to = math.atan2(top[1], top[2]) c.roll = steerPD(roll_to, agent.me.rvelocity[2] * 0.5) if time_left <= 0.1: event.done = True path.Event = None return c turnRate = 1.5 v = velocity1D(agent.me)[1] r = turn_radius(v) if path.line == 0: line = path.lines[0] nextLine = path.lines[1] firstProjection = line.project_distance(agent.me.location) secProjection = nextLine.project_distance(agent.me.location) if (agent.me.location - (line.start + line.direction * firstProjection)).magnitude() > ( agent.me.location - (nextLine.start + nextLine.direction * secProjection)).magnitude(): path.line = 1 td = firstProjection + r for event in line.events: if event.type == "drift" and event.done == False: local = toLocal(nextLine.end, agent.me) angle = abs(math.atan2(local[1], local[0])) if angle / turnRate > (line.length - firstProjection) / (v + 1): path.Event = event if td > line.length: target = nextLine.start + (nextLine.direction * (td - line.length)) else: target = line.start + (line.direction * td) speed = ((line.length - firstProjection) + nextLine.length) / cap( path.game_time - agent.current_time, 0.01, 10) else: line = path.lines[-1] projection = line.project_distance(agent.me.location) td = projection + r for event in line.events: if event.type == "aerial" and event.done == False: time_2D = (line.end.flatten() - agent.me.location.flatten()).magnitude() / (v + 1) time_3D = (line.end[2] - 100) / 1000 if time_2D - 1 < time_3D: path.Event = event elif event.type == "dodge" and event.done == False: time_2D = (line.end.flatten() - agent.me.location.flatten()).magnitude() / (v + 1) if time_2D <= 1.1: path.Event = event target = line.start + (line.direction * td) speed = (line.length - projection) / cap( path.game_time - agent.current_time, 0.01, 10) local = toLocal(target, agent.me) angle = math.atan2(local.data[1], local.data[0]) c.steer = steer(angle) c.throttle, c.boost = throttle(speed, v) return c
def get_output(self, packet: GameTickPacket) -> SimpleControllerState: ############################################################################################### #Startup and frame info ############################################################################################### #Initialization info if self.is_init: self.is_init = False self.field_info = self.get_field_info() #Find teammates and opponents self.teammate_indices = [] self.opponent_indices = [] for i in range(packet.num_cars): if i == self.index: pass elif packet.game_cars[i].team == self.team: self.teammate_indices.append(i) else: self.opponent_indices.append(i) self.utils_game = utils.simulation.Game(self.index, self.team) utils.simulation.Game.set_mode("soccar") if TESTING or DEBUGGING: EvilGlobals.renderer = self.renderer self.prediction = PredictionPath( ball_prediction=self.get_ball_prediction_struct(), source="Framework", team=self.team) #Game state info self.game_info = GameState(packet=packet, rigid_body_tick=self.get_rigid_body_tick(), utils_game=self.utils_game, field_info=self.field_info, my_index=self.index, my_team=self.team, ball_prediction=self.prediction, teammate_indices=self.teammate_indices, opponent_indices=self.opponent_indices, my_old_inputs=self.old_inputs) ############################################################################################### #Planning ############################################################################################### #For now everything is a 1v1. I'll fix team code again in the future. #if self.game_info.team_mode == "1v1": self.plan, self.persistent = OnesPlanning.make_plan( self.game_info, self.plan.old_plan, self.plan.path, self.persistent) ''' else: self.plan, self.persistent = TeamPlanning.make_plan(self.game_info, self.plan.old_plan, self.plan.path, self.persistent) ''' #Check if it's a kickoff. If so, we'll run kickoff code later on. self.kickoff_position = update_kickoff_position( self.game_info, self.kickoff_position) #If we're in the first frame of an RLU mechanic, start up the object. #If we're finished with it, reset it to None ### if self.persistent.aerial_turn.initialize: self.persistent.aerial_turn.initialize = False self.persistent.aerial_turn.action = AerialTurn( self.game_info.utils_game.my_car) self.persistent.aerial_turn.action.target = rot_to_mat3( self.persistent.aerial_turn.target_orientation) elif not self.persistent.aerial_turn.check: self.persistent.aerial_turn.action = None self.persistent.aerial_turn.check = False ### if self.persistent.aerial.initialize: self.persistent.aerial.initialize = False self.persistent.aerial.action = Aerial( self.game_info.utils_game.my_car) self.persistent.aerial.action.target = Vec3_to_vec3( self.persistent.aerial.target_location, self.game_info.team_sign) self.persistent.aerial.action.arrival_time = self.persistent.aerial.target_time self.persistent.aerial.action.up = Vec3_to_vec3( self.persistent.aerial.target_up, self.game_info.team_sign) elif not self.persistent.aerial.check: self.persistent.aerial.action = None self.persistent.aerial.check = False ### ############################################################################################### #Testing ############################################################################################### if TESTING: #Copy-paste from a testing file here controller_input = SimpleControllerState() return controller_input ############################################################################################### #Run either Kickoff or Cowculate ############################################################################################### if self.plan.layers[0] == "Kickoff": if self.old_kickoff_data != None: self.kickoff_data = Kickoff(self.game_info, self.kickoff_position, self.old_kickoff_data.memory, self.persistent) else: self.kickoff_data = Kickoff(self.game_info, self.kickoff_position, None, self.persistent) output, self.persistent = self.kickoff_data.input() else: output, self.persistent = Cowculate(self.plan, self.game_info, self.prediction, self.persistent) ############################################################################################### #Update previous frame variables. ############################################################################################### self.old_kickoff_data = self.kickoff_data self.old_inputs = output #Make sure we don't get stuck turtling. Not sure how effective this is. if output.throttle == 0: output.throttle = 0.01 #Making sure that RLU output is interpreted properly as an input for RLBot framework_output = SimpleControllerState() framework_output.throttle = output.throttle framework_output.steer = output.steer framework_output.yaw = output.yaw framework_output.pitch = output.pitch framework_output.roll = output.roll framework_output.boost = output.boost framework_output.handbrake = output.handbrake framework_output.jump = output.jump return framework_output
def exec(self, bot): if not self.announced_in_quick_chat: self.announced_in_quick_chat = True bot.send_quick_chat(QuickChats.CHAT_EVERYONE, QuickChats.Information_IGotIt) ct = bot.info.time car = bot.info.my_car up = car.up controls = SimpleControllerState() # Time remaining till intercept time T = self.intercept_time - bot.info.time # Expected future position xf = car.pos + car.vel * T + 0.5 * GRAVITY * T ** 2 # Expected future velocity vf = car.vel + GRAVITY * T # Is set to false while jumping to avoid FeelsBackFlipMan rotate = True if self.jumping: if self.jump_begin_time == -1: jump_elapsed = 0 self.jump_begin_time = ct else: jump_elapsed = ct - self.jump_begin_time # How much longer we can press jump and still gain upward force tau = JUMP_MAX_DUR - jump_elapsed # Add jump pulse if jump_elapsed == 0: vf += up * JUMP_SPEED xf += up * JUMP_SPEED * T rotate = False # Acceleration from holding jump vf += up * JUMP_SPEED * tau xf += up * JUMP_SPEED * tau * (T - 0.5 * tau) if self.do_second_jump: # Impulse from the second jump vf += up * JUMP_SPEED xf += up * JUMP_SPEED * (T - tau) if jump_elapsed < JUMP_MAX_DUR: controls.jump = True else: controls.jump = False if self.do_second_jump: if self.jump_pause_counter < 4: # Do a 4-tick pause between jumps self.jump_pause_counter += 1 else: # Time to start second jump # we do this by resetting our jump counter and pretend and our aerial started in the air self.jump_begin_time = -1 self.jumping = True self.do_second_jump = False else: # We are done jumping self.jumping = False else: controls.jump = False delta_pos = self.hit_pos - xf direction = normalize(delta_pos) car_to_hit_pos = self.hit_pos - car.pos dodging = self.dodge_begin_time != -1 if dodging: controls.jump = True # We are not pressing jump, so let's align the car if rotate and not dodging: if self.do_dodge and norm(car_to_hit_pos) < Ball.RADIUS + 80: # Start dodge self.dodge_begin_time = ct hit_local = dot(car_to_hit_pos, car.rot) hit_local.z = 0 dodge_direction = normalize(hit_local) controls.roll = 0 controls.pitch = -dodge_direction.x controls.yaw = sign(car.rot.get(2, 2)) * direction.y controls.jump = True else: # Adjust orientation if norm(delta_pos) > 50: pd = bot.fly.align(bot, looking_in_dir(delta_pos)) else: if self.target_rot is not None: pd = bot.fly.align(bot, self.target_rot) else: pd = bot.fly.align(bot, looking_in_dir(self.hit_pos - car.pos)) controls.roll = pd.roll controls.pitch = pd.pitch controls.yaw = pd.yaw if not dodging and angle_between(car.forward, direction) < 0.3: if norm(delta_pos) > 40: controls.boost = 1 controls.throttle = 0 else: controls.boost = 0 controls.throttle = clip01(0.5 * THROTTLE_AIR_ACCEL * T ** 2) else: controls.boost = 0 controls.throttle = 0 prediction = predict.ball_predict(bot, T) self.done = T < 0 if norm(self.hit_pos - prediction.pos) > 50: # Jump shot failed self.done = True bot.send_quick_chat(QuickChats.CHAT_EVERYONE, QuickChats.Apologies_Cursing) if bot.do_rendering: car_to_hit_dir = normalize(self.hit_pos - car.pos) color = bot.renderer.pink() rendering.draw_cross(bot, self.hit_pos, color, arm_length=100) rendering.draw_circle(bot, lerp(car.pos, self.hit_pos, 0.25), car_to_hit_dir, 40, 12, color) rendering.draw_circle(bot, lerp(car.pos, self.hit_pos, 0.5), car_to_hit_dir, 40, 12, color) rendering.draw_circle(bot, lerp(car.pos, self.hit_pos, 0.75), car_to_hit_dir, 40, 12, color) bot.renderer.draw_line_3d(car.pos, self.hit_pos, color) return controls