nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
inventory.cc
Go to the documentation of this file.
1#include "server/inventory.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <random>
6#include <string>
7#include <utility>
8
9#include "absl/status/status.h"
10#include "protocol/messages.h"
11
12namespace nanommo {
13
14namespace {
15
16bool IsPlace(const protocol::Place& place) {
17 switch (place.where_case()) {
18 case protocol::Place::kBag:
19 return place.bag() < Inventory::kBagSlots;
20 case protocol::Place::kWorn:
21 return place.worn() != items::SLOT_NONE &&
22 items::Slot_IsValid(place.worn());
23 case protocol::Place::WHERE_NOT_SET:
24 break;
25 }
26 return false;
27}
28
29// The bag's slots and the worn ones are each a map, by number.
30uint32_t Key(const protocol::Place& place) {
31 return place.has_bag() ? place.bag() : static_cast<uint32_t>(place.worn());
32}
33
34bool SamePlace(const protocol::Place& a, const protocol::Place& b) {
35 return a.where_case() == b.where_case() && Key(a) == Key(b);
36}
37
38const items::Item* Find(const ItemTable& table, uint32_t id) {
39 const auto found = table.by_id.find(id);
40 return found != table.by_id.end() ? &found->second : nullptr;
41}
42
43absl::Status Nothing() {
44 return Refusal(absl::StatusCode::kNotFound, Msg("There's nothing there."));
45}
46
47absl::Status NotThatMuchGold() {
48 return Refusal(absl::StatusCode::kFailedPrecondition,
49 Msg("You haven't that much gold."));
50}
51
52uint32_t StackSizeOf(const ItemTable& table, uint32_t id) {
53 const items::Item* item = Find(table, id);
54 return item != nullptr ? StackSize(*item) : 1;
55}
56
57// Whether the stack can go at the place: anything goes in the bag, and a slot
58// that's worn takes what's worn in it.
59absl::Status CheckFits(const ItemTable& table, const items::Stack& stack,
60 const protocol::Place& place) {
61 if (!place.has_worn()) {
62 return absl::OkStatus();
63 }
64 const items::Item* item = Find(table, stack.item());
65 if (item == nullptr) {
66 return Refusal(absl::StatusCode::kFailedPrecondition,
67 Msg("You can't wear that."));
68 }
69 if (!item->has_equip()) {
70 return Refusal(absl::StatusCode::kFailedPrecondition,
71 Msg("You can't wear the $0.", Of(*item)));
72 }
73 if (item->equip().slot() != place.worn()) {
74 // Translators: $1 is the slot, such as "main hand".
75 return Refusal(absl::StatusCode::kFailedPrecondition,
76 Msg("The $0 goes in the $1 slot.", Of(*item),
77 Of(item->equip().slot())));
78 }
79 return absl::OkStatus();
80}
81
82} // namespace
83
84uint64_t NewSerial() {
85 // Not from a world's own numbers, as two copies' seeds can be the same.
86 std::random_device device;
87 return uint64_t{device()} << 32 | device();
88}
89
90const items::Stack* Inventory::At(const protocol::Place& place) const {
91 if (!IsPlace(place)) {
92 return nullptr;
93 }
94 const auto& slots = place.has_bag() ? items_.bag() : items_.worn();
95 const auto found = slots.find(Key(place));
96 return found != slots.end() ? &found->second : nullptr;
97}
98
99items::Stack* Inventory::MutableAt(const protocol::Place& place) {
100 if (!IsPlace(place)) {
101 return nullptr;
102 }
103 auto& slots =
104 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
105 const auto found = slots.find(Key(place));
106 return found != slots.end() ? &found->second : nullptr;
107}
108
109void Inventory::Put(const protocol::Place& place, items::Stack stack) {
110 auto& slots =
111 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
112 slots[Key(place)] = std::move(stack);
113}
114
115void Inventory::Clear(const protocol::Place& place) {
116 auto& slots =
117 place.has_bag() ? *items_.mutable_bag() : *items_.mutable_worn();
118 slots.erase(Key(place));
119}
120
121uint32_t Inventory::Add(const ItemTable& table, const items::Stack& stack) {
122 uint32_t left = CountOf(stack);
123 const items::Item* item = Find(table, stack.item());
124 if (item == nullptr) {
125 return left;
126 }
127 const uint32_t before = left;
128 const uint32_t size = StackSize(*item);
129 auto& bag = *items_.mutable_bag();
130 if (size > 1) {
131 for (uint32_t slot = 0; slot < kBagSlots && left > 0; ++slot) {
132 const auto found = bag.find(slot);
133 if (found != bag.end() && found->second.item() == stack.item() &&
134 found->second.count() < size) {
135 const uint32_t onto = std::min(left, size - found->second.count());
136 found->second.set_count(found->second.count() + onto);
137 left -= onto;
138 }
139 }
140 }
141 for (uint32_t slot = 0; slot < kBagSlots && left > 0; ++slot) {
142 if (!bag.contains(slot)) {
143 items::Stack put = stack;
144 put.set_count(std::min(left, size));
145 left -= put.count();
146 bag[slot] = std::move(put);
147 }
148 }
149 if (left < before) {
150 Changed();
151 }
152 return left;
153}
154
155void Inventory::Number(const ItemTable& table) {
156 bool numbered = false;
157 for (auto* slots : {items_.mutable_bag(), items_.mutable_worn()}) {
158 for (auto& [slot, stack] : *slots) {
159 if (stack.serial() == 0 && StackSizeOf(table, stack.item()) == 1) {
160 stack.set_serial(NewSerial());
161 numbered = true;
162 }
163 }
164 }
165 if (numbered) {
166 Changed();
167 }
168}
169
170uint32_t Inventory::AddGold(uint32_t amount) {
171 const uint32_t room = kMostGold > gold() ? kMostGold - gold() : 0;
172 const uint32_t added = std::min(amount, room);
173 if (added > 0) {
174 items_.set_gold(gold() + added);
175 Changed();
176 }
177 return amount - added;
178}
179
180bool Inventory::Wear(const ItemTable& table, const items::Stack& stack) {
181 const items::Item* item = Find(table, stack.item());
182 if (item == nullptr || !item->has_equip() ||
183 At(nanommo::Worn(item->equip().slot())) != nullptr) {
184 return false;
185 }
186 items::Stack worn = stack;
187 worn.set_count(1);
188 Put(nanommo::Worn(item->equip().slot()), std::move(worn));
189 Changed();
190 return true;
191}
192
193absl::Status Inventory::Move(const ItemTable& table,
194 const protocol::Place& from,
195 const protocol::Place& to, uint32_t count) {
196 if (!IsPlace(from) || !IsPlace(to)) {
197 return Refusal(absl::StatusCode::kInvalidArgument,
198 Msg("There's no such place."));
199 }
200 items::Stack* moving = MutableAt(from);
201 if (moving == nullptr) {
202 return Nothing();
203 }
204 if (SamePlace(from, to)) {
205 return absl::OkStatus();
206 }
207 if (const absl::Status fits = CheckFits(table, *moving, to); !fits.ok()) {
208 return fits;
209 }
210 const uint32_t all = moving->count();
211 const uint32_t moved = count == 0 ? all : std::min(count, all);
212 items::Stack* there = MutableAt(to);
213 if (there == nullptr) {
214 items::Stack part = *moving;
215 part.set_count(moved);
216 moving->set_count(all - moved);
217 if (moving->count() == 0) {
218 Clear(from);
219 }
220 Put(to, std::move(part));
221 } else if (there->item() == moving->item() &&
222 StackSizeOf(table, there->item()) > 1) {
223 const uint32_t size = StackSizeOf(table, there->item());
224 const uint32_t room = size > there->count() ? size - there->count() : 0;
225 if (room == 0) {
226 return Refusal(absl::StatusCode::kFailedPrecondition,
227 Msg("That stack is full."));
228 }
229 const uint32_t onto = std::min(moved, room);
230 there->set_count(there->count() + onto);
231 moving->set_count(all - onto);
232 if (moving->count() == 0) {
233 Clear(from);
234 }
235 } else {
236 if (moved < all) {
237 return Refusal(absl::StatusCode::kFailedPrecondition,
238 Msg("Only a whole stack can swap places."));
239 }
240 if (const absl::Status fits = CheckFits(table, *there, from); !fits.ok()) {
241 return fits;
242 }
243 std::swap(*moving, *there);
244 }
245 Changed();
246 return absl::OkStatus();
247}
248
249absl::Status Inventory::TakeOne(const protocol::Place& place) {
250 items::Stack* stack = MutableAt(place);
251 if (stack == nullptr) {
252 return Nothing();
253 }
254 if (stack->count() > 1) {
255 stack->set_count(stack->count() - 1);
256 } else {
257 Clear(place);
258 }
259 Changed();
260 return absl::OkStatus();
261}
262
263absl::Status Inventory::Take(const protocol::Place& place, uint32_t count) {
264 items::Stack* stack = MutableAt(place);
265 if (stack == nullptr || stack->count() < count) {
266 return Refusal(absl::StatusCode::kNotFound,
267 Msg("There aren't that many there."));
268 }
269 if (stack->count() > count) {
270 stack->set_count(stack->count() - count);
271 } else {
272 Clear(place);
273 }
274 Changed();
275 return absl::OkStatus();
276}
277
278absl::Status Inventory::TakeGold(uint32_t amount) {
279 if (amount > gold()) {
280 return NotThatMuchGold();
281 }
282 if (amount > 0) {
283 items_.set_gold(gold() - amount);
284 Changed();
285 }
286 return absl::OkStatus();
287}
288
289absl::Status Inventory::Destroy(const protocol::Place& place) {
290 if (At(place) == nullptr) {
291 return Nothing();
292 }
293 Clear(place);
294 Changed();
295 return absl::OkStatus();
296}
297
298} // namespace nanommo
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
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
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 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
uint32_t CountOf(const items::Stack &stack)
How many are in the stack, which is one when it doesn't say.
Definition items.cc:102
absl::Status Refusal(absl::StatusCode code, const Sentence &why)
Definition messages.cc:218
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
Definition items.cc:98
protocol::Place Worn(items::Slot slot)
A slot that's worn.
Definition places.cc:13
Sentence Msg(std::string_view text, Parts... parts)
Definition messages.h:92
Part Of(const items::Item &item, uint32_t count)
Definition messages.cc:139
uint64_t NewSerial()
Definition inventory.cc:84
The items in the items file, by id.
Definition items.h:18