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"
27constexpr std::string_view kWays =
28 "STARTS_IN|STARTS_WITH|KNOWS|GATE|SPAWNS|STANDS|DROPS|SUMMONS|GIVES|"
29 "REWARDS|TEACHES|NEXT|CRAFTS|MAKES|ENLISTS";
33 std::string_view label;
34 std::string_view called;
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"},
46 explicit Builder(smollergraph::Graph& graph) : graph_(graph) {}
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));
59 graph_.SetLabel(graph_.AddNode(name), label);
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);
69 status_ = absl::NotFoundError(
70 absl::StrCat(
"nothing is called ", start ? to : from));
73 graph_.AddRelationship(*start, type, *end);
76 const absl::Status& status()
const {
return status_; }
79 smollergraph::Graph& graph_;
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));
94std::string Text(
const smollergraph::Cell& cell) {
95 return cell.value ? smollergraph::ValueText(*cell.value) :
"";
101absl::StatusOr<std::set<std::string>> Reached(
const smollergraph::Graph& graph,
102 std::string_view label) {
103 absl::StatusOr<smollergraph::Result> reached = smollergraph::Execute(
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);
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();
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]));
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);
131 if (crafted.empty())
break;
132 for (
const std::string& recipe : crafted) {
133 add.Relationship(
kNewPlayer,
"CRAFTS", recipe);
143 smollergraph::Graph& graph) {
146 for (
const Land& land :
kLands) add.Node(land.name,
"Land");
148 add.Node(kind.name(),
"Monster");
150 for (
const auto& [
id, kind] : content.
npcs.
kinds) {
151 add.Node(kind.name(),
"Npc");
153 for (
const auto& [
id, quest] : content.
quests) {
154 add.Node(quest.name(),
"Quest");
156 for (
const auto& [
id, item] : content.
items.
by_id) {
157 add.Node(item.name(),
"Item");
159 for (
const auto& [
id, skill] : content.
skills.
by_id) {
160 add.Node(skill.name(),
"Skill");
162 for (
const auto& [
id, recipe] : content.
recipes) {
163 add.Node(recipe.name(),
"Recipe");
165 for (
const auto& [
id, faction] : content.
factions) {
166 add.Node(faction.name(),
"Faction");
168 if (!add.status().ok())
return add.status();
182 add.Relationship(
kLands[gate.land].name,
"GATE",
kLands[gate.to].name);
188 add.Relationship(spawn.land(),
"SPAWNS",
191 for (
const monsters::Camps& camps : content.
monsters.
camps) {
192 add.Relationship(camps.land(),
"SPAWNS",
195 for (
const npcs::Spawn& spawn : content.
npcs.
spawns) {
196 add.Relationship(spawn.land(),
"STANDS",
197 content.
npcs.
kinds.at(spawn.kind()).name());
202 for (
const monsters::Drop& drop : kind.loot()) {
203 add.Relationship(kind.name(),
"DROPS",
206 for (
const monsters::Phase& phase : kind.phases()) {
207 for (
const monsters::Add& more : phase.adds()) {
208 add.Relationship(kind.name(),
"SUMMONS",
217 for (
const auto& [
id, kind] : content.
npcs.
kinds) {
218 for (
const uint32_t skill : kind.teaches()) {
219 add.Relationship(kind.name(),
"TEACHES",
223 for (
const auto& [
id, faction] : content.
factions) {
224 add.Relationship(content.
npcs.
kinds.at(faction.herald()).name(),
"ENLISTS",
227 for (
const auto& [
id, quest] : content.
quests) {
228 add.Relationship(content.
npcs.
kinds.at(quest.giver()).name(),
"GIVES",
230 for (
const quests::Prize& prize : quest.prizes()) {
231 add.Relationship(quest.name(),
"REWARDS",
234 add.Relationship(quest.name(),
"ASKS_FOR",
237 for (
const auto& [
id, skill] : content.
skills.
by_id) {
238 if (skill.next() != 0) {
239 add.Relationship(skill.name(),
"NEXT",
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",
250 add.Relationship(recipe.name(),
"MAKES",
251 content.
items.
by_id.at(recipe.output().item()).name());
253 if (!add.status().ok())
return add.status();
254 return Craft(graph, add);
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));
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"));
293absl::StatusOr<std::vector<std::string>>
Cycles(
294 const smollergraph::Graph& graph) {
300 absl::StatusOr<smollergraph::Result> leads = smollergraph::Execute(
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]);
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));
constexpr std::array< Gate, 4 > kGates
absl::StatusOr< std::vector< std::string > > Cycles(const smollergraph::Graph &graph)
constexpr std::array< Land, 3 > kLands
absl::Status AddContentGraph(const Content &content, smollergraph::Graph &graph)
absl::StatusOr< std::vector< std::string > > Unreachable(const smollergraph::Graph &graph)
constexpr char kNewPlayer[]
std::map< uint32_t, factions::Faction > factions
By id.
std::map< uint32_t, quests::Quest > quests
By id.
std::map< uint32_t, recipes::Recipe > recipes
By id.
A ring of stones in a land, which takes players to another.
std::vector< items::Stack > starting
What each new player starts with.
std::map< uint32_t, items::Item > by_id
std::vector< monsters::Spawn > spawns
std::map< uint32_t, monsters::Kind > kinds
std::vector< monsters::Camps > camps
std::vector< npcs::Spawn > spawns
std::map< uint32_t, npcs::Kind > kinds
std::set< uint32_t > starting
std::map< uint32_t, skills::Skill > by_id