nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
inventory.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_INVENTORY_H_
2#define NANOMMO_SERVER_INVENTORY_H_
3
4#include <cstdint>
5#include <utility>
6
7#include "absl/status/status.h"
8#include "content/items.h"
9#include "protocol/nanommo.pb.h"
10#include "protocol/places.h"
11
12namespace nanommo {
13
14/// A serial for one of an item that doesn't stack: 64 random bits from the
15/// system, so that two items share one by chance about once in 2^64 for each
16/// pair, which is never.
17uint64_t NewSerial();
18
19/// What a player carries, in a bag of kBagSlots, and wears, an item in each
20/// slot, kept as the message that's saved and sent. Each change takes it to
21/// its next revision. Items the table doesn't have, such as one taken out of
22/// the content, stay where they are, so that nothing is lost by a mistake in
23/// the file, but they can't be worn, used or stacked.
24class Inventory {
25 public:
26 static constexpr uint32_t kBagSlots = nanommo::kBagSlots;
27
28 /// The most gold a player carries: a billion, so that adding any two
29 /// amounts of it never goes past what a uint32_t holds.
30 static constexpr uint32_t kMostGold = 1'000'000'000;
31
32 Inventory() = default;
33 explicit Inventory(protocol::Inventory items) : items_(std::move(items)) {}
34
35 const protocol::Inventory& items() const { return items_; }
36 uint32_t revision() const { return items_.revision(); }
37 uint32_t gold() const { return items_.gold(); }
38
39 /// What's at the place, or nullptr when it's empty, or isn't a place.
40 const items::Stack* At(const protocol::Place& place) const;
41
42 /// Into the bag: onto stacks of the item that have room, then into empty
43 /// slots, from the first. Returns how many didn't fit.
44 uint32_t Add(const ItemTable& table, const items::Stack& stack);
45
46 /// Adds the gold, up to kMostGold, and returns how much didn't fit.
47 uint32_t AddGold(uint32_t amount);
48
49 /// Wears it, if it's worn and its slot is free, and returns whether it did,
50 /// as for a new player's kit.
51 bool Wear(const ItemTable& table, const items::Stack& stack);
52
53 /// Gives each item that doesn't stack and has no serial, such as the kit's
54 /// or one saved before serials, a NewSerial, and moves to the next revision
55 /// if it gave any, so that they're saved.
56 void Number(const ItemTable& table);
57
58 /// Moves count of what's at from to to, or all of it given zero: into an
59 /// empty place, onto a stack of the same item, as many as it has room for,
60 /// or swapping with what's there. What's worn has to be worn in its slot.
61 /// Or the error says why not, in words for the player.
62 absl::Status Move(const ItemTable& table, const protocol::Place& from,
63 const protocol::Place& to, uint32_t count = 0);
64
65 /// Takes one off the stack at the place, as using one does.
66 absl::Status TakeOne(const protocol::Place& place);
67
68 /// Takes count off the stack at the place, as trading some does, or the
69 /// error says there aren't that many there.
70 absl::Status Take(const protocol::Place& place, uint32_t count);
71
72 /// Takes the gold away, or the error says there isn't that much.
73 absl::Status TakeGold(uint32_t amount);
74
75 /// Throws away all of what's at the place.
76 absl::Status Destroy(const protocol::Place& place);
77
78 private:
79 items::Stack* MutableAt(const protocol::Place& place);
80 void Put(const protocol::Place& place, items::Stack stack);
81 void Clear(const protocol::Place& place);
82 void Changed() { items_.set_revision(items_.revision() + 1); }
83
84 protocol::Inventory items_;
85};
86
87} // namespace nanommo
88
89#endif // NANOMMO_SERVER_INVENTORY_H_
bool Wear(const ItemTable &table, const items::Stack &stack)
Definition inventory.cc:180
uint32_t AddGold(uint32_t amount)
Adds the gold, up to kMostGold, and returns how much didn't fit.
Definition inventory.cc:170
Inventory(protocol::Inventory items)
Definition inventory.h:33
static constexpr uint32_t kBagSlots
Definition inventory.h:26
absl::Status Destroy(const protocol::Place &place)
Throws away all of what's at the place.
Definition inventory.cc:289
uint32_t revision() const
Definition inventory.h:36
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
uint32_t Add(const ItemTable &table, const items::Stack &stack)
Definition inventory.cc:121
uint32_t gold() const
Definition inventory.h:37
const protocol::Inventory & items() const
Definition inventory.h:35
const items::Stack * At(const protocol::Place &place) const
What's at the place, or nullptr when it's empty, or isn't a place.
Definition inventory.cc:90
void Number(const ItemTable &table)
Definition inventory.cc:155
static constexpr uint32_t kMostGold
Definition inventory.h:30
absl::Status Take(const protocol::Place &place, uint32_t count)
Definition inventory.cc:263
absl::Status TakeGold(uint32_t amount)
Takes the gold away, or the error says there isn't that much.
Definition inventory.cc:278
Definition chat.cc:9
constexpr uint32_t kBagSlots
How many slots each player's bag has.
Definition items.h:15
uint64_t NewSerial()
Definition inventory.cc:84
What a change to another member of a guild did, for the news.
Definition guilds.h:43
The items in the items file, by id.
Definition items.h:18