extensions/sdktools/voice.cpp
author Kyle Sanderson <kyle.leet@gmail.com>
Tue May 06 18:40:35 2014 -0700 (2014-05-06)
changeset 4344 c445c3266192
parent 4268 3c383c5b1aaa
permissions -rw-r--r--
Unify Maximum supported clients, raised SDKTools limit (bug 5964, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  *
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <extension.h>
    33 #include <hooks.h>
    34 
    35 #define SPEAK_NORMAL		0
    36 #define SPEAK_MUTED			1
    37 #define SPEAK_ALL			2
    38 #define SPEAK_LISTENALL		4
    39 #define SPEAK_TEAM			8
    40 #define SPEAK_LISTENTEAM	16
    41 #define LISTEN_DEFAULT		0
    42 #define LISTEN_NO			1
    43 #define LISTEN_YES			2
    44 
    45 /* this enum needs to be in the order as the LISTEN_ defines above to keep
    46    the old broken SetClientListening functionality untouched */
    47 enum ListenOverride
    48 {
    49 	Listen_Default,		/**< Leave it up to the game */
    50 	Listen_No,			/**< Can't hear */
    51 	Listen_Yes,			/**< Can hear */
    52 };
    53 
    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];
    58 
    59 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
    60 
    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 &);
    65 #else
    66 SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
    67 #endif
    68 
    69 bool DecHookCount()
    70 {
    71 	if (--g_VoiceHookCount == 0)
    72 	{
    73 		SH_REMOVE_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
    74 		return true;
    75 	}
    76 
    77 	return false;
    78 }
    79 
    80 void IncHookCount()
    81 {
    82 	if (!g_VoiceHookCount++)
    83 	{
    84 		SH_ADD_HOOK(IVoiceServer, SetClientListening, voiceserver, SH_MEMBER(&g_SdkTools, &SDKTools::OnSetClientListening), false);
    85 	}
    86 }
    87 
    88 void SDKTools::VoiceInit()
    89 {
    90 	memset(g_VoiceMap, 0, sizeof(g_VoiceMap));
    91 	memset(g_ClientMutes, 0, sizeof(g_ClientMutes));
    92 
    93 	SH_ADD_HOOK(IServerGameClients, ClientCommand, serverClients, SH_MEMBER(this, &SDKTools::OnClientCommand), true);
    94 }
    95 
    96 #if SOURCE_ENGINE == SE_DOTA
    97 void SDKTools::OnClientCommand(CEntityIndex index, const CCommand &args)
    98 {
    99 	int client = index.Get();
   100 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   101 void SDKTools::OnClientCommand(edict_t *pEntity, const CCommand &args)
   102 {
   103 	int client = IndexOfEdict(pEntity);
   104 #else
   105 void SDKTools::OnClientCommand(edict_t *pEntity)
   106 {
   107 	CCommand args;
   108 	int client = IndexOfEdict(pEntity);
   109 #endif
   110 
   111 	if ((args.ArgC() > 1) && (stricmp(args.Arg(0), "vban") == 0))
   112 	{
   113 		for (int i = 1; (i < args.ArgC()) && (i < 3); i++)
   114 		{
   115 			unsigned long mask = 0;
   116 			sscanf(args.Arg(i), "%p", (void**)&mask);
   117 			
   118 			for (int j = 0; j < 32; j++)
   119 			{
   120 				g_ClientMutes[client][1 + j + 32 * (i - 1)] = !!(mask & 1 << j);
   121 			}
   122 		}
   123 	}
   124 
   125 	RETURN_META(MRES_IGNORED);
   126 }
   127 
   128 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
   129 {
   130 	if (g_ClientMutes[iReceiver][iSender])
   131 	{
   132 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
   133 	}
   134 
   135 	if (g_VoiceFlags[iSender] & SPEAK_MUTED)
   136 	{
   137 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
   138 	}
   139 
   140 	if (g_VoiceMap[iReceiver][iSender] == Listen_No)
   141 	{
   142 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
   143 	}
   144 	else if (g_VoiceMap[iReceiver][iSender] == Listen_Yes)
   145 	{
   146 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
   147 	}
   148 
   149 	if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
   150 	{
   151 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
   152 	}
   153 
   154 	if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
   155 	{
   156 		IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
   157 		IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
   158 
   159 		if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
   160 		{
   161 			IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
   162 			IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
   163 
   164 			if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
   165 			{
   166 				RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
   167 			}
   168 		}
   169 	}
   170 
   171 	RETURN_META_VALUE(MRES_IGNORED, bListen);
   172 }
   173 
   174 void SDKTools::OnClientDisconnecting(int client)
   175 {
   176 	int max_clients = playerhelpers->GetMaxClients();
   177 
   178 	if (g_VoiceHookCount == 0)
   179 	{
   180 		return;
   181 	}
   182 
   183 	/* This can probably be optimized more, but I doubt it's that much 
   184 	 * of an actual bottleneck.
   185 	 */
   186 
   187 	/* Reset other clients who receive from or have muted this client */
   188 	for (int i = 1; i <= max_clients; i++)
   189 	{
   190 		if (i == client)
   191 		{
   192 			continue;
   193 		}
   194 
   195 		g_ClientMutes[i][client] = false;
   196 		g_ClientMutes[client][i] = false;
   197 		
   198 		if (g_VoiceMap[i][client] != Listen_Default)
   199 		{
   200 			g_VoiceMap[i][client] = Listen_Default;
   201 			if (DecHookCount())
   202 			{
   203 				break;
   204 			}
   205 		}
   206 		if (g_VoiceMap[client][i] != Listen_Default)
   207 		{
   208 			g_VoiceMap[client][i] = Listen_Default;
   209 			if (DecHookCount())
   210 			{
   211 				break;
   212 			}
   213 		}
   214 	}
   215 
   216 	if (g_VoiceFlags[client])
   217 	{
   218 		g_VoiceFlags[client] = SPEAK_NORMAL;
   219 		DecHookCount();
   220 	}
   221 }
   222 
   223 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
   224 {
   225 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   226 	if (player == NULL)
   227 	{
   228 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   229 	}
   230 	else if (!player->IsConnected())
   231 	{
   232 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   233 	}
   234 
   235 	if (!params[2] && g_VoiceFlags[params[1]])
   236 	{
   237 		DecHookCount();
   238 	}
   239 	else if (!g_VoiceFlags[params[1]] && params[2])
   240 	{
   241 		IncHookCount();
   242 	}
   243 
   244 	g_VoiceFlags[params[1]] = params[2];
   245 
   246 	return 1;
   247 }
   248 
   249 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
   250 {
   251 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   252 	if (player == NULL)
   253 	{
   254 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   255 	}
   256 	else if (!player->IsConnected())
   257 	{
   258 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   259 	}
   260 
   261 	return g_VoiceFlags[params[1]];
   262 }
   263 
   264 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
   265 {
   266 	int r, s;
   267 	IGamePlayer *player;
   268 	
   269 	player = playerhelpers->GetGamePlayer(params[1]);
   270 	if (player == NULL)
   271 	{
   272 		return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
   273 	}
   274 	else if (!player->IsConnected())
   275 	{
   276 		return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
   277 	}
   278 
   279 	player = playerhelpers->GetGamePlayer(params[2]);
   280 	if (player == NULL)
   281 	{
   282 		return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
   283 	}
   284 	else if (!player->IsConnected())
   285 	{
   286 		return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
   287 	}
   288 
   289 	r = params[1];
   290 	s = params[2];
   291 	
   292 	if (g_VoiceMap[r][s] == Listen_Default && params[3] != Listen_Default)
   293 	{
   294 		g_VoiceMap[r][s] = (ListenOverride) params[3];
   295 		IncHookCount();
   296 	}
   297 	else if (g_VoiceMap[r][s] != Listen_Default && params[3] == Listen_Default)
   298 	{
   299 		g_VoiceMap[r][s] = (ListenOverride) params[3];
   300 		DecHookCount();
   301 	}
   302 	else
   303 	{
   304 		g_VoiceMap[r][s] = (ListenOverride) params[3];
   305 	}
   306 
   307 	return 1;
   308 }
   309 
   310 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
   311 {
   312 	IGamePlayer *player;
   313 
   314 	player = playerhelpers->GetGamePlayer(params[1]);
   315 	if (player == NULL)
   316 	{
   317 		return pContext->ThrowNativeError("Receiver client index %d is invalid", params[1]);
   318 	}
   319 	else if (!player->IsConnected())
   320 	{
   321 		return pContext->ThrowNativeError("Receiver client %d is not connected", params[1]);
   322 	}
   323 
   324 	player = playerhelpers->GetGamePlayer(params[2]);
   325 	if (player == NULL)
   326 	{
   327 		return pContext->ThrowNativeError("Sender client index %d is invalid", params[2]);
   328 	}
   329 	else if (!player->IsConnected())
   330 	{
   331 		return pContext->ThrowNativeError("Sender client %d is not connected", params[2]);
   332 	}
   333 
   334 	return g_VoiceMap[params[1]][params[2]];
   335 }
   336 
   337 static cell_t IsClientMuted(IPluginContext *pContext, const cell_t *params)
   338 {
   339 	IGamePlayer *player;
   340 
   341 	player = playerhelpers->GetGamePlayer(params[1]);
   342 	if (player == NULL)
   343 	{
   344 		return pContext->ThrowNativeError("Muter client index %d is invalid", params[1]);
   345 	}
   346 	else if (!player->IsConnected())
   347 	{
   348 		return pContext->ThrowNativeError("Muter client %d is not connected", params[1]);
   349 	}
   350 
   351 	player = playerhelpers->GetGamePlayer(params[2]);
   352 	if (player == NULL)
   353 	{
   354 		return pContext->ThrowNativeError("Mutee client index %d is invalid", params[2]);
   355 	}
   356 	else if (!player->IsConnected())
   357 	{
   358 		return pContext->ThrowNativeError("Mutee client %d is not connected", params[2]);
   359 	}
   360 
   361 	return g_ClientMutes[params[1]][params[2]];
   362 }
   363 
   364 sp_nativeinfo_t g_VoiceNatives[] =
   365 {
   366 	{"SetClientListeningFlags",		SetClientListeningFlags},
   367 	{"GetClientListeningFlags",		GetClientListeningFlags},
   368 	{"SetClientListening",			SetClientListening},
   369 	{"GetClientListening",			GetClientListening},
   370 	{"SetListenOverride",			SetClientListening},
   371 	{"GetListenOverride",			GetClientListening},
   372 	{"IsClientMuted",				IsClientMuted},
   373 	{NULL,							NULL},
   374 };