Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m27s
Foundation / checks (network) (push) Successful in 12m46s
Foundation / checks (tsan) (push) Successful in 7m59s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 25m13s
Foundation / checks (postgres) (push) Canceled after 12m48s
ADR-0095. The engineering client times presentation with the tick clock: its own marker between ticks, other markers interpolated a few ticks behind (grey when stale). Silence beyond 3 ticks and long-gap holds are stale. A lost session leaves its last view frozen and labelled stale until the reconnect's fresh baseline; nothing is predicted or sent meanwhile. evo-motion-trace --remote measures remote-marker error between two clients through the relay, with a 1.5 s outage (motion-trace-remote-smoke CTest). Prediction errors are recorded only for ticks inputs are scheduled for. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.8 KiB
C++
114 lines
4.8 KiB
C++
#pragma once
|
|
#include "evo/network/probe_motion.hpp"
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
// 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<double> 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<double> silence(Clock::time_point now) const;
|
|
|
|
private:
|
|
[[nodiscard]] double raw(double base, Clock::time_point now) const;
|
|
TickClockLimits limits_;
|
|
std::optional<double> 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;
|
|
};
|
|
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<const RemoteMarker> 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<PresentedMarker> 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<RemoteMarker> markers; // Sorted by id.
|
|
};
|
|
InterpolationLimits limits_;
|
|
std::deque<Snapshot> snapshots_;
|
|
std::deque<std::uint64_t> gaps_;
|
|
double delay_, target_;
|
|
InterpolationStats stats_;
|
|
};
|
|
} // namespace evo::client
|