Multiplayer Template |
Top Previous Next |
Last month we were discussing the key multiplayer games problems: a) They require too much bandwidth. b) They fail to work properly because of the poor latency.
I have created a multiplayer article series in AUM74... 79, but that code was using Gamestudio's built-in multiplayer architecture. It's a good foundation for LAN-based games, but I knew that if I wanted the games to run properly over the internet (and the internet doesn't always mean fiber optics connections) I will have use a different approach, running as much code as possible on the clients and sending as few bytes as possible to the server.
I started to browse the forum, trying to find out if somebody else has already coded a multiplayer project that addresses the two issues above, what problems has he run into, and so on. Imagine my surprise when I have discovered Superku's multiplayer script, which can be found in Gamestudio's Wiki. The code is brilliantly simple and does the job perfectly; in fact, it is so well written that I had to think twice before starting to modify it.
So what have we gotten this month? A fully functional multiplayer script which handles the client player movement on the client, and thus sends very few (dozens) of bytes per second over the network. Not only that, but the code also compensates the poor latencies each second. If the distance between the real player position and the computed position is too big, the player is teleported to the correct place; otherwise, if the difference isn't that big, its position is blended smoothly until it reaches the destination.
Our players can move, rotate and animate; believe it or not, these things are done without using more bandwidth than what Superku's original code is using. So how do we get to test the code in action?
Copy the \multiplayer1 folder inside your Gamestudio folder, start Wed, and then load the map1.wmp level. Press the Run button to start the associated (multiplayer.c) script; you will see an image that looks like this:
Press the "1" key to start a server; we need to do that before adding clients (players).
The picture above shows the server running, but without having any client connected to it. Use Wed to "Run" the script twice, choosing the second option, and thus adding 2 clients to the server.
The server will now display the unique IDs for each client / player (they are automatically assigned by the engine) and you will get to see them moving, rotating, animating, and so on.
As you can see, the client window displays player's unique ID, the latency value and the number of bytes per second. The current player (the one that can be controlled by us in the active client instance) will have a greenish color, while the other players will have a red color. You can move the players around and rotate them using the WSAD keys; as mentioned above, the possible movement errors caused by different frame rates, latency, etc will be compensated each second.
The code is very well commented. I will see you next time, when we will add more features to the template.
|