nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
items.cc
Go to the documentation of this file.
1#include "content/items.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <map>
6#include <string>
7#include <string_view>
8#include <utility>
9
10#include "absl/strings/str_cat.h"
11#include "content/checks.h"
12#include "content/figures.h"
13#include "content/text.h"
14
15namespace nanommo {
16
17namespace {
18
19// What one item's parts can't be together, or what's wrong with one of them.
20absl::Status CheckParts(const items::Item& item) {
21 const std::string& name = item.name();
22 if (item.has_weapon() && item.equip().slot() != items::SLOT_MAIN_HAND) {
23 return absl::InvalidArgumentError(absl::StrCat(
24 "the ", name, " is a weapon, so it's held in the main hand"));
25 }
26 if (item.has_equip() && item.equip().slot() == items::SLOT_NONE) {
27 return absl::InvalidArgumentError(
28 absl::StrCat("the ", name, " is worn, but has no slot to be worn in"));
29 }
30 // Worn items, and items that roll, are each one of a kind, which a stack of
31 // them can't be.
32 if (StackSize(item) > 1 && (item.has_equip() || !item.rolls().empty())) {
33 return absl::InvalidArgumentError(absl::StrCat(
34 "the ", name, " stacks, so it can't be worn, or roll its own"));
35 }
36 for (const items::Modifier& modifier : item.equip().modifiers()) {
37 if (modifier.stat() == items::STAT_NONE) {
38 return absl::InvalidArgumentError(
39 absl::StrCat("the ", name, " adds to no stat"));
40 }
41 }
42 for (const figures::Part& part : item.look().parts()) {
43 const absl::Status checked = CheckPart(part);
44 if (!checked.ok()) {
45 return absl::InvalidArgumentError(
46 absl::StrCat("the ", name, "'s look: ", checked.message()));
47 }
48 }
49 for (const items::Roll& roll : item.rolls()) {
50 if (roll.stat() == items::STAT_NONE || roll.min() > roll.max()) {
51 return absl::InvalidArgumentError(
52 absl::StrCat("the ", name, " rolls from ", roll.min(), " to ",
53 roll.max(), " on ", items::Stat_Name(roll.stat())));
54 }
55 }
56 return absl::OkStatus();
57}
58
59} // namespace
60
61absl::StatusOr<ItemTable> ParseItems(std::string_view text) {
62 items::Items file;
63 const absl::Status parsed = ParseText(text, file);
64 if (!parsed.ok()) {
65 return parsed;
66 }
67 absl::StatusOr<std::map<uint32_t, items::Item>> by_id = ById(file.items());
68 if (!by_id.ok()) {
69 return by_id.status();
70 }
71 for (const auto& [id, item] : *by_id) {
72 const absl::Status checked = CheckParts(item);
73 if (!checked.ok()) {
74 return checked;
75 }
76 if (std::ranges::find(file.retired(), id) != file.retired().end()) {
77 return absl::InvalidArgumentError(absl::StrCat(
78 "the ", item.name(), " has the id ", id, ", which is retired"));
79 }
80 }
81 ItemTable table{.by_id = *std::move(by_id),
82 .starting = {file.starting().begin(), file.starting().end()}};
83 for (const items::Stack& stack : table.starting) {
84 const auto item = table.by_id.find(stack.item());
85 if (item == table.by_id.end()) {
86 return absl::InvalidArgumentError(
87 absl::StrCat("there's no item ", stack.item(), " to start with"));
88 }
89 if (CountOf(stack) > StackSize(item->second)) {
90 return absl::InvalidArgumentError(
91 absl::StrCat("a new player starts with ", CountOf(stack), " of the ",
92 item->second.name(), ", which don't fit in a slot"));
93 }
94 }
95 return table;
96}
97
98uint32_t StackSize(const items::Item& item) {
99 return std::max(item.stack(), 1u);
100}
101
102uint32_t CountOf(const items::Stack& stack) {
103 return std::max(stack.count(), 1u);
104}
105
106std::string_view SlotName(items::Slot slot) {
107 switch (slot) {
108 case items::SLOT_MAIN_HAND:
109 return "main hand";
110 case items::SLOT_OFF_HAND:
111 return "off hand";
112 case items::SLOT_HEAD:
113 return "head";
114 case items::SLOT_BODY:
115 return "body";
116 case items::SLOT_LEGS:
117 return "legs";
118 case items::SLOT_FEET:
119 return "feet";
120 default:
121 return "nowhere";
122 }
123}
124
125} // namespace nanommo
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::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
Definition checks.h:19
absl::StatusOr< ItemTable > ParseItems(std::string_view text)
Definition items.cc:61
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::Status CheckPart(const figures::Part &part)
Definition figures.cc:24
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
Definition items.cc:98
std::string_view SlotName(items::Slot slot)
The slot's name, as a player would say it, such as "main hand".
Definition items.cc:106
The items in the items file, by id.
Definition items.h:18
std::vector< items::Stack > starting
What each new player starts with.
Definition items.h:21
std::map< uint32_t, items::Item > by_id
Definition items.h:19