14constexpr float kJump = 2.0f;
17constexpr float kFade = 20.0f;
21constexpr std::size_t kMostPending = 100;
26 float direction_x,
float direction_z,
bool run,
28 pending_.push_back({sequence, direction_x, direction_z, run});
29 if (pending_.size() > kMostPending) {
33 WalkAlong(terrain, x_, z_, direction_x, direction_z, run, seconds);
38 uint32_t acked,
float seconds) {
39 while (!pending_.empty() && pending_.front().sequence <= acked) {
42 const float was_x = x_;
43 const float was_z = z_;
46 for (
const Keys& keys : pending_) {
47 WalkAlong(terrain, x_, z_, keys.x, keys.z, keys.run, seconds);
50 if (placed_ && std::hypot(was_x - x_, was_z - z_) < kJump) {
51 error_x_ += was_x - x_;
52 error_z_ += was_z - z_;
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()) {
67 for (protocol::Player& player : *shown.mutable_players()) {
68 if (player.id() != shown.you()) {
71 player.set_x(prediction.
shown_x());
72 player.set_z(prediction.
shown_z());
73 if (shown.steered()) {
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);
84 player.set_yaw(std::atan2(direction_x, direction_z));
92 const float left = std::exp(-kFade * seconds);
void Walk(const Terrain &terrain, uint32_t sequence, float direction_x, float direction_z, bool run, float seconds)
void Settle(float seconds)
Fades what the last corrections moved, over the frame's time.
void Correct(const Terrain &terrain, float x, float z, uint32_t acked, float seconds)
protocol::World Predicted(const protocol::World &world, const Prediction &prediction, float direction_x, float direction_z, bool run)
void WalkAlong(const Terrain &terrain, float &x, float &z, float direction_x, float direction_z, bool run, float seconds)