The goal is to minimize the chance for major conflicting game states (such as an explosion only happening on some clients) and to make speed and money hacks a little harder to do. It has to be done while trying to keep the game responsive and realistic (when my bullet hits the bad guy, I expect them to take damage). I also want to have the game support running as a dedicated server, which the current version does not support.
(Note: I develop in Unity, so what I write from hereon will be biased toward Unity developers)
The journey began with reading https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking and using it as a checklist for my implementation. So far, I've achieved Entity Interpolation and Input Prediction; I haven't done anything for Lag Compensation yet though it did give me some ideas. I'm also following these basic guidelines (not set in stone as I'm still learning, by the way):
- You can't just blindly attach network components to everything in the game and call it a day. Every object has unique needs; some of them may not even need network components.
- The server will almost ways be the one to instantiate network objects. The only exception to this are player objects that only require a visual presence, such as the player's stick figure arm rotating to show their aim.
- The game should always immediately respond to keyboard, mouse, and joystick inputs.
- Don't stream player inputs. Just send reliable messages when players press or depress buttons.
- Servers and clients will both use collision detection for player-enemy contacts; but only the server will decide whether the contact results in hitpoint loss or visible damage.
- Instantiations and buffered RPC's always get invoked first when a player joins. Minimize buffered RPC's and try to send them in the beginning of the object's lifetime if you have to; and make sure they get called in a predictable order.
- Never trust the client.
After a bit of work, you can see the results here. This movie was cobbled from videos taken on two different computers; one hard-wired to my FIOS connection and the other tethered from my smartphone. The video editing is imperfect, but I did my best to try to demonstrate how things look between the client and the server in a game with higher-than-desired latency.
The swinging stick figure arm follows the mouse cursor position of the owning player. It is a standalone object with its own network view where OnSerializeNetworkView will read the owning player's current rotation, and just assign it to the arm. No interpolation, no other magic involved. It is the only object in the scene; and at present the entire game, that clients can instantiate.
The bullets that fire from the pistol are not network objects. When the server player fires, an RPC is sent to all the clients which includes the weapon ID and arm rotation; and then a local (non-networked) projectile is instantiated in the server's scene. When a client gets the RPC, they update the server player's arm rotation and instantiate a projectile in their client scene. Both the server and client's projectiles follow the rules of physics; but only the server decides if anything loses hitpoints in the process. The bullets are always launched from a position relative to where the player is standing at the time.
When a client fires, an RPC is sent to the server with the weapon ID and arm rotation. After that, a local projectile is instantiated in the client's scene. When the server gets the RPC, it verifies that the client has the weapon and enough ammo to fire it. When it does, it RPC's a message to every other client that this one fired, and then instantiates its own projectile.
When a client moves back and forth, the only RPC's exchanged between the server and client are the new client's velocity caused by the client pressing or depressing a button. Input Prediction makes sure the client's movements are immediate while being reasonable with the server's expectations; if the client strays too far they will get "rubberbanded" backwards as you may have seen in other games. Other clients simply use Entity interpolation to simulate the movements of the client and server players based on the data that the server gives them in OnSerializeNetworkView.
This is my initial solution, and my comfort level is currently "uncertain." I need to see it in action before I can pass judgement. I suspect I will see reports of players shooting things but not killing them. I feel like I can do more about the projectiles, though I haven't figured out what yet. I also wonder if streaming unreliable player inputs are faster than using reliable messages; and if so, is it worth the additional traffic? After all, players are constantly moving and changing direction anyway, so maybe it won't make for that much more traffic?
The next step
Up to now, I've been entirely focused on engineering. I really need to map out the game design. This is the part that should be a lot of fun; but I also want to be careful about it. At one extreme, I could have a simple side scroller ala Sunset Riders with little enemy and weapon variety. At another extreme, I could have a very diverse ecology of enemies, a big weapon and item selection, leveling, skill trees, and plug-ins. Both can be fun in their own way; and though I favor the simpler scroller, there's something to be said about replayability in the presence of a leveling system and unlockable items.
I also want to have randomly generated content in each level. For example, level 1 is a simple left-to-right walking level, but I want there to be different buildings every time. Level 2 is probably going to be a jump-the-platforms level like the waterfall level in Contra, and I want the platforms to be randomly placed.