Creating Teams

From Valve Developer Community
Jump to: navigation, search

This tutorial will lay out how to force teamplay in a Deathmatch Source SDK. If you want to set up teams for a from-scratch SDK, refer to this tutorial.

First we will create a team menu, similar to the one seen in Counter-Strike: Source, then we will force HL2MP to be teamplay only.

Warning.pngWarning:When using this method, maps that only have info_player_combine and info_player_rebel on them must be run with mp_teamplay 1, set on the server. This can be done by entering it into your Autoexec.cfg file for the game Steamapps\SourceMods\<modname>\cfg\autoexec.cfg

VGUI overview

Before you start working on the VGUI team menu, it is recommended that you look at the VGUI Documentation. You will learn that the base for all VGUI panels (not class-based, but where VGUI-objects are attached) is a viewport. This viewport is created in cl_dll/sdk/clientmode_sdk.cpp in the method ClientModeSDKNormal::InitViewport:

This is just for reference. You do not need to edit anything here.

void ClientModeSDKNormal::InitViewport()
{
  m_pViewport = new SDKViewport();
  m_pViewport->Start( gameuifuncs, gameeventmanager );
}

Team panel and menu

Let's take a look at the CBaseViewport, which handles all the different Panels used by the mod. It is defined in cl_dll/baseviewport.cpp.

This function creates all panels known to this viewport:

void CBaseViewport::CreateDefaultPanels( void )
{
  AddNewPanel( CreatePanelByName( PANEL_SCOREBOARD ) );
  AddNewPanel( CreatePanelByName( PANEL_INFO ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECGUI ) );
  AddNewPanel( CreatePanelByName( PANEL_SPECMENU ) );
  // AddNewPanel( CreatePanelByName( PANEL_TEAM ) );
  // AddNewPanel( CreatePanelByName( PANEL_CLASS ) );
  // AddNewPanel( CreatePanelByName( PANEL_BUY ) );
}

Since you want to enable the team menu, uncomment

AddNewPanel( CreatePanelByName( PANEL_TEAM ) );

The "real" creation is done in the function CBaseViewport::CreatePanelByName, which takes the panelname as string and creates a corresponding class. The panel names are referenced in game_shared/viewport_panel_names.h, e.g.

#define PANEL_TEAM "team" 

PANEL_TEAM creates the class CTeamMenu, which contains all the functionality of that panel and is implemented in game_controls/teammenu.cpp. So uncomment that, too (in cl_dll/baseviewport.cpp).

  else if ( Q_strcmp(PANEL_TEAM, szPanelName) == 0 )
  {
    newpanel = new CTeamMenu( this );
  }

The class CTeamMenu is nearly functional; the only thing missing is the function which handles the messages created by the panel. Open game_controls/teammenu.h to add the declaration of the new function OnCommand to the class. Find

// command callbacks 

and below it add

virtual void OnCommand( const char *command );

Then add the implementation in game_controls/teammenu.cpp:

void CTeamMenu::OnCommand( const char *command )
{
	if ( Q_stricmp( command, "vguicancel" ) )
	{
		engine->ClientCmd( const_cast<char *>( command ) );
	}
	Close();
	gViewPortInterface->ShowBackGround( false );
	BaseClass::OnCommand(command);
}

You don't see anything done here with the commands. They are passed to the baseclass where hopefully they will be employed in a useful manner, but that is beyond the scope of this tutorial.

Menu layout

The layout and contents of your panel can be defined solely by a resource file. If you don't need to do customized things (e.g. displaying gamemode-dependent buttons or map information), you won't require any more to complete your menu.

Most resources are missing in the Source SDK; the team menu resource is no exception. You need to create one yourself (or copy one from a cache file); the standard path would be <modname>/resource/ui/Teammenu.res. I think the format is self-explanatory, and you'll find an example below.

It defines one panel (team), one label (joinTeam) and five buttons (jointeam1, jointeam2, jointeam3, autojoin, CancelButton). The interesting part in the button-definition is "Command", because this defines, which command the client executes, if he presses that button.

You don't need to edit the file by hand. Valve included a nice tool with which you may change it in-game, as mentioned in VGUI Documentation. Just press [SHIFT]+[CTRL]+[ALT]+[B] in the game to open the VGUI Build-Mode editor:

Added: replaced example with modified Counter-Strike: Source team menu, which includes HTML map info panel. You can save this example into <modname>/resource/ui/TeamMenu.res:

"Resource/UI/TeamMenu.res"
{
	"team"
	{
		"ControlName"		"CTeamMenu"
		"fieldName"		"team"
		"xpos"			"0"
		"ypos"			"0"
		"wide"			"640"
		"tall"			"480"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"SysMenu"
	{
		"ControlName"		"Menu"
		"fieldName"		"SysMenu"
		"xpos"			"0"
		"ypos"			"0"
		"wide"			"64"
		"tall"			"24"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"0"
		"tabPosition"		"0"
	}
	"MapInfoHTML"
	{
		"ControlName"		"HTML"
		"fieldName"			"MapInfoHTML"
		"xpos"				"244"
		"ypos"				"116"
		"wide"				"316"
		"tall"				"286"
		"autoResize"		"0"
		"pinCorner"			"0"
		"visible"			"1"
		"enabled"			"1"
		"tabPosition"		"0"
	}
	"joinTeam"
	{
		"ControlName"		"Label"
		"fieldName"		"joinTeam"
		"xpos"		"76"
		"ypos"		"22"
		"wide"		"450"
		"tall"		"48"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"labelText"		"#TM_Join_Team"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"	"0"
		"font"		"MenuTitle"
	}
	"mapname"
	{
		"ControlName"		"Label"
		"fieldName"		"mapname"
		"xpos"			"244"
		"ypos"			"72"
		"wide"			"180"
		"tall"			"24"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"labelText"		""
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"	"1"
	}
	"jointeam2"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam2"
		"xpos"			"76"
		"ypos"			"116"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"3"
		"labelText"		"#TM_Join_Team_2"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 2"
	}
	"jointeam3"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam3"
		"xpos"			"76"
		"ypos"			"148"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"4"
		"labelText"		"#TM_Join_Team_3"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 3"
	}	
	"autojoin"
	{
		"ControlName"		"Button"
		"fieldName"		"autojoin"
		"xpos"			"76"
		"ypos"			"212"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"1"
		"labelText"		"#TM_Auto_Join"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 0"
		"Default"		"1"
	}
	"jointeam1"
	{
		"ControlName"		"Button"
		"fieldName"		"jointeam1"
		"xpos"			"76"
		"ypos"			"244"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"2"
		"labelText"		"#TM_Join_Team_1"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"command"		"jointeam 1"
	}
	"CancelButton"
	{
		"ControlName"		"Button"
		"fieldName"		"CancelButton"
		"xpos"			"76"
		"ypos"			"276"
		"wide"			"148"
		"tall"			"20"
		"autoResize"		"0"
		"pinCorner"		"2"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
		"labelText"		"#TM_Cancel"
		"textAlignment"		"west"
		"dulltext"		"0"
		"brighttext"		"0"
		"Command"		"vguicancel"
	}
}

Menu command

The only thing missing to show the menu is a command to bring it up on screen. cl_dll/baseviewport.cpp already implements a command that you can enter from the console to show any panel:

CON_COMMAND( showpanel, "Shows a viewport panel <name>" )

but making a team menu command is no big deal:

CON_COMMAND( chooseteam, "Opens a menu for teamchoose" )
{
 if ( !gViewPortInterface )
  return;
 gViewPortInterface->ShowPanel( "team", true );
}

With this code, if you enter the command "chooseteam" in the console it will show the menu.

Localization

If you do not already have a <modname>_<language>.txt in your mod's resource folder, you will need to create one.

The example below has been extracted from the HL2MP cache and modified to include team names from the team menu. Save it as <modname>_english.txt into your mod's resource folder.

If you already have a <modname>_english.txt file, you can copy just the team parts from the example below and paste them into your existing file.

"lang" 
{ 
"Language" "English" 
"Tokens" 
{ 
//spectate HUD labels
"Cstrike_Spec_Ter_Score"		"Team 1:"
"Cstrike_Spec_CT_Score"			"Team 2:"

//team menu labels  
"TM_Join_Team"     "Join a Team"
"TM_Join_Team_1"   "Spectate"
"TM_Join_Team_2"   "Team Combine"
"TM_Join_Team_3"   "Team Rebels"
"TM_Auto_Join"     "Auto-Select"
"TM_Cancel"        "Cancel"

"hl2_AmmoFull"			"FULL"

"HL2_357Handgun"	".357 MAGNUM"
"HL2_Pulse_Rifle"	"OVERWATCH STANDARD ISSUE\n(PULSE-RIFLE)"
"HL2_Bugbait"		"PHEROPOD\n(BUGBAIT)"
"HL2_Crossbow"		"CROSSBOW"
"HL2_Crowbar"		"CROWBAR"
"HL2_Grenade"		"GRENADE"
"HL2_GravityGun"	"ZERO-POINT ENERGY GUN\n(GRAVITY GUN)"
"HL2_Pistol"		"9MM PISTOL"
"HL2_RPG"		"RPG\n(ROCKET PROPELLED GRENADE)"
"HL2_Shotgun"		"SHOTGUN"
"HL2_SMG1"		"SMG\n(SUBMACHINE GUN)"
"HL2_SLAM"		"S.L.A.M\n(Selectable Lightweight Attack Munition)"
"HL2_StunBaton"		"STUNSTICK"

"HL2_357Handgun_Menu"	".357 MAGNUM"
"HL2_Pulse_Rifle_Menu"	"PULSE RIFLE"
"HL2_Crossbow_Menu"	"CROSSBOW"
"HL2_Crowbar_Menu"	"CROWBAR"
"HL2_Grenade_Menu"	"GRENADE"
"HL2_GravityGun_Menu"	"GRAVITY GUN"
"HL2_Pistol_Menu"	"9MM PISTOL"
"HL2_RPG_Menu"		"RPG"
"HL2_Shotgun_Menu"	"SHOTGUN"
"HL2_SMG1_Menu"		"SMG"
"HL2_SLAM_Menu"		"S.L.A.M"
"HL2_StunBaton_Menu"	"STUNSTICK"

"ScoreBoard_Player"		"%s1    -   %s2 player"
"ScoreBoard_Players"		"%s1    -   %s2 players"
"ScoreBoard_Deathmatch"		"Deathmatch"
"ScoreBoard_TeamDeathmatch"	"Team Deathmatch"
"Playerid_sameteam"		"Friend: %s1 Health: %s2"
"Playerid_diffteam"		"Enemy: %s1"
"Playerid_noteam"		"%s1 Health:%s2"
"Team"				"Team %s1"

"Game_connected"			"%s1 connected"
"Game_disconnected"			"%s1 has left the game"
"Cannot_Be_Spectator"			"This server does not allow spectating"

} 
}

If you take a look back at TeamMenu.res, you'll find labeltexts beginning with a '#'. These labels are replaced by their counterparts in the translation file, only without the '#'. For example #TM_Join_Team will be replaced with "Join a Team" if your Steam language is set to English. For other languages you need to create additional files, like <modname>_german, <modname>_french.txt, etc.

Using the example TeamMenu.res provided above, you can also display an HTML mapinfo file next to the team menu. Just create a simple HTML file and fill it with map information. It should be named <mapname>_<language>.html and be located in <modname>/resource/maphtml/

Forcing teamplay

Warning.pngWarning: In source SDK 2013 there is already a function called HandleCommand_JoinTeam in hl2mp_player.cpp that invalidates most of these steps. Use with caution.

We will now force HL2MP to be teamplay only. It will use the existing teams (Combine and Rebels) but you will learn how to change the team names if you wish.

First ensure that you have 3 teams defined in shareddefs.h:

#define TEAM_INVALID			-1
#define TEAM_UNASSIGNED			0
#define TEAM_SPECTATOR			1

TEAM_COMBINE and TEAM_REBELS should NOT be defined here unless you are modding a from-scratch Source SDK. Here we are modding a deathmatch SDK, so the two teams have already been defined in an enum in hl2mp_gamerules.h

Now open teamplay_gamerules.cpp, and in the function bool CTeamplayRules::ClientCommand( CBaseEntity *pEdict, const CCommand &args ) below the lines

	if( BaseClass::ClientCommand( pEdict, args ) )
		return true;

add these lines:

	if (pEdict->IsPlayer() && static_cast<CBasePlayer *>(pEdict)->ClientCommand(args))
	{
		return true;
	}

to make the link between the call of CTeamMenu::OnCommand and CBasePlayer::ClientCommand

In player.cpp, in the function bool CBasePlayer::ClientCommand(const char *cmd) we will add

else if ( stricmp( cmd, "jointeam" ) == 0 ) //start jointeam
{
	if ( args.ArgC() < 2 )
		return true;

	int team = atoi( args.Arg(1) );

	//don't do anything if you join your own team
	if ( team == GetTeamNumber() )
		return true;

	//auto assign if you join team 0
	if ( team == 0 )
	{
		if ( g_Teams[TEAM_COMBINE]->GetNumPlayers() > g_Teams[TEAM_REBELS]->GetNumPlayers() )
			team = TEAM_REBELS;
		else
			team = TEAM_COMBINE;
	}

	if ( !IsDead() )
	{
		if ( GetTeamNumber() != TEAM_UNASSIGNED )
		{
			CommitSuicide();
			IncrementFragCount(1); //adds 1 frag to balance out the 1 subtracted for killing yourself
		}
	}

	ChangeTeam( team );

	return true;
} //end jointeam

Notice the test to check if the player belongs to TEAM_UNASSIGNED, this removes the dead body falling on the floor when we connect to a team.

Also to make it compile correctly add this include in player.cpp to the top of the file

#include "hl2mp_gamerules.h"

If you continue to have compiling errors, add this after #include "tier0/memdbgon.h" in player.cpp:

enum
{
	TEAM_COMBINE = 2,
	TEAM_REBELS,
};

And finally, to force teamplay to be on all the time (even if mp_teamplay is set to 0), go into hl2mp_gamerules.cpp and search for the line

m_bTeamPlayEnabled = teamplay.GetBool();

and replace it with

m_bTeamPlayEnabled = true;

Final touches

We're almost done, but there are first a few more things we need to do. First, we need to force players to join Spectator as soon as they connect to the server.

In hl2mp_player.cpp, find function void CHL2MP_Player::PickDefaultSpawnTeam( void ) and comment the entire function. Add this in its place:

void CHL2MP_Player::PickDefaultSpawnTeam( void )
{
    if ( GetTeamNumber() == 0 )
    {
        if ( HL2MPRules()->IsTeamplay() == false )
        {
            if ( GetModelPtr() == NULL )
            {
                ChangeTeam( TEAM_UNASSIGNED );
            }
        }
        else
        {
            ChangeTeam( TEAM_SPECTATOR );
        }
    }
}

Now find function void CHL2MP_Player::Spawn(void) and add this to the end of that function:

if ( GetTeamNumber() != TEAM_SPECTATOR )
{
	StopObserverMode();
}
else
{
	//if we are a spectator then go into roaming mode
	StartObserverMode( OBS_MODE_ROAMING );
}

And lastly we will make the team menu pop up after the MOTD is closed, so the player knows to join a team.

In hl2mp_client.cpp find the line

pPlayer->ShowViewPortPanel( PANEL_INFO, true, data );

and add this ABOVE it:

pPlayer->ShowViewPortPanel( PANEL_TEAM, true, data );

(This is really just a temporary solution; what it does is bring up the team menu, then brings up the motd over it so when you close the motd the team menu appears to pop up)

One final thing: If you want to change the names of your teams (Combine and Rebels), you do not need to change the predefined names. If you're modding the Deathmatch SDK and decided to change TEAM_COMBINE and TEAM_REBELS in hl2mp_gamerules.h to something else, you'd have to find all instances of those everywhere in the SDK and change them to match, which is a big hassle and likely to cause problems.

You can change the visual name appearance of your teams very easily in the file hl2mp_gamerules.cpp by searching for the line

char *sTeamNames[] =

Below it you will notice the names for all the defined teams, including Combine and Rebels. Just change those names and your teams will appear with the names on the scoreboard and HUD. To change their names in the Team Menu that we created earlier, you can edit <modname>_english.txt.

To add a bindable key to the key configuration list, just open kb_act.lst in your mod's script directory, and add this to the bottom:

"chooseteam"				"Choose Team"

Credits

This Tutorial was originally from gneu.org and moved here to combine all the tutorials into one location.

That should be everything you need to do to turn your Deathmatch mod into Teamplay Only. I may have missed something, so feel free to edit it in or discuss it in the talk page. --Wildfire 23:19, 10 December 2006 (EST)