extensions/sdktools/voice.cpp
author Phong Dang <teame06@gmail.com>
Wed Aug 01 18:22:53 2007 +0000 (2007-08-01)
changeset 1245 bb0611ab4bed
parent 1235 88bb46f9731e
child 1247 26e4ba1e6cd2
permissions -rw-r--r--
Updated license header.
     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 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  *
    31  */
    32 
    33 #include <extension.h>
    34 
    35 #define SPEAK_NORMAL		0
    36 #define SPEAK_MUTED			1
    37 #define SPEAK_ALL			2
    38 #define SPEAK_LISTENALL		4
    39 
    40 size_t g_VoiceFlags[65];
    41 size_t g_VoiceFlagsCount = 0;
    42 
    43 SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool);
    44 
    45 bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen)
    46 {
    47 	if (g_VoiceFlags[iSender] & SPEAK_MUTED) 
    48 	{
    49 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false));
    50 	}
    51 
    52 	if (g_VoiceFlags[iSender] & SPEAK_ALL)
    53 	{
    54 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
    55 	}
    56 
    57 	if (g_VoiceFlags[iReceiver] & SPEAK_LISTENALL)
    58 	{
    59 		RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true));
    60 	}
    61 
    62 	RETURN_META_VALUE(MRES_IGNORED, bListen);
    63 }
    64 
    65 void SDKTools::OnClientDisconnecting(int client)
    66 {
    67 	if (g_VoiceFlags[client])
    68 	{	
    69 		g_VoiceFlags[client] = 0;
    70 		if (!--g_VoiceFlagsCount)
    71 		{
    72 			SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
    73 		}
    74 	}
    75 }
    76 
    77 static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
    78 {
    79 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
    80 	if (player == NULL)
    81 	{
    82 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
    83 	} else if (!player->IsConnected()) {
    84 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
    85 	}
    86 
    87 	if (!params[2] && g_VoiceFlags[params[1]])
    88 	{
    89 		if (!--g_VoiceFlagsCount)
    90 		{
    91 			SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
    92 		}
    93 	} else if (!g_VoiceFlags[params[1]] && params[2]) {
    94 
    95 		if (!g_VoiceFlagsCount++)
    96 		{
    97 			SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false);
    98 		}
    99 	}
   100 
   101 	g_VoiceFlags[params[1]] = params[2];
   102 
   103 	return 1;
   104 }
   105 
   106 static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *params)
   107 {
   108 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   109 	if (player == NULL)
   110 	{
   111 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   112 	} else if (!player->IsConnected()) {
   113 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   114 	}
   115 
   116 	return g_VoiceFlags[params[1]];
   117 }
   118 
   119 static cell_t SetClientListening(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 	bool bListen = !params[3] ? false : true;
   130 
   131 	return voiceserver->SetClientListening(params[1], params[2], bListen) ? 1 : 0;
   132 }
   133 
   134 static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params)
   135 {
   136 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   137 	if (player == NULL)
   138 	{
   139 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   140 	} else if (!player->IsConnected()) {
   141 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   142 	}
   143 
   144 	return voiceserver->GetClientListening(params[1], params[2]) ? 1 : 0;
   145 }
   146 
   147 sp_nativeinfo_t g_VoiceNatives[] = 
   148 {
   149 	{"SetClientListeningFlags",		SetClientListeningFlags},
   150 	{"GetClientListeningFlags",		GetClientListeningFlags},
   151 	{"SetClientListening",			SetClientListening},
   152 	{"GetClientListening",			GetClientListening},
   153 	{NULL,							NULL},
   154 };