Our MMORPG's skills move the way we write them now, not the way a downloaded clip does. A skill's move is a few keys in the content, each turning a handful of joints at a time into the skill, with an ease that makes a strike snap and a wind-up gather. The raylib client turns the bones on top of the standing clip, each joint taking the bones below it round, and works out the GPU's skinning matrices again. Then come the fx: a trail where the sword has just been, posed again at moments just gone, glows, rings along the ground, sparks, motes and a camera shake, each worked out from the time since the skill started, so it looks alike at any frame rate. The C++ server sends the monster a skill lands on, and the swing on Space becomes Strike, a skill every player has, with the weapon's reach, damage and timing. This is episode 53 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
Topics: Graphics
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep053/end.
Transcript
Intro
0:00 Welcome back to episode fifty-three, where we're going to animate our skills ourselves, with moves we write as keyframes, and give them fx: trails, glows, rings of light and sparks. First, a quick look at what we'll have by the end.
0:15 By the end of the episode, each swing and skill is a move we wrote, with a trail where the sword has just been. The thrust lunges, with sparks where it lands, and the whirlwind spins twice, with a ring of light along the ground and sparks as it lands.
0:30 There's more about the project on its website. The code is open source, on GitHub, linked from there, and each episode has a page of its own, with the code at each of its steps.
Last time
0:40 A quick reminder of where we left off, first. The server split each land into squares of a metre, to walk on or not, and found a way across them with A star. A player can ask to walk somewhere, and the server walks them there, around what's in the way. The client got a map on M, and a minimap in the corner, and the monsters' camps spread out over the land.
Today
1:03 Our skills play clips from Mixamo, which are captured from real people, and they look good. However, their files can't go in the repo, so each one is another download, and a new skill needs a clip that happens to fit it. Most other games reuse a handful of moves, and tell their skills apart with effects, so that's the way we're going to go. First, a skill's move becomes a few keyframes in the content, which the client plays on the skeleton. Then the fx, starting with a trail where the sword has been, and glows. Then rings along the ground, sparks, motes of light and a shake of the camera, around the player or on the monster the skill lands on. And the swing on Space becomes a skill too, which everyone has. So, let's get going, with the content, where a move is written.
Moves of our own
1:52 In our skills proto file, a move is going to turn the joints of the skeleton, so we start with which joints there are. The content can't hold a pointer to a bone, so a move names its joints, and the client finds them. Joint is an enum of thirteen of them, from the hips and the spine, through the chest and the head, to the arms, the forearms, the right hand that holds the sword, and the legs and the knees. Those are enough for a lunge, a kick or a jump, and the fingers and the feet can stay as the clip under the move has them. Each of them is a bone of our characters. A turn says how far a joint turns, as three angles in degrees: the pitch, the yaw and the roll. Those are about the player's own axes, so a move looks alike whichever way the player faces. Each one turns the way your fingers curl with your right thumb along its axis. So a positive pitch, about the axis out to the player's left, bows the chest forward, and swings a hanging arm back. Angles are easier to write by hand than quaternions, and they can go past half a turn, which a spin is going to need. Ease is how the joints get from one key to the next. Smooth starts slowly and ends slowly, while snap starts fast and settles in, which is what makes a strike feel like a strike, and gather starts slowly and speeds up into the next key, for a wind-up. A key is a pose at a time into the skill, in milliseconds, with its ease and its turns. And two more numbers move the whole body: how far it lifts, in metres, for a jump, and how far it goes ahead, for a lunge. And a move is a list of keys. It starts from the clip under it, which is the idle, and ends on its last key, which is why that key is usually the clip again, with no turns at all. The look loses its clip, field two, and we reserve its number and its name, so that an old file or an old client can't read something new into them. Its place goes to the move, field four, and a skill without one keeps the player standing.
3:51 The client plays the moves in a file of its own, moves, starting with its header file. It needs raylib, for its vectors and quaternions, array from the standard library, and the skills' generated header, for the move. A move's turns arrive from the content as numbers, and the client looks them up in the rig, by name. It's an array of thirteen C strings, the names Mixamo uses. A string literal lives as long as the program does, so a pointer to each one is all the array needs to hold. Mixamo names each bone with a prefix, mixamorig and a colon, which our converted models keep. K joint bones lists them in the enum's order, so that a joint's number is its place in the array. It's inline constexpr, so it's worked out at compile time, and a header can define it without each file that includes it getting a copy of its own. Joints is where a model's bones are, by joint, or minus one for a joint the model doesn't have, such as on the skeleton, whose rig is Quaternius's. A bend is a move at one moment: a quaternion for each joint, and a shift of the whole body, in metres. A quaternion is how raylib keeps a rotation, and two of them multiply into one turn after another. Find joints looks a model's bones up, once, as it loads. Bend at gives the move at a time, between the keys either side of it. And apply turns a model's bones by a bend, after a clip has posed them. A metre is how many of the model's units make a metre, as the lift and the lunge are in metres. In moves, we need raymath, raylib's maths, c string, for comparing the bones' names, and vector. Eased turns how far we are between two keys, from zero to one, into how far the joints have got. Snap is a cubic that leaves fast and slows down into the key, gather is t cubed, which starts slowly and arrives fast, and smooth is the smoothstep, slow at both ends. Any other value of the enum, from a newer file, gets the smoothstep too. The cubics are cheap, with no powers or square roots, as they run once for each figure using a move, each frame. Angles makes an array of three angles per joint from a key, with zeroes wherever the key doesn't say, which leaves the clip's pose for that joint. A joint's number comes from the file, so we check it's one we know, as a newer file could have more. To matrix makes a transform into a matrix, as raylib's own update does for each bone, which we're about to repeat. It multiplies the scale by the rotation, then by the translation, so a bone is sized, turned, then moved. Find joints starts each joint at minus one, for no bone, until it finds one. Two loops, over thirteen joints and sixty-five bones, find each joint's bone by its name, once per character, as it loads, so a plain search is fine. Until the first key, from is an empty key, at zero milliseconds, which leaves the clip as it is. Bend at walks the keys, in order, to the first one that's still ahead of the time. Once we find the key ahead, t is how far we've got towards it since the key before, eased by the key's ease. Two keys at one time make t one, which saves a division by zero, and we stop looking. Each key we pass becomes the last one behind us, and after the last key, t stays at one, so the move holds its last pose. Before is the key we're coming from, and after the one we're going to, each laid out by joint. Then for each joint, we blend its angles from one key to the next, in degrees, and then make them a quaternion. Blending the angles, not the quaternions, is what lets a key turn the hips seven hundred and twenty degrees, two turns round, as a quaternion's blend takes the shortest way, and would hardly move. The shift blends the lift and the lunge alike. Apply puts a bend on a model's bones, after its clip. It starts from the model's bone count, and a turn for each bone, with no turn at all for most of them. Then it spreads the joints' turns over their bones, skipping a joint the model doesn't have. raylib keeps the clip's pose of each bone in current pose, in the model's own space, so we copy it before we change it. Then we go through the bones in order. The bones come parent first in our files, as raylib expects, so when we reach a bone, its parent has been placed already, with the turns of all the joints above. The root bone, the hips, moves by the shift, in the model's units. Any other bone stays as far from its parent as the clip had it, turned the way its parent's been turned, so a turn of the chest takes the arms and the head round with it. And a bone's own turn adds to its parent's, for the bones below. Its rotation is the clip's, turned by all of that. Last, raylib skins the mesh on the GPU, with a matrix per bone, which takes it from its bind pose to its pose now. So we work those out again, from the bind pose and the new pose, as raylib does after a clip.
8:47 In the characters' header file, we include moves, and learn now keeps a skill's move, for the players who use it. Each character gets the bones its moves turn, in place of the skills' clips. Pose for no longer needs the skill, as a skill has no clip of its own any more. Pose as is going to pose the character's bones as the figure is: its clips, blended, and then the move of the skill it's using. For finds the character a figure is drawn with, and the characters keep each skill's move, by the skill's id. In the characters, each character finds its joints as it loads. Learn keeps the move of a skill that has one. Pose for plays the idle while the player uses a skill, and the move goes on top of it. A skill's move is shared by both characters, so it's kept once, by the skill's id. For is the lookup draw used to do: an NPC goes by its kind, and anyone else by their id. Pose as starts the way draw did: the clip the figure is coming from and the one it's going to, each at its own time, blended by how far the figure has got from one to the other. The blend runs from zero to one over a quarter of a second after each change of motion. Then, if the figure is using a skill with a move, it applies the move at the time it's been at it, in milliseconds. A model holds pointers to its bones, so the copy we pass to apply turns the character's own bones. A metre is one over the character's scale, as the scale takes the model's units to metres. Pose as is const, as posing changes the model's bones, not the characters. In draw, the posing it did comes out, and a call to pose as goes in, so what the figure wears and holds moves with the bones, as before. And in the client's CMake file, the core gets moves, and the tests get a file for them.
10:39 Now the moves themselves, in the skills file. The thrust is going to be quick, so it holds the player for one point four seconds, down from three, and lands at five hundred and twenty milliseconds. Its first key, three hundred milliseconds in, pulls back: the body sinks a little and goes back ten centimetres, and the chest turns thirty-five degrees to the right. The sword arm goes back with its elbow bent, while the left arm comes forward. The next key is the lunge, a hundred and eighty milliseconds later, with snap, so it leaves fast. The body goes forty-five centimetres ahead and lower, the chest leans into it, and the arm goes straight out. The hand turns too, as raising the arm forward tips the blade up, and a thrust wants it pointing ahead. The front leg steps forward with its knee bent, and the back one goes back. Two hundred and eighty milliseconds later, the third key holds the lunge, a little looser, with the arm and the legs a few degrees back. So the pose eases out instead of freezing, and goes back to the clip by one point three seconds. The kick takes one point two seconds now, as Mixamo's took over two, which is a long time to stand on one leg, and it lands at four hundred and twenty milliseconds. Its first key, at two hundred and twenty milliseconds, leans the chest back ten degrees. It bends the right leg up, with the knee at a hundred degrees, which is the chamber a kick starts from. The arms go out as the leg comes up, the left one out to its side and the right one a little, so the body has something to balance with. Then, with snap, comes twelve centimetres of lunge, so the foot reaches the monster, with the chest further back, the leg straight out to ninety degrees, and the arms a little further out. At six hundred and fifty, the leg folds back in, and the kick is back at the clip by one point oh five seconds. The kick has no turn of the hand, as the sword stays down at the side while the leg does the work. Second wind keeps its glow, and takes a little longer now, one point two seconds, up from one, while the player catches their breath. It lifts the player five centimetres, tilts the chest and the head back, and swings both arms out and up from the sides. The right arm's roll is minus fifty and the left's is fifty, as a roll turns the two arms opposite ways. It opens a little further by eight hundred milliseconds, and lets go by one point one five seconds. The whirlwind is quicker, one point eight seconds, down from two and a half, and it shows why the angles are blended in degrees. It winds up by turning the hips fifty degrees to the left, and the chest a little further. The sword arm goes out to the side, its hand turned so the blade points out along it, and the knees bend. Then the hips turn the other way, to minus six hundred and ninety, which is nearly two turns round, in a little under a second. The left arm goes out the other way, for balance, as it would on someone spinning. It ends on minus seven hundred and twenty, which is where it started, as a pose. The hips are the root bone, so a turn of the hips turns the whole body, about the point between the legs. Its arm stays out to the side through the spin, which is what sweeps a ring. In the skills' header file, parse skills is going to refuse two more mistakes in a file. A move whose keys go back in time is a mistake in the file, so parse skills refuses it, with the skill's name. And so is a move that runs on after its skill is over, as the server has let the player go by then, and they'd be walking off mid-kick. And in the build of the characters, three clips go: the thrust slash, the kicking and the spin, so that's three fewer files to download from Mixamo.
14:25 Next, a few quick tests of the moves. They include moves, raylib and raymath for the maths, array, and GoogleTest, which our tests have used from the start. Bow makes a key that bows the chest forward by a pitch, in degrees, at a time, with an ease, smooth unless we say otherwise. It sets the key's time and ease, then adds a turn to it, of the chest, by the pitch, leaving the yaw and the roll at zero, as protobuf's numbers start at zero. And pitch reads back how far a bend bows the chest, in degrees, from its quaternion. raymath's quaternion to Euler gives the three angles in radians, and pitch is the first of them, about x. The first test has a single key, forty degrees at two hundred milliseconds. At zero, the move is the clip's own pose, so nothing's turned. Halfway, a smooth ease has got halfway, to twenty degrees, and long after the key, it holds forty. The second pins down what snap is for, with two moves of one key each, sixty degrees at a hundred milliseconds, one smooth and one snapping. A quarter of the way to the key, snap has turned further than smooth, which is what makes a lunge feel sudden. The third builds a model by hand, with three bones, a metre apart up the y axis: the hips, the chest and the head. Bone info is raylib's struct for a bone, with its name and its parent's index. Bones, poses and matrices reach raylib as pointers to arrays, so the test makes the arrays on the stack: the bones, where they were bound, where they are, and their skinning matrices. Each bone's parent is the one before, with minus one for the root, and each is bound a metre above the one before, and starts where it was bound. Then the model's skeleton, pose and matrices point at those arrays. raylib needs no window for this, as it's all maths on the CPU. The chest is bone one, and the move bows it ninety degrees, at a hundred milliseconds. We apply it right on that key, so the chest has bowed all ninety. Then the chest stays where it is, and the head has gone round it, forward along z, to a metre up and a metre ahead. And the head's skinning matrix moves its bind position to that place too, which is what the GPU draws. Vector3 transform moves the point two metres up, where the head was bound, through that matrix, and the test expects it a metre up and a metre ahead. In the content's tests, each skill is written inline, in the text format, with its effect and its look. The first is a kick with a cast time of nine hundred milliseconds, whose keys go forward, at two hundred and then nine hundred, so it ends as the kick does, and it parses. In the second, they're out of order, five hundred and then two hundred, so its move goes back in time, and parsing refuses it, with a message that says so. The third has its last key at a thousand milliseconds, a tenth of a second past the kick's cast time of nine hundred. It's refused too, with a message of its own, as its move would run on after the skill is over. Each message names the skill and what's wrong with its move, so that a typo in the skills file says where to look.
17:32 We build the tests at this step, in Docker, and run the moves' tests and the content's test of them. The keys blend as they should, and a turn of the chest takes the head round with it.
In the game
17:45 Let's see the moves in the game, at this step. Nothing new goes over the network for them. The server sends each player's motion as a skill, with the skill's id, as before, and each client works out the pose from how long the player's been at it. So the moves come out alike on each player's screen, while the server has no code for joints at all. The whirlwind spins twice in a little under a second, and second wind still has its glow, around the move.
Trails and glows
18:13 Now the fx, which is short for effects: what a skill shows besides its move. In our skills proto file, an fx has a kind, and we start with two of them. A trail is a ribbon of light where the blade has just been, and a glow is a ball of light, like second wind's. Place is where an fx is: at the player's feet, or at the tip of their blade, where it is now, so that a glow can follow the sword. Each one starts a number of milliseconds into the skill, and lasts for a number of them, so the fx line up with the move's keys. Its colour is red, green, blue and alpha, written as one hex number, the way the look's glow was, and its size is in metres. The server parses these fields with the rest of the file and leaves them alone, as fx are the client's part. The look reserves the glow's number and name too, as the glow becomes an fx, and a skill can have as many fx as it likes, in field five. In the skills file, the thrust gets a trail, from three hundred and thirty milliseconds, as the lunge starts, for two hundred, in a pale blue, for cold steel. Second wind's glow becomes a glow round the player's middle, in green, for a little over a second. And the whirlwind's trail lasts the nine hundred milliseconds of the spin, in orange.
19:32 A trail needs to know where the blade is, which the characters work out as they draw the sword. In their header file, we include optional and utility, for the pair. Blade gives the two ends of a figure's sword in the world, or nothing for a figure without one. Optional says there may be no blade, without a pointer or a made-up value, and the pair holds its two ends. It's const, as finding the blade changes nothing a caller could see. Placed is a matrix draw already works out, where the figure stands, turned and at its scale, as a function of its own now, so that blade can use it too. In hand is the other, for the item in the fist, from the hand's bone and the item's grip. They're static, as they read nothing of the class but the arguments they're given. A weapon gets the two ends of its blade, in the model's own units. Hold loads each item's model once, as the content's items are read, and keeps it with its grip. In hold, the blade's two ends come from the sword's box: the middle of it across, from just past the hand to the top. The hilt end is one and a half grips up from the bottom, so the trail starts at the blade, not at the fist. The weapon is made with designated initialisers, which name each field as it's set, so a field can't end up in the wrong place. Placed is the matrix draw made, from the character's scale, the figure's turn and its place, multiplied in that order, as raylib's matrices apply from the left. The trail needs the blade in the world, not in the hand, so both matrices go into finding it. And in hand is where the held item goes, as the fist is now. It takes the hand's bone as the clip and the move have posed it, and makes that into a matrix, from its scale, its rotation and its translation. Then it puts the grip before it, as draw did. Blade first looks up the weapon the figure holds, by its id, among the ones we've loaded. It returns nothing when the figure holds nothing we have a model for, or its character has no hand. Otherwise, it poses the character as the figure is. The blade's ends are in the sword model's own units, so they go through the matrices the sword is drawn with. The grip comes first, which scales the model and puts it in the fist, then the hand's bone, as the clip and the move left it, and last, where the figure stands. Vector3 transform takes the hilt and the tip through it, and the pair goes back. Draw uses placed and in hand, and draws as before.
22:04 The fx get a file of their own, fx, starting with its header file. It needs the animator, for the figures, the atmosphere, for the fog, the characters, for the blade, and the skills. An fx depends on nothing but the time since its skill started, so two clients that drew different numbers of frames agree on what it looks like. The constructor takes the skills, and it's explicit, so a skill table can't turn into an fx by accident. It loads a shader, so like the other classes that own something on the GPU, it can't be copied. Update watches for players using skills, and holds on to each one until its last fx has finished, as a trail fades on after the blade has stopped. Draw light draws them, from where the camera is, faded into the atmosphere's fog, with the characters to find the blades. Casts is a count of them, for the tests. A cast is a skill someone's using, as the fx see it: the figure of the player, kept from the last frame they used it, and the seconds since it started. The fx keep the skills, when each skill's fx are over, the casts by the player's id, and the shader they draw with. In fx, we include raymath, and rlgl, raylib's lower layer, which lets us hand the GPU triangles a vertex at a time, along with algorithm, array, cmath, utility and vector. The light gets a pair of shaders of its own. The vertex shader passes each vertex's colour on, and its depth, for the fog. Light gives off its own, so it needs none of the sun's shading, and its shaders are short. Each shader is a raw string literal, which starts with R and a bracket and keeps the text as it is, newlines and all, so it reads as GLSL. The fragment shader sends the colour out three times as bright, past white. Our frame keeps colours past white, and the bloom spreads them, so the light glows, and the fog fades it with distance. A trail shows the last sixteen hundredths of a second of the blade's path. Up is the y axis, and a ball of light goes at about someone's middle, ninety centimetres up. Ease out is a cubic that slows down into its end, for things that grow. Hot takes a colour towards white as it gets hotter, as the brightest part of a flame or a spark is nearly white. Vertex hands rlgl a corner, with a colour and how opaque it is, scaled by the colour's own alpha. rlgl batches the corners we hand it, and sends them to the GPU together, so a few thousand of them a frame are fine. Quad hands rlgl six corners, each with its own colour and alpha. A and b run along one side, and c and d along the other, and the corners a and d go in twice, as the diagonal its two triangles share. A ribbon or a ring is built from them, a piece at a time. Blob draws a fan of twenty triangles round a middle point, and a fading band around it. Forward is the way the camera looks, from its position to its target. Its right is the cross product of forward and up, and its up is the cross product back, so the two stay square to each other, and flat to the screen from any angle. Edge finds a point on the circle round the middle, a twentieth of a turn for each side, out along right and up by a share of the radius. The middle is hot and nearly opaque, and a ring a third of the way out is about half opaque, before the outside fades to nothing at the edge. Each side is a triangle from the middle to that ring, and a quad from the ring out to the edge. The outside drops away quickly, and the core is what the bloom spreads, so it reads as light, and not as a see-through disc over the grass. The constructor keeps the skills, and loads the light's shader. Then, for each skill, it works out when its fx are over, from the last one to end. A trail's length goes on top of each, as a trail lingers once the swing is done. The ends are in seconds, as the casts count in seconds, which is what the frame time comes in. Update first moves each cast on by the seconds. Then, for each player using a skill we know, it keeps the figure, and the time they've been at it, which comes from the animator, and a cast that's over goes. Monsters and NPCs use no skills, so they're left out. The animator's to time starts again when a player's motion changes, so it counts from the moment the skill started on our screen. A moment is one fx of a cast, as far as it's got: where it is, its age and how long it lasts, in seconds, and its colour. Done is how far through it we are, from zero to one. Draw trail shows where the blade has been. The obvious way is to keep the blade's positions from frame to frame. However, at thirty frames a second, a fifth of a second's lunge is six frames, so the ribbon would be six straight pieces, and at nine frames a second, on a slow machine, two. As the move is worked out from time, we can work it out again for moments before now, which makes a smooth curve at any frame rate. The newest end of the trail is now, or where the trail stops, and the oldest is sixteen hundredths of a second before, or where it starts. If there's nothing between them, there's no trail to draw. We pose a copy of the figure seventeen times over that stretch, for sixteen pieces, and ask the characters where the blade was each time. Posing a skeleton is matrix maths over sixty-five bones, with no vertices to touch, so seventeen poses a frame cost very little. Each pose fades with its age, squared, so the trail's tail goes quickly. Each pair of poses makes one piece of the ribbon, between the blade where it was and where it was a moment later. The middle is sixty percent of the way from the hilt to the tip, and next middle is that point on the next pose, a moment later. Each piece of the ribbon is two quads. The first runs from the hilt, where it's clear, to the middle, where it's half there. The second runs on from the middle to the point, where it's full, and hot. Each pose's fade scales its edge of the piece, and heats its tip. So the newest end, right behind the blade, is the brightest and the whitest part of the ribbon, and the oldest is clear. A glow reaches its size in the first third of its time, and dims with the square of what's left, lifted to the middle of the player unless it's at the blade. The glow's radius starts at half its size and grows to all of it, while it fades in over the first sixth of its time. Lifted is false at the blade, as the blade's tip is already where that glow belongs. Draw light does nothing when there are no casts to draw. Otherwise it gives the light shader the fog, finding each setting in the shader by its name. Uniforms are a shader's settings, which stay set until we change them, so setting the fog once a frame is enough. Then it flushes what raylib has batched so far, as we're about to change how things are drawn. The light comes after the sea and the shadows, so it lies over all of them. A ribbon is seen from either face, so back faces aren't culled, and the light writes no depth, so whatever is behind it still draws. We blend the light with alpha, over what's already drawn. Added on top, as light usually is, our orange would wash out to yellow over the grass, while blending keeps its colour, and the bloom still sees it past white. Then, for each cast, we look up the skill the player is using, by the figure's skill id, and go through the skill's fx in turn. Each works out its start and its length in seconds, from its milliseconds, and how long it's been going. Its colour comes from the hex number, which raylib's get colour reads as red, green, blue and alpha. A trail goes straight to draw trail, with the figure to pose. Anything else waits for its time, and stops once it's over. It goes at the player's feet, or at their blade, where its tip is now, found by posing a copy of the figure all in the skill's move, as the trail does. If the figure holds nothing, it stays at the feet. A moment holds all of that, ready to draw. The switch draws it by its kind, between a begin and an end of rlgl's triangles. For now there's a glow, and the other kinds come next. Last, we put the drawing back the way it was.
30:21 The renderer owns the fx, so in its header file, we include them, and animate advances them by the frame's seconds, from the current figures. The glows' ball and their colours go, and the renderer keeps the fx behind a unique pointer, like the sea. In the renderer, the glow's constants, the emission it set, and the sphere go too. The constructor makes the fx from the content's skills, and its loop over the skills no longer collects glows. At the end of draw, in place of the glows, the fx's light draws at the very end, after the sea. In the client's main, the fx move on each frame, by the frame's time, right after the figures. And the CMake file builds fx, and a test file for it.
31:07 Now, a quick test that a skill's fx outlast the skill. It includes the fx, the window, the animator for the figure, the skills, and the status or from Abseil, which is how a window comes back from create. The test first opens a window, as the renderer's tests do, a hidden one. The fx load a shader, which needs a window to make the GPU's context. Assert true stops the test there if the window can't open, as the rest would crash without it. Parse skills reads its skill from text, like the content's tests do. Its skill holds the player for half a second, and has a glow from two hundred milliseconds, for eight hundred. The skill parses, which the assert checks before the test goes on. A figure is the animator's view of a player, so the test makes one by hand, with an id, a motion that's a skill, and the skill's id. Update takes the figures and the seconds since the last frame, as the renderer's animate does. Then the player goes back to idle, with their skill set to none, and after six tenths of a second, the glow's still going, so the cast is kept. Another six tenths, and the cast is gone, with its glow.
32:19 We build the tests at this step and run the fx's test under xvfb run, which gives it a virtual display to open its window on. The glow outlasts the skill, and goes once it's over.
32:32 Let's see the trails in the game, at this step. Again, nothing new goes over the network for them. Each client poses the sword again at the moments just gone, and joins them into the ribbon, so the whirlwind's trail is a smooth ring around the player, at whatever frame rate the client draws. And second wind's glow is a patch of light facing the camera now, with a hot middle, in place of the tinted ball.
Rings, sparks, a shake
32:58 Sparks where a thrust lands need to know where it lands, on each player's screen, and the other players' clients aren't sent which monster we picked. So in our proto file, a player gets the monster they picked, by its id, as field thirteen, while they use a skill, so that everyone sees it land there. The rest of the time, it stays zero, which protobuf doesn't send at all, so it costs nothing while nobody's using a skill. In the server, the player's target goes with their skill, as the world keeps it from their input. In the animator's header file, a figure keeps the monster a player picked while they use a skill, which the animator copies from the player, along with what they hold and wear.
33:37 In the skills' proto file, the fx get four more kinds. A ring is a wave along the ground, spreading out to the fx's size, sparks fly out and fall, motes are specks of light that rise and swirl, and a shake shakes the camera, by the size in metres. An fx can go at the feet of the monster the player picked now, and without one, it's left out, as sparks that fly off the empty air would look odd. And sparks and motes come in a count. In the skills file, the thrust gets orange sparks and a flash at the monster it hits, twenty milliseconds before the server lands the hit, so they arrive together on screen. The kick gets a pale ring of dust at the monster's feet, a flash, and a small shake of six centimetres, as the foot lands. Second wind gets a green ring spreading out from the player, and forty motes rising around them, and the whirlwind gets a ring out to three metres, a little past its reach, and sparks as it lands.
34:34 In the fx's header file, we include the terrain, as a ring lies on the ground, and draw light takes it. Shake gives the camera's offset, right now, for the blows near it. A cast keeps an optional position for the monster, and a seed, which the counter after it hands out, so that two casts don't throw identical sparks. In fx, sparks fall, at nine point eight metres a second, squared. Each spark needs a few random numbers: how long it lives, which way it flies and how fast. The standard library's generators keep a state, which moves on with each number, so the sparks would have to be made once and kept. Instead, random mixes four numbers into one: the cast's seed, the fx's place in the skill, the spark's number, and what we're asking for. It multiplies each by a large odd constant, and mixes the bits with shifts, so that numbers next to each other give results far apart. The top twenty-four bits make a float from zero to one. So a spark gets its numbers anew each frame, and they come out alike each time, which is what lets the fx be worked out from the time alone. The constants are xxHash's primes, and the shifts and multiplies at the end are a well-known mixer, which spreads each bit of the input over all of the output. Streak is how a spark is drawn on the screen. It takes the two ends, a thickness, a colour and an alpha, and the eye's position. Its side is the cross product of a to b and the way to the eye, which is square to both, and two quads fade out from the centre to the edges. The side reaches half the thickness out, so the two halves together make the streak as thick as we asked. A spark is a few pixels wide on screen, and a streak that faces the camera keeps its width from any angle. Around takes the terrain, a point, how far to go from it, and which way, as an angle. It gives back the spot that far away in that direction, at the terrain's height there, plus a little above, so the terrain right under it doesn't cover it. The cast is keyed by the player's id, so a player has one cast at a time. In update, try emplace finds the player's cast, or makes it, and says which. A fresh cast gets a new seed from the counter, and so does another skill, or a skill started again, which we can tell as its time went back. A new cast also starts with no target, for a player who's picked nothing, or a monster that isn't among the figures we know. Then, each frame, we look for the monster the player picked, among the figures, and keep where it is, so the sparks follow a monster that's still moving. Once a hit kills it, the client lets go of its pick, so it isn't found any more, and the cast keeps where it fell, for the rest of its fx to land on. A moment gets the cast's seed, and the fx's place in its skill's list, as its index. So a moment's random needs no more than the spark's number and what we want, and a skill with two lots of sparks gets two different bursts. A ring has forty-eight pieces round it, and a rim ten centimetres wide. Done is how far through the ring is, from zero to one, and all of the ring follows from that number. Its radius grows out to its size, quickly at first, with ease out, and its band gets thinner, and fades, as it goes. The wall is how high a band on the rim stands, and the rim's colour starts hot, and cools as the ring spreads. Each piece goes from one angle to the next, a forty-eighth of a turn round. At is a lambda for a point at a distance and an angle from the ring's middle, five centimetres up, so the ring lies on slopes. Each piece has two quads lying flat, side by side. The first goes from clear on the inside to a third of the colour's strength, at the inner edge of the rim. The second is solid over the rim, and hot, so the ring has a bright edge and a soft trail. Seen from above and behind, a flat ring is thin, so it gets a short upright band at its edge, which thins out as it rises, and reads as a wave. Each spark lives for between half its fx's time and all of it, and starts within the first tenth. It flies up at between fifteen and seventy-five degrees, any way round, fast enough to cover about the fx's size, and gravity pulls it down. Rise is the angle up from the ground, and turn the angle round, so together they give a direction in three dimensions. Where is a lambda, which captures the spark's direction and speed by reference, and gives its position at any age, so the head and the tail of its streak come from one formula. With its life, its direction and its speed, a spark's whole flight is a formula of its age, a straight line pulled down by gravity, so there's nothing to update from one frame to the next. A spark that's gone under the ground is left out. Each one is drawn as a streak from where it was a twentieth of a second ago to where it is, so a fast spark is long, and it gets thinner and cooler as it ages. A mote starts within the first half of its fx, lives for half of it, and spins one way or the other round the player, at two to four radians a second. Its turn round the player starts at a random angle, and its distance out from the middle shrinks as it ages. It rises to the fx's size as it goes, and fades in and out on a sine. Motes are blobs a few centimetres across, which the bloom turns into specks of light. Each mote's spin and drift come from its own random numbers, so the swirl looks loose, not like a wheel. In draw light, an fx at the target is left out without one, and goes at the monster's feet with one. For a killing blow, that's where the monster fell, as update kept it, so the sparks still land on the body. Anything not at the blade goes on the ground, at its height. The moment's fields are named as they're set, so the seed and the index, both whole numbers, can't swap places by mistake. Then the switch draws a ring with draw ring, sparks with draw sparks, and motes with draw motes. The renderer asks for the shake once a frame, before it draws, with the camera's position. Shakes from several casts add up, so two blows at once shake harder than one. Shake adds up the shakes of the casts near the camera. It goes through each cast, and each fx of the skill the cast's player is using, working out how long it's been going and how long it lasts. It leaves out any other kind, and any that isn't going on now. Each shake starts when its fx does, and goes on for its time, so a heavy blow can shake for half a second and a kick for a fifth. Distance is from the camera to the player who used the skill, so a blow across the field barely moves it. A shake counts in full up to fifteen metres from the camera, and not at all beyond forty, and fades with the square of what's left of it. It moves the camera along three sines, at different speeds on each axis, so it jitters without settling into a pattern. In the renderer, the camera moves by the shake: we copy it, move it and what it looks at by the shake, and draw with the copy, so the camera the client follows the player with stays put. And the skills' light gets the terrain, for the rings.
41:38 Let's see them in the game, at this step. The server now sends the monster a thrust goes for, with the skill, and each client puts the sparks at that monster's feet. A kick throws no sparks, but its ring and its glow land at the monster's feet too, and it shakes the camera of each client within forty metres, fully within fifteen. The whirlwind's ring spreads a little past its reach, and second wind's motes rise around the player as the health comes back.
Strike on Space
42:06 The swing on Space still plays Mixamo's slash, and it's the move a player sees most. Most other games have it on the bar with the skills, as a skill each player starts with, so that's what it becomes, with a move and fx of its own. However, a swing hits as far and as hard as the weapon in hand, and takes as long as the weapon says. So in our proto file, an effect can be a swing of the weapon in the player's hand, with its reach, its damage and its timing, in place of the ones above. And the skills file names the swing on Space, which each player knows. In the skills' header file, the table keeps the swing too, as attack. In parse skills, the checks on moves stay, with one change. A swing's move isn't checked against its cast time, as that's the weapon's, and its effect in the file has none. Everyone has the swing from the start, when the file names one. Its number has to be one of the file's skills, which parse skills checks in the table it's just made. If it isn't, parse skills refuses the file, naming the number. If it is, it joins the starting skills, and the table keeps it as its attack. The world gives a player the starting skills each time they join, so a player saved before today gets it too, without anything in the database. In the skills file, the swing on Space is number five. For now, while we try them out, each skill can be learnt from level one, so the kick, second wind and the whirlwind come down to it. Strike is skill five, a swing of the weapon, with the burst icon. Its move winds up over the left shoulder in half a second, with gather, so it speeds up into the strike. Strike's move is four poses, like the thrust's. The chest turns forty degrees to the left, with the arm across the body, and the hand turned so the blade points back over the shoulder. The left arm goes a little out, for balance. At eight hundred and sixty milliseconds, with snap, the chest has turned thirty-five degrees the other way. The arm has come across and down to the right, just before the sword's hit at nine hundred, with a small step in. At one point three seconds, it follows through, a little looser, and it's back at the clip by one point eight five seconds, before the sword's two seconds are up. Its fx come after the move, timed, as its poses are, from when the swing starts. Its trail runs from five hundred and sixty milliseconds, for a third of a second, in the pale colour of steel. Twelve sparks fly at the monster at eight hundred and eighty, as the sword lands.
44:38 A swing has no cooldown, so holding Space starts the next one in the tick the last one ends. The player's motion stays a skill, with the skill's id, so a client would see nothing change, and hold the move's last pose. So in our proto file, a player using a skill gets how many skills they've started, as field fourteen, and a new count is a new cast, even of the skill before. On the server, the old swing goes, so in the world's header file, the player loses the time into their swing, and swing and hit go, as a swing is a skill now. The player keeps the count of the skills they've started. A swing used to have a motion of its own, attack, a timer of its own and a hit of its own, next to the skills', which did much of that already. Effect of says what a skill does when a player uses it: a swing's reach, damage and timing are the weapon's in their hand, and any other skill's are its own. And land takes that effect, not the skill, from now on. Usable says whether a player can use a skill now: they know it, and it's ready. A swing needs a weapon to swing, and a skill that hurts one monster needs a living one picked, so that its cooldown isn't spent on the air, while a swing goes at the air. In the world, putting the weapon away ends a swing, as it did, and a swing is now a skill whose effect says it's the weapon's, so that's what we look for. A skill under way takes its hit and its cast time from effect of, so a swing lands at the weapon's hit, and lasts as long as its swing. The old swing's branch, with its own timer, goes. The old branch for Space, with its run-up and its swing, goes too, as the skills' path does both. Holding Space asks for the swing, as if its key had gone down, unless another skill is waiting. So it takes the skills' path from here, and holding Space down still swings again and again. The swing goes through usable and start, like any skill, so it gets the run-up, the aim and the cast time without code of its own. The client sends attack as a flag held down, so the world sees it on each tick until it's let go. Input skill is the skill a player asked for, which waits until they're free, so a swing asked for during another skill starts as that one ends. The input's attack flag stays as it was, so the client and the bots send what they did before. A skill is far when it hits one monster, the player has picked one, and it's out of reach. With no monster chosen, Space isn't far from anything, so the blow comes straight away, as it did. Approach runs up to the effect's reach, which for a swing is the weapon's. Out of reach, the player is run up to the monster, unless they walk off, which calls the run-up off. Effect of is called in each of these places, so a weapon swapped mid-fight changes the next blow. A weapon skill without a weapon has no reach, damage or timing, so starting it would land nothing, tick after tick. A heal, or a skill that hits all around, has no such check, and goes whatever the player has chosen. Effect of copies the skill's effect, which is cheap, as an effect is a handful of numbers, and looks at the weapon the player holds. For a swing, with a weapon in hand, it takes the reach and the damage from the weapon, then the cast time from the weapon's swing, and its hit. Start counts the cast, as the player starts a new one. It sets a cooldown when the skill has one, as a swing doesn't, and aims with the effect's reach. And land takes the effect, so a swing does the weapon's damage, plus a level's five each, and what the player wears, as a swing did before. And the server sends the count along with the player's skill and target, while they use one.
48:15 The world's tests use content of their own, and their swings now need a skill. So the swing is skill a hundred in the tests, well out of the way of the ones the tests make. Inline constexpr makes it a constant the header can define, as we did for the joints' names. With strike takes a test's skills by value, and adds the swing, a skill like the game's, with a name and a level, and an effect that says it's a swing of the weapon. It goes in the table by its id, joins the starting skills, and is the table's attack, before the skills go back. And with now builds each world with it, as the game's content has it. The swing is a skill everyone has now, so the tests of swinging expect the player's motion to be a skill, and the first one checks it's the swing. A new test makes a world, as the other swing tests do, and joins a player, who holds Space down, and a tick in, the swing is cast one. A swing takes two seconds, and with Space still down, the next one starts in the tick the first one ends. So the test runs two seconds more, and a tick, and the player is still using a skill, with two casts counted. Further down, a swing at the air, and the test that a player without a weapon can't swing, check for a skill too. Their hits, their timing and their damage don't change, so a swing that's a skill hits as the old one did. Then a quick test that the content gives everyone the swing, with a file of a single skill, named as the attack. A swing named in the file is the table's attack, and a starting skill, so a new player knows it. A file whose attack is skill three, which it doesn't have, is refused, with a message naming the number. And in the animator's tests, a new test makes an animator, and a world message from the server with one player in it. Its motion is a skill, skill five, at cast one, and a second and a half later, the figure is that far into the move. The next swing comes straight after, with the skill unchanged and a count of two, and the figure is back at the start of the move.
50:12 We build the tests at this step, and run the world's tests of swinging, the content's test of the swing, and the animator's new test. Docker builds the test image from this step's code first, and then the three test binaries run one after another, with grep keeping the lines that say a test passed. Each swing is a cast of its own now, and it hits where and when the old one did.
50:35 On the client, the animator starts a figure's clip again when its motion or its skill changes. A figure keeps its player's count of casts too. In the animator, follow takes the count as an argument of its own. A new count starts the move again from its first key, so each swing gets its trail and its sparks, as a new skill would. Monsters cast nothing, so they pass zero for the count. The hotbar is the bar of skills along the bottom of the screen, which we built along with the skills. It gets the swing first, on Space, and the other skills after it, on the number keys, as before. A flag says if slot zero holds the swing. The constructor leaves the swing out as it sorts the skills by level. The swing's level is one, so sorting would have put it among the others. Then it puts the swing in front, if the content has one. Each slot draws as before, with its icon, its key and its cooldown. Its tooltip says it's a swing of what you hold, as it has no level or cooldown to show. Content without a swing, such as a test's, leaves swing underscore false, so its bar numbers from one, as before. Number is a slot's key, counted from one after the swing. Its slot shows Space, and the thrust keeps key one. A click on the swing's slot asks for it like any skill. In the characters, with no attack clip, a character uses its idle in its place, as no player's motion is an attack now. The skeleton keeps its own attack, for its swings at us. So the build of the characters drops the sword slash too, and the characters come from Mixamo with no more than an idle, a walk, a run and a death.
52:19 Let's see the swing in the game, at this step. The client still says no more than that Space is down, and the server uses the swing from the skills file, as if its key had gone down. So other players see a skill with an id, like any other, with a count that goes up with each swing, and draw its move, its trail and its sparks each time, and the hotbar shows it first, on Space. A skill with a cooldown, like the whirlwind, still waits for it, while the swing comes again as soon as the last one ends.
Wrap-up
52:49 So, today, a skill's move became a few keys in the content, which the client turns the skeleton's joints by, on top of the idle, with no more clips for the skills to download. The fx came next, worked out from the time since the skill started: a trail where the sword has just been, posed again at moments just gone, and glows. Then rings along the ground, sparks, motes and a shake of the camera, some of them at the monster the server says the skill lands on. And the swing on Space became Strike, a skill everyone has, with the weapon's reach, damage and timing.
Playing it
54:17 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.