Advertisement
Guest User

SampleBot.java

a guest
Apr 8th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. package rlbotexample;
  2.  
  3. import rlbot.Bot;
  4. import rlbot.ControllerState;
  5. import rlbot.cppinterop.RLBotDll;
  6. import rlbot.cppinterop.RLBotInterfaceException;
  7. import rlbot.flat.BallPrediction;
  8. import rlbot.flat.GameTickPacket;
  9. import rlbot.flat.QuickChatSelection;
  10. import rlbot.manager.BotLoopRenderer;
  11. import rlbot.render.Renderer;
  12. import rlbotexample.boost.BoostManager;
  13. import rlbotexample.input.DataPacket;
  14. import rlbotexample.input.car.CarData;
  15. import rlbotexample.output.ControlsOutput;
  16. import rlbotexample.prediction.BallPredictionHelper;
  17. import rlbotexample.vector.Vector2;
  18. import rlbotexample.vector.Vector3;
  19.  
  20. import java.awt.*;
  21.  
  22. public class SampleBot implements Bot {
  23.  
  24.  
  25.     private final int playerIndex;
  26.  
  27.     public SampleBot(int playerIndex) {
  28.         this.playerIndex = playerIndex;
  29.     }
  30.  
  31.     /**
  32.      * This is where we keep the actual bot logic. This function shows how to chase the ball.
  33.      * Modify it to make your bot smarter!
  34.      */
  35.  
  36.  
  37.     private ControlsOutput processInput(DataPacket input) {
  38.        
  39.         Vector2 ballPosition = input.ball.position.flatten();
  40.         CarData myCar = input.car;
  41.         Vector2 carPosition = myCar.position.flatten();
  42.         Vector2 carDirection = myCar.orientation.noseVector.flatten();
  43.         int team = myCar.team;
  44.         //Vector2 oppositeGoal = new Vector2(0, 5120 * (team == 0 ? 1 : -1));
  45.         Vector2 backPoint = new Vector2(ballPosition.x, 5120 * (team == 0 ? -1 : 1));
  46.  
  47.         Vector2 carToBall = ballPosition.minus(carPosition);
  48.         Vector2 carToBackPoint = backPoint.minus(carPosition);
  49.  
  50.         if (input.ball.position.z > 300) {
  51.             RLBotDll.sendQuickChat(playerIndex, false, QuickChatSelection.Compliments_NiceOne);
  52.         }
  53.  
  54.         double ballY = input.ball.position.y;
  55.         double carY = input.car.position.y;
  56.         double z = input.car.position.z - input.ball.position.z;
  57.         double distanceToBall = Math.sqrt((carY - ballY) * (carY - ballY) + (carPosition.x - ballPosition.y) * (carPosition.x - ballPosition.x)
  58.                 + z * z);
  59.  
  60.         double steerCorrectionRadians = 0;
  61.  
  62.         if (team == 0 && carY > ballY || team == 1 && carY < ballY) {
  63.             steerCorrectionRadians = carDirection.correctionAngle(carToBackPoint);
  64.         } else if (team == 0 && carY < ballY && ballY - carY > 450 || team == 1 && carY > ballY && ballY - carY < 450) {
  65.             steerCorrectionRadians = carDirection.correctionAngle(carToBall);
  66.         }
  67.  
  68.         boolean steerBool;
  69.         int steer;
  70.         if (steerCorrectionRadians > 0.05) {
  71.             steerBool = true;
  72.             steer = -1;
  73.         } else if (steerCorrectionRadians < -0.05) {
  74.             steerBool = false;
  75.             steer = 1;
  76.         } else {
  77.             steerBool = Boolean.parseBoolean(null);
  78.             steer = 0;
  79.         }
  80.  
  81.         boolean powerSlide;
  82.         powerSlide = steerCorrectionRadians > 2 || steerCorrectionRadians < -2;
  83.  
  84.         boolean boost;
  85.         boost = steer == 0;
  86.  
  87.  
  88.         int dodgeCounter = 0;
  89.         boolean dodging = false;
  90.         float pitch = 0;
  91.         boolean jump = false;
  92.  
  93.         if (distanceToBall < 500 && !dodging) {
  94.             dodging = true;
  95.             dodgeCounter = 1;
  96.         }
  97.  
  98.         if (dodging) {
  99.                 if (dodgeCounter < 5) {
  100.                     jump = true;
  101.                     dodgeCounter++;
  102.                 } else if (dodgeCounter < 10) {
  103.                     jump = false;
  104.                     pitch = -1;
  105.                     dodgeCounter++;
  106.                 } else if (dodgeCounter < 15) {
  107.                     pitch = -1;
  108.                     jump = true;
  109.                     dodgeCounter++;
  110.                 } else {
  111.                     jump = false;
  112.                     pitch = 0;
  113.                     dodging = false;
  114.                     dodgeCounter = 0;
  115.                 }
  116.  
  117.         }
  118.  
  119.         drawDebugLines(input, myCar, steerBool);
  120.  
  121.         return new ControlsOutput().withThrottle(1).withJump(jump).withSteer(steer).withPitch(pitch).withSlide(powerSlide).withBoost(boost);
  122.     }
  123.  
  124.  
  125.     private void drawDebugLines(DataPacket input, CarData myCar, boolean goLeft) {
  126.         // Here's an example of rendering debug data on the screen.
  127.         Renderer renderer = BotLoopRenderer.forBotLoop(this);
  128.  
  129.         int team = myCar.team;
  130.  
  131.         Vector3 ownGoal = new Vector3(0, 5120 * (team == 0 ? -1 : 1), 321.3875);
  132.  
  133.         // Draw a line from the car to the ball
  134.         renderer.drawLine3d(Color.LIGHT_GRAY, myCar.position, input.ball.position);
  135.         renderer.drawLine3d(Color.ORANGE, myCar.position, ownGoal);
  136.         // Draw a line that points out from the nose of the car.
  137.         renderer.drawLine3d(goLeft ? Color.BLUE : Color.RED,
  138.                 myCar.position.plus(myCar.orientation.noseVector.scaled(150)),
  139.                 myCar.position.plus(myCar.orientation.noseVector.scaled(300)));
  140.  
  141.         renderer.drawString3d(goLeft ? "left" : "right", Color.WHITE, myCar.position, 2, 2);
  142.  
  143.         if(input.ball.hasBeenTouched) {
  144.             float lastTouchTime = myCar.elapsedSeconds - input.ball.latestTouch.gameSeconds;
  145.             Color touchColor = input.ball.latestTouch.team == 0 ? Color.BLUE : Color.ORANGE;
  146.             renderer.drawString3d((int)lastTouchTime + "s", touchColor, input.ball.position, 2, 2);
  147.         }
  148.  
  149.         try {
  150.             // Draw 3 seconds of ball prediction
  151.             BallPrediction ballPrediction = RLBotDll.getBallPrediction();
  152.             BallPredictionHelper.drawTillMoment(ballPrediction, myCar.elapsedSeconds + 3, Color.CYAN, renderer);
  153.         } catch (RLBotInterfaceException e) {
  154.             e.printStackTrace();
  155.         }
  156.     }
  157.  
  158.  
  159.     @Override
  160.     public int getIndex() {
  161.         return this.playerIndex;
  162.     }
  163.  
  164.     /**
  165.      * This is the most important function. It will automatically get called by the framework with fresh data
  166.      * every frame. Respond with appropriate controls!
  167.      */
  168.     @Override
  169.     public ControllerState processInput(GameTickPacket packet) {
  170.  
  171.         if (packet.playersLength() <= playerIndex || packet.ball() == null || !packet.gameInfo().isRoundActive()) {
  172.             // Just return immediately if something looks wrong with the data. This helps us avoid stack traces.
  173.             return new ControlsOutput();
  174.         }
  175.  
  176.         // Update the boost manager and tile manager with the latest data
  177.         BoostManager.loadGameTickPacket(packet);
  178.  
  179.         // Translate the raw packet data (which is in an unpleasant format) into our custom DataPacket class.
  180.         // The DataPacket might not include everything from GameTickPacket, so improve it if you need to!
  181.         DataPacket dataPacket = new DataPacket(packet, playerIndex);
  182.  
  183.         // Do the actual logic using our dataPacket.
  184.         ControlsOutput controlsOutput = null;
  185.  
  186.             controlsOutput = processInput(dataPacket);
  187.  
  188.         return controlsOutput;
  189.     }
  190.  
  191.     @Override
  192.     public void retire() {
  193.         System.out.println("Retiring sample bot " + playerIndex);
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement