Multiplayer template

Top  Previous  Next

The 3rd version of the multiplayer template code is a complete rewrite (99.9% new code) compared to the previous two iterations. While the latter tried to keep it simple using mostly basic stuff such as sending skills, the limitations resulting from that approach became more apparent the further the development went.


Additionally, synchronized entity creation took and takes a while in multiplayer systems, and even failed for seemingly inexplicable in a few rare cases. This is not only bad for the player creation itself, but for projectiles too, which is why 3 different types of projectiles were introduced back then (hitscan/ instantaneous c_trace, semi synchronized locally created projectiles for such things as plasma guns and fully synchronized globally created entities).


The new system takes a completely different approach. It does not synchronize entity creation and does not send entity updates, which means no usage of send_skill and the client_id feature. Instead, all networking stuff is done by sending (struct) data via send_data_to. Players, projectiles and items exist (on an abstract object level) only virtually, as arrays of corresponding structs, and entities are created only locally to represent the synchronized data.


When you take a look at the source code it may be a little intimidating at first, especially if you're not that familiar with structs. Nevertheless, you can learn more about them by reading my tutorials in Aum72 and 83, for example.


The new template version is much more robust, reliable and flexible in comparison with the old entity and send_skill approach. In particular the projectile system has been refined, it now only uses (from a coding perspective) one type of projectiles for all kinds of different weapons,

including machine guns, plasma weapons, rocket and grenade launchers and so on.


The code currently implements the following features:

- Dynamic client and bot creation via a player bitmask. This way there is no join or leave event dependency, and no "dead" players of clients who have left the game and where a packet may not have reached one ore more of the clients.

- Level changing and support for different game modes (deathmatch and team deathmatch).

- 32 players, which can be extended to support even more clients (requires a few changes in code).

- AI bots for DM and TDM.

- Chat system, including kill updates and admin commands (which allow to change the number of bots, the map and maybe even the game mode dynamically).

- All kinds of different projectiles via a function pointer approach.

- Easy weapon setup.

- Damage and health system (including splash damage and weapon inaccuracy), as well as respawn logic.

- A rewind system, similar to the one from the previous template version, which recalculates the game situation from when the client fired the projectile for the best possible hit detection.

- Item pickups for health packs, weapons, and (a potentially very high number of) moving platforms which don't need any traffic!

- A scoreboard (press and hold TAB).


Simply open main.c and run it to test new version of the template. And if you are curious about the source code, open multiplayer.c first (that is the main file of this template) and read its comments, then explore types.h and check out the structs!