File: sarmusic.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (422 lines) | stat: -rw-r--r-- 10,573 bytes parent folder | download | duplicates (6)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <math.h>

#include "../include/string.h"
#include "../include/disk.h"

#include "sound.h"
#include "menu.h"
#include "obj.h"
#include "objutils.h"
#include "sar.h"
#include "sarmusic.h"
#include "sarmenucodes.h"
#include "config.h"


void SARMusicUpdate(sar_core_struct *core_ptr);


#define MAX(a,b)	(((a) > (b)) ? (a) : (b))
#define MIN(a,b)	(((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)	(MIN(MAX((a),(l)),(h)))


/*
 *	Checks the current situation (current music, current menu or
 *	simulation, player object state, etc) and changes the music
 *	as needed.
 *
 *	This function should be called once per loop or whenever the
 *	music is suspected to need to be changed.
 *
 *	The global opt->music state will be checked and music will
 *	be turned on or off as needed.
 */
void SARMusicUpdate(sar_core_struct *core_ptr)
{
	int prev_id, new_id, new_enter_id;
	sar_menu_struct *cur_menu_ptr;
	snd_recorder_struct *recorder = core_ptr->recorder;
	sar_option_struct *opt = &core_ptr->option;
	if(recorder == NULL)
	    return;

	/* Get current music id (can be -1) and record it as the
	 * previous music id
	 */
	prev_id = core_ptr->cur_music_id;

	/* Is music state switched on in the options? */
	if(opt->music)
	{

	}
	else
	{
	    /* Music is suppose to be off, so turn it off as needed
	     * and return
	     */
	    if(SoundMusicIsPlaying(recorder))
		SoundMusicStopPlay(recorder);
	    return;
	}

	/* ******************************************************** */

	/* Begin checking which music id should be played, for
	 * `repeating' and `enter' styles. After this check, new_id
	 * and new_enter_id will be updated to the id codes of the
	 * songs intended to be played
	 */
	new_id = -1;
	new_enter_id = -1;

	/* First determine if we are in the menu system or in
	 * simulation
	 *
	 * The appropriate new music ids should be choosen (if any)
	 * here
	 */
	cur_menu_ptr = SARGetCurrentMenuPtr(core_ptr);
	if(cur_menu_ptr != NULL)
	{
	    /* In menu system, choose new music ids by checking
	     * which menu we are currently in (check the current
	     * menu's name)
	     */
	    const char *menu_name = (const char *)cur_menu_ptr->name;
	    if(menu_name != NULL)
	    {
		/* Loading simulation menu? */
		if(!strcasecmp(menu_name, SAR_MENU_NAME_LOADING_SIMULATION))
		{
		    new_id = SAR_MUSIC_ID_LOADING_SIMULATION;
		}
		/* Main menu? */
		else if(!strcasecmp(menu_name, SAR_MENU_NAME_MAIN))
		{
		    new_id = SAR_MUSIC_ID_MENUS;
		}
/* TODO Add code to check other menus here */
		else
		{
		    /* Some other menu, use the generic menu music id */
		    new_id = SAR_MUSIC_ID_MENUS;
		}
	    }
	    else
	    {
		/* No menu name available, just use the generic menu
		 * music id then
		 */
		new_id = SAR_MUSIC_ID_MENUS;
	    }
	}       /* In menu system? */
	else
	{
	    /* Not in menu system, probably in simulation.  Check
	     * the state of the scene and player object to choose
	     * the appropriate music id
	     */
	    sar_scene_struct *scene = core_ptr->scene;
	    sar_object_struct *obj_ptr = NULL;


	    /* Scene structure must be available */
	    if(scene != NULL)
	    {
		/* Get the pointer to the player object, the state
		 * of the player object will dictate which music
		 * id will be choosen.
		 */
		obj_ptr = scene->player_obj_ptr;

		/* Player object exists? */
		if(obj_ptr != NULL)
		{
		    sar_object_aircraft_struct *obj_aircraft_ptr;
		    sar_obj_hoist_struct *hoist_ptr;


		    /* Get pointers to object's substructures */
		    obj_aircraft_ptr = SAR_OBJ_GET_AIRCRAFT(obj_ptr);
		    hoist_ptr = SARObjGetHoistPtr(obj_ptr, 0, NULL);

		    /* Begin checking by player object type and its
		     * status to determine the new music id's
		     */
		    if(obj_aircraft_ptr != NULL)
		    {
			if(obj_aircraft_ptr->landed)
			{
			    new_id =
			SAR_MUSIC_ID_SIMULATION_ONGROUND;
			    new_enter_id =
			SAR_MUSIC_ID_SIMULATION_ONGROUND_ENTER;
			}
			else
			{
			    /* In flight */
			    switch(scene->tod_code)
			    {
			      case SAR_TOD_CODE_NIGHT:
				new_id =
			SAR_MUSIC_ID_SIMULATION_INFLIGHT_NIGHT;
				new_enter_id =
			SAR_MUSIC_ID_SIMULATION_INFLIGHT_NIGHT_ENTER;
				break;
			      case SAR_TOD_CODE_DUSK:
			      case SAR_TOD_CODE_DAWN:
			      case SAR_TOD_CODE_DAY:
			      case SAR_TOD_CODE_UNDEFINED:
				new_id =
			SAR_MUSIC_ID_SIMULATION_INFLIGHT_DAY;
				new_enter_id =
			SAR_MUSIC_ID_SIMULATION_INFLIGHT_DAY_ENTER;
				break;
			    }
			}
		    }
		    if(hoist_ptr != NULL)
		    {
			if(hoist_ptr->rope_cur > 0.0)
			{
			    new_id =
				SAR_MUSIC_ID_SIMULATION_RESCUE;
			    new_enter_id =
				SAR_MUSIC_ID_SIMULATION_RESCUE_ENTER;
			}
		    }

		    /* Set music id's to defaults if they were not able
		     * to be determined above.
		     */
		    if(new_id < 0)
			new_id = SAR_MUSIC_ID_SIMULATION_ONGROUND;
		    if(new_enter_id < 0)
			new_enter_id = SAR_MUSIC_ID_SIMULATION_ONGROUND_ENTER;
		}
		else
		{
		    /* No player object available, assume on ground */
		    new_id = SAR_MUSIC_ID_SIMULATION_ONGROUND;
		    new_enter_id = SAR_MUSIC_ID_SIMULATION_ONGROUND_ENTER;
		}
	    }

	}       /* In simulation */


	/* ******************************************************** */

	/* At this point the new music id's should have been
	 * choosen, they can still be -1 to indicate no change.
	 */

/* Macro to return the music reference file as tmp_path from the
 * given music_ref_ptr. Uses variables tmp_path[PATH_MAX + NAME_MAX]
 * and music_ref_ptr.
 */
#define GET_MUSIC_REF_FILE      \
{ \
 *tmp_path = '\0'; \
 if((music_ref_ptr != NULL) ? (music_ref_ptr->filename != NULL) : 0) \
 { \
  struct stat stat_buf; \
  const char *cstrptr = PrefixPaths( \
   dname.local_data, music_ref_ptr->filename \
  ); \
  /* Check local file to see if it exists first */ \
  if((cstrptr != NULL) ? !stat(cstrptr, &stat_buf) : 0) \
  { \
   /* Found local file, put that path value in tmp_path */ \
   strncpy(tmp_path, cstrptr, PATH_MAX + NAME_MAX); \
  } \
  else \
  { \
   /* Check global file */ \
   cstrptr = PrefixPaths( \
    dname.global_data, music_ref_ptr->filename \
   ); \
   if((cstrptr != NULL) ? !stat(cstrptr, &stat_buf) : 0) \
   { \
    /* Found global file, put that path value in tmp_path */ \
    strncpy(tmp_path, cstrptr, PATH_MAX + NAME_MAX); \
   } \
  } \
 } \
}


	/* Change in music id's and new music id is valid? */
	if((new_id != prev_id) && (new_id > -1))
	{
	    int music_ref_num;
	    sar_music_ref_struct *music_ref_ptr;
	    char tmp_path[PATH_MAX + NAME_MAX];


	    if(opt->runtime_debug)
		printf(
"SARMusicUpdate(): Changing to music id %i from id %i\n",
		    new_id, prev_id
		);

	    /* Find music reference in list that matches the music id
	     * specified by new_id.
	     */
	    music_ref_ptr = SARMusicMatchPtr(
		core_ptr->music_ref, core_ptr->total_music_refs,
		new_id, &music_ref_num
	    );
	    /* Found music reference matching new_id? */
	    if(music_ref_ptr != NULL)
	    {
		/* Stop playing previous music (if any) and start
		 * playing the new one.
		 */
		GET_MUSIC_REF_FILE
		if(SoundMusicStartPlay(
		    recorder,
		    tmp_path,
		    (music_ref_ptr->flags & SAR_MUSIC_REF_FLAGS_REPEAT) ?
			-1 : 1
		))
		{
		    /* Error playing this music, need to print warning
		     * and switch off music.
		     */
		    fprintf(
			stderr,
			"%s: Unable to play music, turning music off.\n",
			music_ref_ptr->filename
		    );
		    opt->music = False;
		    core_ptr->cur_music_id = prev_id = new_id = -1;
		}
		else
		{
		    /* Successfully started playing new music, update
		     * current music id.
		     */
		    core_ptr->cur_music_id = prev_id = new_id;
		}
	    }
	    else
	    {
		/* No music reference for the given id, continue playing
		 * previous music and do not update current music id.
		 */
	    }
	}
	/* No change in music id's and new music id is valid? */
	else if(new_id > -1)
	{
	    /* No change in music id, but need to check if music is
	     * still playing. If it not playing then it needs to be
	     * either restarted or changed to the next consecutive
	     * song.
	     */
	    if(!SoundMusicIsPlaying(recorder))
	    {
		int music_ref_num;
		sar_music_ref_struct *music_ref_ptr;
		char tmp_path[PATH_MAX + NAME_MAX];


		if(opt->runtime_debug)
		    printf(
"SARMusicUpdate(): Music id %i has stopped playing.\n",
			new_id
		    );

		/* Update new_id to the `next' song that needs to be
		 * played after the current one has stopped.
		 */
		switch(prev_id)
		{
		  case SAR_MUSIC_ID_SIMULATION_ONGROUND_ENTER:
		    new_id = SAR_MUSIC_ID_SIMULATION_ONGROUND;
		    break;

		  case SAR_MUSIC_ID_SIMULATION_INFLIGHT_DAY_ENTER:
		    new_id = SAR_MUSIC_ID_SIMULATION_INFLIGHT_DAY;
		    break;

		  case SAR_MUSIC_ID_SIMULATION_INFLIGHT_NIGHT_ENTER:
		    new_id = SAR_MUSIC_ID_SIMULATION_INFLIGHT_NIGHT;
		    break;

		  case SAR_MUSIC_ID_SIMULATION_RESCUE_ENTER:
		    new_id = SAR_MUSIC_ID_SIMULATION_RESCUE;
		    break;

		  default:
		    /* All other music id's when stopped should just
		     * re-start as the same music id.
		     */
		    new_id = prev_id;
		    break;
		}

		if(opt->runtime_debug)
		    printf(
"SARMusicUpdate(): Previous music id %i stopped, playing new music id %i.\n",
			prev_id, new_id
		    );

		/* Find music reference in list that matches the music
		 * id specified by new_id.
		 */
		music_ref_ptr = SARMusicMatchPtr(
		    core_ptr->music_ref, core_ptr->total_music_refs,
		    new_id, &music_ref_num
		);
		/* Found music reference matching new_id? */
		if(music_ref_ptr != NULL)
		{
		    /* Start playing new music */
		    GET_MUSIC_REF_FILE
		    if(SoundMusicStartPlay(
			recorder,
			tmp_path,
			(music_ref_ptr->flags & SAR_MUSIC_REF_FLAGS_REPEAT) ?
			    -1 : 1
		    ))
		    {
			/* Error playing this music, need to print warning
			 * and switch off music.
			 */
			fprintf(
			    stderr,
			    "%s: Unable to play music, turning music off.\n",
			    music_ref_ptr->filename
			);
			opt->music = False;
			core_ptr->cur_music_id = prev_id = new_id = -1;
		    }
		    else
		    {
			/* Successfully started playing new music, update
			 * current music id.
			 */
			core_ptr->cur_music_id = prev_id = new_id;
		    }
		}
		else
		{
		    /* No music reference for the given id or music needs
		     * to stop until changed. So do not update the current
		     * music id (even if the previous music has stopped).
		     */
		}
	    }   /* Music has stopped playing? */
	}

#undef GET_MUSIC_REF_FILE
}