]> icculus.org git repositories - btb/d2x.git/blob - main/scores.c
remove rcs tags
[btb/d2x.git] / main / scores.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Inferno High Scores and Statistics System
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "scores.h"
30 #include "error.h"
31 #include "pstypes.h"
32 #include "gr.h"
33 #include "mono.h"
34 #include "key.h"
35 #include "palette.h"
36 #include "game.h"
37 #include "gamefont.h"
38 #include "u_mem.h"
39 #include "songs.h"
40 #include "newmenu.h"
41 #include "menu.h"
42 #include "player.h"
43 #include "screens.h"
44 #include "gamefont.h"
45 #include "mouse.h"
46 #include "joy.h"
47 #include "timer.h"
48 #include "text.h"
49 #include "strutil.h"
50
51 #define VERSION_NUMBER          1
52 #define SCORES_FILENAME         "descent.hi"
53 #define COOL_MESSAGE_LEN        50
54 #define MAX_HIGH_SCORES         10
55
56 typedef struct stats_info {
57         char    name[CALLSIGN_LEN+1];
58         int             score;
59         sbyte   starting_level;
60         sbyte   ending_level;
61         sbyte   diff_level;
62         short   kill_ratio;             // 0-100
63         short   hostage_ratio;  // 
64         int             seconds;                // How long it took in seconds...
65 } stats_info;
66
67 typedef struct all_scores {
68         char                    signature[3];                   // DHS
69         sbyte           version;                                // version
70         char                    cool_saying[COOL_MESSAGE_LEN];
71         stats_info      stats[MAX_HIGH_SCORES];
72 } all_scores;
73
74 static all_scores Scores;
75
76 stats_info Last_game;
77
78 char scores_filename[128];
79
80 #define XX  (7)
81 #define YY  (-3)
82
83 #define LHX(x)          ((x)*(MenuHires?2:1))
84 #define LHY(y)          ((y)*(MenuHires?2.4:1))
85
86
87 char * get_scores_filename()
88 {
89 #ifndef RELEASE
90         // Only use the MINER variable for internal developement
91         char *p;
92         p=getenv( "MINER" );
93         if (p)  {
94                 sprintf( scores_filename, "%s\\game\\%s", p, SCORES_FILENAME );
95                 Assert(strlen(scores_filename) < 128);
96                 return scores_filename;
97         }
98 #endif
99         sprintf( scores_filename, "%s", SCORES_FILENAME );
100         return scores_filename;
101 }
102
103 #ifndef D2_OEM
104 #define COOL_SAYING TXT_REGISTER_DESCENT
105 #else
106 #define COOL_SAYING "Get all 30 levels of D2 from 1-800-INTERPLAY"
107 #endif
108
109 void scores_read()
110 {
111         PHYSFS_file *fp;
112         int fsize;
113
114         // clear score array...
115         memset( &Scores, 0, sizeof(all_scores) );
116
117         fp = PHYSFS_openRead(get_scores_filename());
118         if (fp==NULL) {
119                 int i;
120
121                 // No error message needed, code will work without a scores file
122                 sprintf( Scores.cool_saying, "%s", COOL_SAYING );
123                 sprintf( Scores.stats[0].name, "Parallax" );
124                 sprintf( Scores.stats[1].name, "Matt" );
125                 sprintf( Scores.stats[2].name, "Mike" );
126                 sprintf( Scores.stats[3].name, "Adam" );
127                 sprintf( Scores.stats[4].name, "Mark" );
128                 sprintf( Scores.stats[5].name, "Jasen" );
129                 sprintf( Scores.stats[6].name, "Samir" );
130                 sprintf( Scores.stats[7].name, "Doug" );
131                 sprintf( Scores.stats[8].name, "Dan" );
132                 sprintf( Scores.stats[9].name, "Jason" );
133
134                 for (i=0; i<10; i++)
135                         Scores.stats[i].score = (10-i)*1000;
136                 return;
137         }
138
139         fsize = PHYSFS_fileLength(fp);
140
141         if ( fsize != sizeof(all_scores) )      {
142                 PHYSFS_close(fp);
143                 return;
144         }
145         // Read 'em in...
146         PHYSFS_read(fp, &Scores, sizeof(all_scores), 1);
147         PHYSFS_close(fp);
148
149         if ( (Scores.version!=VERSION_NUMBER)||(Scores.signature[0]!='D')||(Scores.signature[1]!='H')||(Scores.signature[2]!='S') )     {
150                 memset( &Scores, 0, sizeof(all_scores) );
151                 return;
152         }
153 }
154
155 void scores_write()
156 {
157         PHYSFS_file *fp;
158
159         fp = PHYSFS_openWrite(get_scores_filename());
160         if (fp==NULL) {
161                 nm_messagebox( TXT_WARNING, 1, TXT_OK, "%s\n'%s'", TXT_UNABLE_TO_OPEN, get_scores_filename()  );
162                 return;
163         }
164
165         Scores.signature[0]='D';
166         Scores.signature[1]='H';
167         Scores.signature[2]='S';
168         Scores.version = VERSION_NUMBER;
169         PHYSFS_write(fp, &Scores,sizeof(all_scores), 1);
170         PHYSFS_close(fp);
171 }
172
173 void int_to_string( int number, char *dest )
174 {
175         int i,l,c;
176         char buffer[20],*p;
177
178         sprintf( buffer, "%d", number );
179
180         l = strlen(buffer);
181         if (l<=3) {
182                 // Don't bother with less than 3 digits
183                 sprintf( dest, "%d", number );
184                 return;
185         }
186
187         c = 0;
188         p=dest;
189         for (i=l-1; i>=0; i-- ) {
190                 if (c==3) {
191                         *p++=',';
192                         c = 0;
193                 }
194                 c++;
195                 *p++ = buffer[i];
196         }
197         *p++ = '\0';
198         strrev(dest);
199 }
200
201 void scores_fill_struct(stats_info * stats)
202 {
203                 strcpy( stats->name, Players[Player_num].callsign );
204                 stats->score = Players[Player_num].score;
205                 stats->ending_level = Players[Player_num].level;
206                 if (Players[Player_num].num_robots_total > 0 )  
207                         stats->kill_ratio = (Players[Player_num].num_kills_total*100)/Players[Player_num].num_robots_total;
208                 else
209                         stats->kill_ratio = 0;
210
211                 if (Players[Player_num].hostages_total > 0 )    
212                         stats->hostage_ratio = (Players[Player_num].hostages_rescued_total*100)/Players[Player_num].hostages_total;
213                 else
214                         stats->hostage_ratio = 0;
215
216                 stats->seconds = f2i(Players[Player_num].time_total)+(Players[Player_num].hours_total*3600);
217
218                 stats->diff_level = Difficulty_level;
219                 stats->starting_level = Players[Player_num].starting_level;
220 }
221
222 //char * score_placement[10] = { TXT_1ST, TXT_2ND, TXT_3RD, TXT_4TH, TXT_5TH, TXT_6TH, TXT_7TH, TXT_8TH, TXT_9TH, TXT_10TH };
223
224 void scores_maybe_add_player(int abort_flag)
225 {
226         char text1[COOL_MESSAGE_LEN+10];
227         newmenu_item m[10];
228         int i,position;
229
230         #ifdef APPLE_DEMO               // no high scores in apple oem version
231         return;
232         #endif
233
234         if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
235                 return;
236   
237         scores_read();
238         
239         position = MAX_HIGH_SCORES;
240         for (i=0; i<MAX_HIGH_SCORES; i++ )      {
241                 if ( Players[Player_num].score > Scores.stats[i].score )        {
242                         position = i;
243                         break;
244                 }
245         }
246         
247         if ( position == MAX_HIGH_SCORES ) {
248                 if (abort_flag)
249                         return;
250                 scores_fill_struct( &Last_game );
251         } else {
252 //--            if ( Difficulty_level < 1 )     {
253 //--                    nm_messagebox( "GRADUATION TIME!", 1, "Ok", "If you would had been\nplaying at a higher difficulty\nlevel, you would have placed\n#%d on the high score list.", position+1 );
254 //--                    return;
255 //--            }
256
257                 if ( position==0 )      {
258                         strcpy( text1,  "" );
259                         m[0].type = NM_TYPE_TEXT; m[0].text = TXT_COOL_SAYING;
260                         m[1].type = NM_TYPE_INPUT; m[1].text = text1; m[1].text_len = COOL_MESSAGE_LEN-5;
261                         newmenu_do( TXT_HIGH_SCORE, TXT_YOU_PLACED_1ST, 2, m, NULL );
262                         strncpy( Scores.cool_saying, text1, COOL_MESSAGE_LEN );
263                         if (strlen(Scores.cool_saying)<1)
264                                 sprintf( Scores.cool_saying, "No Comment" );
265                 } else {
266                         nm_messagebox( TXT_HIGH_SCORE, 1, TXT_OK, "%s %s!", TXT_YOU_PLACED, *(&TXT_1ST + position) );
267                 }
268         
269                 // move everyone down...
270                 for ( i=MAX_HIGH_SCORES-1; i>position; i-- )    {
271                         Scores.stats[i] = Scores.stats[i-1];
272                 }
273
274                 scores_fill_struct( &Scores.stats[position] );
275         
276                 scores_write();
277
278         }
279         scores_view(position);
280 }
281
282 void scores_rprintf(int x, int y, char * format, ... )
283 {
284         va_list args;
285         char buffer[128];
286         int w, h, aw;
287         char *p;
288
289         va_start(args, format );
290         vsprintf(buffer,format,args);
291         va_end(args);
292
293         //replace the digit '1' with special wider 1
294         for (p=buffer;*p;p++)
295                 if (*p=='1') *p=132;
296
297         gr_get_string_size(buffer, &w, &h, &aw );
298
299         gr_string( LHX(x)-w, LHY(y), buffer );
300 }
301
302
303 void scores_draw_item( int  i, stats_info * stats )
304 {
305         char buffer[20];
306
307                 int y;
308
309                 y = 7+70+i*9;
310
311                 if (i==0) y -= 8;
312
313                 if ( i==MAX_HIGH_SCORES )       {
314                         y += 8;
315                         //scores_rprintf( 17+33+XX, y+YY, "" );
316                 } else {
317                         scores_rprintf( 17+33+XX, y+YY, "%d.", i+1 );
318                 }
319
320                 if (strlen(stats->name)==0) {
321                         gr_printf( LHX(26+33+XX), LHY(y+YY), TXT_EMPTY );
322                         return;
323                 }
324                 gr_printf( LHX(26+33+XX), LHY(y+YY), "%s", stats->name );
325                 int_to_string(stats->score, buffer);
326                 scores_rprintf( 109+33+XX, y+YY, "%s", buffer );
327
328                 gr_printf( LHX(125+33+XX), LHY(y+YY), "%s", MENU_DIFFICULTY_TEXT(stats->diff_level) );
329
330                 if ( (stats->starting_level > 0 ) && (stats->ending_level > 0 ))
331                         scores_rprintf( 192+33+XX, y+YY, "%d-%d", stats->starting_level, stats->ending_level );
332                 else if ( (stats->starting_level < 0 ) && (stats->ending_level > 0 ))
333                         scores_rprintf( 192+33+XX, y+YY, "S%d-%d", -stats->starting_level, stats->ending_level );
334                 else if ( (stats->starting_level < 0 ) && (stats->ending_level < 0 ))
335                         scores_rprintf( 192+33+XX, y+YY, "S%d-S%d", -stats->starting_level, -stats->ending_level );
336                 else if ( (stats->starting_level > 0 ) && (stats->ending_level < 0 ))
337                         scores_rprintf( 192+33+XX, y+YY, "%d-S%d", stats->starting_level, -stats->ending_level );
338
339                 {
340                         int h, m, s;
341                         h = stats->seconds/3600;
342                         s = stats->seconds%3600;
343                         m = s / 60;
344                         s = s % 60;
345                         scores_rprintf( 311-42+XX, y+YY, "%d:%02d:%02d", h, m, s );
346                 }
347 }
348
349 void scores_view(int citem)
350 {
351         fix t1;
352         int i,done,looper;
353         int k;
354         sbyte fades[64] = { 1,1,1,2,2,3,4,4,5,6,8,9,10,12,13,15,16,17,19,20,22,23,24,26,27,28,28,29,30,30,31,31,31,31,31,30,30,29,28,28,27,26,24,23,22,20,19,17,16,15,13,12,10,9,8,6,5,4,4,3,2,2,1,1 };
355
356 ReshowScores:
357         scores_read();
358
359         set_screen_mode(SCREEN_MENU);
360  
361         gr_set_current_canvas(NULL);
362         
363         nm_draw_background(0,0,grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
364
365         grd_curcanv->cv_font = MEDIUM3_FONT;
366
367         gr_string( 0x8000, LHY(15), TXT_HIGH_SCORES );
368
369         grd_curcanv->cv_font = SMALL_FONT;
370
371         gr_set_fontcolor( BM_XRGB(31,26,5), -1 );
372         gr_string(  LHX(31+33+XX), LHY(46+7+YY), TXT_NAME );
373         gr_string(  LHX(82+33+XX), LHY(46+7+YY), TXT_SCORE );
374         gr_string( LHX(127+33+XX), LHY(46+7+YY), TXT_SKILL );
375         gr_string( LHX(170+33+XX), LHY(46+7+YY), TXT_LEVELS );
376 //      gr_string( 202, 46, "Kills" );
377 //      gr_string( 234, 46, "Rescues" );
378         gr_string( LHX(288-42+XX), LHY(46+7+YY), TXT_TIME );
379
380         if ( citem < 0 )        
381                 gr_string( 0x8000, LHY(175), TXT_PRESS_CTRL_R );
382
383         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
384
385         gr_printf( 0x8000, LHY(31), "%c%s%c  - %s", 34, Scores.cool_saying, 34, Scores.stats[0].name );
386
387         for (i=0; i<MAX_HIGH_SCORES; i++ )              {
388                 //@@if (i==0)   {
389                 //@@    gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
390                 //@@} else {
391                 //@@    gr_set_fontcolor( gr_fade_table[BM_XRGB(28,28,28)+((28-i*2)*256)], -1 );
392                 //@@}                                                                                                            
393
394                 gr_set_fontcolor( BM_XRGB(28-i*2,28-i*2,28-i*2), -1 );
395                 scores_draw_item( i, &Scores.stats[i] );
396         }
397
398         gr_palette_fade_in( gr_palette,32, 0);
399
400 #ifdef OGL
401         gr_update();
402 #endif
403
404         game_flush_inputs();
405
406         done = 0;
407         looper = 0;
408
409         while(!done)    {
410                 if ( citem > -1 )       {
411         
412                         t1      = timer_get_fixed_seconds();
413                         while ( timer_get_fixed_seconds() < t1+F1_0/128 );      
414
415                         //@@gr_set_fontcolor( gr_fade_table[fades[looper]*256+BM_XRGB(28,28,28)], -1 );
416                         gr_set_fontcolor( BM_XRGB(7+fades[looper],7+fades[looper],7+fades[looper]), -1 );
417                         looper++;
418                         if (looper>63) looper=0;
419                         if ( citem ==  MAX_HIGH_SCORES )
420                                 scores_draw_item( MAX_HIGH_SCORES, &Last_game );
421                         else
422                                 scores_draw_item( citem, &Scores.stats[citem] );
423                         gr_update();
424                 }
425
426                 for (i=0; i<4; i++ )    
427                         if (joy_get_button_down_cnt(i)>0) done=1;
428                 for (i=0; i<3; i++ )    
429                         if (mouse_button_down_count(i)>0) done=1;
430
431                 //see if redbook song needs to be restarted
432                 songs_check_redbook_repeat();
433
434                 k = key_inkey();
435                 switch( k )     {
436                 case KEY_CTRLED+KEY_R:          
437                         if ( citem < 0 )                {
438                                 // Reset scores...
439                                 if ( nm_messagebox( NULL, 2,  TXT_NO, TXT_YES, TXT_RESET_HIGH_SCORES )==1 )     {
440                                         PHYSFS_delete(get_scores_filename());
441                                         gr_palette_fade_out( gr_palette, 32, 0 );
442                                         goto ReshowScores;
443                                 }
444                         }
445                         break;
446                 case KEY_BACKSP:                                Int3(); k = 0; break;
447                 case KEY_PRINT_SCREEN:          save_screen_shot(0); k = 0; break;
448                         
449                 case KEY_ENTER:
450                 case KEY_SPACEBAR:
451                 case KEY_ESC:
452                         done=1;
453                         break;
454                 }
455         }
456
457 // Restore background and exit
458         gr_palette_fade_out( gr_palette, 32, 0 );
459
460         gr_set_current_canvas(NULL);
461
462         game_flush_inputs();
463         
464 }