1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/voice.cpp Sun Jul 06 04:49:55 2008 +0000
1.3 @@ -0,0 +1,285 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod SDKTools Extension
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include <extension.h>
1.36 +
1.37 +#define SPEAK_NORMAL 0
1.38 +#define SPEAK_MUTED 1
1.39 +#define SPEAK_ALL 2
1.40 +#define SPEAK_LISTENALL 4
1.41 +#define SPEAK_TEAM 8
1.42 +#define SPEAK_LISTENTEAM 16
1.43 +#define LISTEN_DEFAULT 0
1.44 +#define LISTEN_NO 1
1.45 +#define LISTEN_YES 2
1.46 +
1.47 +size_t g_VoiceFlags[65];
1.48 +size_t g_VoiceHookCount = 0;
1.49 +int g_VoiceMap[65][65];
1.50 +
1.51 +SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
1.52 +
1.53 +bool DecHookCount(int amount = 1);
1.54 +bool DecHookCount(int amount)
1.55 +{
1.56 + g_VoiceHookCount -= amount;
1.57 + if (g_VoiceHookCount == 0)
1.58 + {
1.59 + SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
1.60 + return true;
1.61 + }
1.62 +
1.63 + return false;
1.64 +}
1.65 +
1.66 +void IncHookCount()
1.67 +{
1.68 + if (!g_VoiceHookCount++)
1.69 + {
1.70 + SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
1.71 + }
1.72 +}
1.73 +
1.74 +void SDKTools::VoiceInit()
1.75 +{
1.76 + memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
1.77 +}
1.78 +
1.79 +bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
1.80 +{
1.81 + if (g_VoiceMap[iReceiver][iSender] == LISTEN_NO)
1.82 + {
1.83 + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
1.84 + }
1.85 + else if (g_VoiceMap[iReceiver][iSender] == LISTEN_YES)
1.86 + {
1.87 + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
1.88 + }
1.89 +
1.90 + if (g_VoiceFlags[iSender] & SPEAK_MUTED)
1.91 + {
1.92 + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
1.93 + }
1.94 +
1.95 + if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
1.96 + {
1.97 + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
1.98 + }
1.99 +
1.100 + if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
1.101 + {
1.102 + IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
1.103 + IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
1.104 +
1.105 + if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
1.106 + {
1.107 + IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
1.108 + IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
1.109 +
1.110 + if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
1.111 + {
1.112 + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
1.113 + }
1.114 + }
1.115 + }
1.116 +
1.117 + RETURN_META_VALUE(MRES_IGNORED, bListen);
1.118 +}
1.119 +
1.120 +void SDKTools::OnClientDisconnecting(int client)
1.121 +{
1.122 + int max_clients = playerhelpers->GetMaxClients();
1.123 +
1.124 + if (g_VoiceHookCount == 0)
1.125 + {
1.126 + return;
1.127 + }
1.128 +
1.129 + /* This can probably be optimized more, but I doubt it's that much
1.130 + * of an actual bottleneck.
1.131 + */
1.132 +
1.133 + /* Reset clients who receive from us */
1.134 + for (int i = 1; i <= max_clients; i++)
1.135 + {
1.136 + if (i == client)
1.137 + {
1.138 + continue;
1.139 + }
1.140 +
1.141 + if (g_VoiceMap[i][client] != LISTEN_DEFAULT)
1.142 + {
1.143 + g_VoiceMap[i][client] = LISTEN_DEFAULT;
1.144 + if (DecHookCount())
1.145 + {
1.146 + return;
1.147 + }
1.148 + }
1.149 + }
1.150 +
1.151 + /* Reset clients who send to us. I'm shoving a count in the 0 index! */
1.152 + if (g_VoiceMap[client][0] > 0)
1.153 + {
1.154 + DecHookCount(g_VoiceMap[client][0]);
1.155 + memset(&g_VoiceMap[client], 0, sizeof(int) * 65);
1.156 + }
1.157 +
1.158 + if (g_VoiceFlags[client])
1.159 + {
1.160 + g_VoiceFlags[client] = 0;
1.161 + DecHookCount();
1.162 + }
1.163 +}
1.164 +
1.165 +static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
1.166 +{
1.167 + IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1.168 + if (player == NULL)
1.169 + {
1.170 + return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
1.171 + }
1.172 + else if (!player->IsConnected())
1.173 + {
1.174 + return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1.175 + }
1.176 +
1.177 + if (!params[2] && g_VoiceFlags[params[1]])
1.178 + {
1.179 + DecHookCount();
1.180 + }
1.181 + else if (!g_VoiceFlags[params[1]] && params[2])
1.182 + {
1.183 + IncHookCount();
1.184 + }
1.185 +
1.186 + g_VoiceFlags[params[1]] = params[2];
1.187 +
1.188 + return 1;
1.189 +}
1.190 +
1.191 +static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
1.192 +{
1.193 + IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
1.194 + if (player == NULL)
1.195 + {
1.196 + return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
1.197 + }
1.198 + else if (!player->IsConnected())
1.199 + {
1.200 + return pContext->ThrowNativeError("Client %d is not connected", params[1]);
1.201 + }
1.202 +
1.203 + return g_VoiceFlags[params[1]];
1.204 +}
1.205 +
1.206 +static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
1.207 +{
1.208 + int r, s;
1.209 + IGamePlayer *player;
1.210 +
1.211 + player = playerhelpers->GetGamePlayer(params[1]);
1.212 + if (player == NULL)
1.213 + {
1.214 + return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]);
1.215 + }
1.216 + else if (!player->IsConnected())
1.217 + {
1.218 + return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]);
1.219 + }
1.220 +
1.221 + player = playerhelpers->GetGamePlayer(params[2]);
1.222 + if (player == NULL)
1.223 + {
1.224 + return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]);
1.225 + }
1.226 + else if (!player->IsConnected())
1.227 + {
1.228 + return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]);
1.229 + }
1.230 +
1.231 + r = params[1];
1.232 + s = params[2];
1.233 +
1.234 + if (g_VoiceMap[r][s] == LISTEN_DEFAULT && params[3] != LISTEN_DEFAULT)
1.235 + {
1.236 + g_VoiceMap[r][s] = params[3];
1.237 + g_VoiceMap[r][0]++;
1.238 + IncHookCount();
1.239 + }
1.240 + else if (g_VoiceMap[r][s] != LISTEN_DEFAULT && params[3] == LISTEN_DEFAULT)
1.241 + {
1.242 + g_VoiceMap[r][s] = params[3];
1.243 + g_VoiceMap[r][0]--;
1.244 + DecHookCount();
1.245 + }
1.246 + else
1.247 + {
1.248 + g_VoiceMap[r][s] = params[3];
1.249 + }
1.250 +
1.251 + return 1;
1.252 +}
1.253 +
1.254 +static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
1.255 +{
1.256 + IGamePlayer *player;
1.257 +
1.258 + player = playerhelpers->GetGamePlayer(params[1]);
1.259 + if (player == NULL)
1.260 + {
1.261 + return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]);
1.262 + }
1.263 + else if (!player->IsConnected())
1.264 + {
1.265 + return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]);
1.266 + }
1.267 +
1.268 + player = playerhelpers->GetGamePlayer(params[2]);
1.269 + if (player == NULL)
1.270 + {
1.271 + return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]);
1.272 + }
1.273 + else if (!player->IsConnected())
1.274 + {
1.275 + return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]);
1.276 + }
1.277 +
1.278 + return g_VoiceMap[params[1]][params[2]];
1.279 +}
1.280 +
1.281 +sp_nativeinfo_t g_VoiceNatives[] =
1.282 +{
1.283 + {"SetClientListeningFlags", SetClientListeningFlags},
1.284 + {"GetClientListeningFlags", GetClientListeningFlags},
1.285 + {"SetClientListening", SetClientListening},
1.286 + {"GetClientListening", GetClientListening},
1.287 + {NULL, NULL},
1.288 +};