-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRLBotClient.cpp
More file actions
477 lines (402 loc) · 19.2 KB
/
Copy pathRLBotClient.cpp
File metadata and controls
477 lines (402 loc) · 19.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "RLBotClient.h"
#include <rlbot/platform.h>
#include <rlbot/botmanager.h>
#include <cmath>
using namespace RLGC;
using namespace GGL;
RLBotParams g_RLBotParams = {};
rlbot::Bot* BotFactory(int index, int team, std::string name) {
return new RLBotBot(index, team, name, g_RLBotParams);
}
Vec ToVec(const rlbot::flat::Vector3* rlbotVec) {
if (!rlbotVec) return Vec();
return Vec(rlbotVec->x(), rlbotVec->y(), rlbotVec->z());
}
PhysState ToPhysState(const rlbot::flat::Physics* phys) {
PhysState obj = {};
if (phys) {
obj.pos = ToVec(phys->location());
if (phys->rotation()) {
Angle ang = Angle(phys->rotation()->yaw(), phys->rotation()->pitch(), phys->rotation()->roll());
obj.rotMat = ang.ToRotMat();
}
obj.vel = ToVec(phys->velocity());
obj.angVel = ToVec(phys->angularVelocity());
}
return obj;
}
RLBotBot::RLBotBot(int _index, int _team, std::string _name, const RLBotParams& params)
: rlbot::Bot(_index, _team, _name), params(params) {
RG_LOG("Created RLBot bot: index " << _index << ", name: " << name << "...");
}
RLBotBot::~RLBotBot() {
}
void RLBotBot::UpdateBallHitInfo(Player& player, PlayerInternalState& internalState,
float curTime, const rlbot::flat::Touch* latestTouch) {
// Update ball hit info if this player touched the ball
if (latestTouch && latestTouch->playerIndex() == player.index) {
float timeSinceTouch = curTime - latestTouch->gameSeconds();
// Only update if this is a recent touch
if (timeSinceTouch < 0.1f && gs.lastTickCount > internalState.ballHitInfo.tickCountWhenHit) {
internalState.ballHitInfo.isValid = true;
internalState.ballHitInfo.tickCountWhenHit = gs.lastTickCount;
// Calculate relative position on ball
Vec ballPos = gs.ball.pos;
Vec touchLocation = ToVec(latestTouch->location());
internalState.ballHitInfo.ballPos = ballPos;
internalState.ballHitInfo.relativePosOnBall = touchLocation - ballPos;
// Extra hit velocity (approximated - RLBot doesn't provide this directly)
internalState.ballHitInfo.extraHitVel = Vec(0, 0, 0);
}
} else {
// Invalidate old hit info after some time
if (gs.lastTickCount > internalState.ballHitInfo.tickCountWhenHit + 120) {
internalState.ballHitInfo.isValid = false;
}
}
// Note: We don't assign to player.ballHitInfo since Player doesn't have this field
// The internal state tracking is sufficient for observation builders that need it
}
void RLBotBot::UpdatePlayerState(Player& player, Player* prevPlayer,
PlayerInternalState& internalState,
float deltaTime, bool isLocalPlayer) {
if (player.isSupersonic) {
internalState.supersonicTime += deltaTime;
if (internalState.supersonicTime > RLBotConst::SUPERSONIC_MAINTAIN_MAX_TIME) {
internalState.supersonicTime = RLBotConst::SUPERSONIC_MAINTAIN_MAX_TIME;
}
} else {
internalState.supersonicTime = 0;
}
player.supersonicTime = internalState.supersonicTime;
bool currentlyBoosting = (isLocalPlayer && controls.boost) ||
(prevPlayer && prevPlayer->prevAction.boost != 0);
if (internalState.timeSpentBoosting > 0) {
if (!currentlyBoosting && internalState.timeSpentBoosting >= RLBotConst::BOOST_MIN_TIME) {
internalState.timeSpentBoosting = 0;
} else {
internalState.timeSpentBoosting += deltaTime;
}
} else {
if (currentlyBoosting) {
internalState.timeSpentBoosting = deltaTime;
}
}
player.timeSpentBoosting = internalState.timeSpentBoosting;
bool currentlyHandbraking = (isLocalPlayer && controls.handbrake) ||
(prevPlayer && prevPlayer->prevAction.handbrake != 0);
if (currentlyHandbraking) {
internalState.handbrakeVal += RLBotConst::POWERSLIDE_RISE_RATE * deltaTime;
} else {
internalState.handbrakeVal -= RLBotConst::POWERSLIDE_FALL_RATE * deltaTime;
}
internalState.handbrakeVal = RS_CLAMP(internalState.handbrakeVal, 0.f, 1.f);
player.handbrakeVal = internalState.handbrakeVal;
// We estimate by setting all 4 wheels to the same state (API limitation)
int numWheelsInContact = player.isOnGround ? 4 : 0;
for (int i = 0; i < 4; i++) {
internalState.wheelsWithContact[i] = (numWheelsInContact > 0);
player.wheelsWithContact[i] = internalState.wheelsWithContact[i];
}
internalState.numWheelsInContact = numWheelsInContact;
if (player.isDemoed) {
if (!internalState.wasDemoedLastFrame) {
// Just got demoed this frame
internalState.demoRespawnTimer = RLBotConst::DEMO_RESPAWN_TIME;
internalState.demoTick = gs.lastTickCount;
} else {
// Continue counting down
internalState.demoRespawnTimer -= deltaTime;
if (internalState.demoRespawnTimer < 0) {
internalState.demoRespawnTimer = 0;
}
}
} else {
if (internalState.wasDemoedLastFrame) {
// Just respawned
internalState.demoRespawnTimer = 0;
}
}
player.demoRespawnTimer = internalState.demoRespawnTimer;
internalState.wasDemoedLastFrame = player.isDemoed;
if (internalState.carContact.cooldownTimer > 0) {
internalState.carContact.cooldownTimer -= deltaTime;
if (internalState.carContact.cooldownTimer < 0) {
internalState.carContact.cooldownTimer = 0;
internalState.carContact.otherCarID = 0;
}
}
player.carContact.otherCarID = internalState.carContact.otherCarID;
player.carContact.cooldownTimer = internalState.carContact.cooldownTimer;
if (player.isOnGround) {
internalState.isJumping = false;
internalState.isFlipping = false;
internalState.flipTime = 0;
internalState.hasFlipped = false;
internalState.flipRelTorque = Vec(0, 0, 0);
internalState.airTime = 0;
internalState.airTimeSinceJump = 0;
internalState.autoFlipTimer = 0;
internalState.isAutoFlipping = false;
internalState.autoFlipTorqueScale = 0;
// Reset hasJumped when landing (with time padding for minimum jumps)
if (prevPlayer && prevPlayer->hasJumped) {
if (internalState.jumpTime < RLBotConst::JUMP_MIN_TIME + RLBotConst::JUMP_RESET_TIME_PAD) {
// Don't reset jump yet - might still be leaving ground after min-time jump
} else {
internalState.jumpTime = 0;
}
}
} else {
internalState.airTime += deltaTime;
player.airTime = internalState.airTime;
if (internalState.isJumping) {
internalState.jumpTime += deltaTime;
// Jump ends after JUMP_MAX_TIME
if (internalState.jumpTime >= RLBotConst::JUMP_MAX_TIME) {
internalState.isJumping = false;
}
}
player.jumpTime = internalState.jumpTime;
if (internalState.isFlipping) {
internalState.flipTime += deltaTime;
// Flip torque ends after FLIP_TORQUE_TIME
if (internalState.flipTime >= RLBotConst::FLIP_TORQUE_TIME) {
internalState.isFlipping = false;
}
}
player.flipTime = internalState.flipTime;
if (player.hasJumped && !internalState.isJumping) {
internalState.airTimeSinceJump += deltaTime;
} else {
internalState.airTimeSinceJump = 0;
}
player.airTimeSinceJump = internalState.airTimeSinceJump;
// Car auto-flips when upside down in the air for too long
bool shouldAutoFlip = (player.rotMat.up.z < RLBotConst::CAR_AUTOFLIP_NORMZ_THRESH) &&
(std::abs(player.rotMat.forward.z) < 0.9f);
if (shouldAutoFlip) {
internalState.autoFlipTimer += deltaTime;
if (internalState.autoFlipTimer >= RLBotConst::CAR_AUTOFLIP_TIME && !internalState.isAutoFlipping) {
internalState.isAutoFlipping = true;
// Calculate auto-flip direction based on roll angle
Angle angles = Angle::FromRotMat(player.rotMat);
float absRoll = std::abs(angles.roll);
if (absRoll > RLBotConst::CAR_AUTOFLIP_ROLL_THRESH) {
internalState.autoFlipTorqueScale = (angles.roll > 0) ? 1.f : -1.f;
}
}
} else {
internalState.autoFlipTimer = 0;
internalState.isAutoFlipping = false;
internalState.autoFlipTorqueScale = 0;
}
// Flip reset occurs when all wheels touch a surface while in the air
internalState.gotFlipResetThisFrame = false;
// Detect flip reset: hasJumped or hasDoubleJumped goes false while in air
if (prevPlayer) {
bool hadJumpedBefore = internalState.hadJumpedLastFrame;
bool hadDoubleJumpedBefore = internalState.hadDoubleJumpedLastFrame;
// If hasJumped was true and is now false while in air to flip reset
if (hadJumpedBefore && !player.hasJumped && !player.isOnGround) {
internalState.gotFlipResetThisFrame = true;
internalState.lastFlipResetTick = gs.lastTickCount;
// Reset flip related states
internalState.hasFlipped = false;
internalState.isFlipping = false;
internalState.flipTime = 0;
internalState.flipRelTorque = Vec(0, 0, 0);
internalState.airTimeSinceJump = 0;
}
// Alternative detection: doubleJumped goes false while in air
if (hadDoubleJumpedBefore && !player.hasDoubleJumped && !player.isOnGround && player.hasJumped) {
internalState.gotFlipResetThisFrame = true;
internalState.lastFlipResetTick = gs.lastTickCount;
}
}
}
if (prevPlayer) {
// Detect first jump
if (player.hasJumped && !prevPlayer->hasJumped) {
internalState.isJumping = true;
internalState.jumpTime = 0;
internalState.jumpReleased = false;
// Starting a new jump cancels any flip state
internalState.isFlipping = false;
internalState.flipTime = 0;
}
// Detect second jump/flip (double jump changes from false to true)
if (player.hasDoubleJumped && !prevPlayer->hasDoubleJumped && !player.isOnGround) {
internalState.isFlipping = true;
internalState.flipTime = 0;
internalState.hasFlipped = true;
internalState.isJumping = false; // Flip consumes second jump, ends jumping state
// Calculate flip direction from controls
// Get the current action being applied
Action currentAction = isLocalPlayer ? controls : prevPlayer->prevAction;
float pitch = currentAction.pitch;
float yaw = currentAction.yaw;
// Normalize the dodge direction
Vec dodgeDir = Vec(pitch, yaw, 0);
float dodgeMag = dodgeDir.Length();
if (dodgeMag > 0.1f) {
dodgeDir = dodgeDir.Normalized();
// Apply deadzones (< 0.1 becomes 0)
if (std::abs(dodgeDir.x) < 0.1f) dodgeDir.x = 0;
if (std::abs(dodgeDir.y) < 0.1f) dodgeDir.y = 0;
// Calculate relative flip torque: flipRelTorque = (-yaw, pitch, 0)
// This matches RocketSim's calculation
internalState.flipRelTorque = Vec(-dodgeDir.y, dodgeDir.x, 0);
} else {
// Neutral flip (straight up double jump) - no flip torque
internalState.flipRelTorque = Vec(0, 0, 0);
}
}
}
player.isJumping = internalState.isJumping;
player.isFlipping = internalState.isFlipping;
player.hasFlipped = internalState.hasFlipped;
player.flipRelTorque = internalState.flipRelTorque;
player.isAutoFlipping = internalState.isAutoFlipping;
player.autoFlipTimer = internalState.autoFlipTimer;
player.autoFlipTorqueScale = internalState.autoFlipTorqueScale;
// World contact (estimated based on wheel contact)
player.worldContact.hasContact = internalState.worldContact.hasContact;
player.worldContact.contactNormal = internalState.worldContact.contactNormal;
// Store previous frame states for next update
internalState.wasOnGroundLastFrame = player.isOnGround;
internalState.wasInAirLastFrame = !player.isOnGround;
internalState.hadJumpedLastFrame = player.hasJumped;
internalState.hadDoubleJumpedLastFrame = player.hasDoubleJumped;
// Note: Don't track hadWheelContactLastFrame since Player doesn't have hasWheelContact field
}
void RLBotBot::UpdateGameState(rlbot::GameTickPacket& packet, float deltaTime, float curTime) {
prevGs = gs;
gs = {};
gs.lastTickCount = packet->gameInfo()->frameNum();
gs.deltaTime = deltaTime;
PhysState ballPhys = ToPhysState(packet->ball()->physics());
static_cast<PhysState&>(gs.ball) = ballPhys;
auto latestTouch = packet->ball()->latestTouch();
auto boostPadStates = packet->boostPadStates();
gs.boostPads.resize(CommonValues::BOOST_LOCATIONS_AMOUNT, true);
gs.boostPadsInv.resize(CommonValues::BOOST_LOCATIONS_AMOUNT, true);
gs.boostPadTimers.resize(CommonValues::BOOST_LOCATIONS_AMOUNT, 0);
gs.boostPadTimersInv.resize(CommonValues::BOOST_LOCATIONS_AMOUNT, 0);
if (boostPadStates && boostPadStates->size() == CommonValues::BOOST_LOCATIONS_AMOUNT) {
for (int i = 0; i < CommonValues::BOOST_LOCATIONS_AMOUNT; i++) {
gs.boostPads[i] = boostPadStates->Get(i)->isActive();
gs.boostPadsInv[CommonValues::BOOST_LOCATIONS_AMOUNT - i - 1] = gs.boostPads[i];
gs.boostPadTimers[i] = boostPadStates->Get(i)->timer();
gs.boostPadTimersInv[CommonValues::BOOST_LOCATIONS_AMOUNT - i - 1] = gs.boostPadTimers[i];
}
}
auto players = packet->players();
gs.players.resize(players->size());
for (int i = 0; i < players->size(); i++) {
auto playerInfo = players->Get(i);
Player& player = gs.players[i];
Player* prevPlayer = (prevGs.players.size() > i && prevGs.players[i].carId == playerInfo->spawnId())
? &prevGs.players[i] : nullptr;
PlayerInternalState& internalState = internalPlayerStates[i];
// Basic physics state
static_cast<PhysState&>(player) = ToPhysState(playerInfo->physics());
// Basic player info from RLBot
player.carId = playerInfo->spawnId();
player.team = (Team)playerInfo->team();
player.boost = playerInfo->boost();
player.isDemoed = playerInfo->isDemolished();
player.isOnGround = playerInfo->hasWheelContact();
player.hasJumped = playerInfo->jumped();
player.hasDoubleJumped = playerInfo->doubleJumped();
player.isSupersonic = playerInfo->isSupersonic();
player.index = i;
player.prev = prevPlayer;
// Update comprehensive state tracking (1:1 with RocketSim)
bool isLocalPlayer = (i == index);
UpdatePlayerState(player, prevPlayer, internalState, deltaTime, isLocalPlayer);
// Update ball hit info
UpdateBallHitInfo(player, internalState, curTime, latestTouch);
player.ballTouchedStep = false;
player.ballTouchedTick = false;
if (latestTouch && latestTouch->playerIndex() == i) {
float timeSinceTouch = curTime - latestTouch->gameSeconds();
// Step touch: within the current step's time window
if (timeSinceTouch < (params.tickSkip * CommonValues::TICK_TIME) + 0.01f) {
player.ballTouchedStep = true;
gs.lastTouchCarID = player.carId;
}
// Tick touch: within this specific tick
if (timeSinceTouch < deltaTime + 0.01f) {
player.ballTouchedTick = true;
}
internalState.lastTouchTick = gs.lastTickCount;
}
}
gs.goalScored = false;
for (int i = 0; i < 2; i++) {
int currentScore = packet->teams()->Get(i)->score();
if (currentScore > lastTeamScores[i]) {
gs.goalScored = true;
}
lastTeamScores[i] = currentScore;
}
}
rlbot::Controller RLBotBot::GetOutput(rlbot::GameTickPacket gameTickPacket) {
float curTime = gameTickPacket->gameInfo()->secondsElapsed();
if (prevTime == 0) prevTime = curTime;
float deltaTime = curTime - prevTime;
prevTime = curTime;
int ticksElapsed = (ticks == -1) ? params.tickSkip : roundf(deltaTime * 120);
// If no time has passed, return previous controls
if (ticksElapsed == 0 && ticks != -1) {
rlbot::Controller output_controller = {};
output_controller.throttle = controls.throttle;
output_controller.steer = controls.steer;
output_controller.pitch = controls.pitch;
output_controller.yaw = controls.yaw;
output_controller.roll = controls.roll;
output_controller.jump = controls.jump != 0;
output_controller.boost = controls.boost != 0;
output_controller.handbrake = controls.handbrake != 0;
return output_controller;
}
last_ticks = ticks;
ticks += ticksElapsed;
// Update game state with comprehensive 1:1 RocketSim tracking
UpdateGameState(gameTickPacket, deltaTime, curTime);
auto& localPlayer = gs.players[index];
localPlayer.prevAction = controls;
// Determine if we need new action from policy
if (ticks >= params.tickSkip || ticks == -1) {
ticks %= params.tickSkip;
updateAction = true;
}
// Get new action from policy if needed
if (updateAction) {
updateAction = false;
action = params.inferUnit->InferAction(localPlayer, gs, params.deterministic);
}
// Apply action delay
if (last_ticks < params.actionDelay && ticks >= params.actionDelay) {
controls = action;
}
// Convert to RLBot controller format
rlbot::Controller output_controller = {};
output_controller.throttle = controls.throttle;
output_controller.steer = controls.steer;
output_controller.pitch = controls.pitch;
output_controller.yaw = controls.yaw;
output_controller.roll = controls.roll;
output_controller.jump = controls.jump != 0;
output_controller.boost = controls.boost != 0;
output_controller.handbrake = controls.handbrake != 0;
output_controller.useItem = false;
return output_controller;
}
void RLBotClient::Run(const RLBotParams& params) {
g_RLBotParams = params;
rlbot::platform::SetWorkingDirectory(rlbot::platform::GetExecutableDirectory());
rlbot::BotManager botManager(BotFactory);
botManager.StartBotServer(params.port);
}