Ejemplo n.º 1
0
    def run(self, agent: _GoslingAgent):
        if self.target != None:
            local_target = agent.me.local((self.target - agent.me.location).flatten())
        else:
            local_target = agent.me.local(agent.me.velocity.flatten())

        defaultPD(agent, local_target)
        agent.controller.throttle = 1
        if not agent.me.airborne:
            agent.pop()
Ejemplo n.º 2
0
 def run(self, agent: _GoslingAgent):
     target = agent.ball.location + Vector3(0, 200 * side(agent.team), 0)
     local_target = agent.me.local(target - agent.me.location)
     defaultPD(agent, local_target)
     defaultThrottle(agent, 2300)
     if local_target.magnitude() < 650:
         agent.pop()
         # flip towards opponent goal
         agent.push(
             flip(agent.me.local(agent.foe_goal.location - agent.me.location))
         )
Ejemplo n.º 3
0
    def run(self, agent: _GoslingAgent):
        car_to_target = self.target - agent.me.location
        distance_remaining = car_to_target.flatten().magnitude()

        agent.line(
            self.target - Vector3(0, 0, 500),
            self.target + Vector3(0, 0, 500),
            [255, 0, 255],
        )

        if self.vector != None:
            # See commends for adjustment in jump_shot or aerial for explanation
            side_of_vector = sign(self.vector.cross((0, 0, 1)).dot(car_to_target))
            car_to_target_perp = car_to_target.cross((0, 0, side_of_vector)).normalize()
            adjustment = car_to_target.angle(self.vector) * distance_remaining / 3.14
            final_target = self.target + (car_to_target_perp * adjustment)
        else:
            final_target = self.target

        # Some adjustment to the final target to ensure it's inside the field and we don't try to dirve through any goalposts to reach it
        if abs(agent.me.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_target = agent.me.local(final_target - agent.me.location)

        angles = defaultPD(agent, local_target, self.direction)
        defaultThrottle(agent, 2300, self.direction)

        agent.controller.boost = False
        agent.controller.handbrake = (
            True if abs(angles[1]) > 2.3 else agent.controller.handbrake
        )

        velocity = 1 + agent.me.velocity.magnitude()
        if distance_remaining < 350:
            agent.pop()
        elif (
            abs(angles[1]) < 0.05
            and velocity > 600
            and velocity < 2150
            and distance_remaining / velocity > 2.0
        ):
            agent.push(flip(local_target))
        elif abs(angles[1]) > 2.8 and velocity < 200:
            agent.push(flip(local_target, True))
        elif agent.me.airborne:
            agent.push(recovery(self.target))
Ejemplo n.º 4
0
    def run(self, agent: _GoslingAgent):
        car_to_boost = self.boost.location - agent.me.location
        distance_remaining = car_to_boost.flatten().magnitude()

        agent.line(
            self.boost.location - Vector3(0, 0, 500),
            self.boost.location + Vector3(0, 0, 500),
            [0, 255, 0],
        )

        if self.target != None:
            vector = (self.target - self.boost.location).normalize()
            side_of_vector = sign(vector.cross((0, 0, 1)).dot(car_to_boost))
            car_to_boost_perp = car_to_boost.cross((0, 0, side_of_vector)).normalize()
            adjustment = car_to_boost.angle(vector) * distance_remaining / 3.14
            final_target = self.boost.location + (car_to_boost_perp * adjustment)
            car_to_target = (self.target - agent.me.location).magnitude()
        else:
            adjustment = 9999
            car_to_target = 0
            final_target = self.boost.location

        # Some adjustment to the final target to ensure it's inside the field and we don't try to dirve through any goalposts to reach it
        if abs(agent.me.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_target = agent.me.local(final_target - agent.me.location)

        angles = defaultPD(agent, local_target)
        defaultThrottle(agent, 2300)

        agent.controller.boost = self.boost.large if abs(angles[1]) < 0.3 else False
        agent.controller.handbrake = (
            True if abs(angles[1]) > 2.3 else agent.controller.handbrake
        )

        velocity = 1 + agent.me.velocity.magnitude()
        if (
            self.boost.active == False
            or agent.me.boost >= 99.0
            or distance_remaining < 350
        ):
            agent.pop()
        elif agent.me.airborne:
            agent.push(recovery(self.target))
        elif (
            abs(angles[1]) < 0.05
            and velocity > 600
            and velocity < 2150
            and (
                distance_remaining / velocity > 2.0
                or (adjustment < 90 and car_to_target / velocity > 2.0)
            )
        ):
            agent.push(flip(local_target))
Ejemplo n.º 5
0
 def run(self, agent: _GoslingAgent):
     if self.time == -1:
         elapsed = 0
         self.time = agent.time
     else:
         elapsed = agent.time - self.time
     if elapsed < 0.15:
         agent.controller.jump = True
     elif elapsed >= 0.15 and self.counter < 3:
         agent.controller.jump = False
         self.counter += 1
     elif elapsed < 0.3 or (not self.cancel and elapsed < 0.9):
         agent.controller.jump = True
         agent.controller.pitch = self.pitch
         agent.controller.yaw = self.yaw
     else:
         agent.pop()
         agent.push(recovery())
Ejemplo n.º 6
0
    def run(self, agent: _GoslingAgent):
        car_to_ball, distance = (agent.ball.location - agent.me.location).normalize(
            True
        )
        ball_to_target = (self.target - agent.ball.location).normalize()

        relative_velocity = car_to_ball.dot(agent.me.velocity - agent.ball.velocity)
        if relative_velocity != 0.0:
            eta = cap(distance / cap(relative_velocity, 400, 2300), 0.0, 1.5)
        else:
            eta = 1.5

        # If we are approaching the ball from the wrong side the car will try to only hit the very edge of the ball
        left_vector = car_to_ball.cross((0, 0, 1))
        right_vector = car_to_ball.cross((0, 0, -1))
        target_vector = -ball_to_target.clamp(left_vector, right_vector)
        final_target = agent.ball.location + (target_vector * (distance / 2.75))

        # Some adjustment to the final target to ensure we don't try to dirve through any goalposts to reach it
        if abs(agent.me.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        agent.line(
            final_target - Vector3(0, 0, 100),
            final_target + Vector3(0, 0, 100),
            [255, 255, 255],
        )

        angles = defaultPD(agent, agent.me.local(final_target - agent.me.location))
        defaultThrottle(
            agent,
            distance / eta * (agent.ball.location.z / 300) * 20,
            # 2300 if distance > 1600 else 2300 - cap(1600 * abs(angles[1]), 0, 2050),
        )
        agent.controller.boost = (
            False
            if agent.me.airborne or abs(angles[1]) > 0.3
            else agent.controller.boost
        )
        agent.controller.handbrake = (
            True if abs(angles[1]) > 2.3 else agent.controller.handbrake
        )

        if abs(angles[1]) < 0.05 and (eta < 0.35 or distance < 150):
            agent.pop()
            agent.push(flip(agent.me.local(car_to_ball)))
Ejemplo n.º 7
0
def shot_valid(agent: _GoslingAgent, shot, threshold=45):
    # Returns True if the ball is still where the shot anticipates it to be
    # First finds the two closest slices in the ball prediction to shot's intercept_time
    # threshold controls the tolerance we allow the ball to be off by
    slices = agent.get_ball_prediction_struct().slices
    soonest = 0
    latest = len(slices) - 1
    while len(slices[soonest:latest + 1]) > 2:
        midpoint = (soonest + latest) // 2
        if slices[midpoint].game_seconds > shot.intercept_time:
            latest = midpoint
        else:
            soonest = midpoint
    # preparing to interpolate between the selected slices
    dt = slices[latest].game_seconds - slices[soonest].game_seconds
    time_from_soonest = shot.intercept_time - slices[soonest].game_seconds
    slopes = (Vector3(slices[latest].physics.location) -
              Vector3(slices[soonest].physics.location)) * (1 / dt)
    # Determining exactly where the ball will be at the given shot's intercept_time
    predicted_ball_location = Vector3(
        slices[soonest].physics.location) + (slopes * time_from_soonest)
    # Comparing predicted location with where the shot expects the ball to be
    return (shot.ball_location -
            predicted_ball_location).magnitude() < threshold
Ejemplo n.º 8
0
def find_hits(agent: _GoslingAgent, targets):
    # find_hits takes a dict of (left,right) target pairs and finds routines that could hit the ball between those target pairs
    # find_hits is only meant for routines that require a defined intercept time/place in the future
    # find_hits should not be called more than once in a given tick, as it has the potential to use an entire tick to calculate

    # Example Useage:
    # targets = {"goal":(opponent_left_post,opponent_right_post), "anywhere_but_my_net":(my_right_post,my_left_post)}
    # hits = find_hits(agent,targets)
    # print(hits)
    # >{"goal":[a ton of jump and aerial routines,in order from soonest to latest], "anywhere_but_my_net":[more routines and stuff]}
    hits = {name: [] for name in targets}
    struct = agent.get_ball_prediction_struct()

    # Begin looking at slices 0.25s into the future
    # The number of slices
    i = 15
    while i < struct.num_slices:
        # Gather some data about the slice
        intercept_time = struct.slices[i].game_seconds
        time_remaining = intercept_time - agent.time
        if max_hit_time > time_remaining > 0:
            ball_location = Vector3(struct.slices[i].physics.location)
            ball_velocity = Vector3(struct.slices[i].physics.velocity).magnitude()

            if abs(ball_location[1]) > 5250:
                break  # abandon search if ball is scored at/after this point

            # determine the next slice we will look at, based on ball velocity (slower ball needs fewer slices)
            i += 15 - cap(int(ball_velocity // 150), 0, 13)

            car_to_ball = ball_location - agent.me.location
            # Adding a True to a vector's normalize will have it also return the magnitude of the vector
            direction, distance = car_to_ball.normalize(True)

            # How far the car must turn in order to face the ball, for forward and reverse
            forward_angle = direction.angle(agent.me.forward)
            backward_angle = math.pi - forward_angle

            # Accounting for the average time it takes to turn and face the ball
            # Backward is slightly longer as typically the car is moving forward and takes time to slow down
            forward_time = time_remaining - (forward_angle * 0.318)
            backward_time = time_remaining - (backward_angle * 0.418)

            # If the car only had to drive in a straight line, we ensure it has enough time to reach the ball (a few assumptions are made)
            forward_flag = forward_time > 0.0 and (distance * 1.05 / forward_time) < (
                2290 if agent.me.boost > distance / 100 else 1400
            )
            backward_flag = (
                distance < 1500
                and backward_time > 0.0
                and (distance * 1.05 / backward_time) < 1200
            )

            # Provided everything checks out, we begin to look at the target pairs
            if forward_flag or backward_flag:
                for pair in targets:
                    # First we correct the target coordinates to account for the ball's radius
                    # If swapped == True, the shot isn't possible because the ball wouldn't fit between the targets
                    left, right, swapped = post_correction(
                        ball_location, targets[pair][0], targets[pair][1]
                    )
                    if not swapped:
                        # Now we find the easiest direction to hit the ball in order to land it between the target points
                        left_vector = (left - ball_location).normalize()
                        right_vector = (right - ball_location).normalize()
                        best_shot_vector = direction.clamp(left_vector, right_vector)

                        # Check to make sure our approach is inside the field
                        if in_field(ball_location - (200 * best_shot_vector), 1):
                            # The slope represents how close the car is to the chosen vector, higher = better
                            # A slope of 1.0 would mean the car is 45 degrees off
                            slope = find_slope(best_shot_vector, car_to_ball)
                            if forward_flag:
                                if ball_location[2] <= 300 and slope > 0.0:
                                    hits[pair].append(
                                        jump_shot(
                                            ball_location,
                                            intercept_time,
                                            best_shot_vector,
                                            slope,
                                        )
                                    )
                                if (
                                    ball_location[2] > 300
                                    and ball_location[2] < 600
                                    and slope > 1.0
                                    and (ball_location[2] - 250) * 0.14 > agent.me.boost
                                ):
                                    hits[pair].append(
                                        aerial_shot(
                                            ball_location,
                                            intercept_time,
                                            best_shot_vector,
                                            slope,
                                        )
                                    )
                            elif (
                                backward_flag
                                and ball_location[2] <= 280
                                and slope > 0.25
                            ):
                                hits[pair].append(
                                    jump_shot(
                                        ball_location,
                                        intercept_time,
                                        best_shot_vector,
                                        slope,
                                        -1,
                                    )
                                )
    return hits
Ejemplo n.º 9
0
    def run(self, agent: _GoslingAgent):
        raw_time_remaining = self.intercept_time - agent.time
        # Capping raw_time_remaining above 0 to prevent division problems
        time_remaining = cap(raw_time_remaining, 0.001, 10.0)
        car_to_ball = self.ball_location - agent.me.location
        # whether we are to the left or right of the shot vector
        side_of_shot = sign(self.shot_vector.cross((0, 0, 1)).dot(car_to_ball))

        car_to_dodge_point = self.dodge_point - agent.me.location
        car_to_dodge_perp = car_to_dodge_point.cross(
            (0, 0, side_of_shot)
        )  # perpendicular
        distance_remaining = car_to_dodge_point.magnitude()

        speed_required = distance_remaining / time_remaining
        acceleration_required = backsolve(
            self.dodge_point, agent.me, time_remaining, 0 if not self.jumping else 650
        )
        local_acceleration_required = agent.me.local(acceleration_required)

        # The adjustment causes the car to circle around the dodge point in an effort to line up with the shot vector
        # The adjustment slowly decreases to 0 as the bot nears the time to jump
        adjustment = (
            car_to_dodge_point.angle(self.shot_vector) * distance_remaining / 2.0
        )  # size of adjustment
        adjustment *= (
            cap(
                self.jump_threshold - (acceleration_required[2]),
                0.0,
                self.jump_threshold,
            )
            / self.jump_threshold
        )  # factoring in how close to jump we are
        # we don't adjust the final target if we are already jumping
        final_target = (
            self.dodge_point
            + ((car_to_dodge_perp.normalize() * adjustment) if not self.jumping else 0)
            + Vector3(0, 0, 50)
        )
        # Ensuring our target isn't too close to the sides of the field, where our car would get messed up by the radius of the curves

        # Some adjustment to the final target to ensure it's inside the field and we don't try to dirve through any goalposts to reach it
        if abs(agent.me.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_final_target = agent.me.local(final_target - agent.me.location)

        # drawing debug lines to show the dodge point and final target (which differs due to the adjustment)
        agent.line(agent.me.location, self.dodge_point)
        agent.line(
            self.dodge_point - Vector3(0, 0, 100),
            self.dodge_point + Vector3(0, 0, 100),
            [255, 0, 0],
        )
        agent.line(
            final_target - Vector3(0, 0, 100),
            final_target + Vector3(0, 0, 100),
            [0, 255, 0],
        )

        # Calling our drive utils to get us going towards the final target
        angles = defaultPD(agent, local_final_target, self.direction)
        defaultThrottle(agent, speed_required, self.direction)

        agent.line(
            agent.me.location,
            agent.me.location + (self.shot_vector * 200),
            [255, 255, 255],
        )

        agent.controller.boost = (
            False
            if abs(angles[1]) > 0.3 or agent.me.airborne
            else agent.controller.boost
        )
        agent.controller.handbrake = (
            True
            if abs(angles[1]) > 2.3 and self.direction == 1
            else agent.controller.handbrake
        )

        if not self.jumping:
            if (
                raw_time_remaining <= 0.0
                or (speed_required - 2300) * time_remaining > 45
                or not shot_valid(agent, self)
            ):
                # If we're out of time or not fast enough to be within 45 units of target at the intercept time, we pop
                agent.pop()
                if agent.me.airborne:
                    agent.push(recovery())
            elif (
                local_acceleration_required[2] > self.jump_threshold
                and local_acceleration_required[2]
                > local_acceleration_required.flatten().magnitude()
            ):
                # Switch into the jump when the upward acceleration required reaches our threshold, and our lateral acceleration is negligible
                self.jumping = True
        else:
            if (
                (raw_time_remaining > 0.2 and not shot_valid(agent, self, 150))
                or raw_time_remaining <= -0.9
                or (not agent.me.airborne and self.counter > 0)
            ):
                agent.pop()
                agent.push(recovery())
            elif (
                self.counter == 0
                and local_acceleration_required[2] > 0.0
                and raw_time_remaining > 0.083
            ):
                # Initial jump to get airborne + we hold the jump button for extra power as required
                agent.controller.jump = True
            elif self.counter < 3:
                # make sure we aren't jumping for at least 3 frames
                agent.controller.jump = False
                self.counter += 1
            elif raw_time_remaining <= 0.1 and raw_time_remaining > -0.9:
                # dodge in the direction of the shot_vector
                agent.controller.jump = True
                if not self.dodging:
                    vector = agent.me.local(self.shot_vector)
                    self.p = abs(vector[0]) * -sign(vector[0])
                    self.y = abs(vector[1]) * sign(vector[1]) * self.direction
                    self.dodging = True
                # simulating a deadzone so that the dodge is more natural
                agent.controller.pitch = self.p if abs(self.p) > 0.2 else 0
                agent.controller.yaw = self.y if abs(self.y) > 0.3 else 0
Ejemplo n.º 10
0
    def run(self, agent: _GoslingAgent):
        raw_time_remaining = self.intercept_time - agent.time
        # Capping raw_time_remaining above 0 to prevent division problems
        time_remaining = cap(raw_time_remaining, 0.01, 10.0)

        car_to_ball = self.ball_location - agent.me.location
        # whether we are to the left or right of the shot vector
        side_of_shot = sign(self.shot_vector.cross((0, 0, 1)).dot(car_to_ball))

        car_to_intercept = self.intercept - agent.me.location
        car_to_intercept_perp = car_to_intercept.cross(
            (0, 0, side_of_shot)
        )  # perpendicular
        distance_remaining = car_to_intercept.flatten().magnitude()

        speed_required = distance_remaining / time_remaining
        # When still on the ground we pretend gravity doesn't exist, for better or worse
        acceleration_required = backsolve(
            self.intercept, agent.me, time_remaining, 0 if self.jump_time == 0 else 325
        )
        local_acceleration_required = agent.me.local(acceleration_required)

        # The adjustment causes the car to circle around the dodge point in an effort to line up with the shot vector
        # The adjustment slowly decreases to 0 as the bot nears the time to jump
        adjustment = (
            car_to_intercept.angle(self.shot_vector) * distance_remaining / 1.57
        )  # size of adjustment
        adjustment *= (
            cap(
                self.jump_threshold - (acceleration_required[2]),
                0.0,
                self.jump_threshold,
            )
            / self.jump_threshold
        )  # factoring in how close to jump we are
        # we don't adjust the final target if we are already jumping
        final_target = self.intercept + (
            (car_to_intercept_perp.normalize() * adjustment)
            if self.jump_time == 0
            else 0
        )

        # Some extra adjustment to the final target to ensure it's inside the field and we don't try to dirve through any goalposts to reach it
        if abs(agent.me.location[1]) > 5150:
            final_target[0] = cap(final_target[0], -750, 750)

        local_final_target = agent.me.local(final_target - agent.me.location)

        # drawing debug lines to show the dodge point and final target (which differs due to the adjustment)
        agent.line(agent.me.location, self.intercept)
        agent.line(
            self.intercept - Vector3(0, 0, 100),
            self.intercept + Vector3(0, 0, 100),
            [255, 0, 0],
        )
        agent.line(
            final_target - Vector3(0, 0, 100),
            final_target + Vector3(0, 0, 100),
            [0, 255, 0],
        )

        angles = defaultPD(agent, local_final_target)

        if self.jump_time == 0:
            defaultThrottle(agent, speed_required)
            agent.controller.boost = (
                False
                if abs(angles[1]) > 0.3 or agent.me.airborne
                else agent.controller.boost
            )
            agent.controller.handbrake = (
                True if abs(angles[1]) > 2.3 else agent.controller.handbrake
            )
            if acceleration_required[2] > self.jump_threshold:
                # Switch into the jump when the upward acceleration required reaches our threshold, hopefully we have aligned already...
                self.jump_time = agent.time
        else:
            time_since_jump = agent.time - self.jump_time

            # While airborne we boost if we're within 30 degrees of our local acceleration requirement
            if (
                agent.me.airborne
                and local_acceleration_required.magnitude() * time_remaining > 100
            ):
                angles = defaultPD(agent, local_acceleration_required)
                if abs(angles[0]) + abs(angles[1]) < 0.5:
                    agent.controller.boost = True
            if self.counter == 0 and (
                time_since_jump <= 0.2 and local_acceleration_required[2] > 0
            ):
                # hold the jump button up to 0.2 seconds to get the most acceleration from the first jump
                agent.controller.jump = True
            elif time_since_jump > 0.2 and self.counter < 3:
                # Release the jump button for 3 ticks
                agent.controller.jump = False
                self.counter += 1
            elif local_acceleration_required[2] > 300 and self.counter == 3:
                # the acceleration from the second jump is instant, so we only do it for 1 frame
                agent.controller.jump = True
                agent.controller.pitch = 0
                agent.controller.yaw = 0
                agent.controller.roll = 0
                self.counter += 1

        if raw_time_remaining < -0.25 or not shot_valid(agent, self):
            agent.pop()
            agent.push(recovery())