// M10.7 tick clock and remote-marker interpolation (ADR-0095): monotonic drift-corrected // clock with pauses and snaps; interpolation, bounded extrapolation and staleness; removal // and appearance at their snapshot ticks; reordering; long gaps; adaptive delay; fractional // own-marker prediction. #include "evo/client/interpolation.hpp" #include "evo/client/motion_prediction.hpp" #include "network_test.hpp" #include #include #include namespace evo::network_test { namespace { using evo::client::MarkerInterpolator; using evo::client::RemoteMarker; using evo::client::TickClock; using Clock = TickClock::Clock; using std::chrono::microseconds; using std::chrono::milliseconds; constexpr network::motion::Area area{-50'000'000, -50'000'000, 50'000'000, 50'000'000, 0}; bool near(double a, double b, double tolerance = 1e-6) { return std::abs(a - b) <= tolerance; } void clock_basics() { TickClock clock; const auto t0 = Clock::now(); CHECK(!clock.at(t0)); clock.observe(10, t0); CHECK(near(must(clock.at(t0)), 10)); CHECK(near(must(clock.at(t0 + milliseconds{25})), 10.5)); // Never more than one tick beyond the newest observation: a silent server freezes it. CHECK(near(must(clock.at(t0 + milliseconds{400})), 11)); // An older observation (late datagram) never moves it backwards. clock.observe(9, t0 + milliseconds{410}); CHECK(must(clock.at(t0 + milliseconds{410})) >= 11); // A resume far ahead (pause, reconnect) snaps forward. clock.observe(200, t0 + milliseconds{5000}); CHECK(clock.snaps() == 1 && near(must(clock.at(t0 + milliseconds{5000})), 200)); clock.reset(); CHECK(!clock.at(t0)); clock.observe(3, t0); CHECK(near(must(clock.at(t0)), 3)); // After a reset (new epoch) it may start lower. } // The server simulates every `period_us`; the client observes each tick after a jittered // delay and assumes 50 ms ticks. The estimate stays within `bound` ticks of the newest // observation, never decreases and never snaps. void clock_drift(std::int64_t period_us, double bound) { TickClock clock; const auto t0 = Clock::now(); double previous = 0, worst = 0; std::uint64_t seed = 0x1234'5678'9ABC'DEF1ULL; for (std::uint64_t tick = 1; tick <= 4000; ++tick) { seed = (seed * 6364136223846793005ULL) + 1442695040888963407ULL; const auto jitter = static_cast((seed >> 33) % 15'000); // 0-15 ms. const auto arrival = t0 + microseconds{(static_cast(tick) * period_us) + 20'000 + jitter}; clock.observe(tick, arrival); for (const auto probe : {microseconds{0}, microseconds{period_us / 2}}) { const auto v = must(clock.at(arrival + probe)); CHECK(v >= previous); previous = v; worst = std::max(worst, std::abs(v - static_cast(tick))); } } CHECK(worst < bound && clock.snaps() == 0); } RemoteMarker marker(std::uint64_t id, std::int64_t x, std::int16_t vx = 1000) { return {id, {x, 0, {vx, 0}}}; } void interpolation() { MarkerInterpolator m; CHECK(m.at(5).empty()); // Marker 1 moves +100 mm per tick; marker 2 is removed at tick 4; marker 3 appears at 4. for (std::uint64_t t = 1; t <= 3; ++t) { const std::vector s{marker(1, static_cast(t) * 100'000), marker(2, 7'000'000, 0)}; m.push(t, s); } const std::vector four{marker(1, 400'000), marker(3, -1'000'000, 0)}; m.push(4, four); CHECK(near(m.target_delay(), 2)); // Every snapshot arrived: the minimum. auto at = m.at(1.5); CHECK(at.size() == 2 && at[0].id == 1 && near(at[0].x, 150'000) && at[1].id == 2 && near(at[1].x, 7'000'000) && !at[0].extrapolated && !at[0].stale); // Removal: shown where it was until the removing snapshot's tick, not moved toward it. at = m.at(3.5); CHECK(at.size() == 2 && near(at[0].x, 350'000) && at[1].id == 2 && near(at[1].x, 7'000'000)); // Appearance: marker 3 exists only from tick 4. CHECK(std::ranges::none_of(m.at(3.9), [](const auto& p) { return p.id == 3; })); at = m.at(4); CHECK(at.size() == 2 && at[0].id == 1 && at[1].id == 3 && !at[0].extrapolated); // Beyond the newest: the held vector continues for at most two ticks, then it is stale. at = m.at(5); CHECK(near(at[0].x, 500'000) && at[0].extrapolated && !at[0].stale && near(at[1].x, -1'000'000)); // A held zero vector does not move. at = m.at(9); CHECK(near(at[0].x, 600'000) && at[0].stale && m.stats().stale_frames == 1); // Reordered (older or equal) snapshots are ignored. m.push(3, four); m.push(4, four); CHECK(m.stats().ignored == 2 && m.stats().snapshots == 4); // A long gap is not interpolated across: the older is held until the newer's tick. const std::vector later{marker(1, 9'000'000)}; m.push(20, later); at = m.at(12); CHECK(at.size() == 2 && near(at[0].x, 400'000) && m.stats().gap_snaps == 1); CHECK(near(m.at(20)[0].x, 9'000'000)); m.reset(); CHECK(m.at(20).empty() && near(m.delay(), 2)); } void adaptive_delay() { MarkerInterpolator m; std::uint64_t t = 0; const std::vector s{marker(1, 0)}; // Every other snapshot lost: gaps of 2 ticks, so the target is 3. for (int i = 0; i < 20; ++i) m.push(t += 2, s); CHECK(near(m.target_delay(), 3) && near(m.delay(), 2)); m.advance(250); // Raised at 2 ticks per second. CHECK(near(m.delay(), 2.5)); m.advance(1000); CHECK(near(m.delay(), 3)); // Long gaps raise it to the maximum, never beyond. for (int i = 0; i < 20; ++i) m.push(t += 9, s); m.advance(10'000); CHECK(near(m.target_delay(), 6) && near(m.delay(), 6)); // A steady stream lowers it slowly (0.5 ticks per second). for (int i = 0; i < 40; ++i) m.push(t += 1, s); CHECK(near(m.target_delay(), 2)); m.advance(1000); CHECK(near(m.delay(), 5.5)); } void fractional_prediction() { client::MotionPredictor p; CHECK(!p.predict_between(3, 0.5)); CHECK(p.input({1, 3, {1000, 0}})); p.authoritative({2, 0, {0, 0, {}}, area}); const auto mid = must(p.predict_between(3, 0.5)); // Tick 3 at 100 mm, tick 4 at 200 mm. CHECK(near(mid.x, 150'000) && near(mid.y, 0)); CHECK(near(must(p.predict_between(3, 2.0)).x, 200'000)); // Fraction clamped to 1. } } // namespace void interpolation_tests() { clock_basics(); clock_drift(50'000, 1.5); clock_drift(52'000, 1.5); // Server 4 % slower than assumed. clock_drift(48'000, 1.5); // Server 4 % faster. interpolation(); adaptive_delay(); fractional_prediction(); } } // namespace evo::network_test