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