Player versus player duels for our MMORPG, in C++. /duel and a name asks a player within ten metres, and once they say yes, in the chat or on the question on their screen, a flag goes down and the server counts three seconds down on both screens. Then each picks the other, as they would a monster, in a field of its own beside the monster's, which the world keeps for the opponent and nobody else, and swings and skills land on them. The blow that would leave a duellist without health leaves them one, and they've lost, as has anyone who goes more than 25 metres from the flag: nobody dies in a duel. The world keeps the duels, the server sends both sides each change over TCP, and the raylib client draws the countdown, the flag and a ring at the opponent's feet. This is episode 81 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 ep081/end.
Transcript
Intro
0:00 Hello and welcome back to episode eighty-one, where, for the first time, the players in our game get to fight each other, in duels that both of them say yes to. The server decides each step of it, from the ask to the blow that ends it, and nobody dies, as a duel stops at one health. Let's begin with a quick look at where we're going to be by the end of the episode.
0:22 Here, gwen has asked us to duel, and once we say yes, the server counts three seconds down on both of our screens. A click picks her, as it would a monster, and we fight until one of us is left with one health, as nobody dies in a duel. She's two levels up on us, which means harder blows and more health than ours.
0:46 The game's code is all open source, on GitHub, and the series' website links to it, with a page for each episode, where you can browse its code at each of its steps.
Last time
0:56 A quick reminder of where we left off, first. Last time, the game got a page of its own in the browser, where holding Space can't scroll anything. We wrote the server's side of WebSocket from its RFC, and a gateway on top of it, which carries a page's WebSocket to the login server or a world, over TLS. The client's two kinds of link file the server's messages into one inbox, so a browser logs in and plays online, in one world with everyone else.
Today
1:23 Here's the game as the last episode left it, with gwen standing a few metres ahead of us. There's no slash duel in the client yet, so the line goes to the world, as something said to everyone in Ember. A click on gwen picks nobody, as a click picks monsters and nothing else, and Space swings at the air, as nothing in the game lets one player hurt another. In most other games, two players can agree to a duel and fight until one of them is beaten, which is what we're going to build.
1:56 So, today, a player asks another to duel, by name, from within ten metres, and once the other says yes, a flag goes down, and a countdown of three seconds starts. Then each of them picks the other, as they'd pick a monster, and hits them with swings and skills, until a blow would leave one of them without health. That blow leaves them one instead, and they've lost. Going more than twenty-five metres from the flag loses too, so a duel can't turn into a chase across the whole land. The world keeps the duels and decides each step, while the server sends both sides each change over TCP, and the client draws the countdown, the flag and the ring at the opponent's feet. I've wanted duels in the game for quite a while, and I think they're going to be a lot of fun to watch! Right, let's start with the messages a duel needs.
The messages
2:42 First, in the top CMake file, the game's version goes up to zero point eighty-one, for this episode's code. A duel starts where a trade did, in our proto file, with what a client can ask for, and what it's told. The client's input gets a duel, a duel action, as field nineteen. Over it, we write that it's something to do about a duel with another player, sent once, like a trade's. And the World message gets your duel, as field twenty-seven, which comes the way a trade does, as the duel's asked for, counts down, starts and ends. At the end of the file, a duel action holds a oneof, as a player does one of these at a time. Ask names a player near you in your land, to ask them to duel. Accept says yes to the player who asked you, which starts the countdown, decline says no to them, and yield gives the duel up to the other side, or calls it off before it starts. Over the message, we note that the server says why not in a line from nobody, as it does for a trade. Then an enum for how far a duel has got, from the ask to the fight. None is zero, for no duel at all, as protobuf wants the first value of an enum to be zero, which is what a field reads as when it's left off the wire. Asking is when you asked them, and they haven't answered. Asked is the other way round, when they asked you first. Countdown is after the yes, while the three seconds run down to the start, and fighting is when each of you can hit the other, and nobody else. Over the enum, we write that it's how far a duel has got. Last, the duel the client is told about. It has the opponent's name, for the screen, and their id, as the client is going to pick them by it. Then how far it's got, and during the countdown, how long until the duel starts, in milliseconds. I like that the server sends what's left and lets the client round it for the screen. And the flag, from the countdown on, where the one who asked stood as the other said yes, which neither of you may go far from. Over the message, we write that it's your duel, with who it's with by their name and their id, or nobody once it's over, and how far it's got.
Duels in each world
4:54 The duels get a header file of their own in the server, called duels, next to the trades'. It brings in chrono, for how long an ask and a countdown last, c std int, map, set and vector from the standard library, and the lands' header, for a place in a land, which is where the flag goes. Duels are the duels between the players in one copy of a land, which its World keeps, as it does their trades. I think the world is the right home for them, as both players are always in that copy, and a gate or a log out reaches the duel through the world they leave. Over the class, we write how a duel goes. A player asks another to duel, and once the other says yes, a countdown starts, and then the duel. And a player is in one duel at a time, from the ask to the end. A player asks another within ten metres of them, which is the reach, a lot further than a trade's four, as two people about to fight tend to stand apart. An ask waits thirty seconds for a yes. That's a chrono duration, with the unit in its type, so it goes into a duel's nanoseconds left as it is, and chrono converts it for us. From the yes to the duel is three seconds, so that both sides are ready for it, which in my opinion is enough to get your hands on the keys, and not so long that you start to wonder. And the flag goes down where the one who asked stands as the other says yes, and a duellist who goes further than twenty-five metres from it loses. A duel's state is an enum class: asked, counting down, or fighting. A duel has who asked, the challenger, and who they asked, the challenged, both by their ids. It starts out asked, with how long it has left, first of the ask and then of the countdown, and the flag. Against gives the other side from a player's, by their id, which the world's code needs all over the place. Then come the functions that the world calls on the duels. Ask has a player ask another, where neither may be in a duel already, which the world checks before it asks. Accept says yes for the player, to whoever asked them, which puts the flag down and starts the countdown. It returns who asked, or zero if nobody did. Of gives the player's duel, from the ask to the end, or a null pointer. Opponent gives who the player's duelling, by their id, once the countdown's over, or zero. It's what the world is going to ask before a blow lands on a player, so a duel that's still counting down hurts nobody. End ends the player's duel, however far it's got, and returns who it was with. Step runs the asks and the countdowns down, starts the duels whose countdowns are over, and returns the asks that ran out, which are over, for the world to tell the one who asked. All hands out each duel, by its challenger's id, and take news hands over the players whose duel has been asked for, counted down, started or ended since the last time, as the trades do. Privately, the duels sit in a map by the challenger's id. A second map gives the challenger of each player's duel, by the ids of both sides, so we find a player's duel straight away, through its challenger, whichever side they're on. And the news is a set of ids.
8:05 In duels, we bring in our header, chrono, c std int, set, utility, for std exchange, and vector. Ask makes the duel, in the map under the one who asked, with them as the challenger and the other as the challenged. Then both sides point at it, through the challenger's id, and both of them have news, so both hear about it after the tick. Accept starts by looking up the challenger of the player's duel. A player in no duel, or the challenger, has nobody to say yes to, so it returns zero. That second check matters a lot, as without it, the one who asked could say yes for both of them. A duel that's past its ask can't be said yes to again either. Otherwise the duel moves on to its countdown, with three seconds left, and the flag where the world says it goes. Both sides get news, and it returns who asked. Of finds the player's challenger, and gives the duel under them, or a null pointer. Opponent is the other side while the duel is fighting, and zero before that. End finds the player's challenger too, and returns zero for a player who's in no duel, as there's nothing to end. Otherwise it finds their duel, keeps who it was against, then erases the duel and both sides' entries. Both get news, as a duel with nobody in it is how the client learns it's over. Then step, which the world calls once a tick. A duel that's fighting has nothing to count down, so it's skipped. Otherwise we keep how long was left before this tick, and take the tick off. An ask whose time has run out goes on the list of asks that ran out, to end after the loop, as ending it now would erase from the map we're looping over. For a countdown, we write over it that both sides hear as each second of it goes, so that each shows the seconds the server has left, and as it ends. A countdown that's over starts the fight, with nothing left, and both sides hear. Otherwise, chrono's ceil rounds what's left up to whole seconds, and when that's gone down since before this tick, from three to two, say, both sides hear again. So a countdown is four messages to each side, as it starts, at two, at one, and as the fight starts, and not one each tick, which would be twenty a second. After the loop, each ask that ran out ends, and step hands the list back. And take news swaps the set for an empty one with std exchange, and returns what it had, like the trades' take news.
10:29 Next, the World, which keeps the duels, starting in the world's header file, where we bring in the duels' header. Ask to duel has the player ask the one of that name here, whatever its case, within the reach, or say yes to them asking first. Or it returns an error that says why not, in words for the player. Answer duel says yes or no to the one who asked, and a yes puts the flag down where they stand, and starts the countdown, which the referee runs down from there. Yield gives the duel up, so that the other side wins it, or before it's started, calls it off, which is what slash duel yield is going to do in the client. Duel of gives the player's duel as their client gets it, with who it's with, how far it's got, and its flag. Take duel news hands over the players whose duel has been asked for, counted down, started or ended since the last time, for the server to send after the tick. Privately, the referee runs after everyone's acted, each tick. The asks and the countdowns run down, and a duellist who's dead, or too far from the flag, loses. Lose ends a player's duel, as they've lost, and both hear who won. And the world gets its duels, right under its trades. In the world, a player who leaves, which a gate and a log out both do, calls their trade off, and over it, we now write that both have to be here to trade, or to duel. So their duel ends too, whatever state it's in, and the other side hears that they've gone, so the duel's off, as there's nobody left to fight. Ask to duel starts like ask to trade, from episode seventy-four. It finds the player who asks, and the one named, whatever its case, among the players in this copy of the land, and if either's missing, nobody of that name is here. You can't duel yourself either, as you'd be both the one asking and the one asked. Then, asking back the one who asked you is a yes, as it is for a trade, so slash duel and their name gets you your duel, through answer duel. It checks what a yes needs on its own, so we return what it says. That way, the two ways of saying yes can't drift apart. Further than the reach is too far away, and the error names the one they asked, so that it reads well in the chat. A player who's in a duel already can't ask anyone else, as a player is in one duel at a time, and nobody can ask a player who is. Then the duels have the ask, and the world writes two notices. The one asked hears who asks them, and that slash duel yes says yes, and the one who asked hears that they did, so they know the ask went out. Answer duel needs a duel where the player was asked, and still is, or else nobody's asked them. It keeps who asked, by value, as ending the duel would leave the pointer dangling. A no ends the duel, and the one who asked hears that this player said no to a duel. For a yes, we check the distance again, and over it, we write why: they may have walked apart while the ask waited, and thirty seconds is plenty of time to wander off. Then the duels accept it, with the flag where the one who asked stands now. Yield needs a duel to give up, or it says you're not in one. Once it's fighting, yielding loses it, as if the last blow had landed. Before that, it ends the duel, and the other side hears that it was called off, and nobody won. Duel of starts from an empty duel, which is what a player without one gets, and returns it straight away for them. With a duel, it fills in the opponent's name and id, and switches on the state. An ask is asking for the challenger, and asked for the other side, and it returns there, as there's no flag yet, and nothing else to send. A countdown sends what's left of it, in milliseconds. That's a thirty-two-bit number, so we static cast what a duration cast from the nanoseconds the duels keep gives us, and a fight sends its state. From the countdown on, it fills in the flag. Take duel news passes the duels' news on. In step, after the players and the monsters have acted, the referee runs. The referee steps the duels, with the tick's time, so the asks and the countdowns run down, and the duels whose countdowns are over start. Then it goes through the asks that ran out, which step handed back, and pushes a notice for each one who asked, that the other didn't answer, so there's no duel. Over the losers, we write that losing ends a duel, so the losers are found first, and lose after, as ending a duel would erase it from the map we're looping over. For each duel that's fighting, we look at both sides. A side has strayed when they're further from the flag than its reach, measured from where they stand to the flag, and they hear that they went too far from the flag. Strayed, or dead, as a monster can still kill you in a duel, and that side has lost. The break gives a duel one loser, even when both sides stray in one tick. Then each of the losers loses their duel, one after another. Lose ends the duel, which gives us the winner, as end returns who the duel was with. The loser hears who won, and the winner hears who they won against. Last, in the server's CMake file, duels go into the world's library, next to the trades, as the client runs the world's rules offline too, and its tests go into the server's tests.
15:40 Now, let's pin these rules down with a few quick tests, in a test file of their own. We bring in our header, chrono, c math, c std int, set and vector, Google Test, the test content, the world and the terrain. The tests step the world fifty milliseconds at a time, the server's tick. Their world is an island, with the test sword. Over it, we write why: a duel reaches further than the tests' little board, and the island's players start in a clearing, sixteen metres around the middle, where no tree grows. Wait steps the world a tick at a time, for as long as we ask, as a world runs at whatever pace its caller steps it. The first test is that a challenge waits for a yes. Player one, alice, joins two metres west of the middle, and player two, bob, three metres east. As we write over it, alice asks for bob by his name, in capitals, as a name matches whatever its case. We assert on that, as nothing after it means much if the ask failed. There are two notices, and the first is for player two, bob's, which says how to say yes, and the second is alice's. alice is asking, bob is asked, with alice as his opponent, and both of them have news, as a set of their two ids. Over the wait, we write that nothing starts until bob says yes, and that alice can't say it for him. So we wait ten seconds, a third of the ask's time, and his duel is still asked, while alice, answering as player one, hears that nobody's asked her. Then bob does say yes, and that's asserted, as the rest depends on it. Over the next lines, we write that the flag goes down where alice stands, and the countdown starts. His duel is counting down, from three thousand milliseconds, and the flag is at minus two, where alice stood, as she's the one who asked, and both of them have news again. Over the waits, we write that both hear as each second goes, and that it starts three seconds on. A second later, alice's countdown says two thousand, and both have news again, as a second has gone. Then we wait a tick short of the next two seconds, and it's still counting down, and one tick later, they're fighting, with bob, player two, as alice's opponent. Next, ten and a half metres is too far to challenge. This time, alice stands in the middle, and bob ten and a half metres east of her. Her ask's error says she's too far away to duel bob, bob has no duel, and nobody gets a notice. Its twin is that ten metres is near enough, and bob's asked. Together, the two tests show that the line is at the reach, and not somewhere short of it, which a test of a refusal alone couldn't tell us. Then, an ask runs out after thirty seconds. alice asks bob, and we take the notices and the news the ask made, so that what we look at next is what the waiting makes. We wait a tick short of thirty seconds, which is twenty-nine thousand, nine hundred and fifty milliseconds, and bob is still asked. Then one more tick, the fifty milliseconds that make it thirty seconds, and neither of them has a duel any more, as the ask is gone. Both have news, as a duel with nobody in it tells them it's gone. Then we take the notices, and there's one, for alice, that bob didn't answer, so there's no duel, and bob's yes comes too late, as nobody's asked him any more. Last, a test that leaving the flag loses the duel. Here alice joins in the middle, and bob three metres east. She asks, he says yes, and we wait out the countdown, asserting that they're fighting, then take the notices. Over the walk, we write that bob walks off, round any trees, and that the duel's on until he's more than twenty-five metres from the flag. So we pick a walkable spot forty metres east, and he walks there, with the pathfinding from episode fifty-two. From flag is a lambda that measures how far bob is from the flag, at the middle, where alice stood. While he's within the flag's reach, we step the world, and each tick, the duel has to still be on, for four hundred ticks at most, in case he gets stuck. Once he's past it, alice's duel is over, and so is bob's, and there are three notices, which we check in order. First, bob hears that he went too far from the flag, then that alice won, and last, alice hears that she won against bob.
19:58 We build the game at this step and run the duels' tests. They run a world in the test process, without MySQL or Redis, so dash dash no deps leaves those out, and grep keeps the lines of the tests that passed. The build takes a while, as the image builds the whole game, with this step's code. All five pass, from a challenge that waits for its yes, to a duel lost by going too far from the flag.
Hitting each other
20:22 Now the duellists need to hit each other. A swing or a skill goes for what the player picked, their target, and so far that's a monster, by its id. A player's id is their character's, from MySQL, while each world counts its monsters from one, so player twelve and monster twelve can both be standing there. So the player you pick goes in a field of its own, next to the monster's, called target player. The world keeps it for your opponent, in a duel that's started, and anyone else is nobody, whatever a client sends. Then blows land on the opponent, all-around skills too, though they don't push or throw a player, and a blow that would leave them without health leaves them one, and they've lost.
21:05 First, the new field goes in our proto file. The input gets target player, as field twenty. Over it, we write what it is: in a duel, the player you picked, by their id, in place of a monster, which can be your opponent and nobody else. It comes with each input, like the monster. And a player, as everyone else sees them, gets a target player too, as field sixteen, while they use a skill in a duel. That way, the skill's sparks land on the right person, on everyone's screen. A field that's zero costs nothing on the wire, as protobuf leaves it out, and I much prefer that to a rule about which numbers are monsters. In the world's header file, we bring in optional, and a player gets their target player, next to their target. Over it, we write what it is: in a duel, the player they picked, by their id, in place of a monster, which is their opponent, as nobody else can be. Players and monsters have ids of their own, so it goes beside the monster. Pick takes the player too, which is zero unless we say otherwise, so the code that picks monsters stays as it is. In a duel, it takes the opponent, by their id, in the monster's place, and over it, we write that a player who isn't the opponent is no target. Approach runs the player towards what they picked now, whatever it is, and we say so over it. Target gave the monster a player picked, while it was alive and in reach. Now it gives where it is, as an optional place, the monster or the opponent, as the aim and the run-up need where to go, and nothing more. And there's a second hurt, for a blow on a player from their opponent, where one that would leave them without health leaves them one, and they've lost. In inputs, both places that apply a player's keys pick with the input's target player too, the one for an input that comes on its own, and the one for numbered inputs, at their tick. And in shown, a player using a skill shows their target player, next to their target.
23:04 In the world, we bring in optional, for what target gives back now, as a place may not be there. Then within, a function template, as it works on a map of monsters or of players alike. It takes the map, an id, the player and a reach. Over it, we write what it gives: where the unit is, a monster or a player, while it's alive and within reach of the player, or nothing. A unit that isn't there, is dead, or is out of reach gives std nullopt, and otherwise its place. It's the check that target did for monsters, moved out of target and into a function of its own, so that the opponent gets it too. In arc, which says whether a monster is in the slice in front of a player, for skills with an arc, becomes a template too. So a duel's opponent goes through it as well, as the unit can be a monster or a player. When a player joins a land, the player they picked was another land's, as much as the monster, so it goes back to zero. Pick is where the world decides what to trust. Whatever a client sends, it keeps a picked player when they're the opponent in a duel that's started, as opponent is zero until then. Over it, we write that their opponent goes in place of a monster, and anyone else is nobody. So a picked opponent clears the monster, and anyone else leaves target player at zero. A modified client can send whichever player it likes, and no player gets picked. Where act asks whether a skill's target is too far, it asks whether target has a value, twice. Approach takes the optional that target gives, and runs the player towards where it is, or returns when there's nothing there. It measures the distance with std hypot, as what it has now is a spot on the map, and not a monster, and goes along the line towards it, at a run, as before. The last step is a little shorter, so that it stops within the reach. Then target, which gave a monster, gives an optional place now. It calls within on the players for a picked player, and on the monsters otherwise, which replaces the six lines of the old check. Aim turns the player towards the place, monster or opponent, if there is one, and without one, they still swing at the air the way they face. Usable checks the optional too, for a skill that hits one. So a swing or a skill at an opponent out of reach runs the player up to them first, as it does for a monster, and none of that code knows about duels, which I find quite satisfying. In land, after the monsters a blow reaches, it hurts the opponent too, in a duel that's started. The blow has to be for them, or all around, with them alive, within the skill's reach, and in its arc, and then it hurts them, with the damage it does a monster. Over it, we write that a player isn't pushed or thrown, as their own keys move them. Being knocked about by someone else's kick would feel pretty unfair. The new hurt is the blow on a duellist. What it takes is the smaller of the damage and their health less one, so they're left with one at least. Over it, we write the rule: nobody dies in a duel, and the blow that would leave them nothing leaves them one, and they've lost. What was taken is a hurt event, as a monster's blow is, so the players near them see the damage it did, and a blow that couldn't all be taken loses them the duel. Last, when a player loses, both sides' picks go back to zero. Over it, we write that neither can hit the other any more.
26:30 Next, a few quick tests that duellists hit each other, and nobody else. In the duels' tests, we bring in memory and the skills' header. Swords is content with the test sword, the one the other tests use, and with a skill that we parse from text format, the way the content files are written. Over it, we write what it is: a sword, and a whirl of it that hits all around, as Whirlwind does. It takes a second to cast, lands half a second in, and hits for twenty-five, all around, within two and a half metres. Start duel has alice, player one, ask bob, player two, and he says yes, and the countdown runs out, as we write over it. Each of those steps is asserted, as a test that starts from a broken duel would tell us nothing, and the wait is the countdown's three seconds. Last, it asserts they're fighting, and takes the notices, so that each test starts from the fight. The first is that duellists hit each other. On the tests' little board this time, as nobody walks far, alice stands in the middle, and bob a metre east. Over the picks, we write that each picks the other, as they would a monster, and swings a sword, for fifteen, nine tenths of a second in. Each pick has zero for the monster and the other's id as the player. Both of them swing, with the attack key held. We take the events so far, so that what's left is the swings'. A second later, each is down fifteen from a hundred, alice and bob alike. Over what comes next, we write that each blow is an event, for the players near it to see. So we count the hurt events among the ones the world hands over, each of fifteen, and there are two, and the duel's still on, as nobody's anywhere near one health. Then, a test that others can't hit a duellist. This time, carol joins too, a metre on the other side of alice, and both alice and carol know the whirl, as skill one. Over the picks, we write who picks whom. First carol picks alice, who isn't hers to fight, and alice picks carol, who isn't bob, so neither keeps the pick, as the world turned both of them down, and both their target players stay at zero. Then alice and carol both swing, with the attack key held, and we wait a second, which is plenty for a swing to land. All three of them have their hundred, as each swing went at the air. Then they both let go of the attack key, and over that, we write what comes next. alice's whirl hits all around her, so it hits bob, and not carol, who's right beside her too, and carol's whirl hits neither of them. They wait two seconds, for the swings to finish, and each of them uses the whirl, which is skill one, and a second later, bob is down twenty-five, while alice and carol have their hundred. Last, a duel ends at one health, and nobody dies, so this time, bob joins with ten health. The duel starts as before, and alice picks him and swings. Over her swing, we write that her fifteen would leave bob nothing, so it leaves him one, and he's lost. We wait a second for the blow to land, and take bob from the world's players, by reference. His health is one, and not zero, he isn't dead, and his duel is over, as duel of says he has none, so the world ended it. Then we take the notices, and there are two. The first is for bob, player two, who hears that alice won, and the second is for alice, who hears that she won against bob. Her pick of him is gone too, as the duel's over. It's over now, so her swings go at the air, and four seconds of them later, his health stays at one, as the duel left it.
30:04 We run all the duels' tests at this step, without the databases again. The build comes first, as the world's code has changed, and so has its library. All eight pass: duellists hit each other and nobody else, and a duel ends at one health.
Over the network
30:19 Next, the duels go over the network, starting in the world's header file, where call off duel ends the player's duel, however far it's got, with nobody winning, as when the world hands its players over to its next instance. It returns who the duel was with, or zero. In the world, it ends the player's duel in the duels, and returns what end gives back. The server runs a few copies of a land, so in the copies' header file, take duel news gives the players whose duel has news in any of them, as one set. In copies, take duel news starts from an empty set, goes through each copy, by reference, and merges each copy's news into it, as it does for trades, which moves the ids across without copying them. In the server's header file, the server gets Duel, for a player's duel action, next to Trade, and send duels, for each player whose duel was asked for, counted down, started or ended. Each player gets a token bucket for their duel actions too. In the server, the duels get a limit of their own, a burst of three. Over it, we write why: an ask for a duel is a line in another player's chat, so a player can ask or answer three times at once, and then once a second, as for a guild. The refill is one a second, on the token bucket. Without it, a modified client could fill someone's chat with asks in no time. An input with a duel goes to Duel as soon as it arrives, like a trade. A player gets their bucket as they say hello, and it goes as they leave. Duel starts with two refusals, before it asks the world anything. While the server hands its players over, nothing duels, and over it, we write that the next instance has none of this one's duels. Past the limit, the player hears that they're asking for duels too fast. Then we take the world of the copy the player's in, and a switch on the action's case calls the world. An ask goes to ask to duel, with the name, a yes goes to answer duel with true, a no goes there with false, and a yield goes to yield, while an action without anything in it returns. Whatever the world refused, the player hears why, in a line from nobody. Then hand over, after the save, gets a line that says the duel's off, as the world moves to its next instance. Over it, we write why: the next instance has none of this one's duels, so each is off, and both sides hear so before they're told to go. For each session, the world calls the player's duel off, and if there was one, both sides get the line. Then the duels go out straight away, with nobody in them, before the players are told where to go, and in the tick, after the trades, as each tick's news goes out once the world has stepped. Send duels goes through the players with news, in all the copies, as take duel news gathers them. Over it, we write that a duel goes like a trade, on its own, over TCP, to each side, and with nobody in it once it's over. For each one, we find their session and the world they're in, and a player who's gone is skipped. Each one's duel goes in a World message of its own, which is framed, counted in the bytes we've sent, and sent on their session. And in the bot, a World message with a duel is passed over, as a guild's is, so the bot doesn't take a duel's news for a piece of the world. We note over it that a bot doesn't say yes to a duel either.
33:31 Now, a quick test that two players duel over the network, with the real server, in the server's tests. The test player gets somewhere to keep a duel, an optional that's empty until the server sends one, as we write over it. Each World message with a duel in it is kept as its duel, next to its trade. The test makes a server of its own, called arena. Over its content, we write that each joins with a sword in hand, which Space swings. Its items are the test sword, and its skills the test's strike, and nothing more, so a blow does fifteen. The arena is made the way the fixture makes its server, on a port of its own, with fifty-millisecond ticks and the fixture's databases, and we assert that it started. alice and bob connect to it, each with a token for their account, as any client would, and we run the event loop until each sees the other in the world. In is a lambda that says whether a player has a duel from the server yet, and whether it's in a given state, which the waits below use. Over the ask, we write that alice asks bob, by his name, and he says yes. She sends an input whose duel asks for bob's name, and we run until she's asking and he's asked. His duel names alice as his opponent, and his last notice is the one that says how to say yes. Then he says yes, with an input whose duel accepts, which he sends. Over the waits, we write that both hear it count down, and start. We run until alice is counting down, from three thousand milliseconds at most, as a tick may have gone by, and then until both are fighting, with bob's id as alice's opponent. Over the swing, we write that she picks him and swings, and he sees her blow land on him. Her input attacks, with bob's id as the player she picked, and she sends it, as a client sends its keys. We run the loop until one of the events bob's test player keeps is a hurt event, on him, and of fifteen, which is her blow landing. Then he yields, and so she's won it. His input's duel yields, and he sends it to the server. Then we run until both duels are back to none, alice has at least one notice, and her last notice is the one we expect, that she won the duel against bob, while bob's says that alice won. Last, she sends the ask again, and over it, we write that she asks again, and leaves, which calls it off. We run until bob's asked, and then she leaves the game, which closes her connection to the server. We run until his duel is none, with a last notice that she's gone, so the duel's off. The second test hands the players over in the middle of an ask. It makes the next instance, a second server on a port of its own, with the fixture's content, made the way the fixture makes its own, with fifty-millisecond ticks, and asserts that it started. Then alice and bob connect to the first server, the fixture's own, each with their token, and we run until they see each other, as before, with a new input for the ask. Then alice asks bob, as before, and we run until bob has a duel, whatever its state. That's the moment we care about, with an ask in the air as the world moves. Then the first server hands its two players over to the next, and returns two, as it hands over both. Over that, we write that the next instance has none of this one's duels, so both hear it's off before they're told to move. We run until both are told to move, to the next instance. The last notice each of them has says the duel's off, as the world moves to its next instance, and both their duels have nobody in them.
36:59 We run both at this step, against MySQL and Redis in Compose, as the players' tokens and characters live there, with each test's server on a port of its own. The build comes first, as the server's code has changed again. Two players duel over the network, and a hand over calls a duel off, so both pass.
On screen
37:19 On the client, a duel starts in the chat, where the chat window's header file gets duel line, which turns a line into a duel action, when it starts with slash duel and a space. Over it, we write what each one does: yes or no to the one who asked, yield, or with a name, asking that player. Any other line is nothing, so it goes on to be said. In the chat window, we bring in string view, as duel line looks at the line without copying it. The command is slash duel and a space, as a constexpr string view, and a line that doesn't start with it is nothing, which C++20's starts with tells us. What comes after the command is what to do, as a view into the line. Yes accepts, no declines, yield yields, each of them setting one field of the oneof, and anything else is a name, to ask, copied into the action as a string. So a player who calls themselves yes can't be challenged from the chat, which I can live with.
38:16 The duel gets a window on the client, in a new header file, duel window, which brings in c std int, optional and our protocol. Countdown seconds turns the server's milliseconds into whole seconds to show, rounded up, so that it says one until the duel starts, and not zero for the whole of the last second. A duel window is a duel on the screen. Over it, we write what that is: a question when someone asks you, with Duel and Not now, the countdown's seconds in the middle of the screen, and the word fight, as it starts. Who won goes in the chat, from the server. The word fight stays up for a second, as it fades, and an answer hides the question for a second while the server hears it. If it's still asking after that, as when it turned a yes down, the question's back, so a refused yes can be tried again. Build takes the duel the server sent, and over it, we write that it draws what the duel needs now, and returns the player's answer, once. It keeps the state it last saw, when the duel started, by ImGui's clock, and when we answered the ask that's up. Both start far enough back that nothing shows at first. In duel window, we bring in ImGui, c float, for the largest float, c std int, optional, string, and our theme, for its fonts and colours. Countdown seconds adds nine hundred and ninety-nine before it divides by a thousand, which is integer division that rounds up. Build starts by noticing a new state, which forgets an answer, and keeps the time the fight started. Over the screen's size, we write where the countdown goes: big, in gold, over the world and under the windows, as You died is. Shout is a lambda that draws words that way. It takes the title font, at a hundred and ten points at a scale of one, and measures the words at that size, so that it can centre them. Then it draws them on the background draw list, centred across the screen, a little over a fifth of the way down. They go from light gold to dark, faded as much as it's asked, with a shadow, the way the theme draws its gradient text. Then it works out how long the fight's been on, from the time it started, by ImGui's clock. During the countdown, it shouts the seconds the server has left, rounded up, at full strength. Once the fight's on, for its first second, it shouts the word fight, fading as the second goes. The question is up while you're asked, unless you answered in the last second. It's centred, thirty percent of the way down, in one of our panels, titled Duel, with a question mark, which fits its size to what's in it. It says who asks you to duel, by the name in the duel, and Duel, our main button, in the colours of a fire, a hundred and fifty points wide, says yes. Not now says no, and so does closing it. Over it, we write why: so that someone else can ask either of you. An answer is kept, with the time, and returned. And in the client's CMake file, the duel window goes into the client's core library, and its test into the client's tests.
41:18 Next, picking the player we're duelling, as you'd pick a monster, starting in the picking header file, which brings in c std int, for the ids. Pick monster takes the opponent's id, which is zero outside a duel, and gives the nearest living monster along the ray, or in a duel, the opponent, whom the player picks as they would a monster. Picked is new, and over it, we write that it says whether a figure is the one the player picked: the monster, by its id, or in a duel, the player, by theirs, as each has ids of their own. The ring, the frame, the hotbar and the fx all need that answer, so it's written once, here. In picking, we bring in c std int. Pick monster's signature takes the opponent's id, and its test lets the opponent through too, alive, as it does a monster, by asking picked about each figure. Picked asks first whether the figure is a monster, and checks a monster against the target, as long as the target isn't zero. Then it checks a player against the target player, leaving out NPCs, so neither monster twelve nor NPC twelve passes for player twelve. In the animator's header file, a figure gets its target player, next to its target, which, while they use a skill, is the monster they picked, or in a duel, the player. The animator copies it from the player the server sent, as it does the target, so the fx know who a skill's for. In the player frame's header file, draw target frame takes the target player too, which is zero unless it's given. It draws the monster you picked, or in a duel, the player, with their name, level and health, at the top in the middle, as we write over it. In the player frame, we bring in picking, and the frame finds the figure with picked, in place of looking for a monster with that id. A frame for nobody returns before it draws anything. Its icon is a skull for a monster, and someone for the player you're duelling, as we write over it. Font Awesome's user icon is a little person, which reads well at that size. In the hotbar's header file, build takes the target player as well. Over it, we write that a skill that hits one is red while nobody's picked, as the server runs the player up to whoever they picked, however far. That's as true of an opponent as it is of a monster. In the hotbar, we bring in picking, and the lambda that says whether something's picked now asks picked, with both targets, whether there's someone alive to use a skill on. Last, in fx, which draws each skill's sparks, slashes and glows, we bring in picking. A cast lands where the figure it was aimed at stands, and with picked, that's the opponent too, from their target player. So a thrust at your opponent sparks on them, on everyone's screen, as the server sends who it's for. Picked does that work in five places now, the pick among them, which is the kind of thing I like to see.
44:11 Then the renderer, which draws the world, and whose header file gets the target player for the ring. Over set target, we write that the monster the player picked, or in a duel, the player, gets a ring at their feet. It takes the player too, which is zero unless it's given, so the code that sets a monster stays as it is, and it keeps them both, the monster in target, and the player in target player. Set flag gives it where a duel's flag is, while there's one. Over it, we write what that draws: a pole, with a ring at its foot, and a ring as far out as the duellists can go. It keeps the player, next to the monster, and the flag, which is nothing outside a duel. In the renderer, we bring in picking, for picked, and the duels' header from the server, for the flag's reach, which the client and the server share. The flag is gold, and over it, we write what the flag is: a pole a little taller than a player, in gold, with its furthest ring fainter, as it's there to be seen from inside. So the far ring is a fainter gold, and the pole is twelve centimetres thick and two point four metres tall. Draw ring drew thirty-two sides, and a ring twenty-five metres out would get sides of about five metres, which cut through any bump in the ground. So a wide ring gets more sides, a metre or so long each, so that it follows the ground, and we note that over it. With a flag, the pole is drawn from the spire the scenery uses, a square stone that comes to a point, thinned down to a pole and raised by half its height, as the model sits around its middle. The red ring goes round whichever figure is picked, monster or player. And with a flag, a gold ring goes at its foot, and a faint one twenty-five metres out, from the server's own constant, so the client and the server can't disagree about where the edge is.
45:56 The duel reaches the game through the link, the interface from the browser episodes that both kinds of connection, and offline play, have in common. It gets duel, our duel, with nobody in it for none, the way the trade is. In the inbox's header file, where we now list our duel among what the inbox files, the inbox hands the duel out, and keeps one. In the inbox, which reads each message the server sends, a World message with a duel is filed as our duel, as it has nothing of the world in it, and over the list, we now name our duel too. A duel replaces the one we had, as each message carries the whole of it. The connection hands out its inbox's duel, as does the WebSocket link, so a browser gets duels too, and the local connection keeps one that stays empty. Offline, where there's nobody else, an input with a duel gets a notice that a duel takes another player. Then main, which brings in the duel window, and makes one, next to the trade window. Next to the monster we picked, main keeps the player, while we're duelling them, and we add that over it. A reconnect lets go of it, like the monster, and it goes out with each input. A new land lets go of it too, as what we picked was the last land's. After the monster that's gone is let go of, main reads our duel from the connection, and we write over it that this is for the player, while we're duelling them. Our opponent is the duel's, while it's fighting, and nobody otherwise, and a player we chose who isn't that one any more is let go of. So once a duel's over, the ring and the frame go. A skill's sound plays as aimed at someone when it's for a player too. Over the click, we now write that a click on a monster, or in a duel on our opponent, picks it, and one on neither lets go of what we'd picked. So pick monster gets the id of whoever we're fighting, and what it picked goes into one of the two, as a monster or a player, with the other set back to zero. Escape, once the windows are closed, lets go of whichever we picked, and opens the menu after that. The renderer gets both, for the ring, and from the countdown on, the flag, as we write over it. It goes at the height of the ground where it stands, as the server sends where it is on the map, and nothing about how high. The target frame and the hotbar get the target player too, so the frame shows the opponent, and the skills light up for them. A line from the chat with a duel in it goes as the input's duel, in place of being said. And the duel window's answer goes in an input of its own, a copy of the keys we last sent, as the trade window's does.
48:32 Now, a few quick tests of the client's side. In the chat window's tests, we bring in string view, for a test that slash duel asks or answers. Duel is a lambda that gives the action a line makes, or an empty one. Slash duel bob asks bob, yes accepts, no declines, and yield yields, while a line without the slash, or slash duelling, without the space, is no duel at all. The duel window gets a test file of its own, which checks that it counts down in whole seconds, rounded up. Three thousand milliseconds is three, two thousand and one is still three, and two thousand is two. And as we write over it, it says one until the duel starts, a millisecond before it too, while zero is zero. In the picking tests, which cast a ray into the figures, as a click does, there's a test that the opponent in a duel is picked as a monster is, with three figures. Over the figures, we write that two players stand ahead, seven at four metres and eight at six, and there's a dead monster with the further player's id, as players and monsters have ids of their own. The ray starts half a metre up, and looks straight ahead. Out of a duel, the ray picks nobody, as neither player is a monster, and the monster is dead. With eight as the opponent, it picks player eight, past player seven, who's nearer. And picked tells monster eight from player eight, both ways. Last, in the inbox's test, which files a message of each kind, a World message with a duel in it, against bob, goes in with the others, and comes out as our duel.
50:02 We run those four at this step, without the databases, as they're about the client alone. The build comes first, as the client has a new window and new tests. Slash duel goes where it should, the countdown rounds up, the opponent is picked as a monster, and the inbox files our duel, so all four pass.
Duelling
50:21 Let's duel in the game, at this step, with gwen a few metres ahead of us. She's level eight, two levels up on us, which is going to matter in a moment. Her ask comes in as a line in the chat, and as a question in the middle of the screen, with Duel and Not now. Duel sends the server a yes, and it counts three seconds down on both of our screens, telling us as each one goes, with the flag at gwen's feet, as she asked. Once it says fight, a click on gwen picks her, as it would a monster, and her frame comes up at the top, with someone in place of the skull. With Space held, the server runs us up to her and swings, as it would at a skeleton, and our thrust's sparks land on her, as the server sends her as our target player while we use it. She was swinging back all along, and each blow was a hurt event, so the damage they did floated up over whoever took it. With two levels on us, gwen hits harder, and we're the ones who run out. The blow that would have left us nothing leaves us one, as our frame at the top left says, and the chat says that gwen won the duel. Losing a duel in my own game stings a bit, though I'm still standing, on one health, with all my gear.
51:37 This time, the duel is ours to ask for, from the chat. Slash duel and gwen's name, sent as the input's duel, and she says yes, so the flag goes down at our feet, as we asked. Zoomed out, there's the gold pole and its ring, and the faint ring twenty-five metres out, which is as far as either of us may go. And this time, gwen runs off, away from the flag. The referee looks at both sides after each tick, once everyone's moved, and when she's past that faint ring, she's gone too far from the flag, so she's lost, and the chat says we won the duel against her. There's no prize for it, but after the last one, I'll take it.
Writing it down
52:22 Last, the game's docs, starting with the gameplay page, which gets a section on duels. It says how one's asked for and answered, the flag and the countdown, picking and hitting, and what ends a duel. It also says that after a duel, health comes back from a potion, a skill or a new level, as after a fight, so the next duel starts with what the last one left. The networking page gets a duel's messages, in a table, and a paragraph on what I think is the open problem here. The server hits a duellist where it has them as the blow lands, while our client draws them a hundred milliseconds in the past, as it draws everyone else. So against a player who's moving, a blow that looked like a miss can land, and the other way round. Making up for that is called lag compensation, and it's a problem I find really interesting. And the choices we made get decision record thirty-three. Players fight each other in duels, which end at one health. Its context is that, until now, a blow was a player's on a monster, or a monster's on a player, and that a player's id and a monster's can be one number. The decision is a duel that both say yes to, a blow that leaves one health, and none back at the end, and the player picked beside the monster. Then come the alternatives we turned down, each with its reason. A duel to the death would send the loser back to the middle of the land at full health, so losing would heal them. Giving both sides their health back at the end would make a duel a free heal, for anyone with a friend who'll yield. One target for both, with a rule about which numbers are players, is a rule both ends of the wire have to keep. And players hurting each other anywhere comes with a lot of rules of its own, and it's for later, if the game ever gets it. What it costs is two targets on the client, and the lag we've just seen on the networking page. It goes in the index too, as the thirty-third row.
Wrap-up
54:16 Today, players got duels, where slash duel and a name asks, and a yes, typed or clicked, puts the flag down and starts a countdown of three seconds, which the server sends both sides as it goes. A duellist picks the other in a field of their own, beside the monster, which the world keeps for the opponent and nobody else. The blow that would leave them without health leaves them one, and they've lost, as has anyone who goes more than twenty-five metres from the flag. The duel's news goes to both over TCP, after the tick, and the client draws the countdown, the flag, and the ring at the opponent's feet. Decision record thirty-three says why a duel ends at one health. And what's still open is the hundred milliseconds between where we see an opponent, and where the server has them.
Playing it
55:51 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.