-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathUpdate_Poses.cpp
More file actions
581 lines (509 loc) · 18.2 KB
/
Update_Poses.cpp
File metadata and controls
581 lines (509 loc) · 18.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "pch.h"
#include "RenderConst.h"
#include "cardefs.h"
#include "par.h"
#include "Def_Str.h"
#include "Axes.h"
#include "CGame.h"
#include "CHud.h"
#include "CGui.h"
#include "SceneXml.h"
#include "CScene.h"
#include "FollowCamera.h"
#include "Road.h"
#include "game.h"
#include "quickprof.h"
#include "dbl.h"
#include "gameclient.hpp"
#include "SoundMgr.h"
// #include "Slider.h"
// #include "SplitScreen.h"
#include "settings.h"
#include "CarModel.h"
#include <Ogre.h>
#include <MyGUI.h>
// #include <OgreCamera.h>
// #include <OgreQuaternion.h>
// #include <OgreSceneNode.h>
// #include <OgreVector3.h>
using namespace Ogre;
// 🧵💫 newPoses - Get new car pos from game
// caution: called from GAME, 2nd thread, no Ogre stuff here
/// Todo: move arrow update and ChampionshipAdvance to updatePoses ...
//---------------------------------------------------------------------------------------------------------------
void App::newPoses(float time) // time only for camera update
{
if (!pGame || bLoading || pGame->cars.empty() /*|| carPoses.empty() || iCurPoses.empty()*/)
return;
PROFILER.beginBlock(".newPos ");
double rplTime = pGame->timer.GetReplayTime(0); // from start
double lapTime = pGame->timer.GetPlayerTime(0);
double rewTime = /*pSet->rpl_ghostrewind*/1 ? pGame->timer.GetRewindTimeGh(0) : lapTime;
bool collect = pSet->game.collect_num >= 0;
// iterate through all car models and set new pos info (from vdrift sim or replay)
CarModel* carM0 = carModels[0];
for (int c = 0; c < carModels.size(); ++c)
{
CarModel* carM = carModels[c];
bool bGhost = carM->isGhost();
CAR* pCar = carM->pCar;
PosInfo pi; // new, to fill data
// car perf test logic
//--------------------------------
if (bPerfTest && c==0)
newPerfTest(time);
// play get data from replay / ghost
///-----------------------------------------------------------------------
if (bGhost)
{ ///>> 🏞️👻 track's ghost
if (carM->isGhostTrk())
{
TrackFrame tf; // par: sec after, 1st lap
float lap1 = pGame->timer.GetCurrentLap(0) > 0 ? 2.f : 0.f;
bool ok = ghTrk.GetFrame(rewTime + lap1, &tf);
// car
Axes::toOgre(pi.pos, tf.pos);
pi.rot = Axes::toOgre(tf.rot);
pi.braking = tf.brake; pi.reverse = 0;
pi.steer = tf.steer / 127.f;
//pi.fboost = 0.f; pi.speed = 0.f; pi.percent = 0.f;
//pi.fHitTime = 0.f; pi.fParIntens = 0.f; pi.fParVel = 0.f;
//pi.vHitPos = Vector3::ZERO; pi.vHitNorm = Vector3::UNIT_Y;
// wheels
//dynamics.SetSteering(state.steer, pGame->GetSteerRange()); //peers can have other game settings..
for (int w=0; w < carM->numWheels; ++w)
{
MATHVECTOR<float,3> whP = carM->whPos[w];
whP[2] += 0.05f; // up
tf.rot.RotateVector(whP);
Axes::toOgre(pi.whPos[w], tf.pos + whP);
if (w < 2) // front steer
{ float a = (pi.steer * carM->maxangle) * -PI_d/180.f;
QUATERNION<float> q; q.Rotate(a, 0,0,1);
pi.whRot[w] = Axes::toOgreW(tf.rot * carM->qFixWh[w%2] * q);
}else
pi.whRot[w] = Axes::toOgreW(tf.rot * carM->qFixWh[w%2]);
}
}else ///>> 👻 ghost
{
ReplayFrame2 gf;
float ti = std::min((float)rewTime, ghPlay.GetTimeLength());
bool ok = ghPlay.GetFrame(ti, &gf, 0);
if (ok)
pi.FromRpl2(&gf, 0);
if (carM->vType == V_Sphere)
{ // weird fix, mini rot
pi.carY = Vector3::UNIT_Y;
pi.hov_roll = -pi.hov_roll;
}
}
}
else ///>> 📽️ replay
if (bRplPlay)
{
#ifdef DEBUG
assert(c < frm.size());
#endif
ReplayFrame2& rf = frm[c]; // frm also used in car.cpp for sounds
if (c < replay.header.numPlayers)
{
bool ok = replay.GetFrame(rplTime, &rf, c);
if (ok)
{ pi.FromRpl2(&rf, &pCar->dynamics);
pCar->SetPosition(rf.pos, rf.rot); // for objs hit
carM->trackPercent = rf.percent /255.f*100.f;
}else
{ carM->fCam->First();
pGame->timer.RestartReplay(0); //at end
} }
}
else ///>> sim, game - get data from vdrift
if (pCar)
{
pi.FromCar(pCar);
pi.percent = carM->trackPercent;
}
pi.bNew = true;
//<< ⏪ rewind
///-----------------------------------------------------------------------
if (!bRplPlay && !pGame->pause && !bGhost && pCar)
if (pCar->bRewind && pSet->game.rewind_type > 0)
{ // do rewind (go back)
double& gtime = pGame->timer.GetRewindTime(c);
gtime = std::max(0.0, gtime - time * gPar.rewindSpeed);
double& ghtim = pGame->timer.GetRewindTimeGh(c);
ghtim = std::max(0.0, ghtim - time * gPar.rewindSpeed); //rewind ghost time too
if (pSet->game.rewind_type == 2 || gPar.backTime)
{ pGame->timer.Back(c, - time * gPar.rewindSpeed);
ghost.DeleteFrames(0, ghtim);
}
RewindFrame rf;
bool ok = rewind.GetFrame(gtime, &rf, c);
pCar->SetPosRewind(rf.pos, rf.rot, rf.vel, rf.angvel);
pCar->dynamics.fDamage = rf.fDamage; // take damage back
if (carModels[c]->vType == V_Sphere)
pCar->dynamics.sphereYaw = rf.hov_roll;
carModels[c]->First();
}
else if (c < 4) // save data
{
const CARDYNAMICS& cd = pCar->dynamics;
RewindFrame fr;
fr.time = pGame->timer.GetRewindTime(c);
fr.pos = cd.body.GetPosition();
fr.rot = cd.body.GetOrientation();
fr.vel = cd.GetVelocity();
fr.angvel = cd.GetAngularVelocity();
fr.fDamage = cd.fDamage;
if (cd.vtype == V_Sphere)
fr.hov_roll = cd.sphereYaw;
else
fr.hov_roll = cd.hov_roll; //? fr.hov_throttle = cd.hov_throttle;
rewind.AddFrame(fr, c); // rec rewind
}
//<< 👻 record save data
///-----------------------------------------------------------------------
if (pSet->rpl_rec && !bRplPlay && !pGame->pause && !bGhost && pCar)
{
if (iRplSkip++ >= 1) // 1 half game framerate
{ iRplSkip = 0;
ReplayFrame2 fr;
fr.time = rplTime;
fr.percent = carM->trackPercent /100.f*255.f;
fr.FromCar(pCar, replay.GetLastHitTime(c));
replay.AddFrame(fr, c); // rec replay
if (c==0) /// rec ghost lap
{
fr.time = lapTime;
ghost.AddFrame(fr, 0);
}
// recorded info ..in update
{
int size = replay.GetNumFrames() * 232; //par approx sizeof(ReplayFrame);
std::string s = fToStr( float(size)/1000000.f, 2,5);
String ss = String( TR("#{RplRecTime}: ")) + StrTime(replay.GetTimeLength()) + TR(" #{RplSize}: ") + s + TR(" #{UnitMB}");
gui->valRplName2->setCaption(ss);
}
}
}
if (bRplPlay) gui->valRplName2->setCaption("");
///-----------------------------------------------------------------------
// 🏁 checkpoints, lap start
//-----------------------------------------------------------------------
if (bGhost && !gui->bLesson) // dont check for ghost
carM->bWrongChk = false;
else
{
/// 🔝 arrow update --------------------------------------
SplineRoad* road = scn->road;
if (pSet->check_arrow && carM->cType == CarModel::CT_LOCAL &&
!bRplPlay && hud->arrChk[c].node && road && road->mChks.size()>0)
hud->arrChk[c].UpdateChk(road, carM, pi.pos);
//----------------------------------------------------------------------------
if (carM->bGetStart) // get finish, end box
{ carM->bGetStart = false;
int st = !road || road->isLooped ? 0 : pSet->game.track_reversed ? 0 : 1;
carM->matStart.makeInverseTransform(
Axes::toOgre(scn->sc->startPos[st]), Vector3::UNIT_SCALE,
Axes::toOgre(scn->sc->startRot[st]));
carM->ResetChecks();
}
if (road && !carM->bGetStart)
{
// 🏁 finish/start box dist
Vector4 carP(pi.pos.x,pi.pos.y,pi.pos.z,1);
carM->vStartDist = carM0->matStart * carP; // start pos from 1st car always
carM->bInStart = abs(carM->vStartDist.x) < road->vStartBoxDim.x &&
abs(carM->vStartDist.y) < road->vStartBoxDim.y &&
abs(carM->vStartDist.z) < road->vStartBoxDim.z;
carM->iInChk = -1; carM->bWrongChk = false;
bool locar = carM->cType == CarModel::CT_LOCAL;
int ncs = road->mChks.size();
if (ncs > 0)
{
// 🏁 Finish --------------------------------------
if (locar && !bRplPlay &&
(carM->bInStart && carM->iNumChks == ncs && carM->iCurChk != -1))
{
/// Lap
bool finished = (pGame->timer.GetCurrentLap(c) >= pSet->game.num_laps)
&& (mClient || pSet->game.local_players > 1); // 📡 networked or 👥 splitscreen
bool best = finished || collect ? false : // dont inc laps when race over (in ^)
pGame->timer.Lap(c, !finished, pSet->game.track_reversed); //,boost_type?
double timeCur = pGame->timer.GetPlayerTimeTot(c);
// /📡 Network notification, send: car id, lap time ----
if (mClient && c == 0 && !finished)
mClient->lap(pGame->timer.GetCurrentLap(c), pGame->timer.GetLastLap(c));
/// new best lap, save ghost
bool newbest = false;
if (!pSet->rpl_bestonly || best) //- || pSet->game.rewind_type == 2 || gPar.backTime)
if (c==0 && pSet->rpl_rec) // for many, only 1st car
{
ghost.SaveFile(gui->GetGhostFile()); //,boost_type?
ghPlay.CopyFrom(ghost);
isGhost2nd = false; // hide 2nd ghost
newbest = true;
}
bool champ = pSet->game.champ_num >= 0, chall = pSet->game.chall_num >= 0;
bool chs = champ || chall;
if (!chs && !collect)
{ if (newbest) // 🔉
pGame->snd_lapbest->start();
else
pGame->snd_lap->start();
}
ghost.Clear();
carM->ResetChecks();
// 💨 restore boost fuel, each lap ----
if (pSet->game.boost_type == 1 && carM->pCar)
carM->pCar->dynamics.boostFuel = carM->pCar->dynamics.boostFuelStart;
// 🔨 damage decrease, each lap ---
if (pSet->game.damage_dec > 0.f)
carM->pCar->dynamics.fDamage = std::max(0.f,
carM->pCar->dynamics.fDamage - pSet->game.damage_dec);
// ⏱️ upd lap results ----
if (!collect)
{
carM->updLap = false;
carM->fLapAlpha = 1.f;
}
/// all laps
finished = pGame->timer.GetCurrentLap(c) >= pSet->game.num_laps;
if (finished && !mClient && !collect)
{
if (!chs)
{ // 👥 splitscreen winner places
if (carM->iWonPlace == 0)
{
if (pSet->game.local_players > 1)
{
int n = std::min(2, std::max(0, 3 - carIdWin));
pGame->snd_win[n]->start(); // 🔉
}
carM->iWonPlace = carIdWin++;
}
}
else if (champ)
gui->ChampionshipAdvance(timeCur);
else
gui->ChallengeAdvance(timeCur);
}
}
// 🔵 checkpoints --------------------------------------
for (int i=0; i < ncs; ++i)
{
const CheckSphere& cs = road->mChks[i];
Real d2 = pi.pos.squaredDistance(cs.pos);
if (d2 < cs.r2) // car in checkpoint
{
carM->iInChk = i;
//\ ➰🎥 loop camera change
if (pSet->cam_loop_chng && carM->fCam &&
cs.loop && (carM->iLoopChk == -1 || carM->iLoopChk != i) &&
pCar->dynamics.vtype != V_Sphere)
{
carM->iLoopChk = i;
if (carM->iLoopLastCam == -1)
{
carM->iLoopLastCam = carM->fCam->miCurrent;
carM->fCam->setCamera(pSet->cam_in_loop);
}else
{ carM->fCam->setCamera(carM->iLoopLastCam);
carM->iLoopLastCam = -1;
}
}
// next check ()
if (i == carM->iNextChk && carM->iNumChks < ncs)
{
carM->iCurChk = i; carM->iNumChks++;
carM->timeAtCurChk = pGame->timer.GetPlayerTime(c);
bool rev = pSet->game.track_reversed;
int inc = (rev ? -1 : 1) * road->iDir;
carM->iNextChk = (carM->iCurChk + inc + ncs) % ncs;
carM->UpdNextCheck();
// save car pos and rot
carM->pCar->SavePosAtCheck();
carM->updTimes = true;
// trail next start ->--
int id = carM->iIndex;
if (scn->trail[id] && carM->iNumChks < ncs-1) // skip last
scn->trail[id]->trailSegId =
(inc * (i - scn->road->iChkId1) + (rev ? 0 : 1) + ncs) % ncs;
if (pSet->s.snd_chk && locar)
pGame->snd_chk->start(); // 🔉
}
else
if (!collect && //?
carM->iInChk != carM->iCurChk && !bRplPlay &&
!scn->sc->noWrongChks) // denies
{
carM->bWrongChk = true; // ❌
if (carM->iInWrongChk != carM->iInChk)
{ carM->iInWrongChk = carM->iInChk;
if (pSet->s.snd_chkwr && locar)
pGame->snd_chkwr->start(); // 🔉
} }
break;
}
} }
} }
/// store new pos info in queue _________
if (!(isFocGui || isTweakTab()) || mClient) // dont if gui, but network always
{
int qn = (iCurPoses[c] + 1) % CarPosCnt; // next index in queue
carPoses[qn][c] = pi;
auto vt = carM->vType;
// 🎥 update camera
if (carM->fCam)
carM->fCam->update(
time, pi, &carPoses[qn][c], &pGame->collision,
!bRplPlay && pSet->cam_bounce,
vt == V_Sphere,
vt == V_Hovercar || vt == V_Drone || vt == V_Spaceship); //V*
iCurPoses[c] = qn; // atomic, set new index in queue
//)) 🔉🎥 upd sound camera
if (c == 0 && pGame->snd)
{
Vector3 x,y,z;
carPoses[qn][c].camRot.ToAxes(x,y,z);
bool camCar = carM->fCam && carM->fCam->TypeCar(); // fix
pGame->snd->setCamera(carPoses[qn][c].camPos, camCar ? -y : -z, camCar ? -z : y, Vector3::ZERO);
}
}
}
// 💎 collection game
//-----------------------------------------------------------------------
if (collect && lapTime > 0.1f) //-
UpdCollects();
PROFILER.endBlock(".newPos ");
}
// 💫 updatePoses - Set car pos for Ogre nodes, update particles, trails
//---------------------------------------------------------------------------------------------------------------
void App::updatePoses(float time)
{
if (carModels.empty()) return;
PROFILER.beginBlock(".updPos ");
// Update all carmodels from their carPos
const CarModel* playerCar = carModels.front();
int cgh = -1;
for (int c = 0; c < carModels.size(); ++c)
{
CarModel* carM = carModels[c];
if (!carM) {
PROFILER.endBlock(".updPos ");
return; }
/// 👻 ghosts visibility . . .
// hide when empty or near car
bool bGhostCar = carM->cType == (isGhost2nd ? CarModel::CT_GHOST2 : CarModel::CT_GHOST), // show only actual
bGhTrkVis = carM->isGhostTrk() && ghTrk.GetTimeLength()>0 && pSet->rpl_trackghost,
bGhostVis = ghPlay.GetNumFrames()>0 && pSet->rpl_ghost,
bGhostEnd = pGame->timer.GetPlayerTime(0) > ghPlay.GetTimeLength();
if (bGhostCar) cgh = c;
if (carM->isGhost()) // for all
{
bool loading = iLoad1stFrames >= 0; // show during load ?..
bool curVisible = carM->bVisible;
bool newVisible = bGhostVis && bGhostCar /**/&& !bGhostEnd/**/ || bGhTrkVis;
if (loading)
carM->setVisible(true); //!carM->isGhost());
else
{ // hide ghost when close to player
float d = carM->ndMain->getPosition().squaredDistance(playerCar->ndMain->getPosition());
if (d < pSet->ghoHideDist * pSet->ghoHideDist)
newVisible = false;
if (carM->isGhostTrk() && cgh >= 0) // hide track's ghost when near ghost
{
float d = carM->ndMain->getPosition().squaredDistance(carModels[cgh]->ndMain->getPosition());
if (d < pSet->ghoHideDistTrk * pSet->ghoHideDistTrk)
newVisible = false;
}
if (curVisible == newVisible)
carM->hideTime = 0.f;
else
{ carM->hideTime += time; // change vis after delay
if (carM->hideTime > gPar.ghostHideTime)
carM->setVisible(newVisible);
}
} }
// 🚗💫 update car pos ----
int q = iCurPoses[c];
int cc = (c + iRplCarOfs) % carModels.size(); // replay offset, camera from other car
int qq = iCurPoses[cc];
PosInfo& pi = carPoses[q][c], &pic = carPoses[qq][cc];
carM->Update(carPoses[q][c], carPoses[qq][cc], time);
if (mCubeCamera && c == 0) // 🔮 reflection
{ mCubeCamera->setPosition(carM->ndMain->getPosition());// carPoses[q][c].pos);
// mCubeCamera->setVisibilityFlags( RV_MaskReflect /*32*/ );
// mCubeCamera->setOrientation(carM->ndMain->getOrientation());
// mCubeCamera->_getFullTransformUpdated(); //-?
}
// nick text pos upd 3d to 2d
if (carM->pNickTxt && carM->ndMain)
{
Camera* cam = playerCar->fCam->cam->cam; //above car 1m
Vector3 p = hud->projectPoint(cam, carM->ndMain->getPosition() + Vector3(0,1.f,0));
p.x *= mDims[0].width0; // 1st viewport dims
p.y *= mDims[0].height0;
carM->pNickTxt->setPosition(p.x-40, p.y-16); // center doesnt work
carM->pNickTxt->setVisible(p.z > 0.f);
}
}
/// 📽️ Replay info
if (bRplPlay && !pGame->cars.empty())
{
double pos = pGame->timer.GetPlayerTime(0);
float len = replay.GetTimeLength();
gui->valRplPerc->setCaption(fToStr(pos/len*100.f, 1,4)+" %");
gui->valRplCur->setCaption(StrTime(pos));
gui->valRplLen->setCaption(StrTime(len));
float v = pos/len; //gui->slRplPos->setValue(v);
if (pos < 0.05) // rpl trail end
for (auto& carM : carModels)
carM->ResetChecks();
// 👈 lessons >> >
if (gui->bLesson)
{
if (v > 0.98f && !isFocGui) // end, back to gui
{ //bRplPause = true;
pSet->iMenu = MN_HowTo; isFocGui = true; gui->toggleGui(false);
// hud restore ..
gui->ckTimes.SetValue(1);
}
bool vis = false;
for (auto s : gui->rplSubtitles)
if (pos > s.beg && pos < s.end)
{
vis = true;
if (!mWndRplTxt->getVisible())
{
gui->rplSubText->setCaption(TR(s.txt));
gui->setHintImg(gui->rplSubImg, s.hintImg);
} break;
}
mWndRplTxt->setVisible(vis);
}
}
/// 📦 objects - dynamic (props) -------------------------------------------------------------
for (int i=0; i < scn->sc->objects.size(); ++i)
{
Object& o = scn->sc->objects[i];
if (o.ms)
{
btTransform tr, ofs;
o.ms->getWorldTransform(tr);
const btVector3& p = tr.getOrigin();
const btQuaternion& q = tr.getRotation();
o.pos[0] = p.x(); o.pos[1] = p.y(); o.pos[2] = p.z();
o.rot[0] = q.x(); o.rot[1] = q.y(); o.rot[2] = q.z(); o.rot[3] = q.w();
o.SetFromBlt();
if (o.playSound && // obj hit snd 🔉
(!o.playing || o.playGain > o.playingGain)) // quiet or louder
{ o.playSound =0;
if (pSet->s.snd_dynamic && !o.sound.empty())
{
pGame->snd->CreateDynamic(o.sound, o.nd, &o, o.playGain);
} }
}
}
PROFILER.endBlock(".updPos ");
}