-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathcsg.cpp
More file actions
2054 lines (1607 loc) · 55 KB
/
csg.cpp
File metadata and controls
2054 lines (1607 loc) · 55 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
#include "controlconfig/controlsconfig.h"
#include "controlconfig/presets.h"
#include "cutscene/cutscenes.h"
#include "freespace.h"
#include "gamesnd/eventmusic.h"
#include "hud/hudconfig.h"
#include "io/joy.h"
#include "io/mouse.h"
#include "menuui/techmenu.h"
#include "mission/missioncampaign.h"
#include "mission/missionload.h"
#include "missionui/missionscreencommon.h"
#include "missionui/missionshipchoice.h"
#include "options/OptionsManager.h"
#include "parse/sexp_container.h"
#include "pilotfile/pilotfile.h"
#include "pilotfile/plr_hudprefs.h"
#include "playerman/player.h"
#include "ship/ship.h"
#include "sound/audiostr.h"
#include "stats/medals.h"
#include "utils/string_utils.h"
#include "weapon/weapon.h"
#define REDALERT_INTERNAL
#include "missionui/redalert.h"
#include <iostream>
#include <sstream>
#include <limits>
// this is kinda tricky
enum class TechroomState : ubyte
{
DEFAULT = 0,
ADDED = 1,
REMOVED = 2
};
void pilotfile::csg_read_flags()
{
// tips?
p->tips = (int)cfread_ubyte(cfp);
// avoid having to read everything to get the rank
if (csg_ver >= 5) {
p->stats.rank = cfread_int(cfp);
}
}
void pilotfile::csg_write_flags()
{
startSection(Section::Flags);
// tips
cfwrite_ubyte((ubyte)p->tips, cfp);
// avoid having to read everything to get the rank
cfwrite_int(p->stats.rank, cfp);
endSection();
}
void pilotfile::csg_read_info()
{
char t_string[NAME_LENGTH+1] = { '\0' };
index_list_t ilist;
int idx, list_size = 0;
ubyte allowed = 0;
if ( !m_have_flags ) {
throw "Info before Flags!";
}
//
// NOTE: lists may contain missing/invalid entries for current data
// this is not necessarily fatal
//
// ship list (NOTE: may contain more than MAX_SHIP_CLASSES)
list_size = cfread_int(cfp);
for (idx = 0; idx < list_size; idx++) {
cfread_string_len(t_string, NAME_LENGTH, cfp);
ilist.name = t_string;
ilist.index = ship_info_lookup(t_string);
ship_list.push_back(ilist);
}
// weapon list (NOTE: may contain more than MAX_WEAPON_TYPES)
list_size = cfread_int(cfp);
for (idx = 0; idx < list_size; idx++) {
cfread_string_len(t_string, NAME_LENGTH, cfp);
ilist.name = t_string;
ilist.index = weapon_info_lookup(t_string);
weapon_list.push_back(ilist);
}
// intel list (NOTE: may contain more than MAX_INTEL_ENTRIES)
list_size = cfread_int(cfp);
for (idx = 0; idx < list_size; idx++) {
cfread_string_len(t_string, NAME_LENGTH, cfp);
ilist.name = t_string;
ilist.index = intel_info_lookup(t_string);
intel_list.push_back(ilist);
}
// medals list (NOTE: may contain more than Num_medals)
list_size = cfread_int(cfp);
for (idx = 0; idx < list_size; idx++) {
cfread_string_len(t_string, NAME_LENGTH, cfp);
ilist.name = t_string;
ilist.index = medals_info_lookup(t_string);
medals_list.push_back(ilist);
}
// last ship flown (index into ship_list)
idx = cfread_int(cfp);
// check the idx is within bounds
Assertion ((idx < (int)ship_list.size()), "Campaign file contains an incorrect value for the last flown ship class. No data in ship_list for ship number %d.", idx);
if (idx >= (int)ship_list.size())
idx = -1;
else if (idx != -1)
p->last_ship_flown_si_index = ship_list[idx].index;
else
p->last_ship_flown_si_index = -1;
// progression state
Campaign.prev_mission = cfread_int(cfp);
Campaign.next_mission = cfread_int(cfp);
// check that the next mission won't be greater than the total number of missions
// though ensure we only flag if campaign exists and has been loaded
if (Campaign.num_missions > 0 && Campaign.next_mission >= Campaign.num_missions) {
Campaign.next_mission = 0; // Prevent trying to load from invalid mission data downstream
m_data_invalid = true; // Causes a warning popup to be displayed
}
// loop state
Campaign.loop_reentry = cfread_int(cfp);
Campaign.loop_enabled = cfread_int(cfp);
// missions completed
Campaign.num_missions_completed = cfread_int(cfp);
// allowed ships
list_size = (int)ship_list.size();
for (idx = 0; idx < list_size; idx++) {
allowed = cfread_ubyte(cfp);
if (allowed) {
if (ship_list[idx].index >= 0) {
Campaign.ships_allowed[ship_list[idx].index] = 1;
} else {
mprintf(("Found invalid ship \"%s\" in campaign save file. Skipping...\n", ship_list[idx].name.c_str()));
}
}
}
// allowed weapons
list_size = (int)weapon_list.size();
for (idx = 0; idx < list_size; idx++) {
allowed = cfread_ubyte(cfp);
if (allowed) {
if (weapon_list[idx].index >= 0) {
Campaign.weapons_allowed[weapon_list[idx].index] = 1;
} else {
mprintf(("Found invalid weapon \"%s\" in campaign save file. Skipping...\n",
weapon_list[idx].name.c_str()));
}
}
}
if (csg_ver >= 2) {
// single/campaign squad name & image
cfread_string_len(p->s_squad_name, NAME_LENGTH, cfp);
cfread_string_len(p->s_squad_filename, MAX_FILENAME_LEN, cfp);
}
// if anything we need/use was missing then it should be considered fatal
if (m_data_invalid) {
throw "Invalid data for CSG!";
}
}
void pilotfile::csg_write_info()
{
int idx;
startSection(Section::Info);
// ship list
cfwrite_int(ship_info_size(), cfp);
for (auto &si : Ship_info) {
cfwrite_string_len(si.name, cfp);
}
// weapon list
cfwrite_int(weapon_info_size(), cfp);
for (auto &wi : Weapon_info) {
cfwrite_string_len(wi.name, cfp);
}
// intel list
cfwrite_int(intel_info_size(), cfp);
for (auto &ii : Intel_info) {
cfwrite_string_len(ii.name, cfp);
}
// medals list
cfwrite_int((int)Medals.size(), cfp);
for (idx = 0; idx < (int)Medals.size(); idx++) {
cfwrite_string_len(Medals[idx].name, cfp);
}
// last ship flown
cfwrite_int(p->last_ship_flown_si_index, cfp);
// progression state
cfwrite_int(Campaign.prev_mission, cfp);
cfwrite_int(Campaign.next_mission, cfp);
// loop state
cfwrite_int(Campaign.loop_reentry, cfp);
cfwrite_int(Campaign.loop_enabled, cfp);
// missions completed
cfwrite_int(Campaign.num_missions_completed, cfp);
// allowed ships
for (idx = 0; idx < ship_info_size(); idx++) {
cfwrite_ubyte(Campaign.ships_allowed[idx], cfp);
}
// allowed weapons
for (idx = 0; idx < weapon_info_size(); idx++) {
cfwrite_ubyte(Campaign.weapons_allowed[idx], cfp);
}
// single/campaign squad name & image
cfwrite_string_len(p->s_squad_name, cfp);
cfwrite_string_len(p->s_squad_filename, cfp);
endSection();
}
void pilotfile::csg_read_missions()
{
int i, j, idx, list_size;
cmission *missionp;
if ( !m_have_info ) {
throw "Missions before Info!";
}
for (i = 0; i < Campaign.num_missions_completed; i++) {
idx = cfread_int(cfp);
missionp = &Campaign.missions[idx];
missionp->completed = 1;
// flags
missionp->flags = cfread_int(cfp);
// goals
missionp->goals.clear();
int num_goals = cfread_int(cfp);
for (j = 0; j < num_goals; j++) {
missionp->goals.emplace_back();
auto& stored_goal = missionp->goals.back();
cfread_string_len(stored_goal.name, NAME_LENGTH, cfp);
stored_goal.status = cfread_char(cfp);
}
// events
missionp->events.clear();
int num_events = cfread_int(cfp);
for (j = 0; j < num_events; j++) {
missionp->events.emplace_back();
auto& stored_event = missionp->events.back();
cfread_string_len(stored_event.name, NAME_LENGTH, cfp);
stored_event.status = cfread_char(cfp);
}
// variables
missionp->variables.clear();
int num_variables = cfread_int(cfp);
for (j = 0; j < num_variables; j++) {
missionp->variables.emplace_back();
auto& stored_variable = missionp->variables.back();
stored_variable.type = cfread_int(cfp);
cfread_string_len(missionp->variables[j].text, TOKEN_LENGTH, cfp);
cfread_string_len(missionp->variables[j].variable_name, TOKEN_LENGTH, cfp);
}
// scoring stats
missionp->stats.score = cfread_int(cfp);
missionp->stats.rank = cfread_int(cfp);
missionp->stats.assists = cfread_int(cfp);
missionp->stats.kill_count = cfread_int(cfp);
missionp->stats.kill_count_ok = cfread_int(cfp);
missionp->stats.bonehead_kills = cfread_int(cfp);
missionp->stats.p_shots_fired = cfread_uint(cfp);
missionp->stats.p_shots_hit = cfread_uint(cfp);
missionp->stats.p_bonehead_hits = cfread_uint(cfp);
missionp->stats.s_shots_fired = cfread_uint(cfp);
missionp->stats.s_shots_hit = cfread_uint(cfp);
missionp->stats.s_bonehead_hits = cfread_uint(cfp);
// ship kills (scoring)
list_size = (int)ship_list.size();
for (j = 0; j < list_size; j++) {
idx = cfread_int(cfp);
if (ship_list[j].index >= 0) {
missionp->stats.kills[ship_list[j].index] = idx;
}
}
// medals (scoring)
list_size = (int)medals_list.size();
for (j = 0; j < list_size; j++) {
idx = cfread_int(cfp);
if (medals_list[j].index >= 0) {
missionp->stats.medal_counts[medals_list[j].index] = idx;
}
}
}
}
void pilotfile::csg_write_missions()
{
int idx, j;
cmission *missionp;
startSection(Section::Missions);
for (idx = 0; idx < MAX_CAMPAIGN_MISSIONS; idx++) {
if (Campaign.missions[idx].completed) {
missionp = &Campaign.missions[idx];
cfwrite_int(idx, cfp);
// flags
cfwrite_int(missionp->flags, cfp);
// goals
cfwrite_int((int)missionp->goals.size(), cfp);
for (j = 0; j < (int)missionp->goals.size(); j++) {
cfwrite_string_len(missionp->goals[j].name, cfp);
cfwrite_char(missionp->goals[j].status, cfp);
}
// events
cfwrite_int((int)missionp->events.size(), cfp);
for (j = 0; j < (int)missionp->events.size(); j++) {
cfwrite_string_len(missionp->events[j].name, cfp);
cfwrite_char(missionp->events[j].status, cfp);
}
// variables
cfwrite_int((int)missionp->variables.size(), cfp);
for (j = 0; j < (int)missionp->variables.size(); j++) {
cfwrite_int(missionp->variables[j].type, cfp);
cfwrite_string_len(missionp->variables[j].text, cfp);
cfwrite_string_len(missionp->variables[j].variable_name, cfp);
}
// scoring stats
cfwrite_int(missionp->stats.score, cfp);
cfwrite_int(missionp->stats.rank, cfp);
cfwrite_int(missionp->stats.assists, cfp);
cfwrite_int(missionp->stats.kill_count, cfp);
cfwrite_int(missionp->stats.kill_count_ok, cfp);
cfwrite_int(missionp->stats.bonehead_kills, cfp);
cfwrite_uint(missionp->stats.p_shots_fired, cfp);
cfwrite_uint(missionp->stats.p_shots_hit, cfp);
cfwrite_uint(missionp->stats.p_bonehead_hits, cfp);
cfwrite_uint(missionp->stats.s_shots_fired, cfp);
cfwrite_uint(missionp->stats.s_shots_hit, cfp);
cfwrite_uint(missionp->stats.s_bonehead_hits, cfp);
// ship kills (scoring)
for (j = 0; j < ship_info_size(); j++) {
cfwrite_int(missionp->stats.kills[j], cfp);
}
// medals earned (scoring)
for (j = 0; j < (int)Medals.size(); j++) {
cfwrite_int(missionp->stats.medal_counts[j], cfp);
}
}
}
endSection();
}
void pilotfile::csg_read_techroom()
{
int idx, list_size = 0;
TechroomState state;
if ( !m_have_info ) {
throw "Techroom before Info!";
}
// visible ships
list_size = (int)ship_list.size();
for (idx = 0; idx < list_size; idx++) {
state = (TechroomState) cfread_ubyte(cfp);
if (state != TechroomState::DEFAULT) {
if (ship_list[idx].index >= 0) {
if (state == TechroomState::ADDED) {
Ship_info[ship_list[idx].index].flags.set(Ship::Info_Flags::In_tech_database);
} else if (state == TechroomState::REMOVED) {
Ship_info[ship_list[idx].index].flags.remove(Ship::Info_Flags::In_tech_database);
} else {
mprintf(("Unrecognized techroom state: %d\n", (int) state));
}
} else {
mprintf(("Found invalid ship \"%s\" in campaign save file. "
"Skipping...\n",
ship_list[idx].name.c_str()));
}
}
}
// visible weapons
list_size = (int)weapon_list.size();
for (idx = 0; idx < list_size; idx++) {
state = (TechroomState) cfread_ubyte(cfp);
if (state != TechroomState::DEFAULT) {
if (weapon_list[idx].index >= 0) {
if (state == TechroomState::ADDED) {
Weapon_info[weapon_list[idx].index].wi_flags.set(Weapon::Info_Flags::In_tech_database);
} else if (state == TechroomState::REMOVED) {
Weapon_info[weapon_list[idx].index].wi_flags.remove(Weapon::Info_Flags::In_tech_database);
} else {
mprintf(("Unrecognized techroom state: %d\n", (int) state));
}
} else {
mprintf(("Found invalid weapon \"%s\" in campaign save file. Skipping...\n",
weapon_list[idx].name.c_str()));
}
}
}
// visible intel entries
list_size = (int)intel_list.size();
for (idx = 0; idx < list_size; idx++) {
state = (TechroomState) cfread_ubyte(cfp);
if (state != TechroomState::DEFAULT) {
if (intel_list[idx].index >= 0) {
if (state == TechroomState::ADDED) {
Intel_info[intel_list[idx].index].flags |= IIF_IN_TECH_DATABASE;
} else if (state == TechroomState::REMOVED) {
Intel_info[intel_list[idx].index].flags &= ~IIF_IN_TECH_DATABASE;
} else {
mprintf(("Unrecognized techroom state: %d\n", (int) state));
}
} else {
mprintf(("Found invalid intel entry \"%s\" in campaign save file. Skipping...\n",
intel_list[idx].name.c_str()));
}
}
}
// if anything we need/use was missing then it should be considered fatal
if (m_data_invalid) {
throw "Invalid data for CSG!";
}
}
void pilotfile::csg_write_techroom()
{
TechroomState state;
startSection(Section::Techroom);
// write whether it differs from the default for ships
for (auto &si : Ship_info) {
if (si.flags[Ship::Info_Flags::In_tech_database] == si.flags[Ship::Info_Flags::Default_in_tech_database]) {
state = TechroomState::DEFAULT;
} else if (si.flags[Ship::Info_Flags::In_tech_database]) {
state = TechroomState::ADDED;
} else {
state = TechroomState::REMOVED;
}
cfwrite_ubyte((ubyte) state, cfp);
}
// and for weapons
for (auto &wi : Weapon_info) {
if (wi.wi_flags[Weapon::Info_Flags::In_tech_database] == wi.wi_flags[Weapon::Info_Flags::Default_in_tech_database]) {
state = TechroomState::DEFAULT;
} else if (wi.wi_flags[Weapon::Info_Flags::In_tech_database]) {
state = TechroomState::ADDED;
} else {
state = TechroomState::REMOVED;
}
cfwrite_ubyte((ubyte) state, cfp);
}
// and for intel entries
for (auto &ii : Intel_info) {
if (((ii.flags & IIF_IN_TECH_DATABASE) != 0) == ((ii.flags & IIF_DEFAULT_IN_TECH_DATABASE) != 0)) {
state = TechroomState::DEFAULT;
} else if ((ii.flags & IIF_IN_TECH_DATABASE) != 0) {
state = TechroomState::ADDED;
} else {
state = TechroomState::REMOVED;
}
cfwrite_ubyte((ubyte) state, cfp);
}
endSection();
}
void pilotfile::csg_read_loadout()
{
int j, count, ship_idx = -1, wep_idx = -1;
size_t idx, list_size = 0;
if ( !m_have_info ) {
throw "Loadout before Info!";
}
// base info
cfread_string_len(Player_loadout.filename, MAX_FILENAME_LEN, cfp);
cfread_string_len(Player_loadout.last_modified, DATE_TIME_LENGTH, cfp);
// ship pool
list_size = ship_list.size();
for (idx = 0; idx < list_size; idx++) {
count = cfread_int(cfp);
if (ship_list[idx].index >= 0) {
Player_loadout.ship_pool[ship_list[idx].index] = count;
}
}
// weapon pool
list_size = weapon_list.size();
for (idx = 0; idx < list_size; idx++) {
count = cfread_int(cfp);
if (weapon_list[idx].index >= 0) {
Player_loadout.weapon_pool[weapon_list[idx].index] = count;
}
}
// player ship loadout
list_size = (uint)cfread_ushort(cfp);
for (uint i = 0; i < list_size; i++) {
wss_unit *slot = NULL;
if (i < MAX_WSS_SLOTS) {
slot = &Player_loadout.unit_data[i];
}
// ship
ship_idx = cfread_int(cfp);
if ( (ship_idx >= (int)ship_list.size()) || (ship_idx < -1) ) { // on the casts, assume that ship & weapon lists will never exceed ~2 billion
mprintf(("CSG => Parse Warning: Invalid value for ship index (%d), emptying slot.\n", ship_idx));
ship_idx = -1;
}
if (slot) {
if (ship_idx == -1) { // -1 means no ship in this slot
slot->ship_class = -1;
} else {
slot->ship_class = ship_list[ship_idx].index;
}
}
// primary weapons
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
wep_idx = cfread_int(cfp);
if ( (wep_idx >= (int)weapon_list.size()) || (wep_idx < -1) ) {
mprintf(("CSG => Parse Warning: Invalid value for primary weapon index (%d), emptying slot.\n", wep_idx));
wep_idx = -1;
}
if ( slot && (j < MAX_SHIP_PRIMARY_BANKS) ) {
if (wep_idx == -1) { // -1 means no weapon in this slot
slot->wep[j] = -1;
} else {
slot->wep[j] = weapon_list[wep_idx].index;
}
}
int read_idx = cfread_int(cfp);
if ( slot && (j < MAX_SHIP_PRIMARY_BANKS) ) {
slot->wep_count[j] = read_idx;
}
}
// secondary weapons
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
wep_idx = cfread_int(cfp);
if ( (wep_idx >= (int)weapon_list.size()) || (wep_idx < -1) ) {
mprintf(("CSG => Parse Warning: Invalid value for secondary weapon index (%d), emptying slot.\n", wep_idx));
wep_idx = -1;
}
if ( slot && (j < MAX_SHIP_SECONDARY_BANKS) ) {
if (wep_idx == -1) { // -1 means no weapon in this slot
slot->wep[j+MAX_SHIP_PRIMARY_BANKS] = -1;
} else {
slot->wep[j+MAX_SHIP_PRIMARY_BANKS] = weapon_list[wep_idx].index;
}
}
int read_idx = cfread_int(cfp);
if ( slot && (j < MAX_SHIP_SECONDARY_BANKS) ) {
slot->wep_count[j+MAX_SHIP_PRIMARY_BANKS] = read_idx;
}
}
}
}
void pilotfile::csg_write_loadout()
{
int idx, j;
startSection(Section::Loadout);
// base info
cfwrite_string_len(Player_loadout.filename, cfp);
cfwrite_string_len(Player_loadout.last_modified, cfp);
// ship pool
for (idx = 0; idx < ship_info_size(); idx++) {
cfwrite_int(Player_loadout.ship_pool[idx], cfp);
}
// weapon pool
for (idx = 0; idx < weapon_info_size(); idx++) {
cfwrite_int(Player_loadout.weapon_pool[idx], cfp);
}
// play ship loadout
cfwrite_ushort(MAX_WSS_SLOTS, cfp);
for (idx = 0; idx < MAX_WSS_SLOTS; idx++) {
wss_unit *slot = &Player_loadout.unit_data[idx];
// ship
cfwrite_int(slot->ship_class, cfp);
// primary weapons
cfwrite_int(MAX_SHIP_PRIMARY_BANKS, cfp);
for (j = 0; j < MAX_SHIP_PRIMARY_BANKS; j++) {
cfwrite_int(slot->wep[j], cfp);
cfwrite_int(slot->wep_count[j], cfp);
}
// secondary weapons
cfwrite_int(MAX_SHIP_SECONDARY_BANKS, cfp);
for (j = 0; j < MAX_SHIP_SECONDARY_BANKS; j++) {
cfwrite_int(slot->wep[j+MAX_SHIP_PRIMARY_BANKS], cfp);
cfwrite_int(slot->wep_count[j+MAX_SHIP_PRIMARY_BANKS], cfp);
}
}
endSection();
}
void pilotfile::csg_read_stats()
{
int idx, list_size = 0;
int count;
if ( !m_have_info ) {
throw "Stats before Info!";
}
// scoring stats
p->stats.score = cfread_int(cfp);
p->stats.rank = cfread_int(cfp);
p->stats.assists = cfread_int(cfp);
p->stats.kill_count = cfread_int(cfp);
p->stats.kill_count_ok = cfread_int(cfp);
p->stats.bonehead_kills = cfread_int(cfp);
p->stats.p_shots_fired = cfread_uint(cfp);
p->stats.p_shots_hit = cfread_uint(cfp);
p->stats.p_bonehead_hits = cfread_uint(cfp);
p->stats.s_shots_fired = cfread_uint(cfp);
p->stats.s_shots_hit = cfread_uint(cfp);
p->stats.s_bonehead_hits = cfread_uint(cfp);
p->stats.flight_time = cfread_uint(cfp);
p->stats.missions_flown = cfread_uint(cfp);
p->stats.last_flown = (_fs_time_t)cfread_int(cfp);
p->stats.last_backup = (_fs_time_t)cfread_int(cfp);
// ship kills (scoring)
list_size = (int)ship_list.size();
for (idx = 0; idx < list_size; idx++) {
count = cfread_int(cfp);
if (ship_list[idx].index >= 0) {
p->stats.kills[ship_list[idx].index] = count;
}
}
// medals earned (scoring)
list_size = (int)medals_list.size();
for (idx = 0; idx < list_size; idx++) {
count = cfread_int(cfp);
if (medals_list[idx].index >= 0) {
p->stats.medal_counts[medals_list[idx].index] = count;
}
}
}
void pilotfile::csg_write_stats()
{
int idx;
startSection(Section::Scoring);
// scoring stats
cfwrite_int(p->stats.score, cfp);
cfwrite_int(p->stats.rank, cfp);
cfwrite_int(p->stats.assists, cfp);
cfwrite_int(p->stats.kill_count, cfp);
cfwrite_int(p->stats.kill_count_ok, cfp);
cfwrite_int(p->stats.bonehead_kills, cfp);
cfwrite_uint(p->stats.p_shots_fired, cfp);
cfwrite_uint(p->stats.p_shots_hit, cfp);
cfwrite_uint(p->stats.p_bonehead_hits, cfp);
cfwrite_uint(p->stats.s_shots_fired, cfp);
cfwrite_uint(p->stats.s_shots_hit, cfp);
cfwrite_uint(p->stats.s_bonehead_hits, cfp);
cfwrite_uint(p->stats.flight_time, cfp);
cfwrite_uint(p->stats.missions_flown, cfp);
cfwrite_int((int)p->stats.last_flown, cfp);
cfwrite_int((int)p->stats.last_backup, cfp);
// ship kills (scoring)
for (idx = 0; idx < ship_info_size(); idx++) {
cfwrite_int(p->stats.kills[idx], cfp);
}
// medals earned (scoring)
for (idx = 0; idx < (int)Medals.size(); idx++) {
cfwrite_int(p->stats.medal_counts[idx], cfp);
}
endSection();
}
void pilotfile::csg_read_redalert()
{
int idx, i, j, ship_list_size = 0, wing_list_size = 0;
int count;
char t_string[MAX_FILENAME_LEN+NAME_LENGTH+1] = { '\0' };
float hit;
wep_t weapons;
if ( !m_have_info ) {
throw "RedAlert before Info!";
}
ship_list_size = cfread_int(cfp);
if (ship_list_size > 0) {
cfread_string_len(t_string, MAX_FILENAME_LEN, cfp);
Red_alert_precursor_mission = t_string;
for (idx = 0; idx < ship_list_size; idx++) {
red_alert_ship_status ras;
cfread_string_len(t_string, NAME_LENGTH, cfp);
ras.name = t_string;
ras.hull = cfread_float(cfp);
// ship class, index into ship_list[]
i = cfread_int(cfp);
if ( (i >= (int)ship_list.size()) || (i < RED_ALERT_LOWEST_VALID_SHIP_CLASS) ) {
mprintf(("CSG => Parse Warning: Invalid value for red alert ship index (%d), emptying slot.\n", i));
ras.ship_class = RED_ALERT_DESTROYED_SHIP_CLASS;
} else if ( (i < 0 ) && (i >= RED_ALERT_LOWEST_VALID_SHIP_CLASS) ) { // ship destroyed/exited
ras.ship_class = i;
} else {
ras.ship_class = ship_list[i].index;
}
// subsystem hits
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
hit = cfread_float(cfp);
ras.subsys_current_hits.push_back( hit );
}
// subsystem aggregate hits
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
hit = cfread_float(cfp);
ras.subsys_aggregate_current_hits.push_back( hit );
}
// primary weapon loadout and status
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
i = cfread_int(cfp);
weapons.index = weapon_list[i].index;
weapons.count = cfread_int(cfp);
// triggering this means something is really fubar
if (weapons.index < 0) {
continue;
}
ras.primary_weapons.push_back( weapons );
}
// secondary weapon loadout and status
count = cfread_int(cfp);
for (j = 0; j < count; j++) {
i = cfread_int(cfp);
weapons.index = weapon_list[i].index;
weapons.count = cfread_int(cfp);
// triggering this means something is really fubar
if (weapons.index < 0) {
continue;
}
ras.secondary_weapons.push_back( weapons );
}
// this is quite likely a *bad* thing if it doesn't happen
if (ras.ship_class >= RED_ALERT_LOWEST_VALID_SHIP_CLASS) {
Red_alert_ship_status.push_back( ras );
}
}
}
// old versions of CSG files do not store wing status
if (csg_ver < 8) {
return;
}
wing_list_size = cfread_int(cfp);
if (wing_list_size > 0) {
for (idx = 0; idx < wing_list_size; idx++) {
red_alert_wing_status rws;
cfread_string_len(t_string, NAME_LENGTH, cfp);
rws.name = t_string;
rws.latest_wave = cfread_int(cfp);
rws.wave_count = cfread_int(cfp);
rws.total_arrived_count = cfread_int(cfp);
rws.total_departed = cfread_int(cfp);
rws.total_destroyed = cfread_int(cfp);
rws.total_vanished = cfread_int(cfp);
Red_alert_wing_status.push_back(rws);
}
}
}
void pilotfile::csg_write_redalert()
{
int idx, j, ship_list_size = 0, wing_list_size = 0;
int count;
startSection(Section::RedAlert);
ship_list_size = (int)Red_alert_ship_status.size();
cfwrite_int(ship_list_size, cfp);
if (ship_list_size) {
cfwrite_string_len(Red_alert_precursor_mission.c_str(), cfp);
for (idx = 0; idx < ship_list_size; idx++) {
auto ras = &Red_alert_ship_status[idx];
cfwrite_string_len(ras->name.c_str(), cfp);
cfwrite_float(ras->hull, cfp);
// ship class, should be index into ship_list[] on load
cfwrite_int(ras->ship_class, cfp);
// subsystem hits
count = (int)ras->subsys_current_hits.size();
cfwrite_int(count, cfp);
for (j = 0; j < count; j++) {
cfwrite_float(ras->subsys_current_hits[j], cfp);
}
// subsystem aggregate hits
count = (int)ras->subsys_aggregate_current_hits.size();
cfwrite_int(count, cfp);
for (j = 0; j < count; j++) {
cfwrite_float(ras->subsys_aggregate_current_hits[j], cfp);
}
// primary weapon loadout and status
count = (int)ras->primary_weapons.size();
cfwrite_int(count, cfp);
for (j = 0; j < count; j++) {
cfwrite_int(ras->primary_weapons[j].index, cfp);
cfwrite_int(ras->primary_weapons[j].count, cfp);
}
// secondary weapon loadout and status
count = (int)ras->secondary_weapons.size();
cfwrite_int(count, cfp);
for (j = 0; j < count; j++) {
cfwrite_int(ras->secondary_weapons[j].index, cfp);
cfwrite_int(ras->secondary_weapons[j].count, cfp);
}
}
}
wing_list_size = (int)Red_alert_wing_status.size();
cfwrite_int(wing_list_size, cfp);
if (wing_list_size) {
for (idx = 0; idx < wing_list_size; idx++) {
auto rws = &Red_alert_wing_status[idx];