Files
aevolution/include/evo/client/interpolation.hpp
T
dingoandClaude Opus 5.5 90cdb61e30
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (client) (push) Successful in 12m44s
Foundation / checks (network) (push) Successful in 12m45s
Foundation / checks (tsan) (push) Successful in 8m5s
Foundation / checks (checks) (push) Successful in 32m4s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 21m38s
Add M10.7a tick clock and remote-marker interpolation
ADR-0095 (presentation only): a drift-corrected monotonic tick clock that
never runs more than one tick past the newest observation; remote probe
markers shown 2-6 ticks behind (adaptive to snapshot gaps), interpolated,
extrapolated for at most 2 ticks and then marked stale; appearance and
removal at their snapshot ticks; organisms never extrapolated; sub-tick
own-marker prediction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 17:16:20 -04:00

111 lines
4.5 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_;
}
private:
[[nodiscard]] double raw(double base, Clock::time_point now) const;
TickClockLimits limits_;
std::optional<double> 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<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