nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
bag.cc
Go to the documentation of this file.
1#include "bot/bag.h"
2
3#include <cstdint>
4#include <optional>
5
6#include "protocol/places.h"
7
8namespace nanommo {
9
10std::optional<protocol::ItemAction> Tend(const ItemTable& items,
11 const protocol::Inventory& bag,
12 int health, int max_health,
13 bool can_drink) {
14 const bool hurt = health > 0 && health * 2 < max_health;
15 for (uint32_t slot = 0; slot < kBagSlots; ++slot) {
16 const auto stack = bag.bag().find(slot);
17 const auto item = stack != bag.bag().end()
18 ? items.by_id.find(stack->second.item())
19 : items.by_id.end();
20 if (item == items.by_id.end()) {
21 continue;
22 }
23 protocol::ItemAction action;
24 if (hurt && can_drink && item->second.use().heal() > 0) {
25 action.set_use(slot);
26 return action;
27 }
28 const items::Slot worn = item->second.equip().slot();
29 if (!hurt && item->second.has_equip() && !bag.worn().contains(worn)) {
30 *action.mutable_move()->mutable_from() = InBag(slot);
31 *action.mutable_move()->mutable_to() = Worn(worn);
32 return action;
33 }
34 }
35 return std::nullopt;
36}
37
38} // namespace nanommo
Definition chat.cc:9
constexpr uint32_t kBagSlots
How many slots each player's bag has.
Definition items.h:15
protocol::Place Worn(items::Slot slot)
A slot that's worn.
Definition places.cc:13
std::optional< protocol::ItemAction > Tend(const ItemTable &items, const protocol::Inventory &bag, int health, int max_health, bool can_drink)
Definition bag.cc:10
protocol::Place InBag(uint32_t slot)
A place in the bag, by its slot from zero.
Definition places.cc:7
The items in the items file, by id.
Definition items.h:18
std::map< uint32_t, items::Item > by_id
Definition items.h:19