示例#1
0
        private Controller driveToLocation(rlbot.flat.GameTickPacket gameTickPacket, Controller controller, Vector3 location)
        {
            Vector3 carLocation = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Location.Value);

            rlbot.flat.Rotator carRotation = gameTickPacket.Players(this.index).Value.Physics.Value.Rotation.Value;

            // Stuck in goal.
            if (Math.Abs(carLocation.Y) > 5120)
            {
                location = new Vector3(Math.Min(800, Math.Max(-800, location.X)), location.Y, location.Z);
            }

            if (carLocation.Z < 120)
            {
                double botToLocationAngle      = Math.Atan2(location.Y - carLocation.Y, location.X - carLocation.X);
                double botFrontToLocationAngle = correctAngle(botToLocationAngle - carRotation.Yaw);

                float steer = (float)botFrontToLocationAngle * 2.5F;
                controller.Steer = steer;

                controller.Boost     = (Math.Abs(steer) < 0.12F && gameTickPacket.Players(this.index).Value.HasWheelContact&& !gameTickPacket.Players(this.index).Value.IsSupersonic);
                controller.Handbrake = (Math.Abs(steer) > 2.8F && gameTickPacket.Players(this.index).Value.HasWheelContact);
            }
            else
            {
                controller.Boost     = false;
                controller.Handbrake = false;
                controller.Steer     = carRotation.Roll * 10;
            }
            controller.Throttle = 1F;

            return(controller);
        }
示例#2
0
        /// <summary>
        /// Converts a flatbuffers rotator to a System.Numerics quaternion.
        /// </summary>
        /// <param name="rotator">The flatbuffers rotator</param>
        /// <returns>Returns a System.Numerics quaternion.</returns>
        public static Quaternion ToQuaternion(this rlbot.flat.Rotator rotator)
        {
            Quaternion roll  = Quaternion.CreateFromYawPitchRoll(0, -rotator.Roll, 0);
            Quaternion pitch = Quaternion.CreateFromYawPitchRoll(-rotator.Pitch, 0, 0);
            Quaternion yaw   = Quaternion.CreateFromYawPitchRoll(0, 0, rotator.Yaw);

            return(Quaternion.Multiply(yaw, Quaternion.Multiply(pitch, roll)));
        }
示例#3
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            // This controller object will be returned at the end of the method.
            // This controller will contain all the inputs that we want the bot to perform.
            Controller controller = new Controller();

            // Wrap gameTickPacket retrieving in a try-catch so that the bot doesn't crash whenever a value isn't present.
            // A value may not be present if it was not sent.
            // These are nullables so trying to get them when they're null will cause errors, therefore we wrap in try-catch.
            try
            {
                // Start printing for this frame.
                Console.Write(this.index + ": ");

                // Store the required data from the gameTickPacket.
                Vector3            ballLocation = fromFramework(gameTickPacket.Ball.Value.Physics.Value.Location.Value);
                Vector3            ballVelocity = fromFramework(gameTickPacket.Ball.Value.Physics.Value.Velocity.Value);
                Vector3            carLocation  = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Location.Value);
                Vector3            carVelocity  = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Velocity.Value);
                rlbot.flat.Rotator carRotation  = gameTickPacket.Players(this.index).Value.Physics.Value.Rotation.Value;
                Boolean            wheelContact = gameTickPacket.Players(this.index).Value.HasWheelContact;
                int team = gameTickPacket.Players(this.index).Value.Team;

                // Wildfire?
                Boolean teammate = hasTeammate(gameTickPacket);
                rlbot.flat.PlayerInfo?wildfire = getTeammate(gameTickPacket, carLocation);
                double wildfireDistanceBall    = (teammate ? getDistance2D(((rlbot.flat.PlayerInfo)wildfire).Physics.Value.Location.Value.X, ballLocation.X, ((rlbot.flat.PlayerInfo)wildfire).Physics.Value.Location.Value.Y, ballLocation.Y) : Double.MaxValue);

                // Quick-chat.
                int goalsScored = gameTickPacket.Players(this.index).Value.ScoreInfo.Value.Goals;
                if (goalsScored > this.goals)
                {
                    SendQuickChatFromAgent(false, rlbot.flat.QuickChatSelection.Compliments_NiceOne);
                    Task.Delay(750).ContinueWith(t => SendQuickChatFromAgent(false, rlbot.flat.QuickChatSelection.Compliments_Thanks));
                    Task.Delay(1500).ContinueWith(t => SendQuickChatFromAgent(false, rlbot.flat.QuickChatSelection.Apologies_NoProblem));
                }
                this.goals = goalsScored;

                // Get the ball prediction data.
                rlbot.flat.BallPrediction prediction = GetBallPrediction();

                // Determine which way we are shooting (positive = blue, negative = orange).
                int     teamSign  = (team == 0 ? 1 : -1);
                Boolean wrongSide = carLocation.Y * teamSign > ballLocation.Y * teamSign;

                // Determine where the goals are.
                Vector3 enemyGoal = new Vector3(0F, 5120F * teamSign, 0F);
                Vector3 homeGoal  = new Vector3(0F, -5120F * teamSign, 0F);

                // Make a dodge boolean, this'll come in handy later.
                Boolean dodge = false;

                // Calculate the distance from the car to the ball.
                var distanceToBall = getDistance2D(carLocation.X, ballLocation.X, carLocation.Y, ballLocation.Y);
                Console.Write((int)distanceToBall + " = ball distance");

                // The earliest point we can hit the ball on its predicted path.
                Vector3 hitPoint = getHitPoint(gameTickPacket, prediction);

                // Kickoff.
                Boolean kickoff = (ballLocation.X == 0 && ballLocation.Y == 0 && ballVelocity.X == 0 && ballVelocity.Y == 0 && ballVelocity.Z == 0);
                if (kickoff)
                {
                    activeState = "Kickoff";

                    if (wildfireDistanceBall < distanceToBall && Math.Abs(carLocation.Y) > 3000)
                    {
                        controller.Throttle  = Math.Sign(Math.Abs(carLocation.Y) - 5120);
                        controller.Boost     = false;
                        controller.Handbrake = false;
                    }
                    else
                    {
                        Vector3 targetLocation = new Vector3(0, Math.Abs(carLocation.Y) > 3600 ? carLocation.Y + teamSign * 900 : 0, 0);
                        controller           = driveToLocation(gameTickPacket, controller, targetLocation);
                        controller.Boost     = true;
                        controller.Handbrake = false;
                        dodge = Math.Abs(controller.Steer) < 0.4F && distanceToBall < (carLocation.X == 0 ? 3000 : 2850);
                    }
                }
                else if (Math.Abs(ballLocation.X) > 1800 && wildfireDistanceBall < distanceToBall - 800 && teamSign * ballLocation.Y > 1000 && teammate)
                {
                    // Recieve the pass!
                    activeState = "Lurking";
                    Vector3 targetLocation = new Vector3(-ballLocation.X / 3, enemyGoal.Y - (enemyGoal.Y - ballLocation.Y) * 2, 0);
                    controller = driveToLocation(gameTickPacket, controller, targetLocation);
                    dodge      = (distanceToBall > 3500 && !gameTickPacket.Players(this.index).Value.IsSupersonic&& gameTickPacket.Players(this.index).Value.Boost < 10);
                }
                else
                {
                    Boolean grabBoost = (distanceToBall > (teammate ? 2750 : 3750) && gameTickPacket.Players(this.index).Value.Boost < 40 && !wrongSide);
                    Vector3?boost     = (grabBoost ? getClosestBoost(gameTickPacket, carLocation) : null);
                    if (boost != null)
                    {
                        //Grab boost
                        activeState      = "Boost";
                        controller       = driveToLocation(gameTickPacket, controller, (Vector3)boost);
                        controller.Boost = (Vector3.Distance((Vector3)boost, carLocation) > 2000);
                    }
                    else if (ballLocation.Z < (distanceToBall < 400 ? 140 : 250))
                    {
                        // Defending.
                        double defendingThreshold = (teammate ? 2.5D : 3.25D);
                        double ballAngle          = correctAngle(Math.Atan2(ballLocation.Y - carLocation.Y, ballLocation.X - carLocation.X) - carRotation.Yaw);
                        double enemyGoalAngle     = correctAngle(Math.Atan2(enemyGoal.Y - carLocation.Y, enemyGoal.X - carLocation.X) - carRotation.Yaw);
                        if (Math.Abs(ballAngle) + Math.Abs(enemyGoalAngle) > defendingThreshold && (Math.Abs(ballVelocity.Y) < 1200 || wrongSide) && distanceToBall > 800)
                        {
                            activeState      = "Defending";
                            controller       = driveToLocation(gameTickPacket, controller, getDistance2D(carLocation, homeGoal) < 1250 || teamSign * carLocation.Y < -5120 ? ballLocation : homeGoal);
                            dodge            = Math.Abs(controller.Steer) < 0.2F && teamSign * carLocation.Y > -3000 && (gameTickPacket.Players(this.index).Value.Boost < 10 || !wrongSide);
                            controller.Boost = (controller.Boost && wrongSide);
                        }
                        else
                        {
                            // Attacking.
                            activeState = "Attacking";

                            // Get the target location so we can shoot the ball towards the opponent's goal.
                            double  distance      = getDistance2D(hitPoint, carLocation);
                            Vector3 carToHitPoint = Vector3.Subtract(hitPoint, carLocation);
                            double  offset        = Math.Max(0, Math.Min(0.28, 0.03 * Math.Abs(carToHitPoint.Y) / Math.Abs(carToHitPoint.X)));
                            Console.Write(", " + (float)offset + " = offset");

                            Vector3 goalToHitPoint = Vector3.Subtract(enemyGoal, hitPoint);
                            Vector3 targetLocation = Vector3.Add(hitPoint, Vector3.Multiply(Vector3.Normalize(goalToHitPoint), (float)-(92.75 + distance * offset)));

                            Renderer.DrawLine3D(Color.FromRgb(100, 255, 200), hitPoint, targetLocation);
                            Renderer.DrawLine3D(Color.FromRgb(100, 255, 200), carLocation, targetLocation);
                            Renderer.DrawLine3D(Color.FromRgb(100, 255, 200), carLocation, hitPoint);

                            controller = driveToLocation(gameTickPacket, controller, targetLocation);

                            // Two conditions for dodging when attacking:
                            // 1: to get closer to the ball.
                            // 2: to hit the ball.
                            dodge = false;
                            if (Math.Abs(controller.Steer) < 0.2F && gameTickPacket.Players(this.index).Value.Boost < 10)
                            {
                                if (distanceToBall > 2400 && !gameTickPacket.Players(this.index).Value.IsSupersonic)
                                {
                                    dodge = true;
                                }
                                else if (distanceToBall < 380 && ballLocation.Z < 180 && Math.Abs(ballAngle - enemyGoalAngle) < 0.4 && ballVelocity.Length() > 1500 && Math.Abs(ballAngle) < 0.3)
                                {
                                    // Towards the ball.
                                    dodge            = true;
                                    controller.Steer = (float)ballAngle;
                                }
                            }
                        }
                    }
                    else
                    {
                        // Catching the ball.
                        activeState = "Catching";

                        // Determine the time the ball will take to touch the ground
                        double u    = ballVelocity.Z;
                        double a    = -650;
                        double s    = -(ballLocation.Z - 92.75);
                        double time = (-u - Math.Sqrt(Math.Pow(u, 2) + 2 * a * s)) / a;
                        Console.Write(", " + (float)time + " = time");

                        Vector3 bounceLocation = getBounceLocation(prediction);

                        // Add an offset so we dribble towards the enemy goal.
                        Vector3 bounceOffset = Vector3.Multiply(Vector3.Normalize(Vector3.Subtract(enemyGoal, bounceLocation)), -60);
                        bounceLocation = Vector3.Add(bounceLocation, bounceOffset);

                        controller = driveToLocationInTime(gameTickPacket, controller, bounceLocation, time);

                        if (time < 3.2 && getDistance2D(bounceLocation, carLocation) < 180 && Math.Abs(bounceLocation.X) < 800 && Math.Abs(bounceLocation.Y) > 4850 && teamSign * carVelocity.Y >= 0)
                        {
                            // Jump when the ball is near the goal.
                            dodge           = false;
                            controller.Jump = DateTimeOffset.Now.ToUnixTimeMilliseconds() % 500 > 100;
                        }
                        else
                        {
                            dodge = gameTickPacket.Players(this.index).Value.Boost < 1 && getDistance2D(bounceLocation, carLocation) > 2900;
                        }
                    }
                }
                Console.Write(", " + (float)controller.Throttle + " = throttle");
                Console.Write(", " + (float)controller.Steer + " = steer");

                // Land on wheels.
                if (carLocation.Z > 200 && !isDodging(gameTickPacket))
                {
                    activeState = "Recovery";
                    float proportion = 0.8F;
                    controller.Roll  = (float)carRotation.Roll * -proportion;
                    controller.Pitch = (float)carRotation.Pitch * -proportion;
                    controller.Boost = false;
                }

                // Handles dodging.
                Console.Write(", " + (dodgeWatch.ElapsedMilliseconds / 1000F) + "s dodge");
                if (isDodging(gameTickPacket))
                {
                    // Get the controller required for the dodge.
                    /** activeState = "Dodge"; */
                    controller = getDodgeOutput(controller, controller.Steer);
                }
                else if (dodge && canDodge(gameTickPacket) && wheelContact)
                {
                    // Begin a new dodge.
                    dodgeWatch.Restart();
                }

                // Render the active state.
                Renderer.DrawString3D(activeState, (team == 0 ? Color.FromRgb(0, 191, 255) : Color.FromRgb(255, 165, 0)), carLocation, 2, 2);

                // End the line printed this frame.
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            return(clampControlValues(controller));
        }
示例#4
0
 public static Rotation ToRotation(rlbot.flat.Rotator rotator)
 {
     return(new Rotation(rotator.Yaw, rotator.Pitch, rotator.Roll));
 }