diff --git a/cmake/Network.cmake b/cmake/Network.cmake index 61d0093..3e698c1 100644 --- a/cmake/Network.cmake +++ b/cmake/Network.cmake @@ -37,7 +37,8 @@ add_library(evo_network_protocol STATIC src/network/protocol/codec.cpp target_link_libraries(evo_network_protocol PUBLIC evo_client_views evo_client_controls) evo_owned_target(evo_network_protocol) # M10.6 client prediction and reconciliation of the probe marker (ADR-0094). -add_library(evo_client_prediction STATIC src/client/prediction/motion_prediction.cpp) +add_library(evo_client_prediction STATIC src/client/prediction/motion_prediction.cpp + src/client/prediction/interpolation.cpp) target_link_libraries(evo_client_prediction PUBLIC evo_network_protocol) evo_owned_target(evo_client_prediction) # Mbed TLS adapters: dev grants (HMAC-SHA256 via PSA) and the TLS 1.3 channel. @@ -65,7 +66,7 @@ evo_owned_target(evo-dev-session) if(BUILD_TESTING) add_executable(evo-network-tests tests/network_protocol.cpp tests/network_tls.cpp tests/network_session.cpp tests/network_control.cpp tests/network_replication.cpp - tests/network_motion.cpp tests/network_main.cpp) + tests/network_motion.cpp tests/network_interpolation.cpp tests/network_main.cpp) # The TLS tests generate throwaway dev certificates with Mbed TLS directly. target_link_libraries(evo-network-tests PRIVATE evo_network_session evo_identity evo_client_prediction mbedtls) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index df35bef..ef115ef 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -531,7 +531,9 @@ probe) with process evidence done; 10.5c engineering client switched to M10 sess 10.6 split (ADR-0094, EXPERIMENT probe marker, not an organism movement rule): 10.6a shared probe-motion-v1 rule, server markers, session protocol v3 marker records and the client predictor done; 10.6b wiring (sessions, evo-server, probe, GUI marker) done; 10.6c impaired -traces done (networking/M10_PREDICTION_TRACES.md). 10.7-10.9 planned. +traces done (networking/M10_PREDICTION_TRACES.md). 10.7 split (ADR-0095): 10.7a tick clock, +remote-marker interpolation and fractional prediction done; 10.7b client wiring, loss view +and two-client trace follow. 10.8-10.9 planned. **Entry:** M9 client/session evidence and M7/M8 dev/stage durability. Read NETWORK_ARCHITECTURE, PERSISTENCE_ARCHITECTURE and ADR-0017. The M9 transport is diff --git a/docs/STATUS.md b/docs/STATUS.md index a4d309d..6710ff1 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -59,8 +59,10 @@ M10.6b wires it: evo-server gives every session a marker in the habitat area, th prints it, and the engineering client predicts and draws it (WASD), following it with camera and interest. M10.6c traces it through the impairment relay: exact without loss, 94-99 % exact at 60-200 ms RTT with 2-5 % loss, convergence after stopping and a restart -resync of about 2-3 s (networking/M10_PREDICTION_TRACES.md). M10.6 is complete. Next: M10.7 -interpolation and session loss. +resync of about 2-3 s (networking/M10_PREDICTION_TRACES.md). M10.6 is complete. M10.7a (ADR-0095) adds a drift-corrected tick clock, adaptive +interpolation of remote markers (2-6 ticks behind, at most 2 ticks extrapolated, then +stale; organisms never) and sub-tick own-marker presentation. Next: 10.7b client wiring, +the stale loss view and a two-client trace. ## Previous gate - 2026-10-02 diff --git a/docs/adr/0095-m10-interpolation-and-session-loss.md b/docs/adr/0095-m10-interpolation-and-session-loss.md new file mode 100644 index 0000000..c69ddf7 --- /dev/null +++ b/docs/adr/0095-m10-interpolation-and-session-loss.md @@ -0,0 +1,57 @@ +# ADR-0095: M10 tick clock, remote interpolation and session loss + +## Status +Accepted for M10.7 (2026-10-06). Decisions confirmed with the maintainer on 2026-10-06: +- an adaptive interpolation delay of 2-6 ticks; +- remote markers extrapolate at most 2 ticks, and organisms never; +- on session loss the last view stays frozen, dimmed and labelled stale, and a reconnect + starts from a fresh baseline; +- a drift-corrected client tick clock, with sub-tick presentation. + +Delivery: 10.7a the headless clock, interpolator and fractional prediction (this text); +10.7b engineering client wiring, the loss view and a two-client trace. Each segment amends +this ADR. Presentation only: none of it is authority, and no outcome is invented. + +## Context +NETWORK_ARCHITECTURE requires remote entities to interpolate buffered snapshots with +bounded extrapolation, loss to request a fresh baseline, and death, mutation and +reproduction to remain server decisions. M10.6 (ADR-0094) predicts the own probe marker at +whole ticks. Other markers are drawn as last replicated, so they step at 20 Hz and jump +when a snapshot is lost. No controllable organism moves yet. + +## Decision (10.7a) +- **Tick clock** (`include/evo/client/interpolation.hpp`). Each observed server tick + (watermark or snapshot) slews a local estimate by 10 % of the error. An error beyond 4 + ticks snaps forward. The estimate is monotonic between resets and never more than one + tick beyond the newest observation, so a silent or paused server freezes it rather than + inventing time. Assumes 50 ms ticks; drift of ±4 % stays within 1.5 ticks in tests. +- **Remote markers** are presented at `clock - delay`, interpolated linearly between the + buffered snapshots around that tick (at most 16 snapshots). + - A marker appears at the tick of the snapshot that adds it. + - It is shown unmoved until the tick of the snapshot that removes it: never moved toward + a removal. + - Snapshots more than 8 ticks apart are not interpolated across. + - Reordered or duplicate snapshots are ignored. + - Beyond the newest snapshot the replicated held vector continues for at most 2 ticks; + then the marker freezes there and is marked stale. +- **Adaptive delay.** The target is the 90th-percentile gap between received snapshots + (last 40) plus one tick, within 2-6. The delay rises toward it at 2 ticks/s and falls at + 0.5 ticks/s. +- **Organisms** are never extrapolated or interpolated: they are shown as replicated (newest + snapshot, ADR-0093). A dead or removed organism is therefore never shown moving or as + present after its removal. +- **Own marker between ticks.** `MotionPredictor::predict_between(tick, fraction)` + interpolates the prediction between two whole ticks for presentation. Error measurement + still records whole ticks. + +## Alternatives considered +- A fixed 2- or 3-tick delay: simpler, but either stutters on lossy links or adds latency + on good ones. +- No extrapolation: visible stalls on every lost snapshot. +- Longer extrapolation (4 ticks): larger errors when a remote changes direction. +- Clock from the local timer only: drifts without bound against the server's pacing. + +## Consequences +- Remote markers are shown 100-300 ms in the past, the own marker in the predicted + present. That is the usual split; interactions that need both are later work. +- Extrapolated or stale markers are flagged so the client can draw them differently. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 1b00827..4071ee4 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4603,3 +4603,35 @@ The Postgres suite was not rerun: no server code changed since 10.6b. Not done: - traces over the dev route (a v3 server must be deployed first); - many-client load (10.8). + +## 2026-10-06 — M10.7a tick clock and remote interpolation (ADR-0095) + +Implemented: +- `TickClock` (slew 10 %, forward snap beyond 4 ticks, monotonic, at most 1 tick beyond the + newest observation); +- `MarkerInterpolator`: + - interpolation, then at most 2 ticks of extrapolation, then stale; + - appearance and removal at their snapshot ticks; + - reordering ignored; no interpolation across gaps over 8 ticks; + - adaptive delay of 2-6 ticks from the 90th-percentile gap; +- `MotionPredictor::predict_between`. + +Tests (`tests/network_interpolation.cpp`): +- clock basics: freeze while silent, no backward step, forward snap, reset; +- drift with jitter at 0 % and ±4 % (4,000 ticks): within 1.5 ticks, monotonic, no snaps; +- interpolation, removal, appearance, extrapolation and staleness, reordering, long gaps, + reset; +- adaptive delay up and down; +- fractional prediction. + +Executed: +- **Linux GCC 14:** + - preset `quic`: 4/4; + - `client-network`: 15/15. +- **Linux Clang 19:** ASan/UBSan `evo-network-tests`, and clang-tidy 19 on the new sources + and tests (after fixing one optional-access finding). +- **Windows MSVC 19.44:** + - preset `quic`: 4/4 (rebuilt after the fix); + - `client-network`: 13/13. + +Not done: client wiring, the loss view and the two-client trace (10.7b). diff --git a/include/evo/client/interpolation.hpp b/include/evo/client/interpolation.hpp new file mode 100644 index 0000000..7be4230 --- /dev/null +++ b/include/evo/client/interpolation.hpp @@ -0,0 +1,110 @@ +#pragma once +#include "evo/network/probe_motion.hpp" +#include +#include +#include +#include +#include +#include +#include + +// M10.7 client tick clock and remote-entity interpolation (ADR-0095). Headless, +// single-owner, presentation only: nothing here is authority or decides an outcome. +namespace evo::client { + +// A drift-corrected, monotonic estimate of the server's simulated tick between snapshots. +// Each observation (a tick the client just learned the server had simulated) slews the +// estimate toward it by a fraction; a large disagreement snaps forward, never backward. The +// estimate never runs more than `max_ahead` ticks beyond the newest observation, so a +// paused or silent server freezes it instead of inventing time. +struct TickClockLimits { + double tick_ms{50}; + double gain{0.1}; // Fraction of the error corrected per observation. + double snap_ticks{4}; // Errors beyond this snap (forward) instead of slewing. + double max_ahead{1.0}; // Ticks beyond the newest observation. +}; +class TickClock { + public: + using Clock = std::chrono::steady_clock; + explicit TickClock(TickClockLimits limits = {}) : limits_{limits} {} + void reset() noexcept; + void observe(std::uint64_t tick, Clock::time_point now); + // The estimated server tick (fractional) at `now`; nothing before the first observation. + // Never decreases between resets. + [[nodiscard]] std::optional at(Clock::time_point now); + [[nodiscard]] std::uint64_t snaps() const noexcept { + return snaps_; + } + + private: + [[nodiscard]] double raw(double base, Clock::time_point now) const; + TickClockLimits limits_; + std::optional base_tick_; + Clock::time_point base_time_{}; + std::uint64_t newest_{}; + double returned_{}; + std::uint64_t snaps_{}; +}; + +// Remote probe markers (other sessions'), shown `delay` ticks behind the clock and +// interpolated between the buffered snapshots around that render tick. Beyond the newest +// snapshot a marker continues its replicated held vector for at most `max_extrapolation` +// ticks, then freezes and is marked stale. A marker appears at the tick of the snapshot that +// added it and disappears at the tick of the one that removed it (never interpolated toward a +// removal). Snapshots further apart than `max_gap` are not interpolated across (the newer +// one is shown from its tick). The delay adapts to the observed gaps between received +// snapshots (lost or late ones), within [min_delay, max_delay]. +struct InterpolationLimits { + double min_delay{2}, max_delay{6}; // Ticks (ADR-0095). + double max_extrapolation{2}; // Ticks beyond the newest snapshot. + std::uint64_t max_gap{8}; // Ticks between snapshots still interpolated. + std::size_t max_snapshots{16}; // Buffered snapshots. + std::size_t gap_window{40}; // Recent arrivals the delay adapts to. + double raise_per_second{2}, lower_per_second{0.5}; // Delay adaptation speed (ticks/s). +}; +struct RemoteMarker { + std::uint64_t id{}; + network::motion::State state; +}; +struct PresentedMarker { + std::uint64_t id{}; + double x{}, y{}; // Micrometres. + bool extrapolated{}, stale{}; +}; +struct InterpolationStats { + std::uint64_t snapshots{}, ignored{}, extrapolated_frames{}, stale_frames{}, gap_snaps{}; +}; +class MarkerInterpolator { + public: + explicit MarkerInterpolator(InterpolationLimits limits = {}) + : limits_{limits}, delay_{limits.min_delay}, target_{limits.min_delay} {} + void reset(); + // A complete snapshot's remote markers (all of them: absence means removal). Snapshots + // not newer than the newest buffered one are ignored (reordering). + void push(std::uint64_t tick, std::span markers); + // Moves the delay toward its target (call once per frame). + void advance(double elapsed_ms); + // Markers at render tick `clock - delay()`. + [[nodiscard]] std::vector at(double render_tick); + [[nodiscard]] double delay() const noexcept { + return delay_; + } + [[nodiscard]] double target_delay() const noexcept { + return target_; + } + [[nodiscard]] const InterpolationStats& stats() const noexcept { + return stats_; + } + + private: + struct Snapshot { + std::uint64_t tick{}; + std::vector markers; // Sorted by id. + }; + InterpolationLimits limits_; + std::deque snapshots_; + std::deque gaps_; + double delay_, target_; + InterpolationStats stats_; +}; +} // namespace evo::client diff --git a/include/evo/client/motion_prediction.hpp b/include/evo/client/motion_prediction.hpp index fd9758c..4ae0573 100644 --- a/include/evo/client/motion_prediction.hpp +++ b/include/evo/client/motion_prediction.hpp @@ -54,6 +54,12 @@ class MotionPredictor { // The predicted state after `tick` (the presented tick: server tick plus input lead); // nothing before the first authoritative state. [[nodiscard]] std::optional predict(std::uint64_t tick); + // M10.7 (ADR-0095): the position `fraction` (0..1) of the way from tick to tick + 1, for + // presentation between ticks; records the prediction for `tick` like predict(). + struct Point { + double x{}, y{}; // Micrometres. + }; + [[nodiscard]] std::optional predict_between(std::uint64_t tick, double fraction); // The jump rebasing made to the newest prediction (old minus new), since the last call. struct Offset { std::int64_t x{}, y{}; diff --git a/src/client/prediction/interpolation.cpp b/src/client/prediction/interpolation.cpp new file mode 100644 index 0000000..7fb5b16 --- /dev/null +++ b/src/client/prediction/interpolation.cpp @@ -0,0 +1,147 @@ +#include "evo/client/interpolation.hpp" +#include +#include + +namespace evo::client { +namespace { +double ms(TickClock::Clock::duration d) { + return std::chrono::duration(d).count(); +} +// Held-vector motion in micrometres for a fractional number of ticks (presentation only). +double motion_um(std::int16_t component, double ticks) { + return static_cast(component) * + static_cast(network::motion::speed_um_per_tick) / 1000.0 * ticks; +} +} // namespace + +void TickClock::reset() noexcept { + base_tick_.reset(); + newest_ = 0; + returned_ = 0; +} + +double TickClock::raw(double base, Clock::time_point now) const { + return base + (ms(now - base_time_) / limits_.tick_ms); +} + +void TickClock::observe(std::uint64_t tick, Clock::time_point now) { + newest_ = std::max(newest_, tick); + const auto sample = static_cast(tick); + if (!base_tick_) { + base_tick_ = sample; + base_time_ = now; + return; + } + const auto estimate = raw(*base_tick_, now); + const auto error = sample - estimate; + if (error > limits_.snap_ticks) { + base_tick_ = sample; // Far behind (a pause or a reconnect): catch up at once. + ++snaps_; + } else { + base_tick_ = estimate + (error * limits_.gain); // Slew; at() keeps it monotonic. + } + base_time_ = now; +} + +std::optional TickClock::at(Clock::time_point now) { + if (!base_tick_) + return std::nullopt; + const auto bounded = + std::min(raw(*base_tick_, now), static_cast(newest_) + limits_.max_ahead); + returned_ = std::max(returned_, bounded); + return returned_; +} + +void MarkerInterpolator::reset() { + snapshots_.clear(); + gaps_.clear(); + delay_ = target_ = limits_.min_delay; +} + +void MarkerInterpolator::push(std::uint64_t tick, std::span markers) { + if (!snapshots_.empty() && tick <= snapshots_.back().tick) { + ++stats_.ignored; + return; + } + ++stats_.snapshots; + if (!snapshots_.empty()) { + gaps_.push_back(tick - snapshots_.back().tick); + while (gaps_.size() > limits_.gap_window) + gaps_.pop_front(); + // Cover the 90th-percentile gap between received snapshots, plus the clock's lead. + std::vector sorted(gaps_.begin(), gaps_.end()); + std::ranges::sort(sorted); + const auto p90 = sorted.at((sorted.size() - 1) * 9 / 10); + target_ = std::clamp(static_cast(p90) + 1, limits_.min_delay, limits_.max_delay); + } + Snapshot s{tick, {markers.begin(), markers.end()}}; + std::ranges::sort(s.markers, {}, &RemoteMarker::id); + snapshots_.push_back(std::move(s)); + while (snapshots_.size() > limits_.max_snapshots) + snapshots_.pop_front(); +} + +void MarkerInterpolator::advance(double elapsed_ms) { + if (elapsed_ms <= 0) + return; + const auto seconds = elapsed_ms / 1000; + if (delay_ < target_) + delay_ = std::min(target_, delay_ + (limits_.raise_per_second * seconds)); + else + delay_ = std::max(target_, delay_ - (limits_.lower_per_second * seconds)); +} + +std::vector MarkerInterpolator::at(double render_tick) { + std::vector out; + if (snapshots_.empty()) + return out; + const auto& newest = snapshots_.back(); + const auto show = [&](const Snapshot& s) { + for (const auto& m : s.markers) + out.push_back({m.id, static_cast(m.state.x), static_cast(m.state.y)}); + }; + if (render_tick >= static_cast(newest.tick)) { + // Beyond the newest snapshot: continue the held vector briefly, then freeze (stale). + const auto beyond = render_tick - static_cast(newest.tick); + const auto ticks = std::min(beyond, limits_.max_extrapolation); + const bool stale = beyond > limits_.max_extrapolation; + for (const auto& m : newest.markers) + out.push_back({m.id, + static_cast(m.state.x) + motion_um(m.state.vector.x, ticks), + static_cast(m.state.y) + motion_um(m.state.vector.y, ticks), + ticks > 0, stale}); + stats_.extrapolated_frames += ticks > 0 ? 1 : 0; + stats_.stale_frames += stale ? 1 : 0; + return out; + } + if (render_tick <= static_cast(snapshots_.front().tick)) { + show(snapshots_.front()); // Not enough history yet: the oldest as it was. + return out; + } + // a.tick <= render_tick < b.tick. + const auto b = std::ranges::find_if( + snapshots_, [&](const Snapshot& s) { return static_cast(s.tick) > render_tick; }); + const auto& a = *std::prev(b); + if (b->tick - a.tick > limits_.max_gap) { + ++stats_.gap_snaps; // Too far apart to interpolate: hold the older until the newer. + show(a); + return out; + } + const auto t = + (render_tick - static_cast(a.tick)) / static_cast(b->tick - a.tick); + for (const auto& m : a.markers) { + const auto next = std::ranges::lower_bound(b->markers, m.id, {}, &RemoteMarker::id); + if (next == b->markers.end() || next->id != m.id) { + // Removed by b: shown where it was until b's tick, never moved toward a removal. + out.push_back({m.id, static_cast(m.state.x), static_cast(m.state.y)}); + continue; + } + const auto lerp = [t](std::int64_t from, std::int64_t to) { + return static_cast(from) + + ((static_cast(to) - static_cast(from)) * t); + }; + out.push_back({m.id, lerp(m.state.x, next->state.x), lerp(m.state.y, next->state.y)}); + } + return out; // Markers new in b appear from b's tick. +} +} // namespace evo::client diff --git a/src/client/prediction/motion_prediction.cpp b/src/client/prediction/motion_prediction.cpp index 9d05c5b..398a5dd 100644 --- a/src/client/prediction/motion_prediction.cpp +++ b/src/client/prediction/motion_prediction.cpp @@ -81,6 +81,19 @@ std::optional MotionPredictor::predict(std::uint64_t tic return state; } +std::optional MotionPredictor::predict_between(std::uint64_t tick, + double fraction) { + const auto from = predict(tick); + if (!from) + return std::nullopt; + const auto to = replay(tick + 1); + if (!to) + return std::nullopt; + const auto f = std::clamp(fraction, 0.0, 1.0); + return Point{static_cast(from->x) + (static_cast(to->x - from->x) * f), + static_cast(from->y) + (static_cast(to->y - from->y) * f)}; +} + std::optional MotionPredictor::replay(std::uint64_t tick) { if (!authority_) return std::nullopt; diff --git a/tests/network_interpolation.cpp b/tests/network_interpolation.cpp new file mode 100644 index 0000000..45f4428 --- /dev/null +++ b/tests/network_interpolation.cpp @@ -0,0 +1,162 @@ +// 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 diff --git a/tests/network_main.cpp b/tests/network_main.cpp index b35953d..aae5983 100644 --- a/tests/network_main.cpp +++ b/tests/network_main.cpp @@ -9,6 +9,7 @@ int main() try { evo::network_test::control_tests(); evo::network_test::replication_tests(); evo::network_test::motion_tests(); + evo::network_test::interpolation_tests(); std::puts("network tests passed"); return 0; } catch (const std::exception& e) { diff --git a/tests/network_test.hpp b/tests/network_test.hpp index dd639a2..51c4976 100644 --- a/tests/network_test.hpp +++ b/tests/network_test.hpp @@ -29,5 +29,6 @@ void session_tests(); void control_tests(); void replication_tests(); void motion_tests(); +void interpolation_tests(); } // namespace evo::network_test #define CHECK(...) ::evo::network_test::check(static_cast(__VA_ARGS__))