-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathSampleBot.java
More file actions
115 lines (95 loc) · 4.23 KB
/
SampleBot.java
File metadata and controls
115 lines (95 loc) · 4.23 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
package rlbot;
import rlbot.cppinterop.RLBotDll;
import rlbot.cppinterop.RLBotInterfaceException;
import rlbot.flat.*;
import rlbot.gamestate.CarState;
import rlbot.gamestate.DesiredRotation;
import rlbot.gamestate.GameState;
import rlbot.gamestate.PhysicsState;
import rlbot.input.CarData;
import rlbot.input.DataPacket;
import rlbot.input.DropshotTile;
import rlbot.input.FieldInfoManager;
import rlbot.manager.BotLoopRenderer;
import rlbot.output.ControlsOutput;
import rlbot.render.Renderer;
import rlbot.vec.Vector2;
import java.awt.Color;
import java.io.IOException;
public class SampleBot extends BaseBot {
private FieldInfoManager fieldInfoManager = new FieldInfoManager();
public SampleBot(int playerIndex, int team) {
super(playerIndex, team);
System.out.println("Constructed sample bot " + playerIndex);
}
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();
Vector2 carToBall = ballPosition.minus(carPosition);
double steerCorrectionRadians = carDirection.correctionAngle(carToBall);
float steer;
if (steerCorrectionRadians > 0) {
steer = -1;
} else {
steer = 1;
}
Renderer renderer = BotLoopRenderer.forBotLoop(this);
renderer.drawLine3d(Color.BLUE, myCar.position, input.ball.position);
renderer.drawString3d("It's me", Color.green, myCar.position, 2, 2);
renderer.drawCenteredRectangle3d(Color.black, input.ball.position, 20, 20, false);
for (DropshotTile tile: fieldInfoManager.getDropshotTiles()) {
if (tile.state == DropshotTile.State.OPEN) {
renderer.drawCenteredRectangle3d(Color.BLACK, tile.location, 10, 10, true);
}
}
if (myCar.position.z > 100) {
GameState gameState = new GameState()
.withCarState(this.index, new CarState()
.withPhysics(new PhysicsState()
.withRotation(new DesiredRotation((float) Math.PI,0F, 0F))));
RLBotDll.setGameState(gameState.buildPacket());
RLBotDll.sendQuickChat(this.index, false, QuickChatSelection.Apologies_Oops);
try {
MatchSettings matchSettings = RLBotDll.getMatchSettings();
System.out.println("Game mode: " + GameMode.name(matchSettings.gameMode()));
System.out.println("Game map: " + GameMap.name(matchSettings.gameMap()));
System.out.println("Rumble option: " + RumbleOption.name(matchSettings.mutatorSettings().rumbleOption()));
} catch (RLBotInterfaceException e) {
e.printStackTrace();
}
}
try {
QuickChatMessages quickChatMessages = receiveQuickChat();
if (quickChatMessages.messagesLength() > 0) {
RLBotDll.sendQuickChat(this.index, false, QuickChatSelection.Reactions_Wow);
}
} catch (RLBotInterfaceException e) {
e.printStackTrace();
}
try {
final BallPrediction ballPrediction = RLBotDll.getBallPrediction();
Vector3 location = ballPrediction.slices(ballPrediction.slicesLength() / 2).physics().location();
renderer.drawLine3d(Color.CYAN, input.ball.position, rlbot.vec.Vector3.fromFlatbuffer(location));
} catch (IOException e) {
// Ignore
}
return new ControlsOutput()
.withSteer(steer)
.withThrottle(1)
.withUseItem(myCar.velocity.y > 0);
}
@Override
public ControllerState processInput(rlbot.flat.GameTickPacket gameTickPacket) {
if (gameTickPacket.playersLength() <= index || gameTickPacket.ball() == null) {
return new ControlsOutput();
}
DataPacket dataPacket = new DataPacket(gameTickPacket, index);
fieldInfoManager.addPacketInfo(gameTickPacket);
return processInput(dataPacket);
}
public void retire() {
System.out.println("Retiring sample bot " + index);
}
}