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