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