nanoMMO

Playing my C++ MMORPG offline in a browser, without a server

Our C++ MMORPG's client runs the world's own rules on its own, natively and in a browser tab, after a raylib bug turns each person's hand into a spindle in WebGL.

Watch on YouTube

Our C++ MMORPG plays offline: the client runs the world's rules on its own, from the library the server uses, so a browser starts straight into the world without a server. A local connection gives the client a world through the interface its network connection already has, at the server's tick, with the server's own code for inputs and for what a player is shown. In the browser, tall spindles stand by each person, which comes down to raylib sending WebGL its bone matrices untransposed, and a patch fixes it. This is episode 78 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 ep078/end.

  1. 1:53ep078/step-1A world in the clientBrowseChanges
  2. 11:42ep078/step-2Spindles in the browserBrowseChanges
  3. 13:13ep078/endWriting it downBrowseChanges

Transcript

Intro

0:00 It's a sunny day in Manchester today, which doesn't happen often!

0:05 Hi, and welcome back to episode seventy-eight, where we're going to play the game without a server, on a computer and in a browser tab, with the client running the world's rules on its own. It's the payoff of the last two episodes, and on the way, a little bug in the browser that I really enjoyed hunting down. First, a look at where we'll be by the end.

0:25 The page starts straight in the Meadows, with the monsters and the people from the content, and there's no server anywhere. The world is running inside the page, with the server's own rules, and nothing is saved.

0:39 As always, the code is open source, on GitHub, linked from the series' website, where each episode has a page with its code at each step.

Last time

0:47 Last time, the world's rules moved into a library of their own, nanommo world: the world, its monsters, NPCs, levels and trades, and who's near whom, without a database or a network. What each player is shown of the world moved out of the server and into it, and so did what a player's inputs do, each with tests of its own. The server uses them through the library, so the client can link the very rules the server runs.

Today

1:11 The client as it stands starts on the login screen, as always, and on this computer, there are no servers running. Logging in asks the login server, which isn't there, so the screen says it couldn't reach the server, and that's as far as the game goes.

1:26 The client reads its world through a link, the interface from two episodes ago, so a second kind of link can run a world inside the client, with last episode's library. We'll try it on a computer first, with a flag, then in a browser, where there are no servers to reach anyway. And something odd is going to stand next to each person in the browser, which takes us into how the GPU gets a skeleton's bones. All right, let's get started, with the local connection.

A world in the client

1:53 The local connection gets a header file of its own, in the client. We bring in chrono, c std int, deque, memory, optional, string and vector from the standard library. Then the link it implements, the content, our protocol and its parts, and from the world's library, the inputs, interest, for who's near whom, and the world. A local connection is a world of the client's own, without a server: the world's library, run in the client at the server's tick, for one player, in the first land. Nothing is saved, and there's nobody else around. It's made from the content, and the name the player goes by. Then come the link's functions, each overriding the interface's. Poll is where the world moves on, and send takes the player's input. Connected is always true, as there's nothing to lose a connection to. The world is what the player's shown of it, built each tick as a world server would build it. The chat hands back its lines, with the newest last. Whole is the newest world that came whole, from the parts. Move is where a world's instance sent us, and it stays empty here, as there's no other instance to go to. The bag and its gold, and the quests, with how far along each one is, hand back members named after them. So does the guild, which stays empty, as guilds live on the servers, and the trade, which stays empty too, with nobody to trade with. Take events hands over what happened near the player, and take invite hands over a guild's invitation, which doesn't come offline. In private, tick runs one of the world's ticks, show builds what the player's shown of it, and notify and say add a line to the chat, from nobody, or from someone. The player's id is one, as nobody else is here, the world's seed is one, and the land is the first one, the Meadows. Then the members: the content, shared with the world, the world, the inputs, with the queue a world server keeps, and the cells the NPCs stand in, worked out once, as they stand still. When the next tick is due, and a count of the ticks that starts again each second, for the NPCs. Whether the player was in a gate last tick, and the parts, which put together the world a server sends in pieces. Last, what's shown, the chat, the move, the bag, the quests, the guild, the trade and the events, as the link to a world server keeps them. So the game can read either kind of link without knowing which it has.

4:15 Now for the local connection's code, which does the work. We bring in algorithm, chrono, c std int, i o stream, memory, optional, string, utility and vector, then Abseil's status, our clock, and from the world's library the monsters, the NPCs and shown, then the lands and the terrain. The tick is the server's, fifty milliseconds, which is twenty a second. After a long frame, the world catches up a few ticks at once, and five at most. The NPCs go out once a second, on each twentieth tick, as from a server, and the chat keeps fifty lines. The constructor keeps a shared copy of the content, and makes the world, from the seed and the Meadows' terrain, with the first tick due right now. It spawns the land's monsters and NPCs, as a world server does when it starts. It puts the NPCs in their cells, and the player joins by name, as a new player, so in the middle of the land, with the starting kit. It writes how many monsters and NPCs there are to the log, which is handy in a browser, where the log is printed under the page. And it tells the player in the chat that they're playing offline, that the world is theirs alone, and that nothing is saved. Poll runs once a frame, from the game's loop. For each tick that's due, up to five, it ticks the world and moves the next one on by fifty milliseconds, so a slow frame catches up. If it's still behind after that, as when the page was hidden for a while and the browser stopped drawing it, it carries on from now, instead of racing through all the ticks it missed. Send does what a world server does with an input. Its keys go to the inputs from last time, so numbered ones wait for their tick, as they do on a server. Then each thing an input can ask for goes to the world, in turn. There's coming back to life after dying, walking to a spot picked on the map, which the world finds a path to, and using a skill, by its id. Some of them can fail, so each one's result goes into a status, which keeps the first error. There's learning a skill from an NPC who teaches it, taking a quest from an NPC who gives it, and turning a quest in to them once it's done. Then there's an item moved, used or thrown away, through do item, the server's own code, which moved into the world's library last time. If one of them failed, the player's told why, in the chat. What they say goes in the chat under their name, with nobody else around to hear it. And for what lives on the servers, the chat says so: the stories, which come from a server's storyteller, the guilds, which the servers keep, and a trade, which takes another player, and there's nobody else offline. Take events hands over all the events since the last time, and it does that by swapping them for an empty list, so each event goes to the game once, and take invite has nothing to give. A tick is a world server's tick, for one player. The inputs hand each player their next keys, in this world, whoever they are, and the world steps on by fifty milliseconds, so the monsters move and fight, and blows land. Whatever the world has to tell the player goes in the chat. Then, with the player in hand, each event the world recorded becomes what players see of it, through for players, the server's code. It's kept if it happened near the player, as a world server sends each player the events near them. A gate takes a player to another land's world, which lives on a server. So first we work out whether the player's standing in one of this land's gates, by checking each of the gates in turn. If they are, and weren't on the tick before, the chat says that the gates lead to the servers' other lands, and there are none offline. It goes once, as they step in, and not again with each tick they stand there. Then it shows the player the world, and keeps their bag and their quests as a server would send them. Show builds the world the player's shown, as a world server builds its datagram. Once a second, a tick is a full one, with the NPCs in it. Views, the server's interest code, groups the players, monsters and NPCs by cell, with what each cell's players get. We want the view from the player's own cell, so the rest are skipped. Each player near them goes in, which offline is the player alone, then each monster near them, with where it stands and its health, and on a full tick, each NPC near them. They all go through to protocol, the server's code for each of them, so the client can't tell a skeleton from this world from one a server sent. The world gets the time it was sent, as from a server, and the player's own part goes last, as in a server's datagram, through you in, with the last of their inputs that was applied, for prediction. It goes into the parts, as a server's world does in the client, and what's shown is the world they put together. Notify is a line from nobody, and say, which notify calls with an empty name, makes a chat line from a name and some text, the way a line from a server looks. It adds the line to the chat, and drops the oldest once there are more than fifty. I like how small this turned out to be, as nearly all of it calls code the server runs, so a change to the rules reaches both at once.

9:15 Whether the servers can be reached at all is a question for the build, so the servers' header gets a function that answers it, can reach servers. On a computer, the answer is yes, and in a browser, it's no. In main, we bring in string view, and the local connection. Whether we're offline is worked out once, as the client starts, and a string view lets us compare the first argument as it is. The client plays offline when it's started with dash dash offline, or in a browser, which can't reach the servers. So now there may be no servers at all, and the pointer to them starts out empty. Online, they're made as before, from the host and the transport, and the client stops if they can't be. Once they're made, they're moved into the pointer, and offline, it stays empty. Where the client enters a world, offline, it makes a local connection, from the content, for a player called Traveller, and the rest of the game takes it as it takes a connection to a server. The servers are a pointer now, so their calls use an arrow. The login screen asks the login server when we're online, and offline, there's nothing to log in to, so the client goes straight in, under the loading screen, into a world called Offline. And logging out starts the world afresh, with a new Traveller. In the client's CMake file, the client builds the local connection, and links the world's library, in a browser too. And in the top CMake file, the game moves on to version zero point seventy-eight, for this episode's code.

Offline on a computer

10:42 On a computer, with the offline flag, the client goes past the login screen and straight into the world, as Traveller, and the chat says that the world is theirs alone. When the player goes for a skeleton, the blows, the damage and any gold it drops all come from the world's library, running inside the client, twenty ticks a second. And walking goes through prediction, as it does online, with the local connection answering as a server would.

Spindles in the browser

11:06 In the browser, the world's there too, and it plays from the start. But next to each person stands a tall, pale spindle, in their skin's colour, which the client on a computer doesn't draw. To find out where it came from, I switched the drawing off a pass at a time, with a switch in the page's address that I kept in my own copy of the code, not in these steps. The spindles went away with the people, then with their bodies, and then with a single part of the body: the right hand. Then I logged the bones' matrices, on a computer and in the browser, and they were equal, so the difference had to be in how those matrices reach the GPU.

11:42 raylib keeps a matrix row by row, and OpenGL wants it column by column. So on a computer, raylib sends a skeleton's matrices as one array, and asks OpenGL to transpose them as it takes them. On WebGL, raylib doesn't ask for that, as the first version of WebGL couldn't, so for a single matrix, it transposes it first, on the CPU. But for an array, it sent them as they were. Each bone arrived transposed, with its translation in the number the GPU divides each point by. That left each part of the body a little off, and stretched the right hand into that spindle. The patch is a few lines in raylib's rlgl header. It makes room for the transposed matrices, fills it with each matrix as raylib converts a single one, sends that to WebGL, and frees it again. The game keeps its patches to the libraries it fetches in the patches folder, like civetweb's from episode twenty-eight, and CMake applies this one as it fetches raylib. On a computer, the patched lines aren't compiled at all, so it changes the browser's build alone.

12:52 With the patch, the browser's build fetches raylib again and patches it. And now the people stand as they do on a computer, and their arms and hands look right too, as each of their bones had been a little off. I'd really like to send this patch to raylib, as anyone who skins a model on the GPU with raylib in a browser is going to bump into it too.

Writing it down

13:13 Last, the game's docs, where the page about the browser now says how to play offline, in a browser and with the flag. It says what offline has and what it leaves out, and the spindles go in its table of things that can go wrong. And the choice gets its decision record, number thirty: offline, the client runs the world's rules on its own, from the library the server uses, instead of a server for the website's players, reached over WebSockets.

Wrap-up

13:42 Today, the client got a world of its own, a local connection that runs the world's library at the server's tick, through the server's own code for inputs and for what a player's shown. It plays offline on a computer with a flag, and in a browser from the start. And a patch to raylib sends WebGL its bones' matrices transposed, so each person stands as they should. That's the web arc done for now, and I think it's a nice place to leave it, with the game playing in a browser, without a server.

Playing it

15:01 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.