Added MaxSpeed to saved state of each kart.

This commit is contained in:
hiker
2016-09-07 08:15:09 +10:00
parent 7d7b98564f
commit 6245cea609
3 changed files with 173 additions and 8 deletions

View File

@@ -17,9 +17,11 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "karts/kart_rewinder.hpp"
#include "karts/abstract_kart.hpp"
#include "items/attachment.hpp"
#include "items/powerup.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/max_speed.hpp"
#include "modes/world.hpp"
#include "network/rewind_manager.hpp"
#include "network/network_string.hpp"
@@ -82,6 +84,11 @@ BareNetworkString* KartRewinder::saveState() const
// 4) Powerup
// ----------
getPowerup()->saveState(buffer);
// 5) Max speed info
// ------------------
m_max_speed->saveState(buffer);
return buffer;
} // saveState
@@ -113,6 +120,10 @@ void KartRewinder::rewindToState(BareNetworkString *buffer)
// 4) Powerup
// ----------
getPowerup()->rewindTo(buffer);
// 5) Max speed info
// ------------------
m_max_speed->rewindTo(buffer);
return;
} // rewindToState

View File

@@ -135,8 +135,7 @@ void MaxSpeed::instantSpeedIncrease(unsigned int category,
m_kart->getVehicle()->instantSpeedIncreaseTo(speed);
}
// instantSpeedIncrease
} // instantSpeedIncrease
// ----------------------------------------------------------------------------
/** Handles the update of speed increase objects. The m_duration variable
@@ -161,6 +160,33 @@ void MaxSpeed::SpeedIncrease::update(float dt)
m_current_speedup -= dt*m_max_add_speed/m_fade_out_time;
} // SpeedIncrease::update
// ----------------------------------------------------------------------------
void MaxSpeed::SpeedIncrease::saveState(BareNetworkString *buffer) const
{
buffer->addFloat(m_max_add_speed);
buffer->addFloat(m_duration);
buffer->addFloat(m_fade_out_time);
buffer->addFloat(m_current_speedup);
buffer->addFloat(m_engine_force);
} // saveState
// ----------------------------------------------------------------------------
void MaxSpeed::SpeedIncrease::rewindTo(BareNetworkString *buffer,
bool is_active)
{
if(is_active)
{
m_max_add_speed = buffer->getFloat();
m_duration = buffer->getFloat();
m_current_speedup = buffer->getFloat();
m_engine_force = buffer->getFloat();
}
else // make sure to disable this category
{
reset();
}
} // restoreState
// ----------------------------------------------------------------------------
/** Defines a slowdown, which is in fraction of top speed.
* \param category The category for which the speed is increased.
@@ -210,6 +236,38 @@ void MaxSpeed::SpeedDecrease::update(float dt)
m_current_fraction = m_max_speed_fraction;
} // SpeedDecrease::update
// ----------------------------------------------------------------------------
/** Saves the state of an (active) speed decrease category. It is not called
* if the speed decrease is not active.
* \param buffer Buffer which will store the state information.
*/
void MaxSpeed::SpeedDecrease::saveState(BareNetworkString *buffer) const
{
buffer->addFloat(m_max_speed_fraction);
buffer->addFloat(m_fade_in_time);
buffer->addFloat(m_current_fraction);
buffer->addFloat(m_duration);
} // saveState
// ----------------------------------------------------------------------------
/** Restores a previously saved state for an active speed decrease category.
*/
void MaxSpeed::SpeedDecrease::rewindTo(BareNetworkString *buffer,
bool is_active)
{
if(is_active)
{
m_max_speed_fraction = buffer->getFloat();
m_fade_in_time = buffer->getFloat();
m_current_fraction = buffer->getFloat();
m_duration = buffer->getFloat();
}
else // make sure it is not active
{
reset();
}
} // restoreState
// ----------------------------------------------------------------------------
/** Returns how much increased speed time is left over in the given category.
* \param category Which category to report on.
@@ -267,3 +325,71 @@ void MaxSpeed::update(float dt)
} // update
// ----------------------------------------------------------------------------
/** Saves the speed data in a network string for rewind.
* \param buffer Pointer to the network string to store the data.
*/
void MaxSpeed::saveState(BareNetworkString *buffer) const
{
// Save the slowdown states
// ------------------------
// Get the bit pattern of all active slowdowns
uint8_t active_slowdown = 0;
for(unsigned int i=MS_DECREASE_MIN; i<MS_DECREASE_MAX; i++)
{
active_slowdown <<= 1;
if (m_speed_decrease[i].isActive())
active_slowdown |= 1;
}
buffer->addUInt8(active_slowdown);
for(unsigned int i=MS_DECREASE_MIN, b=1; i<MS_DECREASE_MAX; i++, b <<= 1)
{
if (active_slowdown & b)
m_speed_decrease->saveState(buffer);
}
// Now save the speedup state
// --------------------------
// Get the bit pattern of all active speedups
uint8_t active_speedups = 0;
for(unsigned int i=MS_INCREASE_MIN; i<MS_INCREASE_MAX; i++)
{
active_speedups <<= 1;
if(m_speed_increase[i].isActive()) active_speedups |= 1;
}
buffer->addUInt8(active_speedups);
for(unsigned int i=MS_INCREASE_MIN, b=1; i<MS_INCREASE_MAX; i++, b <<= 1)
{
if(active_speedups & b)
m_speed_increase[i].saveState(buffer);
}
} // saveState
// ----------------------------------------------------------------------------
/** Restore a saved state.
* \param buffer Saved state.
*/
void MaxSpeed::rewindTo(BareNetworkString *buffer)
{
// Restore the slowdown states
// ---------------------------
// Get the bit pattern of all active slowdowns
uint8_t active_slowdown = buffer->getUInt8();
for(unsigned int i=MS_DECREASE_MIN, b=1; i<MS_DECREASE_MAX; i++, b <<= 1)
{
m_speed_decrease->rewindTo(buffer, (active_slowdown & b) == b);
}
// Restore the speedup state
// --------------------------
// Get the bit pattern of all active speedups
uint8_t active_speedups = buffer->getUInt8();
for(unsigned int i=MS_INCREASE_MIN, b=1; i<MS_INCREASE_MAX; i++, b <<= 1)
{
m_speed_increase[i].rewindTo(buffer, (active_speedups & b) == b);
}
} // rewindoTo

View File

@@ -22,6 +22,7 @@
/** \defgroup karts */
class AbstractKart;
class BareNetworkString;
class MaxSpeed
{
@@ -81,18 +82,27 @@ private:
/** The constructor initialised the values with a no-increase
* entry, i.e. an entry that does affect top speed at all. */
SpeedIncrease()
{
reset();
} // SpeedIncrease
// --------------------------------------------------------------------
/** Resets this increase category to be not active. */
void reset()
{
m_max_add_speed = 0;
m_duration = -9999999;
m_fade_out_time = 0;
m_current_speedup = 0;
m_engine_force = 0;
} // SpeedIncrease
} // reset
// --------------------------------------------------------------------
void update(float dt);
/** Returns the current speedup for this category. */
void saveState(BareNetworkString *buffer) const;
void rewindTo(BareNetworkString *buffer, bool is_active);
// --------------------------------------------------------------------
/** Returns the current speedup for this category. */
float getSpeedIncrease() const {return m_current_speedup;}
// --------------------------------------------------------------------
/** Returns the remaining time till the fade out time starts.
* Note that this function will return a negative value if
* the fade_out time has started or this speed increase has
@@ -103,7 +113,10 @@ private:
float getEngineForce() const
{
return m_duration > 0 ? m_engine_force : 0;
}
} // getEngineForce
// --------------------------------------------------------------------
/** Returns if this speed increase is active atm. */
bool isActive() const { return m_duration > -m_fade_out_time; }
}; // SpeedIncrease
// ------------------------------------------------------------------------
@@ -126,17 +139,30 @@ private:
/** The constructor initialises the data with data that won't
* affect top speed at all. */
SpeedDecrease()
{
reset();
} // SpeedDecrease
// --------------------------------------------------------------------
/** Resets the state to be inactive. */
void reset()
{
m_max_speed_fraction = 1.0f;
m_fade_in_time = 0.0f;
m_current_fraction = 1.0f;
m_duration = -1.0f;
} // SpeedDecrease
m_duration = 0.0f;
} //reset
// --------------------------------------------------------------------
void update(float dt);
void saveState(BareNetworkString *buffer) const;
void rewindTo(BareNetworkString *buffer, bool is_active);
// --------------------------------------------------------------------
/** Returns the current slowdown fracftion, taking a 'fade in'
* into account. */
float getSlowdownFraction() const {return m_current_fraction;}
// --------------------------------------------------------------------
/** Returns if this speed decrease is active atm. A duration of
* -1 indicates an ongoing effect. */
bool isActive() const { return m_duration > 0 || m_duration <= -1.0f; }
}; // SpeedDecrease
// ------------------------------------------------------------------------
@@ -164,6 +190,8 @@ public:
float getSpeedIncreaseTimeLeft(unsigned int category);
void update(float dt);
void reset();
void saveState(BareNetworkString *buffer) const;
void rewindTo(BareNetworkString *buffer);
// ------------------------------------------------------------------------
/** Sets the minimum speed a kart should have. This is used to guarantee
* that e.g. zippers on ramps will always fast enough for the karts to