Client-side prediction for our MMORPG, in C++. On a network that netem slows to a 150 ms round trip, our player started and stopped walking late, as each key went to the server and back. The client now sends its keys each server tick, numbered, and the server applies them one a tick, catching up when some arrive late, and says which it applied last, with your place, which also puts the round trip on screen. The server's walking moves into a library the client links, built so that a step rounds alike on both, and the client walks you at once, puts you where the server has you after the last input it applied, walks you again through the rest, and fades what moved. Where the server moves you on its own, along a walk you asked for, up to a monster or with a skill, the client follows the server. This is episode 68 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
Topics: Networking
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep068/end.
Transcript
Intro
0:00 Welcome back, and this is episode sixty-eight, where we're going to make our own player move the moment a key goes down, on a slow network too, with what's called client-side prediction. I think it's one of the nicest tricks in game networking, as the client walks ahead of the server, and the server still has the last word. Before we build it, let's see where it's going.
0:21 Both of these are on a network where each packet waits seventy-five milliseconds, so a round trip takes a hundred and fifty. On the left is the game as we left it, where arthur waits for the server before starting to walk, and again before stopping. On the right, with today's code, arthur moves the moment a key goes down, and the server's answers correct the client behind the scenes, where they disagree.
0:43 If you want to follow along, the series' website has a link to the code on GitHub, which is open source, and a page for each episode, with the code at each of its steps.
Last time
0:52 Now, before today's work, a quick look back at the last two episodes, which built guilds. A guild lives in MySQL, with a transaction for each change, as it changes for players other than the one who acts. An invitation waits a minute in Redis, and slash g talks to the guild alone. Then the guild window got a roster, the highest rank first, with whoever's playing now in white. The leader makes officers, a higher rank can remove a lower one, and the leader can end the guild for everyone in it.
Today
1:23 Here's the game as it stands, with netem, which we used in episode nine, on the network between the client and the servers, so that each packet waits seventy-five milliseconds on its way. Keep an eye on the keys in the corner, and on arthur. When a key goes down, the client sends it to the server, which moves us on its next tick, and the world it sends back takes another seventy-five milliseconds to arrive. So arthur starts a moment late, and stops a moment late too. And the client eases us towards each place the server sends, like the other players, which adds to the feeling of walking through mud.
1:57 So here's where the time goes, from a key to a step on screen. A key goes down, and the input takes seventy-five milliseconds to reach the server. It waits up to fifty more for the server's next tick, and the world comes back over another seventy-five, before we see the step on screen. Most other games hide this for your own player, and I think that's the right call, as you feel your own keys far more than anyone else's movement. So first, we number each input, so that the server can say which one it's applied. Then the server's walking goes into a library that the client links too, so the client can walk us at once, and correct us with the server's answer. Last, while the server moves us on its own, the client follows the server and leaves our keys out of it. Right, let's start by numbering the inputs, from the protocol up.
Numbered inputs
2:46 This episode's code is version zero point sixty-eight, so the top CMake file moves the project on. In our proto file, the input gets a sequence, field sixteen, which numbers a player's inputs from one. The client is going to send one for each of the server's ticks, with whatever keys are down at that moment, and the server applies them in order, one a tick. A bot leaves it at zero, and its keys go in at once, as before. Then the world gets acked, field twenty-two, the sequence of the last of your inputs that the server has applied. In the server's header file, we bring in deque, the standard library's double-ended queue, as inputs go in at one end and come out at the other. ApplyInputs takes each player's next numbered input, before the world steps. And the server keeps two maps, by player: the inputs that haven't been applied yet, oldest first, and the sequence of the last one that has. A third map holds Steady, a small struct with the fewest inputs a player had waiting at the start of a tick, and over how many ticks, for draining a queue that stays full, which we'll come to. Limits comes in for the largest size, which the fewest starts at, so that any real count is fewer. In the server, deque comes in at the top as well, and algorithm, for min. Most queued is five, the most inputs a player can have waiting. A client sends an input a tick, so a few of them pile up when the network holds some back and then lets them through together. Past five, the oldest goes, so that a player doesn't drift further and further behind their own keys, as a queue that kept growing would add its length to each round trip. Most behind is three, the most waiting before we skip one. A client sends one a tick, but a slow frame on the client sends two or three together, so up to three waiting is a client a little ahead of our tick. More than that means some came late and we've fallen behind the client, so each tick skips one until we've caught up. But inputs come in as fast as they go out, so once a burst has filled the queue, it stays that full. Each input then waits a tick for each one ahead of it, for as long as the player plays. So most steady is two, and steady ticks twenty, a second's worth. If two or more were waiting at the start of each tick for a second, the queue's holding more than it needs, and it drops one, until one waiting is the most it keeps for long. Where an input arrives, a numbered one waits for its tick, so that the client can tell which tick each of its inputs moved it in. It goes on the back of that player's queue, which the map makes the first time, and once there are more than five, the one at the front goes. An input without a number goes in at once, as before, unless this player has a queue, which means their client numbers its inputs. For them, an input without a number is a copy of the last keys, sent along with a line of chat or a skill, and applying it would move them out of turn. A respawn, a skill, a walk to a place and the rest still go in at once, further down. When a player leaves, their queue, its fewest and their last acked go with them, like their rate limits. Each tick, ApplyInputs runs first, before the worlds step. It goes through the players with a queue. First it notes the fewest waiting at the start of this tick, even when there are none. Once twenty ticks have gone by, the queue drains one if even the fewest was two or more, and the count starts again. Then it skips an empty queue, or a player whose world is gone. When more than three are waiting, or the queue drains, it drops the oldest first. Draining needs two waiting, so one is left to apply. A skipped input counts as applied, and the client corrects the step it walked for it. I'd take that small correction, as applying each late input in turn would add a tick to each round trip after it. Then it takes the input at the front, sets their keys and their pick from it, notes its sequence as acked, and pops it, so the next tick gets the next one. And when the world goes out, each player's own part of it says which of their inputs was applied last, once there's been one. Their own place goes in that part too, so it's in each datagram we send them. The world can come in several parts, and whichever part has the player, their place and the input it follows now always arrive together. Their name, level, most health and weapon are cleared, as those come with the full world once a second, while the place changes each tick.
7:09 The world can come over UDP in parts, which Parts puts back together, so in parts' header file, it keeps acked, like the other fields that are ours alone. In parts, each part leaves room for the fields besides the players and the monsters. Those are the part's time and number, and the fields that are ours alone, such as our experience, the land's seed, our cooldowns and the skills we know. Acked joins them, and as its field number is over fifteen, it takes two bytes, plus up to five for its value, so those fields' room grows from fifty-five bytes to sixty-two. And our own player, without its name, takes up to sixty more. Add takes acked from each part, like the respawn before it, and the world it hands back carries it. Now the client, where a new header file, round trip, holds what we measure with those numbers. Its guard and namespace are as in our other headers, and we bring in c standard int, and map, for the time each input went out. RoundTrip measures how long the server takes to apply an input of ours and tell us. It's the time from sending a numbered input to the world that says it was applied, which is the delay a player feels, so it's the one we put on screen. Sent notes that we sent the input of a sequence, and the time it went, in seconds, as raylib's clock gives it. Acked takes the last sequence a world says the server applied, and the time we read it, which is up to a frame after it came. And milliseconds gives the round trip, smoothed over the last few, or zero before there's one. Inside, the send times go in a map by sequence, next to the milliseconds. Below the class, DrawRoundTrip puts the round trip in the top right corner, right-aligned under the minimap, from the top it's given. It returns how tall it is, for whatever goes under it. In round trip, we bring in ImGui, cfloat, for FLT_MAX, which tells the text measuring there's no limit on the width, cmath, for rounding, iterator, for std next, string, Abseil's StrCat, and our theme. The smoothing is a tenth: each new round trip moves the one shown a tenth of the way towards it, so that the number on screen doesn't jump with each tick. Sent puts the time in the map, under its sequence. Acked looks the sequence up, and a sequence we've measured already, or one we didn't send, is left alone, so it's measured once, however many worlds repeat it. The measured round trip is now, less the time it went, times a thousand for milliseconds. The first one is taken as it is, and after that, each one moves the average a tenth of the way. Then the input and all the ones before it come out of the map, with an erase from the beginning to the one after it. The server applies inputs in order, so an older one won't be acked on its own any more, and the map stays small. DrawRoundTrip draws nothing before the first measurement, and returns no height. Otherwise, the text is the milliseconds, rounded to a whole number, with ms after them. It's in our bold face, at thirteen points, scaled with the interface. We measure it, so that it can end twenty-four points in from the screen's right edge, six below the top it's given. It goes on ImGui's background list, under any window, in the muted colour, with a shadow, and it returns its height plus those six points. The client's CMake file builds round trip into the client's core, and its test into the client's tests. In the client's main, we bring in round trip. The input tick is fifty milliseconds, the server's tick, twenty a second, which is how often our keys go out now. Next to what we sent, the sequence counts our numbered inputs, the input clock gathers the frames' time until a tick's gone by, and the round trip measures. The keys used to go out when they changed, and now they go into sent each frame: the direction, run, attack, and the monster we picked. The input clock adds the frame's time, held at three ticks. The keys go out once each of the server's ticks, numbered, so that the server applies them in order, a tick each, and says which one it's applied. A slow frame sends three at most, as that's as many as the server lets wait before it skips one. For each whole tick on the clock, we take a tick off it, and a copy of the keys goes out with the next sequence. The round trip notes when it went, by raylib's GetTime. Then, each frame, the world's acked goes to the round trip, with that frame's time. And at the bottom, the quest tracker, which sat under the minimap, now goes under the round trip, which DrawRoundTrip draws there first.
11:43 Next, let's pin the server's side down with a test, in the server's tests, which bring in set, for the acked numbers we see. The test player gets SendTogether, next to its Send. It takes a few inputs, and adds each one, framed with its size in front, to one shared string of bytes. Then it writes the whole string with asio's async write, in one write, so that they reach the server together. The test logs alice in, with a token for her, and waits until she sees herself in the world, so that her inputs reach a player who's in one. Then it makes a vector of three inputs, as a network that held them back would let them through. A loop numbers them from one to three, as the client would number three ticks in a row, and each of them walks along x, as if a key were held down. Then they go out together, with SendTogether, in a single write. Each world she gets adds its acked to a set of the ones seen. RunUntil keeps the test's event loop going, world after world, until one says three. If the server applied all three as they came, the worlds would go straight from nothing to three. So the set has to hold one and two as well, which says each tick took a single input, in order. Three waiting is within the most behind, so the server skips none of them. The next test is for the drain that keeps the queue short. Alice logs in with a token, as in the test before, and the test waits until she sees herself in the world, so that her inputs reach a player who's in one. This time, a slow frame sends three at once, and then the client keeps up, with one for each world, so two wait behind each new one until the queue drains them. A vector holds the three inputs, which a loop numbers one to three, as before, without keys down, as standing still is enough here. Each input gets its number and nothing more, as the queue is what's under test, and not the walking. Then the three go out together, in one write. The test keeps the last sequence it sent, three so far, and the time stamp of the last world it saw. Each time RunUntil turns the event loop, the test waits for a world with a new time stamp, and notes it. If that world applied the newest input we sent before it, the queue's level, and the test is done. Otherwise it numbers the next input and sends it, one for each world. As inputs come in as fast as they go out, without the drain those two would go on waiting, and RunUntil would run out of time. With it, the queue drops one a second, twice, and it's level in about two seconds, well within RunUntil's five. Its twin checks that an honest client loses nothing. Alice logs in again, and this time sends one input for each world from the start, with nothing sent together. The test starts its sequence at zero, keeps the time stamp of the last world to compare with, and puts each acked number it sees in a set. RunUntil turns the event loop as before, until enough worlds have come. Each turn, a world with a new time stamp adds its acked number, before the next input goes. It stops after forty inputs, which is two seconds' worth of ticks. With one waiting at the start of each tick, the fewest stays under two, so nothing drains. A loop then checks that each of the first thirty-nine was applied, with the number in the message if one wasn't. A second test checks that the server catches up with a client. Alice logs in again, and this time five inputs go out together, numbered one to five, without any keys down, as standing still is enough for this. Five waiting is more than three, so the first tick skips the oldest and applies the second, and the next three ticks apply the rest in order. So acked reaches five in four ticks, and none of the worlds says one was applied. In parts' test, we check that the client keeps the newest acked number. MakeWorld, the helper the other tests in the file use, makes each world. A world for one player, with a time stamp of one, says acked seven, and goes in first. Then a newer world, stamped two, says eight, and the assembled world says eight too, as it takes acked from the newest world. And the round trip's test, in a file of its own, is from sending an input to hearing it applied. A new round trip starts at zero, as nothing's been measured. Input one goes out at ten seconds, and input two fifty milliseconds later. When one is acked at ten point one five seconds, the round trip is a hundred and fifty milliseconds. That's the first measurement, so it's taken as it is. Two is acked at ten point three, two hundred and fifty milliseconds after it went, which moves the average a tenth of the way, to a hundred and sixty. And two acked again changes nothing, as it's gone from the map by then.
16:27 We build the tests at this step and run the six, the server's four against MySQL and Redis in Compose, as its player logs in for real. The double ampersand runs each binary once the one before it has passed. The server takes three inputs over three ticks, in order, drains a queue that a burst left full, and leaves alone one that keeps up. It catches up with five by skipping the oldest, and the parts and the round trip keep what they should, so all six pass.
16:55 Still on our slow network, where each packet waits seventy-five milliseconds, the client now has a round trip to show. Under the minimap, the round trip comes out at around four hundred milliseconds here, well over the network's hundred and fifty milliseconds. An input also waits in the server's queue, and for its tick, and the answer waits for the client's next frame, which comes slowly in this recording. Walking still starts and stops late, as the client doesn't walk us with the numbers yet.
One walk for both
17:25 For the client to walk us before the server answers, it has to walk us the way the server will, to the centimetre, or each world would pull us back a little. So the server's walking moves into a new header file, walking, which needs the terrain, for what's walkable and where the land's edge is. The speeds come along, in tiles a second, where a tile is a metre: one point eight for walking, which is the walk clip's speed, and three point one for running. They're inline constexpr, so the program has one of each, wherever the header's included, which C++17 allows for variables as well as functions. Walk moves to a place, or if the terrain's in the way there, as far as it can along x or along z alone, so that a player slides along a coast instead of stopping dead. WalkAlong is what a tick of keys does. It walks from x and z for that many seconds, along the direction, at the walk's speed or the run's, inside the land's edge and on land. The server walks a player with it each tick, and the client is going to walk us with it before the server answers, so the two have to agree. In walking, we bring in algorithm, for std clamp. Walk is the function from the world, moved over as it was. It takes the place if it's walkable, or else the step along x alone, or else the step along z alone, and stays put if none of them is. WalkAlong picks the speed from run, and takes the land's edge from the terrain. Then it walks to where the direction takes us in those seconds, at that speed, clamped inside the edge along x, and along z. In the server's CMake file, walking becomes a library of its own, nanommo walking, like the bag's rules in episode sixty-four, so that the client can link it without the rest of the server. It's a small library, two functions and two speeds, so linking it costs the client next to nothing. It finds its headers from the project's root, and links the terrain, which it walks on. And it's built with dash f f p contract equals off, like the terrain, so that a step comes out alike on any machine. Without it, the compiler may fuse a multiply and an add into one instruction, which rounds once where the code says twice. Then a client on a Mac and the server on Linux could end a step a hair apart, and each world would correct us by that hair. The server's core links the library, and the server's tests get walking's test. In the world's header file, we bring in walking, and the declaration of Walk goes, as walking has it now. The world's walk speed and run speed stay, as the monsters walk at them too, and now they're walking's, so the two can't drift apart. In the world, the Walk function goes, and Go hands its step to WalkAlong, with the tick's time in seconds, so the server's players walk through the library. And the client's core links nanommo walking, next to the bag's rules.
20:20 Now, a quick test of a walk, in walking test, with walking's header file and the terrain's. A default Terrain is flat, which keeps the numbers simple. A player starts at the origin, and walking along x for half a second takes them the walk speed times a half. Then running along minus z for a second takes them the run speed the other way. Then a player starts a tenth of a metre from the land's edge, and runs towards it for a second, which would take them three metres past it. They stop at the edge, where the clamp holds them, so a prediction can't walk us off the land either.
20:58 We build the tests at this step, and run walking's two, along with all of the world's, as its players walk through the library now. None of them needs MySQL or Redis, so dash dash no deps starts the test container without them. A tick's walk goes the right distance and stops at the edge, and the world's tests pass as they did, so moving the code didn't change how the server's players walk.
Walking at once
21:22 Here's the idea, before we get to the code of the prediction. Each tick, the client sends an input, say number forty-three, and walks us with it straight away, with the server's step. It keeps the input until the server says it's applied it. A world comes back, which has us at some place after input forty-one, as forty-two and forty-three are still on their way. So the client puts us there, and walks us again through forty-two and forty-three. When the server agrees with us, we end up where we already were, and nothing moves. When it doesn't, the server wins, and we fade from where we were drawn to the new place, most of the way in a tenth of a second, so that we don't jump.
22:03 The prediction goes in a header file of its own, prediction, with its guard and our namespace. We bring in cstddef, for size t, which pending counts in, c standard int, deque, for the inputs still on their way, our protocol, for the world, and the terrain, which a walk needs. A Prediction is where we are before the server says so. Each tick's keys walk us at once, as the server will when they reach it. Each world from the server puts us where the server has us, after the last input it applied, and walks us again through the ones it hasn't applied yet. Walk takes the terrain to walk on, the sequence the keys went with, a direction, whether we run, and the tick's length in seconds. The direction is zero on both axes for standing still. Correct takes where the server has us, after the input it acked, and how long each of its ticks is, for walking the rest a second time. A small difference from where we had ourselves goes away over a moment, and a large one, such as going through a gate, at once. Settle fades what the last corrections moved, by the frame's time, so it runs each frame. Then what the rest of the client reads. X and z are where the prediction has us, and shown x and shown z are where to draw us, with what's left of a correction still added on. Placed says whether the server has placed us yet, as until the first world comes, we don't know where we are. And pending counts the inputs sent and not acked yet, which the tests read. Inside, Keys is a tick of input: its sequence, its direction along x and z, and run. The pending ones go in a deque, oldest first. Then come where the prediction has us, the error still to fade along each axis, and placed. Last, Predicted is a free function, which takes the world as the server sent it, and hands back a copy with us where the prediction draws us. We walk, run or stand as our keys say, facing the way they go, unless the server has us doing something else, such as a swing.
24:07 In prediction, we bring in cmath, for the distance and the fade, cstddef, for size t, and walking's header file from the server, for WalkAlong. A correction of two metres or more is a jump, such as through a gate, and it isn't faded, as watching yourself glide across a land would look broken. The fade's rate is twenty a second, which takes most of a correction away in a tenth of a second. And at most a hundred inputs wait, which is five seconds of ticks at twenty a second. A server that hasn't acked any for that long, say while a world hands its players over to another, isn't going to, and they'd pile up. Walk adds the keys to the back of the list, as a Keys with their sequence, their direction and whether we run. Once there are more than the most pending, the oldest goes from the front. Then, once we've been placed, it walks us along the direction with WalkAlong, the server's step. Before that, the client doesn't know where to walk us from. Correct starts by dropping the inputs the server has applied, which is acked and the ones before it. It remembers where we had ourselves, and puts us where the server has us. Then it walks us again through each input still pending, in order, a tick each, so we end up where those inputs will take us once the server has them too. If we'd been placed, and the new place is less than a jump away, the difference between where we were and where we are now goes into the error, on top of whatever was still fading. So the drawing stays where it was a moment ago, and fades towards the new place. A jump, or the first placing, clears the error, as there's nothing to fade from, and we're placed from then on. Predicted starts from a copy of the world, which it can change, and before we're placed, it hands the copy back as it is, as we don't know where we are yet. Otherwise, it goes through the players, skipping everyone but us, and puts us where the prediction draws us. If the server has us idle, walking or running, we're drawn moving as our keys say: idle without a direction, running with Shift, and walking otherwise. With a direction, we face along the keys, worked out the way the server works out a yaw, from the arc tangent of x and z. A swing, a skill or death keeps the server's motion, as the keys don't decide those. Settle multiplies the error along both axes by e to the minus twenty times the frame's seconds. So how much fades depends on the time that's passed, however fast the frames come. And the client's CMake file builds prediction into the core, and its test into the client's tests.
26:35 In the client's main, we bring in prediction. Next to the round trip, the client keeps the prediction, the time stamp of the last world it corrected us with, and this frame's keys, with whether we run, for drawing us walking as they say. The copy of the keys we number and send is called numbered now, as keys is taken by the frame's keys. As each tick's input goes out, it walks us straight away, as the server will once the input gets there, as long as there's a land to walk on. It walks with the direction and run we're sending, for one input tick. After the loop, keys and run keep this frame's direction, for the drawing. Then, in the world, each new world from the server, by its time stamp, corrects us once, and not on each frame it's drawn. We find ourselves among its players, and Correct puts us where the server has us, after the input it acked, and walks us again through the rest. Settle fades a frame's worth of the correction, each frame, whether a world came or not. So when the server has us a little way off, we slide there over a moment, instead of jumping. The animator now gets the world with us patched in by Predicted, with this frame's keys and whether we run, so that our legs follow them, and we face the way they go. Otherwise we'd slide along standing still for a round trip, until a world from the server said we were walking. The animator eases each figure towards where the server last had it, which is what makes the others move smoothly, but for us, it would add a delay back. So once we're placed, our figure goes where the prediction has us, without the easing.
28:08 Now, a test for each thing the prediction does, in prediction test. It brings in gtest, walking's header file, for the speeds, and the terrain, as each test walks on a flat one. The tick is fifty milliseconds, like the server's. First, the server places us at the origin, with nothing acked. Then a tick walking along x moves us a tick's walk at once, the walk speed times the tick, and leaves that input pending, until the server says it's applied it. The second test starts with the server placing us at the origin too, with nothing acked. Then a loop sends three ticks of walking along x, numbered one to three, and each of them walks us a tick's walk at once. Then the server applies the first, and has us where we'd have been after it, a tick along. Walking the other two again, we're three ticks along, with two inputs pending. The shown place is where we are, so there's nothing to fade, as the server agreed with us. Most worlds go this way, and if a correction moved us at all, we'd see a stutter each time a world came. In the third, we see what happens when the server disagrees with us. The server places us at the origin, and a loop walks us three ticks along x again, the way the second test did, each tick numbered and pending. But something held us back on the server for the first tick, so its world applies the first with us still at the origin. We're put two ticks along, as the server's answer says, walking the two it hasn't applied. We're drawn three ticks along at first, though, where we were a moment ago, and the difference of a tick is the error to fade. After a second of settling, the drawing is where the prediction is, within a millionth of a metre. The last test is a jump, which isn't faded. The server places us at the origin, and then its next world has us forty metres away, as a gate would. That correction is drawn there at once, without a fade.
30:01 We build the client's tests at this step, and run the prediction's four, which need no servers at all. A server that agrees moves nothing, a disagreement fades away, and a jump doesn't, so all four pass.
30:15 With the prediction in, we go back to that slow network, with each packet still held back seventy-five milliseconds. Now the client walks us as each tick's keys go out, so the step shows as the key goes down, and it stops as the key comes up. The round trip under the minimap hasn't changed, and the server still decides where we are. When it agrees with the prediction, its answer moves nothing.
What the server steers
30:38 The prediction has a catch, though, which shows up when the server moves us on its own, away from our keys. With a skeleton picked a few metres ahead, thrust has the server run us up to it, as it has since episode fifty-one. Our keys are still, so the prediction stands us still, and each world drags us towards the skeleton, so arthur glides there without taking a step. The prediction knows our keys and nothing of the server moving us on its own, so a walk we click on the map goes wrong like this as well. And a key held while a skill carries us forward would walk us somewhere the server then pulls us back from.
31:13 So the server has to tell the client when it's moving us on its own. In our proto file, the world gets steered, field twenty-three. It says whether the server moved you this tick, along a walk you asked for, up to a monster or with a skill, or you're dead, so that your keys didn't move you. In the world's header file, steered goes on each player, a flag the world sets as it moves them. In the world, Act sets it at the start of each tick, to whether they're in a skill, whose strides move them, or dead, when nothing does. Follow, which walks a player along their way, sets it, and so does Approach, which runs them up to a monster. The server puts the flag in each player's own part of the world, next to acked. In parts' header file, Parts keeps it, like acked. In parts, the room each part leaves for the fields that are ours alone grows by three bytes. Steered joins acked and the others in the list over the room, and as its field number is over fifteen, it takes two bytes, plus one for a bool, so that part of the room is sixty-five bytes now. Add takes it from each part, along with acked, and the world it hands back carries it, so the client reads it from the newest world.
32:26 In prediction's header file, Predicted changes, so that while the server steers us, we move as the server says, whatever our keys do. In prediction, once our place is patched in, a steered world skips the part that draws us moving as our keys say. So arthur runs, swings or lies there as the server says, facing the way the server's yaw points. And in the client's main, our keys stop walking the prediction while the server steers us. As each tick's input goes out, we read whether the last world said we're steered. The last world is the newest we've had, so it's about half a round trip old. When the server starts steering us while a key's held, the client finds out about a round trip late, and the fade covers the difference. While the last world says we're steered, the input walks the prediction without a direction, so the server's answers alone move us. Our keys still go to the server, which decides when the steering ends, as keys call off a walk we asked for.
33:26 Next, a quick test in the world's tests that it says when it moves a player. A player joins, and walks along x with their keys for a step, and they aren't steered, as the keys moved them. Then the keys are let go, and a walk is asked for, to four, three. The next step is the world's, along the way it found, and steered is set. And in prediction's test, the server places us at one, two. We make a world of our own, where we're player seven and the server steers us. Our player in it runs, at a yaw of half a radian, as the server would have us run up to a monster. Predicted, with our keys still, draws us running, at that yaw, at the prediction's place. Without the steering, it would stand us idle, as our keys say. Then the test turns the steering off, and those still keys stand us idle, as they did before this step.
34:22 We run the world's new test, and all of the prediction's, at this step, again without the databases. The world flags the steps it takes for a player, a steered world draws us as the server says, and they all pass.
34:36 With steered in the world, we try that skeleton again, a few metres ahead of us. Once it's picked, the server runs us up to it, and arthur runs there on screen too, as the client draws the server's motion while it steers us, until the skeleton is in reach and the thrust lands.
Losing packets
34:52 Last, a worse network, which holds each packet back like before, and drops five percent of them as well. Our inputs go over TCP, so a lost one is sent again, and the ones behind it wait, then reach the server together. The queue on the server holds them in order, and hands them out one a tick. When more than three are waiting, the server skips the oldest, a tick at a time, until it's caught up. So a lost packet costs us a small correction, and not a round trip that stays longer. While the server waited, it kept us going with the last keys it had applied, so its answer can disagree with us now and then, and the fade covers that. And the round trip under the minimap barely moves, as a lost input holds up the ones behind it for a moment, and each new round trip moves the one shown a tenth of the way.
Wrap-up
35:40 So, today, each input got a number, one a tick, and the server applies them in order, skipping the oldest when it falls behind, and says which it applied last, which gives us the round trip under the minimap. The server's walking went into a library the client links, built so that a step rounds alike on both. The client walks us the moment our keys go out, and each world puts us where the server has us and walks us again through the rest, with a fade for what moved. And where the server moves us on its own, the client follows the server instead of our keys.
Playing it
37:04 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.