-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathmissioncampaign.cpp
More file actions
1956 lines (1624 loc) · 58 KB
/
Copy pathmissioncampaign.cpp
File metadata and controls
1956 lines (1624 loc) · 58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#include <cstdio>
#include <cstring>
#include <csetjmp>
#include <cerrno>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
#endif
#ifdef SCP_UNIX
#include <sys/stat.h>
#include <glob.h>
#endif
#include "freespace.h"
#include "missioncampaign.h"
#include "cfile/cfile.h"
#include "cutscene/cutscenes.h"
#include "cutscene/movie.h"
#include "gamesequence/gamesequence.h"
#include "gamesnd/eventmusic.h"
#include "localization/localize.h"
#include "menuui/mainhallmenu.h"
#include "menuui/techmenu.h"
#include "mission/missioncampaign.h"
#include "mission/missiongoals.h"
#include "missionui/missionscreencommon.h"
#include "missionui/redalert.h"
#include "parse/parselo.h"
#include "parse/sexp.h"
#include "parse/sexp_container.h"
#include "pilotfile/pilotfile.h"
#include "playerman/player.h"
#include "popup/popup.h"
#include "scripting/global_hooks.h"
#include "scripting/scripting.h"
#include "ship/ship.h"
#include "starfield/supernova.h"
#include "ui/ui.h"
#include "utils/string_utils.h"
#include "weapon/weapon.h"
// campaign wasn't ended
int Campaign_ending_via_supernova = 0;
// stuff for selecting campaigns. We need to keep both arrays around since we display the
// list of campaigns by name, but must load campaigns by filename
char *Campaign_names[MAX_CAMPAIGNS] = { NULL };
char *Campaign_file_names[MAX_CAMPAIGNS] = { NULL };
char *Campaign_descs[MAX_CAMPAIGNS] = { NULL };
int Num_campaigns;
bool Campaign_file_missing = false;
int Campaign_load_failure = 0;
int Campaign_names_inited = 0;
SCP_vector<SCP_string> Ignored_campaigns;
char Default_campaign_file_name[MAX_FILENAME_LEN - 4] = { 0 };
// stuff used for campaign list building
static bool MC_desc = false;
static bool MC_multiplayer = false;
const char *campaign_types[MAX_CAMPAIGN_TYPES] =
{
//XSTR:OFF
"single",
"multi coop",
"multi teams"
//XSTR:ON
};
// modules local variables to deal with getting new ships/weapons available to the player
int Num_granted_ships, Num_granted_weapons; // per mission counts of new ships and weapons
int Granted_ships[MAX_SHIP_CLASSES];
int Granted_weapons[MAX_WEAPON_TYPES];
// variables to control the UI stuff for loading campaigns
LOCAL UI_WINDOW Campaign_window;
LOCAL UI_LISTBOX Campaign_listbox;
LOCAL UI_BUTTON Campaign_okb, Campaign_cancelb;
// the campaign!!!!!
campaign Campaign;
/**
* Returns a string (which is malloced in this routine) of the name of the given freespace campaign file.
* In the type field, we return if the campaign is a single player or multiplayer campaign.
* The type field will only be valid if the name returned is non-NULL
*/
bool mission_campaign_get_info(const char *filename, SCP_string &name, int *type, int *max_players, char **desc, char **first_mission)
{
int i, success = false;
SCP_string campaign_type;
char fname[MAX_FILENAME_LEN];
Assert( type != NULL );
// make sure outputs always have sane values
name.clear();
*type = -1;
if (max_players) {
*max_players = 0;
}
if (desc) {
*desc = nullptr;
}
if (first_mission) {
*first_mission = nullptr;
}
strncpy(fname, filename, MAX_FILENAME_LEN - 1);
auto fname_len = strlen(fname);
if ((fname_len < 4) || stricmp(fname + fname_len - 4, FS_CAMPAIGN_FILE_EXT) != 0){
strcat_s(fname, FS_CAMPAIGN_FILE_EXT);
fname_len += 4;
}
Assert(fname_len < MAX_FILENAME_LEN);
do {
try
{
read_file_text(fname);
reset_parse();
required_string("$Name:");
stuff_string(name, F_NAME);
if (name.empty()) {
nprintf(("Warning", "No name found for campaign file %s\n", filename));
break;
}
required_string("$Type:");
stuff_string(campaign_type, F_NAME);
for (i = 0; i < MAX_CAMPAIGN_TYPES; i++) {
if (!stricmp(campaign_type.c_str(), campaign_types[i])) {
*type = i;
}
}
if (*type < 0) {
Warning(LOCATION, "Invalid campaign type \"%s\"\n", campaign_type.c_str());
break;
}
if (desc) {
if (optional_string("+Description:")) {
*desc = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
else {
*desc = NULL;
}
}
// if this is a multiplayer campaign, get the max players
if ((*type) != CAMPAIGN_TYPE_SINGLE) {
skip_to_string("+Num Players:");
stuff_int(max_players);
// Cyborg17 - and the first mission name if we want it, too
if (first_mission) {
skip_to_string("$Mission:");
*first_mission = stuff_and_malloc_string(F_NAME, nullptr);
}
}
// if we found a valid campaign type
if ((*type) >= 0) {
success = true;
}
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", fname, e.what()));
break;
}
} while (0);
return success;
}
/**
* Parses campaign and returns a list of missions in it.
* @return Number of missions added to the 'list', and up to 'max' missions may be added to 'list'.
* @return Negative on error.
*/
int mission_campaign_get_mission_list(const char *filename, char **list, int max)
{
int i, num = 0;
char name[MAX_FILENAME_LEN];
filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
// read the campaign file and get the list of mission filenames
try
{
read_file_text(filename);
reset_parse();
while (skip_to_string("$Mission:") > 0) {
stuff_string(name, F_NAME, MAX_FILENAME_LEN);
if (num < max)
list[num++] = vm_strdup(name);
else
Warning(LOCATION, "Maximum number of missions exceeded (%d)!", max);
}
}
catch (const parse::ParseException& e)
{
mprintf(("MISSIONCAMPAIGN: Unable to parse '%s'! Error message = %s.\n", filename, e.what()));
// since we can't return count of allocated elements, free them instead
for (i = 0; i<num; i++)
vm_free(list[i]);
num = -1;
}
return num;
}
void mission_campaign_free_list()
{
int i;
if ( !Campaign_names_inited )
return;
for (i = 0; i < Num_campaigns; i++) {
if (Campaign_names[i] != NULL) {
vm_free(Campaign_names[i]);
Campaign_names[i] = NULL;
}
if (Campaign_file_names[i] != NULL) {
vm_free(Campaign_file_names[i]);
Campaign_file_names[i] = NULL;
}
if (Campaign_descs[i] != NULL) {
vm_free(Campaign_descs[i]);
Campaign_descs[i] = NULL;
}
}
Num_campaigns = 0;
Campaign_names_inited = 0;
}
int mission_campaign_maybe_add(const char *filename)
{
SCP_string name;
char *desc = NULL;
int type, max_players;
// don't add ignored campaigns
if (campaign_is_ignored(filename)) {
return 0;
}
if ( mission_campaign_get_info( filename, name, &type, &max_players, &desc) ) {
if ( !MC_multiplayer && (type == CAMPAIGN_TYPE_SINGLE) ) {
Campaign_names[Num_campaigns] = vm_strdup(name.c_str());
if (MC_desc)
Campaign_descs[Num_campaigns] = desc;
Num_campaigns++;
// Note that we're not freeing desc here because the pointer is getting copied to Campaign_descs which is freed later.
return 1;
}
}
if (desc != NULL)
vm_free(desc);
return 0;
}
/**
* Builds up the list of campaigns that the user might be able to pick from.
* It uses the multiplayer flag to tell if we should display a list of single or multiplayer campaigns.
* This routine sets the Num_campaigns and Campaign_names global variables
*/
void mission_campaign_build_list(bool desc, bool sort, bool multiplayer)
{
char wild_card[10];
int i, j, incr = 0;
char *t = NULL;
int rc = 0;
if (Campaign_names_inited)
return;
MC_desc = desc;
MC_multiplayer = multiplayer;
memset(wild_card, 0, sizeof(wild_card));
strcpy_s(wild_card, NOX("*"));
strcat_s(wild_card, FS_CAMPAIGN_FILE_EXT);
// if we have already been loaded then free everything and reload
if (Num_campaigns != 0)
mission_campaign_free_list();
// set filter for cf_get_file_list() if there isn't one set already (the simroom has a special one)
if (Get_file_list_filter == NULL)
Get_file_list_filter = mission_campaign_maybe_add;
// now get the list of all mission names
// NOTE: we don't do sorting here, but we assume CF_SORT_NAME, and do it manually below
rc = cf_get_file_list(MAX_CAMPAIGNS, Campaign_file_names, CF_TYPE_MISSIONS, wild_card, CF_SORT_NONE);
Assert( rc == Num_campaigns );
// now sort everything, if we are supposed to
if (sort) {
incr = Num_campaigns / 2;
while (incr > 0) {
for (i = incr; i < Num_campaigns; i++) {
j = i - incr;
while (j >= 0) {
char *name1 = Campaign_names[j];
char *name2 = Campaign_names[j + incr];
// if we hit this then a coder probably did something dumb (like not needing to sort)
if ( (name1 == NULL) || (name2 == NULL) ) {
Int3();
break;
}
if ( !strnicmp(name1, "the ", 4) )
name1 += 4;
if ( !strnicmp(name2, "the ", 4) )
name2 += 4;
if (stricmp(name1, name2) > 0) {
// first, do filenames
t = Campaign_file_names[j];
Campaign_file_names[j] = Campaign_file_names[j + incr];
Campaign_file_names[j + incr] = t;
// next, actual names
t = Campaign_names[j];
Campaign_names[j] = Campaign_names[j + incr];
Campaign_names[j + incr] = t;
// finally, do descriptions
if (desc) {
t = Campaign_descs[j];
Campaign_descs[j] = Campaign_descs[j + incr];
Campaign_descs[j + incr] = t;
}
j -= incr;
} else {
break;
}
}
}
incr /= 2;
}
}
// Done!
Campaign_names_inited = 1;
}
/**
* Gets optional ship/weapon information
*/
void mission_campaign_get_sw_info()
{
int i, count, ship_list[MAX_SHIP_CLASSES], weapon_list[MAX_WEAPON_TYPES];
if (optional_string("+Starting Ships:")) {
count = sz2i(stuff_int_list(ship_list, MAX_SHIP_CLASSES, ParseLookupType::SHIP_INFO_TYPE));
// now set the array elements stating which ships we are allowed
for (i = 0; i < count; i++) {
if (Ship_info[ship_list[i]].flags[Ship::Info_Flags::Player_ship])
Campaign.ships_allowed[ship_list[i]] = 1;
}
}
else {
// set allowable ships to the SIF_PLAYER_SHIPs
for (auto it = Ship_info.cbegin(); it != Ship_info.cend(); ++it) {
if (it->flags[Ship::Info_Flags::Player_ship])
Campaign.ships_allowed[std::distance(Ship_info.cbegin(), it)] = 1;
}
}
if (optional_string("+Starting Weapons:")) {
count = sz2i(stuff_int_list(weapon_list, MAX_WEAPON_TYPES, ParseLookupType::WEAPON_POOL_TYPE));
// now set the array elements stating which ships we are allowed
for (i = 0; i < count; i++) {
if (Weapon_info[weapon_list[i]].wi_flags[Weapon::Info_Flags::Player_allowed])
Campaign.weapons_allowed[weapon_list[i]] = 1;
}
}
else {
// set allowable weapons to the player-allowed ones
for (auto it = Weapon_info.cbegin(); it != Weapon_info.cend(); ++it) {
if (it->wi_flags[Weapon::Info_Flags::Player_allowed])
Campaign.weapons_allowed[std::distance(Weapon_info.cbegin(), it)] = 1;
}
}
}
/**
* Starts a new campaign. It reads in the mission information in the campaign file
* It also sets up all variables needed inside of the game to deal with starting mission numbers, etc
*
* Note: Due to difficulties in generalizing this function, parts of it are duplicated throughout
* this file. If you change the format of the campaign file, you should be sure these related
* functions work properly and update them if it breaks them.
*/
int mission_campaign_load(const char* filename, const char* full_path, player* pl, int load_savefile)
{
int i;
char name[NAME_LENGTH], type[NAME_LENGTH], temp[NAME_LENGTH];
if (campaign_is_ignored(filename)) {
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_IGNORED;
return CAMPAIGN_ERROR_IGNORED;
}
filename = cf_add_ext(filename, FS_CAMPAIGN_FILE_EXT);
if ( pl == NULL )
pl = Player;
if ( !Fred_running && load_savefile && (pl == NULL) ) {
Int3();
load_savefile = 0;
}
// be sure to remove all old malloced strings of Mission_names
// we must also free any goal stuff that was from a previous campaign
// this also frees sexpressions so the next call to init_sexp will be able to reclaim
// nodes previously used by another campaign.
mission_campaign_clear();
// read the mission file and get the list of mission filenames
try
{
strcpy_s( Campaign.filename, filename );
// only initialize the sexpression stuff when Fred isn't running. It'll screw things up major
// if it does
if ( !Fred_running ){
init_sexp(); // must initialize the sexpression stuff
}
read_file_text( full_path ? full_path : filename );
reset_parse();
// copy filename to campaign structure minus the extension
auto len = strlen(filename) - 4;
Assert(len + 1 < CF_MAX_PATHNAME_LENGTH);
strncpy(Campaign.filename, filename, len);
Campaign.filename[len] = '\0';
required_string("$Name:");
stuff_string( name, F_NAME, NAME_LENGTH );
//Store campaign name in the global struct
strcpy_s( Campaign.name, name );
required_string( "$Type:" );
stuff_string( type, F_NAME, NAME_LENGTH );
for (i = 0; i < MAX_CAMPAIGN_TYPES; i++ ) {
if ( !stricmp(type, campaign_types[i]) ) {
Campaign.type = i;
break;
}
}
if ( i == MAX_CAMPAIGN_TYPES )
Error(LOCATION, "Unknown campaign type %s!", type);
if (optional_string("+Description:"))
stuff_string(Campaign.description, F_MULTITEXT);
// if the type is multiplayer -- get the number of players
if ( Campaign.type != CAMPAIGN_TYPE_SINGLE) {
required_string("+Num players:");
stuff_int( &(Campaign.num_players) );
}
// parse flags - Goober5000
if (optional_string("$Flags:"))
{
stuff_int( &(Campaign.flags) );
}
if (optional_string("$begin_custom_data_map")) {
parse_string_map(Campaign.custom_data, "$end_custom_data_map", "+Val:");
}
// parse the optional ship/weapon information
mission_campaign_get_sw_info();
// parse the mission file and actually read in the mission stuff
while ( required_string_either("#End", "$Mission:") ) {
cmission *cm;
required_string("$Mission:");
stuff_string(name, F_NAME, NAME_LENGTH);
cm = &Campaign.missions[Campaign.num_missions];
cm->name = vm_strdup(name);
cm->notes = NULL;
cm->briefing_cutscene[0] = 0;
if ( optional_string("+Briefing Cutscene:") )
stuff_string( cm->briefing_cutscene, F_NAME, NAME_LENGTH );
cm->flags = 0;
if (optional_string("+Flags:"))
stuff_int(&cm->flags);
cm->main_hall = "0";
// deal with previous campaign versions
if (cm->flags & CMISSION_FLAG_BASTION) {
cm->main_hall = "1";
}
// clear any other flag bits to prevent bogus values causing surprises
cm->flags &= CMISSION_EXTERNAL_FLAG_MASK;
// Goober5000 - new main hall stuff!
// Updated by CommanderDJ
if (optional_string("+Main Hall:")) {
stuff_string(temp, F_RAW, 32);
cm->main_hall = temp;
}
// Goober5000 - substitute main hall (like substitute music)
cm->substitute_main_hall = "";
if (optional_string("+Substitute Main Hall:")) {
stuff_string(temp, F_RAW, 32);
cm->substitute_main_hall = temp;
// if we're running FRED, keep the halls separate (so we can save the campaign file),
// but if we're running FS, replace the main hall with the substitute right now
if (!Fred_running) {
// see if this main hall exists
main_hall_defines* mhd = main_hall_get_pointer(temp);
if (mhd != nullptr) {
cm->main_hall = temp;
} else {
mprintf(("Substitute main hall '%s' not found\n", temp));
}
}
}
// Goober5000 - new debriefing persona stuff!
cm->debrief_persona_index = 0;
if (optional_string("+Debriefing Persona Index:"))
stuff_ubyte(&cm->debrief_persona_index);
cm->formula = -1;
if ( optional_string("+Formula:") ) {
cm->formula = get_sexp_main();
if ( !Fred_running ) {
Assert ( cm->formula != -1 );
sexp_mark_persistent( cm->formula );
} else {
if ( cm->formula == -1 ){
Campaign_load_failure = CAMPAIGN_ERROR_SEXP_EXHAUSTED;
return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
}
}
}
// Do mission branching stuff
if ( optional_string("+Mission Loop:") ) {
cm->flags |= CMISSION_FLAG_HAS_LOOP;
} else if ( optional_string("+Mission Fork:") ) {
cm->flags |= CMISSION_FLAG_HAS_FORK;
}
cm->mission_branch_desc = NULL;
if ( optional_string("+Mission Loop Text:") || optional_string("+Mission Fork Text:") ) {
cm->mission_branch_desc = stuff_and_malloc_string(F_MULTITEXT, NULL);
}
cm->mission_branch_brief_anim = NULL;
if ( optional_string("+Mission Loop Brief Anim:") || optional_string("+Mission Fork Brief Anim:") ) {
ignore_white_space(); // it might be on the next line
cm->mission_branch_brief_anim = stuff_and_malloc_string(F_FILESPEC, nullptr);
(void)optional_string("$end_multi_text"); // consume the unneeded ending token
}
cm->mission_branch_brief_sound = NULL;
if ( optional_string("+Mission Loop Brief Sound:") || optional_string("+Mission Fork Brief Sound:") ) {
ignore_white_space(); // it might be on the next line
cm->mission_branch_brief_sound = stuff_and_malloc_string(F_FILESPEC, nullptr);
(void)optional_string("$end_multi_text"); // consume the unneeded ending token
}
cm->mission_loop_formula = -1;
if ( optional_string("+Formula:") ) {
cm->mission_loop_formula = get_sexp_main();
if ( !Fred_running ) {
Assert ( cm->mission_loop_formula != -1 );
sexp_mark_persistent( cm->mission_loop_formula );
} else {
if ( cm->mission_loop_formula == -1 ){
Campaign_load_failure = CAMPAIGN_ERROR_SEXP_EXHAUSTED;
return CAMPAIGN_ERROR_SEXP_EXHAUSTED;
}
}
}
cm->level = 0;
if (optional_string("+Level:")) {
stuff_int( &cm->level );
if ( cm->level == 0 ) // check if the top (root) of the whole tree
Campaign.next_mission = Campaign.num_missions;
} else
Campaign.realign_required = 1;
cm->pos = 0;
if (optional_string("+Position:"))
stuff_int( &cm->pos );
else
Campaign.realign_required = 1;
cm->goals.clear();
cm->events.clear();
cm->variables.clear();
cm->flags |= CMISSION_FLAG_FRED_LOAD_PENDING;
Campaign.num_missions++;
}
if ((Game_mode & GM_MULTIPLAYER) && Campaign.num_missions > UINT8_MAX)
throw parse::ParseException("Number of campaign missions is too high and breaks multi!");
}
catch (const parse::FileOpenException& foe)
{
mprintf(("Error opening '%s'\r\nError message = %s.\r\n", filename, foe.what()));
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_MISSING;
return CAMPAIGN_ERROR_MISSING;
}
catch (const parse::ParseException& pe)
{
mprintf(("Error parsing '%s'\r\nError message = %s.\r\n", filename, pe.what()));
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_file_missing = true;
Campaign_load_failure = CAMPAIGN_ERROR_CORRUPT;
return CAMPAIGN_ERROR_CORRUPT;
}
// set up the other variables for the campaign stuff. After initializing, we must try and load
// the campaign save file for this player. Since all campaign loads go through this routine, I
// think this place should be the only necessary place to load the campaign save stuff. The campaign
// save file will get written when a mission has ended by player choice.
// loading the campaign will get us to the current and next mission that the player must fly
// plus load all of the old goals that future missions might rely on.
if (!Fred_running && load_savefile && (Campaign.type == CAMPAIGN_TYPE_SINGLE)) {
// savefile can fail to load for numerous otherwise non-fatal reasons
// if it doesn't load in that case then it will be (re)created at save
if ( !Pilot.load_savefile(pl, Campaign.filename) ) {
// but if the data is invalid for the savefile then it is fatal
if ( Pilot.is_invalid() ) {
Campaign.filename[0] = 0;
Campaign.num_missions = 0;
Campaign_load_failure = CAMPAIGN_ERROR_SAVEFILE;
return CAMPAIGN_ERROR_SAVEFILE;
}
// start with fresh new campaign data
else {
Pilot.clear_savefile(false); // don't reset ships and weapons because they are currently the campaign's starting ones
Campaign.next_mission = 0;
Pilot.save_savefile();
}
}
}
// all is good here, move along
Campaign_file_missing = false;
return 0;
}
/*
* initialise Player_loadout with default values
*/
void player_loadout_init()
{
int i = 0, j = 0;
memset(Player_loadout.filename, 0, sizeof(Player_loadout.filename));
memset(Player_loadout.last_modified, 0, sizeof(Player_loadout.last_modified));
for ( i = 0; i < MAX_SHIP_CLASSES; i++ ) {
Player_loadout.ship_pool[i] = 0;
}
for ( i = 0; i < MAX_WEAPON_TYPES; i++ ) {
Player_loadout.weapon_pool[i] = 0;
}
for ( i = 0; i < MAX_WSS_SLOTS; i++ ) {
Player_loadout.unit_data[i].ship_class = -1;
for ( j = 0; j < MAX_SHIP_WEAPONS; j++ ) {
Player_loadout.unit_data[i].wep[j] = 0;
Player_loadout.unit_data[i].wep_count[j] = 0;
}
}
}
/**
* Initializes some variables then loads the default FreeSpace single player campaign.
*/
void mission_campaign_init()
{
mission_campaign_clear();
Campaign_file_missing = false;
player_loadout_init();
}
// the below two functions is internal to this module. It is here so that I can group the save/load
// functions together.
//
/**
* Deletes any save file in the players directory for the given
* campaign filename
*/
void mission_campaign_savefile_delete(const char* cfilename)
{
char filename[_MAX_FNAME];
if ( Player->flags & PLAYER_FLAGS_IS_MULTI ) {
return; // no such thing as a multiplayer campaign savefile
}
auto base = util::get_file_part(cfilename);
// do a sanity check, but don't arbitrarily drop any extension in case the filename contains a period
Assertion(!stristr(base, FS_CAMPAIGN_FILE_EXT), "The campaign should not have an extension at this point!");
// only support the new filename here - taylor
sprintf_safe( filename, NOX("%s.%s.csg"), Player->callsign, base );
cf_delete(filename, CF_TYPE_PLAYERS, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
}
/**
* Deletes all the save files for this particular pilot.
* Just call cfile function which will delete multiple files
*
* @param pilot_name Name of pilot
*/
void mission_campaign_delete_all_savefiles(char *pilot_name)
{
int dir_type, num_files, i;
char file_spec[MAX_FILENAME_LEN + 2];
char filename[1024];
int(*filter_save)(const char *filename);
SCP_vector<SCP_string> names;
auto ext = NOX(".csg");
dir_type = CF_TYPE_PLAYERS;
sprintf(file_spec, NOX("%s.*%s"), pilot_name, ext);
// HACK HACK HACK HACK!!!! cf_get_file_list is not reentrant. Pretty dumb because it should
// be. I have to save any file filters
filter_save = Get_file_list_filter;
Get_file_list_filter = NULL;
num_files = cf_get_file_list(names, dir_type, file_spec, CF_SORT_NONE, nullptr,
CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
Get_file_list_filter = filter_save;
for (i = 0; i < num_files; i++) {
strcpy_s(filename, names[i].c_str());
strcat_s(filename, ext);
cf_delete(filename, dir_type, CF_LOCATION_ROOT_USER | CF_LOCATION_ROOT_GAME | CF_LOCATION_TYPE_ROOT);
}
}
/**
* Sets up the internal veriables of the campaign structure so the player can play the next mission.
* If there are no more missions available in the campaign, this function returns -1, else 0 if the mission was
* set successfully
*/
int mission_campaign_next_mission()
{
if ((Campaign.next_mission == -1) || (Campaign.name[0] == '\0')) // will be set to -1 when there is no next mission
return -1;
if (Campaign.num_missions < 1)
return -2;
Campaign.current_mission = Campaign.next_mission;
strcpy_s(Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name);
// check for end of loop.
if (Campaign.current_mission == Campaign.loop_reentry) {
Campaign.loop_enabled = 0;
}
// reset the number of persistent ships and weapons for the next campaign mission
Num_granted_ships = 0;
Num_granted_weapons = 0;
return 0;
}
/**
* Called to go to the previous mission in the campaign. Used only for Red Alert missions
*/
int mission_campaign_previous_mission()
{
if (!(Game_mode & GM_CAMPAIGN_MODE))
return 0;
if (Campaign.prev_mission == -1)
return 0;
Campaign.current_mission = Campaign.prev_mission;
Campaign.prev_mission = -1;
Campaign.next_mission = Campaign.current_mission;
Campaign.num_missions_completed--;
Campaign.missions[Campaign.next_mission].completed = 0;
// copy backed up variables over
for (auto& ra_variable : Campaign.red_alert_variables) {
Campaign.persistent_variables.push_back(ra_variable);
}
// copy backed up containers over
for (const auto& ra_container : Campaign.red_alert_containers) {
Campaign.persistent_containers.emplace_back(ra_container);
}
Pilot.save_savefile();
// reset the player stats to be the stats from this level
Player->stats.assign( Campaign.missions[Campaign.current_mission].stats );
strcpy_s( Game_current_mission_filename, Campaign.missions[Campaign.current_mission].name );
Num_granted_ships = 0;
Num_granted_weapons = 0;
return 1;
}
/**
* Evaluate next campaign mission - set as Campaign.next_mission. Also set Campaign.loop_mission
*/
void mission_campaign_eval_next_mission()
{
Campaign.next_mission = -1;
int cur = Campaign.current_mission;
// evaluate next mission (straight path)
if (Campaign.missions[cur].formula != -1) {
flush_sexp_tree(Campaign.missions[cur].formula); // force formula to be re-evaluated
eval_sexp(Campaign.missions[cur].formula); // this should reset Campaign.next_mission to proper value
}
// evaluate mission loop mission (if any) so it can be used if chosen
if ( Campaign.missions[cur].flags & CMISSION_FLAG_HAS_LOOP ) {
int saved_next_mission = Campaign.next_mission;
// Set temporarily to -1 so we know if loop formula fails to assign
Campaign.next_mission = -1;
if (Campaign.missions[cur].mission_loop_formula != -1) {
flush_sexp_tree(Campaign.missions[cur].mission_loop_formula); // force formula to be re-evaluated
eval_sexp(Campaign.missions[cur].mission_loop_formula); // this should set Campaign.next_mission to the loop mission
}
Campaign.loop_mission = Campaign.next_mission;
Campaign.next_mission = saved_next_mission;
// If the loop mission and the next mission are the same, then don't do the loop. This could be the case if the campaign
// only allows us to proceed to the loop mission if certain conditions are met.
if (Campaign.loop_mission == Campaign.next_mission) {
Campaign.loop_mission = -1;
}
}
if (Campaign.next_mission == -1) {
nprintf(("allender", "No next mission to proceed to.\n"));
} else {
nprintf(("allender", "Next mission is number %d [%s]\n", Campaign.next_mission, Campaign.missions[Campaign.next_mission].name));
}
}
/**
* Store mission's goals and events in Campaign struct
*/
void mission_campaign_store_goals_and_events()
{
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.current_mission < 0))
return;
int cur = Campaign.current_mission;
auto mission_obj = &Campaign.missions[cur];
// first we must save the status of the current missions goals in the campaign mission structure.
// After that, we can determine which mission is tagged as the next mission. Finally, we
// can save the campaign save file
// we might have goal and event status if the player replayed a mission
mission_obj->goals.clear();
// copy the needed info from the Mission_goal struct to our internal structure
for (const auto& goal: Mission_goals) {
mission_obj->goals.emplace_back();
auto& stored_goal = mission_obj->goals.back();
if (goal.name.empty())
sprintf(stored_goal.name, NOX("Goal #" SIZE_T_ARG), &goal - &Mission_goals[0] + 1);
else
strncpy_s(stored_goal.name, goal.name.c_str(), NAME_LENGTH - 1);
Assert ( goal.satisfied != GOAL_INCOMPLETE ); // should be true or false at this point!!!
stored_goal.status = (char)goal.satisfied;
}
// do the same thing for events as we did for goals
// we might have goal and event status if the player replayed a mission
mission_obj->events.clear();
// copy the needed info from the Mission_goal struct to our internal structure
for (const auto& event: Mission_events) {
mission_obj->events.emplace_back();
auto& stored_event = mission_obj->events.back();
if (event.name.empty()) {
sprintf(stored_event.name, NOX("Event #" SIZE_T_ARG), &event - &Mission_events[0] + 1);
nprintf(("Warning", "Mission event in mission %s must have a +Name field! using %s for campaign save file\n", mission_obj->name, stored_event.name));
} else
strncpy_s(stored_event.name, event.name.c_str(), NAME_LENGTH - 1);
// Old method:
// getting status for the events is a little different. If the formula value for the event entry
// is -1, then we know the value of the result field will never change. If the formula is
// not -1 (i.e. still being evaluated at mission end time), we will write "incomplete" for the
// event evaluation
// New method: check a flag. Also, even with the old method, events are always
// forced satisfied or failed at the end of a mission
if (event.flags & MEF_EVENT_IS_DONE) {
if ( event.result )
stored_event.status = static_cast<int>(EventStatus::SATISFIED);
else
stored_event.status = static_cast<int>(EventStatus::FAILED);
} else
UNREACHABLE("Mission event formula should be marked MEF_EVENT_IS_DONE at end-of-mission");
}
}
void mission_campaign_store_variables(int persistence_type, bool store_red_alert)
{
if (!(Game_mode & GM_CAMPAIGN_MODE) || (Campaign.current_mission < 0))
return;