]> icculus.org git repositories - taylor/freespace2.git/blob - src/stats/stats.cpp
Initial revision
[taylor/freespace2.git] / src / stats / stats.cpp
1 /*
2  * $Logfile: /Freespace2/code/Stats/Stats.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * module for running the stats screen
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 5     10/15/99 3:56p Jefff
15  * lifetime score to alltime stats printout (multi debrief screen)
16  * 
17  * 4     10/15/99 3:01p Jefff
18  * mission score shows up in multiplayer debrief
19  * 
20  * 3     3/19/99 9:51a Dave
21  * Checkin to repair massive source safe crash. Also added support for
22  * pof-style nebulae, and some new weapons code.
23  * 
24  * 3     3/15/99 10:30a Neilk
25  * Add hires support
26  * 
27  * 2     10/07/98 10:54a Dave
28  * Initial checkin.
29  * 
30  * 1     10/07/98 10:51a Dave
31  * 
32  * 38    6/09/98 10:31a Hoffoss
33  * Created index numbers for all xstr() references.  Any new xstr() stuff
34  * added from here on out should be added to the end if the list.  The
35  * current list count can be found in FreeSpace.cpp (search for
36  * XSTR_SIZE).
37  * 
38  * 37    5/18/98 5:25p Chad
39  * Removed old-ass stats display code.
40  * 
41  * 36    5/18/98 12:52a Allender
42  * make initialization of multiplayer stats initialize all players, not
43  * just connected players (fixed ingame join stats problems)
44  * 
45  * 35    5/15/98 4:12p Allender
46  * removed redbook code.  Put back in ingame join timer.  Major fixups for
47  * stats in multiplayer.  Pass correct score, medals, etc when leaving
48  * game.  Be sure clients display medals, badges, etc.
49  * 
50  * 34    5/07/98 12:57a Dave
51  * Fixed incorrect calls to free() from stats code. Put in new artwork for
52  * debrief and host options screens. Another modification to scoring
53  * system for secondary weapons.
54  * 
55  * 33    4/21/98 11:56p Dave
56  * Put in player deaths statskeeping. Use arrow keys in the ingame join
57  * ship select screen. Don't quit the game if in the debriefing and server
58  * leaves.
59  * 
60  * 32    4/09/98 10:31a Dave
61  * Don't display player deaths statistic under single player (doesn't make
62  * sense).
63  * 
64  * 31    3/31/98 5:18p John
65  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
66  * bunch of debug stuff out of player file.  Made model code be able to
67  * unload models and malloc out only however many models are needed.
68  *  
69  * 
70  * 30    3/27/98 3:59p Hoffoss
71  * Due to everyone using different assumptions about what a length define
72  * means, changed code to account for safest assumption.
73  * 
74  * 29    3/17/98 12:29a Dave
75  * Put in hud support for rtvoice. Several ui interface changes.
76  * 
77  * 28    3/15/98 4:17p Dave
78  * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
79  * of network orientation matrices.
80  * 
81  * 27    2/22/98 2:48p John
82  * More String Externalization Classification
83  * 
84  * 26    2/12/98 5:00p Lawrance
85  * underline kills in the stats section
86  * 
87  * 25    1/06/98 5:08p Dave
88  * Put in stats display change to show alltime+mission stats during
89  * debriefing. Fixed a object update sequencing bug.
90  * 
91  * 24    12/10/97 4:45p Dave
92  * Added in more detailed support for multiplayer packet lag/loss. Fixed
93  * some multiplayer stuff. Added some controls to the standalone.
94  * 
95  * 23    11/06/97 9:53a Dave
96  * 
97  * 22    10/25/97 7:23p Dave
98  * Moved back to single set stats storing. Put in better respawning
99  * locations system.
100  * 
101  * 21    10/24/97 6:19p Dave
102  * More standalone testing/fixing. Added reliable endgame sequencing.
103  * Added reliable ingame joining. Added reliable stats transfer (endgame).
104  * Added support for dropping players in debriefing. Removed a lot of old
105  * unused code.
106  * 
107  * 20    10/23/97 7:44p Hoffoss
108  * Added 2 defines to replace hard-coded values in the code.
109  * 
110  * 19    10/21/97 5:21p Dave
111  * Fixed pregame mission load/file verify debacle. Added single vs.
112  * multiplayer stats system.
113  * 
114  * 18    10/14/97 5:37p Dave
115  * Fixed a memory allocation bug
116  * 
117  * 17    10/08/97 4:47p Dave
118  * Fixed bugs turned up in testing. Added some brief comments.
119  * 
120  * 16    9/30/97 8:48p Lawrance
121  * generalize some functions for displaying stats info
122  * 
123  * 15    9/24/97 5:03p Dave
124  * Beginning changes on how kills/assists are evaluated in both single and
125  * multiplayer
126  * 
127  * 14    9/18/97 10:14p Dave
128  * Updated the scoring system struct. Added rank.tbl. Modified how medal
129  * names are displayed.
130  * 
131  * 13    9/17/97 5:12p John
132  * Restructured collision routines.  Probably broke a lot of stuff.
133  * 
134  * 12    9/09/97 4:31p Dave
135  * Put in multiplayer post-game stats stuff.
136  * 
137  * 11    8/26/97 4:59p Dave
138  * Changed display from bonehead hits to friendly hits.
139  * 
140  * 10    8/23/97 11:31a Dave
141  * Changed the definition of bonehead hit %
142  * 
143  * 9     8/20/97 12:31p Dave
144  * Added a death count for auto ressurected players.
145  * Made it easier to reorganize the stats page
146  * 
147  * 8     8/19/97 5:14p Dave
148  * Added a sucker check. :)
149  * 
150  * 7     8/15/97 2:20p Allender
151  * fix stats code to properly get connected players
152  * 
153  * 6     8/15/97 9:28a Dave
154  * Fixed minor bug in multiplayer stats init.
155  * 
156  * 5     8/14/97 5:20p Dave
157  * Made initialization/clearing of players stats more thorough.
158  * 
159  * 4     7/25/97 5:23p Dave
160  * Made text displayed in antialiased font.
161  * 
162  * 3     7/25/97 4:33p Dave
163  * Spiffed up statistics screen considerably. Currently reports all
164  * statistics in the game (except mission/campaign related scores)
165  * 
166  * 2     7/24/97 2:31p Dave
167  * Added basic screen, no interaction yet.
168  * 
169  * 1     7/24/97 1:50p Dave
170  * 
171  * $NoKeywords: $
172  */
173
174 #include <io.h>
175 #include <winsock.h>
176 #include "freespace.h"
177 #include "gamesequence.h"
178 #include "key.h"
179 #include "2d.h"
180 #include "ui.h"
181 #include "timer.h"
182 #include "player.h"
183 #include "stats.h"
184 #include "multi.h"
185 #include "hud.h"
186 #include "font.h"
187
188 #define MISSION_STATS_START_Y 80
189 #define ALLTIME_STATS_START_Y 270
190 #define MULTIPLAYER_LIST_START 20
191
192 static int Mission_stats_start_y[GR_NUM_RESOLUTIONS] = {
193         80,     // GR_640
194         80              // GR_1024
195 };
196
197 static int Alltime_stats_start_y[GR_NUM_RESOLUTIONS] = {
198         270,    // GR_640
199         270     // GR_1024
200 };
201
202 static int Multiplayer_list_start[GR_NUM_RESOLUTIONS] = {
203         20,     // GR_640
204         20              // GR_1024
205 };
206
207 // static UI_WINDOW Player_stats_window;
208
209 player *Active_player;
210
211 void show_stats_init()
212 {
213         // Player_stats_window.create( 0, 0, gr_screen.max_w, gr_screen.max_h, 0 );
214
215         if (Game_mode & GM_MULTIPLAYER) {                               
216                 set_player_stats(MY_NET_PLAYER_NUM);
217         } else {
218                 Active_player = Player;         
219         }       
220 }
221
222 // write out the label for each stat
223 void show_stats_label(int stage, int sx, int sy, int dy)
224 {
225         switch ( stage ) {
226                 case MISSION_STATS:
227                         gr_printf(sx,sy,XSTR( "Mission Stats", 114));
228                         sy += 2*dy;
229                         gr_printf(sx,sy,XSTR( "Total kills", 115));
230                         sy += 2*dy;
231                         gr_printf(sx,sy,XSTR( "Primary weapon shots", 116));
232                         sy += dy;
233                         gr_printf(sx,sy,XSTR( "Primary weapon hits", 117));
234                         sy += dy;
235                         gr_printf(sx,sy,XSTR( "Primary friendly hits", 118));
236                         sy += dy;
237                         gr_printf(sx,sy,XSTR( "Primary hit %%", 119));
238                         sy += dy;
239                         gr_printf(sx,sy,XSTR( "Primary friendly hit %%", 120));
240                         sy += 2*dy;
241
242                         gr_printf(sx,sy,XSTR( "Secondary weapon shots", 121));
243                         sy += dy;
244                         gr_printf(sx,sy,XSTR( "Secondary weapon hits", 122));
245                         sy += dy;
246                         gr_printf(sx,sy,XSTR( "Secondary friendly hits", 123));
247                         sy += dy;
248                         gr_printf(sx,sy,XSTR( "Secondary hit %%", 124));
249                         sy += dy;
250                         gr_printf(sx,sy,XSTR( "Secondary friendly hit %%", 125));
251                         sy += 2*dy;
252
253                         gr_printf(sx,sy,XSTR( "Assists", 126));
254                         sy += 2*dy;
255
256                         if(Game_mode & GM_MULTIPLAYER){
257                                 gr_printf(sx,sy,XSTR( "Player Deaths", 127));
258                                 sy += 2*dy;
259
260                                 gr_printf(sx,sy,XSTR( "Mission score", 1526));
261                         }
262
263
264                         break;
265
266                 case ALL_TIME_STATS:
267                         gr_printf(sx,sy,XSTR( "All Time Stats", 128));
268                         sy += 2*dy;                     
269                         gr_printf(sx,sy,XSTR( "Total kills", 115));
270                         sy += 2*dy;
271                         gr_printf(sx,sy,XSTR( "Primary weapon shots", 116));
272                         sy += dy;
273                         gr_printf(sx,sy,XSTR( "Primary weapon hits", 117));
274                         sy += dy;
275                         gr_printf(sx,sy,XSTR( "Primary friendly hits", 118));
276                         sy += dy;
277                         gr_printf(sx,sy,XSTR( "Primary hit %%", 119));
278                         sy += dy;
279                         gr_printf(sx,sy,XSTR( "Primary friendly hit %%", 120));
280                         sy += 2*dy;
281
282                         gr_printf(sx,sy,XSTR( "Secondary weapon shots", 121));
283                         sy += dy;
284                         gr_printf(sx,sy,XSTR( "Secondary weapon hits", 122));
285                         sy += dy;
286                         gr_printf(sx,sy,XSTR( "Secondary friendly hits", 123));
287                         sy += dy;
288                         gr_printf(sx,sy,XSTR( "Secondary hit %%", 124));
289                         sy += dy;
290                         gr_printf(sx,sy,XSTR( "Secondary friendly hit %%", 125));
291                         sy += 2*dy;                     
292
293                         gr_printf(sx,sy,XSTR( "Assists", 126));
294                         sy += 2*dy;
295
296                         if(Game_mode & GM_MULTIPLAYER){
297                                 gr_printf(sx,sy,XSTR( "Score", 1527));
298                         }
299                         break;
300                 } // end switch
301 }
302
303 void stats_underline_text(int sx, int sy, char *text)
304 {
305         int w,h,fh;
306
307         gr_get_string_size(&w,&h,text);
308         fh=gr_get_font_height();
309         gr_line(sx-1, sy+fh, sx+w+1, sy+fh);
310 }
311
312 void show_stats_numbers(int stage, int sx, int sy, int dy,int add_mission)
313 {
314    char         text[30];
315         float           pct;
316
317         sy += 2*dy;
318         switch ( stage ) {
319                 case MISSION_STATS:
320          // mission kills stats
321                         sprintf(text,"%d",Active_player->stats.m_kill_count_ok);
322                         gr_printf(sx,sy,text);
323                         // stats_underline_text(sx,sy,text);
324                         sy += 2*dy;
325          // mission primary weapon stats
326                         sprintf(text,"%d",Active_player->stats.mp_shots_fired);
327                         gr_printf(sx,sy,text);
328                         sy += dy;
329                         sprintf(text,"%d",Active_player->stats.mp_shots_hit);
330                         gr_printf(sx,sy,text);
331                         sy += dy;
332                         sprintf(text,"%d",Active_player->stats.mp_bonehead_hits);
333                         gr_printf(sx,sy,text);
334                         sy += dy;
335                         if(Active_player->stats.mp_shots_fired>0)
336                                 pct=(float)100.0*((float)Active_player->stats.mp_shots_hit/(float)Active_player->stats.mp_shots_fired);
337                         else pct=(float)0.0;
338                         sprintf(text,"%d",(int)pct); strcat(text," %%");
339                         gr_printf(sx,sy,text);
340                         sy += dy;
341                         if(Active_player->stats.mp_bonehead_hits>0)
342                                 pct=(float)100.0*((float)Active_player->stats.mp_bonehead_hits/(float)Active_player->stats.mp_shots_fired);
343                         else pct=(float)0.0;
344                         sprintf(text,"%d",(int)pct); strcat(text," %%");
345                         gr_printf(sx,sy,text);
346                         sy += 2*dy;
347
348                         // mission secondary weapon stats
349                         sprintf(text,"%d",Active_player->stats.ms_shots_fired);
350                         gr_printf(sx,sy,text);
351                         sy += dy;
352                         sprintf(text,"%d",Active_player->stats.ms_shots_hit);
353                         gr_printf(sx,sy,text);
354                         sy += dy;
355                         sprintf(text,"%d",Active_player->stats.ms_bonehead_hits);
356                         gr_printf(sx,sy,text);
357                         sy += dy;
358                         if(Active_player->stats.ms_shots_fired>0)
359                                 pct=(float)100.0*((float)Active_player->stats.ms_shots_hit/(float)Active_player->stats.ms_shots_fired);
360                         else pct=(float)0.0;
361                         sprintf(text,"%d",(int)pct); strcat(text," %%");
362                         gr_printf(sx,sy,text);
363                         sy += dy;
364                         if(Active_player->stats.ms_bonehead_hits>0)
365                                 pct=(float)100.0*((float)Active_player->stats.ms_bonehead_hits/(float)Active_player->stats.ms_shots_fired);
366                         else pct=(float)0.0;
367                         sprintf(text,"%d",(int)pct); strcat(text," %%");
368                         gr_printf(sx,sy,text);
369                         sy += 2*dy;
370
371                         // mission assists and player rescues (respawns)
372                         sprintf(text,"%d",(int)Active_player->stats.m_assists);
373                         gr_printf(sx,sy,text);
374                         sy += 2*dy;
375
376                         if(Game_mode & GM_MULTIPLAYER){
377                                 sprintf(text,"%d",(int)Active_player->stats.m_player_deaths);
378                                 gr_printf(sx,sy,text);
379                                 sy += 2*dy;
380
381                                 // mission score
382                                 gr_printf(sx, sy, "%d", (int)Active_player->stats.m_score);
383                         }
384
385
386                         break;
387
388                 case ALL_TIME_STATS:
389                          scoring_struct add;
390                         
391                         // if we are passed mission_add (the stats for the current mission), copy it to "add", otherwise,
392                         // zero it out
393                         memset(&add,0,sizeof(scoring_struct));                          
394                         if(add_mission){
395                                 add.kill_count_ok = Active_player->stats.m_kill_count_ok;
396                                 add.p_shots_fired  = Active_player->stats.mp_shots_fired;
397                                 add.p_shots_hit = Active_player->stats.mp_shots_hit;
398                                 add.p_bonehead_hits = Active_player->stats.mp_bonehead_hits;                            
399                                 
400                                 add.s_shots_fired = Active_player->stats.ms_shots_fired;
401                                 add.s_shots_hit = Active_player->stats.ms_shots_hit;
402                                 add.s_bonehead_hits = Active_player->stats.ms_bonehead_hits;                            
403                         }                       
404
405          // mission kills stats
406                         sprintf(text,"%d",Active_player->stats.kill_count_ok + add.kill_count_ok);
407                         hud_num_make_mono(text);
408                         gr_printf(sx,sy,text);
409                         // stats_underline_text(sx,sy,text);
410                         sy += 2*dy;
411          // alltime primary weapon stats
412                         sprintf(text,"%d",Active_player->stats.p_shots_fired + add.p_shots_fired);
413                         gr_printf(sx,sy,text);
414                         sy += dy;
415                         sprintf(text,"%d",Active_player->stats.p_shots_hit + add.p_shots_hit);
416                         gr_printf(sx,sy,text);
417                         sy += dy;
418                         sprintf(text,"%d",Active_player->stats.p_bonehead_hits + add.p_bonehead_hits);
419                         gr_printf(sx,sy,text);
420                         sy += dy;
421                         if((Active_player->stats.p_shots_fired + add.p_shots_fired)>0)
422                                 pct=(float)100.0*((float)(Active_player->stats.p_shots_hit+add.p_shots_hit)/(float)(Active_player->stats.p_shots_fired + add.p_shots_fired));
423                         else pct=(float)0.0;
424                         sprintf(text,"%d",(int)pct); strcat(text," %%");
425                         gr_printf(sx,sy,text);
426                         sy += dy;
427                         if((Active_player->stats.p_bonehead_hits + add.p_bonehead_hits)>0)
428                                 pct=(float)100.0*((float)(Active_player->stats.p_bonehead_hits+add.p_bonehead_hits)/(float)(Active_player->stats.p_shots_fired + add.p_shots_fired));
429                         else pct=(float)0.0;
430                         sprintf(text,"%d",(int)pct); strcat(text," %%");
431                         gr_printf(sx,sy,text);
432                         sy += 2*dy;
433
434                         // alltime secondary weapon stats
435                         sprintf(text,"%d",Active_player->stats.s_shots_fired + add.s_shots_fired);
436                         gr_printf(sx,sy,text);
437                         sy += dy;
438                         sprintf(text,"%d",Active_player->stats.s_shots_hit + add.s_shots_hit);
439                         gr_printf(sx,sy,text);
440                         sy += dy;
441                         sprintf(text,"%d",Active_player->stats.s_bonehead_hits + add.s_bonehead_hits);
442                         gr_printf(sx,sy,text);
443                         sy += dy;
444                         if((Active_player->stats.s_shots_fired+add.s_shots_fired)>0)
445                                 pct=(float)100.0*((float)(Active_player->stats.s_shots_hit + add.s_shots_hit)/(float)(Active_player->stats.s_shots_fired + add.s_shots_fired));
446                         else pct=(float)0.0;
447                         sprintf(text,"%d",(int)pct); strcat(text," %%");
448                         gr_printf(sx,sy,text);
449                         sy += dy;
450                         if((Active_player->stats.s_bonehead_hits + add.s_bonehead_hits)>0)
451                                 pct=(float)100.0*((float)(Active_player->stats.s_bonehead_hits+add.s_bonehead_hits)/(float)(Active_player->stats.s_shots_fired+add.s_shots_fired));
452                         else pct=(float)0.0;
453                         sprintf(text,"%d",(int)pct); strcat(text," %%");
454                         gr_printf(sx,sy,text);
455                         sy += 2*dy;
456
457                         // alltime assists
458                         sprintf(text,"%d",(int)Active_player->stats.assists + add.assists);
459                         gr_printf(sx,sy,text);
460                         sy += 2*dy;
461
462                         if (Game_mode & GM_MULTIPLAYER) {
463                                 gr_printf(sx, sy, "%d", (int)Active_player->stats.score);
464                         }
465                         break;
466         } // end switch
467 }
468
469 int find_netplayer_n(int n)
470 {
471         int idx;
472         int target;
473    target = n;
474         n=0;
475    for(idx=0;idx<MAX_PLAYERS;idx++){
476                 if(MULTI_CONNECTED(Net_players[idx])){
477                         n++;
478                         if(n == target)
479                                 return idx;
480                 }
481         }
482         return -1;
483 }
484
485
486
487 void show_stats_close()
488 {       
489 }
490  
491 // initialize the statistics portion of the player structure for multiplayer.  Only the host of
492 // a netgame needs to be doing this (and if fact, only he *should* be doing this)
493 void init_multiplayer_stats( )
494 {
495         scoring_struct *ptr;
496
497         for (int idx=0; idx < MAX_PLAYERS; idx++) {
498                 ptr = &Players[idx].stats;
499                 scoring_level_init( ptr );
500         }
501 }
502
503 void set_player_stats(int pid)
504 {
505    Active_player = Net_players[pid].player;
506 }
507