nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
inputs.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_INPUTS_H_
2#define NANOMMO_SERVER_INPUTS_H_
3
4#include <chrono>
5#include <cstddef>
6#include <cstdint>
7#include <deque>
8#include <functional>
9#include <limits>
10#include <map>
11#include <optional>
12
13#include "absl/status/status.h"
14#include "protocol/nanommo.pb.h"
15#include "server/world.h"
16
17namespace nanommo {
18
19/// What each player's inputs do to the world, whoever runs it: a world server,
20/// or a client that plays on its own. A numbered input's keys wait for their
21/// tick, so that the client knows which tick each of its inputs moved it in.
22class Inputs {
23 public:
24 /// A client sends an input a tick, so a few queue up only when the network
25 /// holds some back and lets them through together. More than this, and the
26 /// oldest go, so that a player doesn't fall further and further behind.
27 static constexpr std::size_t kMostQueued = 5;
28
29 /// A client sends one a tick, but a slow frame sends two or three together,
30 /// so up to three waiting is a client a little ahead of our tick. More means
31 /// some came late and we're behind it, so each tick skips one, until we've
32 /// caught up.
33 static constexpr std::size_t kMostBehind = 3;
34
35 /// Inputs come in as fast as they go out, so a queue a burst filled stays
36 /// that full, each input a tick late for each one ahead of it. If two or
37 /// more waited at the start of each tick for a second, the queue's kept more
38 /// than it needs, and it drops one, until one waiting is the most it keeps
39 /// for long.
40 static constexpr std::size_t kMostSteady = 2;
41 static constexpr int kSteadyTicks = 20;
42
43 /// A numbered input says when, on our clock, its client drew everyone else,
44 /// and its player's blows on a duel opponent are checked against where the
45 /// opponent was then. A client draws them 100 milliseconds before the newest
46 /// world it has, and the world's way to it and the input's way back are a
47 /// round trip more, so this is enough for a round trip of up to 100. A time
48 /// further back is taken as this, so that a client can't hit its opponent
49 /// wherever they once were, and a time ahead of ours as now.
50 static constexpr std::chrono::milliseconds kMostRewind{200};
51
52 /// The input's keys: a numbered one's wait for their tick, and others are
53 /// set at once, unless the player numbers theirs, when one that isn't is a
54 /// copy of the last, sent with something else.
55 void Take(World& land, uint32_t id, const protocol::Input& input);
56
57 /// Each player's next waiting keys, a tick's worth, in their land.
58 void Apply(const std::function<World*(uint32_t)>& land_of);
59
60 /// The last of the player's numbered inputs applied, if any.
61 std::optional<uint32_t> acked(uint32_t id) const;
62
63 /// Forgets a player who's left.
64 void Leave(uint32_t id);
65
66 private:
67 /// The fewest of a player's inputs waiting at the start of a tick, over the
68 /// ticks since their queue was last checked.
69 struct Steady {
70 std::size_t fewest = std::numeric_limits<std::size_t>::max();
71 int ticks = 0;
72 };
73
74 std::map<uint32_t, std::deque<protocol::Input>> queued_;
75 std::map<uint32_t, uint32_t> acked_;
76 std::map<uint32_t, Steady> steady_;
77};
78
79/// Moves, uses or throws away an item, as the action asks, or says why not,
80/// in words for the player.
81absl::Status DoItem(World& land, uint32_t id,
82 const protocol::ItemAction& action);
83
84/// Starts making a recipe, or stops, as the action asks, or says why not, in
85/// words for the player.
86absl::Status DoCraft(World& land, uint32_t id,
87 const protocol::CraftAction& action);
88
89} // namespace nanommo
90
91#endif // NANOMMO_SERVER_INPUTS_H_
void Apply(const std::function< World *(uint32_t)> &land_of)
Each player's next waiting keys, a tick's worth, in their land.
Definition inputs.cc:44
std::optional< uint32_t > acked(uint32_t id) const
The last of the player's numbered inputs applied, if any.
Definition inputs.cc:71
void Take(World &land, uint32_t id, const protocol::Input &input)
Definition inputs.cc:31
static constexpr std::chrono::milliseconds kMostRewind
Definition inputs.h:50
static constexpr std::size_t kMostQueued
Definition inputs.h:27
static constexpr std::size_t kMostSteady
Definition inputs.h:40
static constexpr int kSteadyTicks
Definition inputs.h:41
void Leave(uint32_t id)
Forgets a player who's left.
Definition inputs.cc:79
static constexpr std::size_t kMostBehind
Definition inputs.h:33
Definition chat.cc:9
absl::Status DoCraft(World &land, uint32_t id, const protocol::CraftAction &action)
Definition inputs.cc:101
absl::Status DoItem(World &land, uint32_t id, const protocol::ItemAction &action)
Definition inputs.cc:85