]> icculus.org git repositories - taylor/freespace2.git/blob - src/mission/missioncampaign.cpp
fix OOB scancode-keycode lookups in hotkey ui
[taylor/freespace2.git] / src / mission / missioncampaign.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Mission/MissionCampaign.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * source for dealing with campaigns
16  *
17  * $Log$
18  * Revision 1.8  2003/06/11 18:30:33  taylor
19  * plug memory leaks
20  *
21  * Revision 1.7  2003/05/25 02:30:42  taylor
22  * Freespace 1 support
23  *
24  * Revision 1.6  2002/07/24 00:20:42  relnev
25  * nothing interesting
26  *
27  * Revision 1.5  2002/06/21 03:34:05  relnev
28  * implemented a stub and fixed a path
29  *
30  * Revision 1.4  2002/06/09 04:41:22  relnev
31  * added copyright header
32  *
33  * Revision 1.3  2002/06/09 03:16:04  relnev
34  * added _splitpath.
35  *
36  * removed unneeded asm, old sdl 2d setup.
37  *
38  * fixed crash caused by opengl_get_region.
39  *
40  * Revision 1.2  2002/06/02 04:26:34  relnev
41  * warning cleanup
42  *
43  * Revision 1.1.1.1  2002/05/03 03:28:09  root
44  * Initial import.
45  *
46  * 
47  * 23    9/14/99 4:35a Dave
48  * Argh. Added all kinds of code to handle potential crashes in debriefing
49  * code.
50  * 
51  * 22    9/09/99 11:40p Dave
52  * Handle an SDL_assert() in beam code. Added supernova sounds. Play the right
53  * 2 end movies properly, based upon what the player did in the mission.
54  * 
55  * 21    9/09/99 9:34a Jefff
56  * fixed a potential exit-loop bug
57  * 
58  * 20    9/07/99 6:55p Jefff
59  * functionality to break out of a loop.  hacked functionality to jump to
60  * a specific mission in a campaign -- doesnt grant ships/weapons from
61  * skipped missions tho.
62  * 
63  * 19    9/07/99 2:19p Jefff
64  * clear skip mission player vars on mission skip
65  * 
66  * 18    9/06/99 9:45p Jefff
67  * break out of loop and skip mission support
68  * 
69  * 17    9/06/99 6:38p Dave
70  * Improved CD detection code.
71  * 
72  * 16    9/03/99 1:32a Dave
73  * CD checking by act. Added support to play 2 cutscenes in a row
74  * seamlessly. Fixed super low level cfile bug related to files in the
75  * root directory of a CD. Added cheat code to set campaign mission # in
76  * main hall.
77  * 
78  * 15    8/27/99 12:04a Dave
79  * Campaign loop screen.
80  * 
81  * 14    8/04/99 5:36p Andsager
82  * Show upsell screens at end of demo campaign before returning to main
83  * hall.
84  * 
85  * 13    2/05/99 3:50p Anoop
86  * Removed dumb campaign mission stats saving from multiplayer.
87  * 
88  * 12    12/17/98 2:43p Andsager
89  * Modify fred campaign save file to include optional mission loops
90  * 
91  * 11    12/12/98 3:17p Andsager
92  * Clean up mission eval, goal, event and mission scoring.
93  * 
94  * 10    12/10/98 9:59a Andsager
95  * Fix some bugs with mission loops
96  * 
97  * 9     12/09/98 1:56p Andsager
98  * Initial checkin of mission loop
99  * 
100  * 8     11/05/98 5:55p Dave
101  * Big pass at reducing #includes
102  * 
103  * 7     11/03/98 4:48p Johnson
104  * Fixed campaign file versioning bug left over from Silent Threat port.
105  * 
106  * 6     10/23/98 3:51p Dave
107  * Full support for tstrings.tbl and foreign languages. All that remains
108  * is to make it active in Fred.
109  * 
110  * 5     10/13/98 2:47p Andsager
111  * Remove reference to Tech_shivan_species_avail
112  * 
113  * 4     10/13/98 9:28a Dave
114  * Started neatening up freespace.h. Many variables renamed and
115  * reorganized. Added AlphaColors.[h,cpp]
116  * 
117  * 3     10/07/98 6:27p Dave
118  * Globalized mission and campaign file extensions. Removed Silent Threat
119  * special code. Moved \cache \players and \multidata into the \data
120  * directory.
121  * 
122  * 2     10/07/98 10:53a Dave
123  * Initial checkin.
124  * 
125  * 1     10/07/98 10:49a Dave
126  * 
127  * 95    9/10/98 1:17p Dave
128  * Put in code to flag missions and campaigns as being MD or not in Fred
129  * and Freespace. Put in multiplayer support for filtering out MD
130  * missions. Put in multiplayer popups for warning of non-valid missions.
131  * 
132  * 94    9/01/98 4:25p Dave
133  * Put in total (I think) backwards compatibility between mission disk
134  * freespace and non mission disk freespace, including pilot files and
135  * campaign savefiles.
136  * 
137  * 93    7/06/98 4:10p Hoffoss
138  * Fixed some bugs that presented themselves when trying to use a pilot
139  * that has a no longer existent campaign active.  Also expanded the
140  * campaign load code to actually return a proper error code, instead of
141  * always trapping errors internally and crashing, and always returning 0.
142  * 
143  * 92    6/17/98 9:30a Allender
144  * fixed red alert replay stats clearing problem
145  * 
146  * 91    6/01/98 11:43a John
147  * JAS & MK:  Classified all strings for localization.
148  * 
149  * 90    5/25/98 1:29p Allender
150  * end mission sequencing
151  * 
152  * 89    5/21/98 9:25p Allender
153  * endgame movie always viewable at end of campaign
154  * 
155  * 88    5/13/98 5:14p Allender
156  * red alert support to go back to previous mission
157  * 
158  * 87    5/12/98 4:16p Hoffoss
159  * Fixed bug where not all missions in all campaigns were being filtered
160  * out of stand alone mission listing in simulator room.
161  * 
162  * 86    5/05/98 3:29p Hoffoss
163  * Changed code so description is BEFORE num players in campaign file.
164  * Other code is relying on this ordering.
165  * 
166  * 85    5/05/98 12:19p Dave
167  * campaign description goes *after* num players
168  * 
169  * 84    5/04/98 5:52p Comet
170  * Fixed bug with Galatea/Bastion selection when finishing missions.
171  * 
172  * 83    5/01/98 2:46p Duncan
173  * fix a cfile problem with campaigns related to the new cfile stuff
174  * 
175  * 82    5/01/98 12:34p John
176  * Added code to force FreeSpace to run in the same dir as exe and made
177  * all the parse error messages a little nicer.
178  * 
179  * 81    4/30/98 7:01p Duncan
180  * AL: don't allow deletion of campaign files in multiplayer
181  * 
182  * 80    4/30/98 4:53p John
183  * Restructured and cleaned up cfile code.  Added capability to read off
184  * of CD-ROM drive and out of multiple pack files.
185  *
186  * $NoKeywords: $
187  */
188
189 #include <stdio.h>
190 #ifndef PLAT_UNIX
191 #include <direct.h>
192 #include <io.h>
193 #else
194 #include <sys/types.h>
195 #include <sys/stat.h>
196 #include <unistd.h>
197 #include <dirent.h>
198 #include <fnmatch.h>
199 #endif
200 #include <string.h>
201 #include <errno.h>
202
203 #include "key.h"
204 #include "ui.h"
205 #include "missioncampaign.h"
206 #include "gamesequence.h"
207 #include "2d.h"
208 #include "parselo.h"
209 #include "missionload.h"
210 #include "freespace.h"
211 #include "sexp.h"
212 #include "cfile.h"
213 #include "player.h"
214 #include "missiongoals.h"
215 #include "movie.h"
216 #include "multi.h"
217 #include "techmenu.h"
218 #include "eventmusic.h"
219 #include "alphacolors.h"
220 #include "localize.h"
221 #include "supernova.h"
222
223 // mission disk stuff
224 #define CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD                                         75
225 #define CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD                                               44
226
227 #define CAMPAIGN_INITIAL_RELEASE_FILE_VERSION                           6
228
229 // campaign wasn't ended
230 int Campaign_ended_in_mission = 0;
231
232 // stuff for selecting campaigns.  We need to keep both arrays around since we display the
233 // list of campaigns by name, but must load campaigns by filename
234 char *Campaign_names[MAX_CAMPAIGNS];
235 char *Campaign_file_names[MAX_CAMPAIGNS];
236 int     Num_campaigns;
237
238 const char *campaign_types[MAX_CAMPAIGN_TYPES] = 
239 {
240 //XSTR:OFF
241         "single",
242         "multi coop",
243         "multi teams"
244 //XSTR:ON
245 };
246
247 // modules local variables to deal with getting new ships/weapons available to the player
248 int Num_granted_ships, Num_granted_weapons;             // per mission counts of new ships and weapons
249 int Granted_ships[MAX_SHIP_TYPES];
250 int Granted_weapons[MAX_WEAPON_TYPES];
251
252 // variables to control the UI stuff for loading campaigns
253 static UI_WINDOW Campaign_window;
254 static UI_LISTBOX Campaign_listbox;
255 static UI_BUTTON Campaign_okb, Campaign_cancelb;
256
257 // the campaign!!!!!
258 campaign Campaign;
259
260 // variables with deal with the campaign save file
261 #define CAMPAIGN_FILE_VERSION                                                   12
262 //#define CAMPAIGN_FILE_COMPATIBLE_VERSION              CAMPAIGN_INITIAL_RELEASE_FILE_VERSION
263 #define CAMPAIGN_FILE_COMPATIBLE_VERSION                        CAMPAIGN_FILE_VERSION
264 #define CAMPAIGN_FILE_ID                                                                0xbeefcafe
265
266 // variables with deal with the campaign stats save file
267 #define CAMPAIGN_STATS_FILE_VERSION                                     1
268 #define CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION  1
269 #define CAMPAIGN_STATS_FILE_ID                                          0xabbadaad
270
271 // mission_campaign_get_name returns a string (which is malloced in this routine) of the name
272 // of the given freespace campaign file.  In the type field, we return if the campaign is a single
273 // player or multiplayer campaign.  The type field will only be valid if the name returned is non-NULL
274 int mission_campaign_get_info(const char *filename, char *name, int *type, int *max_players, char **desc)
275 {
276         int i;
277         char campaign_type[NAME_LENGTH], fname[MAX_FILENAME_LEN];
278
279         SDL_assert( name != NULL );
280         SDL_assert( type != NULL );
281
282         // open localization
283         lcl_ext_open();
284
285         SDL_strlcpy(fname, filename, SDL_arraysize(fname));
286         if ((strlen(fname) < 4) || SDL_strcasecmp(fname + strlen(fname) - 4, FS_CAMPAIGN_FILE_EXT)){
287                 SDL_strlcat(fname, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(fname));
288         }
289
290         SDL_assert(strlen(fname) < MAX_FILENAME_LEN);
291
292         try {
293                 read_file_text( fname );
294                 reset_parse();
295                 required_string("$Name:");
296
297                 stuff_string( name, F_NAME, NULL );
298                 if ( name == NULL ) {
299                         Int3();
300                         nprintf(("Warning", "No name found for campaign file %s\n", filename));
301
302                         // close localization
303                         lcl_ext_close();
304
305                         return 0;
306                 }
307
308                 required_string( "$Type:" );
309                 stuff_string( campaign_type, F_NAME, NULL );
310
311                 *type = -1;
312                 for (i=0; i<MAX_CAMPAIGN_TYPES; i++) {
313                         if ( !SDL_strcasecmp(campaign_type, campaign_types[i]) ) {
314                                 *type = i;
315                         }
316                 }
317
318                 if (desc) {
319                         *desc = NULL;
320                         if (optional_string("+Description:")) {
321                                 *desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
322                         }
323                 }
324
325                 // if this is a multiplayer campaign, get the max players
326                 if ((*type) > 0) {
327                         skip_to_string("+Num Players:");
328                         stuff_int(max_players);
329                 }               
330
331                 // if we found a valid campaign type
332                 if ((*type) >= 0) {
333                         // close localization
334                         lcl_ext_close();
335
336                         return 1;
337                 }
338         } catch (parse_error_t rval) {
339                 if (rval == PARSE_ERROR_FILE_NOT_FOUND) {
340                         // close localization
341                         lcl_ext_close();
342
343                         return 0;
344                 }
345
346                 Error(LOCATION, "Error parsing '%s'\r\nError code = %i.\r\n", fname, (int)rval);
347         }
348
349         Int3();         // get Allender -- incorrect type found
350
351         // close localization
352         lcl_ext_close();
353
354         return 0;
355 }
356
357 // parses campaign and returns a list of missions in it.  Returns number of missions added to
358 // the 'list', and up to 'max' missions may be added to 'list'.  Returns negative on error.
359 //
360 int mission_campaign_get_mission_list(const char *filename, char **list, int max)
361 {
362         int i, num = 0;
363         char name[NAME_LENGTH];
364
365         filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
366
367         // read the mission file and get the list of mission filenames
368         try {
369                 read_file_text(filename);
370                 reset_parse();
371
372                 while (skip_to_string("$Mission:") > 0) {
373                         stuff_string(name, F_NAME, NULL);
374                         if (num < max)
375                                 list[num++] = strdup(name);
376                 }
377         } catch (parse_error_t) {
378                 // since we can't return count of allocated elements, free them instead
379                 for (i=0; i<num; i++)
380                         free(list[i]);
381
382                 return -1;
383         }
384
385         return num;
386 }
387
388 // gets optional ship/weapon information
389 void mission_campaign_get_sw_info()
390 {
391         int i, count, ship_list[MAX_SHIP_TYPES], weapon_list[MAX_WEAPON_TYPES];
392
393         // set allowable ships to the SIF_PLAYER_SHIPs
394         memset( Campaign.ships_allowed, 0, sizeof(Campaign.ships_allowed) );
395         for (i = 0; i < MAX_SHIP_TYPES; i++ ) {
396                 if ( Ship_info[i].flags & SIF_PLAYER_SHIP )
397                         Campaign.ships_allowed[i] = 1;
398         }
399
400         for (i = 0; i < MAX_WEAPON_TYPES; i++ )
401                 Campaign.weapons_allowed[i] = 1;
402
403         if ( optional_string("+Starting Ships:") ) {
404                 for (i = 0; i < MAX_SHIP_TYPES; i++ )
405                         Campaign.ships_allowed[i] = 0;
406
407                 count = stuff_int_list(ship_list, MAX_SHIP_TYPES, SHIP_INFO_TYPE);
408
409                 // now set the array elements stating which ships we are allowed
410                 for (i = 0; i < count; i++ ) {
411                         if ( Ship_info[ship_list[i]].flags & SIF_PLAYER_SHIP )
412                                 Campaign.ships_allowed[ship_list[i]] = 1;
413                 }
414         }
415
416         if ( optional_string("+Starting Weapons:") ) {
417                 for (i = 0; i < MAX_WEAPON_TYPES; i++ )
418                         Campaign.weapons_allowed[i] = 0;
419
420                 count = stuff_int_list(weapon_list, MAX_WEAPON_TYPES, WEAPON_POOL_TYPE);
421
422                 // now set the array elements stating which ships we are allowed
423                 for (i = 0; i < count; i++ )
424                         Campaign.weapons_allowed[weapon_list[i]] = 1;
425         }
426 }
427
428 // mission_campaign_load starts a new campaign.  It reads in the mission information in the campaign file
429 // It also sets up all variables needed inside of the game to deal with starting mission numbers, etc
430 //
431 // Note: Due to difficulties in generalizing this function, parts of it are duplicated throughout
432 // this file.  If you change the format of the campaign file, you should be sure these related
433 // functions work properly and update them if it breaks them.
434 int mission_campaign_load( const char *filename, int load_savefile )
435 {
436         int len, i;
437         char name[NAME_LENGTH], type[NAME_LENGTH];
438
439         filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
440
441         // open localization
442         lcl_ext_open(); 
443
444         // read the mission file and get the list of mission filenames
445         try {
446                 // be sure to remove all old malloced strings of Mission_names
447                 // we must also free any goal stuff that was from a previous campaign
448                 // this also frees sexpressions so the next call to init_sexp will be able to reclaim
449                 // nodes previously used by another campaign.
450                 mission_campaign_close();
451
452                 SDL_strlcpy( Campaign.filename, filename, SDL_arraysize(Campaign.filename) );
453
454                 // only initialize the sexpression stuff when Fred isn't running.  It'll screw things up major
455                 // if it does
456                 if ( !Fred_running ){
457                         init_sexp();            // must initialize the sexpression stuff
458                 }
459
460                 read_file_text( filename );
461                 reset_parse();
462                 memset( &Campaign, 0, sizeof(Campaign) );
463
464                 // copy filename to campaign structure minus the extension
465                 len = min(strlen(filename) - 4 + 1, SDL_arraysize(Campaign.filename));
466                 SDL_strlcpy(Campaign.filename, filename, len);
467
468                 required_string("$Name:");
469                 stuff_string( name, F_NAME, NULL );
470                 
471                 //Store campaign name in the global struct
472                 SDL_strlcpy( Campaign.name, name, SDL_arraysize(Campaign.name) );
473
474                 required_string( "$Type:" );
475                 stuff_string( type, F_NAME, NULL );
476
477                 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
478                         if ( !SDL_strcasecmp(type, campaign_types[i]) ) {
479                                 Campaign.type = i;
480                                 break;
481                         }
482                 }
483
484                 if ( i == MAX_CAMPAIGN_TYPES )
485                         Error(LOCATION, "Unknown campaign type %s!", type);
486
487                 Campaign.desc = NULL;
488                 if (optional_string("+Description:"))
489                         Campaign.desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
490
491                 // if the type is multiplayer -- get the number of players
492                 Campaign.num_players = 0;
493                 if ( Campaign.type != CAMPAIGN_TYPE_SINGLE) {
494                         required_string("+Num players:");
495                         stuff_int( &(Campaign.num_players) );
496                 }               
497
498 #ifdef MAKE_FS1
499                 // check if mission disk -
500                 // doesn't do anything but prevent non fatal error messages
501                 optional_string("+Missiondisk");
502 #endif
503                 
504                 // parse the optional ship/weapon information
505                 mission_campaign_get_sw_info();
506
507                 // parse the mission file and actually read in the mission stuff
508                 Campaign.num_missions = 0;
509                 while ( required_string_either("#End", "$Mission:") ) {
510                         cmission *cm;
511
512                         required_string("$Mission:");
513                         stuff_string(name, F_NAME, NULL);
514                         cm = &Campaign.missions[Campaign.num_missions];
515                         cm->name = strdup(name);
516
517                         cm->briefing_cutscene[0] = 0;
518                         if ( optional_string("+Briefing Cutscene:") )
519                                 stuff_string( cm->briefing_cutscene, F_NAME, NULL );
520
521                         cm->flags = 0;
522                         if (optional_string("+Flags:"))
523                                 stuff_int(&cm->flags);
524
525                         cm->formula = -1;
526                         if ( optional_string("+Formula:") ) {
527                                 cm->formula = get_sexp_main();
528                                 if ( !Fred_running ) {
529                                         SDL_assert ( cm->formula != -1 );
530                                         sexp_mark_persistent( cm->formula );
531
532                                 } else {
533                                         if ( cm->formula == -1 ){
534                                                 // close localization
535                                                 lcl_ext_close();
536
537                                                 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
538                                         }
539                                 }
540                         }
541
542                         // Do misison looping stuff
543                         cm->has_mission_loop = 0;
544                         if ( optional_string("+Mission Loop:") ) {
545                                 cm->has_mission_loop = 1;
546                         }
547
548                         cm->mission_loop_desc = NULL;
549                         if ( optional_string("+Mission Loop Text:")) {
550                                 cm->mission_loop_desc = stuff_and_malloc_string(F_MULTITEXT, NULL, MISSION_DESC_LENGTH);
551                         }
552
553                         cm->mission_loop_brief_anim = NULL;
554                         if ( optional_string("+Mission Loop Brief Anim:")) {
555                                 cm->mission_loop_brief_anim = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
556                         }
557
558                         cm->mission_loop_brief_sound = NULL;
559                         if ( optional_string("+Mission Loop Brief Sound:")) {
560                                 cm->mission_loop_brief_sound = stuff_and_malloc_string(F_MULTITEXT, NULL, MAX_FILENAME_LEN);
561                         }
562
563                         cm->mission_loop_formula = -1;
564                         if ( optional_string("+Formula:") ) {
565                                 cm->mission_loop_formula = get_sexp_main();
566                                 if ( !Fred_running ) {
567                                         SDL_assert ( cm->mission_loop_formula != -1 );
568                                         sexp_mark_persistent( cm->mission_loop_formula );
569
570                                 } else {
571                                         if ( cm->mission_loop_formula == -1 ){
572                                                 // close localization
573                                                 lcl_ext_close();
574
575                                                 return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
576                                         }
577                                 }
578                         }
579
580                         if (optional_string("+Level:")) {
581                                 stuff_int( &cm->level );
582                                 if ( cm->level == 0 )  // check if the top (root) of the whole tree
583                                         Campaign.next_mission = Campaign.num_missions;
584
585                         } else
586                                 Campaign.realign_required = 1;
587
588                         if (optional_string("+Position:"))
589                                 stuff_int( &cm->pos );
590                         else
591                                 Campaign.realign_required = 1;
592
593                         if (Fred_running) {
594                                 cm->num_goals = -1;
595                                 cm->num_events = -1;
596                                 cm->notes = NULL;
597
598                         } else {
599                                 cm->num_goals = 0;
600                                 cm->num_events = 0;
601                         }
602
603                         cm->goals = NULL;
604                         cm->events = NULL;
605                         Campaign.num_missions++;
606                 }
607         } catch (parse_error_t rval) {
608                 mprintf(("Error parsing '%s'\r\nError code = %i.\r\n", filename, (int)rval));
609
610                 // close localization
611                 lcl_ext_close();
612
613                 return CAMPAIGN_ERROR_CORRUPT;
614         }
615
616         // set up the other variables for the campaign stuff.  After initializing, we must try and load
617         // the campaign save file for this player.  Since all campaign loads go through this routine, I
618         // think this place should be the only necessary place to load the campaign save stuff.  The campaign
619         // save file will get written when a mission has ended by player choice.
620         Campaign.next_mission = 0;
621         Campaign.prev_mission = -1;
622         Campaign.current_mission = -1;  
623
624         // loading the campaign will get us to the current and next mission that the player must fly
625         // plus load all of the old goals that future missions might rely on.
626         if (!Fred_running && load_savefile && (Campaign.type == CAMPAIGN_TYPE_SINGLE)) {
627                 mission_campaign_savefile_load(Campaign.filename);
628         }
629
630         // close localization
631         lcl_ext_close();
632
633         return 0;
634 }
635
636 // mission_campaign_load_by_name() loads up a freespace campaign given the filename.  This routine
637 // is used to load up campaigns when a pilot file is loaded.  Generally, the
638 // filename will probably be the freespace campaign file, but not necessarily.
639 int mission_campaign_load_by_name( const char *filename )
640 {
641         char real_filename[MAX_FILENAME_LEN+1] = { 0 };
642         char name[NAME_LENGTH],test[5];
643         int type,max_players;
644
645         // make sure to tack on .fsc on the end if its not there already
646         if(strlen(filename) > 0){
647                 SDL_strlcpy(real_filename, filename, MAX_FILENAME_LEN);
648
649                 if(strlen(real_filename) > 4){
650                         SDL_strlcpy(test, real_filename+(strlen(real_filename)-4), SDL_arraysize(test));
651                         if(strcmp(test, FS_CAMPAIGN_FILE_EXT)!=0){
652                                 SDL_strlcat(real_filename, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(real_filename));
653                         }
654                 } else {
655                         SDL_strlcat(real_filename, FS_CAMPAIGN_FILE_EXT, SDL_arraysize(real_filename));
656                 }
657         } else {
658                 Error(LOCATION,"Tried to load campaign file with illegal length/extension!");
659         }
660
661         if (!mission_campaign_get_info(real_filename, name, &type, &max_players)){
662                 return -1;      
663         }
664
665         Num_campaigns = 0;
666         Campaign_file_names[Num_campaigns] = real_filename;
667         Campaign_names[Num_campaigns] = name;
668         Num_campaigns++;
669         mission_campaign_load(real_filename);           
670         return 0;
671 }
672
673 int mission_campaign_load_by_name_csfe( const char *filename, const char *callsign )
674 {
675         Game_mode |= GM_NORMAL;
676         SDL_strlcpy(Player->callsign, callsign, SDL_arraysize(Player->callsign));
677         return mission_campaign_load_by_name( filename);
678 }
679
680
681 // mission_campaign_init initializes some variables then loads the default Freespace single player campaign.
682 void mission_campaign_init()
683 {
684         memset(&Campaign, 0, sizeof(Campaign) );
685 }
686
687 // Fill in the root of the campaign save filename
688 void mission_campaign_savefile_generate_root(char *filename, const int max_len)
689 {
690         char base[MAX_FILENAME_LEN];
691
692         SDL_assert ( strlen(Campaign.filename) != 0 );
693
694         // build up the filename for the save file.  There could be a problem with filename length,
695         // but this problem can get fixed in several ways -- ignore the problem for now though.
696         base_filename(Campaign.filename, base, SDL_arraysize(base));
697         SDL_assert ( (int)(strlen(base) + strlen(Player->callsign)) < max_len );
698
699         SDL_snprintf( filename, max_len, NOX("%s.%s."), Player->callsign, base );
700 }
701
702 // mission_campaign_savefile_save saves the state of the campaign.  This function will probably always be called
703 // then the player is done flying a mission in the campaign path.  It will save the missions played, the
704 // state of the goals, etc.
705
706 int mission_campaign_savefile_save()
707 {
708         char filename[MAX_PATH_LEN];
709         CFILE *fp;
710         int i,j, mission_count;
711
712         memset(filename, 0, sizeof(filename));
713         mission_campaign_savefile_generate_root(filename, SDL_arraysize(filename));
714
715         // name the file differently depending on whether we're in single player or multiplayer mode
716         // single player : *.csg
717         SDL_strlcat( filename, NOX("csg"), SDL_arraysize(filename) );
718
719         fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
720
721         if (!fp)
722                 return errno;
723
724         // Write out campaign file info
725         cfwrite_int( CAMPAIGN_FILE_ID,fp );
726         cfwrite_int( CAMPAIGN_FILE_VERSION,fp );
727
728         // put in the file signature (single or multiplayer campaign) - see MissionCampaign.h for the #defines
729         cfwrite_int( CAMPAIGN_SINGLE_PLAYER_SIG, fp );
730
731         // do we need to write out the filename of the campaign?
732         cfwrite_string_len( Campaign.filename, fp );
733         cfwrite_int( Campaign.prev_mission, fp );
734         cfwrite_int( Campaign.next_mission, fp );
735         cfwrite_int( Campaign.loop_reentry, fp );
736         cfwrite_int( Campaign.loop_enabled, fp );
737
738         // write out the information for ships/weapons which this player is allowed to use
739         cfwrite_int(Num_ship_types, fp);
740         cfwrite_int(Num_weapon_types, fp);
741         for ( i = 0; i < Num_ship_types; i++ ){
742                 cfwrite_char( Campaign.ships_allowed[i], fp );
743         }
744
745         for ( i = 0; i < Num_weapon_types; i++ ){
746                 cfwrite_char( Campaign.weapons_allowed[i], fp );
747         }
748
749         // write out the completed mission matrix.  Used to tell which missions the player
750         // can replay in the simulator.  Also, each completed mission contains a list of the goals
751         // that were in the mission along with the goal completion status.
752         cfwrite_int( Campaign.num_missions_completed, fp );
753         for (i = 0; i < MAX_CAMPAIGN_MISSIONS; i++ ) {
754                 if ( Campaign.missions[i].completed ) {
755                         cfwrite_int( i, fp );
756                         cfwrite_int( Campaign.missions[i].num_goals, fp );
757                         for ( j = 0; j < Campaign.missions[i].num_goals; j++ ) {
758                                 cfwrite_string_len( Campaign.missions[i].goals[j].name, fp );
759                                 cfwrite_char( Campaign.missions[i].goals[j].status, fp );
760                         }
761                         cfwrite_int( Campaign.missions[i].num_events, fp );
762                         for ( j = 0; j < Campaign.missions[i].num_events; j++ ) {
763                                 cfwrite_string_len( Campaign.missions[i].events[j].name, fp );
764                                 cfwrite_char( Campaign.missions[i].events[j].status, fp );
765                         }
766
767                         // write flags
768                         cfwrite_int(Campaign.missions[i].flags, fp);
769                 }
770         }
771
772         cfclose( fp );
773
774         // 6/17/98
775         // ugh!  due to horrible bug, the stats saved at the end of every level were not written
776         // out to disk.  Write out a seperate file to do this.  We will only read it in if we actually
777         // find the file.
778         memset(filename, 0, sizeof(filename));
779         mission_campaign_savefile_generate_root(filename, SDL_arraysize(filename));
780
781         // name the file differently depending on whether we're in single player or multiplayer mode
782         // single player : *.csg
783         SDL_strlcat( filename, NOX("css"), SDL_arraysize(filename) );
784
785         fp = cfopen(filename,"wb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS);
786
787         if (!fp)
788                 return errno;
789
790         // Write out campaign file info
791         cfwrite_int( CAMPAIGN_STATS_FILE_ID,fp );
792         cfwrite_int( CAMPAIGN_STATS_FILE_VERSION,fp );
793
794         // determine how many missions we are saving -- I think that this method is safer than the method
795         // I used for release
796         mission_count = 0;
797         for ( i = 0; i < Campaign.num_missions; i++ ) {
798                 if ( Campaign.missions[i].completed ) {
799                         mission_count++;
800                 }
801         }
802
803         // write out the stats information to disk.     
804         cfwrite_int( mission_count, fp );
805         for (i = 0; i < Campaign.num_missions; i++ ) {
806                 if ( Campaign.missions[i].completed ) {
807                         cfwrite_int( i, fp );
808                         cfwrite( &Campaign.missions[i].stats, sizeof(scoring_struct), 1, fp );
809                 }
810         }
811
812         cfclose( fp );
813         
814         return 0;
815 }
816
817 // The following function always only ever ever ever called by CSFE!!!!!
818 int campaign_savefile_save(const char *pname)
819 {
820         if (Campaign.type == CAMPAIGN_TYPE_SINGLE)
821                 Game_mode &= ~GM_MULTIPLAYER;
822         else
823                 Game_mode |= GM_MULTIPLAYER;
824
825         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
826         //memcpy(&Campaign, camp, sizeof(campaign));
827         return mission_campaign_savefile_save();
828 }
829
830
831 // the below two functions is internal to this module.  It is here so that I can group the save/load
832 // functions together.
833 //
834
835 // mission_campaign_savefile_delete deletes any save file in the players directory for the given
836 // campaign filename
837 void mission_campaign_savefile_delete( const char *cfilename, int is_multi )
838 {
839         char filename[MAX_PATH_LEN], base[MAX_FILENAME_LEN];
840
841         base_filename(cfilename, base, SDL_arraysize(base));
842
843         if ( Player->flags & PLAYER_FLAGS_IS_MULTI ) {
844                 return; // no such thing as a multiplayer campaign savefile
845         }
846
847         SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.csg"), Player->callsign, base );
848
849         cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
850 }
851
852 void campaign_delete_save( const char *cfn, const char *pname)
853 {
854         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
855         mission_campaign_savefile_delete(cfn);
856 }
857
858 // next function deletes all the save files for this particular pilot.  Just call cfile function
859 // which will delete multiple files
860 // Player_select_mode tells us whether we are deleting single or multiplayer files
861 void mission_campaign_delete_all_savefiles( const char *pilot_name, int is_multi )
862 {
863         int dir_type, num_files, i;
864         char *names[MAX_CAMPAIGNS], spec[MAX_FILENAME_LEN + 2];
865         const char *ext;
866         char filename[1024];
867         int (*filter_save)(const char *filename);
868
869         if ( is_multi ) {
870                 return;                         // can't have multiplayer campaign save files
871         }
872
873         ext = NOX(".csg");
874         dir_type = CF_TYPE_SINGLE_PLAYERS;
875
876         SDL_snprintf(spec, SDL_arraysize(spec), NOX("%s.*%s"), pilot_name, ext);
877
878         // HACK HACK HACK HACK!!!!  cf_get_file_list is not reentrant.  Pretty dumb because it should
879         // be.  I have to save any file filters
880         filter_save = Get_file_list_filter;
881         Get_file_list_filter = NULL;
882         num_files = cf_get_file_list(MAX_CAMPAIGNS, names, dir_type, spec);
883         Get_file_list_filter = filter_save;
884
885         for (i=0; i<num_files; i++) {
886                 SDL_strlcpy(filename, names[i], SDL_arraysize(filename));
887                 SDL_strlcat(filename, ext, SDL_arraysize(filename));
888                 cf_delete(filename, dir_type);
889                 free(names[i]);
890         }
891 }
892
893 // mission_campaign_savefile_load takes a filename of a campaign file as a parameter and loads all
894 // of the information stored in the campaign file.
895 void mission_campaign_savefile_load( const char *cfilename )
896 {
897         char filename[MAX_PATH_LEN], base[MAX_FILENAME_LEN];
898         int version, i, num, j, num_stats_blocks;
899         uint id, type_sig;
900         CFILE *fp;
901
902         SDL_assert ( strlen(cfilename) != 0 );
903
904         // probably only called from single player games anymore!!! should be anyway
905         SDL_assert( Game_mode & GM_NORMAL );            // get allender or DaveB.  trying to save campaign in multiplayer
906
907         // build up the filename for the save file.  There could be a problem with filename length,
908         // but this problem can get fixed in several ways -- ignore the problem for now though.
909         base_filename(cfilename, base, SDL_arraysize(base));
910         SDL_assert ( (strlen(base) + strlen(Player->callsign)) < SDL_arraysize(filename) );
911
912         if(Game_mode & GM_MULTIPLAYER)
913                 SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.msg"), Player->callsign, base );
914         else
915                 SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.csg"), Player->callsign, base );
916
917         fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
918         if ( !fp )
919                 return;
920
921         id = cfread_int( fp );
922         if ( id != CAMPAIGN_FILE_ID ) {
923                 Warning(LOCATION, "Campaign save file has invalid signature");
924                 cfclose( fp );
925                 return;
926         }
927
928         version = cfread_int( fp );
929         if ( version < CAMPAIGN_FILE_COMPATIBLE_VERSION ) {
930                 Warning(LOCATION, "Campaign save file too old -- not compatible.  Deleting file.\nYou can continue from here without trouble\n\n");
931                 cfclose( fp );
932                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
933                 return;
934         }
935
936         // verify that we are loading the correct type of campaign file for the mode that we are in.
937         if(version >= 3)
938                 type_sig = cfread_int( fp );
939         else
940                 type_sig = CAMPAIGN_SINGLE_PLAYER_SIG;
941         // the actual check
942         SDL_assert( ((Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_MULTI_PLAYER_SIG)) || (!(Game_mode & GM_MULTIPLAYER) && (type_sig==CAMPAIGN_SINGLE_PLAYER_SIG)) );
943
944         Campaign.type = type_sig == CAMPAIGN_SINGLE_PLAYER_SIG ? CAMPAIGN_TYPE_SINGLE : CAMPAIGN_TYPE_MULTI_COOP;
945
946         // read in the filename of the campaign and compare the filenames to be sure that
947         // we are reading data that really belongs to this campaign.  I think that this check
948         // is redundant.
949         cfread_string_len( filename, SDL_arraysize(filename), fp );
950         /*if ( SDL_strcasecmp( filename, cfilename) ) { //      Used to be !SDL_strcasecmp.  How did this ever work? --MK, 11/9/97
951                 Warning(LOCATION, "Campaign save file appears corrupt because of mismatching filenames.");
952                 cfclose(fp);
953                 return;
954         }*/
955
956         Campaign.prev_mission = cfread_int( fp );
957         Campaign.next_mission = cfread_int( fp );
958         Campaign.loop_reentry = cfread_int( fp );
959         Campaign.loop_enabled = cfread_int( fp );
960
961         //  load information about ships/weapons allowed        
962         int ship_count, weapon_count;
963
964         // if earlier than mission disk version, use old MAX_SHIP_TYPES, otherwise read from the file           
965         if(version <= CAMPAIGN_INITIAL_RELEASE_FILE_VERSION){
966                 ship_count = CAMPAIGN_SAVEFILE_MAX_SHIPS_OLD;
967                 weapon_count = CAMPAIGN_SAVEFILE_MAX_WEAPONS_OLD;
968         } else {
969                 ship_count = cfread_int(fp);
970                 weapon_count = cfread_int(fp);
971         }
972
973         for ( i = 0; i < ship_count; i++ ){
974                 Campaign.ships_allowed[i] = cfread_ubyte( fp );
975         }
976
977         for ( i = 0; i < weapon_count; i++ ){
978                 Campaign.weapons_allowed[i] = cfread_ubyte( fp );
979         }       
980
981         // read in the completed mission matrix.  Used to tell which missions the player
982         // can replay in the simulator.  Also, each completed mission contains a list of the goals
983         // that were in the mission along with the goal completion status.
984         Campaign.num_missions_completed = cfread_int( fp );
985         for (i = 0; i < Campaign.num_missions_completed; i++ ) {
986                 num = cfread_int( fp );
987                 Campaign.missions[num].completed = 1;
988                 Campaign.missions[num].num_goals = cfread_int( fp );
989                 
990                 // be sure to malloc out space for the goals stuff, then zero the memory!!!  Don't do malloc
991                 // if there are no goals
992                 Campaign.missions[num].goals = (mgoal *)malloc( Campaign.missions[num].num_goals * sizeof(mgoal) );
993                 if ( Campaign.missions[num].num_goals > 0 ) {
994                         memset( Campaign.missions[num].goals, 0, sizeof(mgoal) * Campaign.missions[num].num_goals );
995                         SDL_assert( Campaign.missions[num].goals != NULL );
996                 }
997
998                 // now read in the goal information for this mission
999                 for ( j = 0; j < Campaign.missions[num].num_goals; j++ ) {
1000                         cfread_string_len( Campaign.missions[num].goals[j].name, NAME_LENGTH, fp );
1001                         Campaign.missions[num].goals[j].status = cfread_char( fp );
1002                 }
1003
1004                 // get the events from the savefile
1005                 Campaign.missions[num].num_events = cfread_int( fp );
1006                 
1007                 // be sure to malloc out space for the events stuff, then zero the memory!!!  Don't do malloc
1008                 // if there are no events
1009 //              if (Campaign.missions[num].events < 0)
1010 //                      Campaign.missions[num].events = 0;
1011                 Campaign.missions[num].events = (mevent *)malloc( Campaign.missions[num].num_events * sizeof(mevent) );
1012                 if ( Campaign.missions[num].num_events > 0 ) {
1013                         memset( Campaign.missions[num].events, 0, sizeof(mevent) * Campaign.missions[num].num_events );
1014                         SDL_assert( Campaign.missions[num].events != NULL );
1015                 }
1016                 
1017                 // now read in the event information for this mission
1018                 for ( j = 0; j < Campaign.missions[num].num_events; j++ ) {
1019                         cfread_string_len( Campaign.missions[num].events[j].name, NAME_LENGTH, fp );
1020                         Campaign.missions[num].events[j].status = cfread_char( fp );
1021                 }
1022
1023                 // now read flags
1024                 Campaign.missions[num].flags = cfread_int(fp);
1025         }       
1026
1027         cfclose( fp );
1028
1029         // 4/17/98
1030         // now, try and read in the campaign stats saved information.  This code was added for the 1.03 patch
1031         // since the stats data was never written out to disk.  We try and open the file, and if we cannot find
1032         // it, then simply return
1033         SDL_snprintf( filename, SDL_arraysize(filename), NOX("%s.%s.css"), Player->callsign, base );
1034
1035         fp = cfopen(filename, "rb", CFILE_NORMAL, CF_TYPE_SINGLE_PLAYERS );
1036         if ( !fp )
1037                 return;
1038
1039         id = cfread_int( fp );
1040         if ( id != CAMPAIGN_STATS_FILE_ID ) {
1041                 Warning(LOCATION, "Campaign stats save file has invalid signature");
1042                 cfclose( fp );
1043                 return;
1044         }
1045
1046         version = cfread_int( fp );
1047         if ( version < CAMPAIGN_STATS_FILE_COMPATIBLE_VERSION ) {
1048                 Warning(LOCATION, "Campaign save file too old -- not compatible.  Deleting file.\nYou can continue from here without trouble\n\n");
1049                 cfclose( fp );
1050                 cf_delete( filename, CF_TYPE_SINGLE_PLAYERS );
1051                 return;
1052         }
1053
1054         num_stats_blocks = cfread_int( fp );
1055         for (i = 0; i < num_stats_blocks; i++ ) {
1056                 num = cfread_int( fp );
1057                 cfread( &Campaign.missions[num].stats, sizeof(scoring_struct), 1, fp );
1058         }
1059
1060         cfclose(fp);
1061 }
1062
1063 // the following code only ever called by CSFE!!!!
1064 void campaign_savefile_load(const char *fname, const char *pname)
1065 {
1066         if (Campaign.type==CAMPAIGN_TYPE_SINGLE) {
1067                 Game_mode &= ~GM_MULTIPLAYER;
1068                 Game_mode &= GM_NORMAL;
1069         }
1070         else
1071                 Game_mode |= GM_MULTIPLAYER;
1072         SDL_strlcpy(Player->callsign, pname, SDL_arraysize(Player->callsign));
1073         mission_campaign_savefile_load(fname);
1074 }
1075
1076 // mission_campaign_next_mission sets up the internal veriables of the campaign
1077 // structure so the player can play the next mission.  If there are no more missions
1078 // available in the campaign, this function returns -1, else 0 if the mission was
1079 // set successfully
1080 int mission_campaign_next_mission()
1081 {
1082         if ( (Campaign.next_mission == -1) || (strlen(Campaign.name) == 0) ) // will be set to -1 when there is no next mission
1083                 return -1;
1084
1085         Campaign.current_mission = Campaign.next_mission;       
1086         SDL_strlcpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1087
1088         // check for end of loop.
1089         if (Campaign.current_mission == Campaign.loop_reentry) {
1090                 Campaign.loop_enabled = 0;
1091         }
1092
1093         // reset the number of persistent ships and weapons for the next campaign mission
1094         Num_granted_ships = 0;
1095         Num_granted_weapons = 0;
1096         return 0;
1097 }
1098
1099 // mission_campaign_previous_mission() gets called to go to the previous mission in
1100 // the campaign.  Used only for Red Alert missions
1101 int mission_campaign_previous_mission()
1102 {
1103         if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1104                 return 0;
1105
1106         if ( Campaign.prev_mission == -1 )
1107                 return 0;
1108
1109         Campaign.current_mission = Campaign.prev_mission;
1110         Campaign.next_mission = Campaign.current_mission;
1111         Campaign.num_missions_completed--;
1112         Campaign.missions[Campaign.next_mission].completed = 0;
1113         mission_campaign_savefile_save();
1114
1115         // reset the player stats to be the stats from this level
1116         memcpy( &Player->stats, &Campaign.missions[Campaign.current_mission].stats, sizeof(Player->stats) );
1117
1118         SDL_strlcpy( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name, MAX_FILENAME_LEN );
1119         Num_granted_ships = 0;
1120         Num_granted_weapons = 0;
1121
1122         return 1;
1123 }
1124
1125 /*
1126 // determine what the next mission is after the current one.  Because this evaluates an sexp,
1127 // and that could check just about anything, the results are only going to be valid in
1128 // certain places.
1129 // DA 12/09/98 -- To allow for mission loops, need to maintain call with store stats
1130 int mission_campaign_eval_next_mission( int store_stats )
1131 {
1132         char *name;
1133         int cur, i;
1134         cmission *mission;
1135
1136         Campaign.next_mission = -1;
1137         cur = Campaign.current_mission;
1138         name = Campaign.missions[cur].name;
1139
1140         mission = &Campaign.missions[cur];
1141
1142         // first we must save the status of the current missions goals in the campaign mission structure.
1143         // After that, we can determine which mission is tagged as the next mission.  Finally, we
1144         // can save the campaign save file
1145         // we might have goal and event status if the player replayed a mission
1146         if ( mission->num_goals > 0 ) {
1147                 free( mission->goals );
1148         }
1149
1150         mission->num_goals = Num_goals;
1151         if ( mission->num_goals > 0 ) {
1152                 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1153                 SDL_assert( mission->goals != NULL );
1154         }
1155
1156         // copy the needed info from the Mission_goal struct to our internal structure
1157         for (i = 0; i < Num_goals; i++ ) {
1158                 if ( strlen(Mission_goals[i].name) == 0 ) {
1159                         char name[NAME_LENGTH];
1160
1161                         sprintf(name, NOX("Goal #%d"), i);
1162                         //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1163                         strcpy( mission->goals[i].name, name);
1164                 } else
1165                         strcpy( mission->goals[i].name, Mission_goals[i].name );
1166                 SDL_assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE );           // should be true or false at this point!!!
1167                 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1168         }
1169
1170         // do the same thing for events as we did for goals
1171         // we might have goal and event status if the player replayed a mission
1172         if ( mission->num_events > 0 ) {
1173                 free( mission->events );
1174         }
1175
1176         mission->num_events = Num_mission_events;
1177         if ( mission->num_events > 0 ) {
1178                 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1179                 SDL_assert( mission->events != NULL );
1180         }
1181
1182         // copy the needed info from the Mission_goal struct to our internal structure
1183         for (i = 0; i < Num_mission_events; i++ ) {
1184                 if ( strlen(Mission_events[i].name) == 0 ) {
1185                         char name[NAME_LENGTH];
1186
1187                         sprintf(name, NOX("Event #%d"), i);
1188                         nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1189                         strcpy( mission->events[i].name, name);
1190                 } else
1191                         strcpy( mission->events[i].name, Mission_events[i].name );
1192
1193                 // getting status for the events is a little different.  If the formula value for the event entry
1194                 // is -1, then we know the value of the result field will never change.  If the formula is
1195                 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1196                 // event evaluation
1197                 if ( Mission_events[i].formula == -1 ) {
1198                         if ( Mission_events[i].result )
1199                                 mission->events[i].status = EVENT_SATISFIED;
1200                         else
1201                                 mission->events[i].status = EVENT_FAILED;
1202                 } else
1203                         Int3();
1204         }
1205
1206         // maybe store the alltime stats which would be current at the end of this mission
1207         if ( store_stats ) {
1208                 memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1209                 scoring_backout_accept( &mission->stats );
1210         }
1211
1212         if ( store_stats ) {    // second (last) time through, so use choose loop_mission if chosen
1213                 if ( Campaign.loop_enabled ) {
1214                         Campaign.next_mission = Campaign.loop_mission;
1215                 } else {
1216                         // evaluate next mission (straight path)
1217                         if (Campaign.missions[cur].formula != -1) {
1218                                 flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1219                                 eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1220                         }
1221                 }
1222         } else {
1223
1224                 // evaluate next mission (straight path)
1225                 if (Campaign.missions[cur].formula != -1) {
1226                         flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1227                         eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1228                 }
1229
1230                 // evaluate mission loop mission (if any) so it can be used if chosen
1231                 if ( Campaign.missions[cur].has_mission_loop ) {
1232                         int copy_next_mission = Campaign.next_mission;
1233                         // Set temporarily to -1 so we know if loop formula fails to assign
1234                         Campaign.next_mission = -1;  // Cannot exit campaign from loop
1235                         if (Campaign.missions[cur].mission_loop_formula != -1) {
1236                                 flush_sexp_tree(Campaign.missions[cur].mission_loop_formula);  // force formula to be re-evaluated
1237                                 eval_sexp(Campaign.missions[cur].mission_loop_formula);  // this should reset Campaign.next_mission to proper value
1238                         }
1239
1240                         Campaign.loop_mission = Campaign.next_mission;
1241                         Campaign.next_mission = copy_next_mission;
1242                 }
1243         }
1244
1245         if (Campaign.next_mission == -1)
1246                 nprintf(("allender", "No next mission to proceed to.\n"));
1247         else
1248                 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1249
1250         return Campaign.next_mission;
1251 } */
1252
1253 // Evaluate next campaign mission - set as Campaign.next_mission.  Also set Campaign.loop_mission
1254 void mission_campaign_eval_next_mission()
1255 {
1256         Campaign.next_mission = -1;
1257         int cur = Campaign.current_mission;
1258
1259         // evaluate next mission (straight path)
1260         if (Campaign.missions[cur].formula != -1) {
1261                 flush_sexp_tree(Campaign.missions[cur].formula);  // force formula to be re-evaluated
1262                 eval_sexp(Campaign.missions[cur].formula);  // this should reset Campaign.next_mission to proper value
1263         }
1264
1265         // evaluate mission loop mission (if any) so it can be used if chosen
1266         if ( Campaign.missions[cur].has_mission_loop ) {
1267                 int copy_next_mission = Campaign.next_mission;
1268                 // Set temporarily to -1 so we know if loop formula fails to assign
1269                 Campaign.next_mission = -1;
1270                 if (Campaign.missions[cur].mission_loop_formula != -1) {
1271                         flush_sexp_tree(Campaign.missions[cur].mission_loop_formula);  // force formula to be re-evaluated
1272                         eval_sexp(Campaign.missions[cur].mission_loop_formula);  // this should reset Campaign.next_mission to proper value
1273                 }
1274
1275                 Campaign.loop_mission = Campaign.next_mission;
1276                 Campaign.next_mission = copy_next_mission;
1277         }
1278
1279         if (Campaign.next_mission == -1) {
1280                 nprintf(("allender", "No next mission to proceed to.\n"));
1281         } else {
1282                 nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
1283         }
1284
1285 }
1286
1287 // Store mission's goals and events in Campaign struct
1288 void mission_campaign_store_goals_and_events()
1289 {
1290         int cur, i;
1291         cmission *mission;
1292
1293         cur = Campaign.current_mission;
1294
1295         mission = &Campaign.missions[cur];
1296
1297         // first we must save the status of the current missions goals in the campaign mission structure.
1298         // After that, we can determine which mission is tagged as the next mission.  Finally, we
1299         // can save the campaign save file
1300         // we might have goal and event status if the player replayed a mission
1301         if ( mission->num_goals > 0 ) {
1302                 free( mission->goals );
1303         }
1304
1305         mission->num_goals = Num_goals;
1306         if ( mission->num_goals > 0 ) {
1307                 mission->goals = (mgoal *)malloc( sizeof(mgoal) * Num_goals );
1308                 SDL_assert( mission->goals != NULL );
1309         }
1310
1311         // copy the needed info from the Mission_goal struct to our internal structure
1312         for (i = 0; i < Num_goals; i++ ) {
1313                 if ( strlen(Mission_goals[i].name) == 0 ) {
1314                         char name[NAME_LENGTH];
1315
1316                         SDL_snprintf(name, SDL_arraysize(name), NOX("Goal #%d"), i);
1317                         //Warning(LOCATION, "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name);
1318                         SDL_strlcpy( mission->goals[i].name, name, SDL_arraysize(mission->goals[0].name));
1319                 } else
1320                         SDL_strlcpy( mission->goals[i].name, Mission_goals[i].name, SDL_arraysize(mission->goals[0].name) );
1321                 SDL_assert ( Mission_goals[i].satisfied != GOAL_INCOMPLETE );           // should be true or false at this point!!!
1322                 mission->goals[i].status = (char)Mission_goals[i].satisfied;
1323         }
1324
1325         // do the same thing for events as we did for goals
1326         // we might have goal and event status if the player replayed a mission
1327         if ( mission->num_events > 0 ) {
1328                 free( mission->events );
1329         }
1330
1331         mission->num_events = Num_mission_events;
1332         if ( mission->num_events > 0 ) {
1333                 mission->events = (mevent *)malloc( sizeof(mevent) * Num_mission_events );
1334                 SDL_assert( mission->events != NULL );
1335         }
1336
1337         // copy the needed info from the Mission_goal struct to our internal structure
1338         for (i = 0; i < Num_mission_events; i++ ) {
1339                 if ( strlen(Mission_events[i].name) == 0 ) {
1340                         char name[NAME_LENGTH];
1341
1342                         SDL_snprintf(name, SDL_arraysize(name), NOX("Event #%d"), i);
1343                         nprintf(("Warning", "Mission goal in mission %s must have a +Name field! using %s for campaign save file\n", mission->name, name));
1344                         SDL_strlcpy( mission->events[i].name, name, SDL_arraysize(mission->events[0].name));
1345                 } else
1346                         SDL_strlcpy( mission->events[i].name, Mission_events[i].name, SDL_arraysize(mission->events[0].name) );
1347
1348                 // getting status for the events is a little different.  If the formula value for the event entry
1349                 // is -1, then we know the value of the result field will never change.  If the formula is
1350                 // not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
1351                 // event evaluation
1352                 if ( Mission_events[i].formula == -1 ) {
1353                         if ( Mission_events[i].result )
1354                                 mission->events[i].status = EVENT_SATISFIED;
1355                         else
1356                                 mission->events[i].status = EVENT_FAILED;
1357                 } else
1358                         Int3();
1359         }
1360 }
1361
1362 // this function is called when the player's mission is over.  It updates the internal store of goals
1363 // and their status then saves the state of the campaign in the campaign file.  This gets called
1364 // after player accepts mission results in debriefing.
1365 void mission_campaign_mission_over()
1366 {
1367         int mission_num, i;
1368         cmission *mission;
1369
1370         // I don't think that we should have a record for these -- maybe we might??????  If we do,
1371         // then we should free them
1372         if ( !(Game_mode & GM_CAMPAIGN_MODE) ){
1373                 return;
1374         }
1375
1376         mission_num = Campaign.current_mission;
1377         SDL_assert( mission_num != -1 );
1378         mission = &Campaign.missions[mission_num];
1379
1380         // determine if any ships/weapons were granted this mission
1381         for ( i=0; i<Num_granted_ships; i++ ){
1382                 Campaign.ships_allowed[Granted_ships[i]] = 1;
1383         }
1384
1385         for ( i=0; i<Num_granted_weapons; i++ ){
1386                 Campaign.weapons_allowed[Granted_weapons[i]] = 1;       
1387         }
1388
1389         // DKA 12/11/98 - Unneeded already evaluated and stored
1390         // determine what new mission we are moving to.
1391         //      mission_campaign_eval_next_mission(1);
1392
1393         // update campaign.mission stats (used to allow backout inRedAlert)
1394         memcpy( &mission->stats, &Player->stats, sizeof(Player->stats) );
1395         if(!(Game_mode & GM_MULTIPLAYER)){
1396                 scoring_backout_accept( &mission->stats );
1397         }
1398
1399         // if we are moving to a new mission, then change our data.  If we are staying on the same mission,
1400         // then we don't want to do anything.  Remove information about goals/events
1401         if ( Campaign.next_mission != mission_num ) {
1402                 Campaign.prev_mission = mission_num;
1403                 Campaign.current_mission = -1;
1404                 Campaign.num_missions_completed++;
1405                 Campaign.missions[mission_num].completed = 1;
1406
1407                 // save the scoring values from the previous mission at the start of this mission -- for red alert
1408
1409                 // save the state of the campaign in the campaign save file and move to the end_game state
1410                 if (Campaign.type == CAMPAIGN_TYPE_SINGLE) {
1411                         mission_campaign_savefile_save();
1412                 }
1413
1414         } else {
1415                 // free up the goals and events which were just malloced.  It's kind of like erasing any fact
1416                 // that the player played this mission in the campaign.
1417                 free( mission->goals );
1418                 mission->num_goals = 0;
1419
1420                 free( mission->events );
1421                 mission->num_events = 0;
1422
1423                 Sexp_nodes[mission->formula].value = SEXP_UNKNOWN;
1424         }
1425
1426         SDL_assert(Player);
1427         if (Campaign.missions[Campaign.next_mission].flags & CMISSION_FLAG_BASTION){
1428                 Player->on_bastion = 1;
1429         } else {
1430                 Player->on_bastion = 0;
1431         }
1432
1433         mission_campaign_next_mission();                        // sets up whatever needs to be set to actually play next mission
1434 }
1435
1436 // called when the game closes -- to get rid of memory errors for Bounds checker
1437 void mission_campaign_close()
1438 {
1439         int i;
1440
1441         if (Campaign.desc)
1442                 free(Campaign.desc);
1443
1444         // be sure to remove all old malloced strings of Mission_names
1445         // we must also free any goal stuff that was from a previous campaign
1446         for ( i=0; i<Campaign.num_missions; i++ ) {
1447                 if ( Campaign.missions[i].name ){
1448                         free(Campaign.missions[i].name);
1449                 }
1450
1451                 if (Campaign.missions[i].notes){
1452                         free(Campaign.missions[i].notes);
1453                 }
1454
1455                 if ( Campaign.missions[i].num_goals > 0 ){
1456                         free ( Campaign.missions[i].goals );
1457                 }
1458
1459                 if ( Campaign.missions[i].num_events > 0 ){
1460                         free ( Campaign.missions[i].events );
1461                 }
1462
1463                 // the next three are strdup'd return values from parselo.cpp
1464                 if (Campaign.missions[i].mission_loop_desc) {
1465                         free(Campaign.missions[i].mission_loop_desc);
1466                 }
1467
1468                 if (Campaign.missions[i].mission_loop_brief_anim) {
1469                         free(Campaign.missions[i].mission_loop_brief_anim);
1470                 }
1471
1472                 if (Campaign.missions[i].mission_loop_brief_sound) {
1473                         free(Campaign.missions[i].mission_loop_brief_sound);
1474                 }
1475
1476                 if ( !Fred_running ){
1477                         sexp_unmark_persistent(Campaign.missions[i].formula);           // free any sexpression nodes used by campaign.
1478                 }
1479
1480                 Campaign.missions[i].num_goals = 0;
1481                 Campaign.missions[i].num_events = 0;
1482         }
1483 }
1484
1485 // call from game_shutdown() ONLY!!!
1486 void mission_campaign_shutdown()
1487 {
1488         int i;
1489         
1490         for (i=0; i<MAX_CAMPAIGNS; i++) {
1491                 if (Campaign_names[i] != NULL) {
1492                         free(Campaign_names[i]);
1493                         Campaign_names[i] = NULL;
1494                 }
1495
1496                 if (Campaign_file_names[i] != NULL) {
1497                         free(Campaign_file_names[i]);
1498                         Campaign_file_names[i] = NULL;
1499                 }
1500         }
1501 }
1502
1503 // extract the mission filenames for a campaign.  
1504 //
1505 // filename     =>      name of campaign file
1506 //      dest            => storage for the mission filename, must be already allocated
1507 // num          => output parameter for the number of mission filenames in the campaign
1508 //
1509 // note that dest should allocate at least dest[MAX_CAMPAIGN_MISSIONS][NAME_LENGTH]
1510 int mission_campaign_get_filenames(const char *filename, char dest[][NAME_LENGTH], int *num)
1511 {
1512         // read the mission file and get the list of mission filenames
1513         try {
1514                 read_file_text(filename);
1515                 SDL_assert(strlen(filename) < MAX_FILENAME_LEN - 1);  // make sure no overflow
1516
1517                 reset_parse();
1518                 required_string("$Name:");
1519                 advance_to_eoln(NULL);
1520
1521                 required_string( "$Type:" );
1522                 advance_to_eoln(NULL);
1523
1524                 // parse the mission file and actually read in the mission stuff
1525                 *num = 0;
1526                 while ( skip_to_string("$Mission:") == 1 ) {
1527                         stuff_string(dest[*num], F_NAME, NULL);
1528                         (*num)++;
1529                 }
1530         } catch (parse_error_t rval) {
1531                 return (int)rval;
1532         }
1533
1534         return 0;
1535 }
1536
1537 // function to read the goals and events from a mission in a campaign file and store that information
1538 // in the campaign structure for use in the campaign editor, error checking, etc
1539 void read_mission_goal_list(int num)
1540 {
1541         char *filename, notes[NOTES_LENGTH], goals[MAX_GOALS][NAME_LENGTH];
1542         char events[MAX_MISSION_EVENTS][NAME_LENGTH];
1543         int i, z, event_count, count = 0;
1544
1545         filename = Campaign.missions[num].name;
1546
1547         // open localization
1548         lcl_ext_open(); 
1549
1550         // default counts to zero
1551         event_count = 0;
1552         count = 0;
1553
1554         try {
1555                 read_file_text(filename);
1556                 init_parse();
1557
1558                 // first, read the mission notes for this mission.  Used in campaign editor
1559                 if (skip_to_string("#Mission Info")) {
1560                         if (skip_to_string("$Notes:")) {
1561                                 stuff_string(notes, F_NOTES, NULL);
1562                                 if (Campaign.missions[num].notes){
1563                                         free(Campaign.missions[num].notes);
1564                                 }
1565
1566                                 Campaign.missions[num].notes = (char *) malloc(strlen(notes) + 1);
1567                                 SDL_strlcpy(Campaign.missions[num].notes, notes, strlen(notes) + 1);
1568                         }
1569                 }
1570
1571                 // skip to events section in the mission file.  Events come before goals, so we process them first
1572                 if ( skip_to_string("#Events") ) {
1573                         while (1) {
1574                                 if (skip_to_string("$Formula:", "#Goals") != 1){
1575                                         break;
1576                                 }
1577
1578                                 z = skip_to_string("+Name:", "$Formula:");
1579                                 if (!z){
1580                                         break;
1581                                 }
1582
1583                                 if (z == 1){
1584                                         stuff_string(events[event_count], F_NAME, NULL);
1585                                 } else {
1586                                         SDL_snprintf(events[event_count], NAME_LENGTH, NOX("Event #%d"), event_count + 1);
1587                                 }
1588
1589                                 event_count++;
1590                                 SDL_assert(event_count < MAX_MISSION_EVENTS);
1591                         }
1592                 }
1593
1594                 if (skip_to_string("#Goals")) {
1595                         while (1) {
1596                                 if (skip_to_string("$Type:", "#End") != 1){
1597                                         break;
1598                                 }
1599
1600                                 z = skip_to_string("+Name:", "$Type:");
1601                                 if (!z){
1602                                         break;
1603                                 }
1604
1605                                 if (z == 1){
1606                                         stuff_string(goals[count], F_NAME, NULL);
1607                                 } else {
1608                                         SDL_snprintf(goals[count], NAME_LENGTH, NOX("Goal #%d"), count + 1);
1609                                 }
1610
1611                                 count++;
1612                                 SDL_assert(count < MAX_GOALS);
1613                         }
1614                 }
1615         } catch (parse_error_t rval) {
1616                 Warning(LOCATION, "Error reading \"%s\" (code = %d)", filename, (int)rval);
1617         }
1618
1619         Campaign.missions[num].num_goals = count;
1620         if (count) {
1621                 Campaign.missions[num].goals = (mgoal *) malloc(count * sizeof(mgoal));
1622                 SDL_assert(Campaign.missions[num].goals);  // make sure we got the memory
1623                 memset(Campaign.missions[num].goals, 0, count * sizeof(mgoal));
1624
1625                 for (i=0; i<count; i++){
1626                         SDL_strlcpy(Campaign.missions[num].goals[i].name, goals[i], SDL_arraysize(Campaign.missions[num].goals[i].name));
1627                 }
1628         }
1629                 // copy the events
1630         Campaign.missions[num].num_events = event_count;
1631         if (event_count) {
1632                 Campaign.missions[num].events = (mevent *)malloc(event_count * sizeof(mevent));
1633                 SDL_assert ( Campaign.missions[num].events );
1634                 memset(Campaign.missions[num].events, 0, event_count * sizeof(mevent));
1635
1636                 for (i = 0; i < event_count; i++ ){
1637                         SDL_strlcpy(Campaign.missions[num].events[i].name, events[i], SDL_arraysize(Campaign.missions[num].events[i].name));
1638                 }
1639         }
1640
1641         // close localization
1642         lcl_ext_close();
1643 }
1644
1645 // function to return index into Campaign's list of missions of the mission with the given
1646 // filename.  This function tried to be a little smart about filename looking for the .fsm
1647 // extension since filenames are stored with the extension in the campaign file.  Returns
1648 // index of mission in campaign structure.  -1 if mission name not found.
1649 int mission_campaign_find_mission( const char *name )
1650 {
1651         int i;
1652         char realname[MAX_FILENAME_LEN];
1653
1654         // look for an extension on the file.  If no extension, add default ".fsm" onto the
1655         // end of the filename
1656         if ( SDL_strchr(name, '.') == NULL ){
1657                 SDL_snprintf(realname, SDL_arraysize(realname), NOX("%s%s"), name, FS_MISSION_FILE_EXT );
1658         } else {
1659                 SDL_strlcpy(realname, name, SDL_arraysize(realname));
1660         }
1661
1662         for (i = 0; i < Campaign.num_missions; i++ ) {
1663                 if ( !SDL_strcasecmp(realname, Campaign.missions[i].name) ){
1664                         return i;
1665                 }
1666         }
1667
1668         return -1;
1669 }
1670
1671 void mission_campaign_maybe_play_movie(int type)
1672 {
1673         int mission;
1674         char *filename;
1675
1676         // only support pre mission movies for now.
1677         SDL_assert ( type == CAMPAIGN_MOVIE_PRE_MISSION );
1678
1679         if ( !(Game_mode & GM_CAMPAIGN_MODE) )
1680                 return;
1681
1682         mission = Campaign.current_mission;
1683         SDL_assert( mission != -1 );
1684
1685         // get a possible filename for a movie to play.
1686         filename = NULL;
1687         switch( type ) {
1688         case CAMPAIGN_MOVIE_PRE_MISSION:
1689                 if ( strlen(Campaign.missions[mission].briefing_cutscene) )
1690                         filename = Campaign.missions[mission].briefing_cutscene;
1691                 break;
1692
1693         default:
1694                 Int3();
1695                 break;
1696         }
1697
1698         // no filename, no movie!
1699         if ( !filename )
1700                 return;
1701
1702         movie_play( filename );
1703 }
1704
1705 // return nonzero if the passed filename is a multiplayer campaign, 0 otherwise
1706 int mission_campaign_parse_is_multi(const char *filename, char *name, const int max_len)
1707 {       
1708         int i;
1709         char temp[50];
1710
1711         try {
1712                 read_file_text( filename );
1713                 reset_parse();
1714
1715                 required_string("$Name:");
1716                 stuff_string( temp, F_NAME, NULL );
1717                 if ( name )
1718                         SDL_strlcpy( name, temp, max_len );
1719
1720                 required_string( "$Type:" );
1721                 stuff_string( temp, F_NAME, NULL );
1722
1723                 for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
1724                         if ( !SDL_strcasecmp(temp, campaign_types[i]) ) {
1725                                 return i;
1726                         }
1727                 }
1728         } catch (parse_error_t rval) {
1729                 Error(LOCATION, "Unable to parse %s!  Code = %i.\n", filename, (int)rval);
1730         }
1731
1732         Error(LOCATION, "Unknown campaign type %s", temp );
1733         return -1;
1734 }
1735
1736 // functions to save persistent information during a mission -- which then might or might not get
1737 // saved out when the mission is over depending on whether player replays mission or commits.
1738 void mission_campaign_save_persistent( int type, int sindex )
1739 {
1740         // based on the type of information, save it off for possible saving into the campsign
1741         // savefile when the mission is over
1742         if ( type == CAMPAIGN_PERSISTENT_SHIP ) {
1743                 SDL_assert( Num_granted_ships < MAX_SHIP_TYPES );
1744                 Granted_ships[Num_granted_ships] = sindex;
1745                 Num_granted_ships++;
1746         } else if ( type == CAMPAIGN_PERSISTENT_WEAPON ) {
1747                 SDL_assert( Num_granted_weapons < MAX_WEAPON_TYPES );
1748                 Granted_weapons[Num_granted_weapons] = sindex;
1749                 Num_granted_weapons++;
1750         } else
1751                 Int3();
1752 }
1753
1754 // returns 0: loaded, !0: error
1755 int mission_load_up_campaign()
1756 {
1757         if (strlen(Player->current_campaign))
1758                 return mission_campaign_load(Player->current_campaign);
1759         else
1760                 return mission_campaign_load(BUILTIN_CAMPAIGN);
1761 }
1762
1763 // for end of campaign in the single player game.  Called when the end of campaign state is
1764 // entered, which is triggered when the end-campaign sexpression is hit
1765
1766 void mission_campaign_end_init()
1767 {
1768         // no need to do any initialization.
1769 }
1770
1771 void mission_campaign_end_do()
1772 {
1773         // play the movies
1774         event_music_level_close();
1775         mission_goal_fail_incomplete();
1776         scoring_level_close();
1777         mission_campaign_mission_over();
1778
1779 #ifdef MAKE_FS1
1780         movie_play("endgame.mve");
1781 #else
1782         // eventually we'll want to play one of two options (good ending or bad ending)
1783         // did the supernova blow?
1784         if(Supernova_status == SUPERNOVA_HIT){
1785                 movie_play_two("endpart1.mve", "endprt2b.mve");                 // good ending
1786         } else {
1787                 movie_play_two("endpart1.mve", "endprt2a.mve");                 // good ending
1788         }
1789 #endif
1790
1791 #if defined(FS2_DEMO) || defined(FS1_DEMO)
1792         gameseq_post_event( GS_EVENT_END_DEMO );
1793 #elif defined(MAKE_FS1)
1794         gameseq_post_event( GS_STATE_END_OF_CAMPAIGN );
1795 #else
1796         gameseq_post_event( GS_EVENT_MAIN_MENU );
1797 #endif
1798 }
1799
1800 void mission_campaign_end_close()
1801 {
1802         // nothing to do here.
1803 }
1804
1805
1806 // skip to the next mission in the campaign
1807 // this also posts the state change by default.  pass 0 to override that
1808 void mission_campaign_skip_to_next(int start_game)
1809 {
1810         // mark all goals/events complete
1811         // these do not really matter, since is-previous-event-* and is-previous-goal-* sexps check
1812         // to see if the mission was skipped, and use defaults accordingly.
1813         mission_goal_mark_objectives_complete();
1814         mission_goal_mark_events_complete();
1815
1816         // mark mission as skipped
1817         Campaign.missions[Campaign.current_mission].flags |= CMISSION_FLAG_SKIPPED;
1818
1819         // store
1820         mission_campaign_store_goals_and_events();
1821
1822         // now set the next mission
1823         mission_campaign_eval_next_mission();
1824
1825         // clear out relevant player vars
1826         Player->failures_this_session = 0;
1827         Player->show_skip_popup = 1;
1828
1829         if (start_game) {
1830                 // proceed to next mission or main hall
1831                 if ((Campaign.missions[Campaign.current_mission].has_mission_loop) && (Campaign.loop_mission != -1)) {
1832                         // go to loop solicitation
1833                         gameseq_post_event(GS_EVENT_LOOP_BRIEF);
1834                 } else {
1835                         // closes out mission stuff, sets up next one
1836                         mission_campaign_mission_over();
1837
1838                         if ( Campaign.next_mission == -1 ) {
1839                                 // go to main hall, tha campaign is over!
1840                                 gameseq_post_event(GS_EVENT_MAIN_MENU);
1841                         } else {
1842                                 // go to next mission
1843                                 gameseq_post_event(GS_EVENT_START_GAME);
1844                         }
1845                 }
1846         }
1847 }
1848
1849
1850 // breaks your ass out of the loop
1851 // this also posts the state change
1852 void mission_campaign_exit_loop()
1853 {
1854         // set campaign to loop reentry point
1855         Campaign.next_mission = Campaign.loop_reentry;
1856         Campaign.current_mission = -1;
1857         Campaign.loop_enabled = 0;
1858
1859         // set things up for next mission
1860         mission_campaign_next_mission();
1861         gameseq_post_event(GS_EVENT_START_GAME);
1862 }
1863
1864
1865 // used for jumping to a particular campaign mission
1866 // all pvs missions marked skipped
1867 // this relies on correct mission ordering in the campaign file
1868 void mission_campaign_jump_to_mission(const char *name)
1869 {
1870         int i = 0;
1871         char dest_name[64] = { 0 };
1872
1873         // load in the campaign junk
1874         mission_load_up_campaign();
1875
1876         // tack the .fs2 onto the input name
1877         SDL_strlcpy(dest_name, cf_add_ext(name, FS_MISSION_FILE_EXT), SDL_arraysize(dest_name));
1878
1879         // search for our mission
1880         for (i=0; i<Campaign.num_missions; i++) {
1881                 if ((Campaign.missions[i].name != NULL) && !SDL_strcasecmp(Campaign.missions[i].name, dest_name) ) {
1882                         Campaign.next_mission = i;
1883                         Campaign.prev_mission = i-1;
1884                         mission_campaign_next_mission();
1885                         Game_mode |= GM_CAMPAIGN_MODE;
1886                         gameseq_post_event(GS_EVENT_START_GAME);
1887                         return;
1888                 } else {
1889                         Campaign.missions[i].flags |= CMISSION_FLAG_SKIPPED;
1890                         Campaign.num_missions_completed = i;
1891                 }
1892         }
1893
1894         // if we got here, no match was found
1895         // restart the campaign
1896         mission_campaign_savefile_delete(Campaign.filename);
1897         mission_campaign_load(Campaign.filename);
1898 }
1899