From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/client/entity/moveable.cc | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/client/entity/moveable.cc (limited to 'src/client/entity/moveable.cc') diff --git a/src/client/entity/moveable.cc b/src/client/entity/moveable.cc new file mode 100644 index 0000000..480e6db --- /dev/null +++ b/src/client/entity/moveable.cc @@ -0,0 +1,86 @@ +#include "client/entity/moveable.hh" + +namespace client { + +static auto find_sequence_it(auto& updates, + const shared::tick_t& sequence) noexcept { + return std::ranges::find_if(updates, [&sequence](const auto& update) { + return update.tick_sequence == sequence; + }); +} + +void moveable::repredict_from(const shared::tick_t& sequence) noexcept { + // find the animate update we want to repredict starting from + const auto find_it = find_sequence_it(this->updates, sequence); + if (find_it == std::end(this->updates)) { + return; + } + + // backup current animate + const shared::animate before_move = *this; + + // predict the next ticks starting from find_it until we run + // out of prediction history + for (auto it = find_it; it != std::end(this->updates); ++it) { + const auto next_it = std::next(it); + if (next_it == std::end(this->updates)) { + break; + } + + const auto viewangles = next_it->animate.get_angles(); + const auto commands = next_it->animate.get_commands(); + + this->shared::animate::operator=(it->animate); + this->commands = commands; + this->viewangles = viewangles; + const auto predicted = movement::move(*this, state::chunks); + if (!predicted.has_value()) { + break; + } + next_it->animate = *predicted; + next_it->animate.get_mutable_angles() = viewangles; + next_it->animate.get_mutable_commands() = commands; + } + // restore current animate + this->shared::animate::operator=(before_move); +} + +void moveable::notify(const shared::animate& animate, + const shared::tick_t& sequence, + const bool from_server) noexcept { + this->animate::notify(animate, sequence, from_server); + + if (!from_server) { // Only repredict if we're from the server. + return; + } + this->repredict_from(sequence); +} + +void moveable::extrapolate() noexcept { + // can't extrapolate if no data + if (this->updates.empty()) { + return; + } + + const auto& latest_it = std::rbegin(this->updates); + if (latest_it == std::rend(this->updates)) { + return; + } + + // backup current animate, load latest animate + const shared::animate before_extrapolate = *this; + this->shared::animate::operator=(latest_it->animate); + + // FIXED: we were writing our commands with the old commands, so we were + // always predicting the first commands, which were always for not moving! + this->commands = before_extrapolate.get_commands(); + this->viewangles = before_extrapolate.get_angles(); + + const auto predicted = movement::move(*this, state::chunks); + if (predicted.has_value()) { + this->notify(*predicted, this->get_latest_sequence() + 1, false); + } + this->shared::animate::operator=(before_extrapolate); +} + +} // namespace client -- cgit v1.2.3