nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
prediction.cc
Go to the documentation of this file.
1#include "client/prediction.h"
2
3#include <cmath>
4#include <cstddef>
5
6#include "server/walking.h"
7
8namespace nanommo {
9
10namespace {
11
12// A correction further than this, in metres, is a jump, such as through a
13// gate, and isn't faded.
14constexpr float kJump = 2.0f;
15
16// How fast a correction fades: most of it in a tenth of a second.
17constexpr float kFade = 20.0f;
18
19// Five seconds of ticks: a server that hasn't acked any of them for longer,
20// say while a world hands its players over, won't, and they'd pile up.
21constexpr std::size_t kMostPending = 100;
22
23} // namespace
24
25void Prediction::Walk(const Terrain& terrain, uint32_t sequence,
26 float direction_x, float direction_z, bool run,
27 float seconds) {
28 pending_.push_back({sequence, direction_x, direction_z, run});
29 if (pending_.size() > kMostPending) {
30 pending_.pop_front();
31 }
32 if (placed_) {
33 WalkAlong(terrain, x_, z_, direction_x, direction_z, run, seconds);
34 }
35}
36
37void Prediction::Correct(const Terrain& terrain, float x, float z,
38 uint32_t acked, float seconds) {
39 while (!pending_.empty() && pending_.front().sequence <= acked) {
40 pending_.pop_front();
41 }
42 const float was_x = x_;
43 const float was_z = z_;
44 x_ = x;
45 z_ = z;
46 for (const Keys& keys : pending_) {
47 WalkAlong(terrain, x_, z_, keys.x, keys.z, keys.run, seconds);
48 }
49 // What we drew a moment ago stays where it was, and fades into the new.
50 if (placed_ && std::hypot(was_x - x_, was_z - z_) < kJump) {
51 error_x_ += was_x - x_;
52 error_z_ += was_z - z_;
53 } else {
54 error_x_ = 0.0f;
55 error_z_ = 0.0f;
56 }
57 placed_ = true;
58}
59
60protocol::World Predicted(const protocol::World& world,
61 const Prediction& prediction, float direction_x,
62 float direction_z, bool run) {
63 protocol::World shown = world;
64 if (!prediction.placed()) {
65 return shown;
66 }
67 for (protocol::Player& player : *shown.mutable_players()) {
68 if (player.id() != shown.you()) {
69 continue;
70 }
71 player.set_x(prediction.shown_x());
72 player.set_z(prediction.shown_z());
73 if (shown.steered()) {
74 continue;
75 }
76 const protocol::Motion motion = player.motion();
77 if (motion == protocol::MOTION_IDLE || motion == protocol::MOTION_WALK ||
78 motion == protocol::MOTION_RUN) {
79 const bool moving = direction_x != 0.0f || direction_z != 0.0f;
80 player.set_motion(!moving ? protocol::MOTION_IDLE
81 : run ? protocol::MOTION_RUN
82 : protocol::MOTION_WALK);
83 if (moving) {
84 player.set_yaw(std::atan2(direction_x, direction_z));
85 }
86 }
87 }
88 return shown;
89}
90
91void Prediction::Settle(float seconds) {
92 const float left = std::exp(-kFade * seconds);
93 error_x_ *= left;
94 error_z_ *= left;
95}
96
97} // namespace nanommo
float z() const
Definition prediction.h:36
float x() const
Definition prediction.h:35
float shown_z() const
Definition prediction.h:38
float shown_x() const
Definition prediction.h:37
void Walk(const Terrain &terrain, uint32_t sequence, float direction_x, float direction_z, bool run, float seconds)
Definition prediction.cc:25
bool placed() const
Definition prediction.h:39
void Settle(float seconds)
Fades what the last corrections moved, over the frame's time.
Definition prediction.cc:91
void Correct(const Terrain &terrain, float x, float z, uint32_t acked, float seconds)
Definition prediction.cc:37
Definition chat.cc:9
protocol::World Predicted(const protocol::World &world, const Prediction &prediction, float direction_x, float direction_z, bool run)
Definition prediction.cc:60
void WalkAlong(const Terrain &terrain, float &x, float &z, float direction_x, float direction_z, bool run, float seconds)
Definition walking.cc:19