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