Do not use Lockstep in strategies in RTS
Hello, Habr! I present to you the translation of the article Don't use Lockstep in RTS games .
Lockstep lost! The client-server model won and became the standard for most games. Real-time strategies were the latest, but Lockstep is being used less and less. Let's find out why, but first, what is Lockstep?

In games, there are several players playing the game on the client. But how do you make sure that they play the same game? Using Lockstep, all clients run the same code, with the same parameters and input data. For example, if one of the players orders the army to go to the specified point, this team is transferred to all clients and for all this army moves to the specified point. Even a generatorrandompseudo-random numbers should be the same for everyone, usually this is done by using the same seed at the beginning of the game and doing everything in the same order. This is very effective from a network point of view because only commands are transmitted. Things like an order or action take up very little space in the network packet.
Lockstep is a very effective model of network interaction, but it is difficult to implement due to problems of desync or differences between compilers / different platforms. Lockstep has been used in the past because network bandwidth was very low. This worked great. Conceptually, it is very easy to add to an existing game. For example, DOOM, which was already written and everything worked. To add multiplayer, John Carmack had to run commands on each computer. This is very easy in theory, before delays or architectural differences spoil everything. In practice, this is difficult to do. The programmer must make sure that all commands are executed in the same order on each client. What if someone didn't get the team on time? The client must wait for confirmation of receipt of the command from all clients. What will happen, if someone reconnected? The first thing that comes to mind is to simulate the whole game from the very beginning, which can take quite a lot of time.

Using Lockstep, the programmer must make sure that the compiler does not optimize floating-point operations, because different processor architectures may have slight differences as a result of the same calculation. This is especially noticeable if you are trying to sync your PC and iPad. Architectural differences are a nightmare for Lockstep. If you work with HTML5-Javascript, JIT can optimize calculations in different ways, I make small errors that can lead to big changes. Lockstep programming - how to hold a lot of weight on the tip of a knife.
If this is not enough for you, Lockstep uses p2p, inheriting all the problems of the second. If you use TCP, you cannot connect any two computers over the Internet because of firewalls, NATs, proxies, VPNs, etc. With UDP there is a chance to do hole punching. Fortunately, browsers support webRTC , which works via UDP and has an API for hole punching through a STUN server. At least this part could be done using HTML5.
And what is a Client-Server model? It is simple when the client joins the server and the server sends updates to the client. Done! Server and client can use different code, in different languages and even platforms. The server has full control over the game. There is no knife on the tip of which you need to hold something. The main problem is that the server should send more updates. This is because the movement of each unit, the shot of a bullet, the change in health must be synchronized. Fortunately, the network channel of users is expanding and cheaper than the time of the programmer.
The reason why the RTS used Lockstep was channel width. In FPS, the movement of 32 people does not require a lot of traffic. In RTS, traffic 1000 requires more traffic, but the channel width allows this.
Client-Server model is more resistant to cheaters. Using Lockstep, each client knows everything about the whole game. For example, the fog of war is the client part, the client in reality knows what is behind the fog, but does not show it. Also, everyone knows the IP and open of each of the clients and can spam enemies - micro DDOS. In the Client-Server model, the server simply does not send updates behind the fog of war, and only the server knows the IP of other clients. Of course, a client can make a DDOS attack on a server, but servers can more easily counter attacks and can block clients with suspicious activity.

The disadvantage of servers is that they cost money, while Lockstep does not need servers, all is p2p. It’s good that there are providers from which you can rent servers. But if it’s still too expensive, you can run one server on one of the clients ... it looks like Lockstep ... except that there is one authoritative client, and that it is more loaded.
Examples:
- StarCraft 1 uses Lockstep, while StarCraft 2 uses the Client-Server model.
- Supreme Commander 1 uses Lockstep when newer games, such as Planetary Annihilation, use the Client-Server model.
- DotA ran on the Warcraft 3 engine that used Lockstep, but Valve used the Client-Server model for Dota 2.
- DOOM used Lockstep but Quake uses the Client-Server model; Lockstep was not popular at all in FPS.
- MMOs have never used Lockstep because it is not applicable to so many players.
The development of programming, the cheapening of computers, and the modern speed of the Internet make Client-Server easier to apply than Lockstep. Lockstep will be used less and less.