Ejemplo n.º 1
0
        public GameState InitializeGameState(rlbot.flat.GameTickPacket gameTickPacket)
        {
            GameState gameState = new GameState();

            gameState.packet      = new Packet(gameTickPacket);
            gameState.ballPhysics = gameState.packet.Ball.Physics;

            if (gameState.packet.Players.Length == 0)
            {
                return(null);
            }

            gameState.myCar            = new MyCarState(gameState.packet.Players[index]);
            gameState.fieldState       = new FieldState();
            gameState.fieldState.field = GetFieldInfo();

            gameState.fieldState.ownGoalLocation   = gameState.fieldState.field.Goals[gameState.myCar.player.Team].Location;
            gameState.fieldState.oppGoalLocation   = gameState.fieldState.field.Goals[Math.Abs(gameState.myCar.player.Team - 1)].Location;
            gameState.fieldState.ownGoalLocation.Z = 0;
            gameState.fieldState.oppGoalLocation.Z = 0;
            gameState.ballPrediction = GetBallPrediction();

            gameState.myCar.distanceToOwnGoal = Utils.DistanceBetween(gameState.myCar.player.Physics.Location, gameState.fieldState.ownGoalLocation);
            gameState.myCar.distanceToOppGoal = Utils.DistanceBetween(gameState.myCar.player.Physics.Location, gameState.fieldState.oppGoalLocation);

            return(gameState);
        }
Ejemplo n.º 2
0
        // TODO: Populate extra fields using the bot param.
        public Packet(rlbot.flat.GameTickPacket packet, Bot bot, Renderer renderer)
        {
            PlayerIndex = bot.index;
            Renderer    = renderer;

            Players = new Player[packet.PlayersLength];
            for (int i = 0; i < packet.PlayersLength; i++)
            {
                Players[i] = new Player(packet.Players(i).Value);
            }

            BoostPadStates = new BoostPadState[packet.BoostPadStatesLength];
            for (int i = 0; i < packet.BoostPadStatesLength; i++)
            {
                BoostPadStates[i] = new BoostPadState(packet.BoostPadStates(i).Value);
            }

            Ball     = new Ball(packet.Ball.Value);
            GameInfo = new GameInfo(packet.GameInfo.Value);

            Teams = new TeamInfo[packet.TeamsLength];
            for (int i = 0; i < packet.TeamsLength; i++)
            {
                Teams[i] = new TeamInfo(packet.Teams(i).Value);
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        private Controller driveToLocationInTime(rlbot.flat.GameTickPacket gameTickPacket, Controller controller, Vector3 location, double time)
        {
            Vector3 carLocation = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Location.Value);
            Vector3 carVelocity = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Velocity.Value);

            // Get the default driving controller
            controller = driveToLocation(gameTickPacket, controller, location);

            // Handling the speed
            double distance     = getDistance2D(carLocation.X, location.X, carLocation.Y, location.Y);
            double targetSpeed  = (distance / time);
            double currentSpeed = carVelocity.Length();

            if (targetSpeed > currentSpeed)
            {
                controller.Throttle = 1;
                controller.Boost    = ((targetSpeed > 1410 || targetSpeed > currentSpeed + 800) && carLocation.Z < 30 && Math.Abs(controller.Steer) < 0.5);
            }
            else
            {
                controller.Boost = false;
                if (targetSpeed < currentSpeed - 400)
                {
                    controller.Throttle = -1;
                }
                else
                {
                    controller.Throttle = 0;
                }
            }

            return(controller);
        }
Ejemplo n.º 5
0
        // Returns a hittable point on the ball.
        private Vector3 getHitPoint(rlbot.flat.GameTickPacket gameTickPacket, rlbot.flat.BallPrediction prediction)
        {
            Vector3 carLocation = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Location.Value);
            double  u           = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Velocity.Value).Length();

            // Estimate the maximum velocity.
            double maxV = Math.Max(1410, Math.Min(2300, u + 150 * gameTickPacket.Players(this.index).Value.Boost));

            for (int i = 0; i < prediction.SlicesLength; i++)
            {
                Vector3 point = fromFramework(prediction.Slices(i).Value.Physics.Value.Location.Value);

                double s = Vector3.Distance(point, carLocation) - 92.75;
                double t = (double)i / 60D;
                double v = 2D * (s / t) - u;
                double a = (Math.Pow(v, 2) - Math.Pow(u, 2)) / (2 * s);

                if (v <= maxV && a < 1700) // Approximate max acceleration.
                {
                    renderPrediction(prediction, 0, i, System.Windows.Media.Color.FromRgb(255, 255, 255));
                    return(point);
                }
            }
            return(fromFramework(prediction.Slices(0).Value.Physics.Value.Location.Value));
        }
Ejemplo n.º 6
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            gamePad.Update();
            Controller controller = GamePad.GenerateControlsDefault(gamePad);

            return(controller);
        }
Ejemplo n.º 7
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            gameInfo.Update(gameTickPacket, GetRigidBodyTick());
            ballPrediction.Update(GetBallPrediction());

            if (timeout > 5)
            {
                GameState gamestate = new GameState();
                gamestate.BallState.PhysicsState.Location        = new DesiredVector3(RandomFloat(-3000, 3000), 2000, 100);
                gamestate.BallState.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                gamestate.BallState.PhysicsState.Velocity        = new DesiredVector3(RandomFloat(2000, 2000), RandomFloat(1000, 2000), RandomFloat(1500, 1600));

                CarState carstate = new CarState();
                carstate.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Velocity        = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Location        = new DesiredVector3(0, 0, 17);
                carstate.PhysicsState.Rotation        = new DesiredRotator(0, (float)(Math.PI / 2), 0);

                gamestate.SetCarState(index, carstate);

                SetGameState(gamestate);

                timeout = 0;
                aerial  = null;
            }

            timeout += gameInfo.DeltaTime;

            Controller controller = new Controller();

            Car  car  = gameInfo.Cars[index];
            Ball ball = gameInfo.Ball;

            Slice[] slices = ballPrediction.ToArray();

            if (timeout > 0.3)
            {
                if (aerial == null)
                {
                    aerial = Aerial.FindAerialOpportunity(car, slices, gameInfo.Time, settings);
                }
                else
                {
                    controller = aerial.Step(ball, 0.016667f, gameInfo.Time);
                    Renderer.DrawLine3D(Colors.Red, car.Position, aerial.Target);

                    if (aerial.Finished)
                    {
                        aerial = null;
                    }
                    else
                    {
                        aerial.UpdateAerialTarget(slices, gameInfo.Time);
                    }
                }
            }

            return(controller);
        }
Ejemplo n.º 8
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            // We process the gameTickPacket and convert it to our own internal data structure.
            Packet packet = new Packet(gameTickPacket, this, Renderer);

            _decisionMaker.Tick(packet);

            return(_skillManager.Tick(packet));
        }
Ejemplo n.º 9
0
 public static Ball ParseFromGamePacket(rlbot.flat.GameTickPacket packet)
 {
     rlbot.flat.BallInfo info    = packet.Ball.GetValueOrDefault();
     rlbot.flat.Physics  physics = info.Physics.Value;
     return(new Ball()
     {
         Location = Conversion.ToVector3(physics.Location.GetValueOrDefault()),
         Rotation = Conversion.ToRotation(physics.Rotation.GetValueOrDefault()),
         Velocity = Conversion.ToVector3(physics.Velocity.GetValueOrDefault()),
         AngularVelocity = Conversion.ToVector3(physics.AngularVelocity.GetValueOrDefault()),
     });
 }
Ejemplo n.º 10
0
 private Boolean hasTeammate(rlbot.flat.GameTickPacket gameTickPacket)
 {
     for (int i = 0; i < gameTickPacket.PlayersLength; i++)
     {
         if (i == this.index)
         {
             continue;                  // This is Atlas!
         }
         if (gameTickPacket.Players(i).Value.Team == this.team)
         {
             return(true);
         }
     }
     return(false); // No teammate.
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Updates the GameInfo to reflect the new GameTickPacket and RigidBodyTick.
        /// </summary>
        /// <param name="packet">The GameTickPacket object.</param>
        /// <param name="rigidBodyTick">The RigidBodyTick object.</param>
        public void Update(rlbot.flat.GameTickPacket packet, rlbot.flat.RigidBodyTick rigidBodyTick)
        {
            Update(packet);

            if (rigidBodyTick.Ball.HasValue)
            {
                Ball.Update(rigidBodyTick.Ball.Value);
            }

            for (int i = 0; i < packet.PlayersLength; i++)
            {
                if (rigidBodyTick.Players(i).HasValue)
                {
                    Cars[i].Update(rigidBodyTick.Players(i).Value, DeltaTime);
                }
            }
        }
Ejemplo n.º 12
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            gameInfo.Update(gameTickPacket);

            if (timeout > 5)
            {
                KickOffStruct k = kickOffFrontCorner;

                GameState gamestate = new GameState();
                gamestate.BallState.PhysicsState.Location        = new DesiredVector3(0, 0, 100);
                gamestate.BallState.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                gamestate.BallState.PhysicsState.Velocity        = new DesiredVector3(0, 0, 0);

                CarState carstate = new CarState();
                carstate.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Velocity        = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Location        = new DesiredVector3(new Vector3(k.Position, 17));
                carstate.PhysicsState.Rotation        = new DesiredRotator(0, k.Yaw, 0);
                carstate.Boost = 33;

                gamestate.SetCarState(index, carstate);

                SetGameState(gamestate);

                timeout = 0;
                kickoff = null;
            }

            timeout += gameInfo.DeltaTime;

            Controller controller = new Controller();

            if (timeout > 2)
            {
                if (kickoff == null)
                {
                    kickoff = new KickOff(gameInfo.Cars[index]);
                }

                controller = kickoff.Step(0.0166667f);
            }

            return(controller);
        }
Ejemplo n.º 13
0
        private Vector3?getClosestBoost(rlbot.flat.GameTickPacket gameTickPacket, Vector3 carLocation)
        {
            Vector3?closest         = null;
            double  closestDistance = 0;

            for (int i = 0; i < gameTickPacket.BoostPadStatesLength; i++)
            {
                rlbot.flat.BoostPadState boostPadState = (rlbot.flat.BoostPadState)gameTickPacket.BoostPadStates(i);
                rlbot.flat.BoostPad      boostPosition = (rlbot.flat.BoostPad)GetFieldInfo().BoostPads(i);
                double boostDistance = getDistance2D(carLocation.X, boostPosition.Location.Value.X, carLocation.Y, boostPosition.Location.Value.Y);

                if (boostPadState.IsActive && boostPosition.IsFullBoost && (closest == null || closestDistance > boostDistance))
                {
                    closestDistance = boostDistance;
                    closest         = fromFramework((rlbot.flat.Vector3)boostPosition.Location);
                }
            }
            return(closest);
        }
Ejemplo n.º 14
0
        // private Vector3 getBounceLocation(rlbot.flat.BallPrediction prediction)
        //{
        //     for (int i = 0; i < prediction.SlicesLength; i++)
        //     {
        //         Vector3 point = fromFramework(prediction.Slices(i).Value.Physics.Value.Location.Value);
        //         if (point.Z < 125)
        //         {
        //             renderPrediction(prediction, 0, i, System.Windows.Media.Color.FromRgb(255, 0, 255));
        //             return point;
        //         }
        //     }
        //     return fromFramework(prediction.Slices(0).Value.Physics.Value.Location.Value);
        // }


        //private Vector3 getHitPoint(rlbot.flat.GameTickPacket gameTickPacket, rlbot.flat.BallPrediction prediction)
        //{
        //    Vector3 carLocation = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Location.Value);
        //    double u = fromFramework(gameTickPacket.Players(this.index).Value.Physics.Value.Velocity.Value).Length();

        //    // Estimate the maximum velocity.
        //    double maxV = Math.Max(1410, Math.Min(2300, u + 150 * gameTickPacket.Players(this.index).Value.Boost));

        //    for (int i = 0; i < prediction.SlicesLength; i++)
        //    {
        //        Vector3 point = fromFramework(prediction.Slices(i).Value.Physics.Value.Location.Value);

        //        double s = Vector3.Distance(point, carLocation) - 92.75;
        //        double t = (double)i / 60D;
        //        double v = 2D * (s / t) - u;
        //        double a = (Math.Pow(v, 2) - Math.Pow(u, 2)) / (2 * s);

        //        if (v <= maxV && a < 1700) // Approximate max acceleration.
        //        {
        //            renderPrediction(prediction, 0, i, System.Windows.Media.Color.FromRgb(255, 255, 255));
        //            return point;
        //        }
        //    }
        //    return fromFramework(prediction.Slices(0).Value.Physics.Value.Location.Value);
        //}

        private Vector3?getClosestBoost(rlbot.flat.GameTickPacket gameTickPacket, Vector3 carLocation)
        {
            Vector3?closest         = null;
            double  closestDistance = 0;

            for (int i = 0; i < gameTickPacket.BoostPadStatesLength; i++)
            {
                rlbot.flat.BoostPadState boostPadState = (rlbot.flat.BoostPadState)gameTickPacket.BoostPadStates(i);
                Vector3 boostPosition = GetFieldInfo().BoostPads[i].Location;
                double  boostDistance = Utils.getDistance2D(carLocation.X, boostPosition.X, carLocation.Y, boostPosition.Y);

                if (boostPadState.IsActive && /*boostPosition.IsFullBoost && */ (closest == null || closestDistance > boostDistance))
                {
                    closestDistance = boostDistance;
                    closest         = boostPosition;
                }
            }
            return(closest);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Updates the GameInfo to reflect the new GameTickPacket.
        /// </summary>
        /// <param name="packet">The GameTickPacket object.</param>
        public void Update(rlbot.flat.GameTickPacket packet)
        {
            if (packet.GameInfo.HasValue)
            {
                DeltaTime = packet.GameInfo.Value.SecondsElapsed - Time;
                Time      = packet.GameInfo.Value.SecondsElapsed;

                IsRoundActive = packet.GameInfo.Value.IsRoundActive;

                if (IsKickOffPause == false && packet.GameInfo.Value.IsKickoffPause == true)
                {
                    KickOffStarted(new EventArgs());
                }

                IsKickOffPause = packet.GameInfo.Value.IsKickoffPause;
            }

            if (packet.Ball.HasValue)
            {
                Ball.Update(packet.Ball.Value);
            }

            if (Cars == null || Cars.Length != packet.PlayersLength)
            {
                Cars = new Car[packet.PlayersLength];
            }

            for (int i = 0; i < packet.PlayersLength; i++)
            {
                if (Cars[i] == null)
                {
                    Cars[i] = new Car();
                }

                if (packet.Players(i).HasValue)
                {
                    Cars[i].Update(packet.Players(i).Value, DeltaTime);
                }
            }

            MyCar = Cars[index];
        }
Ejemplo n.º 16
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket, int index)
        {
            // We process the gameTickPacket and convert it to our own internal data structure.
            Packet packet = new Packet(gameTickPacket);

            // Get the data required to drive to the ball.
            Vector3     ballLocation = packet.Ball.Physics.Location;
            Vector3     carLocation  = packet.Players[index].Physics.Location;
            Orientation carRotation  = packet.Players[index].Physics.Rotation;

            // Find where the ball is relative to us.
            Vector3 ballRelativeLocation = Orientation.RelativeLocation(carLocation, ballLocation, carRotation);

            // Decide which way to steer in order to get to the ball.
            // If the ball is to our left, we steer left. Otherwise we steer right.
            float steer;

            if (ballRelativeLocation.Y > 0)
            {
                steer = 1;
            }
            else
            {
                steer = -1;
            }

            // Examples of rendering in the game
            object p = Renderer.DrawString3D("Ball", Colors.Black, ballLocation, 3, 3);

            Renderer.DrawString3D(steer > 0 ? "Right" : "Left", Colors.Aqua, carLocation, 3, 3);
            Renderer.DrawLine3D(Colors.Red, carLocation, ballLocation);

            // This controller will contain all the inputs that we want the bot to perform.
            return(new Controller
            {
                // Set the throttle to 1 so the bot can move.
                Throttle = 1,
                Steer = steer
            });
        }
Ejemplo n.º 17
0
        private rlbot.flat.PlayerInfo?getTeammate(rlbot.flat.GameTickPacket gameTickPacket, Vector3 carLocation)
        {
            rlbot.flat.PlayerInfo?closest = null;
            Vector3 ballLocation          = fromFramework(gameTickPacket.Ball.Value.Physics.Value.Location.Value);
            double  closestDistance       = 0;

            for (int i = 0; i < gameTickPacket.PlayersLength; i++)
            {
                if (i == this.index)
                {
                    continue;                  // This is Atlas!
                }
                Vector3 teammateLocation = fromFramework(gameTickPacket.Players(i).Value.Physics.Value.Location.Value);
                double  distance         = getDistance2D(ballLocation.X, teammateLocation.X, ballLocation.Y, teammateLocation.Y);
                if (gameTickPacket.Players(i).Value.Team == this.team && (closest == null || closestDistance > distance))
                {
                    closest         = (rlbot.flat.PlayerInfo)gameTickPacket.Players(i);
                    closestDistance = distance;
                }
            }
            return(closest);
        }
Ejemplo n.º 18
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            // We process the gameTickPacket and convert it to our own internal data structure.
            Packet packet = new Packet(gameTickPacket);


            Vector3     ballLocation = packet.Ball.Physics.Location;
            Vector3     carLocation  = packet.Players[index].Physics.Location;
            Orientation carRotation  = packet.Players[index].Physics.Rotation;



            Vector3 ballRelativeLocation = Orientation.RelativeLocation(carLocation, ballLocation, carRotation);

            float steer;

            if (ballRelativeLocation.Y > 0)
            {
                steer = 1;
            }
            else
            {
                steer = -1;
            }

            // Examples of rendering in the game
            Renderer.DrawString3D("Ball", Colors.Black, ballLocation, 3, 3);
            Renderer.DrawString3D(steer > 0 ? "Right" : "Left", Colors.Aqua, carLocation, 3, 3);
            Renderer.DrawLine3D(Colors.Red, carLocation, ballLocation);



            count++;
            if (count > 120)
            {
                if (toggle)
                {
                    move = new Move(BlueprintCreator.waveDash());
                }
                else
                {
                    move = new Move(BlueprintCreator.waveDash());
                }

                move.start();

                toggle = !toggle;
                count  = 0;
            }

            Input input = move.get();

            if (input.frame != -1)
            {
                return(new Controller {
                    Throttle = 0,
                    Pitch = input.pitch,
                    Roll = input.roll,
                    Jump = input.jump,
                    Yaw = input.yaw,
                    Steer = input.stear
                });
            }

            float throttle = 1;

            if (toggle)
            {
                throttle = 1;
            }


            return(new Controller {
                Throttle = throttle,

                Steer = 0
            });
        }
Ejemplo n.º 19
0
 // Tells us whether the bot is eligible to perform a dodge or not.
 private Boolean canDodge(rlbot.flat.GameTickPacket gameTickPacket)
 {
     return(dodgeWatch.ElapsedMilliseconds >= 2000 && gameTickPacket.Players(this.index).Value.HasWheelContact);
 }
Ejemplo n.º 20
0
 // Tells us whether the bot is dodging or not.
 private Boolean isDodging(rlbot.flat.GameTickPacket gameTickPacket)
 {
     return(dodgeWatch.ElapsedMilliseconds <= (gameTickPacket.Players(this.index).Value.HasWheelContact ? 600 : 1000));
 }
Ejemplo n.º 21
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));
        }
Ejemplo n.º 22
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            // Convert the current frame data into our own structures and helpers.
            GameState gameState = InitializeGameState(gameTickPacket);

            if (gameState == null)
            {
                return(new Controller());
            }

            BotCalculations botCalculations = new BotCalculations
            {
                controller = new Controller()
            };

            botCalculations.predictedBallLocation = gameState.GetPredictionLocation();

            botCalculations.primaryMechanic = currentMechanic;

            // Decide on a tactic and run it.
            if (currentMechanic == null)
            {
                currentTactic = ChooseTactic(gameState, botCalculations);
            }
            currentTactic.DoWork(gameState, botCalculations);

            currentMechanic = botCalculations.primaryMechanic;

            // Examples of rendering in the game
            Renderer.DrawString3D("Ball", Colors.Black, botCalculations.predictedBallLocation, 3, 3);
            if (botCalculations.controller.Steer != 0)
            {
                Renderer.DrawString3D(botCalculations.controller.Steer > 0 ? "Right" : "Left", Colors.Aqua, gameState.myCar.player.Physics.Location, 3, 3);
            }
            if (botCalculations.controller.Jump)
            {
                Renderer.DrawString3D("Jump", Colors.Green, gameState.myCar.player.Physics.Location, 3, 3);
            }

            Color tacticColor = Colors.Red;

            switch (currentTactic.tacticType)
            {
            case TacticType.Attack:
                tacticColor = Colors.Magenta;
                break;

            case TacticType.Defend:
                tacticColor = Colors.Purple;
                break;

            case TacticType.Patience:
                tacticColor = Colors.DarkGreen;
                break;
            }

            Renderer.DrawLine3D(tacticColor, gameState.myCar.player.Physics.Location, botCalculations.primaryTarget);
            Renderer.DrawCenteredRectangle3D(tacticColor, botCalculations.primaryTarget, 50, 50, false);
            Renderer.DrawCenteredRectangle3D(Colors.LimeGreen, botCalculations.predictedBallLocation, 20, 20, false);
            Renderer.DrawString2D(currentTactic.tacticType.ToString(), Colors.WhiteSmoke, new Vector2(), 5, 5);

            return(botCalculations.controller);
        }
Ejemplo n.º 23
0
        public override Controller GetOutput(rlbot.flat.GameTickPacket gameTickPacket)
        {
            gameInfo.Update(gameTickPacket, GetRigidBodyTick());
            ballPrediction.Update(GetBallPrediction());

            Controller controller = new Controller();

            if (timeout > 6)
            {
                double angle = random.NextDouble() * Math.PI * 2;

                GameState gamestate = new GameState();
                gamestate.BallState.PhysicsState.Location        = new DesiredVector3(700 * (float)Math.Cos(angle), 700 * (float)Math.Sin(angle), 100);
                gamestate.BallState.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                gamestate.BallState.PhysicsState.Velocity        = new DesiredVector3(300 * (float)Math.Cos(angle), 300 * (float)Math.Sin(angle), 1500);

                CarState carstate = new CarState();
                carstate.PhysicsState.AngularVelocity = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Velocity        = new DesiredVector3(0, 0, 0);
                carstate.PhysicsState.Location        = new DesiredVector3(0, 0, 17);
                carstate.PhysicsState.Rotation        = new DesiredRotator(0, (float)(Math.PI / 2), 0);

                gamestate.SetCarState(index, carstate);

                SetGameState(gamestate);

                timeout       = 0;
                flipReset     = null;
                recovery      = null;
                recoveryState = false;
            }

            Car car = gameInfo.Cars[index];

            Slice[] slices = ballPrediction.ToArray();

            if (timeout > 0.3)
            {
                if (flipReset == null)
                {
                    for (int i = 0; i < slices.Length; i++)
                    {
                        if (slices[i].Velocity.Z < 100)
                        {
                            Vector3 A = FlipReset.CalculateCourse(car, slices[i].Position, slices[i].Time - gameInfo.Time);

                            if (A.Length() < 800 && A.Length() > 700)
                            {
                                flipReset = new FlipReset(car, slices[i].Position, gameInfo.Time, slices[i].Time);
                            }
                        }
                    }
                }

                if (flipReset != null && !recoveryState)
                {
                    controller = flipReset.Step(gameInfo.Ball, 1f / 60f, gameInfo.Time);

                    Car     c         = new Car(car);
                    Vector3 lastpoint = c.Position;

                    for (int i = 0; i < ((flipReset.ArrivalTime - gameInfo.Time) / (5f / 60f)); i++)
                    {
                        c.Simulate(new Controller(), 5f / 60f);

                        Renderer.DrawLine3D(Colors.White, c.Position, lastpoint);
                        lastpoint = c.Position;
                    }

                    if (flipReset.Finished)
                    {
                        controller.Roll  = 0;
                        controller.Pitch = -1;
                        controller.Yaw   = 0;

                        controller.Jump = true;
                        recoveryState   = true;
                        recovery        = new Recovery(car);
                    }
                }
                else if (recovery != null)
                {
                    controller = recovery.Step(1f / 60f);
                }
            }

            timeout += gameInfo.DeltaTime;

            return(controller);
        }