The Source engine is a 3D game engine developed by Valve Corporation. Its unique features include a large degree of modularity and flexibility, an artist-driven, shader-based renderer, accurate lip sync and facial expression technology, and a powerful, efficient and completely network-enabled physics system.
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.
Posted by bob_gneu on Nov 28th, 2008
Intermediate Server Side Coding.
Warning! 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\\cfg\autoexec.cfg
Before you start working on the VGUI team menu, it is recommended that you look at the VGUI2 Overview. (http://www.valve-erc.com/srcsdk/VGUI2/vgui2.html). 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.
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:
Since you want to enable the team menu, '''uncomment'''
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.
'''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.
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
and below it add
Then add the implementation in '''game_controls/teammenu.cpp''':
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.
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 /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 /resource/ui/TeamMenu.res:
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:
but making a team menu command is no big deal:
With this code, if you enter the command "chooseteam" in the console it will show the menu.
If you do not already have a _.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 _english.txt into your mod's resource folder.
If you already have a _english.txt file, you can copy just the team parts from the example below and paste them into your existing file.
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 '''_german''', '''_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 _.html and be located in /resource/maphtml/
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''':
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 hl2mpgamerules.h
Now open '''teamplay_gamerules.cpp''', and in the function '''bool CTeamplayRules::ClientCommand( CBaseEntity *pEdict, const CCommand &args )''' ''below'' the lines
add these lines:
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
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
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;
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:
Now find function '''void CHL2MP_Player::Spawn(void)''' and add this to the end of that function:
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 _english.txt.
To add a [[bind|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"