]> icculus.org git repositories - taylor/freespace2.git/blob - src/stats/scoring.cpp
Freespace 1 support
[taylor/freespace2.git] / src / stats / scoring.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/Stats/Scoring.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Scoring system code, medals, rank, etc.
16  *
17  * $Log$
18  * Revision 1.4  2003/05/25 02:30:44  taylor
19  * Freespace 1 support
20  *
21  * Revision 1.3  2002/06/09 04:41:27  relnev
22  * added copyright header
23  *
24  * Revision 1.2  2002/05/07 03:16:52  theoddone33
25  * The Great Newline Fix
26  *
27  * Revision 1.1.1.1  2002/05/03 03:28:10  root
28  * Initial import.
29  *
30  * 
31  * 23    10/25/99 5:51p Jefff
32  * commented some scoring debug code
33  * 
34  * 22    10/15/99 3:01p Jefff
35  * scoring change in coop and tvt.  for big ships, killer gets 100% of
36  * score, but teammates also get 50%.
37  * 
38  * 21    9/12/99 9:56p Jefff
39  * fixed another cause of the
40  * medals-in-training-missions-not-getting-recorded bug
41  * 
42  * 20    9/10/99 9:44p Dave
43  * Bumped version # up. Make server reliable connects not have such a huge
44  * timeout. 
45  * 
46  * 19    9/05/99 11:19p Dave
47  * Made d3d texture cache much more safe. Fixed training scoring bug where
48  * it would backout scores without ever having applied them in the first
49  * place.
50  * 
51  * 18    8/26/99 8:49p Jefff
52  * Updated medals screen and about everything that ever touches medals in
53  * one way or another.  Sheesh.
54  * 
55  * 17    8/22/99 5:53p Dave
56  * Scoring fixes. Added self destruct key. Put callsigns in the logfile
57  * instead of ship designations for multiplayer players.
58  * 
59  * 16    8/22/99 1:19p Dave
60  * Fixed up http proxy code. Cleaned up scoring code. Reverse the order in
61  * which d3d cards are detected.
62  * 
63  * 15    8/17/99 1:12p Dave
64  * Send TvT update when a client has finished joining so he stays nice and
65  * synched.
66  * 
67  * 14    8/06/99 9:46p Dave
68  * Hopefully final changes for the demo.
69  * 
70  * 13    7/30/99 7:01p Dave
71  * Dogfight escort gauge. Fixed up laser rendering in Glide.
72  * 
73  * 12    4/30/99 12:18p Dave
74  * Several minor bug fixes.
75  * 
76  * 11    4/28/99 11:13p Dave
77  * Temporary checkin of artillery code.
78  * 
79  * 10    3/01/99 10:00a Dave
80  * Fxied several dogfight related stats bugs.
81  * 
82  * 9     2/26/99 4:14p Dave
83  * Put in the ability to have multiple shockwaves for ships.
84  * 
85  * 8     2/23/99 2:29p Dave
86  * First run of oldschool dogfight mode. 
87  * 
88  * 7     2/17/99 2:11p Dave
89  * First full run of squad war. All freespace and tracker side stuff
90  * works.
91  * 
92  * 6     1/29/99 2:08a Dave
93  * Fixed beam weapon collisions with players. Reduced size of scoring
94  * struct for multiplayer. Disabled PXO.
95  * 
96  * 5     1/12/99 5:45p Dave
97  * Moved weapon pipeline in multiplayer to almost exclusively client side.
98  * Very good results. Bandwidth goes down, playability goes up for crappy
99  * connections. Fixed object update problem for ship subsystems.
100  * 
101  * 4     10/23/98 3:51p Dave
102  * Full support for tstrings.tbl and foreign languages. All that remains
103  * is to make it active in Fred.
104  * 
105  * 3     10/13/98 9:29a Dave
106  * Started neatening up freespace.h. Many variables renamed and
107  * reorganized. Added AlphaColors.[h,cpp]
108  * 
109  * 2     10/07/98 10:54a Dave
110  * Initial checkin.
111  * 
112  * 1     10/07/98 10:51a Dave
113  * 
114  * 84    9/15/98 11:44a Dave
115  * Renamed builtin ships and wepaons appropriately in FRED. Put in scoring
116  * scale factors. Fixed standalone filtering of MD missions to non-MD
117  * hosts.
118  * 
119  * 83    9/10/98 6:14p Dave
120  * Removed bogus assert.
121  * 
122  * 82    7/14/98 2:37p Allender
123  * fixed bug where two scoring variables were not getting properly reset
124  * between levels in multiplayer game
125  * 
126  * 81    6/01/98 11:43a John
127  * JAS & MK:  Classified all strings for localization.
128  * 
129  * 80    5/23/98 3:16p Allender
130  * work on object update packet optimizations (a new updating system).
131  * Don't allow medals/promotions/badges when playing singple player
132  * missions through the simulator
133  * 
134  * 79    5/18/98 9:14p Dave
135  * Put in network config files support.
136  * 
137  * 78    5/16/98 9:14p Allender
138  * fix scoring ckise fir training missions to actually count medals, but
139  * nothing else.  Code used to Assert when wings were granted then taken
140  * away because they were actually never granted in scoring structure
141  * 
142  * 77    5/15/98 9:52p Dave
143  * Added new stats for freespace. Put in artwork for viewing stats on PXO.
144  * 
145  * 76    5/15/98 4:12p Allender
146  * removed redbook code.  Put back in ingame join timer.  Major fixups for
147  * stats in multiplayer.  Pass correct score, medals, etc when leaving
148  * game.  Be sure clients display medals, badges, etc.
149  * 
150  * 75    5/13/98 5:13p Allender
151  * red alert support to go back to previous mission
152  * 
153  * 74    5/07/98 12:56a Dave
154  * Fixed incorrect calls to free() from stats code. Put in new artwork for
155  * debrief and host options screens. Another modification to scoring
156  * system for secondary weapons.
157  * 
158  * 73    5/06/98 3:15p Allender
159  * fixed some ranking problems.  Made vasudan support ship available in
160  * multiplayer mode.
161  * 
162  * 72    5/04/98 1:43p Dave
163  * Fixed up a standalone resetting problem. Fixed multiplayer stats
164  * collection for clients. Make sure all multiplayer ui screens have the
165  * correct palette at all times.
166  * 
167  * 71    5/03/98 2:52p Dave
168  * Removed multiplayer furball mode.
169  * 
170  * 70    5/01/98 12:34p John
171  * Added code to force FreeSpace to run in the same dir as exe and made
172  * all the parse error messages a little nicer.
173  * 
174  * 69    4/28/98 5:27p Hoffoss
175  * Added kills by type stats to barracks, and fixed some bugs this turned
176  * up but no one knew about yet apparently.
177  * 
178  * 68    4/27/98 6:01p Dave
179  * Modify how missile scoring works. Fixed a team select ui bug. Speed up
180  * multi_lag system. Put in new main hall.
181  * 
182  * 67    4/25/98 3:54p Dave
183  * Fixed a scoring bug where hidden ships had improper return values from
184  * scoring_eval_kill(...)
185  * 
186  * 66    4/21/98 11:55p Dave
187  * Put in player deaths statskeeping. Use arrow keys in the ingame join
188  * ship select screen. Don't quit the game if in the debriefing and server
189  * leaves.
190  * 
191  * 65    4/21/98 4:44p Dave
192  * Implement Vasudan ships in multiplayer. Added a debug function to bash
193  * player rank. Fixed a few rtvoice buffer overrun problems. Fixed ui
194  * problem in options screen.  
195  *
196  * $NoKeywords: $
197  */
198
199 #include "freespace.h"
200 #include "pstypes.h"
201 #include "object.h"
202 #include "ship.h"
203 #include "multi.h"
204 #include "multiutil.h"
205 #include "scoring.h"
206 #include "player.h"
207 #include "parselo.h"
208 #include "multimsgs.h"
209 #include "medals.h"
210 #include "localize.h"
211 #include "multi_team.h"
212 #include "multi_dogfight.h"
213 #include "multi_pmsg.h"
214
215 // 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
216 #define ASSIST_PERCENTAGE                               (0.15f)
217 #define KILL_PERCENTAGE                                 (0.30f)
218
219 // these tables are overwritten with the values from rank.tbl
220 rank_stuff Ranks[NUM_RANKS];
221
222 // scoring scale factors by skill level
223 float Scoring_scale_factors[NUM_SKILL_LEVELS] = {
224         0.2f,                                   // very easy
225         0.4f,                                   // easy
226         0.7f,                                   // medium
227         1.0f,                                   // hard
228         1.25f                                   // insane
229 };
230
231 void parse_rank_tbl()
232 {
233         char buf[MULTITEXT_LENGTH];
234         int rval, idx;
235
236         if ((rval = setjmp(parse_abort)) != 0) {
237                 Error(LOCATION, "Error parsing 'rank.tbl'\r\nError code = %i.\r\n", rval);
238         } 
239
240         // open localization
241         lcl_ext_open();
242
243         read_file_text("rank.tbl");
244         reset_parse();
245
246         // parse in all the rank names
247         idx = 0;
248         skip_to_string("[RANK NAMES]");
249         ignore_white_space();
250         while ( required_string_either("#End", "$Name:") ) {
251                 Assert ( idx < NUM_RANKS );
252                 required_string("$Name:");
253                 stuff_string( Ranks[idx].name, F_NAME, NULL );
254                 required_string("$Points:");
255                 stuff_int( &Ranks[idx].points );
256                 required_string("$Bitmap:");
257                 stuff_string( Ranks[idx].bitmap, F_NAME, NULL );
258                 required_string("$Promotion Voice Base:");
259                 stuff_string( Ranks[idx].promotion_voice_base, F_NAME, NULL, MAX_FILENAME_LEN - 2 );
260                 required_string("$Promotion Text:");
261                 stuff_string(buf, F_MULTITEXT, NULL);
262                 drop_white_space(buf);
263                 compact_multitext_string(buf);
264                 Ranks[idx].promotion_text = strdup(buf);
265                 idx++;
266         }
267
268         required_string("#End");
269
270         // be sure that all rank points are in order
271 #ifndef NDEBUG
272         for ( idx = 0; idx < NUM_RANKS-1; idx++ ) {
273                 if ( Ranks[idx].points >= Ranks[idx+1].points )
274                         Int3();
275         }
276 #endif
277
278         // close localization
279         lcl_ext_close();
280 }
281
282 // initialize a nice blank scoring element
283 void init_scoring_element(scoring_struct *s)
284 {
285         int i;
286
287         if (s == NULL) {
288                 Int3(); //      DaveB -- Fix this!
289                 // read_pilot_file(char* callsign);
290                 return;
291         }
292
293         memset(s, 0, sizeof(scoring_struct));
294         s->score = 0;
295         s->rank = RANK_ENSIGN;
296         s->assists = 0;
297         s->kill_count = 0;
298         s->kill_count_ok = 0;
299
300         for (i=0; i<NUM_MEDALS; i++){
301                 s->medals[i] = 0;
302         }
303
304         for (i=0; i<MAX_SHIP_TYPES; i++){
305                 s->kills[i] = 0;
306                 s->m_kills[i] = 0;
307         }
308
309         s->m_kill_count         = 0;
310         s->m_kill_count_ok      = 0;
311
312         s->m_score = 0;
313         s->m_assists = 0;
314    s->p_bonehead_hits=0; s->mp_bonehead_hits=0;
315         s->s_bonehead_hits=0; s->ms_bonehead_hits=0;
316         s->m_bonehead_kills=0;
317         
318         s->bonehead_kills=0;   
319    
320         s->p_shots_fired=0; s->p_shots_hit=0;
321    s->s_shots_fired=0; s->s_shots_hit=0;
322
323    s->mp_shots_fired=0; s->mp_shots_hit=0;
324    s->ms_shots_fired=0; s->ms_shots_hit=0;
325
326         s->m_player_deaths = 0;
327
328    s->flags = 0;        
329
330         s->missions_flown = 0;
331         s->flight_time = 0;
332         s->last_flown = 0;
333         s->last_backup = 0;
334
335         for(i=0; i<MAX_PLAYERS; i++){
336                 s->m_dogfight_kills[i] = 0;
337         }
338 }
339
340 #ifndef NDEBUG
341 //XSTR:OFF
342 void scoring_eval_harbison( ship *shipp )
343 {
344         FILE *fp;
345
346         if ( !stricmp(shipp->ship_name, "alpha 2") && (!stricmp(Game_current_mission_filename, "demo01") || !stricmp(Game_current_mission_filename, "sm1-01")) ) {
347                 int death_count;
348
349                 fp = fopen("i:\\volition\\cww\\harbison.txt", "r+t");
350                 if ( !fp )
351                         return;
352                 fscanf(fp, "%d", &death_count );
353                 death_count++;
354                 fseek(fp, 0, SEEK_SET);
355                 fprintf(fp, "%d\n", death_count);
356                 fclose(fp);
357         }
358 }
359 //XSTR:ON
360 #endif
361
362 // initialize the Player's mission-based stats before he goes into a mission
363 void scoring_level_init( scoring_struct *scp )
364 {
365         int i;
366
367         scp->m_medal_earned = -1;               // hasn't earned a medal yet
368         scp->m_promotion_earned = -1;
369         scp->m_badge_earned = -1;
370    scp->m_score = 0;
371         scp->m_assists = 0;
372         scp->mp_shots_fired=0;
373         scp->mp_shots_hit = 0;
374         scp->ms_shots_fired = 0;
375         scp->ms_shots_hit = 0;
376
377         scp->mp_bonehead_hits=0;
378         scp->ms_bonehead_hits=0;
379         scp->m_bonehead_kills=0;
380
381    for (i=0; i<MAX_SHIP_TYPES; i++){
382                 scp->m_kills[i] = 0;
383                 scp->m_okKills[i]=0;
384         }
385
386         scp->m_kill_count = 0;
387         scp->m_kill_count_ok = 0;
388         
389         scp->m_player_deaths =0;
390
391         for(i=0; i<MAX_PLAYERS; i++){
392                 scp->m_dogfight_kills[i] = 0;
393         }
394 }
395
396 void scoring_eval_rank( scoring_struct *sc )
397 {
398         int i, score, new_rank, old_rank;
399
400         old_rank = sc->rank;
401         new_rank = old_rank;
402
403         // first check to see if the promotion flag is set -- if so, return the new rank
404         if ( Player->flags & PLAYER_FLAGS_PROMOTED ) {
405         
406                 new_rank = sc->rank;
407
408                 // if the player does indeed get promoted, we should change his mission score
409                 // to reflect the differce between all time and new rank score
410 #ifdef MAKE_FS1
411                 if ( sc->rank < MAX_FREESPACE1_RANK ) {
412 #else
413                 if ( sc->rank < MAX_FREESPACE2_RANK ) {
414 #endif
415                         new_rank = sc->rank + 1;
416                         if ( (sc->m_score + sc->score) < Ranks[new_rank].points )
417                                 sc->m_score = (Ranks[new_rank].points - sc->score);
418                 }
419         } else {
420                 // we get here only if player wasn't promoted automatically.
421                 score = sc->m_score + sc->score;
422                 for (i=0; i<NUM_RANKS; i++) {
423                         if ( score >= Ranks[i].points )
424                                 new_rank = i;
425                 }
426         }
427
428         // if the ranks do not match, then "grant" the new rank
429         if ( old_rank != new_rank ) {
430                 Assert( new_rank >= 0 );
431                 sc->m_promotion_earned = new_rank;
432                 sc->rank = new_rank;
433         }
434 }
435
436 // function to evaluate whether or not a new badge is going to be awarded.  This function returns
437 // which medal is awarded.
438 void scoring_eval_badges(scoring_struct *sc)
439 {
440         int i, total_kills, badge;
441
442         // to determine badges, we count kills based on fighter/bomber types.  We must count kills in
443         // all time stats + current mission stats.  And, only for enemy fighters/bombers
444         total_kills = 0;
445         for (i = 0; i < MAX_SHIP_TYPES; i++ ) {
446                 if ( (Ship_info[i].flags & SIF_FIGHTER) || (Ship_info[i].flags & SIF_BOMBER) ) {
447                         total_kills += sc->m_okKills[i];
448                         total_kills += sc->kills[i];
449                 }
450         }
451
452         // total_kills should now reflect the number of kills on hostile fighters/bombers.  Check this number
453         // against badge kill numbers, and return the badge index if we would get a new one.
454         badge = -1;
455         for (i = 0; i < MAX_BADGES; i++ ) {
456                 if ( total_kills >= Medals[Badge_index[i]].kills_needed ){
457                         badge = i;
458                 }
459         }
460
461         // if player could have a badge based on kills, and doesn't currently have this badge, then
462         // return the badge id.
463         if ( (badge != -1 ) && (sc->medals[Badge_index[badge]] < 1) ) {
464                 sc->medals[Badge_index[badge]] = 1;
465                 sc->m_badge_earned = badge;
466         }
467 }
468
469 // central point for dealing with accepting the score for a misison.
470 void scoring_do_accept(scoring_struct *score)
471 {
472         int idx;
473
474         // do rank, badges, and medals first since they require the alltime stuff
475         // to not be updated yet.       
476
477         // do medal stuff
478         if ( score->m_medal_earned != -1 ){
479                 score->medals[score->m_medal_earned]++;
480         }
481
482         // return when in training mission.  We can grant a medal in training, but don't
483         // want to calculate any other statistics.
484         if (The_mission.game_type == MISSION_TYPE_TRAINING){
485                 return;
486         }       
487
488         scoring_eval_rank(score);
489         scoring_eval_badges(score);
490
491         score->kill_count += score->m_kill_count;
492         score->kill_count_ok += score->m_kill_count_ok;
493
494         score->score += score->m_score;
495         score->assists += score->m_assists;
496         score->p_shots_fired += score->mp_shots_fired;
497         score->s_shots_fired += score->ms_shots_fired;
498
499         score->p_shots_hit += score->mp_shots_hit;
500         score->s_shots_hit += score->ms_shots_hit;
501
502         score->p_bonehead_hits += score->mp_bonehead_hits;
503         score->s_bonehead_hits += score->ms_bonehead_hits;
504         score->bonehead_kills += score->m_bonehead_kills;
505
506         for(idx=0;idx<MAX_SHIP_TYPES;idx++){
507                 score->kills[idx] = (unsigned short)(score->kills[idx] + score->m_okKills[idx]);
508         }
509
510         // add in mission time
511         score->flight_time += (unsigned int)f2fl(Missiontime);
512         score->last_backup = score->last_flown;
513         score->last_flown = time(NULL);
514         score->missions_flown++;
515 }
516
517 // backout the score for a mission.  This function gets called when the player chooses to refly a misison
518 // after debriefing
519 void scoring_backout_accept( scoring_struct *score )
520 {
521         int idx;
522
523         // if a badge was earned, take it back
524         if ( score->m_badge_earned != -1){
525                 score->medals[Badge_index[score->m_badge_earned]] = 0;
526         }
527
528         // return when in training mission.  We can grant a medal in training, but don't
529         // want to calculate any other statistics.
530         if (The_mission.game_type == MISSION_TYPE_TRAINING){
531                 return;
532         }
533
534         score->kill_count -= score->m_kill_count;
535         score->kill_count_ok -= score->m_kill_count_ok;
536
537         score->score -= score->m_score;
538         score->assists -= score->m_assists;
539         score->p_shots_fired -= score->mp_shots_fired;
540         score->s_shots_fired -= score->ms_shots_fired;
541
542         score->p_shots_hit -= score->mp_shots_hit;
543         score->s_shots_hit -= score->ms_shots_hit;
544
545         score->p_bonehead_hits -= score->mp_bonehead_hits;
546         score->s_bonehead_hits -= score->ms_bonehead_hits;
547         score->bonehead_kills -= score->m_bonehead_kills;
548
549         for(idx=0;idx<MAX_SHIP_TYPES;idx++){
550                 score->kills[idx] = (unsigned short)(score->kills[idx] - score->m_okKills[idx]);
551         }
552
553         // if the player was given a medal, take it back
554         if ( score->m_medal_earned != -1 ) {
555                 score->medals[score->m_medal_earned]--;
556                 Assert( score->medals[score->m_medal_earned] >= 0 );
557         }
558
559         // if the player was promoted, take it back
560         if ( score->m_promotion_earned != -1) {
561                 score->rank--;
562                 Assert( score->rank >= 0 );
563         }       
564
565         score->flight_time -= (unsigned int)f2fl(Missiontime);
566         score->last_flown = score->last_backup; 
567         score->missions_flown--;
568 }
569
570 // merge any mission stats accumulated into the alltime stats (as well as updating per campaign stats)
571 void scoring_level_close(int accepted)
572 {
573         int idx;
574         scoring_struct *sc;
575
576         // want to calculate any other statistics.
577         if (The_mission.game_type == MISSION_TYPE_TRAINING){
578                 // call scoring_do_accept
579                 // this will grant any potential medals and then early bail, and
580                 // then we will early bail
581                 scoring_do_accept(&Player->stats);
582                 return;
583         }
584
585         if(accepted){
586                 // apply mission stats for all players in the game
587                 if(Game_mode & GM_MULTIPLAYER){
588                         nprintf(("Network","Storing stats for all players now\n"));
589                         for(idx=0;idx<MAX_PLAYERS;idx++){
590                                 if(MULTI_CONNECTED(Net_players[idx]) && !MULTI_STANDALONE(Net_players[idx])){
591                                         // get the scoring struct
592                                         sc = &Net_players[idx].player->stats;
593                                         scoring_do_accept( sc );
594                                 }
595                         }
596                 } else {
597                         nprintf(("General","Storing stats now\n"));
598                         scoring_do_accept( &Player->stats );
599                 }
600
601                 // If this mission doesn't allow promotion or badges
602                 // then be sure that these don't get done.  Don't allow promotions or badges when
603                 // playing normally and not in a campaign.
604                 if ( (The_mission.flags & MISSION_FLAG_NO_PROMOTION) || ((Game_mode & GM_NORMAL) && !(Game_mode & GM_CAMPAIGN_MODE)) ) {
605                         if ( Player->stats.m_promotion_earned != -1) {
606                                 Player->stats.rank--;
607                                 Player->stats.m_promotion_earned = -1;
608                         }
609
610                         // if a badge was earned, take it back
611                         if ( Player->stats.m_badge_earned != -1){
612                                 Player->stats.medals[Badge_index[Player->stats.m_badge_earned]] = -1;
613                                 Player->stats.m_badge_earned = -1;
614                         }
615                 }
616
617         }       
618 }
619
620 // STATS damage, assists recording stuff
621 void scoring_add_damage(object *ship_obj,object *other_obj,float damage)
622 {
623         int found_slot, signature;
624         int lowest_index,idx;
625         object *use_obj;
626         ship *sp;
627
628         // multiplayer clients bail here
629         if(MULTIPLAYER_CLIENT){
630                 return;
631         }
632
633         // if we have no other object, bail
634         if(other_obj == NULL){
635                 return;
636         }       
637
638         // for player kill/assist evaluation, we have to know exactly how much damage really mattered. For example, if
639         // 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 
640         // points of damage, only that 1 point would count
641         float actual_damage = 0.0f;
642         
643         // other_obj might not always be the parent of other_obj (in the case of debug code for sure).  See
644         // if the other_obj has a parent, and if so, use the parent.  If no parent, see if other_obj is a ship
645         // and if so, use that ship.
646         if ( other_obj->parent != -1 ){         
647                 use_obj = &Objects[other_obj->parent];
648                 signature = use_obj->signature;
649         } else {
650                 signature = other_obj->signature;
651                 use_obj = other_obj;
652         }
653         
654         // don't count damage done to a ship by himself
655         if(use_obj == ship_obj){
656                 return;
657         }
658
659         // get a pointer to the ship and add the actual amount of damage done to it
660         // get the ship object, and determine the _actual_ amount of damage done
661         sp = &Ships[ship_obj->instance];
662         // see comments at beginning of function
663         if(ship_obj->hull_strength < 0.0f){
664                 actual_damage = damage + ship_obj->hull_strength;
665         } else {
666                 actual_damage = damage;
667         }
668         if(actual_damage < 0.0f){
669                 actual_damage = 0.0f;
670         }
671         sp->total_damage_received += actual_damage;
672
673         // go through and clear out all old damagers
674         for(idx=0; idx<MAX_DAMAGE_SLOTS; idx++){
675                 if((sp->damage_ship_id[idx] >= 0) && (ship_get_by_signature(sp->damage_ship_id[idx]) < 0)){
676                         sp->damage_ship_id[idx] = -1;
677                         sp->damage_ship[idx] = 0;
678                 }
679         }
680
681         // only evaluate possible kill/assist numbers if the hitting object (use_obj) is a piloted ship (ie, ignore asteroids, etc)
682         // don't store damage a ship may do to himself
683         if((ship_obj->type == OBJ_SHIP) && (use_obj->type == OBJ_SHIP)){
684                 found_slot = 0;
685                 // try and find an open slot
686                 for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
687                         // if this ship object doesn't exist anymore, use the slot
688                         if((sp->damage_ship_id[idx] == -1) || (ship_get_by_signature(sp->damage_ship_id[idx]) < 0) || (sp->damage_ship_id[idx] == signature) ){
689                                 found_slot = 1;
690                                 break;
691                         }
692                 }
693
694                 // if not found (implying all slots are taken), then find the slot with the lowest damage % and use that
695                 if(!found_slot){
696                         lowest_index = 0;
697                         for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
698                                 if(sp->damage_ship[idx] < sp->damage_ship[lowest_index]){
699                                    lowest_index = idx;
700                                 }
701                         }
702                 } else {
703                         lowest_index = idx;
704                 }
705
706                 // fill in the slot damage and damager-index
707                 if(found_slot){
708                         sp->damage_ship[lowest_index] += actual_damage;                                                         
709                 } else {
710                         sp->damage_ship[lowest_index] = actual_damage;
711                 }
712                 sp->damage_ship_id[lowest_index] = signature;
713         }       
714 }
715
716 char Scoring_debug_text[4096];
717
718 // evaluate a kill on a ship
719 void scoring_eval_kill(object *ship_obj)
720 {               
721         float max_damage_pct;           // the pct% of total damage the max damage object did
722         int max_damage_index;           // the index into the dying ship's damage_ship[] array corresponding the greatest amount of damage
723         int killer_sig;                         // signature of the guy getting credit for the kill (or -1 if none)
724         int idx,net_player_num;
725         player *plr;                                    // pointer to a player struct if it was a player who got the kill
726         net_player *net_plr = NULL;
727         ship *dead_ship;                                // the ship which was killed
728         net_player *dead_plr = NULL;
729         int i;
730
731         // multiplayer clients bail here
732         if(MULTIPLAYER_CLIENT){
733                 return;
734         }
735
736         // we don't evaluate kills on anything except ships
737         if(ship_obj->type != OBJ_SHIP){
738                 return; 
739         }
740         if((ship_obj->instance < 0) || (ship_obj->instance >= MAX_SHIPS)){
741                 return;
742         }
743
744         // assign the dead ship
745         dead_ship = &Ships[ship_obj->instance];
746
747         // evaluate player deaths
748         if(Game_mode & GM_MULTIPLAYER){
749                 net_player_num = multi_find_player_by_object(ship_obj);
750                 if(net_player_num != -1){
751                         Net_players[net_player_num].player->stats.m_player_deaths++;
752                         nprintf(("Network","Setting player %s deaths to %d\n",Net_players[net_player_num].player->callsign,Net_players[net_player_num].player->stats.m_player_deaths));
753                         dead_plr = &Net_players[net_player_num];
754                 }
755         } else {
756                 if(ship_obj == Player_obj){
757                         Player->stats.m_player_deaths++;
758                 }
759         }
760
761         // if this ship doesn't show up on player sensors, then don't eval a kill
762         if ( dead_ship->flags & SF_HIDDEN_FROM_SENSORS ){
763                 // make sure to set invalid killer id numbers
764                 dead_ship->damage_ship_id[0] = -1;
765                 dead_ship->damage_ship[0] = -1.0f;
766                 return;
767         }
768
769 #ifndef NDEBUG
770         scoring_eval_harbison( dead_ship );
771 #endif
772
773         net_player_num = -1;
774
775         // clear out invalid damager ships
776         for(idx=0; idx<MAX_DAMAGE_SLOTS; idx++){
777                 if((dead_ship->damage_ship_id[idx] >= 0) && (ship_get_by_signature(dead_ship->damage_ship_id[idx]) < 0)){
778                         dead_ship->damage_ship[idx] = 0.0f;
779                         dead_ship->damage_ship_id[idx] = -1;
780                 }
781         }
782                         
783         // determine which object did the most damage to the dying object, and how much damage that was
784         max_damage_index = -1;
785         for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
786                 // bogus ship
787                 if(dead_ship->damage_ship_id[idx] < 0){
788                         continue;
789                 }
790
791                 // if this slot did more damage then the next highest slot
792                 if((max_damage_index == -1) || (dead_ship->damage_ship[idx] > dead_ship->damage_ship[max_damage_index])){
793                         max_damage_index = idx;
794                 }                       
795         }
796         
797         // doh
798         if((max_damage_index < 0) || (max_damage_index >= MAX_DAMAGE_SLOTS)){
799                 return;
800         }
801
802         // the pct of total damage applied to this ship
803         max_damage_pct = dead_ship->damage_ship[max_damage_index] / dead_ship->total_damage_received;
804         if(max_damage_pct < 0.0f){
805                 max_damage_pct = 0.0f;
806         } 
807         if(max_damage_pct > 1.0f){
808                 max_damage_pct = 1.0f;
809         }
810
811         // only evaluate if the max damage % is high enough to record a kill and it was done by a valid object
812         if((max_damage_pct >= KILL_PERCENTAGE) && (dead_ship->damage_ship_id[max_damage_index] >= 0)){
813                 // set killer_sig for this ship to the signature of the guy who gets credit for the kill
814                 killer_sig = dead_ship->damage_ship_id[max_damage_index];
815
816                 // null this out for now
817                 plr = NULL;
818                 net_plr = NULL;
819
820                 // get the player (whether single or multiplayer)
821                 net_player_num = -1;
822                 if(Game_mode & GM_MULTIPLAYER){
823                         net_player_num = multi_find_player_by_signature(killer_sig);
824                         if(net_player_num != -1){
825                                 plr = Net_players[net_player_num].player;
826                                 net_plr = &Net_players[net_player_num];
827                         }
828                 } else {
829                         if(Objects[Player->objnum].signature == killer_sig){
830                                 plr = Player;
831                         }
832                 }               
833
834                 // if we found a valid player, evaluate some kill details
835                 if(plr != NULL){
836                         int si_index;
837
838                         // bogus
839                         if((plr->objnum < 0) || (plr->objnum >= MAX_OBJECTS)){
840                                 return;
841                         }                       
842
843                         // get the ship info index of the ship type of this kill.  we need to take ship
844                         // copies into account here.
845                         si_index = dead_ship->ship_info_index;
846                         if ( Ship_info[si_index].flags & SIF_SHIP_COPY ){
847                                 si_index = ship_info_base_lookup( si_index );
848                         }
849
850                         // if you hit this next Assert, find allender.  If not here, don't worry about it, you may safely
851                         // continue
852                         Assert( !(Ship_info[si_index].flags & SIF_SHIP_COPY) );
853
854                         // if he killed a guy on his own team increment his bonehead kills
855                         if((Ships[Objects[plr->objnum].instance].team == dead_ship->team) && !((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT))){
856                                 plr->stats.m_bonehead_kills++;
857                                 plr->stats.m_score -= (int)(dead_ship->score * scoring_get_scale_factor());
858
859                                 // squad war
860                                 if(net_plr != NULL){
861                                         multi_team_maybe_add_score(-(int)(dead_ship->score * scoring_get_scale_factor()), net_plr->p_info.team);
862                                 }
863                         } 
864                         // otherwise increment his valid kill count and score
865                         else {
866                                 // dogfight mode
867                                 if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT) && (multi_find_player_by_object(ship_obj) < 0)){
868                                         // don't add a kill for dogfight kills on non-players
869                                 } else {
870                                         plr->stats.m_okKills[si_index]++;               
871                                         plr->stats.m_kill_count_ok++;
872                                         plr->stats.m_score += (int)(dead_ship->score * scoring_get_scale_factor());
873                                         hud_gauge_popup_start(HUD_KILLS_GAUGE);
874
875                                         // multiplayer
876                                         if(net_plr != NULL){
877                                                 multi_team_maybe_add_score((int)(dead_ship->score * scoring_get_scale_factor()), net_plr->p_info.team);
878
879                                                 // award teammates 50% of score value for big ship kills
880                                                 // not in dogfight tho
881                                                 if (!(Netgame.type_flags & NG_TYPE_DOGFIGHT) && (Ship_info[dead_ship->ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))) {
882                                                         for (i=0; i<MAX_PLAYERS; i++) {
883                                                                 if (MULTI_CONNECTED(Net_players[i]) && (Net_players[i].p_info.team == net_plr->p_info.team) && (&Net_players[i] != net_plr)) {
884                                                                         Net_players[i].player->stats.m_score += (int)(dead_ship->score * scoring_get_scale_factor() * 0.5f);
885 /*
886 #if !defined(RELEASE_REAL)
887                                                                         // DEBUG CODE TO TEST NEW SCORING
888                                                                         char score_text[1024] = "";
889                                                                         sprintf(score_text, "You get %d pts for the helping kill the big ship", (int)(dead_ship->score * scoring_get_scale_factor() * 0.5f));                                                   
890                                                                         if (Net_players[i].player != Net_player->player) {                      // check if its me
891                                                                                 send_game_chat_packet(Net_player, score_text, MULTI_MSG_TARGET, &Net_players[i], NULL, 2);                                                              
892                                                                         } else {
893                                                                                 HUD_printf(score_text);
894                                                                         }
895 #endif
896 */
897                                                                 }
898                                                         }
899                                                 }
900
901                                                 // death message
902                                                 if((Net_player != NULL) && (Net_player->flags & NETINFO_FLAG_AM_MASTER) && (net_plr != NULL) && (dead_plr != NULL) && (net_plr->player != NULL) && (dead_plr->player != NULL)){
903                                                         char dead_text[1024] = "";
904
905                                                         sprintf(dead_text, "%s gets the kill for %s", net_plr->player->callsign, dead_plr->player->callsign);                                                   
906                                                         send_game_chat_packet(Net_player, dead_text, MULTI_MSG_ALL, NULL, NULL, 2);
907                                                         HUD_printf(dead_text);
908                                                 }
909                                         }
910                                 }
911                         }
912                                 
913                         // increment his all-encompassing kills
914                         plr->stats.m_kills[si_index]++;
915                         plr->stats.m_kill_count++;                      
916                         
917                         // update everyone on this guy's kills if this is multiplayer
918                         if(MULTIPLAYER_MASTER && (net_player_num != -1)){
919                                 // send appropriate stats
920                                 if(Netgame.type_flags & NG_TYPE_DOGFIGHT){
921                                         // evaluate dogfight kills
922                                         multi_df_eval_kill(&Net_players[net_player_num], ship_obj);
923
924                                         // update stats
925                                         send_player_stats_block_packet(&Net_players[net_player_num], STATS_DOGFIGHT_KILLS);
926                                 } else {
927                                         send_player_stats_block_packet(&Net_players[net_player_num], STATS_MISSION_KILLS);
928                                 }                               
929                         }
930                 }
931         } else {
932                 // set killer_sig for this ship to -1, indicating no one got the kill for it
933                 killer_sig = -1;
934         }               
935                 
936         // pass in the guy who got the credit for the kill (if any), so that he doesn't also
937         // get credit for an assist
938         scoring_eval_assists(dead_ship,killer_sig);     
939
940         // bash damage_ship_id[0] with the signature of the guy who is getting credit for the kill
941         dead_ship->damage_ship_id[0] = killer_sig;
942         dead_ship->damage_ship[0] = max_damage_pct;
943
944 /*
945         // debug code
946 #if !defined(RELEASE_REAL)
947         if (Game_mode & GM_MULTIPLAYER) {
948                 char buf[256];
949                 sprintf(Scoring_debug_text, "%s killed.\nDamage by ship:\n\n", Ship_info[dead_ship->ship_info_index].name);
950
951                 // show damage done by player
952                 for (int i=0; i<MAX_DAMAGE_SLOTS; i++) {
953                         int net_player_num = multi_find_player_by_signature(dead_ship->damage_ship_id[i]);
954                         if (net_player_num != -1) {
955                                 plr = Net_players[net_player_num].player;
956                                 sprintf(buf, "%s: %f", plr->callsign, dead_ship->damage_ship[i]);
957
958                                 if (dead_ship->damage_ship_id[i] == killer_sig ) {
959                                         strcat(buf, "  KILLER\n");
960                                 } else {
961                                         strcat(buf, "\n");
962                                 }
963
964                                 strcat(Scoring_debug_text, buf);        
965                         }
966
967                 }
968         }
969 #endif
970 */
971 }
972
973 // kill_id is the object signature of the guy who got the credit for the kill (may be -1, if no one got it)
974 // this is to insure that you don't also get an assist if you get the kill.
975 void scoring_eval_assists(ship *sp,int killer_sig)
976 {
977         int idx;
978         player *plr;
979
980         // multiplayer clients bail here
981         if(MULTIPLAYER_CLIENT){
982                 return;
983         }
984                 
985         // evaluate each damage slot to see if it did enough to give the assis
986         for(idx=0;idx<MAX_DAMAGE_SLOTS;idx++){
987                 // if this slot did enough damage to get an assist
988                 if((sp->damage_ship[idx]/sp->total_damage_received) >= ASSIST_PERCENTAGE){
989                         // get the player which did this damage (if any)
990                         plr = NULL;
991                         
992                         // multiplayer
993                         if(Game_mode & GM_MULTIPLAYER){
994                                 int net_player_num = multi_find_player_by_signature(sp->damage_ship_id[idx]);
995                                 if(net_player_num != -1){
996                                         plr = Net_players[net_player_num].player;
997                                 }
998                         }
999                         // single player
1000                         else {
1001                                 if(Objects[Player->objnum].signature == sp->damage_ship_id[idx]){
1002                                         plr = Player;
1003                                 }
1004                         }
1005
1006                         // if we found a player, give him the assist if it wasn't on his own team
1007                         if((plr != NULL) && (sp->team != Ships[Objects[plr->objnum].instance].team) && (killer_sig != Objects[plr->objnum].signature)){
1008                                 plr->stats.m_assists++;
1009
1010                                 nprintf(("Network","-==============GAVE PLAYER %s AN ASSIST=====================-\n",plr->callsign));
1011                                 break;
1012                         }
1013                 }
1014         }
1015 }
1016
1017 // eval a hit on an object (for primary and secondary hit purposes)
1018 void scoring_eval_hit(object *hit_obj, object *other_obj,int from_blast)
1019 {       
1020         // multiplayer clients bail here
1021         if(MULTIPLAYER_CLIENT){
1022                 return;
1023         }
1024
1025         // only evaluate hits on ships and asteroids
1026         if((hit_obj->type != OBJ_SHIP) && (hit_obj->type != OBJ_ASTEROID)){
1027                 return;
1028         }
1029
1030         // if the other_obj == NULL, we can't evaluate where it came from, so bail here
1031         if(other_obj == NULL){
1032                 return;
1033         }
1034
1035         // other bogus situtations
1036         if(other_obj->instance < 0){
1037                 return;
1038         }
1039         
1040         if((other_obj->type == OBJ_WEAPON) && !(Weapons[other_obj->instance].weapon_flags & WF_ALREADY_APPLIED_STATS)){         
1041                 // bogus weapon
1042                 if(other_obj->instance >= MAX_WEAPONS){
1043                         return;
1044                 }
1045
1046                 // bogus parent
1047                 if(other_obj->parent < 0){
1048                         return;
1049                 }
1050                 if(other_obj->parent >= MAX_SHIPS){
1051                         return;
1052                 }
1053                 if(Objects[other_obj->parent].type != OBJ_SHIP){
1054                         return;
1055                 }
1056                 if((Objects[other_obj->parent].instance < 0) || (Objects[other_obj->parent].instance >= MAX_SHIPS)){
1057                         return;
1058                 }               
1059
1060                 int is_bonehead = 0;
1061                 int sub_type = Weapon_info[Weapons[other_obj->instance].weapon_info_index].subtype;
1062
1063                 // determine if this was a bonehead hit or not
1064                 if(hit_obj->type == OBJ_SHIP){
1065                    is_bonehead = Ships[hit_obj->instance].team==Ships[Objects[other_obj->parent].instance].team ? 1 : 0;
1066                 }
1067                 // can't have a bonehead hit on an asteroid
1068                 else {
1069                         is_bonehead = 0;
1070                 }
1071
1072                 // set the flag indicating that we've already applied a "stats" hit for this weapon
1073                 // Weapons[other_obj->instance].weapon_flags |= WF_ALREADY_APPLIED_STATS;
1074
1075                 // in multiplayer -- only the server records the stats
1076                 if( Game_mode & GM_MULTIPLAYER ) {
1077                         if ( Net_player->flags & NETINFO_FLAG_AM_MASTER ) {
1078                                 int player_num;
1079
1080                                 // get the player num of the parent object.  A player_num of -1 means that the
1081                                 // parent of this object was not a player
1082                                 player_num = multi_find_player_by_object( &Objects[other_obj->parent] );
1083                                 if ( player_num != -1 ) {
1084                                         switch(sub_type) {
1085                                         case WP_LASER : 
1086                                                 if(is_bonehead){
1087                                                         Net_players[player_num].player->stats.mp_bonehead_hits++;
1088                                                 } else {
1089                                                         Net_players[player_num].player->stats.mp_shots_hit++; 
1090                                                 }
1091
1092                                                 // Assert( Net_players[player_num].player->stats.mp_shots_hit <= Net_players[player_num].player->stats.mp_shots_fired );
1093                                                 break;
1094                                         case WP_MISSILE :
1095                                                 // friendly hit, once it hits a friendly, its done
1096                                                 if(is_bonehead){                                        
1097                                                         if(!from_blast){
1098                                                                 Net_players[player_num].player->stats.ms_bonehead_hits++;
1099                                                         }                                       
1100                                                 }
1101                                                 // hostile hit
1102                                                 else {
1103                                                         // if its a bomb, count every bit of damage it does
1104                                                         if(Weapons[other_obj->instance].weapon_flags & WIF_BOMB){
1105                                                                 // once we get impact damage, stop keeping track of it
1106                                                                 Net_players[player_num].player->stats.ms_shots_hit++;
1107                                                         }
1108                                                         // if its not a bomb, only count impact damage
1109                                                         else {
1110                                                                 if(!from_blast){
1111                                                                         Net_players[player_num].player->stats.ms_shots_hit++;
1112                                                                 }       
1113                                                         }                               
1114                                                 }
1115                                         default : 
1116                                                 break;
1117                                         }
1118                                 }
1119                         }
1120                 } else if(Player_obj == &(Objects[other_obj->parent])){
1121                         switch(sub_type){
1122                         case WP_LASER : 
1123                                 if(is_bonehead){
1124                                         Player->stats.mp_bonehead_hits++;
1125                                 } else {
1126                                         Player->stats.mp_shots_hit++; 
1127                                 }
1128                                 break;
1129                         case WP_MISSILE :
1130                                 // friendly hit, once it hits a friendly, its done
1131                                 if(is_bonehead){                                        
1132                                         if(!from_blast){
1133                                                 Player->stats.ms_bonehead_hits++;
1134                                         }                                       
1135                                 }
1136                                 // hostile hit
1137                                 else {
1138                                         // if its a bomb, count every bit of damage it does
1139                                         if(Weapons[other_obj->instance].weapon_flags & WIF_BOMB){
1140                                                 // once we get impact damage, stop keeping track of it
1141                                                 Player->stats.ms_shots_hit++;
1142                                         }
1143                                         // if its not a bomb, only count impact damage
1144                                         else {
1145                                                 if(!from_blast){
1146                                                         Player->stats.ms_shots_hit++;
1147                                                 }
1148                                         }
1149                                 }                               
1150                                 break;
1151                         default : 
1152                                 break;
1153                         }
1154                 }
1155         }
1156 }
1157
1158 // get a scaling factor for adding/subtracting from mission score
1159 float scoring_get_scale_factor()
1160 {
1161         // multiplayer dogfight. don't scale anything
1162         if((Game_mode & GM_MULTIPLAYER) && (Netgame.type_flags & NG_TYPE_DOGFIGHT)){
1163                 return 1.0f;
1164         }
1165
1166         // check for bogus Skill_level values
1167         Assert((Game_skill_level >= 0) && (Game_skill_level < NUM_SKILL_LEVELS));
1168         if((Game_skill_level < 0) || (Game_skill_level > NUM_SKILL_LEVELS-1)){
1169                 return Scoring_scale_factors[0];
1170         }
1171
1172         // return the correct scale value
1173         return Scoring_scale_factors[Game_skill_level];
1174 }
1175
1176
1177 // ----------------------------------------------------------------------------------------
1178 // DCF functions
1179 //
1180
1181 // bash the passed player to the specified rank
1182 void scoring_bash_rank(player *pl,int rank)
1183 {       
1184         // if this is an invalid rank, do nothing
1185         if((rank < RANK_ENSIGN) || (rank > RANK_ADMIRAL)){
1186                 nprintf(("General","Could not bash player rank - invalid value!!!\n"));
1187                 return;
1188         }
1189
1190         // set the player's score and rank
1191         pl->stats.score = Ranks[rank].points + 1;
1192         pl->stats.rank = rank;
1193 }
1194
1195 DCF(rank, "changes scoring vars")
1196 {
1197         if(Dc_command){         
1198                 dc_get_arg(ARG_INT);            
1199                 
1200                 // parse the argument and change things around accordingly              
1201                 if((Dc_arg_type & ARG_INT) && (Player != NULL)){                                                        
1202                         scoring_bash_rank(Player,Dc_arg_int);
1203                 }               
1204         }
1205         dc_printf("Usage\n0 : Ensign\n1 : Lieutenant Junior Grade\n");
1206         dc_printf("2 : Lietenant\n3 : Lieutenant Commander\n");
1207         dc_printf("4 : Commander\n5 : Captain\n6 : Commodore\n");
1208         dc_printf("7 : Rear Admiral\n8 : Vice Admiral\n9 : Admiral");
1209 }