extensions/sdktools/vsound.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1233 e9cfc03b9bb8
child 1735 d6966e3316e9
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
     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 #include "CellRecipientFilter.h"
    34 
    35 static cell_t PrefetchSound(IPluginContext *pContext, const cell_t *params)
    36 {
    37 	char *name;
    38 	pContext->LocalToString(params[1], &name);
    39 
    40 	engsound->PrefetchSound(name);
    41 
    42 	return 1;
    43 }
    44 
    45 static cell_t GetSoundDuration(IPluginContext *pContext, const cell_t *params)
    46 {
    47 	char *name;
    48 	pContext->LocalToString(params[1], &name);
    49 
    50 	return sp_ftoc(engsound->GetSoundDuration(name));
    51 }
    52 
    53 static cell_t EmitAmbientSound(IPluginContext *pContext, const cell_t *params)
    54 {
    55 	cell_t entity;
    56 	Vector pos;
    57 	char *name;
    58 	float vol, delay;
    59 	int pitch, flags, level;
    60 
    61 	entity = params[3];
    62 
    63 	cell_t *addr;
    64 	pContext->LocalToPhysAddr(params[2], &addr);
    65 	pos.x = sp_ctof(addr[0]);
    66 	pos.y = sp_ctof(addr[1]);
    67 	pos.z = sp_ctof(addr[2]);
    68 
    69 	pContext->LocalToString(params[1], &name);
    70 
    71 	vol = sp_ctof(params[6]);
    72 	level = params[4];
    73 	flags = params[5];
    74 	pitch = params[7];
    75 	delay = sp_ctof(params[8]);
    76 
    77 	engine->EmitAmbientSound(entity, pos, name, vol, (soundlevel_t)level, flags, pitch, delay);
    78 
    79 	return 1;
    80 }
    81 
    82 static cell_t FadeClientVolume(IPluginContext *pContext, const cell_t *params)
    83 {
    84 	int client = params[1];
    85 	if (client < 1 || client > playerhelpers->GetMaxClients())
    86 	{
    87 		return pContext->ThrowNativeError("Client index %d is not valid", client);
    88 	}
    89 
    90 	IGamePlayer *player = playerhelpers->GetGamePlayer(client);
    91 	if (!player->IsInGame())
    92 	{
    93 		return pContext->ThrowNativeError("Client index %d is not in game", client);
    94 	}
    95 
    96 	engine->FadeClientVolume(player->GetEdict(),
    97 		sp_ctof(params[2]),
    98 		sp_ctof(params[3]),
    99 		sp_ctof(params[4]),
   100 		sp_ctof(params[5]));
   101 
   102 	return 1;
   103 }
   104 
   105 static cell_t StopSound(IPluginContext *pContext, const cell_t *params)
   106 {
   107 	int entity = params[1];
   108 	int channel = params[2];
   109 
   110 	char *name;
   111 	pContext->LocalToString(params[3], &name);
   112 
   113 	engsound->StopSound(entity, channel, name);
   114 
   115 	return 1;
   116 }
   117 
   118 static cell_t EmitSound(IPluginContext *pContext, const cell_t *params)
   119 {
   120 	cell_t *addr, *pl_addr;
   121 
   122 	CellRecipientFilter crf;
   123 	pContext->LocalToPhysAddr(params[1], &pl_addr);
   124 	crf.Initialize(pl_addr, params[2]);
   125 
   126 	char *sample;
   127 	pContext->LocalToString(params[3], &sample);
   128 	
   129 	int entity = params[4];
   130 	int channel = params[5];
   131 	int level = params[6];
   132 	int flags = params[7];
   133 	float vol = sp_ctof(params[8]);
   134 	int pitch = params[9];
   135 	int speakerentity = params[10];
   136 
   137 	Vector *pOrigin = NULL, origin;
   138 	Vector *pDir = NULL, dir;
   139 
   140 	pContext->LocalToPhysAddr(params[11], &addr);
   141 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   142 	{
   143 		pOrigin = &origin;
   144 		origin.x = sp_ctof(addr[0]);
   145 		origin.y = sp_ctof(addr[1]);
   146 		origin.z = sp_ctof(addr[2]);
   147 	}
   148 
   149 	pContext->LocalToPhysAddr(params[12], &addr);
   150 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   151 	{
   152 		pDir = &dir;
   153 		dir.x = sp_ctof(addr[0]);
   154 		dir.y = sp_ctof(addr[1]);
   155 		dir.z = sp_ctof(addr[2]);
   156 	}
   157 
   158 	bool updatePos = params[13] ? true : false;
   159 	float soundtime = sp_ctof(params[14]);
   160 
   161 	CUtlVector<Vector> *pOrigVec = NULL;
   162 	CUtlVector<Vector> origvec;
   163 	if (params[0] > 14)
   164 	{
   165 		pOrigVec = &origvec;
   166 		for (cell_t i = 15; i <= params[0]; i++)
   167 		{
   168 			Vector vec;
   169 			pContext->LocalToPhysAddr(params[i], &addr);
   170 			vec.x = sp_ctof(addr[0]);
   171 			vec.y = sp_ctof(addr[1]);
   172 			vec.z = sp_ctof(addr[2]);
   173 			origvec.AddToTail(vec);
   174 		}
   175 	}
   176 
   177 	/* If we're going to a "local player" and this is a dedicated server,
   178 	 * intelligently redirect each sound.
   179 	 */
   180 
   181 	if (entity == -2 && engine->IsDedicatedServer())
   182 	{
   183 		for (cell_t i=0; i<params[2]; i++)
   184 		{
   185 			cell_t player[1];
   186 			player[0] = pl_addr[i];
   187 			crf.Reset();
   188 			crf.Initialize(player, 1);
   189 			engsound->EmitSound(crf, 
   190 				player[0], 
   191 				channel, 
   192 				sample, 
   193 				vol, 
   194 				(soundlevel_t)level, 
   195 				flags, 
   196 				pitch, 
   197 				pOrigin,
   198 				pDir,
   199 				pOrigVec,
   200 				updatePos,
   201 				soundtime,
   202 				speakerentity);
   203 		}
   204 	} else {
   205 		engsound->EmitSound(crf, 
   206 			entity, 
   207 			channel, 
   208 			sample, 
   209 			vol, 
   210 			(soundlevel_t)level, 
   211 			flags, 
   212 			pitch, 
   213 			pOrigin,
   214 			pDir,
   215 			pOrigVec,
   216 			updatePos,
   217 			soundtime,
   218 			speakerentity);
   219 	}
   220 
   221 	return 1;
   222 }
   223 
   224 static cell_t EmitSentence(IPluginContext *pContext, const cell_t *params)
   225 {
   226 	cell_t *addr;
   227 
   228 	CellRecipientFilter crf;
   229 	pContext->LocalToPhysAddr(params[1], &addr);
   230 	crf.Initialize(addr, params[2]);
   231 
   232 	int sentence = params[3];
   233 	int entity = params[4];
   234 	int channel = params[5];
   235 	int level = params[6];
   236 	int flags = params[7];
   237 	float vol = sp_ctof(params[8]);
   238 	int pitch = params[9];
   239 	int speakerentity = params[10];
   240 
   241 	Vector *pOrigin = NULL, origin;
   242 	Vector *pDir = NULL, dir;
   243 
   244 	pContext->LocalToPhysAddr(params[11], &addr);
   245 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   246 	{
   247 		pOrigin = &origin;
   248 		origin.x = sp_ctof(addr[0]);
   249 		origin.y = sp_ctof(addr[1]);
   250 		origin.z = sp_ctof(addr[2]);
   251 	}
   252 
   253 	pContext->LocalToPhysAddr(params[12], &addr);
   254 	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
   255 	{
   256 		pDir = &dir;
   257 		dir.x = sp_ctof(addr[0]);
   258 		dir.y = sp_ctof(addr[1]);
   259 		dir.z = sp_ctof(addr[2]);
   260 	}
   261 
   262 	bool updatePos = params[13] ? true : false;
   263 	float soundtime = sp_ctof(params[14]);
   264 
   265 	CUtlVector<Vector> *pOrigVec = NULL;
   266 	CUtlVector<Vector> origvec;
   267 	if (params[0] > 14)
   268 	{
   269 		pOrigVec = &origvec;
   270 		for (cell_t i = 15; i <= params[0]; i++)
   271 		{
   272 			Vector vec;
   273 			pContext->LocalToPhysAddr(params[i], &addr);
   274 			vec.x = sp_ctof(addr[0]);
   275 			vec.y = sp_ctof(addr[1]);
   276 			vec.z = sp_ctof(addr[2]);
   277 			origvec.AddToTail(vec);
   278 		}
   279 	}
   280 
   281 	engsound->EmitSentenceByIndex(crf, 
   282 		entity, 
   283 		channel, 
   284 		sentence, 
   285 		vol, 
   286 		(soundlevel_t)level, 
   287 		flags, 
   288 		pitch, 
   289 		pOrigin,
   290 		pDir,
   291 		pOrigVec,
   292 		updatePos,
   293 		soundtime,
   294 		speakerentity);
   295 
   296 	return 1;
   297 }
   298 
   299 sp_nativeinfo_t g_SoundNatives[] = 
   300 {
   301 	{"EmitAmbientSound",	EmitAmbientSound},
   302 	{"EmitSentence",		EmitSentence},
   303 	{"EmitSound",			EmitSound},
   304 	{"FadeClientVolume",	FadeClientVolume},
   305 	{"GetSoundDuration",	GetSoundDuration},
   306 	{"PrefetchSound",		PrefetchSound},
   307 	{"StopSound",			StopSound},
   308 	{NULL,					NULL},
   309 };