Files
aevolution/src/client/prediction/motion_prediction.cpp
T
dingoandClaude Opus 5.5 5728e7682e
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
Wire M10.7b interpolation and the stale loss view into the client
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>
2026-10-06 17:50:41 -04:00

179 lines
6.1 KiB
C++

#include "evo/client/motion_prediction.hpp"
#include <algorithm>
#include <cmath>
#include <utility>
namespace evo::client {
namespace {
std::uint64_t distance_um(const network::motion::State& a, const network::motion::State& b) {
const auto dx = static_cast<double>(a.x - b.x), dy = static_cast<double>(a.y - b.y);
return static_cast<std::uint64_t>(std::llround(std::sqrt((dx * dx) + (dy * dy))));
}
} // namespace
void MotionPredictor::reset() {
authority_.reset();
pending_.clear();
history_.clear();
presented_tick_.reset();
correction_ = {};
}
bool MotionPredictor::input(const MotionInput& in) {
if (full() || !network::motion::valid(in.vector) ||
(authority_ && in.tick <= authority_->tick) ||
(!pending_.empty() && in.sequence <= pending_.back().sequence))
return false;
// A new input changes the future, not what is shown now: it is not a correction.
const auto at = std::ranges::upper_bound(pending_, in.tick, {}, &MotionInput::tick);
pending_.insert(at, in);
stats_.max_pending = std::max<std::uint64_t>(stats_.max_pending, pending_.size());
return true;
}
void MotionPredictor::rejected(std::uint64_t sequence) {
const auto it = std::ranges::find(pending_, sequence, &MotionInput::sequence);
if (it == pending_.end())
return;
const auto before = presented_tick_ ? replay(*presented_tick_) : std::nullopt;
pending_.erase(it);
++stats_.rejected_inputs;
rebase(before);
}
void MotionPredictor::authoritative(const AuthoritativeMarker& a) {
if (authority_ && a.tick <= authority_->tick)
return;
++stats_.authoritative;
if (const auto predicted = history_.find(a.tick); predicted != history_.end()) {
const auto error = distance_um(predicted->second, a.state);
stats_.max_error_um = std::max(stats_.max_error_um, error);
if (errors_.size() < limits_.max_errors)
errors_.push_back(error);
}
std::erase_if(history_, [&](const auto& e) { return e.first <= a.tick; });
const auto before = presented_tick_ ? replay(*presented_tick_) : std::nullopt;
// Applied inputs are in the state; unapplied ones whose tick has run never will be.
stats_.dropped_inputs +=
static_cast<std::uint64_t>(std::ranges::count_if(pending_, [&](const MotionInput& in) {
return in.sequence > a.applied_sequence && in.tick <= a.tick;
}));
std::erase_if(pending_, [&](const MotionInput& in) {
return in.sequence <= a.applied_sequence || in.tick <= a.tick;
});
authority_ = a;
rebase(before);
}
std::optional<network::motion::State> MotionPredictor::predict(std::uint64_t tick) {
return predict(tick, true);
}
std::optional<network::motion::State> MotionPredictor::predict(std::uint64_t tick, bool record) {
if (!authority_)
return std::nullopt;
const auto authority_tick = authority_->tick;
auto state = replay(tick);
if (!state)
return std::nullopt;
presented_tick_ = tick;
if (record && tick > authority_tick) {
history_[tick] = *state;
while (history_.size() > limits_.max_history)
history_.erase(history_.begin());
}
return state;
}
std::optional<MotionPredictor::Point>
MotionPredictor::predict_between(std::uint64_t tick, double fraction, bool record) {
const auto from = predict(tick, record);
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<double>(from->x) + (static_cast<double>(to->x - from->x) * f),
static_cast<double>(from->y) + (static_cast<double>(to->y - from->y) * f)};
}
std::optional<network::motion::State> MotionPredictor::replay(std::uint64_t tick) {
if (!authority_)
return std::nullopt;
auto state = authority_->state;
if (tick <= authority_->tick)
return state;
const auto last =
authority_->tick + std::min(tick - authority_->tick, limits_.max_replay_ticks);
auto next = pending_.begin();
for (auto t = authority_->tick + 1; t <= last; ++t) {
for (; next != pending_.end() && next->tick <= t; ++next)
state.vector = next->vector;
state = network::motion::step(state, authority_->area);
++stats_.replayed_ticks;
}
return state;
}
void MotionPredictor::rebase(const std::optional<network::motion::State>& before) {
if (!before || !presented_tick_)
return;
const auto after = replay(*presented_tick_);
if (!after)
return;
const auto dx = before->x - after->x, dy = before->y - after->y;
if (dx != 0 || dy != 0) {
++stats_.corrections;
correction_.x += dx;
correction_.y += dy;
}
}
MotionPredictor::Offset MotionPredictor::take_correction() noexcept {
return std::exchange(correction_, Offset{});
}
std::vector<std::uint64_t> MotionPredictor::take_errors() {
return std::exchange(errors_, {});
}
void CorrectionSmoother::add(MotionPredictor::Offset c) {
x_ += static_cast<double>(c.x);
y_ += static_cast<double>(c.y);
if (std::hypot(x_, y_) > static_cast<double>(snap_um_)) {
x_ = y_ = 0; // Too far to glide: show the corrected position at once.
++snaps_;
}
}
MotionPredictor::Offset CorrectionSmoother::advance(double elapsed_ms) {
if (elapsed_ms > 0 && half_life_ms_ > 0) {
const double keep = std::exp2(-elapsed_ms / half_life_ms_);
x_ *= keep;
y_ *= keep;
}
if (std::abs(x_) < 1 && std::abs(y_) < 1)
x_ = y_ = 0;
return {static_cast<std::int64_t>(std::llround(x_)),
static_cast<std::int64_t>(std::llround(y_))};
}
void InputLead::too_late() noexcept {
lead_ = std::min(lead_ + 1, max_);
spare_ = 0;
}
void InputLead::admitted(std::uint64_t tick, std::uint64_t server_tick) noexcept {
// More than two ticks to spare twenty times in a row: one tick less lead.
if (tick > server_tick + 2) {
if (++spare_ >= 20 && lead_ > min_) {
--lead_;
spare_ = 0;
}
} else {
spare_ = 0;
}
}
} // namespace evo::client