summaryrefslogtreecommitdiff
path: root/src/avp/bh_snds.c
blob: 30128e6f13de6b8d602a7cc3a4a97b95a5a0cebd (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "3dc.h"
#include "inline.h"
#include "module.h"
#include "stratdef.h"
#include "gamedef.h"
#include "bh_types.h"
#include "bh_snds.h"
#include "dynblock.h"
#include "dynamics.h"

#define UseLocalAssert Yes
#include "ourasert.h"
#include "dxlog.h"

#include "psndplat.h"
#include "jsndsup.h"


void * SoundBehaveInit(void* bhdata, STRATEGYBLOCK* sbptr)
{
	SOUND_BEHAV_BLOCK * sbb = 0;
	SOUND_TOOLS_TEMPLATE * stt = (SOUND_TOOLS_TEMPLATE *)bhdata;

	GLOBALASSERT (sbptr && stt);
	
	sbb = (SOUND_BEHAV_BLOCK *)AllocateMem (sizeof (SOUND_BEHAV_BLOCK));
	
	GLOBALASSERT (sbb);
	
	sbb->sound_loaded = stt->sound_loaded;

	sbb->position = stt->position;
	sbb->inner_range = stt->inner_range;
	sbb->outer_range = stt->outer_range;
	sbb->max_volume = stt->max_volume;
	sbb->pitch = stt->pitch;
		
	sbb->sound_not_started = 1;
	
	sbb->wav_name = (char *) AllocateMem (strlen (stt->sound_name) + 1);
	strcpy (sbb->wav_name, stt->sound_name);

	sbb->playing=stt->playing;
	sbb->loop=stt->loop;

	sbb->activ_no = SOUND_NOACTIVEINDEX;
	
	return((void*)sbb);
	
}

void SoundBehaveDestroy (STRATEGYBLOCK * sbptr)
{
	SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->SBdataptr;
	
	if (sbb->activ_no != SOUND_NOACTIVEINDEX)
	{
		Sound_Stop (sbb->activ_no);
	}
	if (sbb->wav_name)
	{
		DeallocateMem (sbb->wav_name);
	}
}
void SoundBehaveFun (STRATEGYBLOCK * sbptr)
{
	SOUND_BEHAV_BLOCK * sbb = (SOUND_BEHAV_BLOCK*)sbptr->SBdataptr;
	

	if(!sbb->sound_loaded) return;
	if (sbb->sound_not_started && Player)
	{
		SOUND3DDATA s3d;
	
		if(sbb->playing)
		{
			//start playing the sound , but only if it is close enough
			int dist=VectorDistance(&Player->ObWorld,&sbb->position);
			if(dist<=sbb->outer_range)
			{

				s3d.position = sbb->position;
				s3d.inner_range = sbb->inner_range;
				s3d.outer_range = sbb->outer_range;
				s3d.velocity.vx = 0;
				s3d.velocity.vy = 0;
				s3d.velocity.vz = 0;
		
				if(sbb->loop)
					Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "nelh", &s3d, &sbb->activ_no);
				else
					Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "neh", &s3d, &sbb->activ_no);

				if (sbb->activ_no != SOUND_NOACTIVEINDEX)
				{
					Sound_ChangeVolume (sbb->activ_no, sbb->max_volume);
					Sound_ChangePitch (sbb->activ_no, sbb->pitch);
				}
			}
		}
		sbb->sound_not_started = 0;
	}
	
	/* KJL 14:15:39 12/8/97 - kill any objects that are too far away */
	if(sbb->playing)
	{
		int dist=VectorDistance(&Player->ObWorld,&sbb->position);
		if(dist>sbb->outer_range)
		{
			//stop sound if it is playing
		
			if (sbb->activ_no != SOUND_NOACTIVEINDEX)
			{
				Sound_Stop (sbb->activ_no);
			}
			if(!sbb->loop && sbb->playing)
			{
				//not much point in restarting a non-looping sound, since it won't restart at the right place
				sbb->playing=0;
			}
			return;
		}
	}

	if (sbb->activ_no == SOUND_NOACTIVEINDEX && sbb->sound_loaded && sbb->playing)
	{
		if(sbb->loop)
		{
			SOUND3DDATA s3d;

			s3d.position = sbb->position;
			s3d.inner_range = sbb->inner_range;
			s3d.outer_range = sbb->outer_range;
			s3d.velocity.vx = 0;
			s3d.velocity.vy = 0;
			s3d.velocity.vz = 0;

			if(sbb->loop)
				Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "nelh", &s3d, &sbb->activ_no);
			else
				Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "neh", &s3d, &sbb->activ_no);

			if (sbb->activ_no != SOUND_NOACTIVEINDEX)
			{
				Sound_ChangeVolume (sbb->activ_no, sbb->max_volume);
				Sound_ChangePitch (sbb->activ_no, sbb->pitch);
			}
		}
		else //sound has finished playing
		{
			sbb->playing=0;
		}
	}

//	// hack hack hack fixme fixme fix me
//	#pragma message ("Special code to deal with siren.wav!!");	
	
	if (AvP.DestructTimer != -1)
	{
		if (sbb->sound_loaded && sbb->activ_no != SOUND_NOACTIVEINDEX)
		{
			if (!_stricmp(GameSounds[sbb->sound_loaded->sound_num].wavName, "siren.wav"))
			{
				if (AvP.DestructTimer < ONE_FIXED * 10)
				{
					sbb->max_volume = 127;
				}
				else
				{
					sbb->max_volume = 90;
				}
				Sound_ChangeVolume (sbb->activ_no, sbb->max_volume);
			}
		}
	}
}


void StartPlacedSoundPlaying(STRATEGYBLOCK* sbptr)
{
	SOUND_BEHAV_BLOCK * sbb = 0;
	GLOBALASSERT(sbptr);
	GLOBALASSERT(sbptr->I_SBtype==I_BehaviourPlacedSound);
	sbb = (SOUND_BEHAV_BLOCK*)sbptr->SBdataptr;

	if(!sbb->sound_loaded) return;

	if(!sbb->playing)
	{
		//if sound is within range start it
		int dist=VectorDistance(&Player->ObWorld,&sbb->position);
		if(dist<sbb->outer_range)
		{
			SOUND3DDATA s3d;

			s3d.position = sbb->position;
			s3d.inner_range = sbb->inner_range;
			s3d.outer_range = sbb->outer_range;
			s3d.velocity.vx = 0;
			s3d.velocity.vy = 0;
			s3d.velocity.vz = 0;

			if(sbb->loop)
				Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "nelh", &s3d, &sbb->activ_no);
			else
				Sound_Play ((SOUNDINDEX)sbb->sound_loaded->sound_num, "neh", &s3d, &sbb->activ_no);

			if (sbb->activ_no != SOUND_NOACTIVEINDEX)
			{
				Sound_ChangeVolume (sbb->activ_no, sbb->max_volume);
				Sound_ChangePitch (sbb->activ_no, sbb->pitch);
			}
		}
		sbb->playing=1;
	}
}
void StopPlacedSoundPlaying(STRATEGYBLOCK* sbptr)
{
	SOUND_BEHAV_BLOCK * sbb = 0;
	GLOBALASSERT(sbptr);
	GLOBALASSERT(sbptr->I_SBtype==I_BehaviourPlacedSound);
	sbb = (SOUND_BEHAV_BLOCK*)sbptr->SBdataptr;

	if(!sbb->sound_loaded) return;
	if(sbb->playing)
	{
		sbb->playing=0;
		if (sbb->activ_no != SOUND_NOACTIVEINDEX)
		{
			Sound_Stop (sbb->activ_no);
		}
	}
}


/*--------------------**
** Loading and Saving **
**--------------------*/
#include "savegame.h"
typedef struct placed_sound_save_block
{
	SAVE_BLOCK_STRATEGY_HEADER header;

	BOOL playing;


}PLACED_SOUND_SAVE_BLOCK;

//defines for load/save macros
#define SAVELOAD_BLOCK block
#define SAVELOAD_BEHAV sbb 

void LoadStrategy_PlacedSound(SAVE_BLOCK_STRATEGY_HEADER* header)
{
	STRATEGYBLOCK* sbPtr;
	SOUND_BEHAV_BLOCK * sbb;
	PLACED_SOUND_SAVE_BLOCK* block = (PLACED_SOUND_SAVE_BLOCK*) header; 

	//check the size of the save block
	if(header->size!=sizeof(*block)) return;

	//find the existing strategy block
	sbPtr = FindSBWithName(header->SBname);
	if(!sbPtr) return;

	//make sure the strategy found is of the right type
	if(sbPtr->I_SBtype != I_BehaviourPlacedSound) return;

	sbb = (SOUND_BEHAV_BLOCK *)sbPtr->SBdataptr;

	//start copying stuff
	COPYELEMENT_LOAD(playing)

	sbb->sound_not_started = 0;
	Load_SoundState(&sbb->activ_no);
}

void SaveStrategy_PlacedSound(STRATEGYBLOCK* sbPtr)
{
	PLACED_SOUND_SAVE_BLOCK* block;
	SOUND_BEHAV_BLOCK * sbb;

	sbb = (SOUND_BEHAV_BLOCK *)sbPtr->SBdataptr;


	GET_STRATEGY_SAVE_BLOCK(block,sbPtr);
	
	//start copying stuff
	COPYELEMENT_SAVE(playing)
	
	Save_SoundState(&sbb->activ_no);

}