File: BikeGhost.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (472 lines) | stat: -rw-r--r-- 15,421 bytes parent folder | download | duplicates (2)
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
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#include "BikeGhost.h"
#include "../Replay.h"
#include "../GameText.h"
#include "../helpers/Text.h"
#include "../GameEvents.h"
#include "../Theme.h"
#include "Level.h"

#define INTERPOLATION_MAXIMUM_TIME  300
#define INTERPOLATION_MAXIMUM_SPACE 5.0

Ghost::Ghost(PhysicsSettings* i_physicsSettings,
	     bool i_engineSound,
	     Theme *i_theme, BikerTheme* i_bikerTheme,
	     const TColor& i_colorFilter,
	     const TColor& i_uglyColorFilter)
: Biker(i_physicsSettings, i_engineSound, i_theme, i_bikerTheme, i_colorFilter, i_uglyColorFilter) {
  m_diffToPlayer = 0.0;
  m_reference = false;
}

Ghost::~Ghost() {
}

float Ghost::diffToPlayer() const {
  return m_diffToPlayer;
}

bool Ghost::getRenderBikeFront() {
  return true;
}

void Ghost::setReference(bool i_value) {
  m_reference = i_value;
}

bool Ghost::isReference() const {
  return m_reference;
}

void Ghost::setInfo(const std::string& i_info) {
  m_info = i_info;
}

FileGhost::FileGhost(std::string i_replayFile, PhysicsSettings* i_physicsSettings,
		     bool i_isActiv,
		     bool i_engineSound,
		     Theme *i_theme, BikerTheme* i_bikerTheme,
		     const TColor& i_colorFilter,
		     const TColor& i_uglyColorFilter)
: Ghost (i_physicsSettings, i_engineSound, i_theme, i_bikerTheme, i_colorFilter, i_uglyColorFilter) {
  std::string v_levelId;
  std::string v_playerName;

  m_replay = new Replay();
  v_levelId = m_replay->openReplay(i_replayFile, v_playerName);

  // 4 states for cubical interpolation

  // 50% of states are before the time T, 50% are after
  m_ghostBikeStates.push_back(new BikeState(m_physicsSettings));
  m_ghostBikeStates.push_back(new BikeState(m_physicsSettings));
  m_ghostBikeStates.push_back(new BikeState(m_physicsSettings));
  m_ghostBikeStates.push_back(new BikeState(m_physicsSettings));


  for(unsigned int i=0; i<m_ghostBikeStates.size(); i++) {
    m_replay->peekState(m_ghostBikeStates[i], m_physicsSettings);
  }
  *m_bikeState = *(m_ghostBikeStates[0]); // copy

  m_isActiv = i_isActiv;
  m_linearVelocity = 0.0;
  m_teleportationOccured = false;
}

FileGhost::~FileGhost() {
  for(unsigned int i=0; i<m_ghostBikeStates.size(); i++) {
    delete m_ghostBikeStates[i];
  }
}

void FileGhost::execReplayEvents(int i_time, Scene *i_motogame) {
  std::vector<RecordedGameEvent *> *v_replayEvents;
  v_replayEvents = m_replay->getEvents();
  
  /* Start looking for events that should be passed */
  for(unsigned int i=0;i<v_replayEvents->size();i++) {
    /* Not passed? And with a time stamp that tells it should have happened
       by now? */
    if(!(*v_replayEvents)[i]->bPassed && (*v_replayEvents)[i]->Event->getEventTime() < i_time) {
      /* Nice. Handle this event, replay style */
      i_motogame->handleEvent((*v_replayEvents)[i]->Event);
      
      /* Pass it */
      (*v_replayEvents)[i]->bPassed = true;
    }
  }
  
  /* Now see if we have moved back in time and whether we should apply some
     REVERSE events */
  for(int i=v_replayEvents->size()-1;i>=0;i--) {
    /* Passed? And with a time stamp larger than current time? */
    if((*v_replayEvents)[i]->bPassed && (*v_replayEvents)[i]->Event->getEventTime() > i_time) {
      /* Nice. Handle this event, replay style BACKWARDS */
      (*v_replayEvents)[i]->Event->revert(i_motogame);
      
      /* Un-pass it */
      (*v_replayEvents)[i]->bPassed = false;
    }
  }

  // apply moving blocks
  std::vector<rmtime>* v_mb = m_replay->getMovingBlocks();
  for(unsigned int i=0; i<v_mb->size(); i++) {
    // reference the block
    if((*v_mb)[i].block == NULL) {
      (*v_mb)[i].block = i_motogame->getLevelSrc()->getBlockById((*v_mb)[i].name);
    }

    //
    if((*v_mb)[i].states.size() > 0) {

      // continue to read the time
      while((*v_mb)[i].states[(*v_mb)[i].readPos].time < i_time) {
	(*v_mb)[i].readPos++;
      }

      // not at the end
      if((*v_mb)[i].readPos < (*v_mb)[i].states.size()) {
	// and time is found
	if((*v_mb)[i].states[(*v_mb)[i].readPos].time == i_time) {
	  // apply the state
	  i_motogame->SetBlockPos((*v_mb)[i].block,
				  (*v_mb)[i].states[(*v_mb)[i].readPos].position.x,
				  (*v_mb)[i].states[(*v_mb)[i].readPos].position.y);
	  i_motogame->SetBlockRotation((*v_mb)[i].block, (*v_mb)[i].states[(*v_mb)[i].readPos].rotation);
	} else {
	  // interpolation
	  if(m_doInterpolation) {
	    // ok between frame 2 and n-1
	    if((*v_mb)[i].readPos > 0) {
	      float pertime = 
		((float)(i_time - (*v_mb)[i].states[(*v_mb)[i].readPos-1].time)) /
		((float)((*v_mb)[i].states[(*v_mb)[i].readPos].time - (*v_mb)[i].states[(*v_mb)[i].readPos-1].time));
	      
	      float new_pos_x = (*v_mb)[i].states[(*v_mb)[i].readPos-1].position.x +
		((*v_mb)[i].states[(*v_mb)[i].readPos].position.x - (*v_mb)[i].states[(*v_mb)[i].readPos-1].position.x)*pertime;
	      
	      float new_pos_y = (*v_mb)[i].states[(*v_mb)[i].readPos-1].position.y +
		((*v_mb)[i].states[(*v_mb)[i].readPos].position.y - (*v_mb)[i].states[(*v_mb)[i].readPos-1].position.y)*pertime;

	      float new_pos_r = interpolateAngle((*v_mb)[i].states[(*v_mb)[i].readPos-1].rotation, (*v_mb)[i].states[(*v_mb)[i].readPos].rotation, pertime);

	      i_motogame->SetBlockPos((*v_mb)[i].block, new_pos_x, new_pos_y);
	      i_motogame->SetBlockRotation((*v_mb)[i].block, new_pos_r);
	    }
	  }
	}
      }
    }
  }
}

std::string FileGhost::getDescription() const {
  char c_tmp[1024];

  snprintf(c_tmp, 1024,
	   GAMETEXT_GHOSTOF,
	   m_replay->getPlayerName().c_str());

  return
    std::string(c_tmp)   +
    "\n(" + m_info + ")" +
    "\n(" + formatTime(m_replay->getFinishTime()) + ")";
}

std::string FileGhost::getVeryQuickDescription() const {
  return m_replay->getPlayerName();
}

std::string FileGhost::getQuickDescription() const {
  char c_tmp[1024];

  snprintf(c_tmp, 1024,
	   GAMETEXT_GHOSTOF,
	   m_replay->getPlayerName().c_str()); 

  return std::string(c_tmp);
}

std::string FileGhost::playerName() {
  return m_replay->getPlayerName();
}

void FileGhost::initLastToTakeEntities(Level* i_level) {
  std::vector<RecordedGameEvent *> *v_replayEvents;
  v_replayEvents = m_replay->getEvents();
    
  m_lastToTakeEntities.clear();
  m_diffToPlayer = 0.0;

  /* Start looking for events */
  for(unsigned int i=0; i<v_replayEvents->size(); i++) {
    SceneEvent *v_event = (*v_replayEvents)[i]->Event;
      
    if(v_event->getType() == GAME_EVENT_ENTITY_DESTROYED) {
      if(i_level->getEntityById(((MGE_EntityDestroyed*)v_event)->EntityId())->IsToTake()) {
	/* new Strawberry for ghost */
	m_lastToTakeEntities.push_back((*v_replayEvents)[i]->Event->getEventTime());
      }
    }
  }
}

void FileGhost::updateDiffToPlayer(std::vector<float> &i_lastToTakeEntities) {
  /* no strawberry, no update */
  if(i_lastToTakeEntities.size() == 0) {
    return;
  }
    
  /* the ghost did not get this number of strawberries */
  if(m_lastToTakeEntities.size() < i_lastToTakeEntities.size() ) {
    return;
  }
    
  m_diffToPlayer = i_lastToTakeEntities[i_lastToTakeEntities.size()-1]
                 - m_lastToTakeEntities[i_lastToTakeEntities.size()-1];
}

void FileGhost::initToPosition(Vector2f i_position, DriveDir i_direction, Vector2f i_gravity) {
  m_teleportationOccured = true;
  m_linearVelocity = 0.0;
}

void FileGhost::updateToTime(int i_time, int i_timeStep,
			 CollisionSystem *i_collisionSystem, Vector2f i_gravity,
			 Scene *i_motogame) {
  Biker::updateToTime(i_time, i_timeStep, i_collisionSystem, i_gravity, i_motogame);
  DriveDir v_previousDir = m_bikeState->Dir;

  /* back in the past */
  // m_ghostBikeStates.size()/2-1 : it's the more recent frame in the past
  if(m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime > i_time) {
    m_replay->fastrewind(m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime - i_time, 1);

    for(unsigned int i=0; i<m_ghostBikeStates.size(); i++) {
      m_replay->peekState(m_ghostBikeStates[i], m_physicsSettings);
    }

    m_finished = false;
    m_dead     = false;
  }

  /* stop the motor at the end */
  if(m_replay->endOfFile() && m_ghostBikeStates[m_ghostBikeStates.size()-1]->GameTime <= i_time) { // end of file, and interpolation is finished (before last future frame is in the feature)
    m_bikeState->fBikeEngineRPM = 0.0;
    m_replay->peekState(m_ghostBikeStates[m_ghostBikeStates.size()-1], m_physicsSettings); // take the last frame
    *m_bikeState = *(m_ghostBikeStates[m_ghostBikeStates.size()-1]); // copy last frame
    m_linearVelocity = 0.0;

    if(m_replay->didFinish()) {
      m_finished   = true;
      m_finishTime = m_replay->getFinishTime();
    } else {
      m_dead     = true;
      m_deadTime = m_bikeState->GameTime;
    }
  } else {
    BikeState* v_state;
    // to compute the velocity
    Vector2f v_beforePos;
    Vector2f v_afterPos;
    float v_beforeTime;
    float v_afterTime;

    // m_ghostBikeStates.size()/2 : first state in the feature
    if(m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime < i_time) {
      
      bool v_didRead = false;
      do {
	// move the bikestates
	v_state = m_ghostBikeStates[0];
	for(unsigned int i=0; i<m_ghostBikeStates.size()-1; i++) {
	  m_ghostBikeStates[i] = m_ghostBikeStates[i+1];
	}
	m_ghostBikeStates[m_ghostBikeStates.size()-1] = v_state;

	if(m_replay->endOfFile()) {
	  // copy n-1 to n
	  *(m_ghostBikeStates[m_ghostBikeStates.size()-1]) = *(m_ghostBikeStates[m_ghostBikeStates.size()-1-1]);
	  v_didRead = false;
	} else {
	  // read the replay
	  m_replay->loadState(m_ghostBikeStates[m_ghostBikeStates.size()-1], m_physicsSettings);
	  m_replay->loadState(m_bikeState, m_physicsSettings);
	  v_didRead = true;
	}
      } while(m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime < i_time && v_didRead);


      *m_bikeState = *(m_ghostBikeStates[m_ghostBikeStates.size()/2-1]); // copy

      v_beforePos  = m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->CenterP;
      v_beforeTime = m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime;
      v_afterPos   = m_ghostBikeStates[m_ghostBikeStates.size()/2]->CenterP;
      v_afterTime  = m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime;
      
      // warning, absolutly no idea why * 2.0, but it is needed to work... (*100 because hundreadths, *3.6 for km/h)
      if(v_afterTime - v_beforeTime > 0) {
	m_linearVelocity = (Vector2f(v_afterPos - v_beforePos)).length()*100.0*3.6*2.0 / ((float)(v_afterTime - v_beforeTime));
      }

    } else { /* interpolation */
      /* do interpolation if it doesn't seems to be a teleportation or something like that */
      /* in fact, this is not nice ; the best way would be to test if there is a teleport event between the two frames */
      float v_distance = Vector2f(Vector2f(m_ghostBikeStates[m_ghostBikeStates.size()/2]->CenterP.x,
					   m_ghostBikeStates[m_ghostBikeStates.size()/2]->CenterP.y) -
				  Vector2f(m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->CenterP.x,
					   m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->CenterP.y)
				  ).length();
      bool v_can_interpolate =
	// interpolate only if the frame are near in the time
	m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime - m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime < INTERPOLATION_MAXIMUM_TIME &&
	// interpolate only if the state are near in the space
	v_distance < INTERPOLATION_MAXIMUM_SPACE;
      if(m_teleportationOccured) {
	v_can_interpolate = false; // in this case, we sure that interpolation occurred; (but in case of a ghost, we don't read the events)
      }

      if(m_doInterpolation && v_can_interpolate) {
	if(m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime - m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime > 0) {
	  /* INTERPOLATED FRAME */
	  float v_interpolation_value;

	  v_interpolation_value = (i_time - m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime) 
	    /((float)(m_ghostBikeStates[m_ghostBikeStates.size()/2]->GameTime - m_ghostBikeStates[m_ghostBikeStates.size()/2-1]->GameTime));

	  BikeState::interpolateGameState(m_ghostBikeStates, m_bikeState, v_interpolation_value);
	}
      }
    }
  }

  /* update change position */
  if(v_previousDir != m_bikeState->Dir) {
    m_changeDirPer = 0.0;
  }

  if(m_isActiv) {
    m_teleportationOccured = false;
    execReplayEvents(i_time, i_motogame);
  }
}

int FileGhost::getFinishTime() {
  return m_replay->getFinishTime();
}

std::string FileGhost::levelId() {
   return m_replay->getLevelId();
}

float FileGhost::getBikeLinearVel() {
  return m_linearVelocity;
}

float FileGhost::getTorsoVelocity() {
  return 0.0;
}

float FileGhost::getBikeEngineSpeed() {
  return 0.0; /* unable to know it */
}

double FileGhost::getAngle() {
  return 0.0; /* to do */
}

NetGhost::NetGhost(PhysicsSettings* i_physicsSettings,
		   bool i_engineSound,
		   Theme *i_theme, BikerTheme* i_bikerTheme,
		   const TColor& i_colorFilter,
		   const TColor& i_uglyColorFilter) 
: Ghost(i_physicsSettings, i_engineSound, i_theme, i_bikerTheme, i_colorFilter, i_uglyColorFilter) {
}

NetGhost::~NetGhost() {
}

void NetGhost::updateDiffToPlayer(std::vector<float> &i_lastToTakeEntities) {
}

std::string NetGhost::getVeryQuickDescription() const {
  return m_info;
}

std::string NetGhost::getQuickDescription() const {
  char c_tmp[256];
  snprintf(c_tmp, 256, GAMETEXT_WEBGHOSTOF, m_info.c_str()); 
  return std::string(c_tmp);
}

std::string NetGhost::getDescription() const {
  char c_tmp[256];
  snprintf(c_tmp, 256, GAMETEXT_WEBGHOSTOF, m_info.c_str()); 
  return std::string(c_tmp);
}

float NetGhost::getBikeEngineSpeed() {
  return 0.0; /* unable to know it */
}

float NetGhost::getBikeLinearVel() {
  return 0.0; /* to do */
}

float NetGhost::getTorsoVelocity() {
 return 0.0;
}

double NetGhost::getAngle() {
  return 0.0; /* to do */
}

ReplayBiker::ReplayBiker(std::string i_replayFile, PhysicsSettings* i_physicsSettings,
			 bool i_engineSound,
			 Theme *i_theme, BikerTheme* i_bikerTheme)
:FileGhost(i_replayFile, i_physicsSettings, true, i_engineSound, i_theme, i_bikerTheme,
       TColor(255, 255, 255, 0),
       TColor(GET_RED(i_bikerTheme->getUglyRiderColor()),
	      GET_GREEN(i_bikerTheme->getUglyRiderColor()),
	      GET_BLUE(i_bikerTheme->getUglyRiderColor()),
	      GET_ALPHA(i_bikerTheme->getUglyRiderColor()))) {
}

std::string ReplayBiker::getQuickDescription() const {
  char c_tmp[1024];
  
  snprintf(c_tmp, 1024,
	   GAMETEXT_REPLAYOF,
	   m_replay->getPlayerName().c_str()); 
  
  return std::string(c_tmp);
}

std::string ReplayBiker::getVeryQuickDescription() const {
  return m_replay->getPlayerName();
}