nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
predicted_bag.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_PREDICTED_BAG_H_
2#define NANOMMO_CLIENT_PREDICTED_BAG_H_
3
4#include <cstdint>
5#include <optional>
6
7#include "content/items.h"
8#include "protocol/nanommo.pb.h"
9
10namespace nanommo {
11
12/// The bag a step ahead of the server: what the player just did with an item,
13/// done at once to a copy of the bag, by the rules the server keeps, so that
14/// an item lands the moment it's dropped, rather than a round trip later. Once
15/// the server's bag comes at a new revision, it's shown instead; if it doesn't
16/// come in time, as when the server refused, the copy goes, and the item's
17/// back where the server has it.
19 public:
20 /// In seconds.
21 static constexpr float kPatience = 1.0f;
22
23 explicit PredictedBag(const ItemTable& items) : items_(items) {}
24
25 /// The bag to show, with the frame's seconds gone by: ours while we're
26 /// ahead, or the server's.
27 protocol::Inventory Shown(const protocol::Inventory& server, float seconds);
28
29 /// What the player asked for, done to the bag shown, if the rules allow it.
30 void Act(const protocol::Inventory& server,
31 const protocol::ItemAction& action);
32
33 bool ahead() const { return ahead_.has_value(); }
34
35 private:
36 const ItemTable& items_;
37 std::optional<protocol::Inventory> ahead_;
38 // The server's revision when we went ahead, and how long we've waited.
39 uint32_t revision_ = 0;
40 float waited_ = 0.0f;
41};
42
43} // namespace nanommo
44
45#endif // NANOMMO_CLIENT_PREDICTED_BAG_H_
static constexpr float kPatience
In seconds.
protocol::Inventory Shown(const protocol::Inventory &server, float seconds)
void Act(const protocol::Inventory &server, const protocol::ItemAction &action)
What the player asked for, done to the bag shown, if the rules allow it.
PredictedBag(const ItemTable &items)
Definition chat.cc:9
The items in the items file, by id.
Definition items.h:18