nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
graph.cc
Go to the documentation of this file.
1#include "content/graph.h"
2
3#include <algorithm>
4#include <cstdint>
5#include <map>
6#include <optional>
7#include <set>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "absl/strings/str_cat.h"
16#include "smollergraph/graph.h"
17#include "smollergraph/plan.h"
18#include "terrain/lands.h"
19
20namespace nanommo {
21namespace {
22
23// The ways a player gets from one thing to the next, which a path follows.
24// A quest's ASKS_FOR isn't one of them, and nor is an item's INTO a recipe,
25// as one item doesn't get a player to a recipe that takes others too: a new
26// player CRAFTS it once they reach them all.
27constexpr std::string_view kWays =
28 "STARTS_IN|STARTS_WITH|KNOWS|GATE|SPAWNS|STANDS|DROPS|SUMMONS|GIVES|"
29 "REWARDS|TEACHES|NEXT|CRAFTS|MAKES|ENLISTS";
30
31// Each label, and what it's called in a line of Unreachable's.
32struct Kind {
33 std::string_view label;
34 std::string_view called;
35};
36constexpr Kind kKinds[] = {
37 {"Land", "a land"}, {"Monster", "a monster"}, {"Npc", "an NPC"},
38 {"Quest", "a quest"}, {"Item", "an item"}, {"Skill", "a skill"},
39 {"Recipe", "a recipe"}, {"Faction", "a faction"},
40};
41
42// Adds the content's nodes and relationships, a name at a time, and keeps
43// the first thing that goes wrong.
44class Builder {
45 public:
46 explicit Builder(smollergraph::Graph& graph) : graph_(graph) {}
47
48 // A node of its own for each name, as a name the graph has already would
49 // join two things into one.
50 void Node(std::string_view name, std::string_view label) {
51 if (!status_.ok()) return;
52 if (name.size() > smollergraph::Graph::kMaxName) {
53 status_ = absl::InvalidArgumentError(
54 absl::StrCat("the name ", name, " is too long for a node"));
55 } else if (graph_.Find(name)) {
56 status_ = absl::InvalidArgumentError(
57 absl::StrCat("two things are called ", name));
58 } else {
59 graph_.SetLabel(graph_.AddNode(name), label);
60 }
61 }
62
63 void Relationship(std::string_view from, std::string_view type,
64 std::string_view to) {
65 if (!status_.ok()) return;
66 const std::optional<smollergraph::NodeId> start = graph_.Find(from);
67 const std::optional<smollergraph::NodeId> end = graph_.Find(to);
68 if (!start || !end) {
69 status_ = absl::NotFoundError(
70 absl::StrCat("nothing is called ", start ? to : from));
71 return;
72 }
73 graph_.AddRelationship(*start, type, *end);
74 }
75
76 const absl::Status& status() const { return status_; }
77
78 private:
79 smollergraph::Graph& graph_;
80 absl::Status status_;
81};
82
83// The rows' first column, as strings.
84std::set<std::string> Names(const smollergraph::Result& result) {
85 std::set<std::string> names;
86 for (const std::vector<smollergraph::Cell>& row : result.rows) {
87 const std::optional<smollergraph::Value>& name = row[0].value;
88 if (name) names.insert(smollergraph::ValueText(*name));
89 }
90 return names;
91}
92
93// A cell's value as a string, or nothing for Cypher's null.
94std::string Text(const smollergraph::Cell& cell) {
95 return cell.value ? smollergraph::ValueText(*cell.value) : "";
96}
97
98// The names of the nodes with the label that a new player reaches: a
99// shortest path from them to each, along the ways a player gets about, and a
100// row for each node that has one.
101absl::StatusOr<std::set<std::string>> Reached(const smollergraph::Graph& graph,
102 std::string_view label) {
103 absl::StatusOr<smollergraph::Result> reached = smollergraph::Execute(
104 graph,
105 absl::StrCat("MATCH p = shortestPath((s {name: '", kNewPlayer,
106 "'})-[:", kWays, "*]->(x:", label, ")) RETURN x.name"));
107 if (!reached.ok()) return reached.status();
108 return Names(*reached);
109}
110
111// A recipe needs all it takes, where everything else needs a way in. So a
112// new player crafts each recipe once they reach every item it takes, and what
113// that makes may go into another, so it goes round, until a round crafts
114// nothing new.
115absl::Status Craft(const smollergraph::Graph& graph, Builder& add) {
116 absl::StatusOr<smollergraph::Result> inputs = smollergraph::Execute(
117 graph, "MATCH (i:Item)-[:INTO]->(r:Recipe) RETURN r.name, i.name");
118 if (!inputs.ok()) return inputs.status();
119 // What each recipe a new player hasn't crafted yet takes.
120 std::map<std::string, std::set<std::string>> takes;
121 for (const std::vector<smollergraph::Cell>& row : inputs->rows) {
122 takes[Text(row[0])].insert(Text(row[1]));
123 }
124 while (true) {
125 absl::StatusOr<std::set<std::string>> items = Reached(graph, "Item");
126 if (!items.ok()) return items.status();
127 std::vector<std::string> crafted;
128 for (const auto& [recipe, needs] : takes) {
129 if (std::ranges::includes(*items, needs)) crafted.push_back(recipe);
130 }
131 if (crafted.empty()) break;
132 for (const std::string& recipe : crafted) {
133 add.Relationship(kNewPlayer, "CRAFTS", recipe);
134 takes.erase(recipe);
135 }
136 }
137 return add.status();
138}
139
140} // namespace
141
142absl::Status AddContentGraph(const Content& content,
143 smollergraph::Graph& graph) {
144 Builder add(graph);
145 add.Node(kNewPlayer, "Player");
146 for (const Land& land : kLands) add.Node(land.name, "Land");
147 for (const auto& [id, kind] : content.monsters.kinds) {
148 add.Node(kind.name(), "Monster");
149 }
150 for (const auto& [id, kind] : content.npcs.kinds) {
151 add.Node(kind.name(), "Npc");
152 }
153 for (const auto& [id, quest] : content.quests) {
154 add.Node(quest.name(), "Quest");
155 }
156 for (const auto& [id, item] : content.items.by_id) {
157 add.Node(item.name(), "Item");
158 }
159 for (const auto& [id, skill] : content.skills.by_id) {
160 add.Node(skill.name(), "Skill");
161 }
162 for (const auto& [id, recipe] : content.recipes) {
163 add.Node(recipe.name(), "Recipe");
164 }
165 for (const auto& [id, faction] : content.factions) {
166 add.Node(faction.name(), "Faction");
167 }
168 if (!add.status().ok()) return add.status();
169
170 // A new player starts in the first land, with the kit and the starting
171 // skills, the swing on Space among them.
172 add.Relationship(kNewPlayer, "STARTS_IN", kLands[0].name);
173 for (const items::Stack& stack : content.items.starting) {
174 add.Relationship(kNewPlayer, "STARTS_WITH",
175 content.items.by_id.at(stack.item()).name());
176 }
177 for (const uint32_t skill : content.skills.starting) {
178 add.Relationship(kNewPlayer, "KNOWS",
179 content.skills.by_id.at(skill).name());
180 }
181 for (const Gate& gate : kGates) {
182 add.Relationship(kLands[gate.land].name, "GATE", kLands[gate.to].name);
183 }
184
185 // Who's in each land: the monsters that spawn there, alone or in camps,
186 // and the NPCs that stand there.
187 for (const monsters::Spawn& spawn : content.monsters.spawns) {
188 add.Relationship(spawn.land(), "SPAWNS",
189 content.monsters.kinds.at(spawn.kind()).name());
190 }
191 for (const monsters::Camps& camps : content.monsters.camps) {
192 add.Relationship(camps.land(), "SPAWNS",
193 content.monsters.kinds.at(camps.kind()).name());
194 }
195 for (const npcs::Spawn& spawn : content.npcs.spawns) {
196 add.Relationship(spawn.land(), "STANDS",
197 content.npcs.kinds.at(spawn.kind()).name());
198 }
199
200 // What each monster drops, and the ones a boss calls in as it goes.
201 for (const auto& [id, kind] : content.monsters.kinds) {
202 for (const monsters::Drop& drop : kind.loot()) {
203 add.Relationship(kind.name(), "DROPS",
204 content.items.by_id.at(drop.item()).name());
205 }
206 for (const monsters::Phase& phase : kind.phases()) {
207 for (const monsters::Add& more : phase.adds()) {
208 add.Relationship(kind.name(), "SUMMONS",
209 content.monsters.kinds.at(more.kind()).name());
210 }
211 }
212 }
213
214 // What each NPC teaches and gives, the faction each herald swears players
215 // to, what each quest pays and asks for, and the swing each skill goes on
216 // into.
217 for (const auto& [id, kind] : content.npcs.kinds) {
218 for (const uint32_t skill : kind.teaches()) {
219 add.Relationship(kind.name(), "TEACHES",
220 content.skills.by_id.at(skill).name());
221 }
222 }
223 for (const auto& [id, faction] : content.factions) {
224 add.Relationship(content.npcs.kinds.at(faction.herald()).name(), "ENLISTS",
225 faction.name());
226 }
227 for (const auto& [id, quest] : content.quests) {
228 add.Relationship(content.npcs.kinds.at(quest.giver()).name(), "GIVES",
229 quest.name());
230 for (const quests::Prize& prize : quest.prizes()) {
231 add.Relationship(quest.name(), "REWARDS",
232 content.items.by_id.at(prize.item()).name());
233 }
234 add.Relationship(quest.name(), "ASKS_FOR",
235 content.monsters.kinds.at(quest.kill().monster()).name());
236 }
237 for (const auto& [id, skill] : content.skills.by_id) {
238 if (skill.next() != 0) {
239 add.Relationship(skill.name(), "NEXT",
240 content.skills.by_id.at(skill.next()).name());
241 }
242 }
243
244 // What goes into each recipe, and what it makes.
245 for (const auto& [id, recipe] : content.recipes) {
246 for (const recipes::ItemCount& input : recipe.inputs()) {
247 add.Relationship(content.items.by_id.at(input.item()).name(), "INTO",
248 recipe.name());
249 }
250 add.Relationship(recipe.name(), "MAKES",
251 content.items.by_id.at(recipe.output().item()).name());
252 }
253 if (!add.status().ok()) return add.status();
254 return Craft(graph, add);
255}
256
257absl::StatusOr<std::vector<std::string>> Unreachable(
258 const smollergraph::Graph& graph) {
259 std::vector<std::string> lines;
260 std::set<std::string> monsters;
261 for (const Kind& kind : kKinds) {
262 absl::StatusOr<smollergraph::Result> all = smollergraph::Execute(
263 graph, absl::StrCat("MATCH (x:", kind.label, ") RETURN x.name"));
264 if (!all.ok()) return all.status();
265 absl::StatusOr<std::set<std::string>> got = Reached(graph, kind.label);
266 if (!got.ok()) return got.status();
267 if (kind.label == "Monster") monsters = *got;
268 for (const std::string& name : Names(*all)) {
269 if (!got->contains(name)) {
270 lines.push_back(absl::StrCat(name, ", ", kind.called));
271 }
272 }
273 }
274 // A quest is finished by killing its monster, so a monster out of reach
275 // leaves its quests unfinished, wherever their givers stand.
276 absl::StatusOr<smollergraph::Result> asks = smollergraph::Execute(
277 graph, "MATCH (q:Quest)-[:ASKS_FOR]->(m) RETURN q.name, m.name");
278 if (!asks.ok()) return asks.status();
279 for (const std::vector<smollergraph::Cell>& row : asks->rows) {
280 const std::optional<smollergraph::Value>& quest = row[0].value;
281 const std::optional<smollergraph::Value>& asked = row[1].value;
282 if (!quest || !asked) continue;
283 const std::string monster = smollergraph::ValueText(*asked);
284 if (!monsters.contains(monster)) {
285 lines.push_back(absl::StrCat(smollergraph::ValueText(*quest),
286 " asks for the ", monster,
287 ", which no new player can reach"));
288 }
289 }
290 return lines;
291}
292
293absl::StatusOr<std::vector<std::string>> Cycles(
294 const smollergraph::Graph& graph) {
295 // What each recipe leads to: a shortest path from it to each item it makes,
296 // what that goes into makes, and so on. smollerGraph's Cypher can't give a
297 // path's two ends one variable, and the shortest path from a node to itself
298 // is no path at all, so the way back is a path from the recipe to what went
299 // into it.
300 absl::StatusOr<smollergraph::Result> leads = smollergraph::Execute(
301 graph,
302 "MATCH p = shortestPath((r:Recipe)-[:MAKES|INTO*]->(i:Item)) "
303 "RETURN r.name, i.name, p");
304 if (!leads.ok()) return leads.status();
305 std::map<std::pair<std::string, std::string>, std::string> ways;
306 for (const std::vector<smollergraph::Cell>& row : leads->rows) {
307 ways[{Text(row[0]), Text(row[1])}] = Text(row[2]);
308 }
309 absl::StatusOr<smollergraph::Result> inputs = smollergraph::Execute(
310 graph, "MATCH (i:Item)-[:INTO]->(r:Recipe) RETURN i.name, r.name");
311 if (!inputs.ok()) return inputs.status();
312 std::vector<std::string> lines;
313 for (const std::vector<smollergraph::Cell>& row : inputs->rows) {
314 const std::string item = Text(row[0]);
315 const auto back = ways.find({Text(row[1]), item});
316 if (back != ways.end()) {
317 lines.push_back(absl::StrCat(item, " goes back into itself: (", item,
318 ")-[:INTO]->", back->second));
319 }
320 }
321 return lines;
322}
323
324} // namespace nanommo
Definition chat.cc:9
constexpr std::array< Gate, 4 > kGates
Definition lands.h:45
absl::StatusOr< std::vector< std::string > > Cycles(const smollergraph::Graph &graph)
Definition graph.cc:293
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
absl::Status AddContentGraph(const Content &content, smollergraph::Graph &graph)
Definition graph.cc:142
absl::StatusOr< std::vector< std::string > > Unreachable(const smollergraph::Graph &graph)
Definition graph.cc:257
constexpr char kNewPlayer[]
Definition graph.h:16
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
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
NpcTable npcs
Definition content.h:29
A ring of stones in a land, which takes players to another.
Definition lands.h:38
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
std::vector< monsters::Spawn > spawns
Definition monsters.h:19
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18
std::vector< monsters::Camps > camps
Definition monsters.h:20
std::vector< npcs::Spawn > spawns
Definition npcs.h:22
std::map< uint32_t, npcs::Kind > kinds
Definition npcs.h:21
std::set< uint32_t > starting
Definition skills.h:18
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17