nanoMMO

MMORPG skill effects in C++ for stone, ice, lightning and fire

Four new skills in our C++ MMORPG, with shards and spikes that are lit like the world, lightning that strikes again and again, a crescent of fire, and a slice in front of the player for the server to hit.

Watch on YouTube

Our MMORPG gets four new skills, each a move of its own and a list of fx in the skills file. The raylib client draws shards thrown up from the ground and spikes of stone and ice that come up nearest first, as solid gems lit by the world's shader, then lightning that finds a new path each flicker, and a crescent of fire that flies forward from the player. The C++ server gets an arc, so a skill can hit a slice in front of the player and nothing behind. Earthsplitter, Frost Nova, Searing Arc and Stormcaller are entries in a text file, on top of last episode's moves and fx. This is episode 54 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.

Topics: Graphics, Gameplay

The code

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

  1. 2:05ep054/step-1Shards and spikesBrowseChanges
  2. 12:24ep054/step-2Lightning and fireBrowseChanges

Transcript

Intro

0:00 Welcome back to episode fifty-four, where we're going to add four new skills to our game, with stone, ice, lightning and fire, on top of last episode's moves and fx. Before we build them, let's see where it's going.

0:14 By the end of the episode, Earthsplitter slams the ground, and stone spikes come up around us, with rocks thrown into the air. Frost Nova drives the sword into the ground, for a ring of ice crystals. Searing Arc throws a crescent of fire at what's in front of us, and Stormcaller brings lightning down onto the monster we picked.

0:33 There's more about the series on the project's website. The code is open source, on GitHub, and linked from there, and each episode gets a page of its own, with the code at each of its steps.

Last time

0:46 A quick reminder of where we left off, first. The skills got moves of our own, keys of joint turns on top of the standing clip, in place of Mixamo's clips. The blade got a trail, and the glows became fx, which the skills file lists for each skill. Rings on the ground, sparks, motes and a shake of the camera came next, each worked out from how long ago the skill started. And the swing on Space became Strike, a skill each player has, with the weapon's reach and damage, while each skill can be learnt from level one, for now.

Today

1:21 Today, we're going to put that to use, and add four skills, with the few kinds of fx they still need. First, shards and spikes, which are solid, so they're lit like the rest of the world, and not drawn as light. With them come Earthsplitter and Frost Nova, which are entries in the skills file. Then lightning, and a crescent of fire that flies forward from the player. The server gets an arc, so that a skill can hit a slice in front of the player and nothing behind, for Searing Arc, and Stormcaller brings the lightning down. Most of today's work is content, as a new skill is mostly an entry in a text file now, as long as the server already does what it needs. So, let's get going, with the solid fx.

Shards and spikes

2:05 In our proto file, the kinds of fx get two more. Shards are pieces thrown up from where the skill lands, which fall back down, and spikes come out of the ground, in a ring that spreads out to the fx's size. In the fx header file, the constructor takes a shader too. Shards and spikes are solid, so they should be lit like the ground and the figures, with the sun's bands and the fog, and the world's shader does that already. The world's shader is the renderer's, so the renderer hands it over when it makes the fx. Then draw solid draws the shards and the spikes, with the terrain, so that they stand on the ground. And the class keeps a model of a single piece, which each shard and each spike is drawn with, scaled and turned. In fx, we include our triangles header, which builds a mesh a triangle at a time, with a normal for each face, as we did for the stones round the gates. Overshoot is an easing that goes past the end, and comes back. A spike that shoots out of the ground a little too far and settles looks like it came up with some force, where one that stops dead looks placed. The one point seven sets how far past it goes, the usual value for this curve, which overshoots by about a tenth. Gem makes the piece: five corners round its middle, with a point at the top and one at the bottom, a unit tall. A lambda gives each corner, at its turn round the circle, half a unit out from the middle, so the gem is a unit across too. Scaled evenly, it's a chunk of rock or ice, and stretched tall, it's a spike, so a single mesh does for both. Each side gets a triangle up to the top point and one down to the bottom, in white, so the colour we give each piece is the colour it's drawn in. Then upload hands the triangles to the GPU, as a mesh. The constructor loads the gem as a model, and gives its material the lit shader, and the destructor unloads it, along with the light's shader. Now draw solid, which works with a copy of the model, which shares its material, so each piece sets its colour there just before it's drawn. The draw lambda builds a piece's transform in the order raylib applies it. The scale goes first, then a turn about its own upright axis, so the gems don't all show one face to the camera. Then comes the lean, about an axis we give it, and last the move to where it goes. Each piece is a draw call of its own, which for a few dozen gems is fine. Then, for each cast, we look its skill up and go through its fx, with an index, as the random numbers need it. For each fx, the age is how long ago it started, and the span how long it lasts, both in seconds, as the light works them out. An fx at the target needs a monster picked, so without one it's left out, and this function draws nothing but shards and spikes, which the light's switch leaves out. Where it lands is on the ground, at the player's feet or the target's. The moment is the one the light uses, with the cast's seed and the fx's index for its random numbers, and the fx's colour. Each piece gets a direction round the circle, from the cast's random numbers, which don't change from one frame to the next, so a shard flies one way however often we draw it. A shard is thrown up and out, somewhere between forty-five and eighty degrees above the ground. Its speed would carry it between a third and one and a half times the fx's size at forty-five degrees, with the gravity we used for the sparks. It lands when it's back down at the height it started from, which is twice its speed upwards over gravity, so from then on, its time stops, and it lies where it landed. Its height is a tenth of a metre to start with, plus how far it's risen, less what gravity has taken back, which is the usual sum for something thrown. It's twelve to twenty-eight centimetres across, and shrinks away over the last thirty percent of the fx, as a piece of rock that blinked out would catch the eye. It tumbles about an axis of its own, at six to sixteen radians a second, until it lands, and a shard that falls through a slope is kept on top of it. A spike comes out of the ground, somewhere within the fx's size from its middle. The square root spreads the spikes evenly over the circle's area, where a plain random distance would bunch them up in the middle. The nearest come up first, and the furthest a third of the way through, so they come up as a wave going outwards. Each grows out of the ground in about an eighth of a second, past its height and back, and sinks into the ground again over the last quarter. They're eighty centimetres to one point eight metres tall, and shorter towards the edge, so the ring is highest in the middle. Each leans out, away from the middle, by fifteen to thirty-five degrees, about an axis across the way it points. Up for the spike is up, tipped over by that much, and it's thirty to fifty-five centimetres wide. The gem is drawn one point four times as tall as the spike's height, with its middle along that leaning axis. So at its full height, its tip is where it should be, and the lower part of it stays buried, which makes the base look like it's broken through the ground. In the renderer, the fx get the shader the cubes, the trees and the ground are drawn with, and the shards and the spikes are drawn right after the figures, before the see-through shadows, as they're solid too. And in the fx test, the fx need a shader now, so the test passes raylib's default one, which loading with two null paths gives us, as it draws nothing.

Earthsplitter and Frost Nova

7:25 Now the skills file, where the kick gets eight small stones first, thrown up to half a metre from the monster's feet as the kick lands, in a sandy brown, for the dirt it kicks up. Then the first new skill, Earthsplitter, skill six, as Strike took five. Like the others, it's from level one for now, and it's ready again after fifteen seconds. Its effect is the server's part, and the server already does all of it. It holds the player for one point eight seconds, and lands at eight hundred and eighty milliseconds, for forty damage to each monster within three and a half metres, knocking them two metres back. Its icon is Font Awesome's mountain, and then comes the move, as keys of joint turns over the standing clip, with the angles about the player's own axes, as we set them up last time. By a third of a second in, arthur crouches, with the sword up behind the head in both hands. Both arms go up a hundred and fifty degrees or more, and the forearms fold back seventy, which puts the blade behind the head. The thighs come forward thirty degrees and the knees bend sixty, and with the legs that much shorter, the hips come down twelve centimetres, so the feet stay on the ground. Then a jump: by two thirds of a second, the whole body is sixty-five centimetres up and thirty forward, with the knees tucked up, and the sword still overhead. The jump is smooth, so the body slows towards the top, and the slam after it leaves the top fast. The slam snaps down to the ground in two tenths of a second. The body goes thirty-four centimetres below standing, which is a kneel, and fifty-five forward. The spine and the chest each bow thirty degrees, the arms come down in front, and the wrist turns sixty, so the blade points at the ground ahead. The front leg reaches forward, and the back knee bends a hundred and ten degrees, onto the ground. The next key, at one and a quarter seconds, holds the slam a little longer. The spine and the chest are a few degrees less bowed and the arms a touch higher, so the pose isn't frozen while the ring goes out. The move is back to standing by one point seven five seconds, a twentieth of a second before the skill lets go of the player. Then the fx, timed with the move: an orange trail while the sword comes down, and a glow as it lands. Then, at eight hundred and eighty milliseconds, as the damage lands, an orange ring spreads out to four metres. Twenty stone spikes come up within three and a half metres, in a dark brown, for a second and a third, and twenty-four darker rocks are thrown up to a metre and a half, with forty sparks among them. And the camera shakes, up to thirty centimetres, for half a second. Frost Nova is skill seven, which holds the player for one point seven seconds, and lands at eight hundred and twenty milliseconds. It does thirty damage to each monster within four metres, and it's ready again after eighteen seconds. Its effect needs nothing new from the server either, with all set, so it hits each monster in reach and needs no pick. Its icon is the snowflake, and its move starts with a raise. By nearly half a second in, arthur stands three centimetres taller and leans back a touch, with the sword arm raised a hundred and sixty degrees. The wrist turns seventy-five, as a raised arm would otherwise point the blade backwards. The other arm is held out to the side, for balance. Then comes a snap down onto one knee, by eight tenths of a second, about a third of a second after the raise, so the blade comes down fast and settles into the ground. The spine and the chest bow fifteen and twenty-five degrees, and the sword arm comes forward and down, with the wrist turned further, so the blade goes into the ground, while the free arm stays out to the side. The front leg goes forward seventy degrees with the knee at eighty, and the back knee bends a hundred and fifteen, so arthur kneels on it. Then it holds, easing back a couple of degrees while the ice comes up. It's standing again by one point six five seconds, just before the skill lets go of the player at one point seven. Its fx start two tenths of a second in, with a pale glow at the tip of the blade, which follows the blade up as light gathers at it, and then a glow round the player as it lands. A blue ring spreads out to four point four metres, and twenty-eight spikes of pale ice come up within four metres, the nearest first, and stay for nearly two seconds. Fifty white motes rise and swirl among them, and the camera gives a small shake. And in the NPCs file, the trainer teaches the two new skills, so a player who doesn't know them can learn them from any level, as they can the others for now.

11:52 Let's see them in the game, at this step. Nothing new goes over the network for them: the server sends each player's motion, their skill and the monster they picked, as it did last time. Each client sees the skill start, and works out where each stone is from the time since then. The server holds the player for the cast, and lands the damage on each monster within reach as the slam hits the ground, while the client draws the spikes, the rocks and the ring. The ice is the gem the stone is made of, in another colour, from another entry in the skills file.

Lightning and fire

12:24 Back in our proto file, a skill's effect gets an arc, in degrees: how wide a slice in front of the player it hits. Without one, it hits all the way round, so the skills we have stay as they are, and the server's going to use it in a moment. The fx get two more kinds: a bolt is lightning from the sky, and a wave is a crescent of light that flies forward to the fx's size. In fx, the moment of an fx gets the way the player faces, which the wave needs, as it flies that way. Draw bolt draws the lightning, as a path of sixteen steps from the sky down to where it strikes. Lightning doesn't hold still: it strikes, again and again, a little differently each time. So each seventy milliseconds is a strike of its own, with its own number, which picks the random numbers for its path. The frames within a strike draw its path, and the next strike draws a new one. Each strike is bright for its first two thirds, and dim for the rest, which makes it flicker, and it fades out over the fx's time, on a square root, so it stays bright for most of it. Jag is a lambda for a random number between minus a half and a half, for this strike. The bolt starts sixteen metres up, and up to a metre and a half to either side, so it doesn't come straight down each time. Each point along the way is on the line from the sky to the ground, pushed to the side by up to about half a metre each way. The sine is zero at both ends, so the path starts where it should and lands on its spot, and wanders most in the middle. Each step is a wide streak in the bolt's colour, with a thin white core down the middle, which blooms. Real lightning branches, so three forks come off the path partway down, after its fifth, eighth and eleventh steps. Each goes four steps, down a metre and a bit each time, and sideways at random, thinner than the main bolt. Draw wave draws the crescent, which spans a hundred and twenty degrees, sixty to either side of the way the player faces, in twenty-four steps. It starts eighty centimetres in front of the player, and flies the fx's size further out by the end, at a steady speed, as a projectile would. It fades in over the first tenth of its time, and out towards the end, on a cube, so it stays bright for most of the way. At is a lambda for a point at a turn from the way the player faces, using the yaw we added to the moment. It takes a distance out and a height, from the middle of the player's body, as the blade swings at waist height. The front edge is the wave's colour taken most of the way to white, which blooms. For each of the twenty-four steps, a and b are the turns at either end of it, from sixty degrees to one side to sixty to the other. The crescent is fifty-five centimetres thick in the middle, and thins to nothing at its horns, as the cosine does at its ends. The square root keeps it thick for longer, so the shape reads as a crescent and not as a lens. Each step's first quad lies level with the player's middle, which reads from above, where our camera usually is, and it goes from nothing behind it to bright at its front edge. And it's drawn standing too, above and below that front edge, so from the side, it looks like a blade of light and not a line. The two standing quads go from the bright edge to a little behind it, sixty percent of the thickness above and below, where they fade out. The moments in draw solid and draw light take the player's yaw, from the figure as the cast last saw it. The server turned them that way as the skill started, and draw light draws the bolts and the waves.

A slice in front

15:55 In the world, we include numbers, the standard library's header for constants such as pi, from C++20. In arc says whether a monster is within the slice in front of the player, as wide as the arc. A skill without an arc, which is zero, hits all the way round, so the skills we have keep doing what they did. The direction from the player to the monster is a yaw, worked out with atan two the way aim turns the player to face their pick, so the two compare directly. The difference between the two could be anywhere from minus two pi to two pi, and a monster at minus three hundred and fifty degrees is really ten degrees off. Remainder by a whole turn brings it between minus pi and pi, which is the shortest turn from one to the other. The monster is in the slice when that turn is no more than half the arc either way, in radians. And land hits a monster when it's in reach, and in the arc. Next, a quick test that the slice is in front. Its world has the sword, and a skills file of the test's own, a text proto in a raw string. Its one skill holds the player for a second, lands half way through, and hits everything within five metres for twenty, in a slice ninety degrees wide. The player joins knowing it, facing along z, which is where a player who joins without a yaw faces. One skeleton stands three metres ahead of the player, along z. Monster of makes a monster of the tests' skeleton kind at an x and a z, and spawn puts it into the world. Another stands three metres behind, so both are within the reach, and nothing but the arc tells them apart. The player uses the skill, and after a second, the one ahead has lost twenty health, and the one behind none. The hit on the one ahead shows the skill lands at all, so the one behind keeping its health is down to the arc.

17:42 We build the tests at this step, in Docker, and run the world's tests, which need no database. The arc hits what's in front, and the world's other tests pass as before.

Searing Arc and Stormcaller

17:52 In the skills file, Searing Arc is skill eight, ready again after fourteen seconds. It holds the player for one point four seconds, and lands at seven hundred and sixty milliseconds, for thirty-five damage, to each monster within seven metres, in a slice a hundred degrees wide in front of the player. It hits them all, so it needs no pick, and it turns the player to their pick if it's within seven metres. Its icon is the fire, and its first key winds up, with gather, which starts slowly and gets faster into the key, as you would gather yourself before a big swing. The wind-up takes about a third of a second, and the strike after it two tenths. The chest turns fifty-five degrees to the left, and the sword arm comes across the body, with the forearm folded, so the blade is back over the left shoulder. The other arm goes out thirty degrees, and the body dips six centimetres, ready to spring. Then it snaps across to the right in two tenths of a second. The chest turns fifty degrees the other way, the arm swings out at shoulder height, and the body lunges a quarter of a metre forward, with the front leg out. The next key holds the follow-through until nine tenths of a second, the chest still turned and the arm still out, so the swing reads as finished, and the move is back to standing by one point three five seconds. Its trail is red and orange, while the blade comes across. And as the swing comes round, the crescent sets off, and flies out six and a half metres in sixty-five hundredths of a second. With the eighty centimetres it starts at, that covers the reach, and a monster four metres away is hit about as the crescent gets to it. At the monster we picked, if there is one, a glow and sparks go off as the damage lands. Stormcaller is skill nine, with a twenty-second cooldown. It holds the player for one point eight seconds, and lands at one point one, for sixty damage to the monster the player picked, within ten metres. Its effect doesn't hit all of them, so it needs a pick, and runs the player up to it when it's further. Its icon is the bolt, and its move starts by raising the sword. By a little over half a second, the chest leans back eight degrees and the head tips back twelve, and the sword goes up to the sky, with the wrist turned to point the blade up. The other arm goes out to the side, for balance, as it did for Frost Nova. The second key, at one second, raises the sword a little higher still, as the lightning comes down onto it, and the body lifts another centimetre. It holds the pose for most of half a second, with the chest tipped back ten degrees and the head fifteen, which is when the first bolt comes down onto the blade, before the swing down at the target. The third key comes a hundred and fifty milliseconds after it, with a snap. The sword comes down to point at the target, with a small lunge. The chest bows twelve degrees, the front leg steps forward thirty with the knee bent as much, and the back leg goes back fifteen, braced for the strike. It holds that until one point four five seconds, with the pose eased back a couple of degrees, and it's back to standing by one point seven five, a twentieth of a second before the skill ends. Its fx start with a bolt down onto the tip of the raised blade, at six hundred and fifty milliseconds, for a third of a second, before the sword comes down. A pale blue glow sits on the tip for half a second. Then, just before the damage lands, a bolt strikes the monster we picked, for nearly four tenths of a second. A glow over two metres across goes off on it, a blue ring spreads out along the ground to two point six metres, and forty sparks fly two metres out. And the camera shakes by up to twenty-two centimetres, for four tenths of a second. The server sends the player's pick with their skill, so each client nearby draws the lightning onto the skeleton they picked. And in the NPCs file, the trainer teaches these two as well, which makes seven skills the trainer teaches, with Thrust and Strike known from the start.

21:47 In the game, at this step, Stormcaller's reach is ten metres, so the server starts it where the player stands, with no run-up to the skeleton picked. The server sends our pick with the skill, so the client knows which skeleton the lightning goes to, and draws a new path for it each seventy milliseconds. For Searing Arc, the server checks each monster's direction against the way we face, and hits those within the hundred degrees, while the client draws the crescent flying out over them.

Wrap-up

22:14 So, today, the fx got shards and spikes, a gem lit by the world's shader, thrown up and landing, or coming out of the ground nearest first. With them, Earthsplitter and Frost Nova are two entries in the skills file. Lightning finds a new path each flicker, and a crescent of fire flies out from the player. The server's skills got an arc, so they can hit a slice in front of the player, and Searing Arc and Stormcaller put both to use, with the lightning landing on the monster the server says we picked.

Playing it

23:46 Tell me in the comments which skill you'd like to see next, or anything else you'd like to see. And if you want to follow along with the series, subscribe to the channel. I hope you find this useful, and thanks for watching.