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>
34 #define SPEAK_NORMAL 0
37 #define SPEAK_LISTENALL 4
39 #define SPEAK_LISTENTEAM 16
40 #define LISTEN_DEFAULT 0
44 size_t g_VoiceFlags[65];
45 size_t g_VoiceHookCount = 0;
46 int g_VoiceMap[65][65];
48 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
50 bool DecHookCount(int amount = 1);
51 bool DecHookCount(int amount)
53 g_VoiceHookCount -= amount;
54 if (g_VoiceHookCount == 0)
56 SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
65 if (!g_VoiceHookCount++)
67 SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
71 void SDKTools::VoiceInit()
73 memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
76 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
78 if (g_VoiceMap[iReceiver][iSender] == LISTEN_NO)
80 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
82 else if (g_VoiceMap[iReceiver][iSender] == LISTEN_YES)
84 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
87 if (g_VoiceFlags[iSender] & SPEAK_MUTED)
89 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
92 if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
94 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
97 if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
99 IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
100 IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
102 if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
104 IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
105 IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
107 if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
109 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
114 RETURN_META_VALUE(MRES_IGNORED, bListen);
117 void SDKTools::OnClientDisconnecting(int client)
119 int max_clients = playerhelpers->GetMaxClients();
121 if (g_VoiceHookCount == 0)
126 /* This can probably be optimized more, but I doubt it's that much
127 * of an actual bottleneck.
130 /* Reset clients who receive from us */
131 for (int i = 1; i <= max_clients; i++)
138 if (g_VoiceMap[i][client] != LISTEN_DEFAULT)
140 g_VoiceMap[i][client] = LISTEN_DEFAULT;
148 /* Reset clients who send to us. I'm shoving a count in the 0 index! */
149 if (g_VoiceMap[client][0] > 0)
151 DecHookCount(g_VoiceMap[client][0]);
152 memset(&g_VoiceMap[client], 0, sizeof(int) * 65);
155 if (g_VoiceFlags[client])
157 g_VoiceFlags[client] = 0;
162 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
164 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
167 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
169 else if (!player->IsConnected())
171 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
174 if (!params[2] && g_VoiceFlags[params[1]])
178 else if (!g_VoiceFlags[params[1]] && params[2])
183 g_VoiceFlags[params[1]] = params[2];
188 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
190 IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
193 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
195 else if (!player->IsConnected())
197 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
200 return g_VoiceFlags[params[1]];
203 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
208 player = playerhelpers->GetGamePlayer(params[1]);
211 return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]);
213 else if (!player->IsConnected())
215 return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]);
218 player = playerhelpers->GetGamePlayer(params[2]);
221 return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]);
223 else if (!player->IsConnected())
225 return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]);
231 if (g_VoiceMap[r][s] == LISTEN_DEFAULT && params[3] != LISTEN_DEFAULT)
233 g_VoiceMap[r][s] = params[3];
237 else if (g_VoiceMap[r][s] != LISTEN_DEFAULT && params[3] == LISTEN_DEFAULT)
239 g_VoiceMap[r][s] = params[3];
245 g_VoiceMap[r][s] = params[3];
251 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
255 player = playerhelpers->GetGamePlayer(params[1]);
258 return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]);
260 else if (!player->IsConnected())
262 return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]);
265 player = playerhelpers->GetGamePlayer(params[2]);
268 return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]);
270 else if (!player->IsConnected())
272 return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]);
275 return g_VoiceMap[params[1]][params[2]];
278 sp_nativeinfo_t g_VoiceNatives[] =
280 {"SetClientListeningFlags", SetClientListeningFlags},
281 {"GetClientListeningFlags", GetClientListeningFlags},
282 {"SetClientListening", SetClientListening},
283 {"GetClientListening", GetClientListening},