nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
content.cc
Go to the documentation of this file.
1#include "content/content.h"
2
3#include <cstdint>
4#include <set>
5#include <string>
6#include <string_view>
7#include <utility>
8#include <vector>
9
10#include "absl/strings/str_cat.h"
11#include "content/text.h"
12#include "google/protobuf/io/coded_stream.h"
13#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
14
15namespace nanommo {
16
17namespace {
18
19// Reads one of the folder's files into its table, with the file's name in
20// front of what's wrong with it.
21template <typename Table>
22absl::Status Read(const std::string& folder, const char* file,
23 absl::StatusOr<Table> (*parse)(std::string_view),
24 Table& table) {
25 const std::string path = absl::StrCat(folder, "/", file);
26 const absl::StatusOr<std::string> text = ReadFile(path);
27 if (!text.ok()) {
28 return text.status();
29 }
30 absl::StatusOr<Table> parsed = parse(*text);
31 if (!parsed.ok()) {
32 return absl::InvalidArgumentError(
33 absl::StrCat(path, ", ", parsed.status().message()));
34 }
35 table = *std::move(parsed);
36 return absl::OkStatus();
37}
38
39// FNV-1a, 64 bits wide, which gives the same hash on each machine, where
40// std::hash and absl::Hash don't promise to.
41constexpr uint64_t kOffsetBasis = 14695981039346656037u;
42constexpr uint64_t kPrime = 1099511628211u;
43
44void Add(uint64_t& hash, const google::protobuf::Message& message) {
45 std::string bytes;
46 {
47 google::protobuf::io::StringOutputStream stream(&bytes);
48 google::protobuf::io::CodedOutputStream coded(&stream);
49 // So that equal messages give equal bytes, whatever order they were
50 // built in.
51 coded.SetSerializationDeterministic(true);
52 // Writing to memory fails only for a message that lacks a required field,
53 // and the content was read whole.
54 static_cast<void>(message.SerializeToCodedStream(&coded));
55 }
56 for (const char byte : bytes) {
57 hash ^= static_cast<unsigned char>(byte);
58 hash *= kPrime;
59 }
60}
61
62} // namespace
63
64absl::StatusOr<Content> ReadContent(const std::string& folder) {
65 Content content;
66 absl::Status read =
67 Read(folder, "factions.txtpb", ParseFactions, content.factions);
68 if (read.ok()) {
69 read = Read(folder, "figures.txtpb", ParseFigures, content.figures);
70 }
71 if (read.ok()) {
72 read = Read(folder, "items.txtpb", ParseItems, content.items);
73 }
74 if (read.ok()) {
75 read = Read(folder, "monsters.txtpb", ParseMonsters, content.monsters);
76 }
77 if (read.ok()) {
78 read = Read(folder, "npcs.txtpb", ParseNpcs, content.npcs);
79 }
80 if (read.ok()) {
81 read = Read(folder, "quests.txtpb", ParseQuests, content.quests);
82 }
83 if (read.ok()) {
84 read = Read(folder, "recipes.txtpb", ParseRecipes, content.recipes);
85 }
86 if (read.ok()) {
87 read = Read(folder, "rig.txtpb", ParseRig, content.rig);
88 }
89 if (read.ok()) {
90 read = Read(folder, "skills.txtpb", ParseSkills, content.skills);
91 }
92 if (!read.ok()) {
93 return read;
94 }
95 const absl::Status checked = CheckContent(content);
96 if (!checked.ok()) {
97 return absl::InvalidArgumentError(
98 absl::StrCat(folder, ", ", checked.message()));
99 }
100 return content;
101}
102
103absl::Status CheckContent(const Content& content) {
104 // Each part moves with a joint of the rig, and starts and ends at one.
105 std::set<std::string> joints;
106 for (const rig::Joint& joint : Skeleton(content.rig, -1)) {
107 joints.insert(joint.name());
108 }
109 const auto on_the_rig =
110 [&joints](const google::protobuf::RepeatedPtrField<figures::Part>& parts,
111 std::string_view whose) {
112 for (const figures::Part& part : Unfold(parts)) {
113 for (const std::string& joint :
114 {part.bone(), part.from(), part.to()}) {
115 if (!joint.empty() && !joints.contains(joint)) {
116 return absl::InvalidArgumentError(
117 absl::StrCat(whose, " has a part on a joint ", joint,
118 " that isn't in the rig"));
119 }
120 }
121 }
122 return absl::OkStatus();
123 };
124 absl::Status rigged = on_the_rig(content.figures.body(), "the body");
125 for (int i = 0; rigged.ok() && i < content.figures.hairs_size(); ++i) {
126 rigged = on_the_rig(content.figures.hairs(i).parts(), "a way to wear hair");
127 }
128 for (const auto& [id, item] : content.items.by_id) {
129 if (rigged.ok()) {
130 rigged =
131 on_the_rig(item.look().parts(), absl::StrCat("the ", item.name()));
132 }
133 }
134 if (!rigged.ok()) {
135 return rigged;
136 }
137 for (const auto& [id, kind] : content.monsters.kinds) {
138 for (const monsters::Drop& drop : kind.loot()) {
139 if (!content.items.by_id.contains(drop.item())) {
140 return absl::InvalidArgumentError(
141 absl::StrCat("the ", kind.name(), " drops an item ", drop.item(),
142 " that isn't in the items file"));
143 }
144 }
145 }
146 for (const auto& [id, kind] : content.npcs.kinds) {
147 const npcs::Look& look = kind.look();
148 if (std::cmp_greater_equal(look.hair(), content.figures.hairs_size())) {
149 return absl::InvalidArgumentError(
150 absl::StrCat("the ", kind.name(), " has a hair ", look.hair(),
151 " that isn't in the figures file"));
152 }
153 // What it holds, if anything, and what else it wears.
154 std::vector<uint32_t> worn(look.wearing().begin(), look.wearing().end());
155 if (look.weapon() != 0) {
156 worn.push_back(look.weapon());
157 }
158 for (const uint32_t item : worn) {
159 if (!content.items.by_id.contains(item)) {
160 return absl::InvalidArgumentError(
161 absl::StrCat("the ", kind.name(), " wears an item ", item,
162 " that isn't in the items file"));
163 }
164 }
165 for (const uint32_t skill : kind.teaches()) {
166 if (!content.skills.by_id.contains(skill)) {
167 return absl::InvalidArgumentError(
168 absl::StrCat("the ", kind.name(), " teaches a skill ", skill,
169 " that isn't in the skills file"));
170 }
171 }
172 }
173 for (const auto& [id, quest] : content.quests) {
174 if (!content.npcs.kinds.contains(quest.giver())) {
175 return absl::InvalidArgumentError(
176 absl::StrCat("the ", quest.name(), " has a giver ", quest.giver(),
177 " that isn't in the NPCs file"));
178 }
179 if (!content.monsters.kinds.contains(quest.kill().monster())) {
180 return absl::InvalidArgumentError(absl::StrCat(
181 "the ", quest.name(), " asks for a monster ", quest.kill().monster(),
182 " that isn't in the monsters file"));
183 }
184 for (const quests::Prize& prize : quest.prizes()) {
185 if (!content.items.by_id.contains(prize.item())) {
186 return absl::InvalidArgumentError(
187 absl::StrCat("the ", quest.name(), " gives an item ", prize.item(),
188 " that isn't in the items file"));
189 }
190 }
191 }
192 // What a recipe takes and makes is in the items file, and as many as go in
193 // a slot of the bag, so that what it makes always has room in an empty one.
194 const auto in_a_slot = [&content](const recipes::Recipe& recipe,
195 std::string_view verb,
196 const recipes::ItemCount& some) {
197 const auto item = content.items.by_id.find(some.item());
198 if (item == content.items.by_id.end()) {
199 return absl::InvalidArgumentError(
200 absl::StrCat(recipe.name(), " ", verb, " an item ", some.item(),
201 " that isn't in the items file"));
202 }
203 if (some.count() > StackSize(item->second)) {
204 return absl::InvalidArgumentError(
205 absl::StrCat(recipe.name(), " ", verb, " ", some.count(), " of the ",
206 item->second.name(), ", which don't fit in a slot"));
207 }
208 return absl::OkStatus();
209 };
210 for (const auto& [id, recipe] : content.recipes) {
211 absl::Status fits;
212 for (int i = 0; fits.ok() && i < recipe.inputs_size(); ++i) {
213 fits = in_a_slot(recipe, "takes", recipe.inputs(i));
214 }
215 if (fits.ok()) {
216 fits = in_a_slot(recipe, "makes", recipe.output());
217 }
218 if (!fits.ok()) {
219 return fits;
220 }
221 }
222 for (const auto& [id, faction] : content.factions) {
223 if (!content.npcs.kinds.contains(faction.herald())) {
224 return absl::InvalidArgumentError(
225 absl::StrCat(faction.name(), " has a herald ", faction.herald(),
226 " that isn't in the NPCs file"));
227 }
228 }
229 return absl::OkStatus();
230}
231
232uint64_t ClientHash(const Content& content) {
233 uint64_t hash = kOffsetBasis;
234 // The clients draw the players from the figures, on the rig.
235 Add(hash, content.figures);
236 Add(hash, content.rig);
237 // A copy of each with what the clients read, by id. They show all of an
238 // item but how its weapon fights.
239 for (const auto& [id, item] : content.items.by_id) {
240 items::Item seen = item;
241 seen.clear_weapon();
242 Add(hash, seen);
243 }
244 for (const auto& [id, kind] : content.monsters.kinds) {
245 monsters::Kind seen;
246 seen.set_id(id);
247 seen.set_name(kind.name());
248 *seen.mutable_look() = kind.look();
249 // They draw its skills' shapes, filling for as long as each shows, and
250 // say their names, but how hard each hits, how often, and from how near
251 // it starts are the server's.
252 for (monsters::Skill skill : kind.skills()) {
253 skill.clear_damage();
254 skill.clear_cooldown_ms();
255 skill.clear_range();
256 *seen.add_skills() = std::move(skill);
257 }
258 // A boss's bar marks where each phase starts, and what it uses in each
259 // is the server's.
260 for (const monsters::Phase& phase : kind.phases()) {
261 seen.add_phases()->set_health(phase.health());
262 }
263 Add(hash, seen);
264 }
265 for (const auto& [id, kind] : content.npcs.kinds) {
266 npcs::Kind seen;
267 seen.set_id(id);
268 seen.set_name(kind.name());
269 seen.set_talks(kind.talks());
270 *seen.mutable_look() = kind.look();
271 *seen.mutable_teaches() = kind.teaches();
272 Add(hash, seen);
273 }
274 // The clients show all of a quest, and of a recipe.
275 for (const auto& [id, quest] : content.quests) {
276 Add(hash, quest);
277 }
278 for (const auto& [id, recipe] : content.recipes) {
279 Add(hash, recipe);
280 }
281 // The clients mark each player's name in their faction's colour, and open
282 // a herald's window on a click.
283 for (const auto& [id, faction] : content.factions) {
284 Add(hash, faction);
285 }
286 // The clients show each skill's level and cooldown, and the effect is the
287 // server's alone.
288 for (const auto& [id, skill] : content.skills.by_id) {
289 skills::Skill seen = skill;
290 seen.clear_effect();
291 Add(hash, seen);
292 }
293 return hash;
294}
295
296} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< std::map< uint32_t, quests::Quest > > ParseQuests(std::string_view text)
Definition quests.cc:13
absl::StatusOr< ItemTable > ParseItems(std::string_view text)
Definition items.cc:61
absl::StatusOr< rig::Rig > ParseRig(std::string_view text)
Definition rig.cc:49
uint64_t ClientHash(const Content &content)
Definition content.cc:232
absl::StatusOr< figures::Figures > ParseFigures(std::string_view text)
Definition figures.cc:50
absl::StatusOr< std::map< uint32_t, factions::Faction > > ParseFactions(std::string_view text)
Definition factions.cc:14
std::vector< rig::Joint > Skeleton(const rig::Rig &rig, int build)
Definition rig.cc:101
absl::StatusOr< MonsterTable > ParseMonsters(std::string_view text)
Definition monsters.cc:103
uint32_t StackSize(const items::Item &item)
How many of the item go in one slot of a bag.
Definition items.cc:98
absl::StatusOr< std::string > ReadFile(const std::string &path)
Definition text.cc:43
absl::StatusOr< NpcTable > ParseNpcs(std::string_view text)
Definition npcs.cc:15
std::vector< figures::Part > Unfold(const google::protobuf::RepeatedPtrField< figures::Part > &parts)
Definition figures.cc:96
absl::StatusOr< SkillTable > ParseSkills(std::string_view text)
Definition skills.cc:15
absl::Status CheckContent(const Content &content)
Definition content.cc:103
absl::StatusOr< std::map< uint32_t, recipes::Recipe > > ParseRecipes(std::string_view text)
Definition recipes.cc:14
absl::StatusOr< Content > ReadContent(const std::string &folder)
Reads each file in the folder, or says which file is wrong, and where.
Definition content.cc:64
ItemTable items
Definition content.h:27
std::map< uint32_t, factions::Faction > factions
By id.
Definition content.h:25
MonsterTable monsters
Definition content.h:28
SkillTable skills
Definition content.h:36
figures::Figures figures
Definition content.h:26
std::map< uint32_t, quests::Quest > quests
By id.
Definition content.h:31
std::map< uint32_t, recipes::Recipe > recipes
By id.
Definition content.h:33
rig::Rig rig
The skeleton every person is built on, and how it moves.
Definition content.h:35
NpcTable npcs
Definition content.h:29
std::map< uint32_t, items::Item > by_id
Definition items.h:19
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18
std::map< uint32_t, npcs::Kind > kinds
Definition npcs.h:21
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17