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