1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
|