Our MMORPG's fights go for the monster you pick now, not whatever's nearest. The C++ server keeps each player's pick from their input, and swings and skills that hit one monster go for it. In the raylib client, a click on a monster picks it, with a red ring at its feet and a frame beside yours, and the hotbar shows which skills need a pick. The cursor turns into a hand over someone you can talk to, and a crosshair over a monster, and an NPC answers a click only from within talking reach. And a swing or a skill at a monster out of reach runs you up to it first, on the server. This is episode 51 of a series about building an MMORPG from scratch in C++, with the focus on the engineering around the game.
Topics: Gameplay
The code
Each step on screen is a tag in the game's repository. The code at the end of the episode is ep051/end.
Transcript
Intro
0:00 Hi, and welcome to episode fifty-one, where we're going to pick our fights, with the mouse. Swings and skills have gone for whatever monster was nearest, and from today, they go for the one you clicked, and run you up to it when it's out of reach. Let's begin with a quick look at where we're going to be by the end of the episode.
0:19 By the end of the episode, the monster we pick gets a red ring at its feet, and a frame of its own, next to ours. A skill at it, out of reach, has the server run us up to it, and use the skill once we're there. And the swings go for the monster with the ring, the one we picked.
0:41 The project has a website, with more about the series. The code is open source, on GitHub, linked from there, and each episode has a page, with its code at each step.
Last time
0:53 Now, before we get to today's work, let's quickly go over what we did last time. The client got a loading screen, which covers the world while it isn't there yet, and lifts the fog off a new land, nearest first. The dead come back when they ask to, through a death screen that counts down to a button, and the gates got round platforms, in a ring of pointed stones.
Today
1:15 To start, a fight by the skeletons, in the game from last time. Once a skeleton is in reach, Space swings the sword at whichever one is nearest, and there's no way to say which one we mean.
1:28 On the server, aim looks for the nearest living monster within the weapon's reach as each swing starts, and a skill like thrust does that within its own reach. In a crowd, that's whoever happens to be closest, and not the one you meant. Most other games have you pick the monster first, with a click, and then everything goes for it. So today, the server keeps what each player picked, from their input, and the swings and skills go for it. On the client, a click on a monster picks it, with a ring at its feet and a frame of its own. The cursor says what a click would do, and an NPC that's too far away to talk to doesn't answer one. And when what we picked is out of reach, a swing or a skill runs us up to it first. Right, let's start with the server, which decides who gets hit.
Swings go for the pick
2:16 In our proto file, the input gets the monster the player picked, by its id, as field thirteen, or zero for none. It comes with each input, like the keys, so the input the server read last carries the newest pick. In the world's header file, the player's target is the monster they picked now, by its id, which their swings and skills go for, or zero for none. Until now, it was the one aim chose, as each swing or skill started. Then comes pick, which sets the target from now on, with the monster's id from the input, or zero to pick nothing. And target finds the monster the player picked, while it's alive and within a reach, or gives a null pointer. Aim and usable both call it, each with a reach of their own. In the world, join forgets the pick, as a monster's id is its own land's, and a trip through a gate joins the next land. Pick looks the player up by their id, and if they're there, sets their target to the monster. It doesn't check the monster, as a pick that's dead, gone or far away is something target handles each time. Target looks the monster up in the world's map, and gives nothing if there's no such monster, it's dead, or it's further than the reach. Aim no longer looks for the nearest monster. It turns the player towards the one they picked, if target finds it within the reach, and otherwise leaves them facing the way they were, so they swing at the air. Hit already lands on the player's target, if it's still alive and in reach when the swing comes across. In usable, a skill that hurts and doesn't hit all around needs the monster the player picked, within its reach. Without it, the skill isn't used, and its cooldown isn't spent on the air. Second wind, which heals, and whirlwind, which hits all around, need no pick. In the server, each input sets the player's pick. And the bots, which hunt the monsters their quests ask for, pick the one they hunt as they start swinging, as a player clicks it, so that their swings go for it.
4:13 Now, let's write a quick test that the swings go for the pick. The test of the swing at the nearest monster becomes a test of the swing at the one the player picked. Two skeletons stand within the sword's reach, one a metre east and one a metre and a half north. The player picks the far one, and swings. It isn't the nearest, but it's the one they picked, so they turn to face it, which is a yaw of pi, along minus z. After a second, it's the far one that's lost fifteen health, and the near one none. With nothing picked, the player still swings, but the swing goes at the air. This new test has a world with a sword, a player who joins with the defaults, and a skeleton a metre east of them. They hold attack, and a tick later, their motion is attack, so the swing does start. After a second, though, the skeleton still has all fifty of its health. The test of the kills picks its weak skeleton before the swing, and in the test of the skills, both players pick the skeleton before they ask for the kick. And a skill that hits one monster needs one picked, within its reach. This test's world has the sword and the kick, and the player joins with a hundred experience, knowing the kick, which is skill one. One skeleton stands a metre east, and another six metres east, which is too far away for the skill, as it lands from one point four metres at most. With nothing picked, the player asks for the kick, then picks the far one, and asks again. Either way, there's no kick, and no cooldown either: their motion isn't a skill, and the kick's cooldown hasn't started. Then they pick the near one, and this time, a tick later, the kick starts. The trainer's test gets a skeleton a metre east too, which the first player picks, as they kick once they've learnt it. The tests of what they wear, and of swinging with a weapon in hand, pick theirs before they swing. The helper that kills the looted skeleton picks it as it spawns, and the test that records the blows and the healing picks its skeleton too. The server's test of the hit that bob sees has alice pick the skeleton, the first monster, before she swings. And the fuzzer, which plays the world with any input a client could send, draws one more number now, up to twelve, and case eleven picks, with any id from zero to nine.
6:28 We build the tests at this step, in Docker, and run the world's tests, which need no database. They pass, so the swings go for the pick, and a skill that hits one monster needs one picked, within its reach.
In the game
6:41 Let's see it in the game at this step. The sword swings, and the skeleton takes nothing. Nothing's wrong with the server: the client doesn't pick anything yet, so each input it sends says zero, and the world swings at the air.
A click picks
6:56 On the client, picking already finds the NPC under the mouse. In the picking header file, pick monster finds the nearest living monster along a ray, such as the one from the camera through the mouse, or a null pointer if there's none. In picking, the loop that pick NPC had becomes a function template, pick, which finds the nearest figure along the ray, of those it lets through. It lets them through with a test, its last parameter, of the template's type. Each figure is a box as wide as a person's hanging arms and as tall as them, and raylib's ray collision box gives how far along the ray it's hit. A figure the test doesn't let through is skipped. A template lets the test be a lambda, called directly, with no std function in between. The unnamed namespace now ends below pick, as nothing outside picking calls it. Pick NPC calls pick, with a lambda, a function written in place, which lets through the NPCs a click does something with, as before: those who talk, teach or give quests. And pick monster calls pick as well, with a lambda of its own, which lets through the monsters that aren't dead, so a click passes over a dead monster, to a living one behind it, if there's one. In the client's main, the monster we picked is kept by its id, next to the input we last sent, and entering a world starts with none. A new pick is something new to send, like a key, so it goes into the input, with the direction, the run and the attack. Building a new land forgets it, as it was the last land's monster. Each frame, once the figures are updated, we keep the monster we picked while it's alive and near enough to be sent, and let go of it otherwise. So a monster that died, or went out of sight, isn't picked any more. A click on the world does nothing while we're dead. Otherwise, a click on someone to talk to opens their window, one on a monster picks it, and one on neither lets go of what we'd picked. So after the NPCs, pick monster looks for a monster under the mouse, and we keep its id, or zero, for a click on the ground. Escape lets go too, unless we're typing in the chat. The renderer gets the pick each frame, for its ring. Draw player frame returns where our frame ends now, and the target frame goes to the right of it. And the hotbar gets the pick too, as it builds.
9:13 In the renderer's header file, set target takes the monster we picked, by its id, which gets a ring at its feet, or zero for none. The renderer keeps it in target underscore, which starts at zero. In the renderer, the ring gets three constants, next to the shadow's. Its inner edge is fifty-five centimetres from the figure's middle, around the monster the player picked, in a red close to the monsters' nameplates. The outer edge is at sixty-eight, so the whole ring lies outside the shadow, which reaches forty-two. Its colour is that red, at an alpha of two hundred and twenty, a little short of opaque. Draw ring draws a flat ring on the ground, much like draw shadow does under each figure. It goes around in thirty-two sides, twice as many as the shadow has. Its corner lambda takes a distance from the middle and a side's number, and turns the side into an angle around the circle, with cosine and sine giving x and z. Each corner is in the ring's colour, on the ground under it, four centimetres up, so the ring lies on a slope. That's a little above the ground, so the ground doesn't cover it. Then, for each side, two triangles go from the inner edge to the outer one, between this side's corners and the next one's. In draw, the shadows are laid on the ground in their loop, as before, with the depth mask off and blending on, so they darken what's under them. That loop now ends after the shadows, and a second loop over the figures follows it, with that blending. In it, a monster with the id we picked gets its ring, after all the shadows, so that no shadow, such as ours beside it, darkens the ring. A target of zero, for nothing picked, gets none. It's drawn at the figure's position, with the ring's constants. In the player frame's header file, we include cstdint, for the monster's id, a thirty-two-bit unsigned integer. Draw player frame, which returned nothing, returns a float now. It shows our name, level, health and experience, in the top left, and returns where the frame ends, on the right, or zero while it isn't there. Draw target frame is new, and takes the figures, the target, and the left edge to start from. It draws the monster we picked, by its id, with its name, level and health, beside our frame, while there's one. In the player frame, k monster is a red close to the monsters' nameplates, for the target's name. The margin, ten pixels, moves up out of draw player frame, and the window's settings become a constant, k frame, as both frames share them. A frame has no title bar or scroll bar, and its size follows what's in it. It can't be moved, or reached with the keyboard, and doesn't take the focus when it appears. Draw player frame returns a float, zero while our level hasn't come, and begins its window with k frame. Before it ends the window, it measures where the window ends, from its position and its width, as the width depends on the name in it, and returns that. Draw target frame uses ranges find if to look among the figures for the one that's a monster, with the id we picked. It draws nothing without a pick, or while the monster's name hasn't come yet. Its window goes a margin to the right of where ours ends, at the height of ours, and it's called target. It shows the monster's name in k monster's red, then its level, greyed out, on that line, and its health, with the bar ours has, in the health green. In the hotbar's header file, a skill that hits one monster is red while the one we picked, by its id in target, isn't within its reach, and build takes that target, after the world. In the hotbar, we include limits, for infinity. K out of reach is a red, as most other games show a skill whose monster is too far away, or missing. Then build takes the target as its last parameter, and with ranges find if, it finds the monster we picked, if it's alive. Away is how far it is: infinitely far without one, or the distance along the ground, from us to it, with std hypot. It's worked out once, before the loop over the skills, as each skill compares its own reach with it. For each skill, inside the loop, we take its effect, with its damage and its reach. A skill we know, which hurts and doesn't hit all around, is out of reach when the pick is further away than the effect's reach. Then its icon is drawn in k out of reach, pushed as the text colour, as the icon is the button's text, and popped again after the button.
13:35 Next, a quick test that the click finds the right monster. We include cstddef, for std size t, the type of the loop's index. It starts with three figures, and a loop over them gives each an id, from one to three, and makes each a monster. They stand in a line ahead, four, six and eight metres away, along minus z. The nearest is dead, and an NPC in front of them isn't a monster. That NPC, with the id nine, stands two metres away, in front of them all. The ray starts half a metre up, and points straight ahead, along minus z. Pick monster finds one, which the assertion checks first, and it's the second monster, the nearest living one, past the NPC and the dead one.
14:21 We build the client's tests at this step, and run the picking tests. Both pass, so a click finds the NPC, and the nearest living monster.
14:31 Back in the game, the monster we picked gets its ring, and its frame shows next to ours, with its health going down as each hit lands. The client sends the pick with each input, the server turns us to it, and the hit lands on it, and no other. Once it's dead, the client lets go of it, and the ring and the frame move to the next one we pick.
Cursors, and reach
14:51 So far, a click on the guard, from about five and a half metres away, opens its quest, about the bones by the gate. Take goes to the server, which answers in the chat that we're too far away to talk to the guard, something the client could have known before it opened the window.
15:08 On the client, pick NPC finds the NPC under the mouse at any distance, and it's the server that holds us to four metres, once we talk, take a quest or learn. It would be better if a click that can't work didn't look like it could, so the client needs that number too. In the NPCs' content header file, k talk reach is four metres, from the middle of an NPC, as you'd talk to someone. It's how near a player has to be to talk to one, learn from it, or take its quests. The server holds them to it, and the client lets them click from no further. The content library is shared by the server and the client, so both read it from one place. In the world's header file, the world's talk reach is the content's now, so the server and the client hold players to one number. In the picking header file, pick NPC takes where we stand, as a vector 3 called you. It finds the nearest NPC along the ray that the player, standing there, is near enough to talk to, learn from, or take a quest from, such as the one under the mouse, or a null pointer if there's none. In picking, we include cmath, for std hypot, and the NPCs' content header file, for the talk reach. Pick NPC takes where we stand, and its lambda captures it, by value. It lets through an NPC who talks, teaches or gives quests, as before, and stands within talk reach of us, along the ground, which is std hypot of the gaps in x and in z. In the picking tests, Next, the tests need to say where the player stands, so k here is six metres ahead, near enough to talk to the NPCs of the first test, which stand from five to ten metres out. Each call to pick NPC in that test passes it, as where we stand. And a new test has one NPC who talks, ten metres ahead, and the ray straight ahead, as in the other tests. From where we start, at the origin, the NPC is too far away, and isn't picked. From seven metres along, three metres from it, it is.
17:05 The cursor gets a header file of its own, with its include guard and our namespace. Pointing is an enum class of what the mouse is over in the world: nothing, an NPC or a monster. Update cursor sets the mouse cursor for the frame. Over the GUI, it's the GUI's own, and over the world, it's a hand on someone to talk to, and a crosshair on a monster to pick. The GUI leaves the cursor to us, which we set up in the GUI further down, so this runs each frame, once the GUI is built. In the cursor, we include imgui, for the cursor it wants, and raylib, to set the one the window shows. In the unnamed namespace, GUI cursor turns the cursor ImGui wants into raylib's, in a switch, as each library numbers its cursors in its own way. ImGui's text input becomes raylib's I-beam, the bar you see over a text box, and its hand becomes raylib's pointing hand. The resizing arrows map across one to one, as raylib has each of them too: all ways, east to west, north to south, and the two diagonals, north-east to south-west and north-west to south-east. Not allowed maps across too, and anything else, such as ImGui's arrow, gets raylib's default. Update cursor starts from raylib's default, for when the mouse is over nothing. While ImGui wants the mouse, as it's over a window, it takes the GUI's, over an NPC, the pointing hand, and over a monster, the crosshair. Each change asks the window system for a new one, so we keep the one shown in a static local, which is set up on the first call and keeps its value from one frame to the next. It starts at minus one, which isn't a cursor, so the first frame sets one. When the cursor differs from it, as it does on the first frame, we set the mouse cursor and keep it as the one shown, and otherwise leave it alone. ImGui's raylib backend sets the cursor to ImGui's own when that changes, which would undo ours. So in the GUI, its config flags get no mouse cursor change, and the cursor is ours, as the world has cursors of its own. In the client's main, we include the cursor's header file, and each frame starts pointing at nothing. Here is where we stand, from our figure, or the origin before it's come, for the talk reach. The cursor needs to know what's under the mouse each frame, and not just on a click, so the picking moves out of the click. While the mouse isn't the GUI's, and we're alive, we look at what's under it. As before, a click on a monster picks it, and one on neither lets go of what we'd picked, and the cursor says which a click would do. We find the NPC under the mouse that's within talking reach of where we stand, and if there's none, the monster. Pointing is an NPC if we found one, or else a monster if we found one, and stays at nothing otherwise. A click then does what it did, with what we found: with no NPC, it picks the monster, or lets go with zero, and an NPC opens its window. And once the GUI is built, the cursor is updated, from what we're pointing at. In the CMake file, the cursor joins the client's core library.
20:11 We build the client's tests at this step, and run the picking tests again. The NPC answers a click from within talking reach, and not from further.
20:21 In the game at this step, from about five and a half metres away again, the click on the guard opens nothing, so there's no quest to take that the server would turn down.
Running up to it
20:31 Back in the game from last time, with no skeleton in reach, Space swings at the air, where we stand. Thrust goes off at the air too, and its slot starts counting down eight seconds of cooldown, spent on nothing.
20:45 Since the first step, a skill with its pick out of reach isn't used, so its cooldown isn't wasted any more, but a swing still swings at the air, and neither gets us there. Most other games run you up to the monster first, and the server moves the players, so that's where it goes. In the world's header file, k close in is how far into the reach a player runs, as a share of it, eight tenths. A player who swings or uses a skill at the monster they picked, while it's out of reach, runs up to that share of the reach first, so that a monster that steps back is still within it. Go takes a player, a direction in x and z, and whether they run. It moves them along that direction, at most a step of that speed long, and less for a shorter direction. And approach runs a player towards the monster they picked, to within a reach, with go. In the world, we include limits, for any distance, which is infinity, as a reach that finds the pick wherever it is. One monster takes a skill's effect, and says whether it hurts the one monster the player picked: it does damage, and doesn't hit all around them, so a heal isn't one. It's the check usable had, in a function of its own now, as act needs it too. In act, the part that runs once the player isn't busy with a swing or a skill changes. Walking is whether the player holds a key to move. A swing or a skill at the monster they picked, out of reach, runs them up to it first, unless they walk off on their own. A skill asked for stays asked for now, while the player runs, so we look at it without taking it out of the input, as std exchange did. It's far when it's usable, hits one monster, and the pick isn't within its reach. A usable skill that isn't far is taken out of the input, and starts straight away, as before. One that's far runs the player up, a step a tick, unless they're walking, and stays asked for, so the next tick looks at it again. Walking off, or a skill they can't use, lets go of it. A swing, while the player holds attack and isn't walking, runs them up to a living pick that's out of the weapon's reach, and otherwise swings, at the pick or at the air. Move goes through go now, with the keys' direction and whether they run, and go is what move did, with the direction and the run passed in, so that approach can move a player its own way. The speed comes from run, each step from to x and to z, and the motion, running or walking, from run again. Approach takes the player, the reach, and the tick's time. It finds the pick, wherever it is, with target and any distance. Then it takes how far away it is, from the player to the monster, and a running step, the run speed, three point one metres a second, times the tick's seconds. The share is how much of a step it takes to get within eight tenths of the reach, between zero and one. So the last step is shorter, and stops within the reach. And go takes the direction to the monster, as long as that share, at a run, which faces the player the way they go. Usable no longer needs the pick within reach, just alive, as a pick out of reach runs the player to it. A skill that hits a single monster still needs a living one picked, so that its cooldown isn't spent on the air.
24:01 In the hotbar's header file, a skill that hits one monster is red now while we've picked none, by its id in target. From this step on, the world on the server brings us to the monster we pick, wherever it stands, and starts what we asked for once we're close enough. So a monster out of reach is no reason to hold back, and that doesn't turn the icon red any more. As before, build returns the skill, if any, that the player used with its key or a click. In the hotbar, we no longer include limits, as its infinity was the distance to a missing pick. The red is renamed k no target, as most other games show a skill without a target in red. The pick becomes a yes or a no, a bool, with ranges any of in place of find if, and how far it is goes, as distance doesn't matter here any more. A skill we know, which hurts and doesn't hit all around, has no target when there's no living pick. Then its icon goes red, in k no target, pushed as the text colour and popped after the button, as before.
25:03 Next, let's write a quick test of the run up. The test of the skill that needs a pick in reach becomes one that runs up to it. The near skeleton goes, and the far one becomes the skeleton, six metres east of the player, past the kick's reach. With nothing picked, the kick isn't used, and its cooldown isn't spent, as before. The part that picked the far one and asked again goes, as a far pick runs up now. Picked, out of reach, the player runs to it, facing it, and kicks once they're there. A tick in, the player is running, east of where they were, facing east, which is a yaw of half pi, and the cooldown hasn't started. Two seconds later, the kick's cooldown has started, so it's been used, and the skeleton has lost health. A second test checks that walking off calls off the run up. It's set up like the first, with the sword and the kick, a player who knows the kick, and a skeleton six metres east. The player picks it and asks for the kick, and a tick later, walks west, along minus x, for a tick, then lets go of the keys. After two more seconds, the kick's cooldown hasn't started, and the skeleton still has all fifty of its health. And a third test checks that swinging runs up too. Its world has a sword and no kick, and a skeleton stands five metres north of the player. The player picks it, and holds attack, standing still. A tick in, they're running, north, along minus z, and after three seconds, the skeleton has lost health to the sword.
26:35 We build the tests at this step, and run the world's tests. They pass, so a pick out of reach runs us up to it.
26:45 At this step, with a skeleton picked several metres away, thrust has the server run us to it, a step a tick, and thrust once it's within eight tenths of the reach, two metres or so. The client sent the skill once, as its key went down, and the world held on to it until we got there. At level six, the thrust's sixty damage is more than a skeleton's fifty health, so it falls, and the client lets go of it, with its ring and its frame.
Wrap-up
27:10 So, today, the server keeps what each player picked, from their input, and the swings and skills that hit one monster go for it. A click picks a monster, with a red ring at its feet and a frame of its own, and the hotbar turns a skill red while it needs a pick and there's none. The cursor says what a click would do, and the NPCs answer one from within talking reach. And a swing or a skill at a pick out of reach runs us up to it, on the server.
Playing it
28:29 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.