View Single Post
Old 12-08-2010, 10:51 PM   #4
-=*Raz0r*=-
Rookie
 
-=*Raz0r*=-'s Avatar
 
Join Date: Oct 2006
Location: Australia
Posts: 243
Swapping teams is handled via a command ("team x")
You will see other examples of players not being able to join certain teams in Game -> g_cmds.c -> Cmd_Team_f (Which will then call SetTeam, but I suggest keeping your changes on a higher level)

I'd create a variable inside the level_locals_t struct (or even make use of cvars) and control it via an RCon command, or an ingame admin system if you have one set up (I may write a tutorial on setting up a decent admin system some day)

I haven't tested this, but it should work.
I adjusted the end of Cmd_Team_f to look like this:
Code:
	trap_Argv( 1, s, sizeof( s ) );

	//[LockedTeams]
	if ( level.lockedTeams[TEAM_FREE] )
	{//Free
		if ( !Q_stricmp( s, "f" ) )
		{
			trap_SendServerCommand( ent-g_entities, "print \"^3Free ^7team is locked\n\"" );
			return;
		}
	}
	if ( level.lockedTeams[TEAM_RED] )
	{//Red
		if ( !Q_stricmp( s, "red" ) || !Q_stricmp( s, "r" ) )
		{
			trap_SendServerCommand( ent-g_entities, "print \"^1Red ^7team is locked\n\"" );
			return;
		}
	}
	if ( level.lockedTeams[TEAM_BLUE] )
	{//Blue
		if ( !Q_stricmp( s, "blue" ) || !Q_stricmp( s, "b" ) )
		{
			trap_SendServerCommand( ent-g_entities, "print \"^5Blue ^7team is locked\n\"" );
			return;
		}
	}
	if ( level.lockedTeams[TEAM_SPECTATOR] )
	{//Spectator
		if ( !Q_stricmpn( s, "score", 5 ) || !Q_stricmpn( s, "follow", 6 ) || !Q_stricmp( s, "spectator" ) || !Q_stricmp( s, "s" ) )
		{
			trap_SendServerCommand( ent-g_entities, "print \"^3Spectator ^7team is locked\n\"" );
			return;
		}
	}
	//[/LockedTeams]

	SetTeam( ent, s );

	ent->client->switchTeamTime = level.time + 5000;
}
I recommend using cvars which would save these settings across rounds. I'll leave the RCon/admin command up to you (RCon commands are handled in g_svcmds.c -> ConsoleCommand)
There may be logic errors involving gametypes, or unhandled teams, but I hope not =p

EDIT: Oh whatever, I got bored.

The end of g_svcmds.c -> ConsoleCommand will look like:
Code:
	if (Q_stricmp (cmd, "listip") == 0) {
		trap_SendConsoleCommand( EXEC_NOW, "g_banIPs\n" );
		return qtrue;
	}

	//[LockedTeams]
	if ( !Q_stricmp( cmd, "lockteam" ) )
	{
		Svcmd_LockTeam_f();
		return qtrue;
	}
	if ( !Q_stricmp( cmd, "unlockteam" ) )
	{
		Svcmd_UnlockTeam_f();
		return qtrue;
	}
	if ( !Q_stricmp( cmd, "lockedteams" ) )
	{
		G_Printf( "- Free team is %s\n", level.lockedTeams[TEAM_FREE] ? "LOCKED" : "Unlocked" );
		G_Printf( "- Red team is %s\n", level.lockedTeams[TEAM_RED] ? "LOCKED" : "Unlocked" );
		G_Printf( "- Blue team is %s\n", level.lockedTeams[TEAM_BLUE] ? "LOCKED" : "Unlocked" );
		G_Printf( "- Spectator team is %s\n\n", level.lockedTeams[TEAM_SPECTATOR] ? "LOCKED" : "Unlocked" );
		return qtrue;
	}
	//[/LockedTeams]

	if (g_dedicated.integer) {
		if (Q_stricmp (cmd, "say") == 0) {
			trap_SendServerCommand( -1, va("print \"server: %s\n\"", ConcatArgs(1) ) );
			return qtrue;
		}
		// everything else will also be printed as a say command
		trap_SendServerCommand( -1, va("print \"server: %s\n\"", ConcatArgs(0) ) );
		return qtrue;
	}

	return qfalse;
}
Above that function, I have this:
Code:
//[LockedTeams]
static void Svcmd_LockTeam_f( void )
{
	char str[MAX_TOKEN_CHARS];

	if ( trap_Argc() < 2 )
	{
		G_Printf( "Usage: lockteam <team>\n" );
		return;
	}

	trap_Argv( 1, str, sizeof( str ) );

		 if ( str[0] == 'f' )	level.lockedTeams[TEAM_FREE] = qtrue;
	else if ( str[0] == 'r' )	level.lockedTeams[TEAM_RED] = qtrue;
	else if ( str[0] == 'b' )	level.lockedTeams[TEAM_BLUE] = qtrue;
	else if ( str[0] == 's' )	level.lockedTeams[TEAM_SPECTATOR] = qtrue;
	else						G_Printf( "Invalid team '%s'\n", str );
}

static void Svcmd_UnlockTeam_f( void )
{
	char str[MAX_TOKEN_CHARS];

	if ( trap_Argc() < 2 )
	{
		G_Printf( "Usage: unlockteam <team>\n" );
		return;
	}

	trap_Argv( 1, str, sizeof( str ) );

		 if ( str[0] == 'f' )	level.lockedTeams[TEAM_FREE] = qfalse;
	else if ( str[0] == 'r' )	level.lockedTeams[TEAM_RED] = qfalse;
	else if ( str[0] == 'b' )	level.lockedTeams[TEAM_BLUE] = qfalse;
	else if ( str[0] == 's' )	level.lockedTeams[TEAM_SPECTATOR] = qfalse;
	else						G_Printf( "Invalid team '%s'\n", str );
}
//[/LockedTeams]
This part is tested =p


EDIT2: Ok, after a bit of testing, I found some issues
  • "team r" in an FFA game will let you in, even if free team is locked <-- Fix this by checking if gametype < GT_TEAM and testing for "r", "red", etc
  • "team asdf" in an FFA game will let you in even if free team is locked <-- Might want to deny any invalid team names to fix this
I'll probably end up using entirely different code paths based on gametype.
Anyway, this should be enough to get you started =p..

Last edited by -=*Raz0r*=-; 12-10-2010 at 12:47 AM.
-=*Raz0r*=- is offline   you may: quote & reply,