-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
79 lines (66 loc) · 2.14 KB
/
index.ts
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
import {
Client,
Manager,
Controller,
GameTickPacketT,
FieldInfoT,
BallPredictionT,
} from "easyrlbot";
class ExampleBot extends Client {
constructor(botIndex: number, ...args: any[]) {
super(botIndex, ...args); // Do not change this except if you know what you are doing.
}
getOutput(
gameTickPacket: GameTickPacketT,
fieldInfo: FieldInfoT,
ballPrediction: BallPredictionT
) {
let controller = new Controller(); // Create a new controller
if (
!ballPrediction ||
!gameTickPacket.players[this.botIndex] ||
!gameTickPacket.ball ||
!gameTickPacket.ball.physics?.location
)
return; // Return if needed information is not provided
// Define target and car physics
let target = gameTickPacket.ball.physics; // Set targeted location to ball
let car = gameTickPacket.players[this.botIndex].physics;
if (!target.location || !car?.location || !car.rotation) return; // Return if needed information is not provided
// Calculate angle
let botToTargetAngle = Math.atan2(
target.location.y - car.location.y,
target.location.x - car.location.x
);
// Angle relative to car
let botFrontToTargetAngle = botToTargetAngle - car.rotation.yaw;
// Correct angle
if (botFrontToTargetAngle > Math.PI) botFrontToTargetAngle -= 2 * Math.PI;
if (botFrontToTargetAngle < -Math.PI) botFrontToTargetAngle += 2 * Math.PI;
// Calculate distance in 2D between car and ball
let targetDistance2D = Math.round(
Math.sqrt(
Math.pow(target.location.x - car.location.x, 2) +
Math.pow(target.location.y - car.location.y, 2)
)
);
// Steer the car in the targeted direction
if (botFrontToTargetAngle > 0) {
controller.steer = 1;
} else {
controller.steer = -1;
}
// If distance to target is more than 2500 then boost
if (targetDistance2D > 2500) {
controller.boost = true;
}
// Drive forward
controller.throttle = 1;
// Send controller to RLBot
this.controller.sendInput(controller);
}
}
let manager = new Manager(
ExampleBot,
require("./config/AgentConfig.json").port
);