Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.

258 lines
7.2 KiB

9 years ago
//========= Copyright <EFBFBD> 1996-2002, Valve LLC, All rights reserved. ============
//
8 years ago
// Purpose:
9 years ago
//
// $NoKeywords: $
//=============================================================================
#include "voice_gamemgr.h"
#include <string.h>
#include <assert.h>
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "player.h"
#define UPDATE_INTERVAL 0.3
// These are stored off as CVoiceGameMgr is created and deleted.
CPlayerBitVec g_PlayerModEnable; // Set to 1 for each player if the player wants to use voice in this mod.
// (If it's zero, then the server reports that the game rules are saying the
// player can't hear anyone).
CPlayerBitVec g_BanMasks[VOICE_MAX_PLAYERS]; // Tells which players don't want to hear each other.
// These are indexed as clients and each bit represents a client
// (so player entity is bit+1).
CPlayerBitVec g_SentGameRulesMasks[VOICE_MAX_PLAYERS]; // These store the masks we last sent to each client so we can determine if
CPlayerBitVec g_SentBanMasks[VOICE_MAX_PLAYERS]; // we need to resend them.
CPlayerBitVec g_bWantModEnable;
cvar_t voice_serverdebug = {"voice_serverdebug", "0"};
// Set game rules to allow all clients to talk to each other.
// Muted players still can't talk to each other.
cvar_t sv_alltalk = {"sv_alltalk", "0"};
// ------------------------------------------------------------------------ //
// Static helpers.
// ------------------------------------------------------------------------ //
// Find a player with a case-insensitive name search.
8 years ago
static CBasePlayer* FindPlayerByName( const char *pTestName )
9 years ago
{
8 years ago
for( int i = 1; i <= gpGlobals->maxClients; i++ )
9 years ago
{
8 years ago
edict_t *pEdict = g_engfuncs.pfnPEntityOfEntIndex( i );
if( pEdict )
9 years ago
{
8 years ago
CBaseEntity *pEnt = CBaseEntity::Instance( pEdict );
if( pEnt && pEnt->IsPlayer() )
{
const char *pNetName = STRING( pEnt->pev->netname );
if( stricmp( pNetName, pTestName ) == 0 )
9 years ago
{
return (CBasePlayer*)pEnt;
}
}
}
}
return NULL;
}
static void VoiceServerDebug( char const *pFmt, ... )
{
char msg[4096];
va_list marker;
if( !voice_serverdebug.value )
return;
va_start( marker, pFmt );
_vsnprintf( msg, sizeof(msg), pFmt, marker );
va_end( marker );
ALERT( at_console, "%s", msg );
}
// ------------------------------------------------------------------------ //
// CVoiceGameMgr.
// ------------------------------------------------------------------------ //
CVoiceGameMgr::CVoiceGameMgr()
{
m_UpdateInterval = 0;
m_nMaxPlayers = 0;
}
CVoiceGameMgr::~CVoiceGameMgr()
{
}
8 years ago
bool CVoiceGameMgr::Init( IVoiceGameMgrHelper *pHelper, int maxClients )
9 years ago
{
m_pHelper = pHelper;
m_nMaxPlayers = VOICE_MAX_PLAYERS < maxClients ? VOICE_MAX_PLAYERS : maxClients;
8 years ago
g_engfuncs.pfnPrecacheModel( "sprites/voiceicon.spr" );
9 years ago
8 years ago
m_msgPlayerVoiceMask = REG_USER_MSG( "VoiceMask", VOICE_MAX_PLAYERS_DW * 4 * 2 );
9 years ago
m_msgRequestState = REG_USER_MSG( "ReqState", 0 );
// register voice_serverdebug if it hasn't been registered already
8 years ago
if( !CVAR_GET_POINTER( "voice_serverdebug" ) )
9 years ago
CVAR_REGISTER( &voice_serverdebug );
if( !CVAR_GET_POINTER( "sv_alltalk" ) )
CVAR_REGISTER( &sv_alltalk );
return true;
}
8 years ago
void CVoiceGameMgr::SetHelper( IVoiceGameMgrHelper *pHelper )
9 years ago
{
m_pHelper = pHelper;
}
8 years ago
void CVoiceGameMgr::Update( double frametime )
9 years ago
{
// Only update periodically.
m_UpdateInterval += frametime;
8 years ago
if( m_UpdateInterval < UPDATE_INTERVAL )
9 years ago
return;
UpdateMasks();
}
8 years ago
void CVoiceGameMgr::ClientConnected( edict_t *pEdict )
9 years ago
{
8 years ago
int index = ENTINDEX( pEdict ) - 1;
9 years ago
// Clear out everything we use for deltas on this guy.
g_bWantModEnable[index] = true;
8 years ago
g_SentGameRulesMasks[index].Init( 0 );
g_SentBanMasks[index].Init( 0 );
9 years ago
}
// Called to determine if the Receiver has muted (blocked) the Sender
// Returns true if the receiver has blocked the sender
8 years ago
bool CVoiceGameMgr::PlayerHasBlockedPlayer( CBasePlayer *pReceiver, CBasePlayer *pSender )
9 years ago
{
int iReceiverIndex, iSenderIndex;
8 years ago
if( !pReceiver || !pSender )
9 years ago
return false;
iReceiverIndex = pReceiver->entindex() - 1;
8 years ago
iSenderIndex = pSender->entindex() - 1;
9 years ago
8 years ago
if( iReceiverIndex < 0 || iReceiverIndex >= m_nMaxPlayers || iSenderIndex < 0 || iSenderIndex >= m_nMaxPlayers )
9 years ago
return false;
return ( g_BanMasks[iReceiverIndex][iSenderIndex] ? true : false );
}
8 years ago
bool CVoiceGameMgr::ClientCommand( CBasePlayer *pPlayer, const char *cmd )
9 years ago
{
int playerClientIndex = pPlayer->entindex() - 1;
8 years ago
if( playerClientIndex < 0 || playerClientIndex >= m_nMaxPlayers )
9 years ago
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: cmd %s from invalid client (%d)\n", cmd, playerClientIndex );
return true;
}
8 years ago
bool bBan = stricmp( cmd, "vban" ) == 0;
if( bBan && CMD_ARGC() >= 2 )
9 years ago
{
8 years ago
for( int i = 1; i < CMD_ARGC(); i++ )
9 years ago
{
unsigned long mask = 0;
8 years ago
sscanf( CMD_ARGV(i), "%lx", &mask );
9 years ago
8 years ago
if( i <= VOICE_MAX_PLAYERS_DW )
9 years ago
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: vban (0x%x) from %d\n", mask, playerClientIndex );
8 years ago
g_BanMasks[playerClientIndex].SetDWord( i - 1, mask );
9 years ago
}
else
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: invalid index (%d)\n", i );
}
}
// Force it to update the masks now.
//UpdateMasks();
return true;
}
8 years ago
else if( stricmp( cmd, "VModEnable" ) == 0 && CMD_ARGC() >= 2 )
9 years ago
{
8 years ago
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: VModEnable (%d)\n", !!atoi( CMD_ARGV( 1 ) ) );
g_PlayerModEnable[playerClientIndex] = !!atoi( CMD_ARGV( 1 ) );
9 years ago
g_bWantModEnable[playerClientIndex] = false;
8 years ago
//UpdateMasks();
9 years ago
return true;
}
else
{
return false;
}
}
void CVoiceGameMgr::UpdateMasks()
{
m_UpdateInterval = 0;
bool bAllTalk = !!g_engfuncs.pfnCVarGetFloat( "sv_alltalk" );
8 years ago
for( int iClient = 0; iClient < m_nMaxPlayers; iClient++ )
9 years ago
{
8 years ago
CBaseEntity *pEnt = UTIL_PlayerByIndex( iClient + 1 );
if( !pEnt || !pEnt->IsPlayer() )
9 years ago
continue;
// Request the state of their "VModEnable" cvar.
8 years ago
if( g_bWantModEnable[iClient] )
9 years ago
{
8 years ago
MESSAGE_BEGIN( MSG_ONE, m_msgRequestState, NULL, pEnt->pev );
9 years ago
MESSAGE_END();
}
CBasePlayer *pPlayer = (CBasePlayer*)pEnt;
CPlayerBitVec gameRulesMask;
if( g_PlayerModEnable[iClient] )
{
// Build a mask of who they can hear based on the game rules.
8 years ago
for( int iOtherClient = 0; iOtherClient < m_nMaxPlayers; iOtherClient++ )
9 years ago
{
8 years ago
pEnt = UTIL_PlayerByIndex( iOtherClient + 1 );
if( pEnt && pEnt->IsPlayer() && ( bAllTalk || m_pHelper->CanPlayerHearPlayer( pPlayer, (CBasePlayer*)pEnt ) ) )
9 years ago
{
gameRulesMask[iOtherClient] = true;
}
}
}
// If this is different from what the client has, send an update.
8 years ago
if( gameRulesMask != g_SentGameRulesMasks[iClient] || g_BanMasks[iClient] != g_SentBanMasks[iClient] )
9 years ago
{
g_SentGameRulesMasks[iClient] = gameRulesMask;
g_SentBanMasks[iClient] = g_BanMasks[iClient];
8 years ago
MESSAGE_BEGIN( MSG_ONE, m_msgPlayerVoiceMask, NULL, pPlayer->pev );
9 years ago
int dw;
8 years ago
for( dw = 0; dw < VOICE_MAX_PLAYERS_DW; dw++ )
9 years ago
{
8 years ago
WRITE_LONG( gameRulesMask.GetDWord( dw ) );
WRITE_LONG( g_BanMasks[iClient].GetDWord( dw ) );
9 years ago
}
MESSAGE_END();
}
// Tell the engine.
8 years ago
for( int iOtherClient = 0; iOtherClient < m_nMaxPlayers; iOtherClient++ )
9 years ago
{
bool bCanHear = gameRulesMask[iOtherClient] && !g_BanMasks[iClient][iOtherClient];
8 years ago
g_engfuncs.pfnVoice_SetClientListening( iClient + 1, iOtherClient + 1, bCanHear );
9 years ago
}
}
}