Varlock GameRules Pack Tutorial 1 of 4

 

GRPack (GameRules Pack) Tutorial 1 of 4: Adding Two-Team Deathmatch to Your Mod

This is the first tutorial in a four-part series which will help you implement Two-Team Deathmatch, Assault, CTF, and Domination gamemodes into your Half-Life mod. These tutorials were designed as a quick and easy way to add the four gamemodes listed above to a fresh SDK, so you don't have to keep re-inventing the wheel every time you want to use one of these gamemodes. As a result the source code is included; first, because there is so much of it a tutorial would be difficult to follow, and second because it was originally meant to be used as a bare bones addition to the Half-Life SDK. Once you've added the code listed here, you will likely want to modify some (if not all) of the code to fit your mod's needs.
These tutorials contain several sections of code from other tutorials including,

- 2's HUD Graphics for Half-Life Tutorial
- BigGuy's Team Spawn Points Tutorial
- Prefect's VGUI Activation Tutorial
>>Source<<

This Two-Team Deathmatch code is serverside ONLY. The source code for this tutorial is contained in the grtdm.zip file which you should already have. To install, simply unzip the source code on top of a fresh install of the Half-Life SDK 2.2 in the /Source/dlls folder. Make sure you add teamdm.h and teamdm.cpp to the project before you compile or they won't be included in the build. If you want to add these gamerules after you've already modified the Half-Life source code, you will need to make use of some sort of diff program (such as WinDiff included with Microsoft Visual C++). Only a few lines of code have been changed in the existing files; the major changes are all in new files such as teamdm.cpp.

Okay, to start off, let's take a look at the new files. Open teamdm.h. You should see a new gamerules definition (CRulesTeamDM) derived from CHalfLifeMultiplay. There are a lot of similarities between this class and CHalfLifeTeamplay since the CRulesTeamDM class is a heavily modified version of CHalfLifeTeamplay. The most noticeable difference between the two is that all the model-specific code in Valve's CHalfLifeTeamplay is gone; after all, we want two specific teams (such as "Red" and "Blue" or "Human" and "Alien") which are not based on the player's model.
You can change the team's name with these two lines here,

#define TEAM1 "Red"
#define TEAM2 "Blue"
Just change the text inside the quotes to whatever you want your team's name to be. This will affect what appears on the team-select VGUI and on the scoreboard. Later on in this tutorial you'll have to modify more code to change the team's names but for now scroll down to this line (16),

virtual const char *GetGameDescription( void ) { return "Team DM"; }
You can also change this text to affect what will be displayed in the server browser when you are searching for a game online.

Now open teamdm.cpp. Several functions in this file have been recycled from CHalfLifeTeamplay, although most are heavily modified. Find the ClientCommand function on line 68. The "changeteam" command has been added; it will be sent when the player wishes to change their team. It sends back a command to open the TFC Team Select VGUI menu ('2' is the index for this menu - look in tf_defs.h for more index values). If your mod will implement spectator mode, you may want to place the player in observer mode now. Just below the "changeteam" command is the "jointeam" command. At the moment, it extracts the new team number and respawns the player.

Go to line 287 and the PlayerSpawn function. This is where the player's team is changed. The TFC Team Select VGUI will send back a '1' for Team 1, a '2' for Team 2... until it reaches '5' which means Auto-Team. These are the same numbers the VGUI shows to the player. This function checks pPlayer->m_iNextTeam to see if the player wants to change their team. If it's a '1', we change the player's team to Team 1. If it's a '2', we change the player's team to Team 2. Finally, if it's a '5' we change the player's team to the team with fewest players. The rest of this file should be fairly easy to understand and you probably won't modify it unless you're making huge changes.

Now that you've looked over the new gamerules, take a look at the new entities. Team DM doesn't require much: only two new spawn points. Here's the new entities you'll need to add to your .fgd file,

@PointClass base(PlayerClass) = info_player_team1 : "Team 1 Start" []
@PointClass base(PlayerClass) = info_player_team2 : "Team 2 Start" []
You may want to change the names of the two entities listed here and their descriptions to match the team names you chose. Once you've done that, open up subs.cpp and look for line 70. You'll need to change the entity's name here as well. Is that it? No, there's still one more section you'll need to change if you changed the entity's name. Open player.cpp and go to line 2652. You should see this function,

edict_t *EntSelectTeamSpawnPoint( CBaseEntity *pPlayer )
This function chooses a spawn point for the player when they respawn. The old version (EntSelectSpawnPoint) just searched for an info_player_start, but you want it to select a spawn point based on the player's team. If you've changed the spawn point entity's name, search through this function for all occurances of "info_player_team1" and "info_player_team2" and replace them with your own version. That's it for the entities! Now all you need to do is make a map with your new .fgd file and it'll be ready to go.

Now take a look at gamerules.cpp on line 315,

CGameRules *InstallGameRules( void )
This function decides what gamerules to install based on cvars, map names, etc... It currently checks the map's name for either "tdm_", "as_", "ctf_", or "dom_" and applies the correct set of gamerules. If you'd like to change this to another method, here is the place to do it. The "as_", "ctf_", and "dom_" checks are commented out because we don't actually have the code for those complete yet! In the next tutorials we'll come back here and uncomment the right lines.

Finally, you'll need to create a 'titles.txt' file in your mod's folder. This will contain a few messages to be displayed on the TFC Team Select VGUI and the Scoreboard. Paste this text into it,
Player
{
	player
}

Title_SelectYourTeam
{
	SELECT YOUR TEAM
}

Team_AutoAssign
{
	AUTO ASSIGN
}

TEAMS
{
	TEAMS
}

PLAYERS
{
	PLAYERS
}

CLASS
{
	CLASS
}

SCORE
{
	SCORE
}

DEATHS
{
	DEATHS
}

LATENCY
{
	LATENCY
}

Menu_OK
{
	OK
}

Menu_Cancel
{
	CANCEL
}
The CRulesTeamDM class is very important in these tutorials, because all the other gamerules are derived from it. Assault, CTF, and Domination are all two-team games with slight rule modifications, so it's easy to derive future gamerules from CRulesTeamDM and modify only the code we need to. So, you must have the CRulesTeamDM code if you want to add any of the other gamerules in this pack.

Thanks for checking out the first GRPack tutorial. If the next three tutorials are complete and you want to add another set of gamerules now, just open up the appropriate tutorial (for Assault, CTF, or Domination) and follow the instructions.