osb/source/game/StarInterpolationTracker.cpp

66 lines
2.0 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarInterpolationTracker.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
constexpr double VanillaStepsPerSecond = 60.0;
2023-06-20 04:33:09 +00:00
InterpolationTracker::InterpolationTracker(Json config) {
if (config.isNull()) {
config = JsonObject();
} else if (config.type() == Json::Type::String) {
auto assets = Root::singleton().assets();
config = assets->json(config.toString());
}
m_interpolationEnabled = config.getBool("interpolationEnabled", false);
m_entityUpdateDelta = config.getDouble("entityUpdateDelta", 3) / VanillaStepsPerSecond;
m_timeLead = config.getDouble("stepLead", 0) / VanillaStepsPerSecond;
2023-06-20 04:33:09 +00:00
m_extrapolationHint = config.getUInt("extrapolationHint", 0);
m_timeTrackFactor = config.getDouble("stepTrackFactor", 1.0);
m_timeMaxDistance = config.getDouble("stepMaxDistance", 0.0) / VanillaStepsPerSecond;
2023-06-20 04:33:09 +00:00
m_currentTime = 0.0;
2023-06-20 04:33:09 +00:00
}
bool InterpolationTracker::interpolationEnabled() const {
return m_interpolationEnabled;
}
unsigned InterpolationTracker::extrapolationHint() const {
if (m_interpolationEnabled)
return m_extrapolationHint;
else
return 0;
}
float InterpolationTracker::entityUpdateDelta() const {
2023-06-20 04:33:09 +00:00
return m_entityUpdateDelta;
}
void InterpolationTracker::receiveTimeUpdate(double remoteStep) {
m_lastTimeUpdate = remoteStep;
2023-06-20 04:33:09 +00:00
}
void InterpolationTracker::update(double newLocalTime) {
double dt = newLocalTime - m_currentTime;
m_currentTime = newLocalTime;
if (!m_predictedTime || !m_lastTimeUpdate || dt < 0.0f) {
m_predictedTime = m_lastTimeUpdate;
2023-06-20 04:33:09 +00:00
} else {
*m_lastTimeUpdate += dt;
*m_predictedTime += dt;
*m_predictedTime += (*m_lastTimeUpdate - *m_predictedTime) * m_timeTrackFactor;
m_predictedTime = clamp<double>(*m_predictedTime, *m_lastTimeUpdate - m_timeMaxDistance, *m_lastTimeUpdate + m_timeMaxDistance);
2023-06-20 04:33:09 +00:00
}
}
float InterpolationTracker::interpolationLeadTime() const {
if (!m_interpolationEnabled || !m_predictedTime || !m_lastTimeUpdate)
2023-06-20 04:33:09 +00:00
return 0.0f;
return *m_lastTimeUpdate - *m_predictedTime + m_timeLead;
2023-06-20 04:33:09 +00:00
}
}