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[SM_MAXPLAYERS+1];
55 size_t g_VoiceHookCount = 0;
56 ListenOverride g_VoiceMap[SM_MAXPLAYERS+1][SM_MAXPLAYERS+1];
57 bool g_ClientMutes[SM_MAXPLAYERS+1][SM_MAXPLAYERS+1];
59 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
61 #if SOURCE_ENGINE == SE_DOTA
62 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CEntityIndex, const CCommand &);
63 #elif SOURCE_ENGINE >= SE_ORANGEBOX
64 SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
66 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
71 if (--g_VoiceHookCount == 0)
73 SH_REMOVE_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
82 if (!g_VoiceHookCount++)
84 SH_ADD_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
88 void SDKTools::VoiceInit()
90 memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
91 memset(g_ClientMutes, 0, sizeof(g_ClientMutes));
93 SH_ADD_HOOK(IServerGameClients, ClientCommand, serverClients, SH_MEMBER(this, &SDKTools::OnClientCommand), true);
96 #if SOURCE_ENGINE == SE_DOTA
97 void SDKTools::OnClientCommand(CEntityIndex index, const CCommand &args)
99 int client = index.Get();
100 #elif SOURCE_ENGINE >= SE_ORANGEBOX
101 void SDKTools::OnClientCommand(edict_t *pEntity, const CCommand &args)
103 int client = IndexOfEdict(pEntity);
105 void SDKTools::OnClientCommand(edict_t *pEntity)
108 int client = IndexOfEdict(pEntity);
111 if ((args.ArgC() > 1) && (stricmp(args.Arg(0), "vban") == 0))
113 for (int i = 1; (i < args.ArgC()) && (i < 3); i++)
115 unsigned long mask = 0;
116 sscanf(args.Arg(i), "%p", (void**)&mask);
118 for (int j = 0; j < 32; j++)
120 g_ClientMutes[client][1 + j + 32 * (i - 1)] = !!(mask & 1 << j);
125 RETURN_META(MRES_IGNORED);
128 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
130 if (g_ClientMutes[iReceiver][iSender])
132 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
135 if (g_VoiceFlags[iSender] & SPEAK_MUTED)
137 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
140 if (g_VoiceMap[iReceiver][iSender] == Listen_No)
142 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
144 else if (g_VoiceMap[iReceiver][iSender] == Listen_Yes)
146 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
149 if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
151 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
154 if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
156 IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
157 IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
159 if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
161 IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
162 IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
164 if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
166 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
171 RETURN_META_VALUE(MRES_IGNORED, bListen);
174 void SDKTools::OnClientDisconnecting(int 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;
196 g_ClientMutes[client][i] = false;
198 if (g_VoiceMap[i][client] != Listen_Default)
200 g_VoiceMap[i][client] = Listen_Default;
206 if (g_VoiceMap[client][i] != Listen_Default)
208 g_VoiceMap[client][i] = Listen_Default;
216 if (g_VoiceFlags[client])
218 g_VoiceFlags[client] = SPEAK_NORMAL;
223 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
225 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
228 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
230 else if (!player->IsConnected())
232 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
235 if (!params[2] && g_VoiceFlags[params[1]])
239 else if (!g_VoiceFlags[params[1]] && params[2])
244 g_VoiceFlags[params[1]] = params[2];
249 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
251 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
254 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
256 else if (!player->IsConnected())
258 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
261 return g_VoiceFlags[params[1]];
264 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
269 player = playerhelpers->GetGamePlayer(params[1]);
272 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
274 else if (!player->IsConnected())
276 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
279 player = playerhelpers->GetGamePlayer(params[2]);
282 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
284 else if (!player->IsConnected())
286 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
292 if (g_VoiceMap[r][s] == Listen_Default && params[3] != Listen_Default)
294 g_VoiceMap[r][s] = (ListenOverride) params[3];
297 else if (g_VoiceMap[r][s] != Listen_Default && params[3] == Listen_Default)
299 g_VoiceMap[r][s] = (ListenOverride) params[3];
304 g_VoiceMap[r][s] = (ListenOverride) params[3];
310 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
314 player = playerhelpers->GetGamePlayer(params[1]);
317 return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
319 else if (!player->IsConnected())
321 return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
324 player = playerhelpers->GetGamePlayer(params[2]);
327 return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
329 else if (!player->IsConnected())
331 return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
334 return g_VoiceMap[params[1]][params[2]];
337 static cell_t IsClientMuted(IPluginContext *pContext, const cell_t *params)
341 player = playerhelpers->GetGamePlayer(params[1]);
344 return pContext->ThrowNativeError("Muter client index %d is invalid", params[1]);
346 else if (!player->IsConnected())
348 return pContext->ThrowNativeError("Muter client %d is not connected", params[1]);
351 player = playerhelpers->GetGamePlayer(params[2]);
354 return pContext->ThrowNativeError("Mutee client index %d is invalid", params[2]);
356 else if (!player->IsConnected())
358 return pContext->ThrowNativeError("Mutee client %d is not connected", params[2]);
361 return g_ClientMutes[params[1]][params[2]];
364 sp_nativeinfo_t g_VoiceNatives[] =
366 {"SetClientListeningFlags", SetClientListeningFlags},
367 {"GetClientListeningFlags", GetClientListeningFlags},
368 {"SetClientListening", SetClientListening},
369 {"GetClientListening", GetClientListening},
370 {"SetListenOverride", SetClientListening},
371 {"GetListenOverride", GetClientListening},
372 {"IsClientMuted", IsClientMuted},