-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathscoring.cpp
More file actions
1643 lines (1387 loc) · 48.7 KB
/
scoring.cpp
File metadata and controls
1643 lines (1387 loc) · 48.7 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 "ai/ai_profiles.h"
#include "debugconsole/console.h"
#include "freespace.h"
#include "hud/hud.h"
#include "hud/hudmessage.h"
#include "iff_defs/iff_defs.h"
#include "localization/localize.h"
#include "mission/missionparse.h"
#include "network/multi.h"
#include "network/multi_dogfight.h"
#include "network/multi_pmsg.h"
#include "network/multi_team.h"
#include "network/multimsgs.h"
#include "network/multiutil.h"
#include "object/object.h"
#include "parse/parselo.h"
#include "pilotfile/pilotfile.h"
#include "playerman/player.h"
#include "ship/ship.h"
#include "stats/medals.h"
#include "stats/scoring.h"
#include "weapon/weapon.h"
/*
// uncomment to get extra debug messages when a player scores
#define SCORING_DEBUG
*/
// what percent of points of total damage to a ship a player has to have done to get an assist (or a kill) when it is killed
float Kill_percentage = 0.30f;
float Assist_percentage = 0.15f;
// traitor stuff
extern debriefing Traitor_debriefing;
traitor_stuff Traitor;
SCP_vector<traitor_override_t> Traitor_overrides;
// these tables are overwritten with the values from rank.tbl
SCP_vector<rank_stuff> Ranks;
// scoring scale factors by skill level
float Scoring_scale_factors[NUM_SKILL_LEVELS] = {
0.2f, // very easy
0.4f, // easy
0.7f, // medium
1.0f, // hard
1.25f // insane
};
traitor_override_t* get_traitor_override_pointer(const SCP_string& name)
{
for (int i = 0; i < (int)Traitor_overrides.size(); i++) {
if (lcase_equal(name, Traitor_overrides[i].name)) {
return &Traitor_overrides[i];
}
}
return nullptr;
}
static rank_stuff* get_rank_pointer(const char* rank_name)
{
for (int i = 0; i < (int)Ranks.size(); i++) {
if (!stricmp(rank_name, Ranks[i].name)) {
return &Ranks[i];
}
}
// Didn't find anything.
return nullptr;
}
static void rank_stuff_init(rank_stuff* ranki)
{
ranki->name[0] = '\0';
ranki->promotion_text = {};
ranki->points = -1;
ranki->bitmap[0] = '\0';
ranki->promotion_voice_base[0] = '\0';
}
void parse_rank_table(const char* filename)
{
try
{
read_file_text(filename, CF_TYPE_TABLES);
reset_parse();
// parse in all the rank names
//Retail compatibility
if (check_for_string("[RANK NAMES]")) {
skip_to_string("[RANK NAMES]");
}
if (check_for_string("#Ranks")) {
skip_to_string("#Ranks");
}
ignore_white_space();
while (required_string_either("#End", "$Name:"))
{
rank_stuff rank_t;
rank_stuff_init(&rank_t);
rank_stuff* rank_p;
bool create_if_not_found = true;
required_string("$Name:");
stuff_string(rank_t.name, F_NAME, NAME_LENGTH);
if (optional_string("+nocreate")) {
if (!Parsing_modular_table) {
Warning(LOCATION, "+nocreate flag used for rank in non-modular table\n");
}
create_if_not_found = false;
}
// Does this rank exist already?
// If so, load this new info into it
rank_p = get_rank_pointer(rank_t.name);
if (rank_p != nullptr) {
if (!Parsing_modular_table) {
error_display(1,
"Error: Rank %s already exists. All rank names must be unique.",
rank_t.name);
}
} else {
// Don't create rank if it has +nocreate and is in a modular table.
if (!create_if_not_found && Parsing_modular_table) {
if (!skip_to_start_of_string_either("$Name:", "#end")) {
error_display(1, "Missing [#end] or [$Name] after rank %s", rank_t.name);
}
continue;
}
Ranks.push_back(rank_t);
rank_p = &Ranks.back();
}
if (optional_string("$Alt Name:")) {
stuff_string(rank_p->alt_name, F_NAME);
}
if (optional_string("$Title:")) {
stuff_string(rank_p->title, F_NAME);
}
if (optional_string("$Points:")) {
stuff_int(&rank_p->points);
}
// If points are not set then set it to index position + 1
if (rank_p->points == -1) {
rank_p->points = ((int)Ranks.size() + 1);
}
if (optional_string("$Bitmap:")) {
stuff_string(rank_p->bitmap, F_NAME, MAX_FILENAME_LEN);
}
// Check here that the rank has a bitmap. If not, then error out
if (!stricmp(rank_p->bitmap, "")) {
error_display(1, "Missing valid bitmap file for rank %s", rank_p->name);
}
if (optional_string("$Promotion Voice Base:")) {
stuff_string(rank_p->promotion_voice_base, F_NAME, MAX_FILENAME_LEN);
}
// If voice base is not set then set it to the rank name
if (rank_p->promotion_voice_base[0] == '\0') {
strcpy(rank_p->promotion_voice_base, rank_p->name);
}
while (check_for_string("$Promotion Text:"))
{
SCP_string buf;
int persona = -1;
required_string("$Promotion Text:");
stuff_string(buf, F_MULTITEXT);
drop_white_space(buf);
compact_multitext_string(buf);
if (optional_string("+Persona:"))
{
stuff_int(&persona);
if (persona < 0)
{
Warning(LOCATION,
"Debriefing text for %s rank is assigned to an invalid persona: %i (must be 0 or "
"greater).\n",
rank_p->name,
persona);
continue;
}
}
rank_p->promotion_text[persona] = std::move(buf);
}
if (rank_p->promotion_text.find(-1) == rank_p->promotion_text.end())
{
Warning(LOCATION, "%s rank is missing default debriefing text.\n", rank_p->name);
rank_p->promotion_text[-1] = "";
}
}
required_string("#End");
}
catch (const parse::ParseException& e)
{
mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "rank.tbl", e.what()));
return;
}
}
void sort_ranks()
{
bool shouldSort = false;
// be sure no ranks have equal point values
for (int i = 0; i < (int)Ranks.size(); i++) {
for (int j = (i + 1); j < (int)Ranks.size(); j++) {
if (Ranks[i].points == Ranks[j].points) {
Error(LOCATION,
"Rank %s and %s have equal point values! This is not allowed.",
Ranks[i].name,
Ranks[j].name);
}
}
}
// be sure that all rank points are in order
for (int idx = 0; idx < (int)Ranks.size() - 1; idx++) {
if (Ranks[idx].points >= Ranks[idx + 1].points) {
shouldSort = true;
}
}
if (shouldSort) {
Warning(LOCATION,"Ranks were not ordered by points or had equal values adjusted. Sorting the ranks by point values!\n");
sort(Ranks.begin(), Ranks.end(), [](const rank_stuff& lhs, const rank_stuff& rhs) {
return lhs.points < rhs.points;
});
for (int i = 0; i < (int)Ranks.size(); i++) {
mprintf(("Rank %s is now in position %i\n", Ranks[i].name, i));
}
}
}
void rank_init()
{
// first parse the default table
parse_rank_table("rank.tbl");
// parse any modular tables
parse_modular_table("*-rnk.tbm", parse_rank_table);
if ((int)Ranks.size() <= 0) {
error_display(1, "No ranks have been defined in ranks.tbl. Must define at least one rank!");
}
sort_ranks();
}
//Provided as a way to prevent crashes due to ranks differing across mods-Mjn
//If player rank is > max rank, returns max rank index, returns 0 if player rank < 0
//else it returns player rank index
int verify_rank(int ranki)
{
if (ranki > ((int)Ranks.size() - 1)) {
return ((int)Ranks.size() - 1);
} else if (ranki < 0) {
return 0;
}
return ranki;
}
SCP_string get_rank_display_name(rank_stuff* rank)
{
if (!rank->alt_name.empty()) {
return rank->alt_name;
} else {
return rank->name;
}
}
void parse_traitor_tbl(const char* filename)
{
try
{
read_file_text(filename, CF_TYPE_TABLES);
reset_parse();
if (optional_string("#Debriefing_info")) {
// no longer used
if (optional_string("$Num stages:")) {
int junk;
stuff_int(&junk); // consume the data and ignore it
}
// no longer used
if (optional_string("$Formula:")) {
bool junk[1];
stuff_bool_list(junk, 1); // consume the data and ignore it
}
while (check_for_string("$multi text")) {
SCP_string text;
int persona = -1;
required_string("$multi text");
stuff_string(text, F_MULTITEXT);
if (optional_string("+Persona:")) {
stuff_int(&persona);
if (persona < 0) {
Warning(LOCATION,
"Traitor information is assigned to an invalid persona: %i (must be 0 or greater).\n",
persona);
continue;
}
}
Traitor.debriefing_text[persona] = std::move(text);
}
if (optional_string("$Voice:"))
stuff_string(Traitor.traitor_voice_base, F_FILESPEC, MAX_FILENAME_LEN);
if (optional_string("$Recommendation text:"))
stuff_string(Traitor.recommendation_text, F_MULTITEXT);
}
if (optional_string("#Traitor Overrides")) {
while (optional_string("$Name:")) {
SCP_string name;
stuff_string(name, F_NAME);
required_string("$Text:");
SCP_string text;
stuff_string(text, F_MULTITEXT);
required_string("$Voice Filename:");
char file[MAX_FILENAME_LEN];
stuff_string(file, F_FILESPEC, MAX_FILENAME_LEN);
required_string("$Recommendation text:");
SCP_string rec;
stuff_string(rec, F_MULTITEXT);
traitor_override_t traitor;
traitor.name = std::move(name);
traitor.text = std::move(text);
traitor.recommendation_text = std::move(rec);
strcpy_s(traitor.voice_filename, file);
Traitor_overrides.push_back(std::move(traitor));
}
}
}
catch (const parse::ParseException& e)
{
mprintf(("TABLES: Unable to parse '%s'! Error message = %s.\n", "traitor.tbl", e.what()));
return;
}
}
// initialize traitor stuff at game startup
void traitor_init()
{
// there is only ever one traitor debriefing stage
Traitor_debriefing.num_stages = 1;
Traitor_debriefing.stages[0].formula = 1;
// initialize this to an empty string so it can be optional
Traitor.recommendation_text = "";
// first parse the default table
parse_traitor_tbl("traitor.tbl");
// parse any modular tables
parse_modular_table("*-trtr.tbm", parse_traitor_tbl);
// check that we have the default traitor info
if (Traitor.debriefing_text.find(-1) == Traitor.debriefing_text.end()) {
Warning(LOCATION, "Traitor is missing default debriefing information.\n");
Traitor.debriefing_text[-1] = "";
}
}
// initialize a nice blank scoring element
void scoring_struct::init(bool reset_score_and_rank)
{
flags = 0;
if (reset_score_and_rank) {
score = 0;
rank = 0;
}
medal_counts.assign(Medals.size(), 0);
memset(kills, 0, sizeof(kills));
assists = 0;
kill_count = 0;
kill_count_ok = 0;
p_shots_fired = 0;
s_shots_fired = 0;
p_shots_hit = 0;
s_shots_hit = 0;
p_bonehead_hits = 0;
s_bonehead_hits = 0;
bonehead_kills = 0;
missions_flown = 0;
flight_time = 0;
last_flown = 0;
last_backup = 0;
m_medal_earned = -1; // hasn't earned a medal yet
m_promotion_earned = -1;
m_badge_earned.clear();
m_score = 0;
memset(m_kills, 0, sizeof(m_kills));
memset(m_okKills, 0, sizeof(m_okKills));
m_kill_count = 0;
m_kill_count_ok = 0;
m_assists = 0;
mp_shots_fired = 0;
ms_shots_fired = 0;
mp_shots_hit = 0;
ms_shots_hit = 0;
mp_bonehead_hits = 0;
ms_bonehead_hits = 0;
m_bonehead_kills = 0;
m_player_deaths = 0;
memset(m_dogfight_kills, 0, sizeof(m_dogfight_kills));
}
// clone someone else's scoring element
void scoring_struct::assign(const scoring_struct &s)
{
flags = s.flags;
score = s.score;
rank = s.rank;
medal_counts.assign(s.medal_counts.begin(), s.medal_counts.end());
memcpy(kills, s.kills, MAX_SHIP_CLASSES * sizeof(int));
assists = s.assists;
kill_count = s.kill_count;
kill_count_ok = s.kill_count_ok;
p_shots_fired = s.p_shots_fired;
s_shots_fired = s.s_shots_fired;
p_shots_hit = s.p_shots_hit;
s_shots_hit = s.s_shots_hit;
p_bonehead_hits = s.p_bonehead_hits;
s_bonehead_hits = s.s_bonehead_hits;
bonehead_kills = s.bonehead_kills;
missions_flown = s.missions_flown;
flight_time = s.flight_time;
last_flown = s.last_flown;
last_backup = s.last_backup;
m_medal_earned = s.m_medal_earned;
m_promotion_earned = s.m_promotion_earned;
m_badge_earned = s.m_badge_earned;
m_score = s.m_score;
memcpy(m_kills, s.m_kills, MAX_SHIP_CLASSES * sizeof(int));
memcpy(m_okKills, s.m_okKills, MAX_SHIP_CLASSES * sizeof(int));
m_kill_count = s.m_kill_count;
m_kill_count_ok = s.m_kill_count_ok;
m_assists = s.m_assists;
mp_shots_fired = s.mp_shots_fired;
ms_shots_fired = s.ms_shots_fired;
mp_shots_hit = s.mp_shots_hit;
ms_shots_hit = s.ms_shots_hit;
mp_bonehead_hits = s.mp_bonehead_hits;
ms_bonehead_hits = s.ms_bonehead_hits;
m_bonehead_kills = s.m_bonehead_kills;
m_player_deaths = s.m_player_deaths;
memcpy(m_dogfight_kills, s.m_dogfight_kills, MAX_PLAYERS * sizeof(int));
}
template<typename T, size_t N>
bool array_compare(const T (&left)[N], const T (&right)[N]) {
auto left_el = std::begin(left);
auto right_el = std::begin(right);
auto left_end = std::end(left);
auto right_end = std::end(right);
for (; left_el != left_end && right_el != right_end; ++left_el, ++right_el) {
if (!(*left_el == *right_el)) {
return false;
}
}
return true;
}
bool scoring_struct::operator==(const scoring_struct& rhs) const {
return flags == rhs.flags && score == rhs.score && rank == rhs.rank && medal_counts == rhs.medal_counts
&& array_compare(kills, rhs.kills) && assists == rhs.assists && kill_count == rhs.kill_count
&& kill_count_ok == rhs.kill_count_ok && p_shots_fired == rhs.p_shots_fired
&& s_shots_fired == rhs.s_shots_fired && p_shots_hit == rhs.p_shots_hit && s_shots_hit == rhs.s_shots_hit
&& p_bonehead_hits == rhs.p_bonehead_hits && s_bonehead_hits == rhs.s_bonehead_hits
&& bonehead_kills == rhs.bonehead_kills && missions_flown == rhs.missions_flown
&& flight_time == rhs.flight_time && last_flown == rhs.last_flown && last_backup == rhs.last_backup
&& m_medal_earned == rhs.m_medal_earned && m_badge_earned == rhs.m_badge_earned
&& m_promotion_earned == rhs.m_promotion_earned && m_score == rhs.m_score
&& array_compare(m_kills, rhs.m_kills)
&& array_compare(m_okKills, rhs.m_okKills) && m_kill_count == rhs.m_kill_count
&& m_kill_count_ok == rhs.m_kill_count_ok && m_assists == rhs.m_assists && mp_shots_fired == rhs.mp_shots_fired
&& ms_shots_fired == rhs.ms_shots_fired && mp_shots_hit == rhs.mp_shots_hit && ms_shots_hit == rhs.ms_shots_hit
&& mp_bonehead_hits == rhs.mp_bonehead_hits && ms_bonehead_hits == rhs.ms_bonehead_hits
&& m_bonehead_kills == rhs.m_bonehead_kills && m_player_deaths == rhs.m_player_deaths
&& array_compare(m_dogfight_kills, rhs.m_dogfight_kills) ;
}
bool scoring_struct::operator!=(const scoring_struct& rhs) const {
return !(rhs == *this);
}
// initialize the Player's mission-based stats before he goes into a mission
void scoring_level_init( scoring_struct *scp )
{
scp->m_medal_earned = -1; // hasn't earned a medal yet
scp->m_promotion_earned = -1;
scp->m_badge_earned.clear();
scp->m_score = 0;
scp->m_assists = 0;
scp->mp_shots_fired = 0;
scp->mp_shots_hit = 0;
scp->ms_shots_fired = 0;
scp->ms_shots_hit = 0;
scp->mp_bonehead_hits=0;
scp->ms_bonehead_hits=0;
scp->m_bonehead_kills=0;
memset(scp->m_kills, 0, MAX_SHIP_CLASSES * sizeof(int));
memset(scp->m_okKills, 0, MAX_SHIP_CLASSES * sizeof(int));
scp->m_kill_count = 0;
scp->m_kill_count_ok = 0;
scp->m_player_deaths = 0;
memset(scp->m_dogfight_kills, 0, MAX_PLAYERS * sizeof(int));
if (The_mission.ai_profile != NULL) {
Kill_percentage = The_mission.ai_profile->kill_percentage_scale[Game_skill_level];
Assist_percentage = The_mission.ai_profile->assist_percentage_scale[Game_skill_level];
} else {
Kill_percentage = 0.30f;
Assist_percentage = 0.15f;
}
}
void scoring_eval_rank( scoring_struct *sc )
{
int i, score, new_rank, old_rank;
old_rank = sc->rank;
new_rank = old_rank;
// first check to see if the promotion flag is set -- if so, return the new rank
if ( Player->flags & PLAYER_FLAGS_PROMOTED ) {
// if the player does indeed get promoted, we should change his mission score
// to reflect the difference between all time and new rank score
if (old_rank < ((int)Ranks.size() -1)) {
new_rank++;
if ( (sc->m_score + sc->score) < Ranks[new_rank].points )
sc->m_score = (Ranks[new_rank].points - sc->score);
}
} else {
// we get here only if player wasn't promoted automatically.
// it is possible to get a negative mission score but that will
// never result in a degradation
score = sc->m_score + sc->score;
for (i=old_rank + 1; i<(int)Ranks.size(); i++) {
if ( score >= Ranks[i].points )
new_rank = i;
}
}
// if the ranks do not match, then "grant" the new rank
if ( old_rank != new_rank ) {
Assert( new_rank >= 0 );
sc->m_promotion_earned = new_rank;
sc->rank = new_rank;
}
}
// function to evaluate whether or not a new badge is going to be awarded. This function returns
// which medal is awarded.
void scoring_eval_badges(scoring_struct *sc)
{
int total_kills;
// to determine badges, we count kills based on fighter/bomber types. We must count kills in
// all time stats + current mission stats. And, only for enemy fighters/bombers
total_kills = 0;
for (auto it = Ship_info.cbegin(); it != Ship_info.cend(); ++it) {
if (it->is_fighter_bomber()) {
auto i = std::distance(Ship_info.cbegin(), it);
total_kills += sc->m_okKills[i];
total_kills += sc->kills[i];
}
}
// total_kills should now reflect the number of kills on hostile fighters/bombers. Check this number
// against badge kill numbers, and award the appropriate badges as neccessary.
// Now properly awards all appropriate badges regardless of their position in the medals vector,
// but keeps the highest award to show to the player - Mjn
int last_badge_kills = 0;
for (auto i = 0; i < (int)Medals.size(); i++) {
if ( total_kills >= Medals[i].kills_needed
&& Medals[i].kills_needed > 0 )
{
if (sc->medal_counts[i] < 1) {
sc->medal_counts[i] = 1;
if (Medals[i].kills_needed > last_badge_kills) {
last_badge_kills = Medals[i].kills_needed;
sc->m_badge_earned.push_back(i);
}
}
}
}
}
// central point for dealing with accepting the score for a misison.
void scoring_do_accept(scoring_struct *score)
{
int idx;
// do rank, badges, and medals first since they require the alltime stuff
// to not be updated yet.
// do medal stuff
if ( score->m_medal_earned != -1 ){
score->medal_counts[score->m_medal_earned]++;
}
// return when in training mission. We can grant a medal in training, but don't
// want to calculate any other statistics.
if (The_mission.game_type == MISSION_TYPE_TRAINING){
return;
}
scoring_eval_rank(score);
scoring_eval_badges(score);
score->kill_count += score->m_kill_count;
score->kill_count_ok += score->m_kill_count_ok;
score->score += score->m_score;
score->assists += score->m_assists;
score->p_shots_fired += score->mp_shots_fired;
score->s_shots_fired += score->ms_shots_fired;
score->p_shots_hit += score->mp_shots_hit;
score->s_shots_hit += score->ms_shots_hit;
score->p_bonehead_hits += score->mp_bonehead_hits;
score->s_bonehead_hits += score->ms_bonehead_hits;
score->bonehead_kills += score->m_bonehead_kills;
for(idx=0;idx<MAX_SHIP_CLASSES;idx++){
score->kills[idx] = (int)(score->kills[idx] + score->m_okKills[idx]);
}
// add in mission time
score->flight_time += (unsigned int)f2fl(Missiontime);
score->last_backup = score->last_flown;
score->last_flown = (_fs_time_t)time(NULL);
score->missions_flown++;
}
// backout the score for a mission. This function gets called when the player chooses to refly a misison
// after debriefing
void scoring_backout_accept( scoring_struct *score )
{
int idx;
// if a badge was earned, take it back
if ( score->m_badge_earned.size() ){
for (size_t medal = 0; medal < score->m_badge_earned.size(); medal++) {
score->medal_counts[score->m_badge_earned[medal]] = 0;
}
}
// return when in training mission. We can grant a medal in training, but don't
// want to calculate any other statistics.
if (The_mission.game_type == MISSION_TYPE_TRAINING){
return;
}
score->kill_count -= score->m_kill_count;
score->kill_count_ok -= score->m_kill_count_ok;
score->score -= score->m_score;
score->assists -= score->m_assists;
score->p_shots_fired -= score->mp_shots_fired;
score->s_shots_fired -= score->ms_shots_fired;
score->p_shots_hit -= score->mp_shots_hit;
score->s_shots_hit -= score->ms_shots_hit;
score->p_bonehead_hits -= score->mp_bonehead_hits;
score->s_bonehead_hits -= score->ms_bonehead_hits;
score->bonehead_kills -= score->m_bonehead_kills;
for(idx=0;idx<MAX_SHIP_CLASSES;idx++){
score->kills[idx] = (unsigned short)(score->kills[idx] - score->m_okKills[idx]);
}
// if the player was given a medal, take it back
if ( score->m_medal_earned != -1 ) {
score->medal_counts[score->m_medal_earned]--;
Assert( score->medal_counts[score->m_medal_earned] >= 0 );
}
// if the player was promoted, take it back
if ( score->m_promotion_earned != -1) {
score->rank--;
Assert( score->rank >= 0 );
}
score->flight_time -= (unsigned int)f2fl(Missiontime);
score->last_flown = score->last_backup;
score->missions_flown--;
}
// merge any mission stats accumulated into the alltime stats (as well as updating per campaign stats)
void scoring_level_close(int accepted)
{
// want to calculate any other statistics.
if (The_mission.game_type == MISSION_TYPE_TRAINING){
// call scoring_do_accept
// this will grant any potential medals and then early bail, and
// then we will early bail
scoring_do_accept(&Player->stats);
Pilot.update_stats(&Player->stats, true);
return;
}
if(accepted){
// apply mission stats for all players in the game
int idx;
scoring_struct *sc;
if(Game_mode & GM_MULTIPLAYER){
nprintf(("Network","Storing stats for all players now\n"));
for(idx=0;idx<MAX_PLAYERS;idx++){
if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx])){
// get the scoring struct
sc = &Net_players[idx].m_player->stats;
scoring_do_accept( sc );
if (Net_player == &Net_players[idx]) {
Pilot.update_stats(sc);
}
}
}
} else {
nprintf(("General","Storing stats now\n"));
scoring_do_accept( &Player->stats );
}
// If this mission doesn't allow promotion or badges
// then be sure that these don't get done. Don't allow promotions or badges when
// playing normally and not in a campaign.
if ( (The_mission.flags[Mission::Mission_Flags::No_promotion]) || ((Game_mode & GM_NORMAL) && !(Game_mode & GM_CAMPAIGN_MODE)) ) {
if ( Player->stats.m_promotion_earned != -1) {
Player->stats.rank--;
Player->stats.m_promotion_earned = -1;
}
// if a badge was earned, take it back
if ( Player->stats.m_badge_earned.size() ){
for (size_t medal = 0; medal < Player->stats.m_badge_earned.size(); medal++) {
Player->stats.medal_counts[Player->stats.m_badge_earned[medal]] = 0;
}
Player->stats.m_badge_earned.clear();
}
}
if ( !(Game_mode & GM_MULTIPLAYER) ) {
Pilot.update_stats(&Player->stats);
}
}
}
// STATS damage, assists recording stuff
void scoring_add_damage(const object *ship_objp, const object *other_obj, float damage)
{
int found_slot, signature;
int lowest_index,idx;
const object *use_obj;
ship *sp;
// multiplayer clients bail here
if(MULTIPLAYER_CLIENT){
return;
}
// if we have no other object, bail
if(other_obj == NULL){
return;
}
// for player kill/assist evaluation, we have to know exactly how much damage really mattered. For example, if
// a ship had 1 hit point left, and the player hit it with a nuke, it doesn't matter that it did 10,000,000
// points of damage, only that 1 point would count
float actual_damage = 0.0f;
// other_obj might not always be the parent of other_obj (in the case of debug code for sure). See
// if the other_obj has a parent, and if so, use the parent. If no parent, see if other_obj is a ship
// and if so, use that ship.
if ( other_obj->parent != -1 ){
use_obj = &Objects[other_obj->parent];
signature = use_obj->signature;
} else {
signature = other_obj->signature;
use_obj = other_obj;
}
// don't count damage done to a ship by himself
if(use_obj == ship_objp){
return;
}
// get a pointer to the ship and add the actual amount of damage done to it
// get the ship object, and determine the _actual_ amount of damage done
sp = &Ships[ship_objp->instance];
// see comments at beginning of function
if(ship_objp->hull_strength < 0.0f){
actual_damage = damage + ship_objp->hull_strength;
} else {
actual_damage = damage;
}
if(actual_damage < 0.0f){
actual_damage = 0.0f;
}
sp->total_damage_received += actual_damage;
// go through and clear out all old damagers
for(idx=0; idx<MAX_DAMAGE_SLOTS; idx++){
if((sp->damage_ship_id[idx] >= 0) && (ship_get_by_signature(sp->damage_ship_id[idx]) < 0)){
sp->damage_ship_id[idx] = -1;
sp->damage_ship[idx] = 0;
}
}
// only evaluate possible kill/assist numbers if the hitting object (use_obj) is a piloted ship (ie, ignore asteroids, etc)
// don't store damage a ship may do to himself
if((ship_objp->type == OBJ_SHIP) && (use_obj->type == OBJ_SHIP)){
found_slot = 0;
// try and find an open slot
for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
// if this ship object doesn't exist anymore, use the slot
if((sp->damage_ship_id[idx] == -1) || (ship_get_by_signature(sp->damage_ship_id[idx]) < 0) || (sp->damage_ship_id[idx] == signature) ){
found_slot = 1;
break;
}
}
// if not found (implying all slots are taken), then find the slot with the lowest damage % and use that
if(!found_slot){
lowest_index = 0;
for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
if(sp->damage_ship[idx] < sp->damage_ship[lowest_index]){
lowest_index = idx;
}
}
} else {
lowest_index = idx;
}
// fill in the slot damage and damager-index
if(found_slot){
sp->damage_ship[lowest_index] += actual_damage;
} else {
sp->damage_ship[lowest_index] = actual_damage;
}
sp->damage_ship_id[lowest_index] = signature;
}
}
char Scoring_debug_text[4096];
// evaluate a kill on a ship
int scoring_eval_kill(const object *ship_objp)
{
float max_damage_pct; // the pct% of total damage the max damage object did
int max_damage_index; // the index into the dying ship's damage_ship[] array corresponding the greatest amount of damage
int killer_sig; // signature of the guy getting credit for the kill (or -1 if none)
int idx,net_player_num;
player *plr; // pointer to a player struct if it was a player who got the kill
net_player *net_plr = NULL;
ship *dead_ship; // the ship which was killed
net_player *dead_plr = NULL;
float scoring_scale_by_damage = 1; // percentage to scale the killer's score by if we score based on the amount of damage caused
int kill_score, assist_score;
bool is_enemy_player = false; // true if the player just killed an enemy player ship
// multiplayer clients bail here
if(MULTIPLAYER_CLIENT){
return -1;
}
// we don't evaluate kills on anything except ships
if(ship_objp->type != OBJ_SHIP){
return -1;
}
if((ship_objp->instance < 0) || (ship_objp->instance >= MAX_SHIPS)){
return -1;
}
// assign the dead ship
dead_ship = &Ships[ship_objp->instance];
// evaluate player deaths
if(Game_mode & GM_MULTIPLAYER){
net_player_num = multi_find_player_by_object(ship_objp);
if(net_player_num != -1){
Net_players[net_player_num].m_player->stats.m_player_deaths++;
nprintf(("Network","Setting player %s deaths to %d\n",Net_players[net_player_num].m_player->callsign,Net_players[net_player_num].m_player->stats.m_player_deaths));
dead_plr = &Net_players[net_player_num];
is_enemy_player = true;
}
} else {
if(ship_objp == Player_obj){
Player->stats.m_player_deaths++;
}
}
net_player_num = -1;
// clear out invalid damager ships
for(idx=0; idx<MAX_DAMAGE_SLOTS; idx++){
if((dead_ship->damage_ship_id[idx] >= 0) && (ship_get_by_signature(dead_ship->damage_ship_id[idx]) < 0)){
dead_ship->damage_ship[idx] = 0.0f;
dead_ship->damage_ship_id[idx] = -1;
}
}
// determine which object did the most damage to the dying object, and how much damage that was
max_damage_index = -1;
for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
// bogus ship
if(dead_ship->damage_ship_id[idx] < 0){
continue;
}
// if this slot did more damage then the next highest slot
if((max_damage_index == -1) || (dead_ship->damage_ship[idx] > dead_ship->damage_ship[max_damage_index])){
max_damage_index = idx;
}
}
// doh
if((max_damage_index < 0) || (max_damage_index >= MAX_DAMAGE_SLOTS)){
return -1;
}
// the pct of total damage applied to this ship
max_damage_pct = dead_ship->damage_ship[max_damage_index] / dead_ship->total_damage_received;
CLAMP(max_damage_pct, 0.0f, 1.0f);