Skip to content
Adrian Mircea Nenu computo, ergo sum
← Blog
games software engineering distributed systems

Building an MMO, Educationally from Scratch in C++

I've been building nanoMMO, an MMO written from scratch in C++, on a YouTube series where every step of the build is on video. Ninety episodes in, it has become a game you can play in your browser, with a lot more engineering around it than I expected when I started.

Where it comes from

I've always been passionate about game development and complex systems, from Borland Pascal as a teenager to C++ and competitive programming, with websites, games and mobile apps in PHP and C# built on the side. One of those games is Secret Republic, a hacker role-playing game in the browser where you break into simulated servers, crack passwords and run SQL against live databases. I first launched it in 2012, and it is open source, with four versions of it by now.

In 2017 I tried an MMO in Swift as a research experiment into how MMO servers and clients work, with a login server and a world server talking in packets I laid out byte by byte, and nanoMMO still splits the work between servers in the same way. In 2021 I wrote Omen, a backend in Scala for old-school browser games in the style of Travian and Tribal Wars, where you describe a whole game in YAML. Then in 2023 I started Hades, a 3D game engine in C++ with an editor and a Vulkan renderer that can train reinforcement learning agents too.

nanoMMO picks up from all of these, down to the monsters and items being written as data, the way Omen describes a game in YAML. The difference this time is that I'm building the whole thing out in the open, one episode after another.

The name is a working title that takes after nanochat, Andrej Karpathy's repository with code kept minimal and easy to hack on. That is what I'm going for as well, a codebase that covers an MMO from end to end and that you can read, run yourself and play with.

The engineering beyond the gameplay

What drives nanoMMO most of all is my interest in complex software engineering at scale and in the practices that keep large systems running. That interest comes from my years at Morgan Stanley and Google building large distributed systems, where I came across most of what the series covers, though never at a game company.

To me, an MMO is one of the best excuses to bring all of those practices together in a codebase small enough to read. Your average game devlog tends to focus on flashy graphics or a new gameplay mechanic, and nanoMMO has a share of both, but most of the series goes into the software engineering and DevOps side of a game like this, from the choice of databases and Docker to tests, monitoring and deploys.

I want to show what comes into play in a production-like setup, especially for people at the start of their careers, so that when a problem shows up they understand what happens behind the scenes and can debug it, whether or not an AI writes a lot of the code for them. It's the same point I made about junior engineers, that a deep understanding is what lets you use these tools well, instead of handing all the thinking over to them.

By now there's a lot more than a game server, with a client that draws the world with raylib and sends what the player does, while the servers decide what happens. The client logs in over TLS through a login server of our own, against accounts kept in MySQL, and a browser comes in through a WebSocket gateway we wrote from the RFC. Every world registers with Consul and keeps the players in Redis, and a saver writes them behind to MySQL. What the players do goes into ClickHouse for analytics, and Prometheus, Grafana, Loki and Pyroscope keep an eye on all of it. Most of the players you see in the videos are bots that play the game over the network the way a real client would, with as many as two thousand of them online at once.

The game as it is today

The lands of nanoMMO are separate islands, and you travel between them through gates, from the Meadows out in the sea to the Ashlands in a sea of lava, and on to the Crypt, a dungeon where whoever goes in gets a copy of their own. The server works out the time of day and the weather for every land from the wall clock, so all the servers agree on it and there's nothing to save, and the client draws the sunsets, the rain and the stars at night.

The skeletons of the Ashlands and the Crypt show their attacks as shapes on the ground before they land, filling them in as the blow gets closer, so you have a moment to step out of the way. The Bone Colossus, our first world boss, is a skeleton four and a half metres tall that fights in three phases and goes for whoever has hurt it the most, using a threat table that all the monsters keep now.

Two players can agree to a duel that ends once one of them is down to one health, or trade items and gold in a window where both of them have to say they're ready. Players can start guilds too, with a roster, ranks and officers kept in MySQL one transaction at a time.

What went wrong first

A lot of what we built came from something going wrong first, and those are some of my favourite episodes, because they're the parts most tutorials skip and the ones that decide whether a game server holds up once players arrive.

In episode thirteen, saving to MySQL after every change fell behind with only a few dozen players, which is why Redis went in between, with a saver that writes to MySQL in batches. Deploys used to throw everyone out of the game too, until a world learned to hand the players over to a newer version of itself before stopping. With two thousand bots, a world fell from twenty ticks a second to about eight, and a flame graph showed where the time went, all the way from the broadcast down to the kernel's UDP stack.

The network taught us plenty as well, starting with a connection slowed down to a 150 millisecond round trip, where our player started and stopped walking late, so the client now walks you the moment a key goes down and lets the server correct it. When packets got lost, everyone else lurched, so the client now draws other players a tenth of a second in the past, between two updates it already has. In a duel, a blow that looked as if it landed could still miss, so the server now checks it against where the attacker's screen showed the opponent, up to 200 milliseconds back.

The client runs on the player's machine, so whatever it sends can be made up, which is why the game logic stays on the server. When a red-team client of ours tried to cheat, the server already stopped most of the tricks. Trading was another story, as the red-team client found two ways to turn two swords into three, and a third by playing one character on two servers at once during a deploy. We fixed all three on the server, with both sides of a trade saved in a single Redis transaction, a lease per character and an alert in Prometheus whenever an item is held twice.

If you'd like to pick a few episodes to watch, I'd go for those, as dupes like these come up in most other games too, and then for the chaos testing episodes. In those, we froze Redis, MySQL and the login server while the bots played, and found a sword that existed twice and players dropped while our numbers said all was well, before making the game resilient to it and writing our first postmortem.

Why the game is built the way it is, and what we weighed and turned down along the way, is written up in the decision records. What broke and was hard to get right is gathered on the hard parts page of the series' website, next to the episodes where it happened.

Playing it in a browser

More recently, the client learned to build to WebAssembly, so the game now plays in a web browser, either offline with the world's rules running in the page, or online through that WebSocket gateway. On the way there, raylib sent WebGL the bone matrices untransposed, which stood a tall spindle next to every person in the world until a patch fixed it. You can try it on the Play page of the series' website, with nothing to install and no server needed.

Where it's going

I should say up front that what I'm about to describe might not scale in the end, for one reason or another, but what interests me most is dynamic intelligence and a world that evolves. I'd like to end up with something like Sword Art Online, in the sense of a world that can adapt and grow by itself.

Along the way, I want to build features I'd like to see exist, even if some of them might be impractical for most games, such as a system for players to create their own skills. Further down the line, an AI agent could generate quests on the fly for a specific player, and items too.

We've already taken a small step in that direction, in episode twenty-five, with a storyteller NPC that writes you a story from a few words using mvGPT, the small GPT from my other series, served next to the game. In episode ninety, the game's content went into smollerGraph, the graph database from another of my series, as a graph of quests, monsters, items and skills, with a test in CI that fails whenever a new player can no longer reach one of them.

Following along

The website of the series has a page per episode, with the code at every step. The game's code is on GitHub under the MIT licence, and all the steps of all the episodes are tagged in the repository, so you can check out the code wherever you are in a video, build it and run it yourself. If you're new to it, your best bet is episode one, which sets out the plan, followed by the rest in order as they go up on my YouTube channel.

Two books go well with the series, starting with Design Patterns by Gamma, Helm, Johnson and Vlissides, which gave names to patterns like Observer, Command and State. Robert Nystrom's Game Programming Patterns then goes back over them as they turn up in games and adds the ones games need, like the game loop, and it's free to read online!

Latest videos

YouTube