nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
interpolation.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <cstdint>
7#include <iterator>
8#include <limits>
9#include <utility>
10
11namespace nanommo {
12
13namespace {
14
15// A second of worlds, which is plenty to draw from, and to learn the clocks
16// from.
17constexpr std::size_t kMostSnapshots = 20;
18
19// From a to b, by the share of the way from one to the other.
20float Mix(float a, float b, float share) { return a + (b - a) * share; }
21
22// Each unit in the newest world but you, where it was between the two
23// snapshots, by the share of the way from one to the other.
24template <typename Unit, typename Places>
25void Between(google::protobuf::RepeatedPtrField<Unit>& units, uint32_t you,
26 const Places& before, const Places& after, float share) {
27 for (Unit& unit : units) {
28 const auto from = before.find(unit.id());
29 const auto to = after.find(unit.id());
30 if (unit.id() == you || from == before.end() || to == after.end()) {
31 continue;
32 }
33 unit.set_x(Mix(from->second.x, to->second.x, share));
34 unit.set_z(Mix(from->second.z, to->second.z, share));
35 }
36}
37
38} // namespace
39
40void Interpolation::Add(const protocol::World& world, double now) {
41 const double sent = static_cast<double>(world.sent()) / 1e6;
42 // A part of an older world came late, and is no use now.
43 if (!snapshots_.empty() && sent < snapshots_.back().sent) {
44 return;
45 }
46 if (snapshots_.empty() || sent > snapshots_.back().sent) {
47 snapshots_.push_back({.sent = sent, .ahead = now - sent});
48 if (snapshots_.size() > kMostSnapshots) {
49 snapshots_.pop_front();
50 }
51 }
52 Snapshot& snapshot = snapshots_.back();
53 for (const protocol::Player& player : world.players()) {
54 snapshot.players[player.id()] = {player.x(), player.z()};
55 }
56 for (const protocol::Monster& monster : world.monsters()) {
57 snapshot.monsters[monster.id()] = {monster.x(), monster.z()};
58 }
59}
60
61double Interpolation::Then(double now) const {
62 // The world that took least to come shows best how far apart the clocks
63 // are: the others were held up on the way by that much more.
64 double ahead = std::numeric_limits<double>::infinity();
65 for (const Snapshot& snapshot : snapshots_) {
66 ahead = std::min(ahead, snapshot.ahead);
67 }
68 return now - ahead - kDelay;
69}
70
71int64_t Interpolation::Drawn(double now) const {
72 if (snapshots_.empty()) {
73 return 0;
74 }
75 return std::llround(Then(now) * 1e6);
76}
77
78protocol::World Interpolation::Shown(const protocol::World& newest,
79 double now) const {
80 protocol::World shown = newest;
81 if (snapshots_.size() < 2) {
82 return shown;
83 }
84 const double then = Then(now);
85 // The last snapshot at or before then, and the one after it. With none
86 // after it, the next world is late or lost, and the last two carry them on
87 // the way they were going, kMostAhead at most.
88 auto after = std::ranges::find_if(
89 snapshots_, [then](const Snapshot& s) { return s.sent > then; });
90 if (after == snapshots_.begin()) {
91 return shown;
92 }
93 if (after == snapshots_.end()) {
94 after = std::prev(snapshots_.end());
95 }
96 const Snapshot& from = *std::prev(after);
97 const Snapshot& to = *after;
98 const double gap = to.sent - from.sent;
99 const float share = static_cast<float>(
100 std::min((then - from.sent) / gap, 1.0 + kMostAhead / gap));
101 Between(*shown.mutable_players(), newest.you(), from.players, to.players,
102 share);
103 Between(*shown.mutable_monsters(), 0, from.monsters, to.monsters, share);
104 return shown;
105}
106
107} // namespace nanommo
void Add(const protocol::World &world, double now)
int64_t Drawn(double now) const
static constexpr double kDelay
protocol::World Shown(const protocol::World &newest, double now) const
static constexpr double kMostAhead
Definition chat.cc:9
void Mix(Samples &mix, const Samples &sound, float at, float gain)
Definition synth.cc:69