Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rlbotexample;
- import rlbot.Bot;
- import rlbot.ControllerState;
- import rlbot.cppinterop.RLBotDll;
- import rlbot.cppinterop.RLBotInterfaceException;
- import rlbot.flat.BallPrediction;
- import rlbot.flat.GameTickPacket;
- import rlbot.flat.QuickChatSelection;
- import rlbot.manager.BotLoopRenderer;
- import rlbot.render.Renderer;
- import rlbotexample.boost.BoostManager;
- import rlbotexample.input.DataPacket;
- import rlbotexample.input.car.CarData;
- import rlbotexample.output.ControlsOutput;
- import rlbotexample.prediction.BallPredictionHelper;
- import rlbotexample.vector.Vector2;
- import rlbotexample.vector.Vector3;
- import java.awt.*;
- public class SampleBot implements Bot {
- private final int playerIndex;
- public SampleBot(int playerIndex) {
- this.playerIndex = playerIndex;
- }
- /**
- * This is where we keep the actual bot logic. This function shows how to chase the ball.
- * Modify it to make your bot smarter!
- */
- private ControlsOutput processInput(DataPacket input) {
- Vector2 ballPosition = input.ball.position.flatten();
- CarData myCar = input.car;
- Vector2 carPosition = myCar.position.flatten();
- Vector2 carDirection = myCar.orientation.noseVector.flatten();
- int team = myCar.team;
- //Vector2 oppositeGoal = new Vector2(0, 5120 * (team == 0 ? 1 : -1));
- Vector2 backPoint = new Vector2(ballPosition.x, 5120 * (team == 0 ? -1 : 1));
- Vector2 carToBall = ballPosition.minus(carPosition);
- Vector2 carToBackPoint = backPoint.minus(carPosition);
- if (input.ball.position.z > 300) {
- RLBotDll.sendQuickChat(playerIndex, false, QuickChatSelection.Compliments_NiceOne);
- }
- double ballY = input.ball.position.y;
- double carY = input.car.position.y;
- double z = input.car.position.z - input.ball.position.z;
- double distanceToBall = Math.sqrt((carY - ballY) * (carY - ballY) + (carPosition.x - ballPosition.y) * (carPosition.x - ballPosition.x)
- + z * z);
- double steerCorrectionRadians = 0;
- if (team == 0 && carY > ballY || team == 1 && carY < ballY) {
- steerCorrectionRadians = carDirection.correctionAngle(carToBackPoint);
- } else if (team == 0 && carY < ballY && ballY - carY > 450 || team == 1 && carY > ballY && ballY - carY < 450) {
- steerCorrectionRadians = carDirection.correctionAngle(carToBall);
- }
- boolean steerBool;
- int steer;
- if (steerCorrectionRadians > 0.05) {
- steerBool = true;
- steer = -1;
- } else if (steerCorrectionRadians < -0.05) {
- steerBool = false;
- steer = 1;
- } else {
- steerBool = Boolean.parseBoolean(null);
- steer = 0;
- }
- boolean powerSlide;
- powerSlide = steerCorrectionRadians > 2 || steerCorrectionRadians < -2;
- boolean boost;
- boost = steer == 0;
- int dodgeCounter = 0;
- boolean dodging = false;
- float pitch = 0;
- boolean jump = false;
- if (distanceToBall < 500 && !dodging) {
- dodging = true;
- dodgeCounter = 1;
- }
- if (dodging) {
- if (dodgeCounter < 5) {
- jump = true;
- dodgeCounter++;
- } else if (dodgeCounter < 10) {
- jump = false;
- pitch = -1;
- dodgeCounter++;
- } else if (dodgeCounter < 15) {
- pitch = -1;
- jump = true;
- dodgeCounter++;
- } else {
- jump = false;
- pitch = 0;
- dodging = false;
- dodgeCounter = 0;
- }
- }
- drawDebugLines(input, myCar, steerBool);
- return new ControlsOutput().withThrottle(1).withJump(jump).withSteer(steer).withPitch(pitch).withSlide(powerSlide).withBoost(boost);
- }
- private void drawDebugLines(DataPacket input, CarData myCar, boolean goLeft) {
- // Here's an example of rendering debug data on the screen.
- Renderer renderer = BotLoopRenderer.forBotLoop(this);
- int team = myCar.team;
- Vector3 ownGoal = new Vector3(0, 5120 * (team == 0 ? -1 : 1), 321.3875);
- // Draw a line from the car to the ball
- renderer.drawLine3d(Color.LIGHT_GRAY, myCar.position, input.ball.position);
- renderer.drawLine3d(Color.ORANGE, myCar.position, ownGoal);
- // Draw a line that points out from the nose of the car.
- renderer.drawLine3d(goLeft ? Color.BLUE : Color.RED,
- myCar.position.plus(myCar.orientation.noseVector.scaled(150)),
- myCar.position.plus(myCar.orientation.noseVector.scaled(300)));
- renderer.drawString3d(goLeft ? "left" : "right", Color.WHITE, myCar.position, 2, 2);
- if(input.ball.hasBeenTouched) {
- float lastTouchTime = myCar.elapsedSeconds - input.ball.latestTouch.gameSeconds;
- Color touchColor = input.ball.latestTouch.team == 0 ? Color.BLUE : Color.ORANGE;
- renderer.drawString3d((int)lastTouchTime + "s", touchColor, input.ball.position, 2, 2);
- }
- try {
- // Draw 3 seconds of ball prediction
- BallPrediction ballPrediction = RLBotDll.getBallPrediction();
- BallPredictionHelper.drawTillMoment(ballPrediction, myCar.elapsedSeconds + 3, Color.CYAN, renderer);
- } catch (RLBotInterfaceException e) {
- e.printStackTrace();
- }
- }
- @Override
- public int getIndex() {
- return this.playerIndex;
- }
- /**
- * This is the most important function. It will automatically get called by the framework with fresh data
- * every frame. Respond with appropriate controls!
- */
- @Override
- public ControllerState processInput(GameTickPacket packet) {
- if (packet.playersLength() <= playerIndex || packet.ball() == null || !packet.gameInfo().isRoundActive()) {
- // Just return immediately if something looks wrong with the data. This helps us avoid stack traces.
- return new ControlsOutput();
- }
- // Update the boost manager and tile manager with the latest data
- BoostManager.loadGameTickPacket(packet);
- // Translate the raw packet data (which is in an unpleasant format) into our custom DataPacket class.
- // The DataPacket might not include everything from GameTickPacket, so improve it if you need to!
- DataPacket dataPacket = new DataPacket(packet, playerIndex);
- // Do the actual logic using our dataPacket.
- ControlsOutput controlsOutput = null;
- controlsOutput = processInput(dataPacket);
- return controlsOutput;
- }
- @Override
- public void retire() {
- System.out.println("Retiring sample bot " + playerIndex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement