Networking Is Hard (Part 1)

I haven’t had much development time in the last (nearly two) month(s), but I have had time to nearly finish up the networking code for the game, so I thought I would describe some of the work that went into the design of the system.

Resources

I tried to find some references on how most shmups (shoot-em-ups) such as my current game handle their networking, but I didn’t really find anything.  There are a lot of resources on how RTS games do it (such as this great article about Age of Empires’ networking), and even more articles about how to write networking for an FPS game (the best of which, in my opinion, are the articles about the Source engine’s networking and the Unreal engine’s networking).  There’s also a great series of articles on the Gaffer on Games site.  And no list of references would be complete without the Gamedev.net Multiplayer and Networking forum.

One of the most seemingly-relevant things to my game that I read was that, in current system emulators (for instance, SNES emulators) that have network play, they work in lock step with each other…that is, each system sends the other system(s) its inputs, and when all inputs from all systems have arrived, they can tick the simulation forward.  I did an early experiment to see how well this would work with my game, and it turns out, not so well – the latency introduced with even a moderate ping (100ms) is rather prohibitive.  The player would press up on the gamepad and have to wait for the ship to start moving…and it would only get worse with higher latencies!  Obviously, this wouldn’t work.

Since there were many articles about the FPS  model of networking, I opted to use it as a starting point, as my game is also a fast-action game.  First off, I decided to make a (partial) list of the advantages and disadvantages of my game type vs. the FPS model:

Advantages of Procyon vs. an FPS

  • Only two players are supported, which greatly simplifies the bandwidth restrictions and the communication setup.
  • There’s no need to support joining/quitting in the middle of a game (again, simplifying game communications and state management)
  • Since it’s cooperative, I’m not really worried about players cheating, so I can allow clients to be authoritative about their actions moreso than the average FPS can.
  • Players can’t collide with each other, so there’s no need to worry about such things (in fact, a player basically can’t affect the other player at all – a player can only affect enemies)
  • Enemies are deterministic in their movements – none of my enemy designs have movement variation based on the player’s actions, so collision vs. enemies in a given frame is always accurate (unless the enemy was dead “in the past” due to the actions of the other player, which is discussed later).

Disadvantages

  • In an FPS, the view is more limited – it’s rare that a player sees all of the action at once due to the limited field of view.  However, in Procyon, everything that is happening in the game is completely visible on both screens, meaning the game generally has to be less lenient about discrepancies.
  • Most FPS games are “instant hit.”  For instance, when you fire a pistol in a FPS game, there’s no visible bullet that streaks across the screen, so it’s easier to fudge the results on the server.  In Procyon, however, all of the weapons are on-screen and visible, so fudging them in a minimally-noticeable way becomes very difficult.
  • In many FPS games (though not all), there are WAY less entities active at a time than there are in a shoot-em-up.
  • This falls into the realm of “not a lot of references for this type of game,” but many of the “cheats” employed by FPS game programmers are really well-known, but very domain-specific; with a shmup, I’d have to make up my own network fakery to hide latency.

Network Strategies

There are two main network strategies used in games (at least, in those that I researched): lockstep and client-server.

Lockstep keeps all systems involved perfectly in sync, though at the cost of making it more susceptible to latency, because the game doesn’t step until the other player’s inputs have traversed the network.  Generally, every so often (say, every 1/30th of a second), the current input is polled and then sent across the network.  Then, once both/all inputs have reached the local machine, it ticks the game.  This is the type of networking used by emulators (out of necessity – there’s no reliable way to do any sort of prediction with an emulator anyway) and some RTS games (such as Age of Empires, linked above).  RTS games can hide the latency a bit by causing all commands to execute with a bit of a delay.  One interesting property of lockstep networking is that nothing ever has to round-trip across the network.  That is, nothing that server B ever sends back relies on something that Server A sent previously.  Because of this, the game’s latency is effectively halved, as the meaningful measurement is one-way latency, not a full there-and-back ping.  Lockstep games are frequently done peer-to-peer: each system sends its inputs to each other system…there’s no central entity in charge of all communications.

Conversely, a client-server architecture (which is used by an overwhelming majority of FPS games) puts one system in charge and generally gets authority over the activities of all players.  In this case, the client sends its information to the server (inputs, positions, etc), and the server sends back any corrections that need to be made (locations/motion of the other players, shots fired, things hit/killed, etc).  The client can run ahead of the server based on a prediction model, where it gives its best guess as to where things are currently based on the last communication from the server.  This makes game lag much less apparent (At least, in the player’s own movements), as the player’s inputs can be processed immediately on the local machine.

In the next installment, I’ll talk through Procyon’s initial network design, which is a bit of a hybrid between the client-server and lockstep architectures…stay tuned!

2 thoughts on “Networking Is Hard (Part 1)”

Leave a Reply

Your email address will not be published. Required fields are marked *