3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
33 #include <IForwardSys.h>
35 #if SOURCE_ENGINE == SE_DOTA
36 SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, CEntityIndex, const Vector &, const char *, float, soundlevel_t, int, int, float);
38 SH_DECL_HOOK8_void(IVEngineServer, EmitAmbientSound, SH_NOATTRIB, 0, int, const Vector &, const char *, float, soundlevel_t, int, int, float);
41 #if SOURCE_ENGINE >= SE_PORTAL2
42 SH_DECL_HOOK17(IEngineSound, EmitSound, SH_NOATTRIB, 0, int, IRecipientFilter &, int, int, const char *, unsigned int, const char *, float, float, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
43 SH_DECL_HOOK17(IEngineSound, EmitSound, SH_NOATTRIB, 1, int, IRecipientFilter &, int, int, const char *, unsigned int, const char *, float, soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
44 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
45 SH_DECL_HOOK15_void(IEngineSound, EmitSound, SH_NOATTRIB, 0, IRecipientFilter &, int, int, const char *, float, float, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
46 SH_DECL_HOOK15_void(IEngineSound, EmitSound, SH_NOATTRIB, 1, IRecipientFilter &, int, int, const char *, float, soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int);
48 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);
49 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);
52 bool g_InSoundHook = false;
54 /***************************
56 * Sound Related Hook Class *
58 ****************************/
60 cell_t SoundReferenceToIndex(cell_t ref)
62 if (ref == 0 || ref == -1 || ref == -2)
67 return gamehelpers->ReferenceToIndex(ref);
70 size_t SoundHooks::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
72 size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
74 for (size_t i=0; i<size; i++)
77 #if SOURCE_ENGINE == SE_DOTA
78 index = pFilter->GetRecipientIndex(i).Get();
80 index = pFilter->GetRecipientIndex(i);
88 void SoundHooks::_IncRefCounter(int type)
90 if (type == NORMAL_SOUND_HOOK)
92 if (m_NormalCount++ == 0)
94 SH_ADD_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound), false);
95 SH_ADD_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound2), false);
98 else if (type == AMBIENT_SOUND_HOOK)
100 if (m_AmbientCount++ == 0)
102 SH_ADD_HOOK(IVEngineServer, EmitAmbientSound, engine, SH_MEMBER(this, &SoundHooks::OnEmitAmbientSound), false);
107 void SoundHooks::_DecRefCounter(int type)
109 if (type == NORMAL_SOUND_HOOK)
111 if (--m_NormalCount == 0)
113 SH_REMOVE_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound), false);
114 SH_REMOVE_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound2), false);
117 else if (type == AMBIENT_SOUND_HOOK)
119 if (--m_AmbientCount == 0)
121 SH_REMOVE_HOOK(IVEngineServer, EmitAmbientSound, engine, SH_MEMBER(this, &SoundHooks::OnEmitAmbientSound), false);
126 void SoundHooks::Initialize()
128 plsys->AddPluginsListener(this);
131 void SoundHooks::Shutdown()
133 plsys->RemovePluginsListener(this);
136 SH_REMOVE_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound), false);
137 SH_REMOVE_HOOK(IEngineSound, EmitSound, engsound, SH_MEMBER(this, &SoundHooks::OnEmitSound2), false);
141 SH_REMOVE_HOOK(IVEngineServer, EmitAmbientSound, engine, SH_MEMBER(this, &SoundHooks::OnEmitAmbientSound), false);
145 void SoundHooks::OnPluginUnloaded(IPlugin *plugin)
148 IPluginContext *pContext = plugin->GetBaseContext();
152 for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); )
154 if ((*iter)->GetParentContext() == pContext)
156 iter = m_AmbientFuncs.erase(iter);
157 _DecRefCounter(AMBIENT_SOUND_HOOK);
167 for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); )
169 if ((*iter)->GetParentContext() == pContext)
171 iter = m_NormalFuncs.erase(iter);
172 _DecRefCounter(NORMAL_SOUND_HOOK);
182 void SoundHooks::AddHook(int type, IPluginFunction *pFunc)
184 if (type == NORMAL_SOUND_HOOK)
186 m_NormalFuncs.push_back(pFunc);
187 _IncRefCounter(NORMAL_SOUND_HOOK);
189 else if (type == AMBIENT_SOUND_HOOK)
191 m_AmbientFuncs.push_back(pFunc);
192 _IncRefCounter(AMBIENT_SOUND_HOOK);
196 bool SoundHooks::RemoveHook(int type, IPluginFunction *pFunc)
199 if (type == NORMAL_SOUND_HOOK)
201 if ((iter=m_NormalFuncs.find(pFunc)) != m_NormalFuncs.end())
203 m_NormalFuncs.erase(iter);
204 _DecRefCounter(NORMAL_SOUND_HOOK);
212 else if (type == AMBIENT_SOUND_HOOK)
214 if ((iter=m_AmbientFuncs.find(pFunc)) != m_AmbientFuncs.end())
216 m_AmbientFuncs.erase(iter);
217 _DecRefCounter(AMBIENT_SOUND_HOOK);
229 #if SOURCE_ENGINE == SE_DOTA
230 void SoundHooks::OnEmitAmbientSound(CEntityIndex index, const Vector &pos, const char *samp, float vol,
231 soundlevel_t soundlevel, int fFlags, int pitch, float delay)
233 int entindex = index.Get();
235 void SoundHooks::OnEmitAmbientSound(int entindex, const Vector &pos, const char *samp, float vol,
236 soundlevel_t soundlevel, int fFlags, int pitch, float delay)
240 IPluginFunction *pFunc;
241 cell_t vec[3] = {sp_ftoc(pos.x), sp_ftoc(pos.y), sp_ftoc(pos.z)};
242 cell_t res = static_cast<ResultType>(Pl_Continue);
243 char buffer[PLATFORM_MAX_PATH];
244 strcpy(buffer, samp);
246 for (iter=m_AmbientFuncs.begin(); iter!=m_AmbientFuncs.end(); iter++)
249 pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
250 pFunc->PushCellByRef(&entindex);
251 pFunc->PushFloatByRef(&vol);
252 pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&soundlevel));
253 pFunc->PushCellByRef(&pitch);
254 pFunc->PushArray(vec, 3, SM_PARAM_COPYBACK);
255 pFunc->PushCellByRef(&fFlags);
256 pFunc->PushFloatByRef(&delay);
257 g_InSoundHook = true;
258 pFunc->Execute(&res);
259 g_InSoundHook = false;
266 RETURN_META(MRES_SUPERCEDE);
271 vec2.x = sp_ctof(vec[0]);
272 vec2.y = sp_ctof(vec[1]);
273 vec2.z = sp_ctof(vec[2]);
274 #if SOURCE_ENGINE == SE_DOTA
275 RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::EmitAmbientSound,
276 (CEntityIndex(entindex), vec2, buffer, vol, soundlevel, fFlags, pitch, delay));
278 RETURN_META_NEWPARAMS(MRES_IGNORED, &IVEngineServer::EmitAmbientSound,
279 (entindex, vec2, buffer, vol, soundlevel, fFlags, pitch, delay));
286 #if SOURCE_ENGINE >= SE_PORTAL2
287 int SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSoundEntry, unsigned int nSoundEntryHash, const char *pSample,
288 float flVolume, soundlevel_t iSoundlevel, int nSeed, int iFlags, int iPitch, const Vector *pOrigin,
289 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
290 float soundtime, int speakerentity)
291 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
292 void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
293 float flVolume, soundlevel_t iSoundlevel, int iFlags, int iPitch, int iSpecialDSP, const Vector *pOrigin,
294 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
295 float soundtime, int speakerentity)
297 void SoundHooks::OnEmitSound(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
298 float flVolume, soundlevel_t iSoundlevel, int iFlags, int iPitch, const Vector *pOrigin,
299 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
300 float soundtime, int speakerentity)
304 IPluginFunction *pFunc;
305 cell_t res = static_cast<ResultType>(Pl_Continue);
306 char buffer[PLATFORM_MAX_PATH];
307 strcpy(buffer, pSample);
309 for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
311 int players[64], size;
312 size = _FillInPlayers(players, &filter);
315 pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
316 pFunc->PushCellByRef(&size);
317 pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
318 pFunc->PushCellByRef(&iEntIndex);
319 pFunc->PushCellByRef(&iChannel);
320 pFunc->PushFloatByRef(&flVolume);
321 pFunc->PushCellByRef(reinterpret_cast<cell_t *>(&iSoundlevel));
322 pFunc->PushCellByRef(&iPitch);
323 pFunc->PushCellByRef(&iFlags);
324 g_InSoundHook = true;
325 pFunc->Execute(&res);
326 g_InSoundHook = false;
333 #if SOURCE_ENGINE >= SE_PORTAL2
334 RETURN_META_VALUE(MRES_SUPERCEDE, -1);
336 RETURN_META(MRES_SUPERCEDE);
341 CellRecipientFilter crf;
342 crf.Initialize(players, size);
343 #if SOURCE_ENGINE >= SE_PORTAL2
344 RETURN_META_VALUE_NEWPARAMS(
347 static_cast<int (IEngineSound::*)(IRecipientFilter &, int, int, const char*, unsigned int, const char*, float, soundlevel_t,
348 int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
349 (crf, iEntIndex, iChannel, buffer, -1, buffer, flVolume, iSoundlevel, nSeed, iFlags, iPitch, pOrigin,
350 pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
352 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
353 RETURN_META_NEWPARAMS(
355 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, soundlevel_t,
356 int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
357 (crf, iEntIndex, iChannel, buffer, flVolume, iSoundlevel, iFlags, iPitch, iSpecialDSP, pOrigin,
358 pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
361 RETURN_META_NEWPARAMS(
363 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float, soundlevel_t,
364 int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
365 (crf, iEntIndex, iChannel, buffer, flVolume, iSoundlevel, iFlags, iPitch, pOrigin,
366 pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
373 #if SOURCE_ENGINE >= SE_PORTAL2
374 RETURN_META_VALUE(MRES_IGNORED, -1 );
378 #if SOURCE_ENGINE >= SE_PORTAL2
379 int SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSoundEntry, unsigned int nSoundEntryHash, const char *pSample,
380 float flVolume, float flAttenuation, int nSeed, int iFlags, int iPitch, const Vector *pOrigin,
381 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
382 float soundtime, int speakerentity)
383 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
384 void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
385 float flVolume, float flAttenuation, int iFlags, int iPitch, int iSpecialDSP, const Vector *pOrigin,
386 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
387 float soundtime, int speakerentity)
389 void SoundHooks::OnEmitSound2(IRecipientFilter &filter, int iEntIndex, int iChannel, const char *pSample,
390 float flVolume, float flAttenuation, int iFlags, int iPitch, const Vector *pOrigin,
391 const Vector *pDirection, CUtlVector<Vector> *pUtlVecOrigins, bool bUpdatePositions,
392 float soundtime, int speakerentity)
396 IPluginFunction *pFunc;
397 cell_t res = static_cast<ResultType>(Pl_Continue);
398 cell_t sndlevel = static_cast<cell_t>(ATTN_TO_SNDLVL(flAttenuation));
399 char buffer[PLATFORM_MAX_PATH];
400 strcpy(buffer, pSample);
402 for (iter=m_NormalFuncs.begin(); iter!=m_NormalFuncs.end(); iter++)
404 int players[64], size;
405 size = _FillInPlayers(players, &filter);
408 pFunc->PushArray(players, 64, SM_PARAM_COPYBACK);
409 pFunc->PushCellByRef(&size);
410 pFunc->PushStringEx(buffer, sizeof(buffer), SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
411 pFunc->PushCellByRef(&iEntIndex);
412 pFunc->PushCellByRef(&iChannel);
413 pFunc->PushFloatByRef(&flVolume);
414 pFunc->PushCellByRef(&sndlevel);
415 pFunc->PushCellByRef(&iPitch);
416 pFunc->PushCellByRef(&iFlags);
417 g_InSoundHook = true;
418 pFunc->Execute(&res);
419 g_InSoundHook = false;
426 #if SOURCE_ENGINE >= SE_PORTAL2
427 RETURN_META_VALUE(MRES_SUPERCEDE, -1);
429 RETURN_META(MRES_SUPERCEDE);
434 CellRecipientFilter crf;
435 crf.Initialize(players, size);
436 #if SOURCE_ENGINE >= SE_PORTAL2
437 RETURN_META_VALUE_NEWPARAMS(
440 static_cast<int (IEngineSound::*)(IRecipientFilter &, int, int, const char *, unsigned int, const char *, float, float,
441 int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
442 (crf, iEntIndex, iChannel, buffer, -1, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)),
443 nSeed, iFlags, iPitch, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
445 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
446 RETURN_META_NEWPARAMS(
448 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char *, float, float,
449 int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
450 (crf, iEntIndex, iChannel, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)),
451 iFlags, iPitch, iSpecialDSP, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
454 RETURN_META_NEWPARAMS(
456 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char *, float, float,
457 int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>(&IEngineSound::EmitSound),
458 (crf, iEntIndex, iChannel, buffer, flVolume, SNDLVL_TO_ATTN(static_cast<soundlevel_t>(sndlevel)),
459 iFlags, iPitch, pOrigin, pDirection, pUtlVecOrigins, bUpdatePositions, soundtime, speakerentity)
466 #if SOURCE_ENGINE >= SE_PORTAL2
467 RETURN_META_VALUE(MRES_IGNORED, -1 );
471 /************************
473 * Sound Related Natives *
475 *************************/
477 SoundHooks s_SoundHooks;
479 static cell_t PrefetchSound(IPluginContext *pContext, const cell_t *params)
482 pContext->LocalToString(params[1], &name);
484 engsound->PrefetchSound(name);
489 static cell_t GetSoundDuration(IPluginContext *pContext, const cell_t *params)
492 pContext->LocalToString(params[1], &name);
494 return sp_ftoc(engsound->GetSoundDuration(name));
497 static cell_t EmitAmbientSound(IPluginContext *pContext, const cell_t *params)
503 int pitch, flags, level;
505 entity = SoundReferenceToIndex(params[3]);
508 pContext->LocalToPhysAddr(params[2], &addr);
509 pos.x = sp_ctof(addr[0]);
510 pos.y = sp_ctof(addr[1]);
511 pos.z = sp_ctof(addr[2]);
513 pContext->LocalToString(params[1], &name);
515 vol = sp_ctof(params[6]);
519 delay = sp_ctof(params[8]);
523 ENGINE_CALL(EmitAmbientSound)(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
527 engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
533 static cell_t FadeClientVolume(IPluginContext *pContext, const cell_t *params)
535 int client = params[1];
536 if (client < 1 || client > playerhelpers->GetMaxClients())
538 return pContext->ThrowNativeError("Client index %d is not valid", client);
541 IGamePlayer *player = playerhelpers->GetGamePlayer(client);
542 if (!player->IsInGame())
544 return pContext->ThrowNativeError("Client index %d is not in game", client);
547 engine->FadeClientVolume(
548 #if SOURCE_ENGINE == SE_DOTA
561 static cell_t StopSound(IPluginContext *pContext, const cell_t *params)
563 int entity = SoundReferenceToIndex(params[1]);
564 int channel = params[2];
567 pContext->LocalToString(params[3], &name);
569 #if SOURCE_ENGINE >= SE_PORTAL2
570 engsound->StopSound(entity, channel, name, -1);
572 engsound->StopSound(entity, channel, name);
578 static cell_t EmitSound(IPluginContext *pContext, const cell_t *params)
580 cell_t *addr, *cl_array;
581 CellRecipientFilter crf;
582 unsigned int numClients;
584 IGamePlayer *pPlayer = NULL;
586 pContext->LocalToPhysAddr(params[1], &cl_array);
587 numClients = params[2];
589 /* Client validation */
590 for (unsigned int i = 0; i < numClients; i++)
592 client = cl_array[i];
593 pPlayer = playerhelpers->GetGamePlayer(client);
597 return pContext->ThrowNativeError("Client index %d is invalid", client);
598 } else if (!pPlayer->IsInGame()) {
599 return pContext->ThrowNativeError("Client %d is not connected", client);
603 crf.Initialize(cl_array, numClients);
606 pContext->LocalToString(params[3], &sample);
608 int entity = SoundReferenceToIndex(params[4]);
609 int channel = params[5];
610 int level = params[6];
611 int flags = params[7];
612 float vol = sp_ctof(params[8]);
613 int pitch = params[9];
614 int speakerentity = params[10];
616 Vector *pOrigin = NULL, origin;
617 Vector *pDir = NULL, dir;
619 pContext->LocalToPhysAddr(params[11], &addr);
620 if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
623 origin.x = sp_ctof(addr[0]);
624 origin.y = sp_ctof(addr[1]);
625 origin.z = sp_ctof(addr[2]);
628 pContext->LocalToPhysAddr(params[12], &addr);
629 if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
632 dir.x = sp_ctof(addr[0]);
633 dir.y = sp_ctof(addr[1]);
634 dir.z = sp_ctof(addr[2]);
637 bool updatePos = params[13] ? true : false;
638 float soundtime = sp_ctof(params[14]);
640 CUtlVector<Vector> *pOrigVec = NULL;
641 CUtlVector<Vector> origvec;
645 for (cell_t i = 15; i <= params[0]; i++)
648 pContext->LocalToPhysAddr(params[i], &addr);
649 vec.x = sp_ctof(addr[0]);
650 vec.y = sp_ctof(addr[1]);
651 vec.z = sp_ctof(addr[2]);
652 origvec.AddToTail(vec);
656 /* If we're going to a "local player" and this is a dedicated server,
657 * intelligently redirect each sound.
660 if (entity == -2 && engine->IsDedicatedServer())
662 for (unsigned int i = 0; i < numClients; i++)
665 player[0] = cl_array[i];
667 crf.Initialize(player, 1);
668 #if SOURCE_ENGINE >= SE_PORTAL2
671 SH_CALL(enginesoundPatch,
672 static_cast<int (IEngineSound::*)(IRecipientFilter &, int, int, const char*, unsigned int, const char*, float,
673 soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
675 (&IEngineSound::EmitSound))
696 engsound->EmitSound(crf,
714 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
717 SH_CALL(enginesoundPatch,
718 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
719 soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
720 (&IEngineSound::EmitSound))
739 engsound->EmitSound(crf,
758 SH_CALL(enginesoundPatch,
759 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
760 soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
761 (&IEngineSound::EmitSound))
779 engsound->EmitSound(crf,
797 #if SOURCE_ENGINE >= SE_PORTAL2
800 SH_CALL(enginesoundPatch,
801 static_cast<int (IEngineSound::*)(IRecipientFilter &, int, int, const char*, unsigned int, const char*, float,
802 soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
803 (&IEngineSound::EmitSound))
824 engsound->EmitSound(crf,
842 #elif SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
845 SH_CALL(enginesoundPatch,
846 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
847 soundlevel_t, int, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
848 (&IEngineSound::EmitSound))
867 engsound->EmitSound(crf,
886 SH_CALL(enginesoundPatch,
887 static_cast<void (IEngineSound::*)(IRecipientFilter &, int, int, const char*, float,
888 soundlevel_t, int, int, const Vector *, const Vector *, CUtlVector<Vector> *, bool, float, int)>
889 (&IEngineSound::EmitSound))
907 engsound->EmitSound(crf,
928 static cell_t EmitSentence(IPluginContext *pContext, const cell_t *params)
931 CellRecipientFilter crf;
932 unsigned int numClients;
934 IGamePlayer *pPlayer = NULL;
936 pContext->LocalToPhysAddr(params[1], &addr);
937 numClients = params[2];
939 /* Client validation */
940 for (unsigned int i = 0; i < numClients; i++)
943 pPlayer = playerhelpers->GetGamePlayer(client);
947 return pContext->ThrowNativeError("Client index %d is invalid", client);
948 } else if (!pPlayer->IsInGame()) {
949 return pContext->ThrowNativeError("Client %d is not connected", client);
953 crf.Initialize(addr, numClients);
955 int sentence = params[3];
956 int entity = SoundReferenceToIndex(params[4]);
957 int channel = params[5];
958 int level = params[6];
959 int flags = params[7];
960 float vol = sp_ctof(params[8]);
961 int pitch = params[9];
962 int speakerentity = params[10];
964 Vector *pOrigin = NULL, origin;
965 Vector *pDir = NULL, dir;
967 pContext->LocalToPhysAddr(params[11], &addr);
968 if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
971 origin.x = sp_ctof(addr[0]);
972 origin.y = sp_ctof(addr[1]);
973 origin.z = sp_ctof(addr[2]);
976 pContext->LocalToPhysAddr(params[12], &addr);
977 if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
980 dir.x = sp_ctof(addr[0]);
981 dir.y = sp_ctof(addr[1]);
982 dir.z = sp_ctof(addr[2]);
985 bool updatePos = params[13] ? true : false;
986 float soundtime = sp_ctof(params[14]);
988 CUtlVector<Vector> *pOrigVec = NULL;
989 CUtlVector<Vector> origvec;
993 for (cell_t i = 15; i <= params[0]; i++)
996 pContext->LocalToPhysAddr(params[i], &addr);
997 vec.x = sp_ctof(addr[0]);
998 vec.y = sp_ctof(addr[1]);
999 vec.z = sp_ctof(addr[2]);
1000 origvec.AddToTail(vec);
1004 engsound->EmitSentenceByIndex(crf,
1009 (soundlevel_t)level,
1010 #if SOURCE_ENGINE >= SE_PORTAL2
1015 #if SOURCE_ENGINE == SE_CSS || SOURCE_ENGINE == SE_HL2DM || SOURCE_ENGINE == SE_DODS || SOURCE_ENGINE == SE_TF2
1028 static cell_t smn_AddAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
1030 IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1033 return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1036 s_SoundHooks.AddHook(AMBIENT_SOUND_HOOK, pFunc);
1041 static cell_t smn_AddNormalSoundHook(IPluginContext *pContext, const cell_t *params)
1043 IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1046 return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1049 s_SoundHooks.AddHook(NORMAL_SOUND_HOOK, pFunc);
1054 static cell_t smn_RemoveAmbientSoundHook(IPluginContext *pContext, const cell_t *params)
1056 IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1059 return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1062 if (!s_SoundHooks.RemoveHook(AMBIENT_SOUND_HOOK, pFunc))
1064 return pContext->ThrowNativeError("Invalid hooked function");
1070 static cell_t smn_RemoveNormalSoundHook(IPluginContext *pContext, const cell_t *params)
1072 IPluginFunction *pFunc = pContext->GetFunctionById(params[1]);
1075 return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
1078 if (!s_SoundHooks.RemoveHook(NORMAL_SOUND_HOOK, pFunc))
1080 return pContext->ThrowNativeError("Invalid hooked function");
1086 static cell_t smn_GetDistGainFromSoundLevel(IPluginContext *pContext, const cell_t *params)
1088 int decibel = params[1];
1089 float distance = sp_ctof(params[2]);
1091 return sp_ftoc(engsound->GetDistGainFromSoundLevel((soundlevel_t)decibel, distance));
1094 sp_nativeinfo_t g_SoundNatives[] =
1096 {"EmitAmbientSound", EmitAmbientSound},
1097 {"EmitSentence", EmitSentence},
1098 {"EmitSound", EmitSound},
1099 {"FadeClientVolume", FadeClientVolume},
1100 {"GetSoundDuration", GetSoundDuration},
1101 {"PrefetchSound", PrefetchSound},
1102 {"StopSound", StopSound},
1103 {"AddAmbientSoundHook", smn_AddAmbientSoundHook},
1104 {"AddNormalSoundHook", smn_AddNormalSoundHook},
1105 {"RemoveAmbientSoundHook", smn_RemoveAmbientSoundHook},
1106 {"RemoveNormalSoundHook", smn_RemoveNormalSoundHook},
1107 {"GetDistGainFromSoundLevel", smn_GetDistGainFromSoundLevel},