nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
prediction.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_PREDICTION_H_
2#define NANOMMO_CLIENT_PREDICTION_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <deque>
7
8#include "protocol/nanommo.pb.h"
9#include "terrain/terrain.h"
10
11namespace nanommo {
12
13/// Where you are before the server says so. Each tick's keys walk you at
14/// once, as the server will when they reach it, and each world from the
15/// server puts you where it has you, after the last input it applied, and
16/// walks you again through the ones it hasn't yet.
18 public:
19 /// A tick of the keys sent with the sequence: a direction, which is zero
20 /// for standing still, and whether you run.
21 void Walk(const Terrain& terrain, uint32_t sequence, float direction_x,
22 float direction_z, bool run, float seconds);
23
24 /// Where the server has you, after the input it acked, and how long each of
25 /// its ticks is. A small difference from where we had you goes away over a
26 /// moment, and a large one, such as going through a gate, at once.
27 void Correct(const Terrain& terrain, float x, float z, uint32_t acked,
28 float seconds);
29
30 /// Fades what the last corrections moved, over the frame's time.
31 void Settle(float seconds);
32
33 /// Where we have you, and where to draw you, with what's left of a
34 /// correction still to fade.
35 float x() const { return x_; }
36 float z() const { return z_; }
37 float shown_x() const { return x_ + error_x_; }
38 float shown_z() const { return z_ + error_z_; }
39 bool placed() const { return placed_; }
40
41 /// Inputs sent and not yet acked.
42 std::size_t pending() const { return pending_.size(); }
43
44 private:
45 struct Keys {
46 uint32_t sequence = 0;
47 float x = 0.0f;
48 float z = 0.0f;
49 bool run = false;
50 };
51 std::deque<Keys> pending_;
52 float x_ = 0.0f;
53 float z_ = 0.0f;
54 float error_x_ = 0.0f;
55 float error_z_ = 0.0f;
56 bool placed_ = false;
57};
58
59/// The world as the server sent it, with you where the prediction draws you,
60/// and walking, running or standing as your keys say, facing the way they
61/// go, unless the server has you doing something else, such as a swing, or
62/// it steers you, when you move as it says.
63protocol::World Predicted(const protocol::World& world,
64 const Prediction& prediction, float direction_x,
65 float direction_z, bool run);
66
67} // namespace nanommo
68
69#endif // NANOMMO_CLIENT_PREDICTION_H_
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
std::size_t pending() const
Inputs sent and not yet acked.
Definition prediction.h:42
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