forked from NitrOP7674/FreeplayCheckpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cpp
More file actions
310 lines (281 loc) · 8.12 KB
/
Copy pathstate.cpp
File metadata and controls
310 lines (281 loc) · 8.12 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
/*
* Copyright (c) 2021
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "pch.h"
#include "CheckpointPlugin.h"
#include "utils/customrotator.h"
static inline void readVec(std::istream& in, Vector& v) {
readPOD(in, v.X);
readPOD(in, v.Y);
readPOD(in, v.Z);
}
static inline void writeVec(std::ostream& out, const Vector& v) {
writePOD(out, v.X);
writePOD(out, v.Y);
writePOD(out, v.Z);
}
static inline void readRot(std::istream& in, Rotator& r) {
readPOD(in, r.Pitch);
readPOD(in, r.Yaw);
readPOD(in, r.Roll);
}
static inline void writeRot(std::ostream& out, const Rotator& r) {
writePOD(out, r.Pitch);
writePOD(out, r.Yaw);
writePOD(out, r.Roll);
}
ActorState::ActorState() {
location = Vector(0, 0, 0);
velocity = Vector(0, 0, 0);
rotation = Rotator(0, 0, 0);
angVelocity = Vector(0, 0, 0);
}
ActorState::ActorState(ActorWrapper a) {
location = a.GetLocation();
velocity = a.GetVelocity();
rotation = a.GetRotation();
angVelocity = a.GetAngularVelocity();
}
// Returns the object state <percent (0-1.0)> way between lh and rh.
ActorState::ActorState(ActorState lh, ActorState rh, float percent) {
float rhPercent = 1 - percent;
location = lh.location * percent + rh.location * rhPercent;
velocity = lh.velocity * percent + rh.velocity * rhPercent;
/* Custom Rotator */
// TODO: There's a weird blip that goes slightly sideways when crossing verticle.
// Figure out a better way to interpolate.
CustomRotator rotator(percent);
CustomRotator br(rh.rotation);
CustomRotator bdiff = CustomRotator(lh.rotation).diffTo(br) * rotator;
rotation = (br - bdiff).ToRotator();
angVelocity = lh.angVelocity * percent + rh.angVelocity * rhPercent;
}
ActorState::ActorState(std::istream& in) {
readVec(in, location);
readVec(in, velocity);
readRot(in, rotation);
readVec(in, angVelocity);
}
void ActorState::write(std::ostream& out) const {
writeVec(out, location);
writeVec(out, velocity);
writeRot(out, rotation);
writeVec(out, angVelocity);
}
void ActorState::apply(ActorWrapper a) const {
a.SetLocation(location);
a.SetVelocity(velocity);
a.SetRotation(rotation);
a.SetAngularVelocity(angVelocity, false);
}
ActorState ActorState::mirror() const {
ActorState as = *this;
as.location.X *= -1;
as.velocity.X *= -1;
as.angVelocity.Y *= -1;
as.angVelocity.Z *= -1;
auto q = RotatorToQuat(rotation);
q.Y *= -1;
q.Z *= -1;
Quat r(0, 0, 0, 1);
as.rotation = QuatToRotator(q*r);
return as;
}
CarState::CarState() {
actorState = ActorState();
boostAmount = 0;
hasDodge = false;
lastJumped = 0;
}
CarState::CarState(CarWrapper c) {
actorState = ActorState(c);
boostAmount = c.GetBoostComponent().IsNull() ? 0 : c.GetBoostComponent().GetCurrentBoostAmount();
// Save last jump time only if the player jumped.
// After applying this, we will remove the player's dodge when the jump timer expires.
lastJumped = !c.GetbJumped() || c.GetJumpComponent().IsNull() ? -1 : c.GetJumpComponent().GetInactiveTime();
hasDodge = !c.GetbDoubleJumped() && lastJumped < MAX_DODGE_TIME;
}
// Returns the object state <percent (0-1.0)> way between lh and rh.
CarState::CarState(CarState lh, CarState rh, float percent) {
actorState = ActorState(lh.actorState, rh.actorState, percent);
float rhPercent = 1 - percent;
boostAmount = lh.boostAmount * percent + rh.boostAmount * rhPercent;
if (lh.lastJumped == -1 || rh.lastJumped == -1) {
lastJumped = -1;
hasDodge = true;
} else if (rh.lastJumped < lh.lastJumped) {
lastJumped = 0;
hasDodge = true;
} else { // lh.lastJumped <= rh.lastJumped
lastJumped = lh.lastJumped * percent + rh.lastJumped * rhPercent;
hasDodge = lastJumped < MAX_DODGE_TIME;
}
}
CarState::CarState(std::istream& in) {
actorState = ActorState(in);
readPOD(in, boostAmount);
readPOD(in, hasDodge);
readPOD(in, lastJumped);
}
void CarState::write(std::ostream& out) const {
actorState.write(out);
writePOD(out, boostAmount);
writePOD(out, hasDodge);
writePOD(out, lastJumped);
}
void CarState::apply(CarWrapper c) const {
actorState.apply(c);
if (!c.GetBoostComponent().IsNull()) {
c.GetBoostComponent().SetCurrentBoostAmount(boostAmount);
}
c.SetbDoubleJumped(!hasDodge);
c.SetbJumped(!hasDodge);
}
CarState CarState::mirror() const {
CarState cs = *this;
cs.actorState = cs.actorState.mirror();
return cs;
}
GameState::GameState() {
ball = ActorState();
car = CarState();
time = -1;
}
GameState::GameState(std::istream& in) {
readVec(in, ball.location);
readVec(in, car.actorState.location);
readVec(in, ball.velocity);
readVec(in, car.actorState.velocity);
readRot(in, ball.rotation);
readRot(in, car.actorState.rotation);
readVec(in, ball.angVelocity);
readVec(in, car.actorState.angVelocity);
readPOD(in, car.boostAmount);
readPOD(in, car.hasDodge);
readPOD(in, car.lastJumped);
time = -1;
}
void GameState::write(std::ostream& out) const {
writeVec(out, ball.location);
writeVec(out, car.actorState.location);
writeVec(out, ball.velocity);
writeVec(out, car.actorState.velocity);
writeRot(out, ball.rotation);
writeRot(out, car.actorState.rotation);
writeVec(out, ball.angVelocity);
writeVec(out, car.actorState.angVelocity);
writePOD(out, car.boostAmount);
writePOD(out, car.hasDodge);
writePOD(out, car.lastJumped);
}
GameState::GameState(std::shared_ptr<GameWrapper> gw) {
ServerWrapper sw = gw->GetGameEventAsServer();
ball = ActorState(sw.GetBall());
car = CarState(sw.GetGameCar());
if (gw->IsInCustomTraining()) {
time = gw->GetCurrentGameState().GetGameTimeRemaining();
} else {
time = -1;
}
}
GameState::GameState(CarWrapper cw, BallWrapper bw) {
ball = ActorState(bw);
car = CarState(cw);
time = -1;
}
// Returns the game state <percent (0-1.0)> way between lh and rh.
GameState::GameState(const GameState &lh, const GameState &rh, float percent) {
ball = ActorState(lh.ball, rh.ball, percent);
car = CarState(lh.car, rh.car, percent);
if (lh.time != -1 && rh.time != -1) {
time = (lh.time + rh.time) / 2;
} else {
time = -1;
}
}
void GameState::apply(std::shared_ptr<GameWrapper> gw) const {
ServerWrapper sw = gw->GetGameEventAsServer();
if (sw.GetBall().IsNull() || sw.GetGameCar().IsNull()) {
return;
}
if (gw->IsInCustomTraining()) {
if (time == -1) {
// Don't allow loading non-CT state into CT.
return;
} else {
gw->GetCurrentGameState().SetGameTimeRemaining(time);
}
}
ball.apply(sw.GetBall());
car.apply(sw.GetGameCar());
}
GameState GameState::mirror() const {
GameState gs;
gs.car = car.mirror();
gs.ball = ball.mirror();
return gs;
}
/*
* base64enc and base64dec from https://stackoverflow.com/a/34571089. No license
* information provided.
*/
const std::string_view b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string base64enc(const std::string in) {
std::string out;
unsigned val = 0;
int valb = -6;
for (unsigned char c : in) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
out.push_back(b64[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) out.push_back(b64[((val << 8) >> (valb + 8)) & 0x3F]);
while (out.size() % 4) out.push_back('=');
return out;
}
const std::string base64dec(const std::string in) {
std::string out;
std::vector<int> T(256, -1);
for (int i = 0; i < 64; i++) T[b64[i]] = i;
unsigned val = 0;
int valb = -8;
for (unsigned char c : in) {
if (T[c] == -1) break;
val = (val << 6) + T[c];
valb += 6;
if (valb >= 0) {
out.push_back(char((val >> valb) & 0xFF));
valb -= 8;
}
}
return out;
}
GameState::GameState(const std::string enc) {
std::string dec = base64dec(enc);
std::istringstream stream(dec);
readVec(stream, ball.location);
readVec(stream, car.actorState.location);
readVec(stream, ball.velocity);
readVec(stream, car.actorState.velocity);
readRot(stream, ball.rotation);
readRot(stream, car.actorState.rotation);
readVec(stream, ball.angVelocity);
readVec(stream, car.actorState.angVelocity);
readPOD(stream, car.boostAmount);
readPOD(stream, car.hasDodge);
readPOD(stream, car.lastJumped);
time = -1;
}
const std::string GameState::toString() const {
std::ostringstream dec;
write(dec);
return base64enc(dec.str());
}