nanoMMO

Adding factions to an MMORPG

Three factions in the content, each with a herald who swears players to it for good, checked by the world, saved once through Redis into MySQL, and shown as a diamond in its colour before each sworn player's name.

Watch on YouTube

Adding factions to an MMORPG, three sides a player swears to at a herald, once and for good. Factions are content, in a text file with a schema, each with a colour and a herald, an NPC who stands west of where new players start, and the content's checks and its graph make sure each one can be joined. The client sends the herald's id, and the server checks that it's a herald, that the player is within talking reach, and that they haven't sworn already. The faction is saved with the rest of the player through Redis, and the saver writes it into MySQL with COALESCE, only where the row has none, so nothing switches it. Each player's faction goes with their name to everyone near them, and the client draws a diamond in its colour before each sworn name, with a herald's window that asks once more before you swear. The bots swear too, a third to each faction, so a load test's crowd shows all three colours. Factions mean nothing in a fight yet, and guilds have none, so guildmates can be on different sides. This is episode 97 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 ep097/end.

  1. 3:44ep097/step-1Factions in the contentBrowseChanges
  2. 27:16ep097/step-2A herald's windowBrowseChanges
  3. 39:13ep097/step-3Bots take sidesBrowseChanges
  4. 46:31ep097/endDecision record forty-sevenBrowseChanges

Transcript

Intro

0:00 Our game has guilds, duels, guild wars and crafting by now, and today it gets three factions to swear to. Each one has a herald, about eight metres west of where new players start. The window asks once more before you swear, as it's for good, and from then on, the faction's colour goes before your name, for everyone near you to see.

0:24 Hello, and welcome back to episode ninety-seven, where we're going to give our players three factions to choose from, each at a herald, and once they've sworn to one, they keep it. I think the most interesting part is that last bit, keeping it for good, which goes all the way down to how the saver writes it into MySQL.

0:42 There's more about the game on its website, along with a link to the code on GitHub, which is open source. Today's episode gets a page there too, with its code, for reading along.

Last time

0:53 First, a quick look back at episode ninety-six, where a server's sentence started going to the client as a message, its English with numbered placeholders, and the pieces that go in them. On the servers, Msg and MsgN build them, with the English kept beside each one for the logs and the tests, and a refusal stayed a status, with the message in its payload. The client translates each line as the chat draws it, and Romanian's file has all five hundred and twelve strings. That comes in handy today, as the sentences about factions go through it from the start.

Before factions

1:25 Here's the game as last episode left it, with names over heads turned on in the settings, so they show without pointing at anyone. We're about six metres west of where new players start, on the grass where the heralds are going to stand. arthur's frame in the corner has a name, a level and health, and a guild when there's one. The bots coming in from the middle each have a name and a level, and nothing that groups them. A player belongs to a guild at most, which is a handful of friends, and the fights between players are duels and guild wars, which both sides agree to first. Today adds a side bigger than a guild, which each player picks once.

2:09 So, today, factions go into the game's content, each with a colour and a herald, who stands in the Meadows. The client asks to swear at a herald by the herald's id, and the world checks it, once and for good. The faction is saved with the rest of the player, through Redis, and goes with their name to everyone near them. The client gets a herald's window, and a diamond in the faction's colour before each sworn player's name. Last, the bots swear too, a third to each, so a load test looks like a crowd that's picked sides.

Where a player picks a side

2:40 There are a few ways to go about this, and I want to go through them before writing any code. A player could pick a faction as they make their character, next to their looks. However, a new player knows nothing about the world yet, and the login server, which makes characters, doesn't read the content. A guild could swear, with its players following it. A guild is a few friends, though, who may well want different sides, and joining or leaving one would then change your faction. Players could switch sides, which needs a rule for how often and at what cost, and I'd like to see the sides fighting before deciding what a change should cost. Or the factions could fight each other anywhere, straight away. That's open player against player in each land, and the newest players, sworn on their first day, would be the easiest targets. So a player swears at a herald, in the world, once they've seen a bit of it, and it's for good, for now. I like refusing for good, as it's a simple rule, and one we can loosen later. A faction means nothing in a fight yet, and guilds have none, so two players in one guild can be on different sides.

Factions in the content

3:44 First, in the top CMake file, the version goes up to zero point ninety-seven, as the client and the servers have new things to say to each other today. Then a new proto file for the factions, in the content folder, next to the items' and the NPCs'. It's proto three, in a package of its own. A faction is a message, with an id, from one, which it keeps for good, as each player's faction is going to be saved by that number. Then its name, and its colour, as red, green, blue and alpha in a single number, as the NPCs file writes its colours, for the mark by its players' names. Its herald is who swears players to it, by the id of a kind in the NPCs file, so a herald is an NPC like the guard or the trainer, with a look of its own. So a faction is a side to swear to, once, and for now it means nothing in a fight. And the file's message holds a list of them, called factions. Then the factions file, in protobuf's text format, whose first two lines say which schema and which message it follows, for the tools that read it. There are three factions, Concordia, Fortuna and Victoria, and their heralds are going to be kinds seven, eight and nine in the NPCs file. I went with Latin names, as they sound like sides in a war without being anybody's country. The first faction is number one, Concordia, in a sky blue, whose herald is kind seven. Fortuna, number two, is gold, with kind eight as its herald, and Victoria, number three, is crimson, with kind nine. In the NPCs file, the heralds are three new kinds, each in their faction's colour from the factions file. The herald of Concordia is kind seven, dressed in Concordia's sky blue. The herald has a light skin and dark brown hair, worn long, and a linen shirt, cloth trousers and plain shoes from the items file. The herald of Fortuna, kind eight, is in gold, with golden hair, tied back, and leather boots for shoes. The herald of Victoria, kind nine, is in crimson, with black hair, and a sword in hand, as item one is the sword. The colour is written twice now, here and in the factions file, which I don't love, as nothing checks that the two agree. An NPC's look has a colour of its own, though, and I'd leave the two files separate. Then they stand in the Meadows, in a row about eight metres west of where new players start, each turned to face it, and the client can draw them already.

6:07 Then the code that reads the file, starting with a header file in the content folder, factions. After its include guard, it brings in c std int, map and string view, then Abseil's status or, and the header protobuf generates from our new proto file. Parse factions takes the file's text, and returns the factions in a map by their ids, or what's wrong with them, as the content's other parsers do. That's a field the schema hasn't got, a faction without an id or with another's, one without a colour or a herald, or two factions sharing a herald. Herald of takes the factions and a kind of NPC, by its id, and returns the faction that kind is the herald of, or a null pointer for an NPC that's no herald. It's a pointer to a constant, into the map we pass it, so nothing gets copied. In factions, after its header come c std int, map, set, for the heralds a check is going to collect, and string view. Then there's str cat, and two of our own, checks, for a helper the content's parsers share, and text, for protobuf's text format. Parse factions reads the text into a factions message, through parse text, and returns its error if the text doesn't parse. By id puts them into a map, keyed by their ids, and refuses a faction without one, or two that share one. Then a set of the heralds it's seen. Protobuf gives a field we leave out a zero, so a zero colour is a faction nobody would see by a name, and a zero herald is one nobody could swear to. So a faction without a colour is refused, with its name in the error, and so is one without a herald. Insert returns a pair, whose second is false when the herald was in the set already, so two factions with one herald are refused too, as a herald swears players to a single faction. The error names the herald's number, so it's easy to find in the file. Herald of takes the factions and the kind it's asked about, and goes through the factions in the map, in the order of their ids. It returns the address of the one whose herald is that kind, or a null pointer when none is. With three factions, a loop is plenty, and I didn't want a second map by herald to keep in step with the first. In the content's CMake file, the proto file and the source join the content library, and the generated source joins the list of the others protobuf makes. In the content's header file, our factions header comes in, and the content gets its factions, in a map by id, first among its parts. Check content, declared further down, is going to refuse a faction whose herald isn't in the NPCs file, along with all it refused before. In content, read content starts with the factions file now, through parse factions, and then reads the figures, once the factions have read fine, and the rest as before. Check content, which checks the files against each other, goes through the factions, and refuses one whose herald isn't a kind in the NPCs file. It's an invalid argument error, which names the faction and the herald's number, and says that it isn't in the NPCs file. So a wrong number for a herald is caught as the content is checked, instead of leaving a faction that nobody can join. Then the factions go into the client hash, as the clients mark each sworn player's name in the colour this file gives, and open a herald's window on a click. That hash is how a client tells, on the login screen, which worlds have its content.

9:33 In the graph's header file, add content graph puts our content in a smollerGraph graph, as it has since episode ninety, from each land to what its monsters drop and its NPCs give and teach. Now each faction gets a node too, labelled with what it is, and the graph goes on, past the NPCs who stand in a land, to the factions their heralds swear players to. The graph's tests walk its relationships out from a new player, so a test can ask whether a new player can reach each faction, as it does with the rest of the game. In the graph, enlists joins the kinds of relationship a player follows from a node to the next, after crafts and makes, and faction joins the kinds of node, which a line about what can't be reached calls a faction. Each faction becomes a node with its name, after the recipes. Further down, where the graph adds what each NPC teaches and gives, what each quest pays and asks for, and the swing each skill goes on into, a new loop goes through the factions. It adds a relationship from each herald, by their kind's name, to their faction, called enlists. So the graph goes from a new player, to the Meadows, to the herald who stands there, and on to the faction. A faction whose herald stands nowhere is out of reach, which the check from episode ninety finds without any more code.

10:49 Now, let's write a few quick tests, starting in the content's tests, where the one that reads the game's content gets a few more lines at its end. There are three factions, each with its herald where new players start. It's an assert, so the lines after it stop there if the map is any other size. The first is Concordia, by its name, and its herald's kind in the NPCs file is called herald of Concordia, which ties the two files together. Then, for each faction, any of, from the ranges library, goes through the NPCs file's spawns, for one of the herald's kind, standing in the Meadows. When there's none, the faction's name goes into the failure, so we'd know which one it was. Herald of finds each faction by its herald, at the faction's own address in the map, and finds nothing for kind one, the guard, who's no herald. In the test of the client hash, a fresh copy of the game's content has Concordia in green, and its hash isn't the game's, as the clients draw that colour. Then a new test, that the parser refuses factions that don't add up. A small lambda makes a factions file with a single faction, number one, called Concordia, with whatever fields we give it after its name, and parses it. With a colour and a herald, it's fine, which shows that each refusal below is for the field it leaves out. Without the colour, the error says Concordia has no colour, and without the herald, that it has no herald. Then a file of two factions, Concordia and Fortuna, both with herald seven, is refused, as two factions with one herald. Last, the game's content, read from its folder, passes check content as it is, until Concordia's herald is ninety-nine, which isn't in the NPCs file, and the error says so, with the faction and the number. And in the graph's tests, next to the one that finds a quest nobody can take, there's a new one. It finds a faction that nobody can join, as its herald is nowhere. It takes the game's content, and Victoria's herald out of the Meadows, with erase if, which removes each spawn the lambda says yes to, here those of the kind named herald of Victoria. Check content still passes, as the herald's kind is still in the NPCs file, which is why the graph's check is worth having. The graph's problems are the herald, an NPC nobody can reach, and Victoria, which nothing else leads to.

13:08 Let's build it in Docker, the way CI does, and run those three content tests, which takes a few minutes with the build. Grep keeps the lines of the tests that passed, and the summary. The new one passes, along with the two that read the game's content and hash it. Then all of the graph's tests, which reuse that build, where Victoria can't be joined once its herald is gone from the Meadows. They pass, so the content holds together with its factions in it.

The heralds in the game

13:36 Here's the game now, from the spot we stood on before. The herald of Concordia is in sky blue, Fortuna's in gold, and Victoria's in crimson, each turned to face where new players come in. The client draws them as it draws any NPC, from the NPCs file, with their names over their heads, and there's no window for a herald yet. Before that, the server needs a way for a player to swear, and to keep it.

Swearing, on the server

14:06 Let's follow an oath from one end to the other, before writing it. On the client, Swear is going to be a button in the herald's window, which sends the herald's id in an input, once, the way a quest is taken. The world checks it as it checks a trainer, for an NPC in the player's land, within talking reach, who's a herald, and for a player who hasn't sworn already. Then it tells the player which faction they've joined, as a message with the faction by its id, the faction is saved with the rest of the player, and it goes to everyone near them, with their name. We'll start in the protocol, as the client and the server have to agree on it first.

14:42 In our proto file, the input gets enlist, field twenty-four, the id of the herald to swear at, which the client sends once, as it's for good. A player gets their faction, by its id in the factions file, or zero for none, which comes with their name. And an arg, a piece of a server's sentence from last episode, can be a faction, by its id. It's field eleven, as ten is the count's, outside the one of. In the protocol's messages header file, Of gets a version for a faction, next to the quest's and the skill's. In messages, English of says a faction by its name, found by its id in the content's factions, or the word something, for one it hasn't got. And Of, for a faction, sets the arg's faction to its id, with its name as its English, as a quest's does. In parts, which keeps what the client knows between the worlds the server sends, a player's faction is kept with their name, their weapon and their guild. The server sends those with the name, once a second, and leaves them out in between. A quick test goes at the end of the protocol's messages test, that a faction is named by its id. It makes empty content, and puts Concordia into it, as faction one, with its id and its name. Then it makes the sentence a player hears on joining it, the way the world makes it. Its English names Concordia, and its message has a single arg, asserted before it's read. That's faction one, by its id, and the English function, from the message and the content, says it the way Msg made it, which is what the servers' tests read. And in the test of parts that keeps names between the ticks that send them, the full world's player is in faction two, which is still there after a world without the name.

16:24 In the world's header file, a player gets their faction, by its id, or zero for none, next to their guild. Then enlist swears the player to the faction whose herald the NPC is, in this land, for good, and tells them so. Otherwise its error says why not, in words for the player, as a trainer's and a quest giver's do. In the world, our factions header comes in, for herald of. Enlist finds the player, and the NPC by the id the client sent, and when there's one, the faction its kind is the herald of. Without the player, or a faction, it's a refusal, not found, that nobody here swears players to a faction. That covers an NPC who's no herald, an NPC in another land, and a made-up id from a modified client. Then the distance, through hypot, the length of the line between the two, and past talking reach, which is four metres, the player is too far away to talk to the herald. The herald goes in by their kind, so the client names them in the player's language. Then an if with an initialiser looks the player's faction up, and when they're in one, the oath is refused, So the refusal is already exists, that they're sworn to their faction already, whichever herald they asked. The faction goes in as an arg, so the client names it in the player's language. Otherwise the player's faction is the herald's, and the world tells them they've joined it, as a message with the faction by its id. A faction's name isn't looked up anywhere here, which I prefer, as a name is for people to read, and an id is what gets saved. The offline world, which runs the servers' rules inside the client, takes the input too, and calls enlist for us, so swearing works without a server, by that rule as well.

18:06 Next, a test in the world's tests that a herald swears a player to their faction, and for good. It starts from the tests' content with a sword in it, and adds two factions, parsed from text in a raw string, Concordia, with herald seven, and Fortuna, with herald eight. The world gets that content through a shared pointer to a constant, made with make shared, as the worlds share the content between them, and none of them changes it. Spawn puts an NPC into the world and returns its id, so Concordia's herald stands two and a half metres east of the middle, Fortuna's two and a half metres west, and a guard two metres south. The first player joins in the middle, within reach of all three, and the second four metres west, a metre and a half from Fortuna's herald, and six and a half from Concordia's. The guard is no herald, so the first player can't swear there, and nobody here swears players to a faction. There's no NPC with id ninety-nine, which gets that refusal too. The second player, six and a half metres from Concordia's herald, is too far away to talk to them, with the herald named in the sentence. Neither player is in a faction after any of that, as a refusal changes nothing. Within reach of Concordia's herald, the first player swears to it, and is in faction one. Take notices hands over what the world has to tell its players, which is a single notice, for the first player, saying they've joined Concordia. Asking Concordia's herald again is refused, as they're sworn to Concordia already, and that's once and for good. Asking Fortuna's is refused too, with that sentence, as there's no swearing to another faction either. The player is still in faction one, and there's nothing new to tell them, as a refusal goes back as the status, for the server to send. The second player, within reach of Fortuna's herald, swears to Fortuna, which shows the refusals were about the first player having sworn, and not about the heralds. Last, the first player leaves, and joins again with their faction, as a player does who comes back, or comes through a gate. They're as sworn as they were, and still can't swear to Fortuna.

Kept for good

20:11 Then the faction has to outlive a log out. The world keeps it on the player, and saves it into Redis with the rest of them, as it does the skills they've learned, so the tick doesn't wait on MySQL. The saver takes it from there, into a new column on the players table, and writes it where the row has none, so whatever a later save says, the first faction stays. When a player comes back, the world reads MySQL's, or Redis's when the saver hasn't got to it yet. The other way would be for the world to write it into MySQL as the player swears, with an update where the faction is null, as a guild's changes go. That's a single statement for each character, once, but a player's own state goes through Redis, so that a handover's next instance finds it there, and I'd keep it on that path.

20:56 First, a migration, version eighteen, as the players table needs a column for the faction each player swears to. Characters already in the table, from before this episode, get the column empty, a null, which is how the saver tells someone who hasn't sworn, further down. It adds a faction column to the players table, a tiny int, unsigned, from nought to two hundred and fifty-five, which is plenty of factions, and null for a character who hasn't sworn to one. Algorithm instant, as for the looks in episode ninety-four, adds it without copying the table. In the players' header file, progress, which is what the world saves of a player, gets their faction, by its id, or zero for none. It's a plain number, as the factions file is what turns it into a name and a colour. In players, load reads a player as they enter a world, from MySQL first, and then from Redis, whose copy may be newer. Its query asks MySQL for the faction as well, after the looks, and its read from Redis asks the player's hash for it, after the quests, as Redis may have something newer that the saver hasn't written yet. A faction is for good, so it's MySQL's, once the saver has taken it there, or else Redis's, and none is an empty string in both. Simple a to i turns it into a number, and a saved faction that isn't one is an internal error. Further down, where the world saves the players who changed into Redis, a player's faction goes into the hash as its number, or as an empty string for none, as in a hash from before there were factions. It goes in last, after the quests, which were the newest field before it. Then the saver's flush reads the faction from each hash along with the rest, and the statement it builds gets one more value for each player, which is null when it's empty, as the others are. That value is the hash's faction, if they've sworn to one, and the saved rows get a column for it. Last, where the other columns take what was saved, the faction takes coalesce of the row's and the saved one. Coalesce returns the first of its arguments that isn't null, so a row with a faction keeps it, whatever a save says, and a row without one takes the saved one, when there is one. So a faction is written once, and nothing switches it, not even a bug in a world that saved another. In the server's header file, the server gets an enlist of its own, and in the server, progress of, which turns a player into what's saved, copies their faction. When an input comes with enlist set, the server calls its enlist with the player's id and the herald's. That finds the world the player is in, and asks it. A refusal goes back to the player as its sentence, in the chat, for their client to say in their language. Otherwise the log gets a line with who joined which faction, by its number, which is what I'd search for when someone asks why they're on the wrong side. Further down, where the server sends each player's piece of the world without their name, between the full worlds, it leaves their faction out too, as the client keeps it from the last full one. In shown, which turns a player in the world into what goes over the wire, the faction goes in, with the name and the looks. And the piece of you that goes in each datagram, with where you are, leaves it out, as it leaves out your name.

24:05 Now, let's pin that down with a test in the players' tests, which run against a MySQL and a Redis of their own, in Docker. The fixture has made a player already, and the test makes the players class over its database and Redis, with the player's id as a string for the queries further down. Loaded into Ember, a player who hasn't sworn has faction zero. Then the world saves them with faction two, which goes into Redis, and loading them again gives faction two straight away, from Redis, as MySQL hasn't got it yet. Then the saver flushes, and a small lambda reads the faction column from MySQL, by the player's id, or an empty list of rows when the query fails, and MySQL has two. The next saves are the reason for this test. A save with faction three, as a bug in a world might send, is flushed, and so is a save without a faction, which is how a hash from before factions looks, and MySQL still says two after both. Last, the test deletes the player's hash from Redis, as if Redis had lost it, and the player loads with MySQL's faction, which is two as well. And a test in the server's tests, with players logged in over TLS, as the client does, to see the faction go over the wire. It needs content with Concordia's herald where new players start, which the fixture's content hasn't got. So it parses an NPCs file of its own, with kind seven, the herald of Concordia, with a skin and nothing else, standing a metre east of where new players start. Then it parses a factions file with Concordia alone, and herald seven. The fixture's server has the tests' content, so this test makes a server of its own, with the herald in it, on a port of its own, with the fixture's TLS, world, metrics and the rest. The test stops there if the server couldn't be made. Then bob logs in to it, with a token of his own. A lambda says whether bob sees alice in a faction, from what bob was sent. It uses any of over the players in the last world bob got, with a lambda of its own for each player. It looks for alice, with a name, as the faction comes with the name, in the world sent in full once a second, and in that faction. alice logs in, and the test waits until bob sees alice in none, at first. Then it makes an input for alice, with enlist set to one, the herald's id, as the herald is the world's first NPC, a metre away. Once a notice comes, it says alice has joined Concordia, and within a full world, bob sees alice in faction one. That input again gets a second notice, which is the refusal. Then alice leaves, which saves the player, and once bob doesn't see alice any more, alice logs in again, and bob sees alice back in Concordia.

26:44 The protocol's two tests come first, from a fresh Docker build, which takes a few minutes, and grep keeps the passing tests and the summary. A faction is named by its id, and parts keep it between the full worlds. Then the server's three, for which compose starts MySQL and Redis, and runs the migrations, before the tests run against them. The faction is kept for good in MySQL, and another player sees it, before and after a log out. All three pass, so the server's side of an oath holds, and the client can have its window.

A herald's window

27:16 Now the client, where a player is going to swear, starting with a new header file for the herald's window. After its include guard, it brings in c std int, map, optional, string, utility and vector, then the animator's header, for the figures, and the content's factions. Herald window is the window a click on a herald opens. It has two states to draw, before the player has sworn and after, and Enlist is in the first alone. Its constructor takes the factions by value and moves them in, and it's explicit, so a map can't turn into a window by accident. Open takes the herald, by the NPC's id, their name and their faction. The window is open while it has a herald, and close forgets them. Build draws it each frame, with the figures, to find you among them, and returns the herald to swear at, by the NPC's id, once you've said you're sure, which closes the window. Otherwise it returns an empty optional, and the client sends nothing. It keeps the factions, the herald, as an optional NPC id, their name and their faction, and whether Enlist was clicked, so it's asking whether you're sure. That's all of its state, and the rest of the header closes the class, the namespace and the guard. In the herald's window, after its own header come Dear ImGui and raylib, then algorithm, for find if, c std int, optional, string, utility and vector. Then there's Abseil's substitute, for putting a name into a sentence, and our names, theme and translations. Open keeps the herald, moves their name in, as it took the name by value, keeps the faction, and starts without asking, as a window that opens again asks again. Close resets the herald and the question, for the next herald to open it. Build draws nothing while there's no herald, or their faction isn't in this client's content. It finds you among the figures, with find if and a pointer to the member that says which figure is you, and then your faction, if you've sworn to one. The herald's faction is named in the language in use. The window opens in the middle, seventy percent of the way down, under the herald and your character, and stays there while it's open. It's three hundred and eighty points wide at a scale of one, and its height is zero, which ImGui takes as fitting what's in it. It's one of our panels, with the herald's name as its title, and a cross that clears open. The faction's name goes in its colour, as it's going to go by its players' names, in the titles' font, a little bigger, with raylib's colour turned into ImGui's, at full alpha. A separator draws a line under the name. For a player who's sworn already, to theirs or another, it's for good, so the window says which faction they're sworn to, in the server's own sentence, and has no button. For one in none, before Enlist, it says what swearing does, that the faction's colour goes by your name for everyone to see. Enlist goes under it, as wide as the window and in the primary style, and it sends nothing yet, and asks. Then the window says it's for good, that there's no leaving the faction, nor swearing to another. Swear sends the herald, and closes the window, while Not now, beside it, closes it without sending anything. I'm glad it asks twice, so a click on Enlist by mistake costs nothing. Once the window's been closed, by either button or the cross, it forgets the herald. In the client's CMake file, the window joins the core library. In picking, a click finds an NPC to talk to, one who talks, teaches or gives quests, within talking reach, and a herald joins them, by having a faction, or a click on one would pick nobody. In main, the window's header comes in, and the window is made with the content's factions, next to the trainer's. The player's frame gets the factions too, which we come to in a moment. A click on an NPC whose figure has a faction, which makes them a herald, opens their window. Escape closes whichever of the NPCs' windows is open, and the herald's is one of them now. Each frame, the herald's window is built next to the trainer's. Once it returns a herald, the client sends an input with enlist set to them, on top of what it sent last, the way the trainer's window sends what you learn.

A mark by each name

31:21 Next, the mark by each name, starting with the animator's header file. There, a figure gets a faction, a player's, or the one an NPC is the herald of, by its id, or zero for none, and its colour, which is blank without one, raylib's colour of all zeros. In the animator, a small function called colour of gives a faction's colour. It goes through raylib's get colour, which reads the file's number as red, green, blue and alpha, or blank for no faction, or for one this client's content hasn't got. Where it stands an NPC, a herald gets their faction from the content, by their kind, through herald of, along with its colour. And a player's figure takes the faction the server sent, with its colour too. In the names' header file, name of gets a version for a faction, and in names, it's translated in a context of its own, faction and its id, as the content's other names have been since episode ninety-five. In the client's messages, a faction in a server's sentence is found by its id and named through it. In the nameplates' header file, faction mark returns the mark before a player's name, or a herald's, in their faction's colour, or nothing for one in no faction, or in one this client's content hasn't got. Draw nameplates, declared under it, is going to draw a diamond in that colour before the name, using faction mark. In the nameplates, optional and our theme's header come in, the theme for its diamond. A faction's mark is a diamond as wide as half the name is high, with a fifth of the name's height between it and the name. Sizing it by the name keeps it right at any of the interface's scales. Mark width is the room the mark takes before a name, the diamond and its gap, or nothing without a faction. Draw faction mark draws it at the left of a line of text, given where that line starts and how high it is. The diamond has half its width as the radius, centred on the line, through the theme's diamond, outlined in the nameplates' shadow colour. Faction mark is nothing for a monster, as monsters swear to nobody, and nothing for a figure without a faction. Otherwise it's the figure's faction colour, unless that colour's alpha is zero, which blank's is, so a faction this client doesn't know gets no mark. Drawing each nameplate, the mark is worked out once. For an NPC, the name moves right by the mark's width, so the two are centred together over their head, and the diamond goes in front of the name. A player's line is their level and name, so the mark goes before both, and the level and the name move right by its width. Last, the player's frame, in the top left corner, whose header file brings in map, utility and our factions header. The frame is going to show the faction you've sworn to, as well as your face, your name, your level and your health. It takes the factions as it's made, by value, moved in, and explicit, as for the herald's window, and keeps them in a member of its own. In the frame, our names header comes in, for the faction's name in the language in use. The frame finds your faction among its factions, as your faction goes under the plate once you've sworn to one, and you're sworn when it's there. The frame's window then reaches thirty points under the plate, for a line there, and stops under your portrait otherwise. Further down, after your guild's name, the line goes in, in the faction's colour, from the content. It's six points under the plate, with a diamond of that colour, as over your head, outlined in the theme's night colour. Then comes the faction's name, in bold, at fourteen points, with a shadow, in its colour too.

34:54 On the client's side, a few quick tests too, starting with the animator's, where a new test checks who gets marked. It starts from a world with a single player, alice, through the tests' helper, standing still, and alice is sworn to faction two. Then the world gets bob, id two, who's sworn to nothing, an NPC with id one, of kind eight, which is Fortuna's herald, and another NPC, id two, of kind one, a guard. The content has Concordia and Fortuna, parsed from text as before, and nothing else. Once an animator with that content turns the world into figures, a tenth of a second on, there are four of them, the players first. alice's figure is in Fortuna, with gold's red and gold's blue, as the factions file has them. Then the one for bob is in none, with a colour whose alpha is zero, which is blank. The third is the herald, an NPC, who's Fortuna's, and the guard is nobody's. In the nameplates' tests, which had a single test, that dead monsters go bare, there's a new one. It checks that the sworn and the heralds get marks in their factions' colours. Gold is Fortuna's colour, as raylib's red, green, blue and alpha. The figure for alice is sworn to faction two, in gold, which designated initialisers set by name, while the rest of the figure keeps its defaults. And Fortuna's herald is a figure in faction two as well, in gold, and an NPC, as the herald swears players to it. So alice has a mark, in gold, and the herald has one too. Then a figure for bob, in no faction, gets no mark, which an expect false on the optional checks. And one for carol, in faction nine, whose colour the figure hasn't got, as this client's content hasn't got the faction, gets none either. And in the client's messages test, the one that reads the game's own Romanian file and its content gets a faction, by its id, whose name stays as it is, and its herald. It takes Concordia from the game's content, and checks what translated makes of two sentences about it, in Romanian. The sentence for joining it, made as the world makes it, comes out as Romanian's file says it, with Concordia's name as it is. Then being too far away to talk to its herald, with the herald by their kind, names the herald the Romanian way, as the herald of the faction Concordia. Last, picking's test, after the trainer and the NPC who gives quests, gets a herald, an NPC with a faction and nothing else, who's picked as the nearest of them.

37:24 The game speaks Romanian as well, so the new words need translating. Our Python script, po, gathers the strings the game shows into one template. Now it reads the factions file too, so the three names get translated like the rest, with a note that a Latin name can stay as it is. Then the Romanian file, so a player who picks Romanian sees the herald's window in it, from the line about the colour to the warning that it's for good. Concordia, Fortuna and Victoria stay in Latin, which reads fine in Romanian. The two buttons are nouns, an enlistment and an oath, as the game's Romanian doesn't address the player as you.

38:01 Let's build it in Docker, as CI does, and run the animator's new test, the nameplates' and the one with the game's Romanian file, which takes a few minutes with the build. Grep keeps the tests that passed, and the summary. The marks go on the sworn and the heralds, and the faction's sentences come out in Romanian, with its name as it is. They pass, so let's see it in the game.

Swearing in the game

38:24 Now the game with all of that, from the spot west of where new players start again. A click on Concordia's herald opens their window, with the faction's name in its colour, and what swearing does. Enlist asks once more, as it's for good, and sends nothing to the server yet. Swear sends the herald's id, and the world's answer comes into the chat as a message, with Concordia in it by its id. Within a second, the next full world brings arthur's faction with the name, and the diamond goes before it, in sky blue, while the frame in the corner gets Concordia under the plate. Asking Fortuna's herald, or any other, gets the window's sentence about being sworn to Concordia already, without a button, so the client doesn't send anything. Escape closes it, as it does the other NPCs' windows.

Bots take sides

39:14 Last, the bots, which play our load tests. In the quests' header file, a bot's errand can be a trip to a herald, to swear to their faction, as well as a trip to a monster to hunt, or to an NPC for a quest. The kinds of errand get swear, next to hunt, take and turn in, and the quest an errand carries is for a quest's errands alone. And the errand gets the herald to swear at, by the NPC's id, or zero on the way to where they stand, before the bot has seen them. That way a load test runs the oath along with the quests. Then a new header file in the bot's folder, factions. After its include guard, it brings in c std int and optional, the quests' header, for errands, the content's header, and the protocol's, for the world a bot sees. Faction for is how a bot picks its side. It takes the bot's seed, its number in the load test, and goes round the factions file in turn, or gives zero when the content has none. So each bot keeps to one side, run after run. Oath takes the content, the faction, the world the bot sees and its land. It's the trip to the faction's herald, to swear to it. It goes to the herald where the bot sees them, once it does. Before that, it goes in the land the bot's in, by its place in our list of lands, to where the NPCs file stands them, without a herald to swear at until it sees them there. In a land without them, such as the Ashlands, there's no trip at all. And sworn says whether the world shows the bot sworn to a faction, which comes with its name, once a second. They're three free functions, so the tests can call them without a whole bot and its connection. In factions, after its header come algorithm, c std int, iterator, for std next, and optional, then our lands header, for a land's place by its name. Faction for returns zero when the content has no factions. Otherwise it steps along the map from its start, by the seed modulo the number of factions, with std next, as a map's iterator moves a step at a time, and returns that faction's id. So seeds nought, one and two get Concordia, Fortuna and Victoria, and three starts again, so a hundred bots come out at about a third each, whichever number they start from. Oath, with the content and the faction it's for, first finds the faction in the content, by its id, and there's no trip for one that isn't there. Then it takes the herald's kind, and looks for that kind among the NPCs the bot can see, with ranges find and a projection, a pointer to the NPC's kind, which find compares with ours. When there's one, the errand is a swear, to where the herald stands, with their id. The server sends each player the NPCs near them once a second, so a bot that's just arrived may not see the heralds yet. Then it goes through the NPCs file's spawns, for the herald's kind in the land it's in, and goes there, without a herald's id yet. Sworn looks for the bot among the world's players, with any of, by the id the world says is you, with a faction that isn't zero, as the server sends each player which one they are. In the bot's CMake file, factions joins the bot's library, and its test joins the bot's tests. In the bot's header file, a bot is going to pick its faction from its seed as it's made, and its errand can be an oath now. A bot keeps whether it's sworn, or given up, as its herald wasn't where they stand, and which faction it's for, by its id. In the bot, our new header comes in, after the bag's. The constructor picks its faction from its seed, as it's made. Further down, where a bot picks its next errand, it takes the world it sees once, as it's going to look at it twice. Before its quests, while it hasn't sworn, and the world doesn't show it sworn already, as a bot from an earlier run might be, it goes to swear first. That's at its herald, where it sees them, or where they stand in its land. Without an oath to make, it's the next quest's errand, as before. Then, at the end of the trip, for an oath, it swears at the herald it saw, or, if it set off before it could see them, it looks again, now that it's there, with the world it sees now. It gives up if nobody stands there, as a herald might not. When there's a herald, it sends enlist with their id, and either way, it's done with swearing, so a herald who isn't there doesn't keep it walking back to an empty spot.

43:37 To pin the bots' side down, there's a new file next to it, factions test, which reads the game's content from its folder, as the bots do. It includes c std int, map and optional, the content and GoogleTest. In an unnamed namespace, a using declaration lets the tests say errand without the namespace. Then a few constants name the game's factions, Concordia, one, Fortuna, two, and Fortuna's herald, kind eight, then the lands by their places in our list of lands, the Meadows first and the Ashlands second. A small function reads the game's content for each test. The first test is that a crowd of bots swears to each faction in turn. It counts, in a map by faction, the factions of three hundred bots, by their seeds, from nought to two hundred and ninety-nine, and gets a hundred in each of the three. Empty content, without factions, gives a bot none to swear to. The next is that a bot goes to its faction's herald once it sees them. The world the bot sees gets a single NPC, Fortuna's herald, with an id of nine, which is theirs and nobody else's, standing where the NPCs file has them. The oath for Fortuna, in the Meadows, is a swear, at herald nine, at their place, with float equality for the coordinates. There's no faction nine, so there's no oath for it. Then, before it sees them, a bot goes to where its herald stands. With nobody in sight, which is an empty world, the oath for Concordia, in the Meadows, is there, as the assert checks, and it's a swear. It has no herald yet, and goes to where the NPCs file stands Concordia's herald, at minus eight and minus one. In the Ashlands, which have no heralds, there's no oath. The last test is that a bot knows once it's sworn. The world says the bot is id three, with the bot among its players, and another player, id four, in Fortuna. Another player's faction isn't the bot's, so the bot isn't sworn, until its own faction is Fortuna, and then it is.

45:36 Next, the bots' faction tests, in a fresh Docker build, which takes a few minutes, with grep keeping just the tests that passed and the summary. A hundred bots go to each faction, and the oaths go where they should. All four pass, so let's run a crowd.

A crowd in three colours

45:53 Last, the crowd at the end of the episode, with thirty bots logging in from the middle, the way they do in a load test. Each bot walks to its faction's herald before anything else, and swears there, so they bunch up round the three of them for a moment. Within a few seconds, the names round us carry all three colours, and the bots go on to their quests as before. I like how quickly a load test turns into something that looks like a server with sides, and each diamond is an oath the world checked and saved, as arthur's was.

Decision record forty-seven

46:31 Today's choices go into a decision record, number forty-seven. Its context is that a player belonged to nothing bigger than a guild until today, and sides have to exist, with each player on one, before anything can be built for them to fight over. The decision is a faction as content, sworn to at its herald, for good, for now, with guilds left out of it, and nothing in a fight yet. The alternatives are the ones from earlier, choosing it as the character's made, switching sides, guilds' factions and enemies everywhere, along with keeping it in MySQL alone. And there's sending its name on the wire, where its id is one small number the client translates. Among the consequences, a wrong choice can't be undone, apart from making another character, which I'm fine with while the window says so before Swear. The record goes into the index, accepted in episode ninety-seven.

Wrap-up

47:23 Today, our game got three factions in its content, each with a herald standing west of where new players start. A player swears at a herald, once and for good, which the world checks, and the saver writes the faction where a row has none, so nothing switches it. The client asks once more before Swear, and draws a diamond in the faction's colour before each sworn name, and the bots swear too, a third to each. Decision record forty-seven has the reasons, with what was weighed. For now a faction is a colour and a name, and nothing reads it in a fight, so what the sides fight over can wait until players have picked them.

Playing it

48:49 Which of the three would you swear to, Concordia, Fortuna or Victoria, and would you want to be able to leave it? Tell me in the comments, along with what you'd like to see next, and subscribe if you'd like to follow along. I hope you find this useful, and thanks for watching.