Don’t use Lockstep in RTS games

Lockstep has lost! The Client-Server network model has won and has become the standard for pretty much all games. RTS games were the last holdouts, yet there it also falling. Lockstep is simply used less and less. Lets figure out why, but first what is Lockstep?

So in games we have multiple players playing the game on a client. How do you make sure they are playing the same game? With Lockstep the clients all run the exact same code, with exact same parameters and inputs. If one player orders an army forward that command is issued to every other client and they issue the command to move that army forward. Even the random numbers must be agreed upon and is usually done in form of passing the first seed and executing everything in same order. This is very efficient for networking because only the player inputs must be sent. Things such as orders and build commands take up very little space in the network packet.

Lockstep is an extremely efficient network model, but is hard to code for with desyncs and compiler/multi platform issues. Lockstep was used in the past because network bandwidth was very low. It worked very well for that. Conceptually its is very easy to add to an existing game. Take DOOM for example the game was already written and everything worked. To add networking all John Carmack had to do was to replay commands on every computer. This sounded very easy in theory, and it worked for DOOM, But as soon as network latency or different architectures come into play everything breaks down. In practice its hard to code for. The Programmer must make sure that all actions happen in exact order on every client. What if someone did not receive the command in time? Well, the player who issued the command has to wait for all of the other clients to agree. What happens if someone drops and reconnects? Well most likely they have to re-simulate the entire game from the beginning, which might take a lot of time. See this defining paper on Lockstep in RTS games: 1500 Archers on a 288 Network.

With Lockstep one must make sure that the compiler they use does not optimize the float operation away, because CPUs architectures have slight differences on how the float point unit behaves. This is even worse if you are going to synchronize between say PC and iPad. Different architectures create a nightmare for lockstep. If you are coding with HTML5 — Javascript JIT can optimize your float points paths very differently introducing small errors into the calculations that could lead to huge changes later on. Lockstep programming is like balancing a heavy weight on a knife edge.

If that was not enough, Lockstep has peer to peer connection problems as well. If you are using TCP there is almost nothing you can do to connect two random computer over the internet because of firewalls, NATs, proxies, VPNs … etc. With UDP you can do what is called hole punching. Thankfully the browser has a new system called webRTC that works over UDP and has hole punching built into the protocol through a STUN server that is required. Yay! At least this part can be done with HTML5.

So what is Client-Server? Well its very simple you connect to the server and it sends you updates. Done. It is how the web works. It is how email works. It is pretty much how everything except BitTorrent and BitCoin work. The server and the client can run totally different code, even written in different languages, different platforms. The server has full authority over the game. There is no knife edge balancing going on. The problem is that it requires more updates to be sent. Because every single unit’s move, bullet fired, health bar change needs to be synced. But thankfully bandwidth for people is increasing, bandwidth is cheaper than programmer time.

The reason why the RTS genre held onto Lockstep for so long was bandwidth. In FPS games moving 32 people around is very easy. In RTS games moving 1000 units around is very bandwidth heavy, but even so bandwidth got better and cheaper phasing out the need for Lockstep.

Server-Client is also more hacker resistant. With Lockstep you can do maphacks — where you basically disable fog of war — because fog of war is client side (client must know about everything to sync up) and also network hacks where the other client knows your IP address and can try to send a bunch of network traffic your way like a min DDOS so that you get extreme lag disconnect or lose. In the Client-Server model you can’t do map hacks (server does not need send you updates on stuff in the fog of war) and you don’t know the players IP address (only the server does). I guess you can DDOS the server, but then everyone gets lag and servers can deal with it better. The server can also be on the lookout for strange client activity.

http://photos.imgix.com/racking-mac-pros

Another disadvantage of servers is that they cost money. Running game servers can be quite expensive, while with Lockstep there are no servers — it’s all peer-to-peer. Thankfully cloud providers make it easier to buy, spin up or spin down servers. But if thats still too much for you can run a server locally on one of the clients … it looks almost like Lockstep then … except there is still one authoritative source, and that player pays for bandwidth.

  1. StarCraft 1 used a peer-to-peer Lockstep model, while StarCraft 2 is Client-Server ( Starcraft2 Netgames.pdf ).
  2. Supreme Commander 1 uses Lockstep ( Synchronous RTS Engines and a Tale of Desyncs ) while newer games like Planetary Annihilation use Client-Server (Client-Server Network Model ).
  3. Dota 1 was running on the Warcraft 3 engine which used Lockstep, but when Valve rewrote the Source engine it uses Client-Server ( Extending Node-dota2 ).
  4. In the FPS corner DOOM used Lockstep but Quake used Client-Server, Lockstep did not live long with FPS games at all.
  5. MMOs never used Lockstep because its not not feasible for that many players.

Ease of programming and the falling cost of computers and bandwidth just tip the favor into the Client-Server direction so much, you will see Lockstep used less and less.

See also:

  1. About game networking: http://gafferongames.com/networking-for-game-programmers/what-every-programmer-needs-to-know-about-game-networking/
  2. Implement Lockstep in unity: http://clintonbrennan.com/2013/12/lockstep-implementation-in-unity3d/