]> icculus.org git repositories - btb/d2x.git/blob - main/mission.c
Fixes to build system
[btb/d2x.git] / main / mission.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: mission.c,v 1.1.1.1 2001-01-19 03:30:05 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include "pstypes.h"
27 #include "cfile.h"
28
29 #include "strutil.h"
30 #include "inferno.h"
31 #include "mission.h"
32 #include "gameseq.h"
33 #include "titles.h"
34 #include "songs.h"
35 #include "mono.h"
36 #include "error.h"
37 #include "findfile.h"
38
39 mle Mission_list[MAX_MISSIONS];
40
41 int Current_mission_num;
42 int     N_secret_levels;                //      Made a global by MK for scoring purposes.  August 1, 1995.
43 char *Current_mission_filename,*Current_mission_longname;
44
45 //this stuff should get defined elsewhere
46
47 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
48 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
49
50 //where the missions go
51 #ifndef EDITOR
52 #define MISSION_DIR "missions/"
53 #else
54 #define MISSION_DIR "./"
55 #endif
56
57 #ifdef SHAREWARE
58
59 //
60 //  Special versions of mission routines for shareware
61 //
62
63 #define SHAREWARE_MISSION_FILENAME      "d2demo"
64 #define SHAREWARE_MISSION_NAME          "Descent 2 Demo"
65
66 int build_mission_list(int anarchy_mode)
67 {
68         anarchy_mode++;         //kill warning
69
70         strcpy(Mission_list[0].filename,SHAREWARE_MISSION_FILENAME);
71         strcpy(Mission_list[0].mission_name,SHAREWARE_MISSION_NAME);
72         Mission_list[0].anarchy_only_flag = 0;
73
74         return load_mission(0);
75 }
76
77 int load_mission(int mission_num)
78 {
79         Assert(mission_num == 0);
80
81         Current_mission_num = 0;
82         Current_mission_filename = Mission_list[0].filename;
83         Current_mission_longname = Mission_list[0].mission_name;
84
85         N_secret_levels = 0;
86
87         Assert(Last_level == 3);
88
89 #ifdef MACINTOSH        // mac demo is using the regular hog and rl2 files
90         strcpy(Level_names[0],"d2leva-1.rl2");
91         strcpy(Level_names[1],"d2leva-2.rl2");
92         strcpy(Level_names[2],"d2leva-3.rl2");
93 #else
94         strcpy(Level_names[0],"d2leva-1.sl2");
95         strcpy(Level_names[1],"d2leva-2.sl2");
96         strcpy(Level_names[2],"d2leva-3.sl2");
97 #endif  // end of ifdef macintosh
98
99         return 1;
100 }
101
102 int load_mission_by_name(char *mission_name)
103 {
104         if (strcmp(mission_name,SHAREWARE_MISSION_FILENAME))
105                 return 0;               //cannot load requested mission
106         else
107                 return load_mission(0);
108
109 }
110
111
112
113 #else
114
115 #if defined(D2_OEM)
116
117 //
118 //  Special versions of mission routines for Diamond/S3 version
119 //
120
121 #define OEM_MISSION_FILENAME    "d2"
122 #define OEM_MISSION_NAME                "D2 Destination:Quartzon"
123
124 int build_mission_list(int anarchy_mode)
125 {
126         anarchy_mode++;         //kill warning
127
128         strcpy(Mission_list[0].filename,OEM_MISSION_FILENAME);
129         strcpy(Mission_list[0].mission_name,OEM_MISSION_NAME);
130         Mission_list[0].anarchy_only_flag = 0;
131
132         return load_mission(0);
133 }
134
135 int load_mission(int mission_num)
136 {
137         Assert(mission_num == 0);
138
139         Current_mission_num = 0;
140         Current_mission_filename = Mission_list[0].filename;
141         Current_mission_longname = Mission_list[0].mission_name;
142
143         N_secret_levels = 2;
144
145         Assert(Last_level == 8);
146
147         strcpy(Level_names[0],"d2leva-1.rl2");
148         strcpy(Level_names[1],"d2leva-2.rl2");
149         strcpy(Level_names[2],"d2leva-3.rl2");
150         strcpy(Level_names[3],"d2leva-4.rl2");
151
152         strcpy(Secret_level_names[0],"d2leva-s.rl2");
153
154         strcpy(Level_names[4],"d2levb-1.rl2");
155         strcpy(Level_names[5],"d2levb-2.rl2");
156         strcpy(Level_names[6],"d2levb-3.rl2");
157         strcpy(Level_names[7],"d2levb-4.rl2");
158
159         strcpy(Secret_level_names[1],"d2levb-s.rl2");
160
161         Secret_level_table[0] = 1;
162         Secret_level_table[1] = 5;
163
164         return 1;
165 }
166
167 int load_mission_by_name(char *mission_name)
168 {
169         if (strcmp(mission_name,OEM_MISSION_FILENAME))
170                 return 0;               //cannot load requested mission
171         else
172                 return load_mission(0);
173
174 }
175
176 #else
177
178 //strips damn newline from end of line
179 char *mfgets(char *s,int n,CFILE *f)
180 {
181         char *r;
182
183         r = cfgets(s,n,f);
184         if (r && s[strlen(s)-1] == '\n')
185                 s[strlen(s)-1] = 0;
186
187         return r;
188 }
189
190 //compare a string for a token. returns true if match
191 int istok(char *buf,char *tok)
192 {
193         return strnicmp(buf,tok,strlen(tok)) == 0;
194
195 }
196
197 //adds a terminating 0 after a string at the first white space
198 void add_term(char *s)
199 {
200         while (*s && !isspace(*s)) s++;
201
202         *s = 0;         //terminate!
203 }
204
205 //returns ptr to string after '=' & white space, or NULL if no '='
206 //adds 0 after parm at first white space
207 char *get_value(char *buf)
208 {
209         char *t;
210
211         t = strchr(buf,'=')+1;
212
213         if (t) {
214                 while (*t && isspace(*t)) t++;
215
216                 if (*t)
217                         return t;
218         }
219
220         return NULL;            //error!
221 }
222
223 //reads a line, returns ptr to value of passed parm.  returns NULL if none
224 char *get_parm_value(char *parm,CFILE *f)
225 {
226         static char buf[80];
227         
228         if (!mfgets(buf,80,f))
229                 return NULL;
230
231         if (istok(buf,parm))
232                 return get_value(buf);
233         else
234                 return NULL;
235 }
236
237 int ml_sort_func(mle *e0,mle *e1)
238 {
239         return stricmp(e0->mission_name,e1->mission_name);
240
241 }
242
243 extern char CDROM_dir[];
244 extern int HoardEquipped();
245
246 #define BUILTIN_MISSION "d2.mn2"
247
248 //returns 1 if file read ok, else 0
249 int read_mission_file(char *filename,int count,int location)
250 {
251         char filename2[100];
252         CFILE *mfile;
253
254         switch (location) {
255                 case ML_MISSIONDIR:
256                         strcpy(filename2,MISSION_DIR);  
257                         break;
258
259                 case ML_CDROM:                  
260                         songs_stop_redbook();           //so we can read from the CD
261                         strcpy(filename2,CDROM_dir);            
262                         break;
263
264                 default:                                        
265                         Int3();         //fall through
266
267                 case ML_CURDIR:         
268                         strcpy(filename2,"");
269                         break;
270         }
271         strcat(filename2,filename);
272
273         mfile = cfopen(filename2,"rb");
274
275         if (mfile) {
276                 char *p;
277                 char temp[FILENAME_LEN],*t;
278
279                 strcpy(temp,filename);
280                 if ((t = strchr(temp,'.')) == NULL)
281                         return 0;       //missing extension
282                 *t = 0;                 //kill extension
283
284                 strncpy( Mission_list[count].filename, temp, 9 );
285                 Mission_list[count].anarchy_only_flag = 0;
286                 Mission_list[count].location = location;
287
288                 p = get_parm_value("name",mfile);
289
290                 if (!p) {               //try enhanced mission
291                         cfseek(mfile,0,SEEK_SET);
292                         p = get_parm_value("xname",mfile);
293                 }
294
295 #ifdef NETWORK
296                 if (HoardEquipped())
297                 {
298                         if (!p) {               //try super-enhanced mission!
299                                 cfseek(mfile,0,SEEK_SET);
300                                 p = get_parm_value("zname",mfile);
301                         }
302                 }
303 #endif
304
305                 if (p) {
306                         char *t;
307                         if ((t=strchr(p,';'))!=NULL)
308                                 *t=0;
309                         t = p + strlen(p)-1;
310                         while (isspace(*t)) t--;
311                         strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
312                 }
313                 else {
314                         cfclose(mfile);
315                         return 0;
316                 }
317
318                 p = get_parm_value("type",mfile);
319
320                 //get mission type 
321                 if (p)
322                         Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
323
324                 cfclose(mfile);
325
326                 return 1;
327         }
328
329         return 0;
330 }
331
332
333 //fills in the global list of missions.  Returns the number of missions
334 //in the list.  If anarchy_mode set, don't include non-anarchy levels.
335 //if there is only one mission, this function will call load_mission on it.
336
337 extern char CDROM_dir[];
338
339 int build_mission_list(int anarchy_mode)
340 {
341         static int num_missions=-1;
342         int count=0,special_count=0;
343         FILEFINDSTRUCT find;
344         char search_name[100] = MISSION_DIR "*.mn2";
345
346         //now search for levels on disk
347
348 //@@Took out this code because after this routine was called once for
349 //@@a list of single-player missions, a subsequent call for a list of
350 //@@anarchy missions would not scan again, and thus would not find the
351 //@@anarchy-only missions.  If we retain the minimum level of install,
352 //@@we may want to put the code back in, having it always scan for all
353 //@@missions, and have the code that uses it sort out the ones it wants.
354 //@@    if (num_missions != -1) {
355 //@@            if (Current_mission_num != 0)
356 //@@                    load_mission(0);                                //set built-in mission as default
357 //@@            return num_missions;
358 //@@    }
359
360         if (!read_mission_file(BUILTIN_MISSION,0,ML_CURDIR))            //read built-in first
361                 Error("Could not find required mission file <%s>",BUILTIN_MISSION);
362
363         special_count = count=1; 
364
365         if( !FileFindFirst( search_name, &find ) ) {
366                 do      {
367                         if (stricmp(find.name,BUILTIN_MISSION)==0)
368                                 continue;               //skip the built-in
369
370                         if (read_mission_file(find.name,count,ML_MISSIONDIR)) {
371
372                                 if (anarchy_mode || !Mission_list[count].anarchy_only_flag)
373                                         count++;
374                         }
375
376                 } while( !FileFindNext( &find ) && count<MAX_MISSIONS);
377                 FileFindClose();
378         }
379
380         //move vertigo to top of mission list
381         {
382                 int i;
383
384                 for (i=special_count;i<count;i++)
385                         if (!stricmp(Mission_list[i].filename,"D2X")) {         //swap!
386                                 mle temp;
387
388                                 temp = Mission_list[special_count];
389                                 Mission_list[special_count] = Mission_list[i];
390                                 Mission_list[i] = temp;
391
392                                 special_count++;
393
394                                 break;
395                         }
396         }
397
398
399         if (count>special_count)
400                 qsort(&Mission_list[special_count],count-special_count,sizeof(*Mission_list),
401                                 (int (*)( const void *, const void * ))ml_sort_func);
402
403         load_mission(0);                        //set built-in mission as default
404
405         num_missions = count;
406
407         return count;
408 }
409
410 void init_extra_robot_movie(char *filename);
411
412 //values for built-in mission
413
414 //loads the specfied mission from the mission list.  build_mission_list()
415 //must have been called.  If build_mission_list() returns 0, this function
416 //does not need to be called.  Returns true if mission loaded ok, else false.
417 int load_mission(int mission_num)
418 {
419         CFILE *mfile;
420         char buf[80], *v;
421         int found_hogfile;
422         int enhanced_mission = 0;
423
424         Current_mission_num = mission_num;
425
426         mprintf(( 0, "Loading mission %d\n", mission_num ));
427
428         //read mission from file 
429
430         switch (Mission_list[mission_num].location) {
431                 case ML_MISSIONDIR:     strcpy(buf,MISSION_DIR);        break;
432                 case ML_CDROM:                  strcpy(buf,CDROM_dir);          break;
433                 default:                                        Int3();                                                 //fall through
434                 case ML_CURDIR:         strcpy(buf,"");                         break;
435         }
436         strcat(buf,Mission_list[mission_num].filename);
437         strcat(buf,".mn2");
438
439         mfile = cfopen(buf,"rb");
440         if (mfile == NULL) {
441                 Current_mission_num = -1;
442                 return 0;               //error!
443         }
444
445         if (mission_num != 0) {         //for non-builtin missions, load HOG
446
447                 strcpy(buf+strlen(buf)-4,".hog");               //change extension
448
449                 found_hogfile = cfile_use_alternate_hogfile(buf);
450
451                 #ifdef RELEASE                          //for release, require mission to be in hogfile
452                 if (! found_hogfile) {
453                         cfclose(mfile);
454                         Current_mission_num = -1;
455                         return 0;
456                 }
457                 #endif
458         }
459
460         //init vars
461         Last_level =            0;
462         Last_secret_level = 0;
463
464         while (mfgets(buf,80,mfile)) {
465
466                 if (istok(buf,"name"))
467                         continue;                                               //already have name, go to next line
468                 if (istok(buf,"xname")) {
469                         enhanced_mission = 1;
470                         continue;                                               //already have name, go to next line
471                 }
472                 if (istok(buf,"zname")) {
473                         enhanced_mission = 2;
474                         continue;                                               //already have name, go to next line
475                 }
476                 else if (istok(buf,"type"))
477                         continue;                                               //already have name, go to next line                            
478                 else if (istok(buf,"hog")) {
479                         char    *bufp = buf;
480
481                         while (*(bufp++) != '=')
482                                 ;
483
484                         if (*bufp == ' ')
485                                 while (*(++bufp) == ' ')
486                                         ;
487
488                         cfile_use_alternate_hogfile(bufp);
489                         mprintf((0, "Hog file override = [%s]\n", bufp));
490                 }
491                 else if (istok(buf,"num_levels")) {
492
493                         if ((v=get_value(buf))!=NULL) {
494                                 int n_levels,i;
495
496                                 n_levels = atoi(v);
497
498                                 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
499
500                                         add_term(buf);
501                                         if (strlen(buf) <= 12) {
502                                                 strcpy(Level_names[i],buf);
503                                                 Last_level++;
504                                         }
505                                         else
506                                                 break;
507                                 }
508
509                         }
510                 }
511                 else if (istok(buf,"num_secrets")) {
512                         if ((v=get_value(buf))!=NULL) {
513                                 int i;
514
515                                 N_secret_levels = atoi(v);
516
517                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
518
519                                 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
520                                         char *t;
521
522                                         
523                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
524                                         else
525                                                 break;
526
527                                         add_term(buf);
528                                         if (strlen(buf) <= 12) {
529                                                 strcpy(Secret_level_names[i],buf);
530                                                 Secret_level_table[i] = atoi(t);
531                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
532                                                         break;
533                                                 Last_secret_level--;
534                                         }
535                                         else
536                                                 break;
537                                 }
538
539                         }
540                 }
541
542         }
543
544         cfclose(mfile);
545
546         if (Last_level <= 0) {
547                 Current_mission_num = -1;               //no valid mission loaded
548                 return 0;
549         }
550
551         Current_mission_filename = Mission_list[Current_mission_num].filename;
552         Current_mission_longname = Mission_list[Current_mission_num].mission_name;
553
554         if (enhanced_mission) {
555                 char t[50];
556                 extern void bm_read_extra_robots();
557                 sprintf(t,"%s.ham",Current_mission_filename);
558                 bm_read_extra_robots(t,enhanced_mission);
559                 strncpy(t,Current_mission_filename,6);
560                 strcat(t,"-l.mvl");
561                 init_extra_robot_movie(t);
562         }
563
564         return 1;
565 }
566
567 //loads the named mission if exists.
568 //Returns true if mission loaded ok, else false.
569 int load_mission_by_name(char *mission_name)
570 {
571         int n,i;
572
573         n = build_mission_list(1);
574
575         for (i=0;i<n;i++)
576                 if (!stricmp(mission_name,Mission_list[i].filename))
577                         return load_mission(i);
578
579         return 0;               //couldn't find mission
580 }
581
582 #endif
583 #endif
584