Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSampleBot.java
More file actions
Latest commit
128 lines (100 loc) · 4.64 KB
/
Copy pathSampleBot.java
File metadata and controls
128 lines (100 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
packagerlbotexample;
importrlbot.Bot;
importrlbot.ControllerState;
importrlbot.cppinterop.RLBotDll;
importrlbot.cppinterop.RLBotInterfaceException;
importrlbot.flat.BallPrediction;
importrlbot.flat.GameTickPacket;
importrlbot.flat.QuickChatSelection;
importrlbot.manager.BotLoopRenderer;
importrlbot.render.Renderer;
importrlbotexample.boost.BoostManager;
importrlbotexample.input.DataPacket;
importrlbotexample.input.car.CarData;
importrlbotexample.output.ControlsOutput;
importrlbotexample.prediction.BallPredictionHelper;
importrlbotexample.vector.Vector2;
importjava.awt.*;
publicclassSampleBotimplementsBot {
privatefinalintplayerIndex;
publicSampleBot(intplayerIndex) {
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!
*/
privateControlsOutputprocessInput(DataPacketinput) {
Vector2ballPosition = input.ball.position.flatten();
CarDatamyCar = input.car;
Vector2carPosition = myCar.position.flatten();
Vector2carDirection = myCar.orientation.noseVector.flatten();
// Subtract the two positions to get a vector pointing from the car to the ball.
Vector2carToBall = ballPosition.minus(carPosition);
// How far does the car need to rotate before it's pointing exactly at the ball?
doublesteerCorrectionRadians = carDirection.correctionAngle(carToBall);
booleangoLeft = steerCorrectionRadians > 0;
// This is optional!
drawDebugLines(input, myCar, goLeft);
// This is also optional!
if (input.ball.position.z > 300) {
RLBotDll.sendQuickChat(playerIndex, false, QuickChatSelection.Compliments_NiceOne);
}
returnnewControlsOutput()
.withSteer(goLeft ? -1 : 1)
.withThrottle(1);
}
/**
* This is a nice example of using the rendering feature.
*/
privatevoiddrawDebugLines(DataPacketinput, CarDatamyCar, booleangoLeft) {
// Here's an example of rendering debug data on the screen.
Rendererrenderer = BotLoopRenderer.forBotLoop(this);
// Draw a line from the car to the ball
renderer.drawLine3d(Color.LIGHT_GRAY, myCar.position, input.ball.position);
// 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) {
floatlastTouchTime = myCar.elapsedSeconds - input.ball.latestTouch.gameSeconds;
ColortouchColor = 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
BallPredictionballPrediction = RLBotDll.getBallPrediction();
BallPredictionHelper.drawTillMoment(ballPrediction, myCar.elapsedSeconds + 3, Color.CYAN, renderer);
} catch (RLBotInterfaceExceptione) {
e.printStackTrace();
}
}
@Override
publicintgetIndex() {
returnthis.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
publicControllerStateprocessInput(GameTickPacketpacket) {
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.
returnnewControlsOutput();
}
// 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!
DataPacketdataPacket = newDataPacket(packet, playerIndex);
// Do the actual logic using our dataPacket.
ControlsOutputcontrolsOutput = processInput(dataPacket);
returncontrolsOutput;
}
@Override
publicvoidretire() {
System.out.println("Retiring sample bot " + playerIndex);
}
}