#include "evo/client/motion_prediction.hpp" #include #include #include namespace evo::client { namespace { std::uint64_t distance_um(const network::motion::State& a, const network::motion::State& b) { const auto dx = static_cast(a.x - b.x), dy = static_cast(a.y - b.y); return static_cast(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(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::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 MotionPredictor::predict(std::uint64_t tick) { return predict(tick, true); } std::optional 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::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(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; 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& 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 MotionPredictor::take_errors() { return std::exchange(errors_, {}); } void CorrectionSmoother::add(MotionPredictor::Offset c) { x_ += static_cast(c.x); y_ += static_cast(c.y); if (std::hypot(x_, y_) > static_cast(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::llround(x_)), static_cast(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