nanoMMO

MMORPG pathfinding in C++ with A*, a map and a minimap

A* pathfinding over the island's walkable squares on the C++ server, click to walk there, a map and a minimap drawn from the land, and monster camps spread over it, in our MMORPG.

Watch on YouTube

Our MMORPG gets pathfinding. The C++ server marks each metre square of a land a player can walk on, and finds the way around trees, lakes and lava with A*: a priority queue ordered by the cost so far plus the octile distance left, a cap on how much it searches, and a way straightened to the places where it turns. A search across the island takes about a quarter of a millisecond. A double click on the ground, or a click on the new map, walks you there by yourself. The raylib client draws each land from above into a texture for the map on M and the minimap in the corner, and the monster camps spread over the land, at least thirty metres apart. This is episode 52 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 ep052/end.

  1. 2:48ep052/step-1A starBrowseChanges
  2. 15:31ep052/step-2Walking by itselfBrowseChanges
  3. 20:33ep052/step-3Walking by itselfBrowseChanges
  4. 25:25ep052/step-4A map and a minimapBrowseChanges
  5. 42:28ep052/step-5Camps all overBrowseChanges

Transcript

Intro

0:00 Welcome back, and in episode fifty-two, we're going to find our way around the island. The server gets pathfinding, with A star, and the client a map to click on, and a minimap. Before we build it, let's see where it's going.

0:14 By the end of the episode, M opens a map of the whole land, drawn from its heights, its sea and its trees. A click on it walks us there, past the deep middle of the pond in the way, on a path the server finds with A star, in well under a millisecond on my Mac. And the minimap in the corner keeps track of where we are, and of what's around us.

0:45 The series has a website, with more about how the project is built. The code is open source, on GitHub, linked from there, and each episode gets its own page, with its code at each step.

Last time

0:57 Last time, the swings and the skills started going for the monster each player picked, which a click on it picks, with a ring at its feet and a frame of its own. The cursor started saying what a click would do, and the NPCs answering one from within talking reach. And a swing or a skill at a monster out of reach runs us up to it, on the server.

Today

1:17 First, a look around the middle of the Meadows, in the game as it stood after episode fifty-one. M does nothing yet, and there's nothing in the corner to say where on the island we are. And walking somewhere means holding a key down the whole way, W here, and steering around whatever's in the way by hand.

1:36 Walking, so far, is the client sending the server a direction, from the keys, and the server moving the player a step that way each tick. Walk checks the spot is one a player can stand on: on the land, or no deeper in the sea than a player can wade, and not in a tree trunk. Walkable looks at the height there and the trees in the four-metre squares around it. If the spot isn't walkable, walk tries sliding along one axis, then the other. Those checks are cheap, a few for each player each tick, but walk doesn't look past the next step. So a player heading somewhere far off slides along the first shore in the way, of a pond or the sea, and stops where it can't slide any further. So today, each land gets a grid of metre squares, marked walkable or not, and A star finds the shortest way over them. The server walks a player along it, when they ask with a double click on the ground. The client gets a map, and a minimap, drawn from the land, and a click on the map walks you there too. And as the island is mostly empty, the monsters' camps spread out over all of it. Right, let's start with the squares, in the terrain library, which the server and the client share.

A star

2:48 Pathfinding gets a header file of its own in the terrain library, paths. We include c std int, for the stamps' unsigned integers, and vector, for the squares. Then our lands, for the place, an x and a z in metres, and the terrain, which says where a player can stand. Paths holds which of a land's squares a player can walk on, and finds the shortest way between two places over them, with A star. A world finds a way each time a player asks to walk somewhere, between two ticks. So a search has to stay short, and it looks at a limited number of squares before it gives up and goes as near as it got. A square is a metre along each side. So the island, five hundred and twelve metres across, is five hundred and twelve squares along each side, a quarter of a million of them. A search looks at forty thousand squares at most, which is a little under a sixth of the island's squares. Paths is made from a terrain, and its constructor is explicit, so that a terrain doesn't quietly turn into paths. Find gives the way from one place to another, as the places to walk to straight, one after the other. The last of them is the place we asked for, or the place nearest it that the search reached. The way is empty when there's nowhere to go: the square of the place we asked for isn't open, or the start is off the land. Clear says whether a straight walk between two places stays on squares a player can walk on. Open says whether the square a place is in is one of them. And searched says how many squares the last search looked at, which the tests check. Privately, square of gives the square a place is in, by its index, or minus one off the land. Middle goes back, from a square to the place in its middle. The estimate is how far the goal is along the shortest way there could be, with nothing in the way. And straighten cuts the squares of a way down to the places where it turns, so that each is in sight of the one before. Size is how many squares go along each side, and half is half the land's width, in metres. Open holds a yes or a no for each square, marked once, as the paths are made. Cost is how far each square is from the start, along the best way the search has found to it so far. Above it, we write why the searches' scratch space is kept from one search to the next. A search doesn't have to clear or allocate a quarter of a million of each. A square's cost, and where it was reached from, count while its stamp is the number of the search going on. Came from is the square each square was reached from, to follow the way back. Stamp holds the number of the search that last reached each square. Search is the number of the search going on, which goes up by one each time, and searched counts the squares it looked at. In the terrain's CMake file, paths joins the terrain library, and its test joins the terrain's tests.

5:42 In paths, we include algorithm, array, c math, c std def, functional, for std greater, numbers, for the square root of two, queue, for the priority queue, utility and vector. A square is open when a player can stand all over it: on its middle, its corners and the middles of its sides, which over gives as shares of a square, from its middle. Then a straight walk over open squares has ground under it all the way, and a way that goes past a tree keeps clear of its trunk. A straight walk is checked at points twenty-five centimetres apart, and a diagonal step costs the square root of two, against one for a straight one. The constructor works out how many squares go along each side, from the terrain's half size, and keeps the half size, to go from metres to squares. Then it marks each square, going from its index to the place in its middle, with middle, which comes further down. Two loops go over the shares down and across, and a square is open while terrain's walkable says yes at each of those nine points, so the first no closes it. That's nine checks for each of a quarter of a million squares. On my Mac, a land's squares take about thirteen milliseconds to mark, which the server is going to do once for each land. The scratch space is sized once here, and the stamps start at zero, which no search has. Square of turns metres into a square's index. It adds the half size, so the land's corner is at zero, and rounds down to whole squares, across and down. Off the land, on either side, it's minus one. Otherwise, the index goes row by row, so it's the row times the size, plus the column. Middle goes the other way, from an index back to metres. The column is what's left over after dividing by the size, and the row is how many times it divides. We add half a square to each, to get to the middle, and take the half size off again. Open looks up the square a place is in, and says no for a place off the land. Clear measures the line from one place to the other, and works out how many points a quarter of a metre apart go along it. It goes along the line, from the first point to the last one, which is the place it's going to. And it fails at the first point whose square isn't open, or says yes once they all are. That's cheap, as a walk of ten metres is forty lookups in the list of open squares. The estimate is the octile distance, the shortest way there could be with nothing in the way, moving in eight directions. It goes diagonally as far as it can, then straight. So from the rows and the columns of the two squares, we take how far apart they are across, and how far down. That's the longer of the two in straight steps, plus the square root of two less one for each diagonal step, as many as the shorter of the two. A star finds the shortest way as long as its estimate doesn't overstate the real distance, and this one doesn't. Find starts with the squares of the two places, and a count of zero squares searched. A place off the land, or a goal whose square isn't open, gives no way, and so an empty list. And most walks have nothing in the way, so they go straight, without a search at all. Otherwise, the search gets the next number, which makes each square's old cost stale without touching it. The squares to look at next wait in a priority queue, the smallest cost plus estimate first, through std greater, as a priority queue puts the largest first by default. Each entry is a pair, the cost so far plus the estimate left, and the square. Over it, we write that a square can go into the queue more than once, as a shorter way to it turns up. The longer entries come out later, and are skipped. The start gets the search's stamp, a cost of zero and nowhere it came from, and goes in first. We keep the square nearest the goal we've looked at, and how far it was from it, in case the goal can't be reached. While there's something to look at, and we're under the cap, we take the best square off the top. On my Mac, I also timed searches from the middle of the Meadows to sixty-seven open places, forty metres apart, across the island. The biggest looked at seven thousand one hundred and eighty-one squares, so the cap is well above what the island needs. They took about a quarter of a millisecond on average, and two point four milliseconds at the slowest. An entry whose estimate is more than the square's cost now, plus what's left, is one of the longer entries, and we skip it. The thousandth of a metre is room for rounding. Otherwise, it counts as searched, and if it's nearer the goal than the nearest so far, it's the new nearest. Reaching the goal ends the search, with a break out of the loop. Otherwise, from its column and its row, we look at its eight neighbours, one row up and down and one column across each way. We skip the square we're on, and the ones off the land, past its edges. The neighbour's index goes row by row, like in square of. We skip a closed neighbour, and a diagonal step when either of the two squares it passes between is closed. That step would cut through the corner of what's in the way, and we write that over it. Going on, the cost to the neighbour is the square's cost plus one step, the square root of two for a diagonal. A neighbour we haven't reached in this search, or reached the long way, gets the new cost, where it came from, and the stamp. Then it goes into the queue, with its cost plus its estimate. Once the search is over, we follow where each square came from, back from the nearest one to the start, and reverse it. When we got there, the place we asked for goes last, as the middle of its square can be up to half a metre off it, across and down. A way from square to square zigzags, so straighten cuts it down. It takes where we start, and the squares, and gives back a shorter list of places. Looking from where we are, it goes on from the next square as far as the squares are still in a clear line from us. The farthest one it reaches goes on the way, and it looks again from there, with the square after it. Over it, we sum it up: on to the farthest square still in sight, and again from there, so the way turns where something's in the way, and nowhere else.

11:42 Now, let's write a quick test that the ways go where they should. It includes c math, for how long a leg is, optional, for a tree we may not find, and vector, along with GoogleTest, the lands and the terrain. First, a helper, expect walkable, which takes the paths, where the way starts, and the way. It goes along the way, and expects a clear straight walk from where we are to the next place. When there isn't one, the failure prints the two places, across and down, so we can tell which leg it was. GoogleTest adds whatever we stream into an expectation with the arrows to its message. Then the next leg starts where that one ended, and above the helper, we write that each leg is a straight walk over open squares. The first test makes paths over the flat test board, twelve metres across, with nothing on it to walk around. A walk from minus four, minus four, to four, three, is one leg, straight to the place we asked for. So the way holds a single place, its x is four and its z is three. And the search looks at no squares at all, as clear says yes before it starts. Next, the island of seed one, and its paths. From the middle, a walk to two hundred and fifty, two hundred and fifty, out at sea, past the island, gives no way. And a walk to nine hundred, zero, off the land altogether, gives none either. Then a way around a tree, on the island again, with its paths. We keep the tree we find in an optional, as there may not be one. Over it, we write what we're looking for, a tree with open ground two metres either side of it. We go over its trees, and take the first one with open squares two metres away on each of its four sides. We assert that we found one, and then the walk goes from two metres to one side of it to two metres to the other. The straight line between them isn't clear, as it goes through the trunk, and we assert that first, so the test can't pass on a straight walk. The way has at least two legs, each of them clear, and it ends at the place we asked for. And this time, the search looked at some squares. Last, the longer walks, across the island from its middle, to open ground far off in each direction. The loop heads for places a hundred and fifty metres out across and down, towards each of the four corners. Where that isn't open ground, we move it a tenth nearer the middle, until it is. Each way has at least one place, it's clear, leg by leg, and it ends at the place we asked for. And one more, for what clear doesn't look at. It checks the squares, and a player walking a way steps on the ground in between, so each of those steps has to be on ground a player can stand on. On the island, the loops go over a grid of places to start from, thirty-seven metres apart, and places to go to, forty-one apart, mixed up a little, so that the ways cross the island in all directions. Where the start is on an open square and the end is walkable, we find the way. We start where the player stands, and for each leg, we work out how long it is, from how far it goes across and down. Then we walk along it five centimetres at a time, and the share of the leg we've walked gives the place we're at, between its two ends. Each of those steps has to be walkable, and when one isn't, the failure prints it, and the two ends of the way, and the test stops there. The next leg starts where that one ended. We count the ways we got, as a walkable end can still be in a square that isn't open, which gives no way at all. Last, we expect more than fifty ways, so the test can't pass by finding none.

15:19 We build the tests at this step, in Docker, and run the terrain's path tests. All five pass, so a way goes around the tree, across the island to where we asked, and has ground under each step.

Walking by itself

15:32 Next, the server walks a player along a way. In our proto file, the input gets walk to, field fourteen, somewhere for the player to walk to on their own, around what's in the way. It's sent once, in a single input, and after that, the keys, a swing or a skill call the walk off. A point is a place in a land, in metres from its middle, as an x and a z, which are both floats. In the world's header file, we include deque, for the way, our lands, for the place, and our paths. A player gets a way, the places they walk to on their own, in turn. They go to each place straight, until they get there, or they move, fight or die. The way is a deque, a double-ended queue, as places come off the front as each is reached, without moving the rest. Over the world's constructor, we add that the world's copies of a land share its paths, as they're copies of one land. The constructor takes them as a shared pointer, which is null by default, and without them, the world makes its own. Walk to has the player walk there on their own, around what's in the way. Or it takes them as near as the way goes, if they can stand there. Privately, follow runs the player along their way, a step at a time, each tick. And the world keeps the paths it's given in a shared pointer, next to its content. In the world, the constructor takes the paths, and keeps the ones it's given. Otherwise, it makes its own from its terrain, which is what the tests do. Joining a land forgets the way they walked, like the monster they picked, as it was another land's. Walk to is marked as a zone for Tracy, our profiler, as it's where a search runs, and we want to see what it costs. It looks the player up, and does nothing for a player who isn't in the world, or is dead. It also checks that the place is a finite number, as a client could send anything, such as a NaN or an infinity. Then it finds the way from where they stand to that place, and keeps it as their way. An empty way leaves them where they are. In act, which turns each player's input into what they do each tick, the keys, a skill or a swing clear the way. That calls a walk off, and we say so above it. Further down, after the swings and the skills, a player with a way follows it, and doesn't move by the keys. Follow takes the next place on the way, how far it is, and how far a running step goes in this tick. At three point one metres a second, and a tick of fifty milliseconds, that's about fifteen centimetres. Like the run up to a monster, the last step to each place is shorter, so that it stops there. Share is the part of a step that's left, at most a whole one. Go takes the player that way, at a run, through the walking checks we already have. And when the player didn't move at all, something the squares missed is in the way after all, so they stop, and the way is cleared. A place they reached comes off the front, and the next one is where they head on the next tick. Last, when a monster's hit kills a player, they forget their way too. In the server, an input that has walk to set calls walk to on the player's land, with the point's x and z. In the copies' header file, we include our paths, and each land gets its paths, in a shared pointer that its copies share. And in copies, they're made once, from the land's terrain, as the copies start. Further down, a new copy of a land is a world with a random seed, the land's terrain and our content. It gets the land's paths now too, which each of the land's copies shares, as it's one land. A dungeon gets a copy for each player who goes in, which would otherwise mark the squares again each time.

19:11 Now, a quick test that the world walks a player where they asked. On the test board, a world given no paths makes its own, and a player joins it, in the middle. They ask to walk to four, three, and a tick in, they're running. We step the world one second at a time, three times over. That's five metres, at a run of three point one metres a second, so three seconds is plenty. Then they're at four, three, which expect float equal checks give or take rounding, as the last step lands on the place, not past it. And they're idle, with nothing left of the way. Asking to walk to forty, zero, off the board, where nobody can stand, gives them no way at all. The second test pins down that the keys call a walk off. The player asks to walk five metres east, and a tick later, the keys go south for a tick, and then let go. A second later, there's no way left, and the player is less than a metre east of where they started. And the fuzzer, which plays any input a client could send against the world, gets a case more, to ask to walk to any place. Its floats come from any thirty-two bits, so walk to gets NaNs and infinities too, which it has to turn away.

20:22 Now we build the server's tests at this step, and run the world's, with the new ones for walking among them. They pass, so the world walks a player where they asked, and the keys call it off.

20:34 On the client, the picking header file includes optional now, and the terrain. A double click on the ground asks the server for a walk there, so first, we need the place on the ground under the mouse. Pick ground finds where a ray first meets the ground, if it does within the farthest anyone sees, such as the spot under the mouse. It returns an optional, which is empty when the ray meets no ground that near. In picking, we include raymath, raylib's vector maths, for adding and scaling the ray's vectors, and optional. For the ground, we step along the ray half a metre at a time, up to a hundred and fifty metres, which is as far as the fog lets anyone see, and we write that over the two of them. Pick ground starts with a lambda, below, which captures the terrain and the ray by reference. It takes how far along the ray to look, finds that point, and says whether it's under the ground, from the terrain's height there. We start near at zero, the start of the ray. Over it, we write how the search goes: steps along the ray until one's under the ground, then halving the last step, back and forth, to where it went under. So we go along the ray, a step at a time, and once a step goes under, the ground is somewhere in that half metre, between near and far. We halve it twelve times, keeping the half that goes under. The middle becomes far when it's under the ground, and near when it isn't, with the conditional picking which of the two to set. Half a metre, halved twelve times, is about a tenth of a millimetre, and we return the point there. Otherwise, the step's far end becomes the next one's near end. A ray that doesn't go under within a hundred and fifty metres, such as one into the sky, finds nothing. In the renderer's header file, set destination takes where the player's walking to on their own, if they are, which gets a ring of its own. Main sets it each frame, and the renderer keeps it in a member, an optional, which is empty when we're not walking anywhere. In the renderer, the destination's colour is white, a little see-through. When there's a destination, draw puts a ring on the ground there, at six tenths of the size of the ring under a monster we picked. In the client's main, we're there within sixty centimetres of where we walk to. And a walk we asked for that's standing still after half a second has ended, as we got there, or something stopped us. That half second gives the server time to get the input, and to start us running. In main, we keep where we asked to walk to, next to the monster we picked, and for how long we've been walking. After the input goes out, the keys and a swing call off a walk we asked for, as they do on the server, so we let go of the destination. Arriving in a new land forgets it, like the monster we picked, as it was the last land's. Walk to is a lambda, so that the double click, and later the map, can both call it, and it captures what it uses from main by reference. It copies the input we sent last, so the rest of it stays as it was, sets walk to's x and z, and sends it. Then it keeps the destination, with the ground's height there for its ring, and starts the clock at zero. Each frame, while we have one, the clock goes up by the frame's time. We're there once we're within sixty centimetres of it, across the ground. And we've stopped once the server says we're idle, past the first half second. Once we're there, stopped, or dead, we let go of it. Where we handle the mouse, a double click on the ground, not on an NPC or a monster, looks for the ground along the ray from the camera through the mouse. Ground is declared in the if, before the condition, so it lives just for that if. And we walk there, if it's somewhere a player can stand. The renderer gets where we're walking to each frame, for its ring. Further down, where the hotbar asks for a skill, we send it with what we sent last. And a skill lets go of the destination too, as it calls the walk off.

24:27 Then a quick test of picking the ground. It includes raymath, for the ray's direction, optional and the terrain. We need a terrain, and the flat test board will do. A ray starts ten metres up and ten metres along, and looks down and forward at forty-five degrees. Raymath's normalize makes its direction a unit vector, and over the ray, we write that it looks down at an angle, onto the flat board. Pick ground gives us an optional point, and we assert that it found the ground. The point is at the board's height there, to within a centimetre. And it's as far forward as the ray came down, to within a centimetre too, which is what forty-five degrees gives. Last, a ray from ten metres up, looking straight up into the sky, finds nothing at all.

25:16 We build the client's tests at this step, and run the picking tests. They pass, so a ray finds the ground under the mouse, and nothing in the sky.

A map and a minimap

25:26 The map draws the land from above, in the colours the ground has, so first, the ground shares how it picks them. In the ground's header file, ground colour comes out for the map to use. It takes a land's look, a height, and up, the y of the ground's normal there, which is one on the flat and less on a slope. Above it, we write what it gives: sand by the sea, snow up high, rock where it's high or steep, and grass elsewhere. In the ground, it moves out of the unnamed namespace, as the map is going to call it, and the helpers around it stay in there. The map gets a header file of its own, in the client. It includes raylib, for the image and the texture, c std int, optional and vector. Then our animator, for the figures it marks, the lands, for the place, and the terrain. Draw land draws a land from above, north up, a pixel a metre, into an image in memory. An image needs no window, so its test can call it. Over it, we write what goes in: the ground in the land's colours, shaded by its slopes, its sea and its trees. Land map keeps that drawing as a texture, and draws the minimap and the map from it. It's drawn once, into a texture, as the land is built. The minimap in the bottom right shows what's around you, and the map, on M, the whole land, with whoever's near you on both. The minimap shows forty-five metres from you to each side, so it's ninety metres across. The constructor is the default one, and the destructor unloads the texture from the GPU. Copying is deleted, like our other classes that own something on the GPU. Build draws the land into the texture, in place of the last one, as each land is built. Draw minimap draws the corner, with the figures, the monster you picked, by its id, and where you're walking to, if you are. Draw map draws the map, in a window of its own, while open is true. It takes open as a pointer, as its close button sets it to false. And it returns the place you clicked on, to walk to. Privately, a land map keeps the texture, half the land's size, in metres, and which land it is, for its name and its gates. In the map, we include ImGui, which draws the windows and the markers, and raylib. Then algorithm, array, c math, c std int, optional, string and vector, along with the atmosphere, for each land's look, and the ground, for its colours. The sea is shallow down to a metre and a half, and deeper than that, it's the deep colour. Relief is how much the slopes' light and shade show. The minimap is two hundred points square, and it sits twelve points in from the corner of the screen. Then the markers' colours: a dark outline, a little see-through, white for players, red for monsters, and yellow for the NPCs. Opaque turns a raylib colour into one of ImGui's, which packs it into thirty-two bits, at full alpha. And sky gives a land's sky colour, from its look's atmosphere, which keeps it as three floats, from zero to one, for the shaders. A view says where a place goes on the screen, for a view of the land with its middle at centre. Origin is where that middle is on the screen, and scale is how many points a metre takes. Its call operator takes a place's x and z, and gives the point: how far the place is from the middle, scaled, and added to the origin. X goes across the screen, and z goes down it, which puts north at the top. Draw dot draws a filled circle in a colour, on a dark one a point bigger, so that the dot stands out on any ground. Draw markers is what the minimap and the map share. Over it, we write what it draws: the land's gates, its NPCs, monsters and players, the way you're walking, and you, last, on top, as an arrow the way you face. It takes ImGui's draw list, which collects the shapes a window draws, the view, the land, the figures, the monster we picked, and where we're walking to. For each gate in this land, it draws a ring with a radius of six points, over a thicker dark one. The ring is in the sky's colour of the land the gate leads to, so you can tell where a gate goes from its colour. Then come the figures, and each one's position goes through the view, to a point on the screen. We keep ours for later, so that we go on top. An NPC with a quest for us is drawn as its mark, in yellow: an exclamation mark for a quest to take, or a question mark for one to turn in. The mark is a string of one character, which ImGui measures, to centre it on the NPC. It goes over a dark copy, a point down and to the right, as a shadow. Any other NPC is a yellow dot, a little bigger than a monster's. A living monster is a red dot, and a dead one isn't drawn. The monster we picked gets a red ring around it, so we can find it among the others. Any other figure is a player, and gets a white dot. Where we're walking to is a cross, at the place's point on the screen. A loop draws it twice, first in the dark outline, three and a half points thick, and then in white, one and a half points thick, on top. Each time, it's two short lines, eight points across, from corner to corner. We go last, on top, as an arrow the way we face. Our position goes through the view too, and above it, we write that a yaw of zero faces along z, which is down the map. With the sine and the cosine of our yaw, the turned lambda turns a point, across and along, around ours. The tip is eight points ahead of us, and the two back corners five points back and five points to each side. The arrow is a white triangle, with a dark outline around it. Out of the unnamed namespace, the land map's destructor unloads the texture, if there is one. Draw land takes the land's look, and its size, a pixel a metre, so five hundred and twelve pixels for the island. It makes a black image that size, which raylib keeps in memory, and goes over its pixels as raylib's colours. The heights come from a lambda, which keeps each place it reads, the neighbours too, a little more than a metre in from the edge. That way, the slope at the edge still has neighbours a metre either side on the grid. Then it goes row by row, and across each row, from the middle of each pixel to a place in metres, and the height there. Below the sea, it's the sea's shallow colour, or its deep one, more than a metre and a half down. On land, the slope across and down, in metres a metre, comes from the heights a metre either side, and we write that over it. Up is the y of the normal there, one over the length of a normal made from the two slopes. So it's one on the flat, and smaller the steeper it gets. On a steep slope, up gets small, and that slope turns to rock. With up, ground colour then gives the colour this ground has in the game. Then the light, one plus the two slopes added together, times the relief and up, and kept between fifty-five hundredths and one point three five. Above it, we write what it's for: a light from the top left, as maps have. A slope facing the top left rises towards the bottom right, so its two slopes add up to more than zero, and it comes out lighter, and the others darker. That light and shade is what makes the hills stand out, on a map seen from straight above. Each of red, green and blue is multiplied by the light, kept at two hundred and fifty-five at most, and turned back into a byte, an unsigned char. Then the alpha is set to two hundred and fifty-five, fully opaque, for the sea too, as some of the sea's colours are a little see-through. The colour goes into the pixel at its row times the size, plus its column. A tree is a dark green, tinted by the colour the land's look gives its trees, and we write over it that each tree is a dark spot. For each tree, its pixel is its place plus the half size, rounded down to a whole pixel, across and down. We go over the two rows and the two columns that end at it, so that a tree is a square, two by two. Each one is checked to be on the image, for a tree at the edge, and its pixel gets the tree's colour. Then the image goes back to whoever asked, who has to unload it. Build keeps the land and its half size, draws the land, and unloads the last land's texture, if there's one. It uploads the image to the GPU as a texture, and unloads the image, as the texture has it now. Bilinear filtering blends neighbouring pixels, so that the minimap, which shows ninety metres in two hundred points, looks smooth, without hard edges between its pixels. And the texture is clamped at its edges, which we explain over it. Past the edge, the minimap shows more sea, and not the other side of the island, which repeating the texture would. Draw minimap takes the figures, the monster we picked, and where we're walking to. It finds us among the figures, with ranges find if, and draws nothing before there's a texture, or before we're among them. Its window goes in the bottom right, twelve points in from the corner. A pivot of one, one puts the window's bottom right corner there, so it grows up and to the left. It has no title bar, ImGui sizes it to what's in it, and it can't be moved, navigated to, or focused as it appears. The land's name goes over it, centred over the two hundred points the minimap takes. Texture coordinates go from zero to one across the texture. So a place's coordinate is its metres, plus the half size, over the land's size. We show the texture from forty-five metres to our left and above us, to forty-five metres to our right and below us. Uv zero is the top left corner of that part of the texture, and uv one is its bottom right corner. ImGui's image draws that part of it, at the minimap's size, two hundred points square. We take the image's corners on the screen, and the window's draw list. Then we clip the markers to the image, so that the ones past it don't spill out. Draw markers gets a view centred on us, with the image's middle as its origin. Its scale is two hundred points over ninety metres, a little over two points a metre. Then we pop the clip, draw a dark border around the image, and end the window. Draw map takes what draw minimap does, and the pointer to whether the map is open. It draws nothing, and returns no place, before there's a texture, or while the map is closed. The map is three quarters of the screen's height, or of its width if that's smaller. It goes in the middle of the screen, with a pivot of a half each way. Its title is the land's name, such as the Meadows. After the three hashes, ImGui knows the window by Map, so it stays one window from land to land. Passing open gives the window a close button, which sets it to false. It fits what's in it, doesn't move or collapse, and its place isn't saved in the settings file. Then the whole texture goes in, as an image that size. We take the image's corners on the screen, like on the minimap. The map's view is centred on the land's middle, at the image's middle, and its scale is the image's size over the land's. Over the image, the cursor is a hand. And a click turns the mouse back into a place on the land, the view the other way round: from the origin, divided by the scale. Then the markers, clipped to the image, like on the minimap, and a line of grey text under it, that a click walks you there, and M closes the map. Last, it returns the place we clicked, if we did, for main to walk to.

36:51 In the client's main, we include the map, keep one, and whether the map is open, which starts closed. As a land is built, its map is drawn too, behind the cover. Escape now closes the map, if it's open, and otherwise lets go of the monster we picked, as before. And M opens and closes it, unless we're typing into a text field, such as the chat's. With the other windows, the minimap is drawn each frame, from the renderer's figures, with the monster we picked and where we're walking to. Then the map, while it's open, which gives back the place we clicked, if we did. A click on the map walks us there, through walk to, like a double click on the ground, if it's somewhere we can stand. In the CMake file, the map joins the client's core library, and its test the client's tests. And a quick test of the land the map draws, which needs no window, as draw land gives an image in memory. It includes raylib, for the image, the atmosphere, for the Meadows' look, GoogleTest, the lands and the terrain. Pixel at reads a pixel's colour from an image, at its row times the image's width, plus its column. And a second helper checks that two colours match on red, green and blue, and leaves out alpha, as the map makes each pixel opaque. We draw the island of seed one, as the Meadows, the first land. The image is five hundred and twelve pixels square, a pixel a metre. Its corner, at zero, zero, is the deep sea's colour. Above that, we write what we check: deep sea in the corner, and land in the middle, where the players start. The middle pixel is neither the deep sea's colour, nor the shallow sea's. Then we take the island's first tree, and over it, we write what we expect of it, something dark where it stands. Its pixel is at its metres plus two hundred and fifty-six, across and down. Its red, green and blue add up to less than three hundred. Last, we unload the image, as raylib allocated it for us.

38:53 The map's test runs with the client's tests, built at this step, in Docker. It passes, so the island comes out a pixel a metre, with its sea and its trees.

In the game

39:05 Let's see it in the game at this step. The minimap in the corner shows the land around us, with the skeletons near the middle in red. M opens the map of the whole Meadows, with its ponds, the hills and the woods, from the texture drawn as the land was built. The click sends the server a place west of the pond, and the server finds a way past its deep middle, through the shallows a player can wade in, and walks us along it, a step each tick. The client draws our arrow on the map each frame, from where the server says we are. With the map closed, the minimap keeps track of the walk, with a cross where we're going, and the white ring marks the spot on the ground once it's in sight.

Camps all over

39:51 Last, the camps, as the map shows them before this step, from near the middle of the Meadows. The map draws the monsters the server tells us about, the ones near us, in red. Besides the skeletons by the gate, that's three small camps, and nothing else in sight.

40:09 Until this step, the Meadows and the Ashlands had eight camps of monsters each, and the Crypt twelve. The land's seed picks their places at random, and random places bunch up and leave big gaps, so most of the island is empty. From this step, the camps spread out over all of it. In the monsters' header file, camp spacing is thirty metres, the least there can be between the middles of two camps. Camps, which picks the places for a land's camps, now spreads them out over the land, that far apart. It gives as many as we ask for, or as many as it finds room for in its tries. In monsters, the loop tries up to fifty places for each camp we ask for, in all, before the land counts as full. Camps still picks its places with the terrain's seed, so each time the server starts, a land's camps come out where they did the time before. A lambda, spaced, captures the camps we have so far, by reference. It says whether a place is at least thirty metres from each of them, with ranges none of. Over it, we write how it goes: the camps are darts thrown at the land. A place too near a camp we have is thrown again, as is one in the sea, so the camps spread over the land evenly. Places picked at random alone bunch up and leave gaps. Spreading points out like this gives what's called blue noise. The loop gets fifty tries for each camp we ask for, where it had a thousand in all. And a place has to be spaced now, as well as away from the middle, between the beach and the rock, and somewhere a player can walk. In the content, the Meadows and the Ashlands get forty camps each, and the Crypt twenty-four. The Crypt is a dungeon, so each player who goes in gets a copy of it, with its monsters. And a quick test that the camps spread over the land. It includes algorithm, for ranges any of, and c std def, for size t. On the island of seed one, we ask for forty camps, and assert that we get all forty. Then two loops go over the pairs, each camp with the ones after it, and expect each pair to be at least the camp spacing apart, middle to middle. And each quarter of the island has a camp in it. For each of the four quarters, a sign for x and one for z, ranges any of looks for a camp whose x and z have those signs.

42:29 We build the server's tests at this step, and run the monsters' tests, with the new one for the camps among them. They pass, so the camps spread over the island, at least thirty metres apart.

42:40 At this step, from that spot again, the map has camps in red all around us, each a good way from the next.

Wrap-up

42:48 So, today, each land got a grid of metre squares, marked walkable or not, and A star finds the shortest way over them, around what's in the way, with a cap on how much it searches. The server walks a player along it, when they ask with a double click, or with a click on the new map, next to the minimap. And the monsters' camps spread over the whole island now.

Playing it

43:58 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.