3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include <extension.h>
35 #define SPEAK_NORMAL 0
38 #define SPEAK_LISTENALL 4
40 #define SPEAK_LISTENTEAM 16
41 #define LISTEN_DEFAULT 0
45 /* this enum needs to be in the order as the LISTEN_ defines above to keep
46 the old broken SetClientListening functionality untouched */
49 Listen_Default, /**< Leave it up to the game */
50 Listen_No, /**< Can't hear */
51 Listen_Yes, /**< Can hear */
54 size_t g_VoiceFlags[65];
55 size_t g_VoiceHookCount = 0;
56 int g_ClientOverrides[65];
57 ListenOverride g_VoiceMap[65][65];
58 bool g_ClientMutes[65][65];
60 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
62 #if SOURCE_ENGINE >= SE_ORANGEBOX
63 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
65 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
68 bool DecHookCount(int amount = 1);
69 bool DecHookCount(int amount)
71 g_VoiceHookCount -= amount;
72 if (g_VoiceHookCount == 0)
74 SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
83 if (!g_VoiceHookCount++)
85 SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
89 void SDKTools::VoiceInit()
91 memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
92 memset(g_ClientOverrides, 0, sizeof(g_ClientOverrides));
93 memset(g_ClientMutes, 0, sizeof(g_ClientMutes));
95 SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, serverClients, this, &SDKTools::OnClientCommand, true);
98 #if SOURCE_ENGINE >= SE_ORANGEBOX
99 void SDKTools::OnClientCommand(edict_t *pEntity, const CCommand &args)
102 void SDKTools::OnClientCommand(edict_t *pEntity)
106 int client = IndexOfEdict(pEntity);
108 if ((args.ArgC() > 1) && (stricmp(args.Arg(0), "vban") == 0))
110 for (int i = 1; (i < args.ArgC()) && (i < 3); i++)
112 unsigned long mask = 0;
113 sscanf(args[i], "%p", (void**)&mask);
115 for (int j = 0; j < 32; j++)
117 g_ClientMutes[client][1 + j + 32 * (i - 1)] = !!(mask & 1 << j);
122 RETURN_META(MRES_IGNORED);
126 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
128 if (g_ClientMutes[iReceiver][iSender])
130 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
133 if (g_VoiceMap[iReceiver][iSender] == Listen_No)
135 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
137 else if (g_VoiceMap[iReceiver][iSender] == Listen_Yes)
139 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
142 if (g_VoiceFlags[iSender] & SPEAK_MUTED)
144 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
147 if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
149 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
152 if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
154 IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
155 IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
157 if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
159 IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
160 IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
162 if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
164 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
169 RETURN_META_VALUE(MRES_IGNORED, bListen);
172 void SDKTools::OnClientDisconnecting(int client)
174 g_Hooks.OnClientDisconnecting(client);
176 int max_clients = playerhelpers->GetMaxClients();
178 if (g_VoiceHookCount == 0)
183 /* This can probably be optimized more, but I doubt it's that much
184 * of an actual bottleneck.
187 /* Reset other clients who receive from or have muted this client */
188 for (int i = 1; i <= max_clients; i++)
195 g_ClientMutes[i][client] = false;
197 if (g_VoiceMap[i][client] != Listen_Default)
199 g_VoiceMap[i][client] = Listen_Default;
207 /* Reset this client's mutes, just in case */
208 memset(&g_ClientMutes[client], 0, sizeof(int) * 65);
210 /* Reset other clients who send to this client */
211 if (g_ClientOverrides[client] > 0)
213 DecHookCount(g_ClientOverrides[client]);
214 g_ClientOverrides[client] = 0;
215 memset(&g_VoiceMap[client], false, sizeof(ListenOverride) * 65);
216 memset(&g_ClientMutes[client], false, sizeof(bool) * 65);
219 if (g_VoiceFlags[client])
221 g_VoiceFlags[client] = 0;
226 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
228 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
231 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
233 else if (!player->IsConnected())
235 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
238 if (!params[2] && g_VoiceFlags[params[1]])
242 else if (!g_VoiceFlags[params[1]] && params[2])
247 g_VoiceFlags[params[1]] = params[2];
252 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
254 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
257 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
259 else if (!player->IsConnected())
261 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
264 return g_VoiceFlags[params[1]];
267 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
272 player = playerhelpers->GetGamePlayer(params[1]);
275 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
277 else if (!player->IsConnected())
279 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
282 player = playerhelpers->GetGamePlayer(params[2]);
285 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
287 else if (!player->IsConnected())
289 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
295 if (g_VoiceMap[r][s] == Listen_Default && params[3] != Listen_Default)
297 g_VoiceMap[r][s] = (ListenOverride) params[3];
298 g_ClientOverrides[r]++;
301 else if (g_VoiceMap[r][s] != Listen_Default && params[3] == Listen_Default)
303 g_VoiceMap[r][s] = (ListenOverride) params[3];
304 g_ClientOverrides[r]--;
309 g_VoiceMap[r][s] = (ListenOverride) params[3];
315 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
319 player = playerhelpers->GetGamePlayer(params[1]);
322 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
324 else if (!player->IsConnected())
326 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
329 player = playerhelpers->GetGamePlayer(params[2]);
332 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
334 else if (!player->IsConnected())
336 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
339 return g_VoiceMap[params[1]][params[2]];
342 static cell_t IsClientMuted(IPluginContext *pContext, const cell_t *params)
346 player = playerhelpers->GetGamePlayer(params[1]);
349 return pContext->ThrowNativeError("Muter client index %d is invalid", params[1]);
351 else if (!player->IsConnected())
353 return pContext->ThrowNativeError("Muter client %d is not connected", params[1]);
356 player = playerhelpers->GetGamePlayer(params[2]);
359 return pContext->ThrowNativeError("Mutee client index %d is invalid", params[2]);
361 else if (!player->IsConnected())
363 return pContext->ThrowNativeError("Mutee client %d is not connected", params[2]);
366 return g_ClientMutes[params[1]][params[2]];
369 sp_nativeinfo_t g_VoiceNatives[] =
371 {"SetClientListeningFlags", SetClientListeningFlags},
372 {"GetClientListeningFlags", GetClientListeningFlags},
373 {"SetClientListening", SetClientListening},
374 {"GetClientListening", GetClientListening},
375 {"SetListenOverride", SetClientListening},
376 {"GetListenOverride", GetClientListening},
377 {"IsClientMuted", IsClientMuted},