extensions/sdktools/vsound.cpp
branchrefac-jit
changeset 2168 c24a6fa957db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/extensions/sdktools/vsound.cpp	Sun Jul 06 04:49:55 2008 +0000
     1.3 @@ -0,0 +1,789 @@
     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 "vsound.h"
    1.36 +#include <IForwardSys.h>
    1.37 +
    1.38 +SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, int, const Vector &, const char *, float, soundlevel_t, int, int, float);
    1.39 +SH_DECL_HOOK14_void(IEngineSound, EmitSound, SH_NOATTRIB, 0, IRecipientFilter &, int, int, const char *, float, float, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
    1.40 +SH_DECL_HOOK14_void(IEngineSound, EmitSound, SH_NOATTRIB, 1, IRecipientFilter &, int, int, const char *, float, soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
    1.41 +
    1.42 +bool g_InSoundHook = false;
    1.43 +
    1.44 +/***************************
    1.45 +*                          *
    1.46 +* Sound Related Hook Class *
    1.47 +*                          *
    1.48 +****************************/
    1.49 +
    1.50 +size_t SoundHooks::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
    1.51 +{
    1.52 +	size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
    1.53 +
    1.54 +	for (size_t i=0; i<size; i++)
    1.55 +	{
    1.56 +		pl_array[i] = pFilter->GetRecipientIndex(i);
    1.57 +	}
    1.58 +
    1.59 +	return size;
    1.60 +}
    1.61 +
    1.62 +void SoundHooks::_IncRefCounter(int type)
    1.63 +{
    1.64 +	if (type == NORMAL_SOUND_HOOK)
    1.65 +	{
    1.66 +		if (m_NormalCount++ == 0)
    1.67 +		{
    1.68 +			SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
    1.69 +			SH_ADD_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
    1.70 +		}
    1.71 +	}
    1.72 +	else if (type == AMBIENT_SOUND_HOOK)
    1.73 +	{
    1.74 +		if (m_AmbientCount++ == 0)
    1.75 +		{
    1.76 +			SH_ADD_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
    1.77 +		}
    1.78 +	}
    1.79 +}
    1.80 +
    1.81 +void SoundHooks::_DecRefCounter(int type)
    1.82 +{
    1.83 +	if (type == NORMAL_SOUND_HOOK)
    1.84 +	{
    1.85 +		if (--m_NormalCount == 0)
    1.86 +		{
    1.87 +			SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
    1.88 +			SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
    1.89 +		}
    1.90 +	}
    1.91 +	else if (type == AMBIENT_SOUND_HOOK)
    1.92 +	{
    1.93 +		if (--m_AmbientCount == 0)
    1.94 +		{
    1.95 +			SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
    1.96 +		}
    1.97 +	}
    1.98 +}
    1.99 +
   1.100 +void SoundHooks::Initialize()
   1.101 +{
   1.102 +	plsys->AddPluginsListener(this);
   1.103 +}
   1.104 +
   1.105 +void SoundHooks::Shutdown()
   1.106 +{
   1.107 +	plsys->RemovePluginsListener(this);
   1.108 +	if (m_NormalCount)
   1.109 +	{
   1.110 +		SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound, false);
   1.111 +		SH_REMOVE_HOOK_MEMFUNC(IEngineSound, EmitSound, engsound, this, &SoundHooks::OnEmitSound2, false);
   1.112 +	}
   1.113 +	if (m_AmbientCount)
   1.114 +	{
   1.115 +		SH_REMOVE_HOOK_MEMFUNC(IVEngineServer, EmitAmbientSound, engine, this, &SoundHooks::OnEmitAmbientSound, false);
   1.116 +	}
   1.117 +}
   1.118 +
   1.119 +void SoundHooks::OnPluginUnloaded(IPlugin *plugin)
   1.120 +{
   1.121 +	SoundHookIter iter;
   1.122 +	IPluginContext *pContext = plugin->GetBaseContext();
   1.123 +
   1.124 +	if (m_AmbientCount)
   1.125 +	{
   1.126 +		for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); )
   1.127 +		{
   1.128 +			if ((*iter)->GetParentContext() == pContext)
   1.129 +			{
   1.130 +				iter = m_AmbientFuncs.erase(iter);
   1.131 +				_DecRefCounter(AMBIENT_SOUND_HOOK);
   1.132 +			}
   1.133 +			else
   1.134 +			{
   1.135 +				iter++;
   1.136 +			}
   1.137 +		}
   1.138 +	}
   1.139 +	if (m_NormalCount)
   1.140 +	{
   1.141 +		for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); )
   1.142 +		{
   1.143 +			if ((*iter)->GetParentContext() == pContext)
   1.144 +			{
   1.145 +				iter = m_NormalFuncs.erase(iter);
   1.146 +				_DecRefCounter(NORMAL_SOUND_HOOK);
   1.147 +			}
   1.148 +			else
   1.149 +			{
   1.150 +				iter++;
   1.151 +			}
   1.152 +		}
   1.153 +	}
   1.154 +}
   1.155 +
   1.156 +void SoundHooks::AddHook(int type, IPluginFunction *pFunc)
   1.157 +{
   1.158 +	if (type == NORMAL_SOUND_HOOK)
   1.159 +	{
   1.160 +		m_NormalFuncs.push_back(pFunc);
   1.161 +		_IncRefCounter(NORMAL_SOUND_HOOK);
   1.162 +	}
   1.163 +	else if (type == AMBIENT_SOUND_HOOK)
   1.164 +	{
   1.165 +		m_AmbientFuncs.push_back(pFunc);
   1.166 +		_IncRefCounter(AMBIENT_SOUND_HOOK);
   1.167 +	}
   1.168 +}
   1.169 +
   1.170 +bool SoundHooks::RemoveHook(int type, IPluginFunction *pFunc)
   1.171 +{
   1.172 +	SoundHookIter iter;
   1.173 +	if (type == NORMAL_SOUND_HOOK)
   1.174 +	{
   1.175 +		if ((iter=m_NormalFuncs.find(pFunc)) != m_NormalFuncs.end())
   1.176 +		{
   1.177 +			m_NormalFuncs.erase(iter);
   1.178 +			_DecRefCounter(NORMAL_SOUND_HOOK);
   1.179 +			return true;
   1.180 +		}
   1.181 +		else
   1.182 +		{
   1.183 +			return false;
   1.184 +		}
   1.185 +	}
   1.186 +	else if (type == AMBIENT_SOUND_HOOK)
   1.187 +	{
   1.188 +		if ((iter=m_AmbientFuncs.find(pFunc)) != m_AmbientFuncs.end())
   1.189 +		{
   1.190 +			m_AmbientFuncs.erase(iter);
   1.191 +			_DecRefCounter(AMBIENT_SOUND_HOOK);
   1.192 +			return true;
   1.193 +		}
   1.194 +		else
   1.195 +		{
   1.196 +			return false;
   1.197 +		}
   1.198 +	}
   1.199 +
   1.200 +	return false;
   1.201 +}
   1.202 +
   1.203 +void SoundHooks::OnEmitAmbientSound(int entindex, const Vector &pos, const char *samp, float vol, 
   1.204 +									soundlevel_t soundlevel, int fFlags, int pitch, float delay)
   1.205 +{
   1.206 +	SoundHookIter iter;
   1.207 +	IPluginFunction *pFunc;
   1.208 +	cell_t vec[3] = {sp_ftoc(pos.x), sp_ftoc(pos.y), sp_ftoc(pos.z)};
   1.209 +	cell_t res = static_cast<ResultType>(Pl_Continue);
   1.210 +	char buffer[PLATFORM_MAX_PATH];
   1.211 +	strcpy(buffer, samp);
   1.212 +
   1.213 +	for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); iter++)
   1.214 +	{
   1.215 +		pFunc = (*iter);
   1.216 +		pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   1.217 +		pFunc->PushCellByRef(&entindex);
   1.218 +		pFunc->PushFloatByRef(&vol);
   1.219 +		pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&soundlevel));
   1.220 +		pFunc->PushCellByRef(&pitch);
   1.221 +		pFunc->PushArray(vec, 3, SM_PARAM_COPYBACK);
   1.222 +		pFunc->PushCellByRef(&fFlags);
   1.223 +		pFunc->PushFloatByRef(&delay);
   1.224 +		g_InSoundHook = true;
   1.225 +		pFunc->Execute(&res);
   1.226 +		g_InSoundHook = false;
   1.227 +
   1.228 +		switch (res)
   1.229 +		{
   1.230 +		case Pl_Handled:
   1.231 +		case Pl_Stop:
   1.232 +			{
   1.233 +				RETURN_META(MRES_SUPERCEDE);
   1.234 +			}
   1.235 +		case Pl_Changed:
   1.236 +			{
   1.237 +				Vector vec2;
   1.238 +				vec2.x = sp_ctof(vec[0]);
   1.239 +				vec2.y = sp_ctof(vec[1]);
   1.240 +				vec2.z = sp_ctof(vec[2]);
   1.241 +				RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::EmitAmbientSound, 
   1.242 +										(entindex, vec2, buffer, vol, soundlevel, fFlags, pitch, delay));
   1.243 +			}
   1.244 +		}
   1.245 +	}
   1.246 +}
   1.247 +
   1.248 +void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample, 
   1.249 +							 float flVolume, soundlevel_t iSoundlevel, int iFlags, int iPitch, const Vector *pOrigin, 
   1.250 +							 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions, 
   1.251 +							 float soundtime, int speakerentity)
   1.252 +{
   1.253 +	SoundHookIter iter;
   1.254 +	IPluginFunction *pFunc;
   1.255 +	cell_t res = static_cast<ResultType>(Pl_Continue);
   1.256 +	char buffer[PLATFORM_MAX_PATH];
   1.257 +	strcpy(buffer, pSample);
   1.258 +
   1.259 +	for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
   1.260 +	{
   1.261 +		int players[64], size;
   1.262 +		size = _FillInPlayers(players, &filter);
   1.263 +		pFunc = (*iter);
   1.264 +
   1.265 +		pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
   1.266 +		pFunc->PushCellByRef(&size);
   1.267 +		pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   1.268 +		pFunc->PushCellByRef(&iEntIndex);
   1.269 +		pFunc->PushCellByRef(&iChannel);
   1.270 +		pFunc->PushFloatByRef(&flVolume);
   1.271 +		pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&iSoundlevel));
   1.272 +		pFunc->PushCellByRef(&iPitch);
   1.273 +		pFunc->PushCellByRef(&iFlags);
   1.274 +		g_InSoundHook = true;
   1.275 +		pFunc->Execute(&res);
   1.276 +		g_InSoundHook = false;
   1.277 +
   1.278 +		switch (res)
   1.279 +		{
   1.280 +		case Pl_Handled:
   1.281 +		case Pl_Stop:
   1.282 +			{
   1.283 +				RETURN_META(MRES_SUPERCEDE);
   1.284 +			}
   1.285 +		case Pl_Changed:
   1.286 +			{
   1.287 +				CellRecipientFilter crf;
   1.288 +				crf.Initialize(players, size);
   1.289 +				RETURN_META_NEWPARAMS(
   1.290 +					MRES_IGNORED,
   1.291 +					static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, soundlevel_t, 
   1.292 +					int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound), 
   1.293 +					(crf, iEntIndex, iChannel, buffer, flVolume, iSoundlevel, iFlags, iPitch, pOrigin, 
   1.294 +					pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
   1.295 +					);
   1.296 +			}
   1.297 +		}
   1.298 +	}
   1.299 +}
   1.300 +
   1.301 +void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample, 
   1.302 +							 float flVolume, float flAttenuation, int iFlags, int iPitch, const Vector *pOrigin, 
   1.303 +							 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions, 
   1.304 +							 float soundtime, int speakerentity)
   1.305 +{
   1.306 +	SoundHookIter iter;
   1.307 +	IPluginFunction *pFunc;
   1.308 +	cell_t res = static_cast<ResultType>(Pl_Continue);
   1.309 +	cell_t sndlevel = static_cast<cell_t>(ATTN_TO_SNDLVL(flAttenuation));
   1.310 +	char buffer[PLATFORM_MAX_PATH];
   1.311 +	strcpy(buffer, pSample);
   1.312 +
   1.313 +	for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
   1.314 +	{
   1.315 +		int players[64], size;
   1.316 +		size = _FillInPlayers(players, &filter);
   1.317 +		pFunc = (*iter);
   1.318 +
   1.319 +		pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
   1.320 +		pFunc->PushCellByRef(&size);
   1.321 +		pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
   1.322 +		pFunc->PushCellByRef(&iEntIndex);
   1.323 +		pFunc->PushCellByRef(&iChannel);
   1.324 +		pFunc->PushFloatByRef(&flVolume);
   1.325 +		pFunc->PushCellByRef(&sndlevel);
   1.326 +		pFunc->PushCellByRef(&iPitch);
   1.327 +		pFunc->PushCellByRef(&iFlags);
   1.328 +		g_InSoundHook = true;
   1.329 +		pFunc->Execute(&res);
   1.330 +		g_InSoundHook = false;
   1.331 +
   1.332 +		switch (res)
   1.333 +		{
   1.334 +		case Pl_Handled:
   1.335 +		case Pl_Stop:
   1.336 +			{
   1.337 +				RETURN_META(MRES_SUPERCEDE);
   1.338 +			}
   1.339 +		case Pl_Changed:
   1.340 +			{
   1.341 +				CellRecipientFilter crf;
   1.342 +				crf.Initialize(players, size);
   1.343 +				RETURN_META_NEWPARAMS(
   1.344 +					MRES_IGNORED,
   1.345 +					static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, float, 
   1.346 +					int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound), 
   1.347 +					(crf, iEntIndex, iChannel, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)), 
   1.348 +					iFlags, iPitch, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
   1.349 +					);
   1.350 +			}
   1.351 +		}
   1.352 +	}
   1.353 +}
   1.354 +
   1.355 +/************************
   1.356 +*                       *
   1.357 +* Sound Related Natives *
   1.358 +*                       *
   1.359 +*************************/
   1.360 +
   1.361 +SoundHooks s_SoundHooks;
   1.362 +
   1.363 +static cell_t PrefetchSound(IPluginContext *pContext, const cell_t *params)
   1.364 +{
   1.365 +	char *name;
   1.366 +	pContext->LocalToString(params[1], &name);
   1.367 +
   1.368 +	engsound->PrefetchSound(name);
   1.369 +
   1.370 +	return 1;
   1.371 +}
   1.372 +
   1.373 +static cell_t GetSoundDuration(IPluginContext *pContext, const cell_t *params)
   1.374 +{
   1.375 +	char *name;
   1.376 +	pContext->LocalToString(params[1], &name);
   1.377 +
   1.378 +	return sp_ftoc(engsound->GetSoundDuration(name));
   1.379 +}
   1.380 +
   1.381 +static cell_t EmitAmbientSound(IPluginContext *pContext, const cell_t *params)
   1.382 +{
   1.383 +	cell_t entity;
   1.384 +	Vector pos;
   1.385 +	char *name;
   1.386 +	float vol, delay;
   1.387 +	int pitch, flags, level;
   1.388 +
   1.389 +	entity = params[3];
   1.390 +
   1.391 +	cell_t *addr;
   1.392 +	pContext->LocalToPhysAddr(params[2], &addr);
   1.393 +	pos.x = sp_ctof(addr[0]);
   1.394 +	pos.y = sp_ctof(addr[1]);
   1.395 +	pos.z = sp_ctof(addr[2]);
   1.396 +
   1.397 +	pContext->LocalToString(params[1], &name);
   1.398 +
   1.399 +	vol = sp_ctof(params[6]);
   1.400 +	level = params[4];
   1.401 +	flags = params[5];
   1.402 +	pitch = params[7];
   1.403 +	delay = sp_ctof(params[8]);
   1.404 +
   1.405 +	if (g_InSoundHook)
   1.406 +	{
   1.407 +		ENGINE_CALL(EmitAmbientSound)(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
   1.408 +	}
   1.409 +	else
   1.410 +	{
   1.411 +		engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
   1.412 +	}
   1.413 +
   1.414 +	return 1;
   1.415 +}
   1.416 +
   1.417 +static cell_t FadeClientVolume(IPluginContext *pContext, const cell_t *params)
   1.418 +{
   1.419 +	int client = params[1];
   1.420 +	if (client < 1 || client > playerhelpers->GetMaxClients())
   1.421 +	{
   1.422 +		return pContext->ThrowNativeError("Client index %d is not valid", client);
   1.423 +	}
   1.424 +
   1.425 +	IGamePlayer *player = playerhelpers->GetGamePlayer(client);
   1.426 +	if (!player->IsInGame())
   1.427 +	{
   1.428 +		return pContext->ThrowNativeError("Client index %d is not in game", client);
   1.429 +	}
   1.430 +
   1.431 +	engine->FadeClientVolume(player->GetEdict(),
   1.432 +		sp_ctof(params[2]),
   1.433 +		sp_ctof(params[3]),
   1.434 +		sp_ctof(params[4]),
   1.435 +		sp_ctof(params[5]));
   1.436 +
   1.437 +	return 1;
   1.438 +}
   1.439 +
   1.440 +static cell_t StopSound(IPluginContext *pContext, const cell_t *params)
   1.441 +{
   1.442 +	int entity = params[1];
   1.443 +	int channel = params[2];
   1.444 +
   1.445 +	char *name;
   1.446 +	pContext->LocalToString(params[3], &name);
   1.447 +
   1.448 +	engsound->StopSound(entity, channel, name);
   1.449 +
   1.450 +	return 1;
   1.451 +}
   1.452 +
   1.453 +static cell_t EmitSound(IPluginContext *pContext, const cell_t *params)
   1.454 +{
   1.455 +	cell_t *addr, *cl_array;
   1.456 +	CellRecipientFilter crf;
   1.457 +	unsigned int numClients;
   1.458 +	int client;
   1.459 +	IGamePlayer *pPlayer = NULL;
   1.460 +
   1.461 +	pContext->LocalToPhysAddr(params[1], &cl_array);
   1.462 +	numClients = params[2];
   1.463 +
   1.464 +	/* Client validation */
   1.465 +	for (unsigned int i = 0; i < numClients; i++)
   1.466 +	{
   1.467 +		client = cl_array[i];
   1.468 +		pPlayer = playerhelpers->GetGamePlayer(client);
   1.469 +
   1.470 +		if (!pPlayer)
   1.471 +		{
   1.472 +			return pContext->ThrowNativeError("Client index %d is invalid", client);
   1.473 +		} else if (!pPlayer->IsInGame()) {
   1.474 +			return pContext->ThrowNativeError("Client %d is not connected", client);
   1.475 +		}
   1.476 +	}
   1.477 +
   1.478 +	crf.Initialize(cl_array, numClients);
   1.479 +
   1.480 +	char *sample;
   1.481 +	pContext->LocalToString(params[3], &sample);
   1.482 +	
   1.483 +	int entity = params[4];
   1.484 +	int channel = params[5];
   1.485 +	int level = params[6];
   1.486 +	int flags = params[7];
   1.487 +	float vol = sp_ctof(params[8]);
   1.488 +	int pitch = params[9];
   1.489 +	int speakerentity = params[10];
   1.490 +
   1.491 +	Vector *pOrigin = NULL, origin;
   1.492 +	Vector *pDir = NULL, dir;
   1.493 +
   1.494 +	pContext->LocalToPhysAddr(params[11], &addr);
   1.495 +	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   1.496 +	{
   1.497 +		pOrigin = &origin;
   1.498 +		origin.x = sp_ctof(addr[0]);
   1.499 +		origin.y = sp_ctof(addr[1]);
   1.500 +		origin.z = sp_ctof(addr[2]);
   1.501 +	}
   1.502 +
   1.503 +	pContext->LocalToPhysAddr(params[12], &addr);
   1.504 +	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   1.505 +	{
   1.506 +		pDir = &dir;
   1.507 +		dir.x = sp_ctof(addr[0]);
   1.508 +		dir.y = sp_ctof(addr[1]);
   1.509 +		dir.z = sp_ctof(addr[2]);
   1.510 +	}
   1.511 +
   1.512 +	bool updatePos = params[13] ? true : false;
   1.513 +	float soundtime = sp_ctof(params[14]);
   1.514 +
   1.515 +	CUtlVector<Vector> *pOrigVec = NULL;
   1.516 +	CUtlVector<Vector> origvec;
   1.517 +	if (params[0] > 14)
   1.518 +	{
   1.519 +		pOrigVec = &origvec;
   1.520 +		for (cell_t i = 15; i <= params[0]; i++)
   1.521 +		{
   1.522 +			Vector vec;
   1.523 +			pContext->LocalToPhysAddr(params[i], &addr);
   1.524 +			vec.x = sp_ctof(addr[0]);
   1.525 +			vec.y = sp_ctof(addr[1]);
   1.526 +			vec.z = sp_ctof(addr[2]);
   1.527 +			origvec.AddToTail(vec);
   1.528 +		}
   1.529 +	}
   1.530 +
   1.531 +	/* If we're going to a "local player" and this is a dedicated server,
   1.532 +	 * intelligently redirect each sound.
   1.533 +	 */
   1.534 +
   1.535 +	if (entity == -2 && engine->IsDedicatedServer())
   1.536 +	{
   1.537 +		for (unsigned int i = 0; i < numClients; i++)
   1.538 +		{
   1.539 +			cell_t player[1];
   1.540 +			player[0] = cl_array[i];
   1.541 +			crf.Reset();
   1.542 +			crf.Initialize(player, 1);
   1.543 +			if (g_InSoundHook)
   1.544 +			{
   1.545 +				SH_CALL(enginesoundPatch, 
   1.546 +					static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, 
   1.547 +					soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
   1.548 +					(&IEngineSound::EmitSound))
   1.549 +					(crf, 
   1.550 +					player[0], 
   1.551 +					channel, 
   1.552 +					sample, 
   1.553 +					vol, 
   1.554 +					(soundlevel_t)level, 
   1.555 +					flags, 
   1.556 +					pitch, 
   1.557 +					pOrigin,
   1.558 +					pDir,
   1.559 +					pOrigVec,
   1.560 +					updatePos,
   1.561 +					soundtime,
   1.562 +					speakerentity);
   1.563 +			}
   1.564 +			else
   1.565 +			{
   1.566 +				engsound->EmitSound(crf, 
   1.567 +					player[0], 
   1.568 +					channel, 
   1.569 +					sample, 
   1.570 +					vol, 
   1.571 +					(soundlevel_t)level, 
   1.572 +					flags, 
   1.573 +					pitch, 
   1.574 +					pOrigin,
   1.575 +					pDir,
   1.576 +					pOrigVec,
   1.577 +					updatePos,
   1.578 +					soundtime,
   1.579 +					speakerentity);
   1.580 +			}
   1.581 +		}
   1.582 +	} else {
   1.583 +		if (g_InSoundHook)
   1.584 +		{
   1.585 +			SH_CALL(enginesoundPatch, 
   1.586 +				static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, 
   1.587 +				soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
   1.588 +				(&IEngineSound::EmitSound))
   1.589 +				(crf, 
   1.590 +				entity, 
   1.591 +				channel, 
   1.592 +				sample, 
   1.593 +				vol, 
   1.594 +				(soundlevel_t)level, 
   1.595 +				flags, 
   1.596 +				pitch, 
   1.597 +				pOrigin,
   1.598 +				pDir,
   1.599 +				pOrigVec,
   1.600 +				updatePos,
   1.601 +				soundtime,
   1.602 +				speakerentity);
   1.603 +		}
   1.604 +		else
   1.605 +		{
   1.606 +			engsound->EmitSound(crf, 
   1.607 +				entity, 
   1.608 +				channel, 
   1.609 +				sample, 
   1.610 +				vol, 
   1.611 +				(soundlevel_t)level, 
   1.612 +				flags, 
   1.613 +				pitch, 
   1.614 +				pOrigin,
   1.615 +				pDir,
   1.616 +				pOrigVec,
   1.617 +				updatePos,
   1.618 +				soundtime,
   1.619 +				speakerentity);
   1.620 +		}
   1.621 +	}
   1.622 +
   1.623 +	return 1;
   1.624 +}
   1.625 +
   1.626 +static cell_t EmitSentence(IPluginContext *pContext, const cell_t *params)
   1.627 +{
   1.628 +	cell_t *addr;
   1.629 +	CellRecipientFilter crf;
   1.630 +	unsigned int numClients;
   1.631 +	int client;
   1.632 +	IGamePlayer *pPlayer = NULL;
   1.633 +
   1.634 +	pContext->LocalToPhysAddr(params[1], &addr);
   1.635 +	numClients = params[2];
   1.636 +
   1.637 +	/* Client validation */
   1.638 +	for (unsigned int i = 0; i < numClients; i++)
   1.639 +	{
   1.640 +		client = addr[i];
   1.641 +		pPlayer = playerhelpers->GetGamePlayer(client);
   1.642 +
   1.643 +		if (!pPlayer)
   1.644 +		{
   1.645 +			return pContext->ThrowNativeError("Client index %d is invalid", client);
   1.646 +		} else if (!pPlayer->IsInGame()) {
   1.647 +			return pContext->ThrowNativeError("Client %d is not connected", client);
   1.648 +		}
   1.649 +	}
   1.650 +
   1.651 +	crf.Initialize(addr, numClients);
   1.652 +
   1.653 +	int sentence = params[3];
   1.654 +	int entity = params[4];
   1.655 +	int channel = params[5];
   1.656 +	int level = params[6];
   1.657 +	int flags = params[7];
   1.658 +	float vol = sp_ctof(params[8]);
   1.659 +	int pitch = params[9];
   1.660 +	int speakerentity = params[10];
   1.661 +
   1.662 +	Vector *pOrigin = NULL, origin;
   1.663 +	Vector *pDir = NULL, dir;
   1.664 +
   1.665 +	pContext->LocalToPhysAddr(params[11], &addr);
   1.666 +	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   1.667 +	{
   1.668 +		pOrigin = &origin;
   1.669 +		origin.x = sp_ctof(addr[0]);
   1.670 +		origin.y = sp_ctof(addr[1]);
   1.671 +		origin.z = sp_ctof(addr[2]);
   1.672 +	}
   1.673 +
   1.674 +	pContext->LocalToPhysAddr(params[12], &addr);
   1.675 +	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   1.676 +	{
   1.677 +		pDir = &dir;
   1.678 +		dir.x = sp_ctof(addr[0]);
   1.679 +		dir.y = sp_ctof(addr[1]);
   1.680 +		dir.z = sp_ctof(addr[2]);
   1.681 +	}
   1.682 +
   1.683 +	bool updatePos = params[13] ? true : false;
   1.684 +	float soundtime = sp_ctof(params[14]);
   1.685 +
   1.686 +	CUtlVector<Vector> *pOrigVec = NULL;
   1.687 +	CUtlVector<Vector> origvec;
   1.688 +	if (params[0] > 14)
   1.689 +	{
   1.690 +		pOrigVec = &origvec;
   1.691 +		for (cell_t i = 15; i <= params[0]; i++)
   1.692 +		{
   1.693 +			Vector vec;
   1.694 +			pContext->LocalToPhysAddr(params[i], &addr);
   1.695 +			vec.x = sp_ctof(addr[0]);
   1.696 +			vec.y = sp_ctof(addr[1]);
   1.697 +			vec.z = sp_ctof(addr[2]);
   1.698 +			origvec.AddToTail(vec);
   1.699 +		}
   1.700 +	}
   1.701 +
   1.702 +	engsound->EmitSentenceByIndex(crf, 
   1.703 +		entity, 
   1.704 +		channel, 
   1.705 +		sentence, 
   1.706 +		vol, 
   1.707 +		(soundlevel_t)level, 
   1.708 +		flags, 
   1.709 +		pitch, 
   1.710 +		pOrigin,
   1.711 +		pDir,
   1.712 +		pOrigVec,
   1.713 +		updatePos,
   1.714 +		soundtime,
   1.715 +		speakerentity);
   1.716 +
   1.717 +	return 1;
   1.718 +}
   1.719 +
   1.720 +static cell_t smn_AddAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
   1.721 +{
   1.722 +	IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
   1.723 +	if (!pFunc)
   1.724 +	{
   1.725 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
   1.726 +	}
   1.727 +
   1.728 +	s_SoundHooks.AddHook(AMBIENT_SOUND_HOOK, pFunc);
   1.729 +
   1.730 +	return 1;
   1.731 +}
   1.732 +
   1.733 +static cell_t smn_AddNormalSoundHook(IPluginContext *pContext, const cell_t *params)
   1.734 +{
   1.735 +	IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
   1.736 +	if (!pFunc)
   1.737 +	{
   1.738 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
   1.739 +	}
   1.740 +
   1.741 +	s_SoundHooks.AddHook(NORMAL_SOUND_HOOK, pFunc);
   1.742 +
   1.743 +	return 1;
   1.744 +}
   1.745 +
   1.746 +static cell_t smn_RemoveAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
   1.747 +{
   1.748 +	IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
   1.749 +	if (!pFunc)
   1.750 +	{
   1.751 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
   1.752 +	}
   1.753 +
   1.754 +	if (!s_SoundHooks.RemoveHook(AMBIENT_SOUND_HOOK, pFunc))
   1.755 +	{
   1.756 +		return pContext->ThrowNativeError("Invalid hooked function");
   1.757 +	}
   1.758 +
   1.759 +	return 1;
   1.760 +}
   1.761 +
   1.762 +static cell_t smn_RemoveNormalSoundHook(IPluginContext *pContext, const cell_t *params)
   1.763 +{
   1.764 +	IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
   1.765 +	if (!pFunc)
   1.766 +	{
   1.767 +		return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
   1.768 +	}
   1.769 +
   1.770 +	if (!s_SoundHooks.RemoveHook(NORMAL_SOUND_HOOK, pFunc))
   1.771 +	{
   1.772 +		return pContext->ThrowNativeError("Invalid hooked function");
   1.773 +	}
   1.774 +
   1.775 +	return 1;
   1.776 +}
   1.777 +
   1.778 +sp_nativeinfo_t g_SoundNatives[] = 
   1.779 +{
   1.780 +	{"EmitAmbientSound",		EmitAmbientSound},
   1.781 +	{"EmitSentence",			EmitSentence},
   1.782 +	{"EmitSound",				EmitSound},
   1.783 +	{"FadeClientVolume",		FadeClientVolume},
   1.784 +	{"GetSoundDuration",		GetSoundDuration},
   1.785 +	{"PrefetchSound",			PrefetchSound},
   1.786 +	{"StopSound",				StopSound},
   1.787 +	{"AddAmbientSoundHook",		smn_AddAmbientSoundHook},
   1.788 +	{"AddNormalSoundHook",		smn_AddNormalSoundHook},
   1.789 +	{"RemoveAmbientSoundHook",	smn_RemoveAmbientSoundHook},
   1.790 +	{"RemoveNormalSoundHook",	smn_RemoveNormalSoundHook},
   1.791 +	{NULL,						NULL},
   1.792 +};