summaryrefslogtreecommitdiff
path: root/game/server/tf2/info_input_playsound.cpp
blob: 529e5618b154c239efa8a47d855568806a31e5f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "EntityOutput.h"
#include "EntityList.h"
#include "tf_team.h"
#include "baseentity.h"
#include "engine/IEngineSound.h"
#include "triggers.h"

// Spawnflags
#define SF_PLAYSOUND_USE_THIS_ORIGIN		0x0001

//-----------------------------------------------------------------------------
// Purpose: Map entity that plays sounds to players
//-----------------------------------------------------------------------------
class CInfoInputPlaySound : public CBaseEntity
{
	DECLARE_CLASS( CInfoInputPlaySound, CBaseEntity );
public:
	DECLARE_DATADESC();

	virtual void Spawn( void );
	virtual void Precache( void );
	virtual void Activate( void );

	// Inputs
	void InputPlaySoundToAll( inputdata_t &inputdata );
	void InputPlaySoundToTeam1( inputdata_t &inputdata );
	void InputPlaySoundToTeam2( inputdata_t &inputdata );
	void InputPlaySoundToPlayer( inputdata_t &inputdata );
	void InputSetSound( inputdata_t &inputdata );

	// Sound playing
	void PlaySoundToPlayer( CBaseTFPlayer *pPlayer );
	void PlaySoundToTeam( CTFTeam *pTeam );

private:
	string_t	m_iszSound;
	float		m_flVolume;
	float		m_flAttenuation;
	string_t	m_iszTestVolumeName;
	EHANDLE		m_hTestVolume;
};

BEGIN_DATADESC( CInfoInputPlaySound )

	// variables
	DEFINE_KEYFIELD( m_iszSound, FIELD_SOUNDNAME, "Sound" ),
	DEFINE_KEYFIELD( m_flVolume, FIELD_FLOAT, "Volume" ),
	DEFINE_KEYFIELD( m_flAttenuation, FIELD_FLOAT, "Attenuation" ),
	DEFINE_KEYFIELD( m_iszTestVolumeName, FIELD_STRING, "TestVolume" ),

	// inputs
	DEFINE_INPUTFUNC( FIELD_STRING, "SetSound", InputSetSound ),
	DEFINE_INPUTFUNC( FIELD_VOID, "PlaySoundToAll", InputPlaySoundToAll ),
	DEFINE_INPUTFUNC( FIELD_VOID, "PlaySoundToTeam1", InputPlaySoundToTeam1 ),
	DEFINE_INPUTFUNC( FIELD_VOID, "PlaySoundToTeam2", InputPlaySoundToTeam2 ),
	DEFINE_INPUTFUNC( FIELD_EHANDLE, "PlaySoundToPlayer", InputPlaySoundToPlayer ),

END_DATADESC()

LINK_ENTITY_TO_CLASS( info_input_playsound, CInfoInputPlaySound );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::Spawn( void )
{
	m_hTestVolume = NULL;
	Precache();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::Precache( void )
{
	if ( m_iszSound != NULL_STRING )
	{
		PrecacheScriptSound( STRING(m_iszSound) );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::Activate( void )
{
	BaseClass::Activate();

	// Find our test volume, if we have one
	if ( m_iszTestVolumeName != NULL_STRING )
	{
		m_hTestVolume = gEntList.FindEntityByName( NULL, STRING(m_iszTestVolumeName) );
		if ( !m_hTestVolume )
		{
			Msg("ERROR: Could not find test volume %s for info_input_playsound.\n", STRING(m_iszTestVolumeName) );
		}
		else
		{
			// Make sure it's a trigger
			CBaseTrigger *pTrigger = dynamic_cast<CBaseTrigger*>((CBaseEntity*)m_hTestVolume);
			if ( !pTrigger )
			{
				Msg("ERROR: info_input_playsound specifies a volume %s, but it's not a trigger.\n", STRING(m_iszTestVolumeName) );
				m_hTestVolume = NULL;
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to all players
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::InputPlaySoundToAll( inputdata_t &inputdata )
{
	for ( int i = 1; i <= gpGlobals->maxClients; i++ )
	{
		CBaseTFPlayer *pPlayer = ToBaseTFPlayer( UTIL_PlayerByIndex(i) );
		if ( pPlayer )
		{
			PlaySoundToPlayer( pPlayer );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to all players on team 1
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::InputPlaySoundToTeam1( inputdata_t &inputdata )
{
	PlaySoundToTeam( GetGlobalTFTeam(1) );
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to all players on team 2
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::InputPlaySoundToTeam2( inputdata_t &inputdata )
{
	PlaySoundToTeam( GetGlobalTFTeam(2) );
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to a specific player
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::InputPlaySoundToPlayer( inputdata_t &inputdata )
{
	CBaseEntity *pEntity = (inputdata.value.Entity()).Get();
	if ( pEntity && pEntity->IsPlayer() )
	{
		CBaseTFPlayer *pPlayer = (CBaseTFPlayer *)pEntity;
		PlaySoundToPlayer( pPlayer );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Set the sound to play
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::InputSetSound( inputdata_t &inputdata )
{
	m_iszSound = MAKE_STRING( inputdata.value.String() );
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to a team
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::PlaySoundToTeam( CTFTeam *pTeam )
{
	for ( int i = 0; i < pTeam->GetNumPlayers(); i++ )
	{
		PlaySoundToPlayer( (CBaseTFPlayer*)pTeam->GetPlayer(i) );
	}
}

//-----------------------------------------------------------------------------
// Purpose: Play sound to a player
//-----------------------------------------------------------------------------
void CInfoInputPlaySound::PlaySoundToPlayer( CBaseTFPlayer *pPlayer )
{
	// First, if we have a test volume, make sure the player's within it
	if ( m_hTestVolume )
	{
		CBaseTrigger *pTrigger = (CBaseTrigger *)(CBaseEntity*)m_hTestVolume;
		if ( !pTrigger->IsTouching( pPlayer ) )
			return;
	}

	// Check to see if we're supposed to play it from this entity's location
	if ( HasSpawnFlags( SF_PLAYSOUND_USE_THIS_ORIGIN ) )
	{
		CPASAttenuationFilter filter;
		filter.AddRecipient( pPlayer );
		filter.Filter( GetAbsOrigin(), m_flAttenuation );

		EmitSound_t ep;
		ep.m_nChannel = CHAN_VOICE;
		ep.m_pSoundName = STRING(m_iszSound);
		ep.m_flVolume = m_flVolume;
		ep.m_SoundLevel = ATTN_TO_SNDLVL( m_flAttenuation );

		ep.m_pOrigin = &(GetAbsOrigin());

		EmitSound( filter, entindex(), ep );
	}
	else
	{
		CSingleUserRecipientFilter filter( pPlayer );

		EmitSound_t ep;
		ep.m_nChannel = CHAN_VOICE;
		ep.m_pSoundName = STRING(m_iszSound);
		ep.m_flVolume = m_flVolume;
		ep.m_SoundLevel = ATTN_TO_SNDLVL( m_flAttenuation );

		EmitSound( filter, pPlayer->entindex(), ep );
	}
}