nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
predicted_bag.cc
Go to the documentation of this file.
2
3#include "absl/status/status.h"
4#include "server/inventory.h"
5
6namespace nanommo {
7
8protocol::Inventory PredictedBag::Shown(const protocol::Inventory& server,
9 float seconds) {
10 if (!ahead_) {
11 return server;
12 }
13 waited_ += seconds;
14 if (server.revision() != revision_ || waited_ > kPatience) {
15 ahead_.reset();
16 return server;
17 }
18 return *ahead_;
19}
20
21void PredictedBag::Act(const protocol::Inventory& server,
22 const protocol::ItemAction& action) {
23 // On top of what's shown, so that two quick moves both show.
24 Inventory bag(ahead_ ? *ahead_ : server);
25 absl::Status done = absl::OkStatus();
26 switch (action.action_case()) {
27 case protocol::ItemAction::kMove:
28 done = bag.Move(items_, action.move().from(), action.move().to(),
29 action.move().count());
30 break;
31 case protocol::ItemAction::kUse:
32 done = bag.TakeOne(InBag(action.use()));
33 break;
34 case protocol::ItemAction::kDestroy:
35 done = bag.Destroy(action.destroy());
36 break;
37 case protocol::ItemAction::ACTION_NOT_SET:
38 return;
39 }
40 if (!done.ok()) {
41 return;
42 }
43 if (!ahead_) {
44 revision_ = server.revision();
45 }
46 ahead_ = bag.items();
47 waited_ = 0.0f;
48}
49
50} // namespace nanommo
absl::Status Destroy(const protocol::Place &place)
Throws away all of what's at the place.
Definition inventory.cc:289
absl::Status Move(const ItemTable &table, const protocol::Place &from, const protocol::Place &to, uint32_t count=0)
Definition inventory.cc:193
absl::Status TakeOne(const protocol::Place &place)
Takes one off the stack at the place, as using one does.
Definition inventory.cc:249
const protocol::Inventory & items() const
Definition inventory.h:35
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.
Definition chat.cc:9
protocol::Place InBag(uint32_t slot)
A place in the bag, by its slot from zero.
Definition places.cc:7