nanoMMO

Checking game content as a graph for an indie MMO

Our MMO's content as a graph in smollerGraph, the graph database from the other series, and a test that fails when a new player can't reach a quest, a monster, an item or a skill, run in CI.

Watch on YouTube

Checking an indie MMO's game content as a graph, so the build fails when a new player can't reach something. Our content links a quest to the NPC who gives it, a monster to what it drops, a land to the one behind its gate, and our checks made sure each link pointed at something real. Taking the Guard out of the Meadows passed all of them, while nobody could take his quest anymore. So the content goes into smollerGraph, the graph database from the other series on this channel, fetched with CMake at a version, and a test asks it, in Cypher, for a shortest path from a new player to each land, monster, NPC, quest, item and skill. Its explorer draws the whole game, and the test runs in CI with the others. This is episode 90 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

The code

Each step on screen is a tag in the game's repository. The code at the end of the episode is ep090/end.

  1. 2:21ep090/step-1smollerGraph, fetchedBrowseChanges
  2. 3:41ep090/step-2The content as a graphBrowseChanges
  3. 11:15ep090/step-3What a new player can't reachBrowseChanges
  4. 17:57ep090/endDecision record fortyBrowseChanges

Transcript

Intro

0:00 Take the Guard out of the Meadows, and the game still starts, while nobody can take his quest anymore. From today, the tests fail on it, as they follow our content as a graph. And our new tool names what a new player can't reach, the Guard and Bones by the Gate.

0:16 Hi, and welcome to episode ninety, in which our game's content goes into the graph database from the other series on this channel. And our tests start asking it what a new player can reach.

0:27 The game is on GitHub, open source, and tagged as we go, so you can check out the code at any point of an episode. The series' website links to it, and has a page for this episode too, if you'd like to read along.

Last time

0:40 In the last episode, our calls to Redis and MySQL got deadlines. Once one runs out, that database is left alone for a while, which is failing fast. A batch of saves stays in Redis until MySQL has written it. And when Redis can't save a trade, the trade doesn't happen, which put an end to the copied sword. We broke it all again on purpose and wrote our first postmortem. Today's episode is a quieter one, about the game's content.

What the checks miss

1:07 Our content is a few files of text, and a lot of what's in them points at something in another file. The Guard is a kind of NPC, who gives Bones by the Gate, which asks for three skeletons, and pays in health potions. And he stands in the Meadows, by the gate. When the content loads, read content makes sure each of those links points at something real, and the server won't start otherwise. What it can't tell is whether a player can ever get to any of it. If the Guard stands nowhere, the links that are left are all still fine, and his quest is gone from the game, without an error anywhere.

1:44 So let's try it, as it stands at the end of last episode. Sed deletes the line that puts the Guard in the Meadows, the kind of edit someone could make while moving people around town. Git shows one deletion, a single line out of the NPCs file. And the content tests read the files, run check content, and all of them pass. I ran the rest of our tests with it too, and nothing else fails, though on my machine the ones that need Redis and MySQL are skipped. I'd expect a big studio to check its content in its build pipeline, with tools of its own. Ours is going to be built on a graph database.

smollerGraph, fetched

2:22 Whether a new player can get from where they start to each thing in the game is a question about paths, which is what graph databases are for. In the other series, we're writing one called smollerGraph, and its last episode made it a library that another project can fetch. So let's open the CMake file at the top of the project, which fetches each of our libraries near the top and checks each download against its hash. Right after Abseil and GoogleTest, FetchContent declares smollerGraph, at a version. It comes from the tarball GitHub serves for its v0.20.0 tag, like our other dependencies. The SHA-256 next to it means the build stops if those bytes ever change, so we build against the code we tested and nothing else. SYSTEM marks its headers as a system library's, so our warnings don't fire inside them. EXCLUDE_FROM_ALL leaves it out of the build until one of our targets links it. Its tests, its browser build and its docs stay out of our build, GoogleTest with them, as it isn't the top-level project, which is what that episode was about. And make available adds it after our Abseil, which it declares under the name we use, so it builds with our copy, which is newer than its own. I tried that first, on its own, and it builds with our Abseil, in our C++23.

The content as a graph

3:42 The graph code goes in a new header file in the content folder, called graph. It includes Abseil's status, for the errors, and our content header, for the content it's built from. And smollerGraph's graph header comes in by its folder name, smollergraph slash graph, as its headers moved there in its last episode. A constant holds the node each path starts from, A new player, someone who's just made a character, with the land, the kit and the skills they get. Add content graph takes the content we've read, and a smollerGraph graph to add it to. It adds a node for each land, kind of monster and NPC, quest, item and skill, each labelled with what it is, and a relationship for each way a player gets from a node to the next. A quest also asks for its monster, which is what finishing it takes, and isn't a way to get anywhere, so it's a relationship of its own kind. It returns a status, as two things with one name would be one node, and that's an error, and so is a name too long for a node's record. In the graph file next to the header, the includes are cstdint, string and string view, Abseil's status and string concatenation, and smollerGraph's graph header. The terrain's lands come in too, as the lands and their gates are in code, not in the content files. In an unnamed namespace, a small class, the builder, keeps a reference to the graph and the first thing that goes wrong. That way, the code that adds the content can call it line after line, without checking each call. The node function gives each name a node of its own, and once something's gone wrong, it does nothing more. A name longer than smollerGraph's record allows is an error, with the name in it. The find function, which looks a name up in smollerGraph's index of names, says whether the graph has it already. A name it has is an error too, two things are called that name, since adding it again would hand back the node it has, and quietly join two things into one. Otherwise it adds the node and gives it its label, which smollerGraph keeps as a token. The relationship function finds both ends by their names, which works because all the nodes go in first, and joins them with the type. And the status function hands back what went wrong, if anything, as a constant reference, so it isn't copied each time someone asks. Add content graph makes a builder on the graph, and adds the nodes first, a new player, labelled player, as each path starts there, then each land from the terrain's list, labelled land. After them come the kinds of monster, from the monsters' table, and the kinds of NPC from theirs, then the quests from the quests file, and the items and skills. A node goes in under the name players see in the game, as the content gives it. If a name clashed, it returns that error before any relationship goes in. A new player starts in the land at the head of the terrain's list, the Meadows, which is where the server puts a character it has just made. The types go in capitals, the way Cypher usually writes them, which is how our queries name them too. They start with each stack of the starting kit, by the name of its item, from the items file, which the server hands out the first time they log in. They know the starting skills, and the swing on space is among them, as reading the skills file puts it there, with the swings it goes on into. A gate in the terrain's list leads from its land to the one it takes you to. So the Meadows lead to the Ashlands and to the Crypt, and both lead back. A monster with a fixed home spawns in its land, by the land's name, which the monsters file's reader has already checked is one of ours. Camps spawn their kind in their land too, wherever the land's seed places them on its island. The NPCs stand in their land, as their spawns say, whether that's by the gate or across the road from the healer, like the merchant. In the graph, a monster drops each item in its loot, whatever the chance, as any chance at all is a way to get the item, and how many doesn't matter either. A boss's phases summon their adds, which are monsters too, like the Bone Colossus calling in skeletons and then ash skeletons. So a monster that's nowhere else but a boss's adds still counts as reachable, through the boss. An NPC that teaches, which for now means the trainer, leads to its skills. Quests come next, given by their NPC, which a quest names by the giver's kind, looked up in the NPCs' table, and rewarding their prizes, the health potions and the Ash Buckler. A quest also asks for its monster, with that type of its own, which the check keeps apart from the ways to get about. Last, a skill that goes on into another leads to it, as Strike does into Return Cut, and Return Cut into Overhead Cut, the way holding space goes through the chain. Read content has checked all of those ids against their files before we get here, so each lookup with at finds its entry, and we return the builder's status. In the content's CMake file, the graph code becomes a library of its own, nanommo content graph. It links our content library, smollerGraph's library by the name its CMake file gives it, smollergraph colon colon smollergraph, and Abseil's status, status or and strings. The world servers, the client and the bots don't link it, so a graph database stays out of the game's own programs, and their builds don't wait for it. Next to it goes a tool, nanommo graph, which links the graph library and nothing else of ours, as the graph library brings the content with it. The tool is a single main function, in a new file, graph main. It takes two folders, the content's and then the graph's, as its arguments. It includes iostream, for what it prints, Abseil's status and status or, our content and graph headers, and smollerGraph's graph header. With the wrong number of arguments, it says how to use it and returns one, and otherwise reads the content folder from its first argument. A content folder that doesn't read is an error too, with the reason, as read content gives it, the file and the line. It goes on to make a smollerGraph graph, open it in the folder from the second argument, which creates the folder the first time, and add the content to it. Either can fail, and the error goes to standard error. It prints how many nodes and relationships the graph has, and flush writes the graph's pages to its files, so smollerGraph's own program can open them and serve them.

10:06 We build the tool, and sg, smollerGraph's own program, which our build leaves out until we name it. They land in our bin folder, next to the game's programs. The tool writes our content as forty-four nodes and seventy-eight relationships, between three lands, three quests, eleven items and sixteen skills, and who's in the lands.

10:27 sg serve gives the graph its explorer, the page from the other series, and it opens on its own query, about animals, which finds nothing here. So we ask our own, and it's the one the check is going to be built on. It's a shortest path from a new player to any node, along any of the twelve ways to get about. The bars between them are a feature smollerGraph got in its episode nineteen, for attack paths. The relationship from a quest to its monster isn't one of them. The new player sits down at the bottom, holding the kit and the skills. Above them are the Meadows, the Ashlands and the Crypt, with the monsters and people in each, their quests and loot, and the trainer with his skills. Searching for the Guard opens his record, with the quest he gives, and the Meadows, where he stands.

What a new player can't reach

11:15 Now for the check, starting in the header file, where string and vector come in for the lines it answers with. Abseil's status or comes in too, for an answer that can be an error instead. Unreachable takes the graph by constant reference, since it reads the graph and changes nothing, and returns a line for each thing no path from a new player reaches, such as Leather Cap, an item. Each quest whose monster is out of reach gets a line too, as nobody can finish it. If smollerGraph refuses a query, it returns that error instead, which shouldn't happen with queries we wrote ourselves, though the caller would still get to see it. In the graph file, set, vector and the status or join the includes, along with smollerGraph's plan header, which has Execute, the call that runs a query in our Cypher. The ways to get about go in a constant, the twelve types with bars between them, as Cypher writes them, so the query reads like the one we typed. The quest's asks for stays out of the list, as it did in the explorer. Each label gets a small struct with the label, as the graph has it, and what a line of the answer calls it. That's a land, a monster, an NPC, a quest, an item and a skill, in a constant array of six. A small function called names turns a result's first column into a set, with value text, which gives a property's value as a string, so two rows with one name count once, and looking a name up is quick. Unreachable starts with an empty list of lines, and a set for the monsters a new player can reach, which it fills on the way, as the quests need it at the end. Unreachable goes through the labels, and for each, Execute runs two queries. One is for all the nodes with the label, a match on the label, which scans for it and returns each node's name, and if it fails, its error goes back. The other is the one we typed in the explorer, with the label on its end node, so a path has to end on that label. It gives a row for each node of that label a path reaches, and none for one it can't. That one can fail too, which returns its error to the caller. The monsters it reached are kept aside first, for the quests at the end. The names in the first and not in the second are out of reach, and get a line each, with what the label's called. One more query matches each quest to the monster it's after, along asks for, the relationship we kept out of the ways, as a quest is finished by killing that monster. Like the others, the query can fail, and then its error goes back to the caller. There are three rows today, one per quest. For each row, the monster is in the second column, and when it isn't among the monsters reached, the quest gets a line, with its name from the first column. That's wherever its giver stands, as nobody can finish it, and then it returns the lines. Now, let's write some quick tests, in a new file next to it, graph test. It includes the graph header first, as the style guide asks, then algorithm, string, utility and vector, Abseil's status and status or, our content header, GoogleTest and smollerGraph's graph header, and its tests go in an unnamed namespace inside ours. A helper reads the game's content, from the folder the build passes in, as NANOMMO content, like the content's other tests, and fails the test when it doesn't read, with the reason, and hands back what it read. Another helper, called problems, takes some content and builds a graph from it, in temporary files, which go away with the graph. It adds the content, and returns what Unreachable finds, a line each, or the error, as the one line. So each test compares a list of strings, and a failure shows both lists side by side. The first test is the game as it is, and a new player can reach all of it, so there's nothing on the list. The next ones break the content the way an edit could. To find a quest nobody can take, a test starts from the game's content, as they all do. Then it erases the Guard's spawns, by his name, with erase if on the list of spawns, so he stands in no land at all. Check content is still fine with that, as he's still in the NPCs file, and the test checks that first. The graph finds the Guard, an NPC, and Bones by the Gate, a quest, which nobody can take now. For a quest nobody can finish, the Crypt Skeleton loses its camps in the Crypt, as that's where it lives, and the camps are all it has, so with them gone, nothing spawns it, anywhere in the game. That's an edit someone could make while rebalancing the Crypt. This time, the test expects two lines from the graph. The graph finds the Crypt Skeleton, a monster, out of reach, along with Rest for the Crypt, the quest that asks for it, as nobody can finish that quest now. Another takes the Leather Cap out of the loot of each kind of monster, through protobuf's mutable loot, with remove if and erase, as protobuf's repeated field has no erase if. Remove if moves the drops we keep to the front, and erase cuts off the rest. The Skeleton was all that dropped it, so nothing gives it anymore, and the graph finds Leather Cap, an item. Last, an item renamed Guard, after the NPC, makes two things with one name, which add content graph refuses, with the error that two things are called Guard. I like that each of these tests reads as a small story about an edit, and the list it expects is what someone making content would have to fix. If one of them ever breaks, its name says which kind of mistake the check stopped finding, before we go looking. In the CMake file, below the graph library, the tests get a target of their own, called nanommo content graph tests, built from the test file we just wrote. It links the graph library and GoogleTest's main, with the content folder passed in as a definition, NANOMMO content, as the other content tests have it. Gtest discover tests adds them to ctest, so CI runs them with the rest. The tool now prints what a new player can't reach as well, after its counts. It asks Unreachable, prints its error if it has one, and prints each line. It flushes the graph, writing its pages to disk as before. When the list of lines isn't empty, it exits with one, which any shell takes as a failure, so a script, or CI, can tell.

The check

17:19 We run the new tests, and all five pass, in a fraction of a second. Next comes the edit from before, taking the Guard out of the Meadows, and we run the tests again. This time, the test of the game as it is fails, a new player can't reach all of it. Two of the four that break the content on purpose fail too, as they find the Guard on top of what they broke. So a mistake like this fails three tests, and CI with them. The tool says what's wrong, the Guard, an NPC, and Bones by the Gate, a quest. I wanted a check like this to name what to fix, so nobody has to dig through the content files to find it.

Decision record forty

17:57 The content page gets a section, checked as a graph, with what check content can't tell, the labels and the relationships, and how to write the graph out and serve it with smollerGraph's explorer. And it's a choice worth a decision record, number forty. The alternatives were a walk of our own over the content, in C++, which is a breadth-first search over a dozen kinds of link, to write and keep. smollerGraph has that search already, and an explorer to look at the graph with. The check could have gone in read content, as the servers start, but then the game would link a graph database it doesn't otherwise need. A test stops the mistake before it's merged, which is where it costs least. And Neo4j, in Docker, in CI, would be a server to start in each test run. The costs are a dependency on another project's releases, and our newer Abseil having to build smollerGraph too. And a new kind of link in the content needs its relationship in the graph, or what's behind it looks out of reach, and the tests say so.

What's missing

18:59 I think this is a good start, and there's more the graph could check. A path can exist and still be no use, such as a quest for new players behind a boss of level ten, and smollerGraph's cheapest paths could add up levels on the way. A drop with a chance of one in a thousand is reachable, and hardly anyone will see it, so a drop could carry its chance as a weight. The bigger reason I like this is the storyteller, the NPC who talks with our players. If it makes up quests and items of its own one day, a check like this would stop one nobody can reach. And smollerGraph's Cypher can't ask yet for what no path reaches, so the test compares the lists of names in C++.

Wrap-up

19:38 So, our content is a graph now, in smollerGraph, fetched at a version like our other libraries, and kept out of the game's own programs. A test asks it for a path from a new player to each thing in the game, and CI fails when one's missing, and its log names it. I like that two projects from this channel meet here, and that the one from the other series ended up doing a real job for this one.

Playing it

20:54 If your team checks its game content, or any other data, in CI, I'd like to hear how, and what it's caught. Tell me in the comments, along with what you'd like to see next, and subscribe if you want to follow along. I hope you find this useful, and thanks for watching.