Make rewind work with history replay (when replaying kart controls),

which allows to have reproducible test cases.
This commit is contained in:
hiker
2016-09-13 09:58:42 +10:00
parent f8b48a0313
commit 457cb700d2
4 changed files with 53 additions and 22 deletions

View File

@@ -116,9 +116,6 @@ public:
// ------------------------------------------------------------------------
/** Returns all controls of this kart - const version. */
const KartControl& getControls() const { return m_controls; }
// ------------------------------------------------------------------------
/** Sets the kart controls. Used e.g. by replaying history. */
void setControls(const KartControl &c) { m_controls = c; }
// ========================================================================
// Access to the kart properties.

View File

@@ -46,14 +46,31 @@ void KartControl::rewind(BareNetworkString *buffer)
}
} // rewind
// ------------------------------------------------------------------------
/** Sets this KartControl form the given value (basically a copy). This
* function uses the explicit setSteer() etc function, which means that
* rewind information will be collected.
*/
void KartControl::set(const KartControl &c)
{
setAccel(c.getAccel());
setBrake(c.getBrake());
setFire(c.getFire());
setLookBack(c.getLookBack());
setNitro(c.getNitro());
setRescue(c.getRescue());
setSkidControl(c.getSkidControl());
setSteer(c.getSteer());
} // set
// ------------------------------------------------------------------------
/** Sets the current steering value. */
void KartControl::setSteer(float f)
{
float old_steer = m_steer;
m_steer = f;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_steer != m_steer)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_steer != m_steer )
{
// Save full status
BareNetworkString *buffer = new BareNetworkString(getLength());
@@ -68,8 +85,8 @@ void KartControl::setAccel(float f)
{
float old_accel = m_accel;
m_accel = f;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_accel != m_accel)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_accel != m_accel )
{
BareNetworkString *buffer = new BareNetworkString(getLength());
copyToBuffer(buffer);
@@ -81,10 +98,10 @@ void KartControl::setAccel(float f)
/** Sets if the kart is braking. */
void KartControl::setBrake(bool b)
{
bool old_brake = m_brake;
m_brake = b;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_brake != m_brake)
bool old_brake = m_brake;
m_brake = b;
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_brake != m_brake )
{
// Only store the buttons in this case
BareNetworkString *buffer = new BareNetworkString(1);
@@ -98,8 +115,8 @@ void KartControl::setNitro(bool b)
{
bool old_nitro = m_nitro;
m_nitro = b;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_nitro != m_nitro)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_nitro != m_nitro )
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
@@ -113,8 +130,8 @@ void KartControl::setSkidControl(SkidControl sc)
{
SkidControl old_skid = m_skid;
m_skid = sc;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_skid != m_skid)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_skid != m_skid )
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
@@ -128,8 +145,8 @@ void KartControl::setRescue(bool b)
{
bool old_rescue = m_rescue;
m_rescue = b;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_rescue != m_rescue)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_rescue != m_rescue)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
@@ -143,8 +160,8 @@ void KartControl::setFire(bool b)
{
bool old_fire = m_fire;
m_fire = b;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_fire != m_fire)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_fire != m_fire )
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());
@@ -158,8 +175,8 @@ void KartControl::setLookBack(bool b)
{
bool old_look = m_look_back;
m_look_back = b;
RewindManager *re = RewindManager::get();
if (re->isEnabled() && !re->isRewinding() && old_look != m_look_back)
if (RewindManager::isEnabled() && !RewindManager::get()->isRewinding() &&
old_look != m_look_back)
{
BareNetworkString *buffer = new BareNetworkString(1);
buffer->addUInt8(getButtonsCompressed());

View File

@@ -61,6 +61,7 @@ public:
void setRescue(bool b);
void setFire(bool b);
void setLookBack(bool b);
void set(const KartControl &c);
// ------------------------------------------------------------------------
KartControl()

View File

@@ -23,6 +23,7 @@
#include "io/file_manager.hpp"
#include "modes/world.hpp"
#include "karts/abstract_kart.hpp"
#include "network/rewind_manager.hpp"
#include "physics/physics.hpp"
#include "race/race_manager.hpp"
#include "tracks/track.hpp"
@@ -131,9 +132,17 @@ void History::updateReplay(float dt)
{
Log::info("History", "Replay finished");
m_current = 0;
// This is useful to use a reproducable rewind problem:
// replay it with history, for debugging only
#undef DO_REWIND_AT_END_OF_HISTORY
#ifdef DO_REWIND_AT_END_OF_HISTORY
RewindManager::get()->rewindTo(5.0f);
exit(-1);
#else
// Note that for physics replay all physics parameters
// need to be reset, e.g. velocity, ...
world->reset();
#endif
}
unsigned int num_karts = world->getNumKarts();
for(unsigned k=0; k<num_karts; k++)
@@ -147,7 +156,7 @@ void History::updateReplay(float dt)
}
else
{
kart->setControls(m_all_controls[index]);
kart->getControls().set(m_all_controls[index]);
}
}
} // updateReplay
@@ -314,6 +323,11 @@ void History::Load()
sscanf(s, "delta: %f\n",&m_all_deltas[i]);
}
// We need to disable the rewind manager here (otherwise setting the
// KartControl data would access the rewind manager).
bool rewind_manager_was_enabled = RewindManager::isEnabled();
RewindManager::setEnable(false);
for(int i=0; i<m_size; i++)
{
for(unsigned int k=0; k<num_karts; k++)
@@ -334,6 +348,8 @@ void History::Load()
m_all_controls[index].setButtonsCompressed(char(buttonsCompressed));
} // for i
} // for k
RewindManager::setEnable(rewind_manager_was_enabled);
fprintf(fd, "History file end.\n");
fclose(fd);
} // Load