nanoMMO

MMORPG quest system in C++, with NPCs that give quests

Quests in the content, given by NPCs that are people now, which ask you to kill a number of monsters, counted on the server, turned in for experience and prizes, and saved.

Watch on YouTube

We add quests to our MMORPG: a quest is data, in a protocol buffer file, with the NPC who gives it, how many of which monster it asks you to kill, and the experience and items it gives. The server decides it all: taking a quest from its giver, counting the kills for it, and turning it in for its reward, with the prizes going into a copy of the bag first, so that it's all of them or none. The client gets its quests over TCP, like its bag, with a quest window, a tracker in the corner, and a gold mark over each NPC with a quest for you. The quests are saved as JSON in the player's row in MySQL, through Redis, and the world fuzzer takes and turns them in too. Before that, the NPCs become low poly people, like the players, and we fix what CI found in the last episode's code, with clang-format and clang-tidy. This is episode 45 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: Gameplay

The code

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

  1. 3:16ep045/step-1What CI foundBrowseChanges
  2. 4:42ep045/step-2NPCs as peopleBrowseChanges
  3. 12:06ep045/step-3Quests in the contentBrowseChanges
  4. 19:05ep045/step-4Taking questsBrowseChanges
  5. 27:42ep045/step-5A quest windowBrowseChanges
  6. 34:02ep045/step-6Turned in for a rewardBrowseChanges
  7. 42:03ep045/step-7Saved with the playerBrowseChanges
  8. 46:41ep045/step-8A mark over the giverBrowseChanges
  9. 49:55ep045/step-9Fuzzing the questsBrowseChanges

Transcript

Intro

0:00 Hello and welcome back to episode forty-five, where we're going to give our NPCs bodies, like the players have, and add quests, which ask you to kill a number of monsters for a reward. Let's begin with a quick look at where we're going to be by the end of the episode.

0:15 The NPCs are people now, and the guard has a quest for arthur, with a mark over its name. Its window says what it asks for, three skeletons, and what it gives. arthur takes it, and the corner counts the skeletons from here on.

0:31 On the website of the series you can find more information about the project. The code for the project is open source on GitHub, tagged at each step of each episode so that you can follow along.

Last time

0:43 Now, before we get to today's work, let's quickly go over what we did last time. We looked at the figures a lab branch drew, and kept the people. A person is parts in the content, limbs with round ends and low poly balls, with a skin and hair that each player's id picks. Each part moves with a bone of Mixamo's bots, so their clips still pose it. The outline has a hull of its own, so it stays in one piece. The server sends what each player wears, and an item's look has parts of its own, so the clothes are items. And each figure has a shadow under it.

Today

1:19 First, CI failed on the last episode's push, on two of its checks, so we're going to fix that. Then our NPCs, which are still balls, become people, like the players, in clothes of their own. A quest is going to be content, like our items and monsters, with the NPC who gives it, how many of which monster it asks for, and its reward. The server is going to decide it all, as usual: taking a quest from its giver, and counting the kills for it. The client gets a window to take a quest in, and a corner that tracks your kills. Then you turn it in, for experience and items, and the quests are saved with the rest of the player. And an NPC with a quest for you gets a mark over its head, so that you can find it. All right, let's get started, with what CI found.

What CI found

2:06 Before the NPCs, there's something we missed: our private repository's CI hasn't run for a few episodes, as GitHub stopped starting its jobs once the account's Actions spending limit was reached, and private repositories pay for their minutes. So the first CI to check the last episode's code was the public repository's, which runs for free, once the episode was released, and two of its checks failed. The style check found two statements that clang-format lays out differently, and clang-tidy's analyser says a value in the client's figures is used before it's set. Both are stages of our Dockerfile, so we can run them here, as CI does.

2:42 At the end of the last episode, we build the lint stage, which runs clang-format and cpplint, and grep keeps its errors. Sed takes Docker's step numbers off, and cut keeps the file and the line, and sort lists each once. It's a line in characters dot h, and two in characters dot c c, which clang-format would lay out another way. Then the tidy stage, sped up here, which builds what clang-tidy needs, and runs it over our code. It stops on the client's figures dot c c, where it says the first argument of a call is an uninitialised value.

3:16 Both style errors are layout, so clang-format fixes them, and we keep what it writes. In characters dot h, the constructor's last argument fits on the line before. In characters dot c c, a garment's three fields go on one line, under the brace. Neither changes what the code does, so the build and the tests were fine, and the style check was the one to see them. In figures dot c c, add limb finds two ways across the limb's axis, square to it and to each other, which the lambda that makes each ring's corners uses. Those two came from a structured binding, auto with u and v in square brackets, which names the pair's two halves. A lambda can capture a structured binding since C plus plus twenty, but clang-tidy's analyser loses its value inside the lambda, and takes u for unset. So the pair gets a name of its own, across. Above it, we write why: named, not bound, as clang-tidy's analyser loses a structured binding's value in a lambda that captures it. And u and v are copies of its first and second halves, which the analyser can follow.

4:24 We build the lint stage again, at this step, and grep counts no errors. And the tidy stage, sped up again, counts none either, so both of CI's checks are going to pass. From now on, we're going to run both before a release, until the private CI runs again.

NPCs as people

4:42 Our NPCs are balls, and their kind's look in the content is a ball's, a colour and a radius. Now that the players are people, made of parts on Mixamo's bones, an NPC can be one too, so in n p c s dot proto, the look becomes a person's. The radius goes, and its number and its name are reserved, so that no field we add later can take them, as a client built before today would read that field as a radius. The colour stays, and what it's for now goes over it: what the NPC wears that's dyed comes in this colour, the way a player's shirt comes in their colour. An NPC gets a skin and a hair colour, as hex colours like the ones in the figures file. A player's come from their id, but an NPC is a character we design, so its kind says them. Its hair is one of the figures' ways to wear it, by its place in their list, from zero. And it holds a weapon, and wears a list of items, by their ids in the items file, so its clothes are the items the players wear. In n p c s dot text p b, each kind gets its look. The file would take any colour for a skin, but we pick each NPC's skin and hair colour from the figures' lists, so that they look like they belong with the players. Each kind takes several lines now, a field to a line, as there are six where the ball had two. A comment at the top lists what they pick from. That's the figures' six ways to wear the hair by their places, from short to bald, and the items an NPC wears by their ids, from the sword to the leather boots. Then the kinds, in the order of their ids, each keeping the colour it had as a ball, which its dyed clothes take on now. The guard has a light skin, black hair, short, with a beard, a sword, and the leather cap, the tunic, trousers and boots. The merchant wears the linen shirt, in the merchant's gold, with long brown hair. The healer's shirt is green, with fair hair tied in a tail. The Ash Warden has the Ash Blade and the buckler, a tunic and boots, and is bald, with a grey beard. The storyteller's shirt is purple, with long grey hair, and the trainer has a grey shirt, boots and a sword. In n p c s dot c c, parse n p c s gets abseil's status and str cat, and refuses a kind with no skin. Zero is what protobuf gives a field we leave out, so a kind we forgot to give a skin would be drawn in a colour nobody chose, and we'd find out by looking. What the n p c s file can't check on its own goes in content dot c c, which has the other files too. An NPC's hair has to be one of the figures' ways to wear it. Its place has to be less than the number of hairs the figures file has. Comparing goes through c m p greater equal, from utility, which compares a number without a sign with one that has one correctly, where a plain greater or equal would turn the int into an unsigned one first. The error names the NPC and its hair, and says it isn't in the figures file. Content that doesn't add up stops the server and the client as they start, so neither runs with an NPC missing its hair. Then the items it wears, and its weapon, if it has one, go into one list: what it holds, if anything, and what else it wears. Each has to be in the items file, or the error names the NPC and the item. In the content's test, the test's kind had no look before, which a kind needs now for its skin, so it gets one. Then a kind with a skin and a spawn of that kind parse, so the next one is refused for its spawn of a kind that isn't there, as before. Then a third case, with a name and nothing else. A guard with no look is refused for having no skin. And the game's own content, read again for each case, is refused with an item that isn't there on the guard, or with a hair that isn't in the figures file. The guard's hair is set to the tenth way to wear it, of six.

8:34 On the client, the animator turns what the server sends into figures to draw, and an NPC's figure carries its kind's look. In animator dot h, we take optional, and the figures' header, for the features we wrote last time: a skin, a hair colour, and a way to wear the hair. The colour is a monster's tint, as before, or the colour of what an NPC wears that's dyed. The radius goes, and the figure gets features of its own, as a standard optional, which holds a value or none. An NPC's come from its kind, and a player's stay empty, as their id picks theirs when they're drawn. In animator dot c c, stand makes an NPC's figure from what the server sends and its kind in the content. A kind this client doesn't know stays nameless, and undressed, where it was too small to see before. We keep a reference to the kind's look, and the figure's features are the kind's skin, hair colour and hair, with its weapon, and the items it wears, which the characters already draw on a player. In characters dot c c, draw picks which of the two bots a figure is drawn on from its id, and an NPC goes by its kind instead. An NPC's id is its place among its land's spawns, so moving a spawn in the file, or one skipped on ground that isn't walkable, could put the guard on the other bot. Going by its kind, a kind looks alike wherever it stands. Its skin and hair come from the figure's features when it has them, and from the player's id otherwise. In renderer dot h, the ball is still a skill's glow, so it stays, as the glow's mesh. In renderer dot c c, each figure's shadow is a player's size now, and draw figure loses the ball, which is most of the change in the renderer. So an NPC is drawn by the characters, like a player, in its kind's colour, for what it wears that's dyed. In nameplates dot c c, an NPC's name goes over its head at a player's height. And picking, which finds the NPC under the mouse when we click, can't test a ray against a ball any more. So it tests against a box around a person standing there, as wide as their arms hang, eighty centimetres, and as tall as them, one point eight metres. raylib's get ray collision box tells us whether the ray hits the box, and how far along it, so the nearest NPC still wins. In the animator's test, the test's world, as the server would send it, has one NPC. That's all the server sends of an NPC, its id, its place and its kind. The content has its kind, the guard, parsed from text as the game's file would be. Then the animator updates once, and makes a single figure, an NPC's, with the guard's name, and features of its own, as an optional with a value. We check them against the kind's look: its skin, hair colour and hair, its sword and the two items it wears. And the picking test's NPCs have no radius to set any more.

11:27 Docker builds the tests at this step, sped up here, and runs the content's, and the client's animator and picking tests on a virtual screen, as the container has none. The content refuses the NPCs that don't add up, and reads the game's own, and the animator dresses an NPC as its kind.

11:44 In the game, at this step, the NPCs around the gate are people now, each in its kind's skin, hair and clothes, like the guard with its sword and cap, and the merchant in gold. On the other side of the gate, the storyteller's shirt is purple, and the healer's is green. And they stand as the players do, in the idle clip, with a shadow under each.

Quests in the content

12:06 A quest is content, like our items, monsters and NPCs, so that a new one is an entry in a file and no code. Quests dot proto gets a package of its own. To start with, a quest asks you to kill a number of monsters, which covers a lot of what MMORPGs ask for, and keeps our options open for other kinds later. So what it asks for is a kill, of a kind of monster, by its id in the monsters file, and a count, such as three skeletons. A prize is some of an item, by its id in the items file, and a count, such as two health potions. Then the quest, which has an id from one, like our other content, which it keeps for good, as each player's progress is going to be saved by it. The giver is a kind of NPC, by its id, so a quest goes with each NPC of that kind, in whichever land it stands, and the NPC who takes it back once it's done is that kind too. Text is what the giver says, giving it, then the kill it asks for, and the reward: experience, and a list of prizes. And the file's message is a list of quests. In quests dot text p b, the first two lines name the file's schema, like our other content files. A comment names the ids we use, as a quest is mostly ids. The guard is the first kind of NPC, the healer the third and the Ash Warden the fourth, with the monsters and the items they ask for and give. The guard's quest is bones by the gate, and the guard says that skeletons climb out of the ground around it. It asks for three skeletons, the ones whose homes are near the middle of the Meadows, where players start. Its reward is a hundred and fifty experience, and two health potions. In the Ashlands, the Ash Warden says the lava keeps something burning in the bones out there, and asks for five Ash Skeletons, for four hundred, and the Ash Buckler. And the healer, in the Meadows and the Ashlands, says whoever was buried in the Crypt deserves to rest. So it asks for four of the Crypt's skeletons, for six hundred, and three potions, for the way back. A quest's text is a single line in the file, which the window is going to wrap. Quests dot h takes c std int, map and string view, abseil's status or, and the header protoc writes from our proto. It declares parse quests, which reads the file into a map of quests by id, or says what's wrong with it, in the words of our other content. The items and the skills come in a table of their own, with more than a map in it, such as the items a new player starts with. The quests need nothing more than the map, for now, so that's what parse quests returns, in a status or. In quests dot c c, we take abseil's str cat for the errors, and our checks and text headers. Parse text reads protobuf's text format into the file's message, or says on which line it went wrong, as for the other files. Then by id, from checks dot h, makes a map from each quest's id to the quest, and refuses a quest with no id, or one with another's. Its errors come back to the caller as they are. Then a loop over the quests, where, as with the NPCs' skin, a zero is a field we left out, so each check catches a quest nobody could take, or finish. Each quest needs a giver, as no NPC could give it otherwise. It needs a kill with a count, and the loop over its prizes goes through each, as each prize needs a count, and each error names the quest, and what it's missing. Each check returns the first error it finds, as the other files' parsers do, so a file with two mistakes shows one, and then the other once the first is fixed. A quest with no kills to ask for would be done before it starts, and a prize of none would be a prize of nothing. In content dot h, the content gets the quests, as a map by id, and check content's comment adds what it checks of a quest. In content dot c c, read content reads quests dot text p b after the NPCs. And check content checks what the quests file can't on its own, once all the files are read. The giver has to be a kind in the NPCs file, or the error names the quest, and the giver it can't find there. The monster has to be a kind in the monsters file, or the error names the quest and the monster, as a kill of a kind that isn't there couldn't be counted. And each prize has to be one of our items. The first one that isn't is the error, as with the other checks in check content, and read content puts the folder's name in front of it. Further down, client hash goes over the content in the order of its files, and the quests go after the NPCs. Client hash, the fingerprint that tells a client whether a world has its content, takes each quest whole, as the clients are going to show all of it. And in the content's CMake file, the library gets the proto and its parser, and the generated code is compiled without warnings, like the other files'.

17:00 In the content's test, the test that reads the game's own content checks its quests too. The first quest is the guard's, so its giver is the kind called Guard in the NPCs file, and it asks for skeletons, the first kind of monster, some number of them. It checks that the count is above zero without pinning it, so that changing it in the file doesn't break the test. The giver's name comes from the NPCs file, by the quest's giver. Next, the test of what goes into the hash. The quest window is going to show all of a quest, so a quest's experience changes the hash, as the clients have to agree with the world on it. Then a test of the quests that don't add up. Each quest is written in the text format, in a raw string, as in our other tests. The first case is a whole quest, which parses, as it has all it needs, so each of the next three is refused for the reason it names, and not for something we forgot to give it. With no giver, it's the giver that's missing. With a kill of no count, it asks for no kills, so anyone could turn it in straight away, without a fight. And a prize without a count gives none of its item. Those three are refused as the file is parsed, as the quests file can tell them on its own. What it can't tell needs the other files, so the next cases start from the game's own content, which passes, and break a single part of it each. The game's own content, read again for each case, is refused once its first quest has a giver that isn't one of our kinds of NPC, as nobody could take it from anyone. It's refused with a monster that isn't one of our kinds, which no kill could count for. And last, the test breaks the first prize's item. It's refused with a prize that isn't one of our items, which couldn't go into anyone's bag. Each error names the quest, and what it can't find. Read content puts the folder's name in front, and the server or the client stops with it.

18:55 We build the content's tests at this step, and run them. The quests that don't add up are refused, and the game's quests are read and checked, along with the rest of its content.

Taking quests

19:05 As with everything else in the game, the server decides what happens to a quest, and the client asks for it. In our protocol, a quest message names an NPC, by its id in the world, and a quest, by its id in the quests file, the way learning a skill names a trainer and a skill. The input gets take, a quest to take, which the client sends once, like a skill. Then the quests a player is on, as a map from each quest's id to how many they've killed for it. A revision goes up with each change, like a bag's, so telling whether a player's quests changed takes a number. And the world message gets your quests, which come on their own, like your bag. In world dot h, the player gets their quests, a standard map from each quest's id to their kills, which keeps them in order of id. Their quests have a revision too, which a change takes to the next, so that the server can tell when to send them. Take quest is how a player takes a quest from an NPC in this land, or an error that says why not, in words for the player, the way learn does it for a skill. And count is going to count a kill for each of the player's quests that asks for the monster's kind. In world dot c c, take quest begins with three look-ups, like learn. It finds the player, the NPC and the quest, each by its id, the NPC among this land's, and the quest in the content. Any of them missing is a quest nobody here gives. The NPC's kind has to be the quest's giver, so a client can't ask the merchant for the guard's quest, and the error doesn't say which part was wrong. The player has to be within talking reach of the NPC, four metres, like for a trainer, and not on the quest already, or it says they are, with the quest's name. Then they're on it, with no kills yet, and its revision moves on. The world steps each player and monster on each tick, and a monster that dies does so in hurt. In hurt, where a monster dies and its killer gets its experience and its loot, the kill counts for their quests too. The killing blow is what counts, like for the experience. Count looks up the monster's kind, for its name, and goes over the player's quests. A quest the content doesn't have, such as one taken out of the file since, is skipped, and so is a kind it doesn't have. A quest that asks for another kind of monster, or whose kills are all in, doesn't count it. Otherwise it's one more kill, and the revision moves, and a notice for the player says how many of how many they've killed, such as bones by the gate, two of three skeletons killed. The server sends the world's notices after each tick, as a line in the chat from nobody, like the loot.

21:42 In server dot h, take quest handles a player's ask, and send quests sends each player their quests, as they join, and as they change. The server keeps the revision of each player's quests it sent them last, like their bag's. In server dot c c, an input with a quest to take goes to take quest, and a player who leaves takes their sent revision with them. Take quest asks the player's land to take it, and sends a refusal back as a line in the chat, with its words for the player. A quest taken goes in the log, with the player's name. After each tick, after the bags, the server sends the quests that changed. Send quests goes over the players in each copy of each land. It finds the player's session, and the revision it sent them last, if it's sent any. It skips a player without a session, and one whose revision it sent last. A bag's revision is saved, but our quests' revision starts again from zero as a player joins, so a player we haven't sent any yet gets theirs anyway, on none too, which goes in a comment. The quests go in a world of their own, the ones they're on with their kills, and the revision. They change now and then, and have to arrive, so they go over TCP, framed, with the bytes counted in our metrics, like a bag. On the client, in connection dot h, the connection keeps the quests as the server last sent them, for the rest of the client to read. And in connection dot c c, a world with quests in it goes to them, next to the bag, as neither has anything of the world in it.

23:12 The server's tests have content of their own, in test content dot h, so that the game's can change without breaking them. We take c std int and map, and the quests' header. With makes a world's content from the items, skills and monsters a test hands it, and takes quests too now, as a map by id, which is empty when a test leaves it out. They're moved into the content, with the rest. Bones is the tests' quest, given by the first kind of NPC, which asks for three of the first kind of monster, the tests' skeleton. It gives a hundred experience and two of the second item, which is a potion in the tests' gear. It goes through parse quests, as the game's file does, so a mistake in it would show up here. In the world's test, two helpers in an unnamed namespace. Kill and rest kills a skeleton in front of the player, with the helper the loot's tests use, and waits a second for the swing to end, so that the next one dies to a swing of its own. A sword's swing takes two seconds, and the kill's second ends in the middle of one. So it kills a skeleton, and waits for the swing to end, so the next one is killed by a swing of its own. Quest lines keeps the quests' lines, without the loot's, which start with the monster's name, such as the skeleton dropped two bones. The world hands its notices over, so its list is empty after. Quest lines goes over the notices, and keeps each one that doesn't start that way. Then the test, whose world is made like the loot tests' world, with the gear and the looted skeleton, and this time the quest too. A guard, of the first kind, the quest's giver, stands three metres to one side of the middle, and a healer, of another kind, three metres to the other. The first player joins in the middle, within reach of both, and a second joins two metres the other way, five metres from the guard. The healer doesn't give the bones, so the first player can't take it from the healer. The second player is too far away to take it from the guard. And the guard doesn't give a quest the content doesn't have. Each refusal says why, in words for the player. The first player takes the bones from the guard, once, and is on it, with no kills yet. Then the skeletons come, one at a time, with kill and rest. The first skeleton down counts, with its line in the chat, which quest lines picks out from the loot's. Two more make three, each with a line of its own, which the test checks together, as a vector of the two lines, in the order they came. A fourth skeleton doesn't count, as three is all it asks for, and it has no line. In the server's test, the test player is a client of our own, which logs in with a token, and keeps what the server sends it. It keeps the quests too, as an optional, which is empty until they come, so that a test can tell no quests from quests not sent yet. The test player's reader takes each framed message off the connection, and parses it as a world. Chat, the bag and the quests come in worlds of their own, with nobody in them. A world with quests in it goes there, next to the bag, And the member that keeps them starts empty. The test starts a server of its own, with content made in the test, as a content struct with the gear and the bones in it. Over it, a note says the guard stands where new players start, and asks for bones. Then a kind of NPC, the guard, with a skin, as parse NPCs wants one. The guard spawns a metre from where new players start. So alice is within reach as she joins, with no walking. Create takes a free port, the TLS context, the tick, the world's name and seed, and the content, along with the metrics, the tokens, the players and the storyteller that the other tests use. alice's quests come as she joins, on none. Run until runs the test's io context until they're there, and gives up after a while. She asks the first NPC, the guard, for the bones, and they come again, with the quest on, at no kills. Taking it moves the revision on, so the server sends them again at the end of the next tick, and the test checks that she's on it, with no kills. What she can't do comes back as a line from nobody, so she asks for the quest again, and the line says she's on bones already, the way a refusal of a move in the bag comes back.

27:27 We run the two tests at this step, against MySQL and Redis in Compose, as the server test needs them. The world counts the kills for a quest from its giver, and the server sends each player their quests, as they join and as they change.

A quest window

27:42 On the client, quest window dot h has a window with the quests an NPC gives, which opens when the player clicks them, like the trainer's window. It takes c std int, optional, string, utility and vector, with our content and the protocol's messages. Main makes a single quest window, and opens it for whichever NPC is clicked. It keeps its own copy of the content, as it shows the quests, and the monsters' and items' names in them. Explicit, as before, stops the compiler from turning a content into a window on its own. Open takes the NPC's id, its name for the title, and the quests it gives, which the window keeps until it's closed. Build takes the quests you're on, as the server last sent them, and returns an optional quest message, which is empty unless a click asks for one. Main sends what it returns, along with what it sent last. Over it, we write what it draws, while it's open: what the NPC says giving each quest, what it asks for and what it gives, with a button to take it, or how far the player is with it. Its members are the content, the NPC it's open for, as an optional, which is empty while the window is closed, the NPC's name, and the quests it gives. And draw quest tracker draws the quests the player's on, in the top right corner, with how many they've killed for each. In quest window dot c c, we take ImGui, which draws the window, and the standard headers for what the file uses. Then our icon helper, which turns an icon's code point into the text that draws it, and Font Awesome's names for its icons, such as the skull. A quest's name is in gold, as an ImGui colour, with its red, green, blue and alpha from zero to one, in the file's unnamed namespace, with the helper that comes next. Monsters gives what a kill asks for in words, such as three skeletons, by the monster's kind's name. It looks the kind up by its id, and one the content doesn't have is called monster. The count goes in front, and an s at the end, which is all our names need for now. Open keeps the NPC's id, its name and its quests, moving the name and the list in, as they came in copied, and build draws nothing until there's an NPC, which is how the window stays closed. First it works out where the window goes, from the size of the display. The window starts in the middle of the screen, a little below it, and is three hundred and eighty points wide, as tall as what's in it. Its flags keep it from being resized or collapsed, as it holds a few lines, and they'd hide them. A boolean, open, is true while the window is, and its close button sets it to false, which closes the window once it's drawn. Then, for each of the NPC's quests, its name in gold, and what the NPC says, wrapped to the window. Then what it asks you to kill, with a skull, its experience, with a star, and each prize, with its item's icon, from the item's look as in the bag, and how many, after an x. If you're on the quest, it says how many you've killed of how many. Otherwise there's a button to take it, and a click makes the ask, with the NPC and the quest. Push ID gives each quest's button an ID of its own, as ImGui tells widgets apart by their labels, and each button says take. The tracker draws nothing while you're on no quests, so it isn't in the way of a player who hasn't taken one. It sits in the top right corner, under the menu bar, pinned there each frame, on a see-through background. It has no title or border, is sized to what it holds, and takes no clicks, so it doesn't get in the way of the game. A protobuf map has no order, so the quests go into a standard map first, in the order of their ids, as a protobuf map has none. A quest the content doesn't have any more is skipped. And each quest has its name, and under it, the kills so far, out of what it asks for.

31:33 The client has to know which NPCs give quests, to open the window when one's clicked. In animator dot h, a figure gets the quests an NPC gives, by id. In animator dot c c, stand takes the whole content now, as it needs the quests as well as the NPCs, and gives an NPC each quest whose giver is its kind. The figure comes from the last frame's, so its list starts empty each time. And the update hands stand the animator's content. Picking, which finds the NPC under the mouse, picks one that gives quests too, as well as one that talks or teaches. In main, we include the quest window, and make one, with a copy of the content, next to the trainer's. A click on an NPC who talks still opens the talk window. A click on an NPC who gives quests opens it, with the NPC's quests. The tracker goes after the player frame, from the quests the connection last got. And when the window asks for a quest, we send it with what we sent last, like a lesson from the trainer, so that the player keeps moving as they were. The window's source goes into the client's core library. Then the tests, starting with the animator's, which reads no files. In it, an NPC of the fourth kind comes from the server, and of the two quests, it gives the one whose giver is its kind. And the picking test picks an NPC who gives quests, though it neither talks nor teaches.

33:07 At this step, arthur logs in by the guard. A click on the guard opens its quest window, with bones by the gate, what the guard says, and what it gives. We take it, and the window says we've killed none of the three, while the corner shows the quest, at none of three skeletons. We close the window, and hold space to fight. The skeletons near the middle come for arthur, and each one that dies counts. Each kill gets a line in the chat, after the loot's when it drops any, with how many of the three we've killed, and the corner counts them too.

Turned in for a reward

34:02 A quest whose kills are all in gets turned in to its giver, for its reward, and the client asks for that the way it asks to take one. In our protocol, the input gets turn in, a quest to turn in to the NPC who gave it, as a quest message like take's, with the NPC and the quest. And the message with a player's quests, which goes over TCP like the bag, gets the ones they've done, by id, so that the client can tell them from the ones they haven't taken. In world dot h, a player gets the quests they've turned in, as a set, which they can't take again. Turn in is how they turn a quest in to the NPC who gave it, once they've killed as many as it asks for, for its experience, and its prizes in their bag. And give is going to put some of an item into a bag, as loot does, and say how many fitted. In world dot c c, a quest's experience heals a player who reaches a new level, as a kill's does. So that goes into a function of its own, gain, in an unnamed namespace, as nothing outside this file needs it. Gain works out the player's level before the experience, adds the experience, and if the level went up, gives them their most health, the way hurt did after a kill. Take quest refuses a quest the player's done already. Turn in is the bigger of the two. It starts the way take quest does, by finding the player, the NPC and the quest, each by its id, the NPC among this land's, and the NPC has to be of the quest's giver's kind. Otherwise nobody here takes that quest, so that the merchant can't take the guard's, and its error doesn't say which part was wrong. The player has to be within talking reach of the NPC too, as for taking it. We keep a reference to the quest, and call it done, as that's what it's about to be. The player has to be on the quest, with all its kills in, or the error says how many it asks for, and how many they've killed. Then the prizes, which go into a copy of the bag first. A full bag could take the first prize and not the second, and we'd have given half the reward. The copy is a few dozen stacks at most, so making it costs little next to a tick. With the copy, it's all of them or none. We go over the quest's prizes, and skip one the items file doesn't have, such as one taken out since, as the loot does. A prize that doesn't fit refuses the turn-in, and the quest waits until there's room, while the real bag hasn't changed. Each prize goes into a list of words, such as two health potions, from the loot's helper. Once they all fit, the copy becomes the bag, which moves its revision on, so the server sends it, and the player gains the experience. The quest comes off the ones they're on, and into the ones they've done, with its revision moved on. And a notice says what it gave, such as you've done bones by the gate, for a hundred and fifty experience and two health potions. Hurt gains a kill's experience through gain now, and loot gives its drops through give. In count, the line for a quest's last kill says who to go back to, which is the quest's giver, by its kind's name in the NPCs file, if the content has it, such as go back to the guard. The line goes out with the kill's other lines, after the loot's, so the chat says where to go next as soon as there's somewhere to go. And give is what loot did for each drop, moved out of loot, so that a quest's prizes come out like loot. It looks the item up, and gives none of an item the content doesn't have. What stacks goes into the bag as one stack, such as two potions, and what doesn't goes one at a time, such as two bucklers. Each is a stack of the item, with its count, with rolls of its own, such as an Ash Blade's damage, picked at random from each roll's range. A potion has no rolls, so its stack has none. Add puts each into the bag, and returns how many didn't fit, so give counts how many did, and returns that.

37:54 In server dot h, turn in handles a player's ask to turn a quest in. In server dot c c, an input with one goes to it, which asks the player's land, and sends a refusal back as a line in the chat, like take quest. It logs a quest turned in, with the player's name and the quest's id, as a line for each quest taken and turned in helps when a player says one went wrong. And send quests sends the ones they've done, with the ones they're on. On the client, in quest window dot h, the window can ask for one of two things now, to take a quest or to turn one in. Build draws a button to take a quest, how far the player is with it, or a button to turn it in once it's done. So build returns an input, with one of them in it, which is what main sends anyway, so it can merge it into what it sent last. In quest window dot c c, we take algorithm, for ranges find, and a green, for a quest whose kills are all in. Build makes an ask as it goes, which starts empty. A quest you've done says so, in grey, which ranges find tells from the list of done quests the server sent, as a repeated field is a list without a find of its own. A quest you're not on has the button to take it, which puts the quest in the ask's take. A quest you're on says how many you've killed, until they're all in, and then it has a button to turn it in, which puts the quest in the ask's turn in, for the NPC whose window it is. The tracker in the corner changes too, for a quest that's ready. In the tracker, the monsters a quest asks for go into a string first, as both of its lines use them, and a quest whose kills are all in has a tick, in green, until it's turned in. And in main, what the window asks for is merged into what we sent last, as merge from copies over the fields the ask has set, the take or the turn in, and leaves the rest as they were.

39:43 In the world's test, we include memory, for make shared, string, for the chat's lines, and utility, for move. The first test turns a quest in, once its kills are all in. Its content is the test's own, with the bones and a looted skeleton, and a kind of NPC, the guard, as the line for the last kill names the giver's kind. A world shares its content with the copies of its land, so it takes a shared pointer to a constant content, which make shared builds from ours, moved in. The guard stands three metres from the player, within talking reach. Potions is a small lambda that counts the potions in the bag's first slot, where the kit puts its three. A quest you're not on can't be turned in, so we take it, and after a kill, it still can't be, with an error that says how far you are. Then we take the notices, so that the next ones are the kills' lines. Two more kills, and the last one's line says to go back to the guard, as its kind is the quest's giver. Over the check, we note that the last line says who to go back to. Then we turn it in to the guard, and the checks after it look at what that gave. The player has the quest's hundred experience on top of what they had, and the two potions went onto the kit's three, as potions stack. The quest is off the ones they're on, and among the ones they've done, with a line that says what it gave. And a quest that's done can't be taken again. The second test is a bag with no room in it. Its world has the test's content, as before, with no kinds of NPC this time, as no line in it names the guard, which stands where it did. alice holds a sword, from with, the world test's helper for a player, and has a cap in each of the bag's slots, all twenty-four of them, added one at a time, so nothing more fits. One is the test's helper for a single item, with no rolls, and add puts it in the first free slot. She's on the quest with its three kills in, which the test sets on the player before she joins, as if she'd killed them. The turn-in is refused, and she's still on the quest, with its kills, and hasn't done it, so she can make room and come back for the reward.

41:50 We run the world's tests at this step, which don't need MySQL or Redis, and keep the last few lines. Both turn-in tests pass, along with the world's others, which loot and fight through give and gain now.

Saved with the player

42:04 A player's quests have to outlast a session, so they're saved with the rest of the player: in Redis after each tick, and in MySQL by the saver, a few seconds later. In MySQL, we could give them a table, with a row for each quest a player's on or has done, as the skills and the bag have. But a player's quests are a few numbers, read and written whole, with the player, and nothing in the game looks a player up by a quest. So the ninth migration adds a column to the players' rows, quests, as JSON, the way an item's state is JSON, which can be null for a player from before quests. Algorithm instant, as before, has MySQL refuse a change it can't make to the table's definition alone, without rebuilding it. Over it, we write what's in it, and why it's on the row. In players dot h, the progress we save of a player gets their quests and the ones they've done. Progress is compared as a whole to tell whether a player needs saving, so a kill for a quest saves them too. In server dot c c, progress of copies them from the player. In players dot c c, quests of puts what a player's on and has done into one message, the one the client gets, which Redis keeps the bytes of, and MySQL the JSON. It copies the map of quests they're on into the message's map, and the set of the done ones into its list. Load reads the quests' column with the rest of the player's row, after the bag's revision. A player who hasn't been saved in this world has no row, and the left join gives null for each of its columns, which comes to us as an empty string. So the row's values that we keep as they are, from where they are to their land, are picked out by their places. Then the bag's rows are read, with its revision, from the column after the land, and the quests come from the last column. Protobuf's JSON parser reads them into the message, leaving out fields it doesn't know, from a later server. Redis has the newest copy of a player, so the quests come from its hash too, when it has them. A hash written by a server from before quests has none, and then MySQL's stay, as the bag's do. And the player gets the quests they're on, and the ones they've done, from whichever of the two had them last. Save writes them into the player's hash, in protobuf's bytes, after each tick that changes the player. They're a few bytes, so they go each time, where the bag goes when its revision moves. In flush, which the saver runs, the quests come out of each hash, and go into the insert, as the row's last value, which a hash from before worlds doesn't have. The rest of a player's row goes in as before. Each hash's quests are its last value, parsed from protobuf's bytes into the message. The saver turns the message into JSON, with protobuf's JSON printer, and ignores its error, as a message it parsed prints. A hash with none leaves MySQL's, which is what null does. On a duplicate key, coalesce keeps the quests the row has, when the new ones are null.

45:02 In the players' test, which runs against MySQL and Redis, a player is on the second quest, with three kills, and has done the first. Their progress has nothing else in it, as the other tests cover where a player is and what they carry. Saved to Redis, and loaded again, the player has the quests they were on, with their kills, and the one they'd done. Then the saver's flush writes them to MySQL, and we read the quests column back, with a select on the player's row in this world, by the account's id. It's the JSON of the message, with the quest's id as a string, as JSON's keys are strings, in the order MySQL keeps its keys, by their length. Then we delete Redis's copy of the player, as a Redis that lost its data would, and loading the player again gives the quests back from MySQL's row.

45:55 We run the players' tests at this step, against MySQL and Redis in Compose, where Flyway runs the new migration first. The quests are kept, through Redis and MySQL, along with the rest of a player.

46:08 At this step, arthur's row in MySQL has the guard's quest, with two of its three kills, so as arthur logs in, the corner shows the quest, at two of three skeletons. We hold space, and fight the skeletons that come. The kill that makes three has a line that says to go back to the guard, and the corner has a tick. A click on the guard opens its window, with a button to turn the quest in, and turned in, the chat says what it gave: a hundred and fifty experience, and two health potions. The window says the quest is done, and the corner is empty again.

A mark over the giver

46:41 To find who has a quest for you, MMORPGs put a mark over the NPC: an exclamation mark for a quest to take, and a question mark for one to turn in. In animator dot h, a figure gets a mark, a single character, or none, An exclamation mark is for a quest to take, and a question mark for one you've done, to turn in. Which mark depends on your quests, which the animator doesn't have, so in quest window dot h, mark quest givers gives each figure its mark, from your quests and the content. A question mark wins over an exclamation mark, as a quest to turn in is the one you're on your way to. In quest window dot c c, mark quest givers goes over all the figures, players and monsters too, as their lists of quests are empty, so they get no mark. It starts each figure with no mark, so that a mark from an earlier call goes once its quest is taken or turned in. Then, for each quest the NPC gives, a quest you're on, with its kills all in, marks it with a question mark. And a quest you're not on and haven't done marks it with an exclamation mark, if it has no mark yet. In main, the figures get their marks after the animator's update, before the renderer takes them. And nameplates draw the mark over an NPC's name, in gold, like a quest's name in its window, at twice the name's size, so it's seen from further away. The mark is a string of one character, measured at its size, so that it sits in the middle, over the name, with a shadow, like the name's. Mark quest givers works on figures and quests alone, with no window or screen, so it gets a test of its own, in quest window test dot c c. We take c std int, utility, for move, vector, and GoogleTest. Giver makes the figure of an NPC who gives the quests it's handed, as the animator would. The test's content has three quests, each asking for some kills: the first two given by the first kind of NPC, and the third by the second. Parse quests checks them as the game's file is checked. Then two givers, the first with the first two quests, and the second with the third, and your quests, empty to start with. We call mark quest givers on the figures, as main does each frame, with your quests, and the test's content. With nothing taken, each giver has a quest to take, so each has an exclamation mark. Then you take the second's quest, and kill two of the five it asks for. With its kills not all in, the second giver has nothing for you yet, so it has no mark, though you're on its quest. Then you take the first quest, and kill its three skeletons. Its three kills are in, so the first giver has a question mark, which wins over its second quest's exclamation mark, as turning in is what you'd come back for. And then the first quest is turned in. Once it's turned in, the quest is done, and off the ones you're on, so the first giver's mark is the exclamation mark of its second quest, left to take. And the client's CMake file adds the new test to the client's tests.

49:36 At this step, arthur's row has the guard's quest with its three kills, so the guard has a question mark over its name, and the healer, whose quest arthur hasn't taken, has an exclamation mark. A click on the guard, and the quest is turned in, so the guard's mark goes, as it has no more quests for arthur, and the healer's stays.

Fuzzing the quests

49:56 Our world fuzzer, from episode twenty-eight, plays the world with whatever a client could send, and checks what the world has to keep true afterwards. A client can ask to take or turn in any quest, from any NPC, so the fuzzer does too. It picks from two more actions than before, eleven now. One takes a quest, from an NPC of id zero to nine, and a quest of id zero to four, past the ends of both. The other turns one in, and both ignore the errors, as a refusal is a fine answer. Then, for each player, each quest they're on has to be one the content has, with no more kills than it asks for, and not one they've done.

50:38 We build the fuzzers at this step, with the address and undefined behaviour sanitisers, sped up here, and run the world fuzzer for thirty seconds. It plays a few thousand inputs a second, and none of them breaks a check, or trips a sanitiser.

Wrap-up

50:52 So, today we fixed what CI found on the last push, two statements clang-format lays out differently, and a value clang-tidy's analyser lost. The NPCs are people now, with a skin, hair and clothes from their kind. A quest is content, with its giver, how many of which monster it asks for, and its reward. The server decides who can take a quest, and counts each kill for it, and turning it in gives its experience and its prizes, all of them or none. The quests go to the client over TCP, like the bag, and they're saved as JSON in the player's row, through Redis. An NPC with a quest for you has a mark over its name, and the world fuzzer takes and turns in quests too.

Playing it

52:14 Let me know in the comments what you'd like to see next. And if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.