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