Add compression for max speed

This commit is contained in:
Benau
2019-02-27 11:05:22 +08:00
parent cb1769acea
commit c00cf66b26
3 changed files with 79 additions and 33 deletions

View File

@@ -377,7 +377,7 @@ std::function<void()> KartRewinder::getLocalStateRestoreFunction()
// Max speed local state (terrain)
float current_fraction = m_max_speed->m_speed_decrease
[MaxSpeed::MS_DECREASE_TERRAIN].m_current_fraction;
float max_speed_fraction = m_max_speed->m_speed_decrease
uint16_t max_speed_fraction = m_max_speed->m_speed_decrease
[MaxSpeed::MS_DECREASE_TERRAIN].m_max_speed_fraction;
// Skidding local state

View File

@@ -26,6 +26,7 @@
#include <algorithm>
#include <assert.h>
#include <cstdlib>
/** This class handles maximum speed for karts. Several factors can influence
* the maximum speed a kart can drive, some will decrease the maximum speed,
@@ -97,11 +98,35 @@ void MaxSpeed::increaseMaxSpeed(unsigned int category, float add_speed,
assert(add_speed==0.0f || fade_out_time>0.01f);
assert(category>=MS_INCREASE_MIN && category <MS_INCREASE_MAX);
if (add_speed < 0.0f || engine_force < 0.0f)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"Negative add_speed %f or engine_force %f, ignored.",
add_speed, engine_force);
return;
}
int add_speed_i = (int)(add_speed * 1000.0f);
if (add_speed_i > 65535)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"%f add_speed too large.", add_speed);
add_speed_i = 65535;
}
int engine_force_i = (int)(engine_force * 10.0f);
if (engine_force_i > 65535)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"%f engine_force too large.", engine_force);
engine_force_i = 65535;
}
int16_t fade = 0;
if (fade_out_time > 32767)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"%d fade_out_time too large.");
"%d fade_out_time too large.", fade);
fade = (int16_t)32767;
}
else
@@ -110,17 +135,18 @@ void MaxSpeed::increaseMaxSpeed(unsigned int category, float add_speed,
int16_t dur = 0;
if (duration > 32767)
{
Log::warn("MaxSpeed::increaseMaxSpeed", "%d duration too large.");
Log::warn("MaxSpeed::increaseMaxSpeed",
"%d duration too large.", dur);
dur = (int16_t)32767;
}
else
dur = (int16_t)duration;
m_speed_increase[category].m_max_add_speed = add_speed;
m_speed_increase[category].m_max_add_speed = (uint16_t)add_speed_i;
m_speed_increase[category].m_duration = dur;
m_speed_increase[category].m_fade_out_time = fade;
m_speed_increase[category].m_current_speedup = add_speed;
m_speed_increase[category].m_engine_force = engine_force;
m_speed_increase[category].m_engine_force = (uint16_t)engine_force_i;
} // increaseMaxSpeed
// ----------------------------------------------------------------------------
@@ -171,6 +197,7 @@ void MaxSpeed::SpeedIncrease::update(int ticks)
if (m_duration == std::numeric_limits<int16_t>::min())
{
m_current_speedup = 0;
m_max_add_speed = 0;
return;
}
m_duration -= ticks;
@@ -179,23 +206,26 @@ void MaxSpeed::SpeedIncrease::update(int ticks)
{
m_duration = std::numeric_limits<int16_t>::min();
m_current_speedup = 0;
m_max_add_speed = 0;
return;
}
// If we are still in main max speed increase time, do nothing
if(m_duration >0) return;
m_current_speedup = (float)m_max_add_speed / 1000.0f;
if (m_duration > 0)
return;
// Now we are in the fade out period: decrease time linearly
m_current_speedup -= ticks*m_max_add_speed/m_fade_out_time;
m_current_speedup -= std::abs(m_duration - ticks) *
((float)m_max_add_speed / 1000.0f) / m_fade_out_time;
} // SpeedIncrease::update
// ----------------------------------------------------------------------------
void MaxSpeed::SpeedIncrease::saveState(BareNetworkString *buffer) const
{
buffer->addFloat(m_max_add_speed);
buffer->addUInt16(m_max_add_speed);
buffer->addUInt16(m_duration);
buffer->addUInt16(m_fade_out_time);
buffer->addFloat(m_current_speedup);
buffer->addFloat(m_engine_force);
buffer->addUInt16(m_engine_force);
} // saveState
// ----------------------------------------------------------------------------
@@ -204,11 +234,10 @@ void MaxSpeed::SpeedIncrease::rewindTo(BareNetworkString *buffer,
{
if(is_active)
{
m_max_add_speed = buffer->getFloat();
m_max_add_speed = buffer->getUInt16();
m_duration = buffer->getUInt16();
m_fade_out_time = buffer->getUInt16();
m_current_speedup = buffer->getFloat();
m_engine_force = buffer->getFloat();
m_engine_force = buffer->getUInt16();
}
else // make sure to disable this category
{
@@ -229,12 +258,27 @@ void MaxSpeed::setSlowdown(unsigned int category, float max_speed_fraction,
int fade_in_ticks, int duration)
{
assert(category>=MS_DECREASE_MIN && category <MS_DECREASE_MAX);
m_speed_decrease[category].m_max_speed_fraction = max_speed_fraction;
if (max_speed_fraction < 0.0f)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"Negative max_speed_fraction %f, ignored.",
max_speed_fraction);
return;
}
int max_speed_fraction_i = (int)(max_speed_fraction * 1000.0f);
if (max_speed_fraction_i > 65535)
{
Log::warn("MaxSpeed::increaseMaxSpeed",
"%f max_speed_fraction too large.", max_speed_fraction);
max_speed_fraction_i = 65535;
}
int16_t fade = 0;
if (fade_in_ticks > 32767)
{
Log::warn("MaxSpeed::setSlowdown", "%d fade_in_ticks too large.");
Log::warn("MaxSpeed::setSlowdown",
"%d fade_in_ticks too large.", fade);
fade = (int16_t)32767;
}
else
@@ -243,7 +287,8 @@ void MaxSpeed::setSlowdown(unsigned int category, float max_speed_fraction,
int16_t dur = 0;
if (duration > 32767)
{
Log::warn("MaxSpeed::setSlowdown", "%d duration too large.");
Log::warn("MaxSpeed::setSlowdown",
"%d duration too large.", dur);
dur = (int16_t)32767;
}
else
@@ -251,6 +296,8 @@ void MaxSpeed::setSlowdown(unsigned int category, float max_speed_fraction,
m_speed_decrease[category].m_fade_in_ticks = fade;
m_speed_decrease[category].m_duration = dur;
m_speed_decrease[category].m_max_speed_fraction =
(uint16_t)max_speed_fraction_i;
} // setSlowdown
// ----------------------------------------------------------------------------
@@ -267,22 +314,22 @@ void MaxSpeed::SpeedDecrease::update(int ticks)
{
m_duration = 0;
m_current_fraction = 1.0f;
m_max_speed_fraction = 1.0f;
m_max_speed_fraction = 1000;
return;
}
}
float diff = m_current_fraction - m_max_speed_fraction;
float diff = m_current_fraction - m_max_speed_fraction / 1000.0f;
if (diff > 0)
{
if (diff * m_fade_in_ticks > ticks)
m_current_fraction -= float(ticks) / float(m_fade_in_ticks);
else
m_current_fraction = m_max_speed_fraction;
m_current_fraction = m_max_speed_fraction / 1000.0f;
}
else
m_current_fraction = m_max_speed_fraction;
m_current_fraction = m_max_speed_fraction / 1000.0f;
} // SpeedDecrease::update
// ----------------------------------------------------------------------------
@@ -292,7 +339,7 @@ void MaxSpeed::SpeedDecrease::update(int ticks)
*/
void MaxSpeed::SpeedDecrease::saveState(BareNetworkString *buffer) const
{
buffer->addFloat(m_max_speed_fraction);
buffer->addUInt16(m_max_speed_fraction);
buffer->addFloat(m_current_fraction);
buffer->addUInt16(m_fade_in_ticks);
buffer->addUInt16(m_duration);
@@ -306,7 +353,7 @@ void MaxSpeed::SpeedDecrease::rewindTo(BareNetworkString *buffer,
{
if(is_active)
{
m_max_speed_fraction = buffer->getFloat();
m_max_speed_fraction = buffer->getUInt16();
m_current_fraction = buffer->getFloat();
m_fade_in_ticks = buffer->getUInt16();
m_duration = buffer->getUInt16();

View File

@@ -70,8 +70,8 @@ private:
class SpeedIncrease
{
public:
/** The maximum additional speed allowed. */
float m_max_add_speed;
/** The maximum additional speed allowed, 3 digits precision. */
uint16_t m_max_add_speed;
/** How long this speed will apply. This is used as a timer internally,
* to the duration will be decreased. When the duration is <0, the
* fade out time starts, and duration will go down to
@@ -79,11 +79,10 @@ private:
int16_t m_duration;
/** The fadeout time. */
int16_t m_fade_out_time;
/** The current max speed increase value. */
/** The current max speed increase value, updated with duration. */
float m_current_speedup;
/** Additional engine force. */
float m_engine_force;
/** Additional engine force, 1 digit precision. */
uint16_t m_engine_force;
/** The constructor initialised the values with a no-increase
* entry, i.e. an entry that does affect top speed at all. */
SpeedIncrease()
@@ -117,7 +116,7 @@ private:
/** Returns the additional engine force for this speed increase. */
float getEngineForce() const
{
return m_duration > 0 ? m_engine_force : 0;
return m_duration > 0 ? (float)m_engine_force / 10.0f : 0;
} // getEngineForce
// --------------------------------------------------------------------
/** Returns if this speed increase is active atm. */
@@ -129,8 +128,8 @@ private:
class SpeedDecrease
{
public:
/** The maximum slowdown to apply. */
float m_max_speed_fraction;
/** The maximum slowdown to apply, 3 digits precision. */
uint16_t m_max_speed_fraction;
/** The current slowdown fraction, taking the fade-in time
* into account. */
float m_current_fraction;
@@ -138,7 +137,7 @@ private:
/** How long it should take for the full slowdown to take effect. */
int16_t m_fade_in_ticks;
/** How long the effect should last. A -1.0f as value indicates
/** How long the effect should last. A -1 as value indicates
* that this effect stays active till it is changed back. */
int16_t m_duration;
@@ -152,7 +151,7 @@ private:
/** Resets the state to be inactive. */
void reset()
{
m_max_speed_fraction = 1.0f;
m_max_speed_fraction = 1000;
m_current_fraction = 1.0f;
m_fade_in_ticks = 0;
m_duration = 0;