Fix crash if Num_walls=0
[btb/d2x.git] / main / songs.c
1 /* $Id: songs.c,v 1.14 2004-08-29 17:57:23 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Routines to manage the songs in Descent.
18  *
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ctype.h>
30 #if !defined(_MSC_VER) && !defined(macintosh)
31 #include <unistd.h>
32 #endif
33
34 #include "inferno.h"
35 #include "error.h"
36 #include "pstypes.h"
37 #include "args.h"
38 #include "songs.h"
39 #include "mono.h"
40 #include "cfile.h"
41 #include "digi.h"
42 #include "rbaudio.h"
43 #include "kconfig.h"
44 #include "timer.h"
45
46 song_info Songs[MAX_NUM_SONGS];
47 int Songs_initialized = 0;
48
49 #ifndef MACINTOSH
50 int Num_songs;
51 #endif
52
53 extern void digi_stop_current_song();
54
55 int Redbook_enabled = 1;
56
57 //0 if redbook is no playing, else the track number
58 int Redbook_playing = 0;
59
60 #define NumLevelSongs (Num_songs - SONG_FIRST_LEVEL_SONG)
61
62 extern int CD_blast_mixer();
63
64 #ifndef MACINTOSH
65 #define REDBOOK_VOLUME_SCALE  (255/3)           //255 is MAX
66 #else
67 #define REDBOOK_VOLUME_SCALE    (255)
68 #endif
69
70 //takes volume in range 0..8
71 void set_redbook_volume(int volume)
72 {
73         #ifndef MACINTOSH
74         RBASetVolume(0);                // makes the macs sound really funny
75         #endif
76         RBASetVolume(volume*REDBOOK_VOLUME_SCALE/8);
77 }
78
79 extern char CDROM_dir[];
80
81 void songs_init()
82 {
83         int i;
84         char inputline[80+1];
85         CFILE * fp;
86
87         if ( Songs_initialized ) return;
88
89
90         #if !defined(MACINTOSH) && !defined(WINDOWS)    // don't crank it if on a macintosh!!!!!
91                 if (!FindArg("-nomixer"))
92                         CD_blast_mixer();   // Crank it!
93         #endif
94
95
96         if (cfexist("descent.sng")) {   // mac (demo?) datafiles don't have the .sng file
97                 fp = cfopen( "descent.sng", "rb" );
98                 if ( fp == NULL )
99                 {
100                         Error( "Couldn't open descent.sng" );
101                 }
102                 i = 0;
103                 while (cfgets(inputline, 80, fp ))
104                 {
105                         if ( strlen( inputline ) )
106                         {
107                                 Assert( i < MAX_NUM_SONGS );
108                                 sscanf( inputline, "%s %s %s",
109                                                 Songs[i].filename,
110                                                 Songs[i].melodic_bank_file,
111                                                 Songs[i].drum_bank_file );
112                                 //printf( "%d. '%s' '%s' '%s'\n",i,Songs[i].filename,Songs[i].melodic_bank_file,Songs[i].drum_bank_file );
113                                 i++;
114                         }
115                 }
116                 Num_songs = i;
117                 if (Num_songs <= SONG_FIRST_LEVEL_SONG)
118                         Error("Must have at least %d songs",SONG_FIRST_LEVEL_SONG+1);
119                 cfclose(fp);
120         }
121
122         Songs_initialized = 1;
123
124         //      RBA Hook
125         #if !defined(SHAREWARE) || ( defined(SHAREWARE) && defined(APPLE_DEMO) )
126                 if (FindArg("-noredbook"))
127                 {
128                         Redbook_enabled = 0;
129                 }
130                 else    // use redbook
131                 {
132                         #ifndef __MSDOS__ // defined(WINDOWS) || defined(MACINTOSH)
133                                 RBAInit();
134                         #else
135                                 RBAInit(toupper(CDROM_dir[0]) - 'A');
136                         #endif
137
138                                 if (RBAEnabled())
139                         {
140                                 set_redbook_volume(Config_redbook_volume);
141                                 RBARegisterCD();
142                         }
143                 }
144                 atexit(RBAStop);    // stop song on exit
145         #endif  // endof ifndef SHAREWARE, ie ifdef SHAREWARE
146 }
147
148 #define FADE_TIME (f1_0/2)
149
150 //stop the redbook, so we can read off the CD
151 void songs_stop_redbook(void)
152 {
153         int old_volume = Config_redbook_volume*REDBOOK_VOLUME_SCALE/8;
154         fix old_time = timer_get_fixed_seconds();
155
156         if (Redbook_playing) {          //fade out volume
157                 int new_volume;
158                 do {
159                         fix t = timer_get_fixed_seconds();
160
161                         new_volume = fixmuldiv(old_volume,(FADE_TIME - (t-old_time)),FADE_TIME);
162
163                         if (new_volume < 0)
164                                 new_volume = 0;
165
166                         RBASetVolume(new_volume);
167
168                 } while (new_volume > 0);
169         }
170
171         RBAStop();                      // Stop CD, if playing
172
173         RBASetVolume(old_volume);       //restore volume
174
175         Redbook_playing = 0;            
176
177 }
178
179 //stop any songs - midi or redbook - that are currently playing
180 void songs_stop_all(void)
181 {
182         digi_stop_current_song();       // Stop midi song, if playing
183
184         songs_stop_redbook();                   // Stop CD, if playing
185 }
186
187 int force_rb_register=0;
188
189 void reinit_redbook()
190 {
191         #ifndef __MSDOS__ // defined(WINDOWS) || defined(MACINTOSH)
192                 RBAInit();
193         #else
194                 RBAInit(toupper(CDROM_dir[0]) - 'A');
195         #endif
196
197         if (RBAEnabled())
198         {
199                 set_redbook_volume(Config_redbook_volume);
200                 RBARegisterCD();
201                 force_rb_register=0;
202         }
203 }
204
205
206 //returns 1 if track started sucessfully
207 //start at tracknum.  if keep_playing set, play to end of disc.  else
208 //play only specified track
209 int play_redbook_track(int tracknum,int keep_playing)
210 {
211         Redbook_playing = 0;
212
213         if (!RBAEnabled() && Redbook_enabled && !FindArg("-noredbook"))
214                 reinit_redbook();
215
216         if (force_rb_register) {
217                 RBARegisterCD();                        //get new track list for new CD
218                 force_rb_register = 0;
219         }
220
221         if (Redbook_enabled && RBAEnabled()) {
222                 int num_tracks = RBAGetNumberOfTracks();
223                 if (tracknum <= num_tracks)
224                         if (RBAPlayTracks(tracknum,keep_playing?num_tracks:tracknum))  {
225                                 Redbook_playing = tracknum;
226                         }
227         }
228
229         return (Redbook_playing != 0);
230 }
231
232 /*
233  * Some of these have different Track listings!
234  * Which one is the "correct" order?
235  */
236 #define D2_1_DISCID         0x7d0ff809 // Descent II
237 #define D2_2_DISCID         0xe010a30e // Descent II
238 #define D2_3_DISCID         0xd410070d // Descent II
239 #define D2_4_DISCID         0xc610080d // Descent II
240 #define D2_DEF_DISCID       0x87102209 // Definitive collection Disc 2
241 #define D2_OEM_DISCID       0xac0bc30d // Destination: Quartzon
242 #define D2_OEM2_DISCID      0xc40c0a0d // Destination: Quartzon
243 #define D2_VERTIGO_DISCID   0x53078208 // Vertigo
244 #define D2_VERTIGO2_DISCID  0x64071408 // Vertigo + DMB
245 #define D2_MAC_DISCID       0xb70ee40e // Macintosh
246 #define D2_IPLAY_DISCID     0x22115710 // iPlay for Macintosh
247
248 #define REDBOOK_TITLE_TRACK         2
249 #define REDBOOK_CREDITS_TRACK       3
250 #define REDBOOK_FIRST_LEVEL_TRACK   (songs_haved2_cd()?4:1)
251
252 // songs_haved2_cd returns 1 if the descent 2 CD is in the drive and
253 // 0 otherwise
254
255 #if 1
256 int songs_haved2_cd()
257 {
258         int discid;
259
260         if (!Redbook_enabled)
261                 return 0;
262
263         discid = RBAGetDiscID();
264
265         switch (discid) {
266         case D2_1_DISCID:
267         case D2_2_DISCID:
268         case D2_3_DISCID:
269         case D2_4_DISCID:
270         case D2_DEF_DISCID:
271         case D2_OEM_DISCID:
272         case D2_OEM2_DISCID:
273         case D2_VERTIGO_DISCID:
274         case D2_VERTIGO2_DISCID:
275         case D2_MAC_DISCID:
276         case D2_IPLAY_DISCID:
277                 printf("Found D2 CD! discid: %x\n", discid);
278                 return 1;
279         default:
280                 printf("Unknown CD! discid: %x\n", discid);
281                 return 0;
282         }
283 }
284 #else
285 int songs_haved2_cd()
286 {
287         char temp[128],cwd[128];
288         
289         getcwd(cwd, 128);
290
291         strcpy(temp,CDROM_dir);
292
293         #ifndef MACINTOSH               //for PC, strip of trailing slash
294         if (temp[strlen(temp)-1] == '\\')
295                 temp[strlen(temp)-1] = 0;
296         #endif
297
298         if ( !chdir(temp) ) {
299                 chdir(cwd);
300                 return 1;
301         }
302
303         return 0;
304 }
305 #endif
306
307
308 void songs_play_song( int songnum, int repeat )
309 {
310         #ifndef SHAREWARE
311         //Assert(songnum != SONG_ENDLEVEL && songnum != SONG_ENDGAME);  //not in full version
312         #endif
313
314         if ( !Songs_initialized )
315                 songs_init();
316
317         //stop any music already playing
318
319         songs_stop_all();
320
321         //do we want any of these to be redbook songs?
322
323         if (force_rb_register) {
324                 RBARegisterCD();                        //get new track list for new CD
325                 force_rb_register = 0;
326         }
327
328         if (songnum == SONG_TITLE)
329                 play_redbook_track(REDBOOK_TITLE_TRACK,0);
330         else if (songnum == SONG_CREDITS)
331                 play_redbook_track(REDBOOK_CREDITS_TRACK,0);
332
333         if (!Redbook_playing) {         //not playing redbook, so play midi
334
335                 #ifndef MACINTOSH
336                         digi_play_midi_song( Songs[songnum].filename, Songs[songnum].melodic_bank_file, Songs[songnum].drum_bank_file, repeat );
337                 #else
338                         digi_play_midi_song(songnum, repeat);
339                 #endif
340         }
341 }
342
343 int current_song_level;
344
345 void songs_play_level_song( int levelnum )
346 {
347         int songnum;
348         int n_tracks;
349
350         Assert( levelnum != 0 );
351
352         if ( !Songs_initialized )
353                 songs_init();
354
355         songs_stop_all();
356
357         current_song_level = levelnum;
358
359         songnum = (levelnum>0)?(levelnum-1):(-levelnum);
360
361         if (!RBAEnabled() && Redbook_enabled && !FindArg("-noredbook"))
362                 reinit_redbook();
363
364         if (force_rb_register) {
365                 RBARegisterCD();                        //get new track list for new CD
366                 force_rb_register = 0;
367         }
368
369         if (Redbook_enabled && RBAEnabled() && (n_tracks = RBAGetNumberOfTracks()) > 1) {
370
371                 //try to play redbook
372
373                 mprintf((0,"n_tracks = %d\n",n_tracks));
374
375                 play_redbook_track(REDBOOK_FIRST_LEVEL_TRACK + (songnum % (n_tracks-REDBOOK_FIRST_LEVEL_TRACK+1)),1);
376         }
377
378         if (! Redbook_playing) {                        //not playing redbook, so play midi
379
380                 songnum = SONG_FIRST_LEVEL_SONG + (songnum % NumLevelSongs);
381
382                 #ifndef MACINTOSH
383                         digi_play_midi_song( Songs[songnum].filename, Songs[songnum].melodic_bank_file, Songs[songnum].drum_bank_file, 1 );
384                 #else
385                         digi_play_midi_song( songnum, 1 );
386                 #endif
387
388         }
389 }
390
391 //this should be called regularly to check for redbook restart
392 void songs_check_redbook_repeat()
393 {
394         static fix last_check_time;
395         fix current_time;
396
397         if (!Redbook_playing || Config_redbook_volume==0) return;
398
399         current_time = timer_get_fixed_seconds();
400         if (current_time < last_check_time || (current_time - last_check_time) >= F2_0) {
401                 if (!RBAPeekPlayStatus()) {
402                         stop_time();
403                         // if title ends, start credit music
404                         // if credits music ends, restart it
405                         if (Redbook_playing == REDBOOK_TITLE_TRACK || Redbook_playing == REDBOOK_CREDITS_TRACK)
406                                 play_redbook_track(REDBOOK_CREDITS_TRACK,0);
407                         else {
408                                 //songs_goto_next_song();
409         
410                                 //new code plays all tracks to end of disk, so if disk has
411                                 //stopped we must be at end.  So start again with level 1 song.
412         
413                                 songs_play_level_song(1);
414                         }
415                         start_time();
416                 }
417                 last_check_time = current_time;
418         }
419 }
420
421 //goto the next level song
422 void songs_goto_next_song()
423 {
424         if (Redbook_playing)            //get correct track
425                 current_song_level = RBAGetTrackNum() - REDBOOK_FIRST_LEVEL_TRACK + 1;
426
427         songs_play_level_song(current_song_level+1);
428
429 }
430
431 //goto the previous level song
432 void songs_goto_prev_song()
433 {
434         if (Redbook_playing)            //get correct track
435                 current_song_level = RBAGetTrackNum() - REDBOOK_FIRST_LEVEL_TRACK + 1;
436
437         if (current_song_level > 1)
438                 songs_play_level_song(current_song_level-1);
439
440 }
441