Ejemplo n.º 1
0
def exampleController(agent, target_object, target_speed):
    location = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    angle_to_ball = math.atan2(location.data[1], location.data[0])

    current_speed = velocity2D(agent.me)
    #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 distance2D(target_object, agent.me) > (
            velocity2D(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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
 def run(self, my_car, packet, agent):
     car_location = Vec3(my_car.physics.location)
     vertical_vel = my_car.physics.velocity.z
     controls = SimpleControllerState()
     controls.yaw = steer_toward_target(
         my_car, self.target, -my_car.physics.angular_velocity.z / 6)
     controls.boost = not (vertical_vel < 0 and car_location.z > 40
                           and self.tick < 20)
     controls.use_item = car_location.dist(
         Vec3(packet.game_ball.physics.location
              )) < 200 and relative_location(
                  car_location, Orientation(my_car.physics.rotation),
                  Vec3(packet.game_ball.physics.location)).z < 75
     if self.tick == 0:
         controls.jump = True
         self.tick = 1
     else:
         if self.tick <= 10: self.tick += 1
         elif my_car.has_wheel_contact: agent.stack.pop()
         if vertical_vel < 0 and car_location.z > 40 and self.tick < 20:
             self.tick += 1
             controls.pitch = 1
         if vertical_vel < 0 and car_location.z < 40:
             controls.jump = True
             controls.pitch = -1
             controls.yaw = 0
     return controls
Ejemplo n.º 5
0
def frugalController(agent, target, speed):
    controller_state = SimpleControllerState()
    location = toLocal(target, agent.me)
    angle_to_target = math.atan2(location.data[1], location.data[0])

    controller_state.steer = steer(angle_to_target)

    speed -= ((angle_to_target**2) * 300)
    current_speed = velocity1D(agent.me).data[1]
    if current_speed < speed:
        controller_state.throttle = 1.0
    elif current_speed - 50 > speed:
        controller_state.throttle = -1.0
    else:
        controller_state.throttle = 0

    time_difference = time.time() - agent.start
    if time_difference > 2.2 and distance2D(
            target, agent.me) > (velocity2D(agent.me) * 2.3) and abs(
                angle_to_target
            ) < 0.9 and current_speed < speed and current_speed > 220:
        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
Ejemplo n.º 6
0
def waitController(agent, target, speed):
    controller_state = SimpleControllerState()
    loc = toLocal(target, agent.me)
    angle_to_target = math.atan2(loc.data[1], loc.data[0])


    controller_state.steer = steer(angle_to_target)

    current_speed = velocity2D(agent.me) 
    if current_speed < speed:
        controller_state.throttle = 1.0
    elif current_speed - 50 > speed:
        controller_state.throttle = -1.0
    else:
        controller_state.throttle = 0

    time_diff = time.time() - agent.start
    if time_diff > 2.2 and distance2D(target,agent.me) > (velocity2D(agent.me)*2.3) and abs(angle_to_target) < 1 and current_speed < speed:
        agent.start = time.time()
    elif time_diff <= 0.1:
        controller_state.jump = True
        controller_state.pitch = -1
    elif time_diff >= 0.1 and time_diff <= 0.15:
        controller_state.jump = False
        controller_state.pitch = -1
    elif time_diff > 0.15 and time_diff < 1:
        controller_state.jump = True
        controller_state.yaw = controller_state.steer
        controller_state.pitch = -1

    return controller_state
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    def flyController(self, agent):
        controller_state = SimpleControllerState()
        location = toLocal(agent.ball, agent.me)
        angle_to_target = np.arctan2(location[1], location[0])
        location_of_target = toLocal(agent.ball, agent.me)
        '''steering'''
        if angle_to_target > .1:
            controller_state.steer = 1
            #controller_state.yaw = 1
            controller_state.throttle = 1
            if distance2D(agent.ball, agent.me) < 1000:
                controller_state.boost = True

        elif angle_to_target < -.1:
            controller_state.steer = -1
            #controller_state.yaw = -1
            controller_state.throttle = 1
            if distance2D(agent.ball, agent.me) < 1000:
                controller_state.boost = True

        else:
            controller_state.steer = controller_state.yaw = 0
            controller_state.throttle = .5
            if angle_to_target < .1 and angle_to_target > -.1:
                controller_state.boost = True
            else:
                controller_state.boost = False

        #jump
        time_difference = time.time() - agent.start
        if time_difference > 2.2:
            agent.start = time.time()
        elif time_difference < .1 and distance2D(
                agent.ball, agent.me) < 1000 and agent.ball.location[2] > 100:
            controller_state.jump = True
            print("jump")
        else:
            controller_state.jump = False

        if agent.ball.location[2] > agent.me.location[
                2] and agent.me.rotation[0] < verticalangle2D(
                    agent.ball.local_location,
                    agent.me) and agent.me.rotation[1] < angle_to_radians(
                        0):  #change angle, this is wrong
            controller_state.pitch = 1  # nose up

        elif agent.ball.location[2] < agent.me.location[
                2] and agent.me.rotation[0] > verticalangle2D(
                    agent.ball.local_location,
                    agent.me) and agent.me.rotation[1] > angle_to_radians(0):
            controller_state.pitch = -1  # nose down

        #updated code broke this
        # if(agent.ball.location[2] > agent.me.location[2]):
        #     print("ball above car", verticalangle2D(agent.ball.local_location, agent.me)*180/np.pi)
        # if(agent.ball.location[2] < agent.me.location[2]):
        #     print("ball below car", verticalangle2D(agent.ball.local_location, agent.me)*180/np.pi)

        return (controller_state)
Ejemplo n.º 10
0
    def exampleController(self, target_object, target_speed):
        location = target_object.local_location
        controller_state = SimpleControllerState()
        angle_to_ball = math.atan2(location.data[1], location.data[0])
        current_speed = velocity2D(self.me)

        #team
        team = sign(self.team)
        # if team<=0:   #blue
        #try to get the ball to orange goal
        #else:    #orange
        #try to get the ball to blue goal

        #steering
        if angle_to_ball > 0.1:
            controller_state.steer = controller_state.yaw = 1
        elif angle_to_ball < -0.1:
            controller_state.steer = controller_state.yaw = -1
        else:
            controller_state.steer = controller_state.yaw = 0

        #powersliding
        if angle_to_ball > 0.8:
            controller_state.handbrake = True
        elif angle_to_ball < -0.8:
            controller_state.handbrake = True
        else:
            controller_state.handbrake = False

        #throttle
        if target_speed > current_speed:
            controller_state.throttle = 1.0
            if target_speed > 1400 and self.start > 2.2 and current_speed < 2250:
                controller_state.boost = True
        elif target_speed < current_speed:
            controller_state.throttle = 0

        #dodging front only
        time_difference = time.time() - self.start
        if time_difference > 2.2 and distance2D(
                target_object.location,
                self.me.location) > 1000 and abs(angle_to_ball) < 1.3:
            self.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
Ejemplo n.º 11
0
    def update(self):
        stateController = SimpleControllerState()

        if not self.firstJump:
            self.firstJump = True
            stateController.jump = True
            self.jumpTimer = time.time()

        elif self.firstJump and not self.secondJump:
            if time.time() - self.jumpTimer < self.firstJumpHold:
                stateController.jump = True

            elif time.time() - self.jumpTimer > self.firstJumpHold and time.time() - self.jumpTimer < self.firstJumpHold +.05:
                stateController.boost = True
                stateController.jump = False

            else:
                self.secondJump = True
                stateController.boost = True
                self.jumpTimer = time.time()

        else:
            if time.time() - self.jumpTimer < self.secondJumpHold:
                stateController.jump = True
                stateController.boost = True

            else:
                self.active = False
                self.jump = False
                #self.agent.activeState = flightSystems(self.agent)

        if time.time() - self.jumpTimer > 0.15:

            pitchAngle = math.degrees(self.agent.me.rotation[1])
            y_vel = self.agent.me.avelocity[1]
            pitch = 0
            if pitchAngle > 50:
                if y_vel > -.4:
                    pitch = clamp(1,-1,-1 + abs(y_vel))
            elif pitchAngle < 50:
                if y_vel < .4:
                    pitch = clamp(1,-1,1 - abs(y_vel))

            #print(pitchAngle)

            if y_vel > 1:
                pitch = -1
            elif y_vel < -1:
                pitch = 1

            stateController.pitch = pitch
        #print(math.degrees(self.agent.me.rotation[1]))
        return stateController
Ejemplo n.º 12
0
def diagonal(game_info=None, x_sign=None, persistent=None):

    current_state = game_info.me
    controls = SimpleControllerState()

    #Set which boost we want based on team and side.
    if x_sign == -1:
        first_boost = 11
    else:
        first_boost = 10

    if game_info.boosts[first_boost].is_active:
        #If we haven't taken the small boost yet, drive towards it
        controls = GroundTurn(
            current_state,
            current_state.copy_state(pos=Vec3(0, -1000, 0))).input()
        controls.boost = 1

    elif abs(current_state.pos.y) > 1100 and current_state.wheel_contact:
        controls.jump = 1
        controls.boost = 1

    elif abs(current_state.pos.y) > 1100 and current_state.pos.z < 40:
        controls.jump = 1
        controls.boost = 1

    elif abs(current_state.pos.y) > 500 and not current_state.double_jumped:
        controls = CancelledFastDodge(current_state, Vec3(1, x_sign,
                                                          0)).input()

    elif abs(current_state.pos.y) > 250 and not current_state.wheel_contact:
        if persistent.aerial_turn.action == None:
            persistent.aerial_turn.initialize = True
            target_rot = Orientation(pitch=pi / 3,
                                     yaw=current_state.rot.yaw,
                                     roll=0)
            persistent.aerial_turn.target_orientation = target_rot

        else:
            controls, persistent = aerial_rotation(game_info.dt, persistent)
        controls.boost = 1
        controls.steer = x_sign  #Turn into the ball

    elif abs(current_state.pos.y) > 235:
        controls.throttle = 1
        controls.boost = 1
        controls.steer = x_sign

    else:
        controls = FrontDodge(current_state).input()

    return controls, persistent
Ejemplo n.º 13
0
def calcController(agent, target_object, target_speed):
    goal_local = toLocal([0, -sign(agent.team)*FIELD_LENGTH/2, 100], agent.me)
    goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])
    
    loc = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    
    angle_to_targ = math.atan2(loc.data[1],loc.data[0])

    current_speed = velocity2D(agent.me)
    distance = distance2D(target_object, agent.me)

    #steering 
    controller_state.steer = steer(angle_to_targ)

    r = radius(current_speed)
    slowdown = (Vector3([0,sign(target_object.data[0])*(r+40),0])-loc.flatten()).magnitude() / cap(r*1.5,1,1200)
    target_speed = cap(current_speed*slowdown,0,current_speed)


    # throttle
    if agent.ball.location.data[0] == 0 and agent.ball.location.data[1] == 0:
        controller_state.throttle, controller_state.boost = 1, True
    else:
        controller_state.throttle, controller_state.boost = throttle(target_speed,current_speed)


    #dodging
    time_diff = time.time() - agent.start 
    if (time_diff > 2.2 and distance <= 150) or (time_diff > 4 and distance >= 1000) and not kickoff(agent):
        agent.start = time.time()
    elif time_diff <= 0.1:
        controller_state.jump = True
        controller_state.pitch = -1
    elif time_diff >= 0.1 and time_diff <= 0.15:
        controller_state.jump = False
        controller_state.pitch = -1
    elif time_diff > 0.15 and time_diff < 1:
        controller_state.jump = True
        controller_state.yaw = math.sin(goal_angle)
        controller_state.pitch = -abs(math.cos(goal_angle))
        
    if not dodging(agent) and not agent.me.grounded:
        target = agent.me.velocity.normalize()
        targ_local = to_local(target.scale(500), agent.me)

        return recoveryController(agent, targ_local)
    
    return controller_state
Ejemplo n.º 14
0
    def shotController(self, agent):
        controller_state = SimpleControllerState()
        goal_location = toLocal(
            [0, -sign(agent.team) * FIELD_LENGTH / 2, 0],
            agent.me)  #100 is arbitrary since math is in 2D still
        goal_angle = np.arctan2(goal_location[1], goal_location[0])
        #print(goal_angle * 180 / np.pi)

        location = toLocal(agent.ball, agent.me)
        angle_to_target = np.arctan2(location[1], location[0])
        target_speed = velocity2D(
            agent.ball) + (distance2D(agent.ball, agent.me) / 1.5)

        current_speed = velocity2D(agent.me)
        #steering
        if angle_to_target > .1:
            controller_state.steer = controller_state.yaw = 1
        elif angle_to_target < -.1:
            controller_state.steer = controller_state.yaw = -1
        else:
            controller_state.steer = controller_state.yaw = 0
        #throttle
        if angle_to_target >= 1.4:
            target_speed -= 1400
        else:
            if (target_speed > 1400 and target_speed > current_speed
                    and agent.start > 2.2 and current_speed < 2250):
                controller_state.boost = True
        if target_speed > current_speed:
            controller_state.throttle = 1.0
        elif target_speed < current_speed:
            controller_state.throttle = 1

        #dodging
        time_difference = time.time() - agent.start
        if time_difference > 2.2 and distance2D(agent.ball, agent.me) <= 270:
            agent.start = time.time()
        elif time_difference <= .1:
            controller_state.jump = True
            controller_state.pitch = -1
        elif time_difference >= .1 and time_difference <= .15:
            controller_state.jump = False
            controller_state.pitch = -1
        elif time_difference > .15 and time_difference < 1:
            controller_state.jump = True
            controller_state.yaw = math.sin(goal_angle)
            controller_state.pitch = -abs(math.cos(goal_angle))

        return controller_state
Ejemplo n.º 15
0
def follow_controller(agent, target_object, target_speed):
    location = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    angle_to_ball = math.atan2(location.data[1], location.data[0])
    current_speed = velocity2D(agent.me)

    pitch = agent.me.location.data[0] % math.pi
    roll = agent.me.location.data[2] % math.pi

    # Turn dem wheels
    controller_state.steer = cap(angle_to_ball * 5, -1, 1)
    print(velocity2D(agent.me))

    if abs(angle_to_ball) > (math.pi / 3.):
        controller_state.handbrake = True
    else:
        controller_state.handbrake = False

    # throttle
    if target_speed > current_speed:
        controller_state.throttle = 1.0
        if target_speed > 1400 and agent.start > 2.2 and current_speed < 2250 and abs(
                angle_to_ball) < (math.pi / 3.):
            controller_state.boost = True
    elif target_speed < current_speed:
        controller_state.throttle = 0

    # doging
    time_difference = time.time() - agent.start

    if time_difference > 2.2 and distance2D(
            target_object,
            agent.me) > 1000 and abs(angle_to_ball) < 1.3 and velocity2D(
                agent.me) > 1200:
        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

    # print("target: " + str(target_speed) + ", current: " + str(current_speed))
    return controller_state
Ejemplo n.º 16
0
def pid_steer(game_info, target_location, pid: SteerPID):
    """Gives a set of commands to move the car along the ground toward a target location

    Attributes:
        target_location (Vec3): The local location the car wants to aim for

    Returns:
        SimpleControllerState: the set of commands to achieve the goal
    """
    controller_state = SimpleControllerState()
    ball_direction = target_location

    angle = -np.arctan2(ball_direction.y, ball_direction.x)

    if angle > np.pi:
        angle -= 2 * np.pi
    elif angle < -np.pi:
        angle += 2 * np.pi

    # adjust angle
    turn_rate = pid.get_steer(angle)

    controller_state.throttle = 1
    controller_state.steer = turn_rate
    controller_state.jump = False
    return controller_state
Ejemplo n.º 17
0
    def input(self):
        controller_input = SimpleControllerState()
        current_angle_vec = Vec3(cos(self.current_state.rot.yaw), sin(self.current_state.rot.yaw), 0)
        goal_angle_vec = Vec3(cos(self.goal_state.rot.yaw), sin(self.goal_state.rot.yaw), 0)
        vel_2d = Vec3(self.current_state.vel.x, self.current_state.vel.y, 0)



        if (self.current_state.pos - self.goal_state.pos).magnitude() > 400:

            #Turn towards target. Hold throttle until we're close enough to start stopping.
            controller_input = GroundTurn(self.current_state, self.goal_state).input()

        elif vel_2d.magnitude() < 50 and current_angle_vec.dot(goal_angle_vec) < 0:

            #If we're moving slowly, but not facing the right way, jump to turn in the air.
            #Decide which way to turn.  Make sure we don't have wraparound issues.

            goal_x = goal_angle_vec.x
            goal_y = goal_angle_vec.y
            car_theta = self.current_state.rot.yaw


            #Rotated to the car's reference frame on the ground.
            rel_vector = Vec3((goal_x*cos(car_theta)) + (goal_y * sin(car_theta)),
                              (-(goal_x*sin(car_theta))) + (goal_y * cos(car_theta)),
                              0)

            correction_angle = atan2(rel_vector.y, rel_vector.x)

            #Jump and turn to reach goal yaw.
            if self.current_state.wheel_contact:
                controller_input.jump = 1
            else:
                controller_input.yaw = cap_magnitude(correction_angle, 1)

        elif self.current_state.vel.magnitude() > 400:
            #TODO: Proportional controller to stop in the right place
            controller_input.throttle = -1


        else:
            #Wiggle to face ball
            #Check if the goal is ahead of or behind us, and throttle in that direction
            goal_angle = atan2((self.goal_state.pos - self.current_state.pos).y, (self.goal_state.pos - self.current_state.pos).x)
            if abs(angle_difference(goal_angle,self.current_state.rot.yaw)) > pi/2:
                correction_sign = -1
            else:
                correction_sign = 1
            controller_input.throttle = correction_sign

            #Correct as we wiggle so that we face goal_yaw.
            if angle_difference(self.goal_state.rot.yaw, self.current_state.rot.yaw) > 0:
                angle_sign = 1
            else:
                angle_sign = -1

            controller_input.steer = correction_sign*angle_sign

        return controller_input
Ejemplo n.º 18
0
def ground_controller(game_info, target_location):
    """Gives a set of commands to move the car along the ground toward a target location
    
    Attributes:
        target_location (Vec3): The local location the car wants to aim for
        
    Returns:
        SimpleControllerState: the set of commands to achieve the goal
    """
    controller_state = SimpleControllerState()
    ball_direction = target_location
    distance = target_location.flat().length()
    
    angle = -math.atan2(ball_direction.y, ball_direction.x)

    if angle > math.pi:
        angle -= 2*math.pi
    elif angle < -math.pi:
        angle += 2*math.pi
    
    speed = 0.0
    turn_rate = 0.0
    r1 = 250
    r2 = 1000
    # adjust angle
    if angle > 0.02:
        turn_rate = -1.0
    elif angle < -0.02:
        turn_rate = 1.0
    else:
        turn_rate = 0
    if distance <= r1:
        # if toward ball move forward
        if abs(angle) < math.pi / 4:
            speed = 1.0
        else:
            # if not toward ball reverse, flips turn rate to adjust
            turn_rate = turn_rate * -1.0
            speed = -1.0
    # if far away, move at full speed forward
    elif distance >= r2:
        speed = 1.0
        if game_info.me.velocity.length() < 2250:
            controller_state.boost = True
    # if mid range, adjust forward
    else:
        # adjust speed
        if game_info.me.velocity.length() < 2250:
            controller_state.boost = True
        if abs(angle) < math.pi / 2:
            speed = 1.0
        else:
            speed = 0.5

    controller_state.throttle = speed
    controller_state.steer = turn_rate
    controller_state.jump = False
    return controller_state
Ejemplo n.º 19
0
def shotController(agent, target_object, target_speed):
    goal_local = toLocal([0, -sign(agent.team) * FIELD_LENGTH / 2, 100],
                         agent.me)
    goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])

    location = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    angle_to_target = math.atan2(location.data[1], location.data[0])

    current_speed = velocity2D(agent.me)
    #steering
    if angle_to_target > 0.1:
        controller_state.steer = controller_state.yaw = 1
    elif angle_to_target < -0.1:
        controller_state.steer = controller_state.yaw = -1
    else:
        controller_state.steer = controller_state.yaw = 0

    #throttle
    if angle_to_target >= 1.4:
        target_speed -= 1400
    else:
        if target_speed > 1400 and target_speed > current_speed and agent.start > 2.2 and current_speed < 2250:
            controller_state.boost = True
    if target_speed > current_speed:
        controller_state.throttle = 1.0
    elif target_speed < current_speed:
        controller_state.throttle = 0

    #dodging
    time_difference = time.time() - agent.start
    if time_difference > 2.2 and distance2D(target_object, agent.me) <= 270:
        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 = math.sin(goal_angle)
        controller_state.pitch = -abs(math.cos(goal_angle))

    return controller_state
Ejemplo n.º 20
0
    def continue_dodge(self, data):
        ct = time.time()
        controller = SimpleControllerState()

        # target is allowed to be a function that takes data as a parameter. Check what it is
        if callable(self.target):
            target = self.target(data)
        else:
            target = self.target
        car_to_point = target - data.car.location
        vel = data.car.velocity.proj_onto_size(car_to_point)
        face_ang = car_to_point.ang_to(data.car.orientation.front)
        controller.boost = self.boost and face_ang < self._boost_ang_req and vel < self._max_speed

        if ct >= self.last_start_time + self._t_finishing:
            if data.car.wheel_contact:
                self.end_dodge()
            return fix_orientation(data)

        elif ct >= self.last_start_time + self._t_wait_flip:
            controller.throttle = 1

        elif ct >= self.last_start_time + self._t_second_unjump:
            controller.throttle = 1

        elif ct >= self.last_start_time + self._t_aim:
            if ct >= self.last_start_time + self._t_second_jump:
                controller.jump = 1

            controller.throttle = 1

            car_to_point_u = car_to_point.flat().normalized()
            car_to_point_rel = car_to_point_u.rotate_2d(
                -data.car.orientation.front.ang())
            controller.pitch = -car_to_point_rel.x
            controller.yaw = car_to_point_rel.y

        elif ct >= self.last_start_time + self._t_first_unjump:
            controller.throttle = 1

        elif ct >= self.last_start_time:
            controller.jump = 1
            controller.throttle = 1

        return controller
Ejemplo n.º 21
0
def get_controls(game_info, sub_state_machine):

    controls = SimpleControllerState()

    controls.jump = 1
    controls.boost = 1

    persistent = game_info.persistent
    return controls, persistent
Ejemplo n.º 22
0
def simple_front_flip_chain():
    first_controller = SimpleControllerState()
    second_controller = SimpleControllerState()
    third_controller = SimpleControllerState()

    first_controller.jump = True
    first_duration = 0.1

    second_controller.jump = False
    second_controller.pitch = -1
    second_duration = 0.1

    third_controller.jump = True
    third_controller.pitch = -1
    third_duration = 0.1

    return Action_chain([first_controller,second_controller,third_controller],[first_duration,second_duration,
                                                                               third_duration])
Ejemplo n.º 23
0
def shotController(agent, target_object, target_speed):
    goal_local = toLocal([0, -sign(agent.team)*FIELD_LENGTH/2, 100], agent.me)
    goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])

    loc = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    angle_to_targ = math.atan2(loc.data[1],loc.data[0])

    current_speed = velocity2D(agent.me)
    distance = distance2D(target_object, agent.me)

    #steering 
    controller_state.steer = steer(angle_to_targ)

    # throttle
    if agent.ball.location.data[0] == 0 and agent.ball.location.data[1] == 0:
        controller_state.throttle, controller_state.boost = 1, True
    else:
        controller_state.throttle, controller_state.boost = throttle(target_speed,current_speed)

    time_diff = time.time() - agent.start 

    #dodging
    time_diff = time.time() - agent.start 
    if (time_diff > 2.2 and distance <= 270) or (time_diff > 4 and distance >= 1000):
        agent.start = time.time()
    elif time_diff <= 0.1:
        controller_state.jump = True
        controller_state.pitch = -1
    elif time_diff >= 0.1 and time_diff <= 0.15:
        controller_state.jump = False
        controller_state.pitch = -1
    elif time_diff > 0.15 and time_diff < 1:
        controller_state.jump = True
        controller_state.yaw = math.sin(goal_angle)
        controller_state.pitch = -abs(math.cos(goal_angle))

    if not dodging(agent) and not agent.me.grounded:
        target = agent.me.velocity.normalize()
        targ_local = to_local(target.scale(500), agent.me)

        return recoveryController(agent, targ_local)
    
    return controller_state
Ejemplo n.º 24
0
    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"
            )
Ejemplo n.º 25
0
    def input(self):
        controller_input = SimpleControllerState()
        #Add a catch to make sure jump_height isn't higher than the max jump height
        #For now make sure jump_height is zero or higher than the height of the car at rest.

        #Zero jump height means we just jump on frame 1
        if self.jump_height == 0 and self.current_state.wheel_contact:
            controller_input.jump = 1

        #If we're not to jump_height yet, hold jump to jump higher.
        elif self.current_state.pos.z < self.jump_height:
            controller_input.jump = 1

        #Turn in the right direction.
        if self.turn_direction == 0:
            raise AttributeError("turn direction should be 1 or -1")
        controller_input.yaw = self.turn_direction

        return controller_input
Ejemplo n.º 26
0
def shot_controller(agent, target_object,
                    target_speed):  #note target location is an obj
    local_goal_location = toLocal([0, FIELD_LENGTH / 2, 100], agent.car)
    goal_angle = math.atan2(local_goal_location.data[1],
                            local_goal_location.data[0])

    location = toLocal(target_object, agent.car)
    controller_state = SimpleControllerState()
    angle_to_target = math.atan2(location.data[1], location.data[0])
    current_speed = velocity2D(agent.car)

    #to steer
    if angle_to_target > 0.1:
        controller_state.steer = controller_state.yaw = 1
    elif angle_to_target < -0.1:
        controller_state.steer = controller_state.yaw = -1
    else:
        controller_state.steer = controller_state.yaw = 0

        #adjust speed
    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

        #techniquing
    time_diff = time.time() - agent.start  #time since last technique
    if time_diff > 2.2 and distance2D(target_object, agent.car) > 270:
        agent.start = time.time()
    elif time_diff <= 0.1:
        controller_state.jump = True
        controller_state.pitch = -1
    elif time_diff >= 0.1 and time_diff <= 0.15:
        controller_state.jump = False
        controller_state.pitch = -1
    elif time_diff > 0.15 and time_diff < 1:
        controller_state.jump = True
        controller_state.yaw = math.sin(goal_angle)
        controller_state.pitch = -abs(math.cos(goal_angle))

    return controller_state
Ejemplo n.º 27
0
def chase_controller(agent, target_object,
                     target_speed):  #note target location is an obj
    location = toLocal(target_object, agent.car)
    controller_state = SimpleControllerState()
    angle_to_ball = math.atan2(location.data[1], location.data[0])
    current_speed = velocity2D(agent.car)
    ball_path = agent.get_ball_prediction_struct(
    )  #next 6 seconds of ball's path
    print(bal_path[1])

    #to steer
    if angle_to_ball > 0.1:
        controller_state.steer = controller_state.yaw = 1
    elif angle_to_ball < -0.1:
        controller_state.steer = controller_state.yaw = -1
    else:
        controller_state.steer = controller_state.yaw = 0

        #adjust speed
    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

        #techniquing
    time_diff = time.time() - agent.start  #time since last technique
    if time_diff > 2.2 and distance2D(
            target_object.location,
            agent.car.location) > 1000 and abs(angle_to_ball) < 1.3:
        agent.start = time.time()
    elif time_diff <= 0.1:
        controller_state.jump = True
        controller_state.pitch = -1
    elif time_diff >= 0.1 and time_diff <= 0.15:
        controller_state.jump = False
    elif time_diff > 0.15 and time_diff < 1:
        controller_state.jump = True
        controller_state.yaw = controller_state.steer
        controller_state.pitch = -1

    return controller_state
Ejemplo n.º 28
0
def shotController(agent, target_object, target_speed, goal=None):
    if goal == None:
        goal = [0, -sign(agent.team) * FIELD_LENGTH / 2, 100]
    goal_local = toLocal(goal, agent.me)
    goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])
    location = toLocal(target_object, agent.me)
    controller_state = SimpleControllerState()
    angle_to_target = math.atan2(location.data[1], location.data[0])

    current_speed = velocity1D(agent.me).data[1]  #velocity2D(agent.me)
    #steering
    controller_state.steer = steer(angle_to_target)

    #throttle
    if target_speed > 1400 and target_speed > current_speed and agent.start > 2.5 and current_speed < 2250 and agent.me.grounded == True:
        controller_state.boost = True
    if target_speed > current_speed:
        controller_state.throttle = 1.0
    elif target_speed < current_speed:
        controller_state.throttle = -1.0

    #dodging
    closing = distance2D(target_object, agent.me) / cap(
        -dpp(target_object, agent.ball.velocity, agent.me.location,
             agent.me.velocity), 1, 2300)
    time_difference = time.time() - agent.start
    if ballReady(
            agent) and time_difference > 2.2 and closing <= 0.8 and distance2D(
                agent.me, target_object) < 200:
        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 = math.sin(goal_angle)
        controller_state.pitch = -abs(math.cos(goal_angle))

    return controller_state
Ejemplo n.º 29
0
def take_shot_controller(agent, target_object, target_speed):
    goal_local = toLocation([0, -sign(agent.team) * FIELD_LENGTH / 2, 100])
    goal_angle = math.atan2(goal_local.data[1], goal_local.data[0])

    pitch = agent.me.location.data[0] % math.pi
    roll = agent.me.location.data[2] % math.pi

    location = toLocation(target_object)
    controller_state = SimpleControllerState()
    angle_to_target = math.atan2(location.data[1], location.data[0])

    current_speed = velocity2D(agent.me)

    controller_state.steer = cap(angle_to_target * 5, -1, 1)

    # 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

    # doging
    time_difference = time.time() - agent.start

    if time_difference > 2.2 and abs(angle_to_target) < 1.3 and velocity2D(
            agent.me) > 1200 and distance2D(target_object, agent.me) < 1000:
        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 = cap(math.sin(goal_angle) / math.pi, -1, 1)
        controller_state.pitch = -1

    # print("target: " + str(target_speed) + ", current: " + str(current_speed))
    return controller_state
Ejemplo n.º 30
-1
def goto(target_location, target_speed, my_car, agent, packet):
    car_speed = Vec3(my_car.physics.velocity).length()
    distance = Vec3(my_car.physics.location).flat().dist(
        target_location.flat())
    angle = Orientation(
        my_car.physics.rotation).forward.ang_to(target_location -
                                                Vec3(my_car.physics.location))
    controls = SimpleControllerState()
    controls.steer = steer_toward_target(my_car, target_location, 0)
    controls.yaw = steer_toward_target(my_car, target_location,
                                       -my_car.physics.angular_velocity.z / 6)
    controls.throttle = cap(target_speed - car_speed, -1, 1)
    controls.boost = (target_speed > 1410
                      and abs(target_speed - car_speed) > 20 and angle < 0.3)
    controls.handbrake = angle > 2.3
    controls.jump = (1 if my_car.physics.location.y >= 0 else -1) == (
        1 if agent.team == 1 else -1
    ) and abs(
        my_car.physics.location.y) > 5000 and my_car.physics.location.z > 200
    controls.use_item = Vec3(my_car.physics.location).dist(
        Vec3(packet.game_ball.physics.location)) < 200 and relative_location(
            Vec3(my_car.physics.location), Orientation(
                my_car.physics.rotation),
            Vec3(packet.game_ball.physics.location)).z < 75
    if (abs(target_speed - car_speed) > 20 and angle < 0.3 and
            distance > 600) and ((target_speed > 1410 and my_car.boost == 0)
                                 or 700 < car_speed < 800):
        agent.stack.append(wavedash(target_location))
    return controls