nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
npcs.cc
Go to the documentation of this file.
1#include "content/npcs.h"
2
3#include <cstdint>
4#include <map>
5#include <string_view>
6#include <utility>
7
8#include "absl/status/status.h"
9#include "absl/strings/str_cat.h"
10#include "content/checks.h"
11#include "content/text.h"
12
13namespace nanommo {
14
15absl::StatusOr<NpcTable> ParseNpcs(std::string_view text) {
16 npcs::Npcs file;
17 const absl::Status parsed = ParseText(text, file);
18 if (!parsed.ok()) {
19 return parsed;
20 }
21 absl::StatusOr<std::map<uint32_t, npcs::Kind>> kinds = ById(file.kinds());
22 if (!kinds.ok()) {
23 return kinds.status();
24 }
25 NpcTable table{.kinds = *std::move(kinds),
26 .spawns = {file.spawns().begin(), file.spawns().end()}};
27 for (const auto& [id, kind] : table.kinds) {
28 if (kind.look().skin() == 0) {
29 return absl::InvalidArgumentError(
30 absl::StrCat("the ", kind.name(), " has no skin"));
31 }
32 }
33 for (const npcs::Spawn& spawn : table.spawns) {
34 const absl::Status checked = CheckSpawn(table.kinds, spawn);
35 if (!checked.ok()) {
36 return checked;
37 }
38 }
39 return table;
40}
41
42} // namespace nanommo
Definition chat.cc:9
absl::StatusOr< std::map< uint32_t, Entry > > ById(const google::protobuf::RepeatedPtrField< Entry > &entries)
Definition checks.h:19
absl::Status ParseText(std::string_view text, google::protobuf::Message &message)
Definition text.cc:32
absl::StatusOr< NpcTable > ParseNpcs(std::string_view text)
Definition npcs.cc:15
absl::Status CheckSpawn(const std::map< uint32_t, Kind > &kinds, const Spawn &spawn)
Whether a spawn names a kind we have, in a land we have.
Definition checks.h:37
The kinds of NPC, by id, and where each NPC stands.
Definition npcs.h:20
std::vector< npcs::Spawn > spawns
Definition npcs.h:22
std::map< uint32_t, npcs::Kind > kinds
Definition npcs.h:21