1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "voice_gamemgr.h"
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "player.h"
#include "ivoiceserver.h"
#include "usermessages.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.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;
ConVar 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.
ConVar sv_alltalk( "sv_alltalk", "0", FCVAR_NOTIFY | FCVAR_REPLICATED, "Players can hear all other players, no team restrictions" );
CVoiceGameMgr g_VoiceGameMgr;
// ------------------------------------------------------------------------ //
// Static helpers.
// ------------------------------------------------------------------------ //
// Find a player with a case-insensitive name search.
#if 0
static CBasePlayer* FindPlayerByName(const char *pTestName)
{
for(int i=1; i <= gpGlobals->maxClients; i++)
{
edict_t *pEdict = engine->PEntityOfEntIndex(i);
if(pEdict)
{
CBaseEntity *pEnt = CBaseEntity::Instance(pEdict);
if(pEnt && pEnt->IsPlayer())
{
const char *pNetName = STRING(pEnt->GetEntityName());
if(stricmp(pNetName, pTestName) == 0)
{
return (CBasePlayer*)pEnt;
}
}
}
}
return NULL;
}
#endif
static void VoiceServerDebug( const char *pFmt, ... )
{
char msg[4096];
va_list marker;
if( !voice_serverdebug.GetInt() )
return;
va_start( marker, pFmt );
_vsnprintf( msg, sizeof(msg), pFmt, marker );
va_end( marker );
Msg( "%s", msg );
}
CVoiceGameMgr* GetVoiceGameMgr()
{
return &g_VoiceGameMgr;
}
// ------------------------------------------------------------------------ //
// CVoiceGameMgr.
// ------------------------------------------------------------------------ //
CVoiceGameMgr::CVoiceGameMgr()
{
m_UpdateInterval = 0;
m_nMaxPlayers = 0;
m_iProximityDistance = -1;
}
CVoiceGameMgr::~CVoiceGameMgr()
{
}
bool CVoiceGameMgr::Init(
IVoiceGameMgrHelper *pHelper,
int maxClients)
{
m_pHelper = pHelper;
m_nMaxPlayers = VOICE_MAX_PLAYERS < maxClients ? VOICE_MAX_PLAYERS : maxClients;
return true;
}
void CVoiceGameMgr::SetHelper(IVoiceGameMgrHelper *pHelper)
{
m_pHelper = pHelper;
}
void CVoiceGameMgr::Update(double frametime)
{
// Only update periodically.
m_UpdateInterval += frametime;
if(m_UpdateInterval < UPDATE_INTERVAL)
return;
UpdateMasks();
}
void CVoiceGameMgr::ClientConnected(struct edict_t *pEdict)
{
int index = ENTINDEX(pEdict) - 1;
// Clear out everything we use for deltas on this guy.
g_bWantModEnable[index] = true;
g_SentGameRulesMasks[index].Init(0);
g_SentBanMasks[index].Init(0);
}
bool CVoiceGameMgr::ClientCommand( CBasePlayer *pPlayer, const CCommand &args )
{
int playerClientIndex = pPlayer->entindex() - 1;
if(playerClientIndex < 0 || playerClientIndex >= m_nMaxPlayers)
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: cmd %s from invalid client (%d)\n", args[0], playerClientIndex );
return true;
}
bool bBan = stricmp( args[0], "vban" ) == 0;
if( bBan && args.ArgC() >= 2 )
{
for(int i=1; i < args.ArgC(); i++)
{
uint32 mask = 0;
sscanf( args[i], "%x", &mask);
if( i <= VOICE_MAX_PLAYERS_DW )
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: vban (0x%x) from %d\n", mask, playerClientIndex );
g_BanMasks[playerClientIndex].SetDWord(i-1, mask);
}
else
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: invalid index (%d)\n", i );
}
}
// Force it to update the masks now.
//UpdateMasks();
return true;
}
else if(stricmp( args[0], "VModEnable") == 0 && args.ArgC() >= 2)
{
VoiceServerDebug( "CVoiceGameMgr::ClientCommand: VModEnable (%d)\n", !!atoi( args[1] ) );
g_PlayerModEnable[playerClientIndex] = !!atoi( args[1] );
g_bWantModEnable[playerClientIndex] = false;
//UpdateMasks();
return true;
}
else
{
return false;
}
}
void CVoiceGameMgr::UpdateMasks()
{
m_UpdateInterval = 0;
bool bAllTalk = !!sv_alltalk.GetInt();
for(int iClient=0; iClient < m_nMaxPlayers; iClient++)
{
CBaseEntity *pEnt = UTIL_PlayerByIndex(iClient+1);
if(!pEnt || !pEnt->IsPlayer())
continue;
CBasePlayer *pPlayer = (CBasePlayer*)pEnt;
CSingleUserRecipientFilter user( pPlayer );
// Request the state of their "VModEnable" cvar.
if(g_bWantModEnable[iClient])
{
UserMessageBegin( user, "RequestState" );
MessageEnd();
// Since this is reliable, only send it once
g_bWantModEnable[iClient] = false;
}
CPlayerBitVec gameRulesMask;
CPlayerBitVec ProximityMask;
bool bProximity = false;
if( g_PlayerModEnable[iClient] )
{
// Build a mask of who they can hear based on the game rules.
for(int iOtherClient=0; iOtherClient < m_nMaxPlayers; iOtherClient++)
{
CBaseEntity *pEnt = UTIL_PlayerByIndex(iOtherClient+1);
if(pEnt && pEnt->IsPlayer() &&
(bAllTalk || m_pHelper->CanPlayerHearPlayer(pPlayer, (CBasePlayer*)pEnt, bProximity )) )
{
gameRulesMask[iOtherClient] = true;
ProximityMask[iOtherClient] = bProximity;
}
}
}
// If this is different from what the client has, send an update.
if(gameRulesMask != g_SentGameRulesMasks[iClient] ||
g_BanMasks[iClient] != g_SentBanMasks[iClient])
{
g_SentGameRulesMasks[iClient] = gameRulesMask;
g_SentBanMasks[iClient] = g_BanMasks[iClient];
UserMessageBegin( user, "VoiceMask" );
int dw;
for(dw=0; dw < VOICE_MAX_PLAYERS_DW; dw++)
{
WRITE_LONG(gameRulesMask.GetDWord(dw));
WRITE_LONG(g_BanMasks[iClient].GetDWord(dw));
}
WRITE_BYTE( !!g_PlayerModEnable[iClient] );
MessageEnd();
}
// Tell the engine.
for(int iOtherClient=0; iOtherClient < m_nMaxPlayers; iOtherClient++)
{
bool bCanHear = gameRulesMask[iOtherClient] && !g_BanMasks[iClient][iOtherClient];
g_pVoiceServer->SetClientListening( iClient+1, iOtherClient+1, bCanHear );
if ( bCanHear )
{
g_pVoiceServer->SetClientProximity( iClient+1, iOtherClient+1, !!ProximityMask[iOtherClient] );
}
}
}
}
bool CVoiceGameMgr::IsPlayerIgnoringPlayer( int iTalker, int iListener )
{
return !!g_BanMasks[iListener-1][iTalker-1];
}
void CVoiceGameMgr::SetProximityDistance( int iDistance )
{
m_iProximityDistance = iDistance;
}
bool CVoiceGameMgr::CheckProximity( int iDistance )
{
if ( m_iProximityDistance >= iDistance )
return true;
return false;
}
|