Example #1
0
 def __init__(self, index: int, team: int):
     super().__init__()
     self.team = team
     self.id = index
     self.aerial_turn = AerialTurn(self)
     self.aerial = Aerial(self)
     self.hover = Hover(self)
Example #2
0
def defending(agent):
    """"Method that gives output for the defending strategy"""
    target = defending_target(agent)
    agent.drive.target = target
    distance = distance_2d(agent.info.my_car.position, target)
    vf = velocity_forward(agent.info.my_car)
    dodge_overshoot = distance < (abs(vf) + 500) * 1.5
    agent.drive.speed = get_speed(agent, target)
    agent.drive.step(agent.info.time_delta)
    agent.controls = agent.drive.controls
    t = time.time()
    can_dodge, simulated_duration, simulated_target = agent.simulate()
    # print(time.time() - t)
    if can_dodge:
        agent.dodge = Dodge(agent.info.my_car)
        agent.turn = AerialTurn(agent.info.my_car)
        agent.dodge.duration = simulated_duration - 0.1
        agent.dodge.target = simulated_target

        agent.dodge.preorientation = look_at(simulated_target, vec3(0, 0, 1))
        agent.timer = 0
        agent.step = Step.Dodge
    if not agent.should_defend():
        agent.step = Step.Shooting
    elif vf < -900 and (not dodge_overshoot or distance < 600):
        agent.step = Step.HalfFlip
        agent.halfflip = HalfFlip(agent.info.my_car)
    elif not dodge_overshoot and agent.info.my_car.position[2] < 80 and \
            (agent.drive.speed > abs(vf) + 300 and 1200 < abs(vf) < 2000 and agent.info.my_car.boost <= 25):
        # Dodge towards the target for speed
        agent.step = Step.Dodge
        agent.dodge = Dodge(agent.info.my_car)
        agent.dodge.duration = 0.1
        agent.dodge.target = target
Example #3
0
    def __init__(self, car: Car, info: Game):
        self.turn = AerialTurn(car)

        self.target = None
        self.car: Car = car
        self.info: Game = info
        self.controls = Input()
        self.jump = False
Example #4
0
    def __init__(self, car: Car):
        super().__init__(car)

        self.landing = False
        self.aerial_turn = AerialTurn(self.car)

        self.trajectory: List[vec3] = []
        self.landing_pos: Optional[vec3] = None
Example #5
0
    def __init__(self, car: Car, duration: float, target: vec3):
        super().__init__(car)

        self.dodge = AirDodge(car, duration, target)
        self.turn = AerialTurn(car)
        self.turn.target = look_at(direction(car, target), vec3(0, 0, 1))
        self.jump = self.dodge.jump
        self.target = target
Example #6
0
 def __init__(self, car):
     self.timer = 0
     self.car = car
     self.direction = vec3(car.forward() * -1)
     self.target = self.direction * 1000 + self.car.location
     self.aerial_turn = AerialTurn(car)
     self.aerial_turn.target = look_at(self.direction, vec3(0, 0, 1))
     self.controls = SimpleControllerState()
     self.finished = False
Example #7
0
def setup_first_dodge(agent, duration, delay, target, preorientation):
    agent.dodge = Dodge(agent.info.my_car)
    agent.turn = AerialTurn(agent.info.my_car)
    agent.dodge.duration = duration
    agent.dodge.delay = delay
    agent.dodge.target = target
    agent.dodge.preorientation = preorientation
    agent.timer = 0.0
    agent.step = Step.Dodge_1
Example #8
0
    def __init__(self, car: Car, jump_when_upside_down=True):
        super().__init__(car)

        self.jump_when_upside_down = jump_when_upside_down
        self.landing = False
        self.aerial_turn = AerialTurn(self.car)

        self.trajectory: List[vec3] = []
        self.landing_pos: Optional[vec3] = None
Example #9
0
    def __init__(self, car: Car, info: GameInfo, target=None):
        self.drive = Drive(car)
        self.aerial_turn = AerialTurn(car)

        self.jumping = False
        self.time_for_jump = float("inf")
        self.timer = 0.0

        super().__init__(car, info, target)
Example #10
0
    def rotate(self, car: Car, target: vec3, dt) -> SimpleControllerState:
        # up = vec3(*[self.car.theta[i, 2] for i in range(3)])

        target_rotation = look_at(self.target, vec3(0, 0, 1))
        action = AerialTurn(car)
        action.target = target_rotation

        action.step(dt)
        controls = action.controls

        return controls
Example #11
0
 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
Example #12
0
    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
Example #13
0
    def step(self, dt):
        car = self.car

        if self.phase == 1:
            if norm(car.velocity) > 1300:
                self.phase = 2
                self.action = AirDodge(car, 0.05, car.position + car.velocity)

        if self.phase == 2:
            if car.on_ground and self.action.finished:
                self.action = self.drive
                self.phase = 3

        if self.phase == 3:
            if distance(car, self.info.ball) < norm(car.velocity) * 0.4:

                # detect if an opponent is going for kickoff
                is_opponent_going_for_kickoff = False
                for opponent in self.info.opponents:
                    if distance(self.info.ball, opponent) < 1500:
                        is_opponent_going_for_kickoff = True

                if is_opponent_going_for_kickoff:
                    self.phase = 4
                    self.action = AirDodge(car, 0.05, self.info.ball.position)
                else:
                    self.phase = "anti-fake-kickoff"
                    self.action = self.drive

        if self.phase == 4:
            if self.action.finished:
                self.action = AerialTurn(car)
                self.phase = 5

        if self.phase == 5:
            self.action.target = look_at(self.info.my_goal.center,
                                         vec3(0, 0, 1))
            self.action.controls.throttle = 1
            if car.on_ground:
                self.finished = True
                # self.phase = 6
                # self.action = DodgeShot(car, self.info, self.info.their_goal.center)

        if self.phase == 6:
            self.finished = self.action.finished

        if self.phase == "anti-fake-kickoff":
            self.drive.target_pos = vec3(80, 0, 0)
            self.finished = self.info.ball.position[1] != 0

        self.action.step(dt)
        self.controls = self.action.controls
Example #14
0
    def setup(self):
        self.aerial = Aerial(self.agent.game.my_car)
        self.turn = AerialTurn(self.agent.game.my_car)
        myGoal = center = Vector([0, 5200 * sign(self.agent.team), 200])
        enemyGoal = center = Vector([0, 5200 * -sign(self.agent.team), 200])
        if self.agent.me.boostLevel > 0:
            for i in range(0, self.agent.ballPred.num_slices):
                targetVec = Vector([self.agent.ballPred.slices[i].physics.location.x,
                                    self.agent.ballPred.slices[i].physics.location.y,
                                    self.agent.ballPred.slices[i].physics.location.z])
                # interferenceTimer += self.agent.deltaTime
                # if interferenceTimer < 1:
                #     if findEnemyClosestToLocation(self.agent,targetVec)[1] < 150:
                #         break
                if self.agent.ballPred.slices[i].physics.location.z >= 300:
                    goalDist = distance2D(center, targetVec)
                    #if distance2D(myGoal, targetVec) > distance2D(myGoal, self.agent.me.location):
                    if self.agent.me.location[1] * sign(self.agent.team) > self.agent.ballPred.slices[i].physics.location.y * sign(self.agent.team):
                        zOffset = 0
                        if goalDist < 1500:
                            if targetVec[2] > 600:
                                zOffset = 70
                        shotAngle = correctAngle(math.degrees(angle2(targetVec, enemyGoal)) + 90 * -sign(self.agent.team))


                        if abs(shotAngle) <=80:
                            xOffset = clamp(80,-80,(shotAngle*2)*-sign(self.agent.team))
                            self.aerial.target = vec3(self.agent.ballPred.slices[i].physics.location.x+xOffset,
                                                      self.agent.ballPred.slices[i].physics.location.y,
                                                      self.agent.ballPred.slices[i].physics.location.z+zOffset)

                            self.aerial.arrival_time = self.agent.ballPred.slices[i].game_seconds

                            simulation = self.aerial.simulate()

                            # # check if we can reach it by an aerial
                            if norm(simulation.location - self.aerial.target) < 100:
                                self.target_ball = self.agent.ballPred.slices[i]
                                self.xOffset = xOffset
                                self.zOffset = zOffset
                                self.active = True
                                break
Example #15
0
    def get_controls(self):
        """Decides what strategy to uses and gives corresponding output"""
        if self.step == Step.Steer or self.step == Step.Dodge_2 or self.step == Step.Dodge_1 or self.step == Step.Drive:
            print("GETCONTROLS")
            # self.time = 0
            # self.set_state = True
            self.step = Step.Shooting
        if self.step == Step.Shooting:
            target = get_intersect(self, self.info.my_car)
            self.drive.target = target
            self.drive.step(self.info.time_delta)
            self.controls = self.drive.controls
            t = time.time()
            can_dodge, simulated_duration, simulated_target = self.simulate(
                self.their_goal.center)
            # print(time.time() - t)
            if can_dodge:
                self.dodge = Dodge(self.info.my_car)
                self.turn = AerialTurn(self.info.my_car)
                self.dodge.duration = simulated_duration - 0.1
                self.dodge.direction = vec2(self.their_goal.center -
                                            simulated_target)

                target = vec3(vec2(self.their_goal.center)) + vec3(
                    0, 0, jeroens_magic_number * simulated_target[2])
                self.dodge.preorientation = look_at(target - simulated_target,
                                                    vec3(0, 0, 1))
                self.step = Step.Dodge
            if self.should_defend():
                self.step = Step.Defending
            elif not self.closest_to_ball or self.in_front_off_ball:
                self.step = Step.Rotating
            elif should_halfflip(self, target):
                self.step = Step.HalfFlip
                self.halfflip = HalfFlip(self.info.my_car)
            elif should_dodge(self, target):
                self.step = Step.Dodge
                self.dodge = Dodge(self.info.my_car)
                self.dodge.target = target
                self.dodge.duration = 0.1
        elif self.step == Step.Rotating:
            target = 0.5 * (self.info.ball.position -
                            self.my_goal.center) + self.my_goal.center
            self.drive.target = target
            self.drive.speed = 1410
            self.drive.step(self.info.time_delta)
            self.controls = self.drive.controls
            in_position = not not_back(self.info.my_car.position,
                                       self.info.ball.position,
                                       self.my_goal.center)
            faster = self.closest_to_ball and in_position
            if len(self.teammates) == 0 and in_position:
                self.step = Step.Shooting
            elif len(self.teammates) == 1:
                teammate = self.info.cars[self.teammates[0]].position
                teammate_out_location = not_back(teammate,
                                                 self.info.ball.position,
                                                 self.my_goal.center)
                if teammate_out_location or faster:
                    self.step = Step.Shooting
            elif len(self.teammates) == 2:
                teammate1 = self.info.cars[self.teammates[0]].position
                teammate2 = self.info.cars[self.teammates[0]].position
                teammate1_out_location = not_back(teammate1,
                                                  self.info.ball.position,
                                                  self.my_goal.center)
                teammate2_out_location = not_back(teammate2,
                                                  self.info.ball.position,
                                                  self.my_goal.center)
                if teammate1_out_location and teammate2_out_location:
                    self.step = Step.Shooting
                elif (teammate1_out_location
                      or teammate2_out_location) and faster or faster:
                    self.step = Step.Shooting
            if self.should_defend():
                self.step = Step.Defending
            elif should_halfflip(self, target):
                self.step = Step.HalfFlip
                self.halfflip = HalfFlip(self.info.my_car)
            elif should_dodge(self, target):
                self.step = Step.Dodge
                self.dodge = Dodge(self.info.my_car)
                self.dodge.target = target
                self.dodge.duration = 0.1

        elif self.step == Step.Defending:
            defending(self)
        elif self.step == Step.Dodge or self.step == Step.HalfFlip:
            halfflipping = self.step == Step.HalfFlip
            if halfflipping:
                self.halfflip.step(self.info.time_delta)
            else:
                self.dodge.step(self.info.time_delta)
            if (self.halfflip.finished if halfflipping else
                    self.dodge.finished) and self.info.my_car.on_ground:
                self.step = Step.Shooting
            else:
                self.controls = (self.halfflip.controls
                                 if halfflipping else self.dodge.controls)
                if not halfflipping:
                    self.controls.boost = False
                self.controls.throttle = velocity_2d(
                    self.info.my_car.velocity) < 500
Example #16
0
    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
Example #17
0
    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
Example #18
0
    def setup(self):
        self.aerial = Aerial(self.agent.game.my_car)
        self.turn = AerialTurn(self.agent.game.my_car)
        myGoal = center = Vector([0, 5120 * sign(self.agent.team), 200])
        enemyGoal = center = Vector([0, 5120 * -sign(self.agent.team), 200])
        aboveThreshold = False

        if self.agent.me.boostLevel > 0:
            for i in range(0, self.agent.ballPred.num_slices):
                targetVec = Vector([
                    self.agent.ballPred.slices[i].physics.location.x,
                    self.agent.ballPred.slices[i].physics.location.y,
                    self.agent.ballPred.slices[i].physics.location.z
                ])

                if self.agent.ballPred.slices[i].physics.location.z >= 300:
                    if not aboveThreshold:
                        aboveThreshold = True
                    goalDist = distance2D(center, targetVec)
                    acceptable = False
                    if self.agent.team == 0:
                        if self.agent.me.location[1] < targetVec[1]:
                            acceptable = True
                    else:
                        if self.agent.me.location[1] > targetVec[1]:
                            acceptable = True

                    if acceptable:
                        zOffset = -10
                        if goalDist < 1500:
                            if targetVec[2] > 600:
                                zOffset = 70
                        shotAngle = correctAngle(
                            math.degrees(angle2(targetVec, enemyGoal)) +
                            90 * -sign(self.agent.team))

                        if self.agent.team == 0:
                            if targetVec[1] >= 0:
                                if abs(targetVec[0]) > 893:
                                    if targetVec[0] > 0:
                                        closePost = Vector([
                                            893, 5120 * -sign(self.agent.team),
                                            200
                                        ])
                                    else:
                                        closePost = Vector([
                                            -893,
                                            5120 * -sign(self.agent.team), 200
                                        ])

                                    attackAngle = correctAngle(
                                        math.degrees(
                                            angle2(targetVec, closePost)) +
                                        90 * -sign(self.agent.team))

                                    targetLocal = toLocal(
                                        Vector([
                                            self.agent.ballPred.slices[i].
                                            physics.location.x,
                                            self.agent.ballPred.slices[i].
                                            physics.location.y,
                                            self.agent.ballPred.slices[i].
                                            physics.location.z
                                        ]), self.agent.me)
                                    carToBallAngle = correctAngle(
                                        math.degrees(
                                            math.atan2(targetLocal[1],
                                                       targetLocal[0])))
                                    totalAngle = correctAngle(
                                        math.degrees(
                                            math.tan(attackAngle) +
                                            math.tan(carToBallAngle) /
                                            (1 - (math.tan(attackAngle) *
                                                  math.tan(carToBallAngle)))))

                                    if abs(totalAngle) > 60:
                                        continue

                        elif self.agent.team == 1:
                            if targetVec[1] <= 0:
                                if abs(targetVec[0]) > 893:
                                    if targetVec[0] > 0:
                                        closePost = Vector([
                                            893, 5120 * -sign(self.agent.team),
                                            200
                                        ])
                                    else:
                                        closePost = Vector([
                                            -893,
                                            5120 * -sign(self.agent.team), 200
                                        ])

                                    attackAngle = correctAngle(
                                        math.degrees(
                                            angle2(targetVec, closePost)) +
                                        90 * -sign(self.agent.team))

                                    targetLocal = toLocal(
                                        Vector([
                                            self.agent.ballPred.slices[i].
                                            physics.location.x,
                                            self.agent.ballPred.slices[i].
                                            physics.location.y,
                                            self.agent.ballPred.slices[i].
                                            physics.location.z
                                        ]), self.agent.me)
                                    carToBallAngle = correctAngle(
                                        math.degrees(
                                            math.atan2(targetLocal[1],
                                                       targetLocal[0])))

                                    totalAngle = correctAngle(carToBallAngle +
                                                              attackAngle)

                                    if abs(totalAngle) > 60:
                                        continue

                        if abs(shotAngle) <= 75:

                            xOffset = clamp(80, -80, (shotAngle * 2) *
                                            -sign(self.agent.team))
                            self.aerial.target = vec3(
                                self.agent.ballPred.slices[i].physics.location.
                                x + xOffset, self.agent.ballPred.slices[i].
                                physics.location.y, self.agent.ballPred.
                                slices[i].physics.location.z + zOffset)

                            self.aerial.arrival_time = self.agent.ballPred.slices[
                                i].game_seconds

                            simulation = self.aerial.simulate()
                            if norm(simulation.location -
                                    self.aerial.target) < 100:

                                self.target_ball = self.agent.ballPred.slices[
                                    i]
                                self.xOffset = xOffset
                                self.zOffset = zOffset
                                self.active = True
                                if self.agent.onSurface:
                                    if self.agent.ballPred.slices[
                                            i].physics.location.z >= 400:
                                        targetLocal = toLocal(
                                            Vector([
                                                self.agent.ballPred.slices[i].
                                                physics.location.x + xOffset,
                                                self.agent.ballPred.slices[i].
                                                physics.location.y,
                                                self.agent.ballPred.slices[i].
                                                physics.location.z + zOffset
                                            ]), self.agent.me)

                                        carToBallAngle = correctAngle(
                                            math.degrees(
                                                math.atan2(
                                                    targetLocal[1],
                                                    targetLocal[0])))
                                        if abs(carToBallAngle) < 45:
                                            if distance2D(
                                                    self.agent.me.location,
                                                    targetLocal) > 1500:
                                                if self.agent.ballPred.slices[
                                                        i].physics.location.z >= 900:
                                                    self.agent.activeState = airLaunch(
                                                        self.agent)
                                                    self.active = False
                                                    return self.agent.activeState.update(
                                                    )
                                break

                else:
                    if aboveThreshold:
                        break
Example #19
0
    def __init__(self, car: Car):
        super().__init__(car)

        self.turn = AerialTurn(car)
        self.trajectory = []
Example #20
0
 def __init__(self, car: Car):
     self.turn = AerialTurn(car)
     self.target: vec3 = None
     self.car: Car = car
     self.up: vec3 = None
     self.controls: Input = Input()
Example #21
0
    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

            b_position = Vector3(random.uniform(-1500,  1500),
                                 random.uniform( 2500,  3500),
                                 random.uniform(  300,   500))

            b_velocity = Vector3(random.uniform(-300,  300),
                                 random.uniform(-100,  100),
                                 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)
            ))

            # this just initializes the car and ball
            # to different starting points each time
            c_position = Vector3(b_position.x, 0 * random.uniform(-1500, -1000), 25)

            #c_position = Vector3(200, -1000, 25)
            car_state = CarState(physics=Physics(
                location=c_position,
                velocity=Vector3(0, 800, 0),
                rotation=Rotator(0, 1.6, 0),
                angular_velocity=Vector3(0, 0, 0)
            ), boost_amount=100)

            self.set_game_state(GameState(
                ball=ball_state,
                cars={self.game.id: car_state})
            )

            next_state = State.WAIT

        if self.state == State.WAIT:

            if self.timer > 0.2:
                next_state = State.INITIALIZE

        if self.state == State.INITIALIZE:

            self.aerial = Aerial(self.game.my_car)
            # self.aerial.up = normalize(vec3(
            #     random.uniform(-1, 1),
            #     random.uniform(-1, 1),
            #     random.uniform(-1, 1)))
            self.turn = AerialTurn(self.game.my_car)

            # predict where the ball will be
            prediction = Ball(self.game.ball)
            self.ball_predictions = [vec3(prediction.location)]

            for i in range(100):

                prediction.step(0.016666)
                prediction.step(0.016666)
                self.ball_predictions.append(vec3(prediction.location))

                # if the ball is in the air
                if prediction.location[2] > 500:

                    self.aerial.target = prediction.location
                    self.aerial.arrival_time = prediction.time

                    simulation = self.aerial.simulate()

                    # # check if we can reach it by an aerial
                    if norm(simulation.location - self.aerial.target) < 100:
                        prediction.step(0.016666)
                        prediction.step(0.016666)
                        self.aerial.target = prediction.location
                        self.aerial.arrival_time = prediction.time
                        self.target_ball = Ball(prediction)
                        break


            next_state = State.RUNNING

        if self.state == State.RUNNING:

            self.log.write(f"{{\"car\":{self.game.my_car.to_json()},"
                           f"\"ball\":{self.game.ball.to_json()}}}\n")

            self.aerial.step(self.game.time_delta)
            self.controls = self.aerial.controls

            if self.timer > self.timeout:
                next_state = State.RESET

                self.aerial = None
                self.turn = None

        self.timer += self.game.time_delta
        self.state = next_state

        self.renderer.begin_rendering()
        red = self.renderer.create_color(255, 230, 30, 30)
        purple = self.renderer.create_color(255, 230, 30, 230)
        white = self.renderer.create_color(255, 230, 230, 230)

        if self.aerial:
            r = 200
            x = vec3(r, 0, 0)
            y = vec3(0, r, 0)
            z = vec3(0, 0, r)
            target = self.aerial.target

            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)

        if self.ball_predictions:
            self.renderer.draw_polyline_3d(self.ball_predictions, purple)

        self.renderer.end_rendering()

        return self.controls
Example #22
0
    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()

        if not self.action:
            self.action = AerialTurn(self.game.my_car)
            self.action.eps_phi = 0.01
            self.action.eps_omega = 0.02

        if self.timer == 0.0:

            # randomly generate a proper orthogonal matrix
            self.action.target = axis_to_rotation(
                vec3(random.uniform(-2, 2), random.uniform(-2, 2),
                     random.uniform(-2, 2)))

        f = vec3(self.action.target[0, 0], self.action.target[1, 0],
                 self.action.target[2, 0])
        position = Vector3(0, 0, 1000)
        velocity = Vector3(0, 0, 0)

        car_state = CarState(
            physics=Physics(location=position, velocity=velocity))

        # spawn the ball in front of the desired orientation to visually indicate where we're trying to go
        ball_state = BallState(
            physics=Physics(location=Vector3(500 * f[0], 500 *
                                             f[1], 500 * f[2] + 1000),
                            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}))

        self.action.step(self.game.time_delta)
        self.controls = self.action.controls

        print(self.action.alpha)

        self.timer += self.game.time_delta
        if self.timer > 2.0:
            self.timer = 0.0

        error = angle_between(self.game.my_car.rotation, self.action.target)

        self.renderer.begin_rendering("path")
        red = self.renderer.create_color(255, 230, 30, 30)
        self.renderer.draw_string_2d(50, 50, 3, 3,
                                     f"error: {error:.4f} radians", red)
        self.renderer.draw_string_2d(50, 100, 3, 3,
                                     f"Roll:   {self.controls.roll:+.2f}", red)
        self.renderer.draw_string_2d(50, 150, 3, 3,
                                     f"Pitch: {self.controls.pitch:+.2f}", red)
        self.renderer.draw_string_2d(50, 200, 3, 3,
                                     f"Yaw:  {self.controls.yaw:+.2f}", red)
        self.renderer.end_rendering()

        return self.controls
Example #23
0
from rlutilities.linear_algebra import vec3, axis_to_rotation, look_at
from rlutilities.mechanics import AerialTurn
from rlutilities.simulation import Car

c = Car()

c.time = 0.0
c.location = vec3(0, 0, 500)
c.velocity = vec3(0, 0, 0)
c.angular_velocity = vec3(0.1, -2.0, 1.2)
c.rotation = axis_to_rotation(vec3(1.7, -0.5, 0.3))
c.on_ground = False

turn = AerialTurn(c)
turn.target = look_at(vec3(1, 0, 0), vec3(0, 0, 1))

turn.step(0.0166)
print(turn.controls.roll)
print(turn.controls.pitch)
print(turn.controls.yaw)

simulation = turn.simulate()
print(simulation.time)
Example #24
0
 def __init__(self, car: Car, target: vec3):
     super().__init__(car)
     self.turn = AerialTurn(car)
     self.target = target
     self.jump = AirDodge(car, 0.2)
Example #25
0
    def __init__(self, car: Car):
        super().__init__(car)

        self.landing = False
        self.turn = AerialTurn(self.car)
        self.recovery = Recovery(self.car)
Example #26
0
    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

            # put the car in the middle of the field
            car_state = CarState(physics=Physics(
                location=Vector3(-2000, 0, 18),
                velocity=Vector3(0, 0, 0),
                rotation=Rotator(0, 0, 0),
                angular_velocity=Vector3(0, 0, 0)),
                                 jumped=False,
                                 double_jumped=False)

            # put the ball somewhere out of the way
            ball_state = BallState(
                physics=Physics(location=Vector3(0, 5000, 0),
                                velocity=Vector3(0, 0, 0),
                                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}))

            next_state = State.WAIT

        if self.state == State.WAIT:

            if self.timer > 0.2:
                next_state = State.INITIALIZE

        if self.state == State.INITIALIZE:

            self.dodge = Dodge(self.game.my_car)
            self.turn = AerialTurn(self.game.my_car)

            f = self.game.my_car.forward()
            l = self.game.my_car.left()
            u = self.game.my_car.up()

            # musty flick
            if self.counter % 3 == 0:
                self.name = "Musty Flick"
                self.dodge.duration = 0.2
                self.dodge.delay = 0.8
                self.dodge.direction = vec2(f)
                self.dodge.preorientation = look_at(-0.1 * f - u, -1.0 * u)

            # diagonal forward dodge
            if self.counter % 3 == 1:
                self.name = "Fast Forward Dodge"
                self.dodge.duration = 0.15
                self.dodge.delay = 0.4
                self.dodge.direction = vec2(1.0, 0.0)
                self.dodge.preorientation = dot(
                    axis_to_rotation(vec3(0, 0, 3)), self.game.my_car.rotation)

            # diagonal twist
            if self.counter % 3 == 2:
                self.name = "Air-Roll Dodge hit"
                self.dodge.duration = 0.15
                self.dodge.delay = 0.5
                self.dodge.direction = vec2(f - 0.3 * l)
                self.dodge.preorientation = dot(
                    self.game.my_car.rotation,
                    axis_to_rotation(vec3(-0.8, -0.4, 0)))

            self.counter += 1

            next_state = State.RUNNING

        if self.state == State.RUNNING:

            if self.timer > 1.2:
                self.turn.target = look_at(xy(self.game.my_car.velocity),
                                           vec3(0, 0, 1))
                self.turn.step(self.game.time_delta)
                self.controls = self.turn.controls

            else:
                self.dodge.step(self.game.time_delta)
                self.controls = self.dodge.controls

            if self.timer > self.timeout:
                next_state = State.RESET

        self.game.my_car.last_input.roll = self.controls.roll
        self.game.my_car.last_input.pitch = self.controls.pitch
        self.game.my_car.last_input.yaw = self.controls.yaw

        self.timer += self.game.time_delta
        self.state = next_state

        if self.name:
            self.renderer.begin_rendering()
            self.renderer.draw_string_2d(50, 50, 6, 6, self.name,
                                         self.renderer.red())
            self.renderer.end_rendering()

        return self.controls