QuakeZone
MOD Coding Tutorials
Quake II Coding Tutorials!

Introduction
Pre-requisites
Code
Notes
Exercises
Quake II Coding Tutorials

15 Teamplay Enhancements

Introduction Introduction

This tutorial describes enhancements made to QDevel's original teamplay tutorial, to add team names and team switching (via console).

The players can then use "switch 1|2" on the console to switch teams, provided the host has enabled team switching with the sv_allowswitch variable.

Pre-requisites Pre-requisites

  • teamplay add-on tutorial from the QDevels tutorials site
Code Code


g_local.h

Add the following cvar references as indicated:

// PJT - team play
extern cvar_t *sv_teamname1;			// team names
extern cvar_t *sv_teamname2;
extern cvar_t *sv_allowswitch;			// allow team switching
// PJT  <-- end of insert

#define world	(&g_edicts[0])	
		
Then add the following to the end of the file:
// PJT - team play
typedef struct
{
	char	*name;				// team name
	int	score;				// team score
} team_data;
extern team_data team_table[];
// PJT
		

p_client.c

Add to the top of the file, just after the include statements:
cvar_t *sv_teamname1;	// PJT - team names
cvar_t *sv_teamname2;
cvar_t *sv_allowswitch; // PJT - teams - allow switching
		
Add the following to the top of the code as indicated:
void SP_misc_teleporter_dest (edict_t *ent);	//  <-- insert after this line

// PJT - team play
team_data	team_table[] =
{
	{"",     	0},
	{"RedNasties",  0},
	{"BlueMeanies", 0}
}
;	
		
Then, in procedure AssignTeam (new procedure added by the QDevels tutorial) amend the following line as indicated:
                   // if (current > 4)		
                   if (current > maxteams)	// PJT - "maxteams" was "4"
		
At the end of the same procedure add the following lines:
		// PJT - team names
		gi.bprintf (PRINT_MEDIUM, "%s has joined team %s.\n",
					self->client->pers.netname,
					team_table[self->client->resp.team].name);
		

g_save.c

At the end of procedure InitGame add the following lines:
	// PJT - team play
	sv_teamname1 = gi.cvar("sv_teamname1","Red_Team", CVAR_SERVERINFO);	// team names
	sv_teamname2 = gi.cvar("sv_teamname2","Blue_Team",  CVAR_SERVERINFO);	// team names
	team_table[1].name = sv_teamname1->string;
	team_table[2].name = sv_teamname2->string;
	sv_allowswitch = gi.cvar("sv_allowswitch","0",  CVAR_SERVERINFO);	// allow team switching
	// PJT
		

g_cmds.c

Add the following lines into procedure ClientCommand as indicated:
// PJT - team play - switch teams
   	else if (Q_stricmp (cmd, "switch") == 0)
		Cmd_Switch_f (ent);
// PJT <-- end of insert

 	else if (Q_stricmp (cmd, "gameversion") == 0)
 		gi.cprintf (ent, PRINT_HIGH, "%s : %s\n", GAMEVERSION, __DATE__);
		
Then add the following procedure at the end of the file:
// PJT - team play

/* allows players to switch between teams */

void Cmd_Switch_f (edict_t *ent)
{
	int	i, maxteams;

	maxteams = 2;

	// check switch allowed
	if (!(sv_allowswitch->value))
	{
		gi.cprintf (ent, PRINT_HIGH, "Team switching is not enabled.\n");
		return;
	}

	// check valid team number entered
	i = atoi (gi.argv(1));
	if ((i < 1) || (i > maxteams))
	{
		gi.cprintf (ent, PRINT_HIGH, "Invalid team number entered.\n");
		return;
	}

	// check not same team
	if (i == ent->client->resp.team)
	{
		gi.cprintf (ent, PRINT_HIGH, "You are already on team %i.\n", i);
		return;
	}

	// switch to that team (and set score to zero - cannot carry frags to new team)
	ent->client->resp.score = 0;
	ent->client->resp.team  = i;
	gi.bprintf (PRINT_MEDIUM, "%s has joined team %s.\n",
				ent->client->pers.netname,
				team_table[ent->client->resp.team].name);
	AssignSkin(ent, Info_ValueForKey (ent->client->pers.userinfo, "skin"));

}
// PJT
		

cmds.cfg

Add the following lines to your game config file:
set sv_teamname1    Red_Nasties
set sv_teamname2    Blue_Meanies
set sv_allowswitch  1
		
Notes Notes to code


g_local.h
  • the 3 server variables (cvars) hold the 2 team names and the host flag to allow team switching
  • the structure defines the team table to hold team scores and team names

p_client.c
  • the team table is populated with initial values
  • a bug in the original tutorial is fixed, there are only 2 teams when assigning teams
  • a message is displayed when the team has been assigned

g_save.c
  • the team name and allow switching variables are stored at the start of every new game

g_cmds.c
  • a new player command, "switch", is added in ClientCommand that calls procedure Cmd_Switch_f
  • Cmd_Switch_f processes the switch command:
    • the command is rejected if the host has disabled team switching, or an invalid team number is entered
    • otherwise, the player's score is reset (cannot carry frags to new team, sorry!), he is moved to the new team, amessage is displayed and he is assigned the new team skin

cmds.cfg
  • the 3 server variables (cvars) hold the 2 team names and the host flag to allow team switching
Exercises Exercises

  • allow up to 4 teams, with a cvar variable to control the number of teams (2 - 4) - you will need some new green and yellow skins for the new teams!
  • allow auto-balancing (activated by the ubiquitous cvar variable) to spread the number of players evenly when players quit
  • loads more things you can do for team play!


Page last updated 11th January 2001