Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

644 lines
16 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// teamplay_gamerules.cpp
//
9 years ago
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "player.h"
#include "weapons.h"
#include "gamerules.h"
#include "teamplay_gamerules.h"
#include "game.h"
static char team_names[MAX_TEAMS][MAX_TEAMNAME_LENGTH];
static int team_scores[MAX_TEAMS];
static int num_teams = 0;
extern DLL_GLOBAL BOOL g_fGameOver;
extern int gmsgShowMenu;
/*
void ShowMenu( CBasePlayer *pPlayer, int bitsValidSlots, int nDisplayTime, BOOL fNeedMore, char pszText[500] )
{
MESSAGE_BEGIN( MSG_ONE, gmsgShowMenu, NULL, pPlayer->pev );
WRITE_SHORT( bitsValidSlots );
WRITE_CHAR( nDisplayTime );
WRITE_BYTE( fNeedMore );
WRITE_STRING( pszText );
MESSAGE_END();
}
*/
8 years ago
CHalfLifeTeamplay::CHalfLifeTeamplay()
9 years ago
{
m_DisableDeathMessages = FALSE;
m_DisableDeathPenalty = FALSE;
memset( team_names, 0, sizeof(team_names) );
memset( team_scores, 0, sizeof(team_scores) );
num_teams = 0;
// Copy over the team from the server config
m_szTeamList[0] = 0;
// Cache this because the team code doesn't want to deal with changing this in the middle of a game
strncpy( m_szTeamList, teamlist.string, TEAMPLAY_TEAMLISTLENGTH );
8 years ago
edict_t *pWorld = INDEXENT( 0 );
if( pWorld && pWorld->v.team )
9 years ago
{
8 years ago
if( teamoverride.value )
9 years ago
{
8 years ago
const char *pTeamList = STRING( pWorld->v.team );
if( pTeamList && pTeamList[0] != '\0' )
9 years ago
{
strncpy( m_szTeamList, pTeamList, TEAMPLAY_TEAMLISTLENGTH );
}
}
}
// Has the server set teams
if( m_szTeamList[0] != '\0' )
9 years ago
m_teamLimit = TRUE;
else
m_teamLimit = FALSE;
RecountTeams();
}
extern cvar_t timeleft, fragsleft;
4 years ago
#if !NO_VOICEGAMEMGR
9 years ago
#include "voice_gamemgr.h"
8 years ago
extern CVoiceGameMgr g_VoiceGameMgr;
#endif
8 years ago
void CHalfLifeTeamplay::Think( void )
9 years ago
{
///// Check game rules /////
static int last_frags;
static int last_time;
int frags_remaining = 0;
int time_remaining = 0;
4 years ago
#if !NO_VOICEGAMEMGR
9 years ago
g_VoiceGameMgr.Update(gpGlobals->frametime);
#endif
8 years ago
if( g_fGameOver ) // someone else quit the game already
9 years ago
{
CHalfLifeMultiplay::Think();
return;
}
8 years ago
float flTimeLimit = CVAR_GET_FLOAT( "mp_timelimit" ) * 60;
9 years ago
8 years ago
time_remaining = (int)( flTimeLimit ? ( flTimeLimit - gpGlobals->time ) : 0 );
9 years ago
8 years ago
if( flTimeLimit != 0 && gpGlobals->time >= flTimeLimit )
9 years ago
{
GoToIntermission();
return;
}
float flFragLimit = fraglimit.value;
8 years ago
if( flFragLimit )
9 years ago
{
int bestfrags = 9999;
int remain;
// check if any team is over the frag limit
8 years ago
for( int i = 0; i < num_teams; i++ )
9 years ago
{
8 years ago
if( team_scores[i] >= flFragLimit )
9 years ago
{
GoToIntermission();
return;
}
remain = (int)( flFragLimit - team_scores[i] );
8 years ago
if( remain < bestfrags )
9 years ago
{
bestfrags = remain;
}
}
frags_remaining = bestfrags;
}
// Updates when frags change
8 years ago
if( frags_remaining != last_frags )
9 years ago
{
g_engfuncs.pfnCvar_DirectSet( &fragsleft, UTIL_VarArgs( "%i", frags_remaining ) );
}
// Updates once per second
8 years ago
if( timeleft.value != last_time )
9 years ago
{
g_engfuncs.pfnCvar_DirectSet( &timeleft, UTIL_VarArgs( "%i", time_remaining ) );
}
last_frags = frags_remaining;
8 years ago
last_time = time_remaining;
9 years ago
}
//=========================================================
// ClientCommand
// the user has typed a command which is unrecognized by everything else;
// this check to see if the gamerules knows anything about the command
//=========================================================
8 years ago
BOOL CHalfLifeTeamplay::ClientCommand( CBasePlayer *pPlayer, const char *pcmd )
9 years ago
{
4 years ago
#if !NO_VOICEGAMEMGR
8 years ago
if( g_VoiceGameMgr.ClientCommand( pPlayer, pcmd ) )
9 years ago
return TRUE;
#endif
8 years ago
if( FStrEq( pcmd, "menuselect" ) )
9 years ago
{
8 years ago
if( CMD_ARGC() < 2 )
9 years ago
return TRUE;
//int slot = atoi( CMD_ARGV( 1 ) );
9 years ago
// select the item from the current menu
//pPlayer->m_iPlayerClass = slot;
9 years ago
return TRUE;
}
// else if( FStrEq( pcmd, "changeclass" ) )
// ShowMenu( pPlayer, 0x3F, 0, 0, "#class_list" );
9 years ago
return FALSE;
}
extern int gmsgGameMode;
extern int gmsgSayText;
extern int gmsgTeamInfo;
extern int gmsgTeamNames;
extern int gmsgScoreInfo;
8 years ago
void CHalfLifeTeamplay::UpdateGameMode( CBasePlayer *pPlayer )
9 years ago
{
MESSAGE_BEGIN( MSG_ONE, gmsgGameMode, NULL, pPlayer->edict() );
WRITE_BYTE( 1 ); // game mode teamplay
MESSAGE_END();
}
const char *CHalfLifeTeamplay::SetDefaultPlayerTeam( CBasePlayer *pPlayer )
{
// copy out the team name from the model
char *mdls = g_engfuncs.pfnInfoKeyValue( g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model" );
strncpy( pPlayer->m_szTeamName, mdls, TEAM_NAME_LENGTH );
RecountTeams();
// update the current player of the team he is joining
8 years ago
if( pPlayer->m_szTeamName[0] == '\0' || !IsValidTeam( pPlayer->m_szTeamName ) || defaultteam.value )
9 years ago
{
const char *pTeamName = NULL;
8 years ago
if( defaultteam.value )
9 years ago
{
pTeamName = team_names[0];
}
else
{
pTeamName = TeamWithFewestPlayers();
}
strncpy( pPlayer->m_szTeamName, pTeamName, TEAM_NAME_LENGTH );
}
return pPlayer->m_szTeamName;
}
//=========================================================
// InitHUD
//=========================================================
void CHalfLifeTeamplay::InitHUD( CBasePlayer *pPlayer )
{
int i;
// ShowMenu( pPlayer, 0x3F, 0, 0, "#class_list" );
9 years ago
SetDefaultPlayerTeam( pPlayer );
CHalfLifeMultiplay::InitHUD( pPlayer );
// Send down the team names
MESSAGE_BEGIN( MSG_ONE, gmsgTeamNames, NULL, pPlayer->edict() );
WRITE_BYTE( num_teams );
8 years ago
for( i = 0; i < num_teams; i++ )
9 years ago
{
8 years ago
WRITE_STRING( team_names[i] );
9 years ago
}
MESSAGE_END();
RecountTeams();
char *mdls = g_engfuncs.pfnInfoKeyValue( g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model" );
// update the current player of the team he is joining
char text[1024];
8 years ago
if( !strcmp( mdls, pPlayer->m_szTeamName ) )
9 years ago
{
sprintf( text, "* you are on team \'%s\'\n", pPlayer->m_szTeamName );
}
else
{
sprintf( text, "* assigned to team %s\n", pPlayer->m_szTeamName );
}
ChangeTeam( pPlayer, pPlayer->m_szTeamName, FALSE, FALSE );
9 years ago
UTIL_SayText( text, pPlayer );
//int clientIndex = pPlayer->entindex();
9 years ago
RecountTeams();
// update this player with all the other players team info
// loop through all active players and send their team info to the new client
8 years ago
for( i = 1; i <= gpGlobals->maxClients; i++ )
9 years ago
{
CBaseEntity *plr = UTIL_PlayerByIndex( i );
8 years ago
if( plr && IsValidTeam( plr->TeamID() ) )
9 years ago
{
MESSAGE_BEGIN( MSG_ONE, gmsgTeamInfo, NULL, pPlayer->edict() );
WRITE_BYTE( plr->entindex() );
WRITE_STRING( plr->TeamID() );
MESSAGE_END();
}
}
}
void CHalfLifeTeamplay::ChangeTeam( CBasePlayer *pPlayer, const char *pTeamName, BOOL bKill, BOOL bGib )
9 years ago
{
int damageFlags = DMG_GENERIC;
int clientIndex = pPlayer->entindex();
8 years ago
if( !bGib )
9 years ago
{
damageFlags |= DMG_NEVERGIB;
}
else
{
damageFlags |= DMG_ALWAYSGIB;
}
8 years ago
if( bKill )
9 years ago
{
// kill the player, remove a death, and let them start on the new team
m_DisableDeathMessages = TRUE;
m_DisableDeathPenalty = TRUE;
8 years ago
entvars_t *pevWorld = VARS( INDEXENT( 0 ) );
9 years ago
pPlayer->TakeDamage( pevWorld, pevWorld, 900, damageFlags );
m_DisableDeathMessages = FALSE;
m_DisableDeathPenalty = FALSE;
}
// copy out the team name from the model
if( pPlayer->m_szTeamName != pTeamName )
strncpy( pPlayer->m_szTeamName, pTeamName, TEAM_NAME_LENGTH );
9 years ago
g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName );
g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "team", pPlayer->m_szTeamName );
// notify everyone's HUD of the team change
MESSAGE_BEGIN( MSG_ALL, gmsgTeamInfo );
WRITE_BYTE( clientIndex );
WRITE_STRING( pPlayer->m_szTeamName );
MESSAGE_END();
MESSAGE_BEGIN( MSG_ALL, gmsgScoreInfo );
WRITE_BYTE( clientIndex );
WRITE_SHORT( (int)pPlayer->pev->frags );
9 years ago
WRITE_SHORT( pPlayer->m_iDeaths );
WRITE_SHORT( 0 );
WRITE_SHORT( g_pGameRules->GetTeamIndex( pPlayer->m_szTeamName ) + 1 );
MESSAGE_END();
}
//=========================================================
// ClientUserInfoChanged
//=========================================================
void CHalfLifeTeamplay::ClientUserInfoChanged( CBasePlayer *pPlayer, char *infobuffer )
{
char text[1024];
// prevent skin/color/model changes
char *mdls = g_engfuncs.pfnInfoKeyValue( infobuffer, "model" );
8 years ago
if( !stricmp( mdls, pPlayer->m_szTeamName ) )
9 years ago
return;
8 years ago
if( defaultteam.value )
9 years ago
{
int clientIndex = pPlayer->entindex();
g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName );
g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "team", pPlayer->m_szTeamName );
sprintf( text, "* Not allowed to change teams in this game!\n" );
UTIL_SayText( text, pPlayer );
return;
}
8 years ago
if( defaultteam.value || !IsValidTeam( mdls ) )
9 years ago
{
int clientIndex = pPlayer->entindex();
g_engfuncs.pfnSetClientKeyValue( clientIndex, g_engfuncs.pfnGetInfoKeyBuffer( pPlayer->edict() ), "model", pPlayer->m_szTeamName );
sprintf( text, "* Can't change team to \'%s\'\n", mdls );
UTIL_SayText( text, pPlayer );
sprintf( text, "* Server limits teams to \'%s\'\n", m_szTeamList );
UTIL_SayText( text, pPlayer );
return;
}
// notify everyone of the team change
sprintf( text, "* %s has changed to team \'%s\'\n", STRING(pPlayer->pev->netname), mdls );
UTIL_SayTextAll( text, pPlayer );
UTIL_LogPrintf( "\"%s<%i><%s><%s>\" joined team \"%s\"\n",
8 years ago
STRING( pPlayer->pev->netname ),
9 years ago
GETPLAYERUSERID( pPlayer->edict() ),
GETPLAYERAUTHID( pPlayer->edict() ),
pPlayer->m_szTeamName,
mdls );
ChangeTeam( pPlayer, mdls, TRUE, TRUE );
9 years ago
// recound stuff
RecountTeams( TRUE );
pPlayer->SetPrefsFromUserinfo( infobuffer );
9 years ago
}
extern int gmsgDeathMsg;
//=========================================================
// Deathnotice.
//=========================================================
void CHalfLifeTeamplay::DeathNotice( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pevInflictor )
{
8 years ago
if( m_DisableDeathMessages )
9 years ago
return;
8 years ago
if( pVictim && pKiller && pKiller->flags & FL_CLIENT )
9 years ago
{
CBasePlayer *pk = (CBasePlayer*) CBaseEntity::Instance( pKiller );
8 years ago
if( pk )
9 years ago
{
8 years ago
if( ( pk != pVictim ) && ( PlayerRelationship( pVictim, pk ) == GR_TEAMMATE ) )
9 years ago
{
MESSAGE_BEGIN( MSG_ALL, gmsgDeathMsg );
8 years ago
WRITE_BYTE( ENTINDEX( ENT( pKiller ) ) ); // the killer
WRITE_BYTE( ENTINDEX( pVictim->edict() ) ); // the victim
9 years ago
WRITE_STRING( "teammate" ); // flag this as a teammate kill
MESSAGE_END();
return;
}
}
}
CHalfLifeMultiplay::DeathNotice( pVictim, pKiller, pevInflictor );
}
//=========================================================
//=========================================================
8 years ago
void CHalfLifeTeamplay::PlayerKilled( CBasePlayer *pVictim, entvars_t *pKiller, entvars_t *pInflictor )
9 years ago
{
8 years ago
if( !m_DisableDeathPenalty )
9 years ago
{
CHalfLifeMultiplay::PlayerKilled( pVictim, pKiller, pInflictor );
RecountTeams();
}
}
//=========================================================
// IsTeamplay
//=========================================================
BOOL CHalfLifeTeamplay::IsTeamplay( void )
{
return TRUE;
}
BOOL CHalfLifeTeamplay::FPlayerCanTakeDamage( CBasePlayer *pPlayer, CBaseEntity *pAttacker )
{
8 years ago
if( pAttacker && PlayerRelationship( pPlayer, pAttacker ) == GR_TEAMMATE )
9 years ago
{
// my teammate hit me.
8 years ago
if( ( friendlyfire.value == 0 ) && ( pAttacker != pPlayer ) )
9 years ago
{
// friendly fire is off, and this hit came from someone other than myself, then don't get hurt
return FALSE;
}
}
return CHalfLifeMultiplay::FPlayerCanTakeDamage( pPlayer, pAttacker );
}
//=========================================================
//=========================================================
int CHalfLifeTeamplay::PlayerRelationship( CBaseEntity *pPlayer, CBaseEntity *pTarget )
{
// half life multiplay has a simple concept of Player Relationships.
// you are either on another player's team, or you are not.
8 years ago
if( !pPlayer || !pTarget || !pTarget->IsPlayer() )
9 years ago
return GR_NOTTEAMMATE;
8 years ago
if( ( *GetTeamID( pPlayer ) != '\0' ) && ( *GetTeamID( pTarget ) != '\0' ) && !stricmp( GetTeamID( pPlayer ), GetTeamID( pTarget ) ) )
9 years ago
{
return GR_TEAMMATE;
}
return GR_NOTTEAMMATE;
}
//=========================================================
//=========================================================
BOOL CHalfLifeTeamplay::ShouldAutoAim( CBasePlayer *pPlayer, edict_t *target )
{
// always autoaim, unless target is a teammate
CBaseEntity *pTgt = CBaseEntity::Instance( target );
8 years ago
if( pTgt && pTgt->IsPlayer() )
9 years ago
{
8 years ago
if( PlayerRelationship( pPlayer, pTgt ) == GR_TEAMMATE )
9 years ago
return FALSE; // don't autoaim at teammates
}
return CHalfLifeMultiplay::ShouldAutoAim( pPlayer, target );
}
//=========================================================
//=========================================================
int CHalfLifeTeamplay::IPointsForKill( CBasePlayer *pAttacker, CBasePlayer *pKilled )
{
8 years ago
if( !pKilled )
9 years ago
return 0;
8 years ago
if( !pAttacker )
9 years ago
return 1;
8 years ago
if( pAttacker != pKilled && PlayerRelationship( pAttacker, pKilled ) == GR_TEAMMATE )
9 years ago
return -1;
return 1;
}
//=========================================================
//=========================================================
const char *CHalfLifeTeamplay::GetTeamID( CBaseEntity *pEntity )
{
8 years ago
if( pEntity == NULL || pEntity->pev == NULL )
9 years ago
return "";
// return their team name
return pEntity->TeamID();
}
int CHalfLifeTeamplay::GetTeamIndex( const char *pTeamName )
{
8 years ago
if( pTeamName && *pTeamName != 0 )
9 years ago
{
// try to find existing team
8 years ago
for( int tm = 0; tm < num_teams; tm++ )
9 years ago
{
8 years ago
if( !stricmp( team_names[tm], pTeamName ) )
9 years ago
return tm;
}
}
9 years ago
return -1; // No match
}
const char *CHalfLifeTeamplay::GetIndexedTeamName( int teamIndex )
{
8 years ago
if( teamIndex < 0 || teamIndex >= num_teams )
9 years ago
return "";
8 years ago
return team_names[teamIndex];
9 years ago
}
BOOL CHalfLifeTeamplay::IsValidTeam( const char *pTeamName )
{
8 years ago
if( !m_teamLimit ) // Any team is valid if the teamlist isn't set
9 years ago
return TRUE;
return ( GetTeamIndex( pTeamName ) != -1 ) ? TRUE : FALSE;
}
const char *CHalfLifeTeamplay::TeamWithFewestPlayers( void )
{
int i;
int minPlayers = MAX_TEAMS;
int teamCount[MAX_TEAMS] = {0};
9 years ago
char *pTeamName = NULL;
// loop through all clients, count number of players on each team
8 years ago
for( i = 1; i <= gpGlobals->maxClients; i++ )
9 years ago
{
CBaseEntity *plr = UTIL_PlayerByIndex( i );
8 years ago
if( plr )
9 years ago
{
int team = GetTeamIndex( plr->TeamID() );
8 years ago
if( team >= 0 )
teamCount[team]++;
9 years ago
}
}
// Find team with least players
8 years ago
for( i = 0; i < num_teams; i++ )
9 years ago
{
8 years ago
if( teamCount[i] < minPlayers )
9 years ago
{
minPlayers = teamCount[i];
pTeamName = team_names[i];
}
}
return pTeamName;
}
//=========================================================
//=========================================================
void CHalfLifeTeamplay::RecountTeams( bool bResendInfo )
{
8 years ago
char *pName;
char teamlist[TEAMPLAY_TEAMLISTLENGTH];
9 years ago
// loop through all teams, recounting everything
num_teams = 0;
// Copy all of the teams from the teamlist
// make a copy because strtok is destructive
strcpy( teamlist, m_szTeamList );
pName = teamlist;
pName = strtok( pName, ";" );
8 years ago
while( pName != NULL && *pName )
9 years ago
{
8 years ago
if( GetTeamIndex( pName ) < 0 )
9 years ago
{
strcpy( team_names[num_teams], pName );
num_teams++;
}
pName = strtok( NULL, ";" );
}
8 years ago
if( num_teams < 2 )
9 years ago
{
num_teams = 0;
m_teamLimit = FALSE;
}
// Sanity check
memset( team_scores, 0, sizeof(team_scores) );
// loop through all clients
8 years ago
for( int i = 1; i <= gpGlobals->maxClients; i++ )
9 years ago
{
CBaseEntity *plr = UTIL_PlayerByIndex( i );
8 years ago
if( plr )
9 years ago
{
const char *pTeamName = plr->TeamID();
9 years ago
// try add to existing team
int tm = GetTeamIndex( pTeamName );
8 years ago
if( tm < 0 ) // no team match found
9 years ago
{
8 years ago
if( !m_teamLimit )
9 years ago
{
// add to new team
tm = num_teams;
num_teams++;
team_scores[tm] = 0;
strncpy( team_names[tm], pTeamName, MAX_TEAMNAME_LENGTH );
}
}
8 years ago
if( tm >= 0 )
9 years ago
{
team_scores[tm] += (int)plr->pev->frags;
9 years ago
}
8 years ago
if( bResendInfo ) //Someone's info changed, let's send the team info again.
9 years ago
{
8 years ago
if( plr && IsValidTeam( plr->TeamID() ) )
9 years ago
{
MESSAGE_BEGIN( MSG_ALL, gmsgTeamInfo, NULL );
WRITE_BYTE( plr->entindex() );
WRITE_STRING( plr->TeamID() );
MESSAGE_END();
}
}
}
}
}