|
nanoMMO 0.97.8
Building an MMO: The Engineering Series
|
The server sends each client the world over TCP, or over UDP once the client asks for it. To see what a bad network does to each, compose.yaml has a netem service. It runs tools/bench/netem.sh in the server's network namespace, with the NET_ADMIN capability, and uses Linux's netem to delay the server's packets and drop a share of them. It takes a delay in milliseconds, then a loss in percent.
It turns off segmentation offload on the server's interface first. Otherwise the kernel hands netem up to 64 KB at a time, to be cut into packets later, and netem drops all of those packets together. It also lets netem hold 100,000 packets, as its default of 1,000 drops packets of its own once a busy server has more than that waiting.
tools/bench/loss.sh starts 200 bots over the transport you give it, then steps the loss from none to ten percent, with 50 ms of delay, and after a minute at each step asks Prometheus for the p50 and p99 of the bots' world age. Like load.sh, it needs curl and jq.
Without loss, the two are level, with a world up to a tick old, plus the delay. Over TCP, the p99 climbs with each step of loss, as the worlds wait for each lost packet to be sent again, and at ten percent TCP can't keep up with 200 clients at all. Over UDP, the p99 stays within a tick or two of no loss. So the client and the bots take their worlds over UDP by default.
This is how the world gets there over UDP.
Each tick, the server sends each player the players and monsters near them, which is called interest management. Sending everyone the whole world grows with the square of the players, and most of it would be hidden in the fog anyway.
With bots that spread over the island, tools/bench/load.sh 100 12 on an Apple M4 Pro, with the bots on the same machine as the server, gives the numbers below.
| Each player gets | 20 ticks a second up to | At 1,200 players | Sent to each player, with 500 |
|---|---|---|---|
| The whole world | 800 players | 10.4 ticks a second | 377 KB/s |
| Within 96 m | 1,000 players | 15.5 ticks a second | 228 KB/s |
| Within 64 m | 1,200 players | 20, ticks of 36 ms (p50) | 135 KB/s |
| Within 64 m, names and levels once a second | 1,200 players | 20, ticks of 26 ms (p50) | 94 KB/s |
On a VPS with 2 vCPUs, with the bots on it too, the game server now keeps its 20 ticks a second up to about 700 players, where before it fell behind past 400.
The world comes each tick, and the newest replaces the last, so a client knows how things are, but not what happened in between, such as who hit whom, for how much, or that a potion was drunk at the moment a skeleton hit. Those are events, which come once each, and the client floats each over the unit it's about, as combat text.
The server decides where everyone is, so with nothing more, a key you press moves you a round trip later. At 150 milliseconds, you start and stop walking a beat after your fingers. The client walks you at once instead, the way the server will, and the server's answer corrects it.
| Where | What |
|---|---|
| The client | Sends its keys once each of the server's ticks, 20 a second, each numbered, from 1 |
| The server | Applies a player's numbered keys in order, one a tick, before the world steps, keeping a few that arrive together and dropping the oldest past five. A slow frame on the client sends two or three together, so with more than three waiting, it's fallen behind the client, and skips one a tick until it's caught up. Inputs come in as fast as they go out, so a queue a burst filled would stay full. If two or more waited at the start of every tick for a second, it drops one |
| Each world | Says the last of your inputs the server applied (acked), and whether it moved you itself that tick (steered), with where you are, in every datagram |
| The client | Shows the round trip, from sending an input to the world that says it was applied, under the minimap, or in its place with the minimap off |
To feel it, put a delay on the server's packets with the netem service above, and walk.
Worlds come 20 times a second, but not evenly. One can be late, two can come together, and over UDP one can be lost. Drawing each player and monster where the newest world has them shows that as a stop and a lurch. So the client draws everyone else as they were a moment ago, between two worlds it already has (client/interpolation.h).
Each world used to carry everyone near you, whole, 20 times a second, so a player standing in a crowd cost the same as one walking through it. Now each world leaves out what hasn't changed since the newest world your client has.
With forty bots on one machine (nanommo_bot localhost localhost 40 5 1 udp), the server sent 16.8 KB a second to each before, and 7.6 KB after, from nanommo_bytes_sent_total. Those who move still go whole each tick, as a player walking changes where they are, and so their piece.
A duel (Gameplay) is the first time what one client sends hurts another player, so the server keeps it to the two who said yes, and checks the rest as it does a trade.
| Message | Field | What |
|---|---|---|
| Input | duel, 19 | An ask, by name, a yes, a no or a yield, once, over TCP |
| Input | target_player, 20 | The player picked, with each input, beside the monster's target. The world keeps it only for the opponent in a duel that's started, or an enemy at war (below), so it can't hurt anyone else |
| World | duel, 27 | Who with, by name and id, how far it's got, the countdown's milliseconds and the flag. On its own, over TCP, to both sides, as it's asked for, at each second of the countdown, and as it starts and ends, with nobody in it once it's over |
| Player | target_player, 16 | While they use a skill in a duel, the player it's for, so that its fx land on them on everyone's screen, as target does for a monster |
| Input | drawn, 21 | When, on the server's clock, in microseconds, the client was drawing everyone else as it sent it, with each numbered input. A blow on the opponent is checked against where they were then (below) |
A guild war (Gameplay) lets the players of two guilds hurt each other without a duel, so Input's target_player, 20, is kept for a player of the guild at war with yours too, and a blow on them is checked as a duel's is (below).
| Message | Field | What |
|---|---|---|
| GuildAction | war, 11 | A leader's ask, by the other guild's name, or a yes, as asking back is, once, over TCP |
| Guild | war, 4 | The guild's war, with its news, a GuildWar. It has the enemy guild, by name, kills and enemy_kills, from your side, as MySQL has them, a few seconds behind, and left, the seconds it had left, which the client counts down from. Nothing, for no war |
The news comes as the war starts, as its score moves, and as it ends, as the world reads its wars every five seconds. Each kill is a line from nobody in both guilds' chat, on CHANNEL_GUILD, which the client shows in green, after [Guild].
Crafting (Gameplay) changes the bag, so the server checks it as it does a move in the bag, and the client says no more than which recipe.
| Message | Field | What |
|---|---|---|
| Input | craft, 22 | A CraftAction with start, a recipe by its id, or stop, once, over TCP. Three at once, and then one a second |
| World | crafting, 29 | While you're making something, a Crafting with the recipe, by its id, and done, how long you've been at it, in milliseconds, which the client fills its bar from, with the recipe's seconds from its content. In your own part of every world, as respawn is, up to 15 bytes, which each datagram leaves room for. Nothing, while you're making nothing |
What's made goes into the bag, which comes as it always does, once its revision moves, and why something couldn't be made, or that it was, is a line from nobody.
A player chooses how their character looks as they make them (Gameplay), and it never changes, so the looks go as four small numbers, a Looks. They are build, a place in the rig's builds, and skin, hair_color and hair, places in the figures file's lists. Each client wraps each round to what its lists have, so any number draws as a look there is, the same on every client in the world, and there's nothing for the servers to check but that it fits its column (Accounts).
| Message | Field | What |
|---|---|---|
| LoginRequest | looks, 13 | With create_character and the name, the looks chosen. None, as from a bot, and the character's id picks them |
| CharacterListing | looks, 9 | Each character's, for the characters' screen to draw them in, unless it's from before there were looks to choose |
| Player | looks, 17 | Everyone's near you, once a second, with the name, which the client keeps in between, as it does the name. A player with their name and no looks has none, as from before there were looks to choose. Your own part of each world leaves them out, as it does your name |
The quest log, on T, gives up a quest (Gameplay), which takes no NPC, so the client says no more than which.
| Message | Field | What |
|---|---|---|
| Input | give_up, 23 | A quest you're on, by its id in the quests file, once, over TCP. The world takes it off you, kills and all, and your quests come again, as World's quests, 17, at their next revision. Zero, the default, gives up nothing |
Giving up a quest you're not on, or one you've turned in, is a line from nobody that says "You're not on that quest."
A player swears to a faction at its herald (Gameplay), once, so the client says no more than which herald, and everyone near sees the faction as they see a guild.
| Message | Field | What |
|---|---|---|
| Input | enlist, 24 | A herald, by the NPC's id, once, over TCP. The world checks it as it does a trainer, for an NPC in your land, within talking reach, who's a herald, and you in no faction yet. Zero, the default, swears to nothing |
| Player | faction, 18 | The faction they've sworn to, by its id in the factions file, of everyone near you, once a second, with the name, which the client keeps in between, as it does a guild. A player with their name and no faction is in none. Your own part of each world leaves it out, as it does your name |
| Arg | faction, 11 | A faction in a server's sentence, by its id, "You've joined $0.", which the client names in its player's language, in the context faction 1 |
What's refused is a line from nobody. It's "Nobody here swears players to a faction.", "You're too far away to talk to the Herald of Concordia." or "You're sworn to Concordia already.". A faction is a byte or two in each player's piece of a full world, and nothing in the worlds between.
A client draws everyone else 100 milliseconds in the past (above), and its keys reach the server a round trip after the world they answer left it, so by the time a blow lands, the player it was aimed at has moved on from where the attacker saw them. Running, at 3.1 metres a second, with a round trip of 50 to 200 milliseconds, that's 0.5 to 0.9 metres, against a sword's reach of 1.6, so a blow that looked in reach misses, and one that looked as if it would miss can land. Making up for it is called lag compensation, and the server does it for the blows of a duel or a war.
The cost falls on the player who's hit. Running off, out of reach on their own screen, they can still be hit where they were up to 200 milliseconds before, 0.62 metres behind them at a run. A client can always say it's the most behind, so a modified one gets that whatever its network, about 100 milliseconds more than a client beside the server (stale, below). Over a round trip of 100 milliseconds, a player has to lead a runner a little, as everyone did before. Why it's 200 milliseconds, and duels only, is decision 34.
To see it, put a delay on the server's packets with the netem service, start a duel, and have the other player run out of reach as a blow comes.
Each player's own part of the world carries the sky over their land, World's sky (28), with the time of day, the weather, how strong it is, and the wind (Graphics, decision 0037). The server works it out from the wall clock (server/weather.h), so it's nothing to keep, and it's about 25 bytes, so it goes in every world, and a client that loses one has it again a twentieth of a second later. Parts keeps the newest world's.
What the servers say, a refusal, a guild's news, what a monster dropped, or a login server's error, goes as a Message, for the client to say in its player's language (Translations, decision 0046). It holds its English, which is its key, with $0 to $9, and its args.
| Message | Field | What |
|---|---|---|
| Message | text, 1 | The English, with a placeholder for each arg, or a plural's English for one |
| Message | plural, 2, and count, 3 | A plural's English for any other count, and the count, which picks its form |
| Message | args, 4 | An Arg for each placeholder, in order |
| Arg | number, 1, name, 2, or phrase, 3 | A number, a name a player gave or a version, as it is, or a Message of its own, such as so much gold, or the rest of a list |
| Arg | item, 4, monster, 5, npc, 6, quest, 7, skill, 8, slot, 9, or faction, 11 | The content's, by its id, a kind of monster or of NPC by its kind's, or a slot of the bag, which the client names in its player's language |
| Arg | count, 10 | With an item or a monster, so many of it, "a Bone" or "3 Bones". None, its name alone |
| Chat | message, 4 | A line from nobody, in place of text, which is what players say |
| Chat | npc, 5 | An NPC who speaks, in place of name, by its kind, as a storyteller does |
| LoginReply | why, 7 | Why not, as a Message, beside error, 3, its English, which a client from before 0.96.0 reads |
A message is about the size of the line it replaces. It has its English, with placeholders where the names were, and a few bytes for each arg, and goes over TCP, as the lines did.
The client runs on the player's machine, so anything it sends can be made up. nanommo_cheat is the server's red-team client, a player without a window, who breaks the client's rules one way at a time and prints what it gets for it (tools/cheat/main.cc). It runs beside the servers, with an account of its own, as nanommo_cheat <name> <password> <cheat>. For a trade, a duel or a war, it names a partner too, as nanommo_cheat <name> <password> <cheat> <partner>, and the partner runs take for a trade, for a duel says yes in their client, or runs duel, and for a war runs feed.
| Cheat | What it sends | What it gets |
|---|---|---|
| speed | Three running inputs a tick, where the client sends one | 3.1 m/s, as running does, because the server applies one input a tick (ServerTest.MoreInputsThanTicksDontRunFaster) |
| teleport | A direction of 1000, then one that isn't a number | 3.1 m/s, as a direction is scaled back to a length of one, and one that isn't a number is ignored, so the last keys go on |
| reach | A swing at a monster 20 metres away | No hit from there, as the server runs the player up to the monster first |
| rapid | Its first skill ten times a tick | No more hits than using it once it's ready, as the cooldowns are the server's |
| cancel | Its weapon into the bag as each blow lands, and back two ticks later | Nothing, from 0.72.0, as a skill keeps the timings of the weapon it started with, so a swing whose blow has landed plays out (World.PuttingTheWeaponAwayAfterItsBlowDoesntSwingSooner). Before, the next swing started as soon as the weapon was back, about twice as often |
| swap | Its sword offered from the bag, and once the partner's ready, a move of the sword to another slot, and its own ready | Nothing, from 0.75.0, as a place that's offered is held until the trade's over, so the move is refused, and the partner gets the sword (Trades.WhatsOfferedStaysWhereItIsUntilTheTradesOver). The trade gives nothing unless each place still has what was offered (Trades.AnItemMovedAfterItsOfferedIsntGivenTwice). Before, the partner got the copy made at the offer, and the sword stayed too, which made three swords from two |
| run | Its sword offered, and once the partner's ready, its own ready, and it leaves at once | Nothing, from 0.75.0, because a trade saves both bags, together, as it's done, and a player who leaves is saved as they go, so it comes back without the sword (ServerTest.ATradeAndALeaveLeaveOneSword). Before, it left before the tick that would have saved its bag, and came back to the bag Redis had from before the trade, which made three swords from two |
| twice | A hello to the world's second instance too, with a token of its own, each second for twenty seconds until it's let in, and its sword traded away on the first, which it leaves. On the second, which loaded its bag before the trade, a move of the sword into the bag | Nothing, from 0.79.0, because a character plays on one instance at a time, the one that has their lease, so the second turns it away, and the trade goes as it should (ServerTest.ACharacterPlaysOnOneInstanceAtATime). Should the first stall for longer than a lease, the second can take it, and the first can't save it any more, and drops it (ServerTest.AnInstanceDropsAPlayerWhoseLeaseItsLost). Before, each instance saved it as it had it, and the second saved its bag from before the trade over the trade, which made three swords from two |
| stale | In a duel, with each input, that its client drew everyone a second before its newest world, where a client says 100 milliseconds, to hit its opponent where they were a second ago | No more than 200 milliseconds back, from 0.82.0, which the server believes at most (Inputs.AClaimFurtherBackThanTheMostIsTheMost), so its blows land within reach of where its opponent was then, and not of where they were a second ago (ServerTest.ADuelBlowLandsWhereTheClientDrewThemUpToTheMost). It prints what it says, with how far off its opponent was then and is now, and each blow that lands. That's 100 milliseconds more than a client beside the server says, 0.31 metres on a runner |
| farm | In a war with the partner, a friend in the other guild, who runs feed and so stands still until it's killed, comes back as soon as it can and walks back. farm picks them and swings each tick | 2 kills of 17 scored in six and a half minutes, from 0.92.0, as a victim scores for a war once every five minutes at most (Wars.AVictimScoresOnceEveryFiveMinutes). The kills still happen. Before, each one scored, which made 15 to 0 in six minutes, 2.6 a minute, for as long as the war lasts |
walk, fight, swing, take, duel and feed keep the rules, to compare with. Why the server checks everything is decision 25, how a trade is checked and saved, decision 28, why a character plays on one instance at a time, decision 31, and why a victim scores once every five minutes, decision 42.