load d1 oem mission, d1 v1.0 mission
[btb/d2x.git] / main / mission.c
1 /* $Id: mission.c,v 1.19 2003-02-26 11:03:27 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  * Code to handle multiple missions
18  *
19  * Old Log:
20  * Revision 1.4  1995/10/31  10:21:40  allender
21  * no mission support in shareware
22  *
23  * Revision 1.3  1995/10/21  22:53:04  allender
24  * moved missions to data folder
25  *
26  * Revision 1.2  1995/09/13  08:47:29  allender
27  * made to work with Chris' direct stuff
28  *
29  * Revision 1.1  1995/05/16  15:27:48  allender
30  * Initial revision
31  *
32  * Revision 2.9  1995/05/26  16:16:32  john
33  * Split SATURN into define's for requiring cd, using cd, etc.
34  * Also started adding all the Rockwell stuff.
35  *
36  * Revision 2.8  1995/03/20  15:49:31  mike
37  * Remove eof char from comment which confused make depend, causing
38  * no mission.obj: line in makefile.  Pretty stupid tool, huh?
39  *
40  * Revision 2.7  1995/03/20  12:12:11  john
41  * Added ifdef SATURN.
42  *
43  * Revision 2.6  1995/03/15  14:32:49  john
44  * Added code to force the Descent CD-rom in the drive.
45  *
46  * Revision 2.5  1995/03/15  11:41:15  john
47  * Better Saturn CD-ROM support.
48  *
49  * Revision 2.4  1995/03/14  18:24:14  john
50  * Force Destination Saturn to use CD-ROM drive.
51  *
52  * Revision 2.3  1995/03/07  14:19:41  mike
53  * More destination saturn stuff.
54  *
55  * Revision 2.2  1995/03/06  23:09:03  mike
56  * more saturn stuff: make briefings work for saturn mission.
57  *
58  * Revision 2.1  1995/03/06  16:47:48  mike
59  * destination saturn
60  *
61  * Revision 2.0  1995/02/27  11:27:49  john
62  * New version 2.0, which has no anonymous unions, builds with
63  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
64  *
65  * Revision 1.14  1995/02/15  11:30:37  john
66  * Fixed bug with potential mem overwrite on line 160.
67  *
68  * Revision 1.13  1995/02/10  17:53:20  matt
69  * Changed mission name again
70  *
71  * Revision 1.12  1995/02/10  17:35:38  matt
72  * Changed name of built-in mission
73  *
74  * Revision 1.11  1995/02/07  17:13:51  allender
75  * removed return statement in load_mission so that built in mission will
76  * actually set the Current_mission_name and _filename
77  *
78  * Revision 1.10  1995/01/31  01:19:45  matt
79  * Made build_mission_list() sort missions by name
80  *
81  * Revision 1.9  1995/01/30  13:49:58  allender
82  * changed build_mission_list in load_mission_by_name to include
83  * anarchy levels.
84  *
85  * Revision 1.8  1995/01/30  13:03:51  matt
86  * Fixed dumb mistake
87  *
88  * Revision 1.7  1995/01/30  12:55:22  matt
89  * Added vars to point to mission names
90  *
91  * Revision 1.6  1995/01/22  18:57:28  matt
92  * Made player highest level work with missions
93  *
94  * Revision 1.5  1995/01/22  14:13:08  matt
95  * Added flag in mission list for anarchy-only missions
96  *
97  * Revision 1.4  1995/01/21  23:13:12  matt
98  * Made high scores with (not work, really) with loaded missions
99  * Don't give player high score when quit game
100  *
101  * Revision 1.3  1995/01/21  16:27:12  matt
102  * Made endlevel briefing work with missions
103  *
104  * Revision 1.2  1995/01/20  22:47:50  matt
105  * Mission system implemented, though imcompletely
106  *
107  * Revision 1.1  1995/01/20  13:42:19  matt
108  * Initial revision
109  *
110  *
111  */
112
113 #ifdef HAVE_CONFIG_H
114 #include <conf.h>
115 #endif
116
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include <ctype.h>
121 #include <limits.h>
122
123 #include "pstypes.h"
124 #include "cfile.h"
125
126 #include "strutil.h"
127 #include "inferno.h"
128 #include "mission.h"
129 #include "gameseq.h"
130 #include "titles.h"
131 #include "songs.h"
132 #include "mono.h"
133 #include "error.h"
134 #include "findfile.h"
135 #include "hoard.h"
136
137 mle Mission_list[MAX_MISSIONS];
138
139 int Current_mission_num;
140 int N_secret_levels;    // Made a global by MK for scoring purposes.  August 1, 1995.
141 char *Current_mission_filename,*Current_mission_longname;
142
143 int Builtin_mission_num;
144 char Builtin_mission_filename[9];
145 int Builtin_mission_hogsize;
146
147 int D1_Builtin_mission_num;
148 char D1_Builtin_mission_filename[9];
149 int D1_Builtin_mission_hogsize;
150
151 //this stuff should get defined elsewhere
152
153 char Level_names[MAX_LEVELS_PER_MISSION][FILENAME_LEN];
154 char Secret_level_names[MAX_SECRET_LEVELS_PER_MISSION][FILENAME_LEN];
155
156 //where the missions go
157 #ifndef EDITOR
158 #define MISSION_DIR "missions/"
159 #else
160 #define MISSION_DIR "./"
161 #endif
162
163 //values for d1 built-in mission
164 #define BIM_LAST_LEVEL          27
165 #define BIM_LAST_SECRET_LEVEL   -3
166 #define BIM_BRIEFING_FILE       "briefing.tex"
167 #define BIM_ENDING_FILE         "endreg.tex"
168
169 //
170 //  Special versions of mission routines for d1 builtins
171 //
172
173 int load_mission_d1(int mission_num)
174 {
175         int i;
176
177         cfile_use_descent1_hogfile("descent.hog");
178
179         Current_mission_num = mission_num;
180         Current_mission_filename = Mission_list[mission_num].filename;
181         Current_mission_longname = Mission_list[mission_num].mission_name;
182
183         switch (D1_Builtin_mission_hogsize) {
184         case D1_SHAREWARE_MISSION_HOGSIZE:
185         case D1_SHAREWARE_10_MISSION_HOGSIZE:
186                 N_secret_levels = 0;
187
188                 Last_level = 7;
189                 Last_secret_level = 0;
190
191                 //build level names
192                 for (i=0;i<Last_level;i++)
193                         sprintf(Level_names[i], "level%02d.sdl", i+1);
194
195                 break;
196         case D1_MAC_SHARE_MISSION_HOGSIZE:
197                 N_secret_levels = 0;
198
199                 Last_level = 3;
200                 Last_secret_level = 0;
201
202                 //build level names
203                 for (i=0;i<Last_level;i++)
204                         sprintf(Level_names[i], "level%02d.sdl", i+1);
205
206                 break;
207         case D1_OEM_MISSION_HOGSIZE:
208                 N_secret_levels = 1;
209
210                 Last_level = 15;
211                 Last_secret_level = -1;
212
213                 //build level names
214                 for (i=0; i < Last_level - 1; i++)
215                         sprintf(Level_names[i], "level%02d.rdl", i+1);
216                 sprintf(Level_names[i], "saturn%02d.rdl", i+1);
217                 for (i=0; i < -Last_secret_level; i++)
218                         sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
219
220                 Secret_level_table[0] = 10;
221
222                 break;
223         default:
224                 Int3(); // fall through
225         case D1_MISSION_HOGSIZE:
226         case D1_10_MISSION_HOGSIZE:
227         case D1_MAC_MISSION_HOGSIZE:
228                 N_secret_levels = 3;
229
230                 Last_level = BIM_LAST_LEVEL;
231                 Last_secret_level = BIM_LAST_SECRET_LEVEL;
232
233                 //build level names
234                 for (i=0;i<Last_level;i++)
235                         sprintf(Level_names[i], "level%02d.rdl", i+1);
236                 for (i=0;i<-Last_secret_level;i++)
237                         sprintf(Secret_level_names[i], "levels%1d.rdl", i+1);
238
239                 Secret_level_table[0] = 10;
240                 Secret_level_table[1] = 21;
241                 Secret_level_table[2] = 24;
242
243                 break;
244         }
245         strcpy(Briefing_text_filename,BIM_BRIEFING_FILE);
246         strcpy(Ending_text_filename,BIM_ENDING_FILE);
247
248         return 1;
249 }
250
251
252 //
253 //  Special versions of mission routines for shareware
254 //
255
256 int load_mission_shareware(int mission_num)
257 {
258         Current_mission_num = mission_num;
259         Current_mission_filename = Mission_list[mission_num].filename;
260         Current_mission_longname = Mission_list[mission_num].mission_name;
261
262         N_secret_levels = 0;
263
264         Last_level = 3;
265         Last_secret_level = 0;
266
267         switch (Builtin_mission_hogsize) {
268         case MAC_SHARE_MISSION_HOGSIZE:
269                 // mac demo is using the regular hog and rl2 files
270                 strcpy(Level_names[0],"d2leva-1.rl2");
271                 strcpy(Level_names[1],"d2leva-2.rl2");
272                 strcpy(Level_names[2],"d2leva-3.rl2");
273                 break;
274         default:
275                 Int3(); // fall through
276         case SHAREWARE_MISSION_HOGSIZE:
277                 strcpy(Level_names[0],"d2leva-1.sl2");
278                 strcpy(Level_names[1],"d2leva-2.sl2");
279                 strcpy(Level_names[2],"d2leva-3.sl2");
280         }
281
282         return 1;
283 }
284
285
286 //
287 //  Special versions of mission routines for Diamond/S3 version
288 //
289
290 int load_mission_oem(int mission_num)
291 {
292         Current_mission_num = mission_num;
293         Current_mission_filename = Mission_list[mission_num].filename;
294         Current_mission_longname = Mission_list[mission_num].mission_name;
295
296         N_secret_levels = 2;
297
298         Last_level = 8;
299         Last_secret_level = -2;
300
301         strcpy(Level_names[0],"d2leva-1.rl2");
302         strcpy(Level_names[1],"d2leva-2.rl2");
303         strcpy(Level_names[2],"d2leva-3.rl2");
304         strcpy(Level_names[3],"d2leva-4.rl2");
305
306         strcpy(Secret_level_names[0],"d2leva-s.rl2");
307
308         strcpy(Level_names[4],"d2levb-1.rl2");
309         strcpy(Level_names[5],"d2levb-2.rl2");
310         strcpy(Level_names[6],"d2levb-3.rl2");
311         strcpy(Level_names[7],"d2levb-4.rl2");
312
313         strcpy(Secret_level_names[1],"d2levb-s.rl2");
314
315         Secret_level_table[0] = 1;
316         Secret_level_table[1] = 5;
317
318         return 1;
319 }
320
321
322 //strips damn newline from end of line
323 char *mfgets(char *s,int n,CFILE *f)
324 {
325         char *r;
326
327         r = cfgets(s,n,f);
328         if (r && s[strlen(s)-1] == '\n')
329                 s[strlen(s)-1] = 0;
330
331         return r;
332 }
333
334 //compare a string for a token. returns true if match
335 int istok(char *buf,char *tok)
336 {
337         return strnicmp(buf,tok,strlen(tok)) == 0;
338
339 }
340
341 //adds a terminating 0 after a string at the first white space
342 void add_term(char *s)
343 {
344         while (*s && !isspace(*s)) s++;
345
346         *s = 0;         //terminate!
347 }
348
349 //returns ptr to string after '=' & white space, or NULL if no '='
350 //adds 0 after parm at first white space
351 char *get_value(char *buf)
352 {
353         char *t;
354
355         t = strchr(buf,'=')+1;
356
357         if (t) {
358                 while (*t && isspace(*t)) t++;
359
360                 if (*t)
361                         return t;
362         }
363
364         return NULL;            //error!
365 }
366
367 //reads a line, returns ptr to value of passed parm.  returns NULL if none
368 char *get_parm_value(char *parm,CFILE *f)
369 {
370         static char buf[80];
371
372         if (!mfgets(buf,80,f))
373                 return NULL;
374
375         if (istok(buf,parm))
376                 return get_value(buf);
377         else
378                 return NULL;
379 }
380
381 int ml_sort_func(mle *e0,mle *e1)
382 {
383         return stricmp(e0->mission_name,e1->mission_name);
384
385 }
386
387 extern char CDROM_dir[];
388
389 //returns 1 if file read ok, else 0
390 int read_mission_file(char *filename,int count,int location)
391 {
392         char filename2[100];
393         CFILE *mfile;
394
395         //printf("reading: %s\n", filename);
396
397         switch (location) {
398                 case ML_MISSIONDIR:
399                         strcpy(filename2,MISSION_DIR);
400                         break;
401
402                 case ML_CDROM:
403                         songs_stop_redbook();           //so we can read from the CD
404                         strcpy(filename2,CDROM_dir);
405                         break;
406
407                 default:
408                         Int3();         //fall through
409
410                 case ML_CURDIR:
411                         strcpy(filename2,"");
412                         break;
413         }
414         strcat(filename2,filename);
415
416         mfile = cfopen(filename2,"rb");
417
418         if (mfile) {
419                 char *p;
420                 char temp[FILENAME_LEN],*t;
421
422                 strcpy(temp,filename);
423                 if ((t = strchr(temp,'.')) == NULL)
424                         return 0;       //missing extension
425                 // look if it's .mn2 or .msn
426                 Mission_list[count].descent_version = (t[3] == '2') ? 2 : 1;
427                 *t = 0;                 //kill extension
428
429                 strncpy( Mission_list[count].filename, temp, 9 );
430                 Mission_list[count].anarchy_only_flag = 0;
431                 Mission_list[count].location = location;
432
433                 p = get_parm_value("name",mfile);
434
435                 if (!p) {               //try enhanced mission
436                         cfseek(mfile,0,SEEK_SET);
437                         p = get_parm_value("xname",mfile);
438                 }
439
440                 if (HoardEquipped())
441                 {
442                         if (!p) {               //try super-enhanced mission!
443                                 cfseek(mfile,0,SEEK_SET);
444                                 p = get_parm_value("zname",mfile);
445                         }
446                 }
447
448                 if (p) {
449                         char *t;
450                         if ((t=strchr(p,';'))!=NULL)
451                                 *t=0;
452                         t = p + strlen(p)-1;
453                         while (isspace(*t)) t--;
454                         strncpy(Mission_list[count].mission_name,p,MISSION_NAME_LEN);
455                 }
456                 else {
457                         cfclose(mfile);
458                         return 0;
459                 }
460
461                 p = get_parm_value("type",mfile);
462
463                 //get mission type
464                 if (p)
465                         Mission_list[count].anarchy_only_flag = istok(p,"anarchy");
466
467                 cfclose(mfile);
468
469                 return 1;
470         }
471
472         return 0;
473 }
474
475 void add_d1_builtin_mission_to_list(int *count)
476 {
477         if (!cfexist("descent.hog"))
478                 return;
479
480         D1_Builtin_mission_hogsize = cfile_size("descent.hog");
481
482         switch (D1_Builtin_mission_hogsize) {
483         case D1_SHAREWARE_MISSION_HOGSIZE:
484         case D1_SHAREWARE_10_MISSION_HOGSIZE:
485         case D1_MAC_SHARE_MISSION_HOGSIZE:
486                 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
487                 strcpy(Mission_list[*count].mission_name, D1_SHAREWARE_MISSION_NAME);
488                 Mission_list[*count].anarchy_only_flag = 0;
489                 break;
490         case D1_OEM_MISSION_HOGSIZE:
491                 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
492                 strcpy(Mission_list[*count].mission_name, D1_OEM_MISSION_NAME);
493                 Mission_list[*count].anarchy_only_flag = 0;
494                 break;
495         default:
496                 Warning("Unknown D1 hogsize %d\n", D1_Builtin_mission_hogsize);
497                 Int3();
498                 // fall through
499         case D1_MISSION_HOGSIZE:
500         case D1_10_MISSION_HOGSIZE:
501         case D1_MAC_MISSION_HOGSIZE:
502                 strcpy(Mission_list[*count].filename, D1_MISSION_FILENAME);
503                 strcpy(Mission_list[*count].mission_name, D1_MISSION_NAME);
504                 Mission_list[*count].anarchy_only_flag = 0;
505                 break;
506         }
507
508         strcpy(D1_Builtin_mission_filename, Mission_list[*count].filename);
509         Mission_list[*count].descent_version = 1;
510         Mission_list[*count].anarchy_only_flag = 0;
511         ++(*count);
512 }
513
514
515 void add_builtin_mission_to_list(int *count)
516 {
517         Builtin_mission_hogsize = cfile_size("descent2.hog");
518         if (Builtin_mission_hogsize == -1)
519                 Builtin_mission_hogsize = cfile_size("d2demo.hog");
520
521         switch (Builtin_mission_hogsize) {
522         case SHAREWARE_MISSION_HOGSIZE:
523         case MAC_SHARE_MISSION_HOGSIZE:
524                 strcpy(Mission_list[*count].filename,SHAREWARE_MISSION_FILENAME);
525                 strcpy(Mission_list[*count].mission_name,SHAREWARE_MISSION_NAME);
526                 Mission_list[*count].anarchy_only_flag = 0;
527                 break;
528         case OEM_MISSION_HOGSIZE:
529                 strcpy(Mission_list[*count].filename,OEM_MISSION_FILENAME);
530                 strcpy(Mission_list[*count].mission_name,OEM_MISSION_NAME);
531                 Mission_list[*count].anarchy_only_flag = 0;
532                 break;
533         default:
534                 Warning("Unknown hogsize %d, trying %s\n", Builtin_mission_hogsize, FULL_MISSION_FILENAME ".mn2");
535                 Int3(); //fall through
536         case FULL_MISSION_HOGSIZE:
537                 if (!read_mission_file(FULL_MISSION_FILENAME ".mn2", 0, ML_CURDIR))
538                         Error("Could not find required mission file <%s>", FULL_MISSION_FILENAME ".mn2");
539         }
540
541         strcpy(Builtin_mission_filename, Mission_list[*count].filename);
542         Mission_list[*count].descent_version = 2;
543         Mission_list[*count].anarchy_only_flag = 0;
544         ++(*count);
545 }
546
547
548 void add_missions_to_list(char *search_name, int *count, int anarchy_mode)
549 {
550         FILEFINDSTRUCT find;
551         if( !FileFindFirst( search_name, &find ) ) {
552                 do      {
553                         if (read_mission_file( find.name, *count, ML_MISSIONDIR )) {
554
555                                 if (anarchy_mode || !Mission_list[*count].anarchy_only_flag)
556                                         ++(*count);
557                         }
558
559                 } while( !FileFindNext( &find ) && *count < MAX_MISSIONS);
560                 FileFindClose();
561                 if (*count >= MAX_MISSIONS)
562                         mprintf((0, "Warning: more missions than d2x can handle\n"));
563         }
564 }
565
566 /* move <mission_name> to <place> on mission list, increment <place> */
567 void promote (char * mission_name, int * top_place, int num_missions)
568 {
569         int i;
570         char name[FILENAME_LEN], * t;
571         strcpy(name, mission_name);
572         if ((t = strchr(name,'.')) != NULL)
573                 *t = 0; //kill extension
574         //printf("promoting: %s\n", name);
575         for (i = *top_place; i < num_missions; i++)
576                 if (!stricmp(Mission_list[i].filename, name)) {
577                         //swap mission positions
578                         mle temp;
579
580                         temp = Mission_list[*top_place];
581                         Mission_list[*top_place] = Mission_list[i];
582                         Mission_list[i] = temp;
583                         ++(*top_place);
584                         break;
585                 }
586 }
587
588
589
590 //fills in the global list of missions.  Returns the number of missions
591 //in the list.  If anarchy_mode set, don't include non-anarchy levels.
592
593 extern char CDROM_dir[];
594 extern char AltHogDir[];
595 extern char AltHogdir_initialized;
596
597 int build_mission_list(int anarchy_mode)
598 {
599         static int num_missions=-1;
600         int count = 0;
601         int top_place;
602
603         //now search for levels on disk
604
605 //@@Took out this code because after this routine was called once for
606 //@@a list of single-player missions, a subsequent call for a list of
607 //@@anarchy missions would not scan again, and thus would not find the
608 //@@anarchy-only missions.  If we retain the minimum level of install,
609 //@@we may want to put the code back in, having it always scan for all
610 //@@missions, and have the code that uses it sort out the ones it wants.
611 //@@    if (num_missions != -1) {
612 //@@            if (Current_mission_num != 0)
613 //@@                    load_mission(0);                                //set built-in mission as default
614 //@@            return num_missions;
615 //@@    }
616
617         add_builtin_mission_to_list(&count);  //read built-in first
618         add_d1_builtin_mission_to_list(&count);
619         add_missions_to_list(MISSION_DIR "*.mn2", &count, anarchy_mode);
620         add_missions_to_list(MISSION_DIR "*.msn", &count, anarchy_mode);
621
622         if (AltHogdir_initialized) {
623                 char search_name[PATH_MAX + 5];
624                 strcpy(search_name, AltHogDir);
625                 strcat(search_name, "/" MISSION_DIR "*.mn2");
626                 add_missions_to_list(search_name, &count, anarchy_mode);
627                 strcpy(search_name, AltHogDir);
628                 strcat(search_name, "/" MISSION_DIR "*.msn");
629                 add_missions_to_list(search_name, &count, anarchy_mode);
630         }
631
632         // move original missions (in story-chronological order)
633         // to top of mission list
634         top_place = 0;
635         promote("descent", &top_place, count); // original descent 1 mission
636         D1_Builtin_mission_num = top_place - 1;
637         promote(Builtin_mission_filename, &top_place, count); // d2 or d2demo
638         Builtin_mission_num = top_place - 1;
639         promote("d2x", &top_place, count); // vertigo
640
641         if (count > top_place)
642                 qsort(&Mission_list[top_place],
643                       count - top_place,
644                       sizeof(*Mission_list),
645                                 (int (*)( const void *, const void * ))ml_sort_func);
646
647
648         if (count > top_place)
649                 qsort(&Mission_list[top_place],
650                       count - top_place,
651                       sizeof(*Mission_list),
652                       (int (*)( const void *, const void * ))ml_sort_func);
653
654         //load_mission(0);   //set built-in mission as default
655
656         num_missions = count;
657
658         return count;
659 }
660
661 void init_extra_robot_movie(char *filename);
662
663 //values for built-in mission
664
665 //loads the specfied mission from the mission list.
666 //build_mission_list() must have been called.
667 //Returns true if mission loaded ok, else false.
668 int load_mission(int mission_num)
669 {
670         CFILE *mfile;
671         char buf[80], *v;
672         int found_hogfile;
673         int enhanced_mission = 0;
674
675         if (mission_num == D1_Builtin_mission_num) {
676                 cfile_use_descent1_hogfile("descent.hog");
677                 switch (D1_Builtin_mission_hogsize) {
678                 default:
679                         Int3(); // fall through
680                 case D1_MISSION_HOGSIZE:
681                 case D1_10_MISSION_HOGSIZE:
682                 case D1_MAC_MISSION_HOGSIZE:
683                 case D1_OEM_MISSION_HOGSIZE:
684                 case D1_SHAREWARE_MISSION_HOGSIZE:
685                 case D1_SHAREWARE_10_MISSION_HOGSIZE:
686                 case D1_MAC_SHARE_MISSION_HOGSIZE:
687                         return load_mission_d1(mission_num);
688                         break;
689                 }
690         }
691
692         if (mission_num == Builtin_mission_num) {
693                 switch (Builtin_mission_hogsize) {
694                 case SHAREWARE_MISSION_HOGSIZE:
695                 case MAC_SHARE_MISSION_HOGSIZE:
696                         return load_mission_shareware(mission_num);
697                         break;
698                 case OEM_MISSION_HOGSIZE:
699                         return load_mission_oem(mission_num);
700                         break;
701                 default:
702                         Int3(); // fall through
703                 case FULL_MISSION_HOGSIZE:
704                         // continue on... (use d2.mn2 from hogfile)
705                         break;
706                 }
707         }
708
709         Current_mission_num = mission_num;
710
711         mprintf(( 0, "Loading mission %d\n", mission_num ));
712
713         //read mission from file
714
715         switch (Mission_list[mission_num].location) {
716         case ML_MISSIONDIR:
717                 strcpy(buf,MISSION_DIR);
718                 break;
719         case ML_CDROM:
720                 strcpy(buf,CDROM_dir);
721                 break;
722         default:
723                 Int3();                                                 //fall through
724         case ML_CURDIR:
725                 strcpy(buf,"");
726                 break;
727         }
728         strcat(buf,Mission_list[mission_num].filename);
729         if (Mission_list[mission_num].descent_version == 2)
730                 strcat(buf,".mn2");
731         else
732                 strcat(buf,".msn");
733
734         mfile = cfopen(buf,"rb");
735         if (mfile == NULL) {
736                 Current_mission_num = -1;
737                 return 0;               //error!
738         }
739
740         //for non-builtin missions, load HOG
741         if (strcmp(Mission_list[mission_num].filename, Builtin_mission_filename)) {
742
743                 strcpy(buf+strlen(buf)-4,".hog");               //change extension
744
745                 found_hogfile = cfile_use_alternate_hogfile(buf);
746
747                 #ifdef RELEASE                          //for release, require mission to be in hogfile
748                 if (! found_hogfile) {
749                         cfclose(mfile);
750                         Current_mission_num = -1;
751                         return 0;
752                 }
753                 #endif
754
755                 // for Descent 1 missions, load descent.hog
756                 if (Mission_list[mission_num].descent_version == 1 && strcmp(buf, "descent.hog"))
757                         if (!cfile_use_descent1_hogfile("descent.hog"))
758                                 Warning("descent.hog not available, this mission may be missing some files required for briefings\n");
759         }
760
761         //init vars
762         Last_level = 0;
763         Last_secret_level = 0;
764         Briefing_text_filename[0] = 0;
765         Ending_text_filename[0] = 0;
766
767         while (mfgets(buf,80,mfile)) {
768
769                 if (istok(buf,"name"))
770                         continue;                                               //already have name, go to next line
771                 if (istok(buf,"xname")) {
772                         enhanced_mission = 1;
773                         continue;                                               //already have name, go to next line
774                 }
775                 if (istok(buf,"zname")) {
776                         enhanced_mission = 2;
777                         continue;                                               //already have name, go to next line
778                 }
779                 else if (istok(buf,"type"))
780                         continue;                                               //already have name, go to next line
781                 else if (istok(buf,"hog")) {
782                         char    *bufp = buf;
783
784                         while (*(bufp++) != '=')
785                                 ;
786
787                         if (*bufp == ' ')
788                                 while (*(++bufp) == ' ')
789                                         ;
790
791                         cfile_use_alternate_hogfile(bufp);
792                         mprintf((0, "Hog file override = [%s]\n", bufp));
793                 }
794                 else if (istok(buf,"briefing")) {
795                         if ((v = get_value(buf)) != NULL) {
796                                 add_term(v);
797                                 if (strlen(v) < 13)
798                                         strcpy(Briefing_text_filename,v);
799                         }
800                 }
801                 else if (istok(buf,"ending")) {
802                         if ((v = get_value(buf)) != NULL) {
803                                 add_term(v);
804                                 if (strlen(v) < 13)
805                                         strcpy(Ending_text_filename,v);
806                         }
807                 }
808                 else if (istok(buf,"num_levels")) {
809
810                         if ((v=get_value(buf))!=NULL) {
811                                 int n_levels,i;
812
813                                 n_levels = atoi(v);
814
815                                 for (i=0;i<n_levels && mfgets(buf,80,mfile);i++) {
816
817                                         add_term(buf);
818                                         if (strlen(buf) <= 12) {
819                                                 strcpy(Level_names[i],buf);
820                                                 Last_level++;
821                                         }
822                                         else
823                                                 break;
824                                 }
825
826                         }
827                 }
828                 else if (istok(buf,"num_secrets")) {
829                         if ((v=get_value(buf))!=NULL) {
830                                 int i;
831
832                                 N_secret_levels = atoi(v);
833
834                                 Assert(N_secret_levels <= MAX_SECRET_LEVELS_PER_MISSION);
835
836                                 for (i=0;i<N_secret_levels && mfgets(buf,80,mfile);i++) {
837                                         char *t;
838
839                                         
840                                         if ((t=strchr(buf,','))!=NULL) *t++=0;
841                                         else
842                                                 break;
843
844                                         add_term(buf);
845                                         if (strlen(buf) <= 12) {
846                                                 strcpy(Secret_level_names[i],buf);
847                                                 Secret_level_table[i] = atoi(t);
848                                                 if (Secret_level_table[i]<1 || Secret_level_table[i]>Last_level)
849                                                         break;
850                                                 Last_secret_level--;
851                                         }
852                                         else
853                                                 break;
854                                 }
855
856                         }
857                 }
858
859         }
860
861         cfclose(mfile);
862
863         if (Last_level <= 0) {
864                 Current_mission_num = -1;               //no valid mission loaded
865                 return 0;
866         }
867
868         Current_mission_filename = Mission_list[Current_mission_num].filename;
869         Current_mission_longname = Mission_list[Current_mission_num].mission_name;
870
871         if (enhanced_mission) {
872                 char t[50];
873                 extern void bm_read_extra_robots();
874                 sprintf(t,"%s.ham",Current_mission_filename);
875                 bm_read_extra_robots(t,enhanced_mission);
876                 strncpy(t,Current_mission_filename,6);
877                 strcat(t,"-l.mvl");
878                 init_extra_robot_movie(t);
879         }
880
881         return 1;
882 }
883
884 //loads the named mission if exists.
885 //Returns true if mission loaded ok, else false.
886 int load_mission_by_name(char *mission_name)
887 {
888         int n,i;
889
890         n = build_mission_list(1);
891
892         for (i=0;i<n;i++)
893                 if (!stricmp(mission_name,Mission_list[i].filename))
894                         return load_mission(i);
895
896         return 0;               //couldn't find mission
897 }