extensions/sdktools/voice.cpp
author Michael McKoy <ferret99gt@gmail.com>
Sun Sep 23 21:01:07 2007 +0000 (2007-09-23)
changeset 1468 b90faced4648
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
Added VOICE_TEAM and VOICE_LISTENTEAM flags to the SDKTools voice functionality.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2007 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 
    34 #define SPEAK_NORMAL		0
    35 #define SPEAK_MUTED			1
    36 #define SPEAK_ALL			2
    37 #define SPEAK_LISTENALL		4
    38 #define SPEAK_TEAM			8
    39 #define SPEAK_LISTENTEAM	16
    40 
    41 size_t g_VoiceFlags[65];
    42 size_t g_VoiceFlagsCount = 0;
    43 
    44 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
    45 
    46 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
    47 {
    48 	if (g_VoiceFlags[iSender] & SPEAK_MUTED)
    49 	{
    50 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
    51 	}
    52 
    53 	if ((g_VoiceFlags[iSender] & SPEAK_ALL) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL))
    54 	{
    55 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
    56 	}
    57 
    58 	if ((g_VoiceFlags[iSender] & SPEAK_TEAM) || (g_VoiceFlags[iReceiver] & SPEAK_LISTENTEAM))
    59 	{
    60 		IGamePlayer *pReceiver = playerhelpers->GetGamePlayer(iReceiver);
    61 		IGamePlayer *pSender = playerhelpers->GetGamePlayer(iSender);
    62 
    63 		if (pReceiver && pSender && pReceiver->IsInGame() && pSender->IsInGame())
    64 		{
    65 			IPlayerInfo *pRInfo = pReceiver->GetPlayerInfo();
    66 			IPlayerInfo *pSInfo = pSender->GetPlayerInfo();
    67 
    68 			if (pRInfo && pSInfo && pRInfo->GetTeamIndex() == pSInfo->GetTeamIndex())
    69 			{
    70 				RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
    71 			}
    72 		}
    73 	}
    74 
    75 	RETURN_META_VALUE(MRES_IGNORED, bListen);
    76 }
    77 
    78 void SDKTools::OnClientDisconnecting(int client)
    79 {
    80 	if (g_VoiceFlags[client])
    81 	{
    82 		g_VoiceFlags[client] = 0;
    83 		if (!--g_VoiceFlagsCount)
    84 		{
    85 			SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
    86 		}
    87 	}
    88 }
    89 
    90 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
    91 {
    92 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    93 	if (player == NULL)
    94 	{
    95 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
    96 	} else if (!player->IsConnected()) {
    97 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
    98 	}
    99 
   100 	if (!params[2] && g_VoiceFlags[params[1]])
   101 	{
   102 		if (!--g_VoiceFlagsCount)
   103 		{
   104 			SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
   105 		}
   106 	} else if (!g_VoiceFlags[params[1]] && params[2]) {
   107 
   108 		if (!g_VoiceFlagsCount++)
   109 		{
   110 			SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
   111 		}
   112 	}
   113 
   114 	g_VoiceFlags[params[1]] = params[2];
   115 
   116 	return 1;
   117 }
   118 
   119 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
   120 {
   121 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   122 	if (player == NULL)
   123 	{
   124 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   125 	} else if (!player->IsConnected()) {
   126 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   127 	}
   128 
   129 	return g_VoiceFlags[params[1]];
   130 }
   131 
   132 static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params)
   133 {
   134 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   135 	if (player == NULL)
   136 	{
   137 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   138 	} else if (!player->IsConnected()) {
   139 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   140 	}
   141 
   142 	bool bListen = !params[3] ? false : true;
   143 
   144 	return voiceserver->SetClientListening(params[1], params[2], bListen) ? 1 : 0;
   145 }
   146 
   147 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
   148 {
   149 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   150 	if (player == NULL)
   151 	{
   152 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   153 	} else if (!player->IsConnected()) {
   154 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   155 	}
   156 
   157 	return voiceserver->GetClientListening(params[1], params[2]) ? 1 : 0;
   158 }
   159 
   160 sp_nativeinfo_t g_VoiceNatives[] =
   161 {
   162 	{"SetClientListeningFlags",		SetClientListeningFlags},
   163 	{"GetClientListeningFlags",		GetClientListeningFlags},
   164 	{"SetClientListening",			SetClientListening},
   165 	{"GetClientListening",			GetClientListening},
   166 	{NULL,							NULL},
   167 };