#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_; } // Ticks of local time since the last observation (nothing before the first): a silent // server freezes the clock, so silence itself is what marks views stale. [[nodiscard]] std::optional silence(Clock::time_point now) const; private: [[nodiscard]] double raw(double base, Clock::time_point now) const; TickClockLimits limits_; std::optional base_tick_; Clock::time_point base_time_{}, seen_{}; 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 older is // held, marked stale, until the newer one's 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; bool ghost{}; // A probe being handed to this region (ADR-0096): read-only. }; struct PresentedMarker { std::uint64_t id{}; double x{}, y{}; // Micrometres. bool extrapolated{}, stale{}, ghost{}; }; 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