whitespace
[btb/d2x.git] / main / scores.c
1 /* $Id: scores.c,v 1.2 2002-08-02 10:57:12 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef HAVE_CONFIG_H
16 #include <conf.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23
24 #include "scores.h"
25 #include "error.h"
26 #include "pstypes.h"
27 #include "gr.h"
28 #include "mono.h"
29 #include "key.h"
30 #include "palette.h"
31 #include "game.h"
32 #include "gamefont.h"
33 #include "u_mem.h"
34 #include "songs.h"
35 #include "newmenu.h"
36 #include "menu.h"
37 #include "player.h"
38 #include "screens.h"
39 #include "gamefont.h"
40 #include "mouse.h"
41 #include "joy.h"
42 #include "timer.h"
43 #include "text.h"
44 #include "d_io.h"
45 #include "strutil.h"
46
47 #define VERSION_NUMBER          1
48 #define SCORES_FILENAME         "DESCENT.HI"
49 #define COOL_MESSAGE_LEN        50
50 #define MAX_HIGH_SCORES         10
51
52 typedef struct stats_info {
53         char    name[CALLSIGN_LEN+1];
54         int             score;
55         byte    starting_level;
56         byte    ending_level;
57         byte    diff_level;
58         short   kill_ratio;             // 0-100
59         short   hostage_ratio;  // 
60         int             seconds;                // How long it took in seconds...
61 } stats_info;
62
63 typedef struct all_scores {
64         char                    signature[3];                   // DHS
65         byte                    version;                                // version
66         char                    cool_saying[COOL_MESSAGE_LEN];
67         stats_info      stats[MAX_HIGH_SCORES];
68 } all_scores;
69
70 static all_scores Scores;
71
72 stats_info Last_game;
73
74 char scores_filename[128];
75
76 #define XX  (7)
77 #define YY  (-3)
78
79 #define LHX(x)          ((x)*(MenuHires?2:1))
80 #define LHY(y)          ((y)*(MenuHires?2.4:1))
81
82
83 char * get_scores_filename()
84 {
85 #ifndef RELEASE
86         // Only use the MINER variable for internal developement
87         char *p;
88         p=getenv( "MINER" );
89         if (p)  {
90                 sprintf( scores_filename, "%s\\game\\%s", p, SCORES_FILENAME );
91                 Assert(strlen(scores_filename) < 128);
92                 return scores_filename;
93         }
94 #endif
95         #ifdef MACINTOSH                // put the high scores into the data folder
96         sprintf( scores_filename, ":Data:%s", SCORES_FILENAME );
97         #else
98         sprintf( scores_filename, "%s", SCORES_FILENAME );
99         #endif
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         FILE * fp;
112         int fsize;
113
114         // clear score array...
115         memset( &Scores, 0, sizeof(all_scores) );
116
117         fp = fopen( get_scores_filename(), "rb" );
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, 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 = filelength( fileno( fp ));
140
141         if ( fsize != sizeof(all_scores) )      {
142                 fclose(fp);
143                 return;
144         }
145         // Read 'em in...
146         fread( &Scores, sizeof(all_scores),1, fp );
147         fclose(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         FILE * fp;
158
159         fp = fopen( get_scores_filename(), "wb" );
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         fwrite( &Scores,sizeof(all_scores),1, fp );
170         fclose(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         WIN(DDGRLOCK(dd_grd_curcanv));
310                 y = 7+70+i*9;
311
312                 if (i==0) y -= 8;
313
314                 if ( i==MAX_HIGH_SCORES )       {
315                         y += 8;
316                         //scores_rprintf( 17+33+XX, y+YY, "" );
317                 } else {
318                         scores_rprintf( 17+33+XX, y+YY, "%d.", i+1 );
319                 }
320
321                 if (strlen(stats->name)==0) {
322                         gr_printf( LHX(26+33+XX), LHY(y+YY), TXT_EMPTY );
323                         WIN(DDGRUNLOCK(dd_grd_curcanv));
324                         return;
325                 }
326                 gr_printf( LHX(26+33+XX), LHY(y+YY), "%s", stats->name );
327                 int_to_string(stats->score, buffer);
328                 scores_rprintf( 109+33+XX, y+YY, "%s", buffer );
329
330                 gr_printf( LHX(125+33+XX), LHY(y+YY), "%s", MENU_DIFFICULTY_TEXT(stats->diff_level) );
331
332                 if ( (stats->starting_level > 0 ) && (stats->ending_level > 0 ))
333                         scores_rprintf( 192+33+XX, y+YY, "%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-%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, "S%d-S%d", -stats->starting_level, -stats->ending_level );
338                 else if ( (stats->starting_level > 0 ) && (stats->ending_level < 0 ))
339                         scores_rprintf( 192+33+XX, y+YY, "%d-S%d", stats->starting_level, -stats->ending_level );
340
341                 {
342                         int h, m, s;
343                         h = stats->seconds/3600;
344                         s = stats->seconds%3600;
345                         m = s / 60;
346                         s = s % 60;
347                         scores_rprintf( 311-42+XX, y+YY, "%d:%02d:%02d", h, m, s );
348                 }
349         WIN(DDGRUNLOCK(dd_grd_curcanv));
350 }
351
352 void scores_view(int citem)
353 {
354         fix t1;
355         int i,done,looper;
356         int k;
357         byte 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 };
358
359 ReshowScores:
360         scores_read();
361
362         set_screen_mode(SCREEN_MENU);
363  
364         WINDOS( dd_gr_set_current_canvas(NULL),
365                                 gr_set_current_canvas(NULL)
366         );
367         
368         nm_draw_background(0,0,grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
369
370         grd_curcanv->cv_font = MEDIUM3_FONT;
371
372 WIN(DDGRLOCK(dd_grd_curcanv));
373         gr_string( 0x8000, LHY(15), TXT_HIGH_SCORES );
374
375         grd_curcanv->cv_font = SMALL_FONT;
376
377         gr_set_fontcolor( BM_XRGB(31,26,5), -1 );
378         gr_string(  LHX(31+33+XX), LHY(46+7+YY), TXT_NAME );
379         gr_string(  LHX(82+33+XX), LHY(46+7+YY), TXT_SCORE );
380         gr_string( LHX(127+33+XX), LHY(46+7+YY), TXT_SKILL );
381         gr_string( LHX(170+33+XX), LHY(46+7+YY), TXT_LEVELS );
382 //      gr_string( 202, 46, "Kills" );
383 //      gr_string( 234, 46, "Rescues" );
384         gr_string( LHX(288-42+XX), LHY(46+7+YY), TXT_TIME );
385
386         if ( citem < 0 )        
387                 gr_string( 0x8000, LHY(175), TXT_PRESS_CTRL_R );
388
389         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
390
391         gr_printf( 0x8000, LHY(31), "%c%s%c  - %s", 34, Scores.cool_saying, 34, Scores.stats[0].name );
392 WIN(DDGRUNLOCK(dd_grd_curcanv));        
393
394         for (i=0; i<MAX_HIGH_SCORES; i++ )              {
395                 //@@if (i==0)   {
396                 //@@    gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
397                 //@@} else {
398                 //@@    gr_set_fontcolor( gr_fade_table[BM_XRGB(28,28,28)+((28-i*2)*256)], -1 );
399                 //@@}                                                                                                            
400
401                 gr_set_fontcolor( BM_XRGB(28-i*2,28-i*2,28-i*2), -1 );
402                 scores_draw_item( i, &Scores.stats[i] );
403         }
404
405         gr_palette_fade_in( gr_palette,32, 0);
406
407         game_flush_inputs();
408
409         done = 0;
410         looper = 0;
411
412         while(!done)    {
413                 if ( citem > -1 )       {
414         
415                         t1      = timer_get_fixed_seconds();
416                         while ( timer_get_fixed_seconds() < t1+F1_0/128 );      
417
418                         //@@gr_set_fontcolor( gr_fade_table[fades[looper]*256+BM_XRGB(28,28,28)], -1 );
419                         gr_set_fontcolor( BM_XRGB(7+fades[looper],7+fades[looper],7+fades[looper]), -1 );
420                         looper++;
421                         if (looper>63) looper=0;
422                         if ( citem ==  MAX_HIGH_SCORES )
423                                 scores_draw_item( MAX_HIGH_SCORES, &Last_game );
424                         else
425                                 scores_draw_item( citem, &Scores.stats[citem] );
426                 }
427
428                 for (i=0; i<4; i++ )    
429                         if (joy_get_button_down_cnt(i)>0) done=1;
430                 for (i=0; i<3; i++ )    
431                         if (mouse_button_down_count(i)>0) done=1;
432
433                 //see if redbook song needs to be restarted
434                 songs_check_redbook_repeat();
435
436         #ifdef WINDOWS
437                 {
438                         MSG msg;
439
440                         DoMessageStuff(&msg);
441
442                         if (_RedrawScreen) {
443                                 _RedrawScreen = FALSE;
444                                 goto ReshowScores;
445                         }
446
447                         DDGRRESTORE;
448                 }
449         #endif
450
451                 k = key_inkey();
452                 switch( k )     {
453                 case KEY_CTRLED+KEY_R:          
454                         if ( citem < 0 )                {
455                                 // Reset scores...
456                                 if ( nm_messagebox( NULL, 2,  TXT_NO, TXT_YES, TXT_RESET_HIGH_SCORES )==1 )     {
457                                         remove( get_scores_filename() );
458                                         gr_palette_fade_out( gr_palette, 32, 0 );
459                                         goto ReshowScores;
460                                 }
461                         }
462                         break;
463                 case KEY_BACKSP:                                Int3(); k = 0; break;
464                 case KEY_PRINT_SCREEN:          save_screen_shot(0); k = 0; break;
465                         
466                 case KEY_ENTER:
467                 case KEY_SPACEBAR:
468                 case KEY_ESC:
469                         done=1;
470                         break;
471                 }
472         }
473
474 // Restore background and exit
475         gr_palette_fade_out( gr_palette, 32, 0 );
476
477 #ifdef WINDOWS
478         DDGRRESTORE;
479 #endif
480
481         WINDOS( dd_gr_set_current_canvas(NULL),
482                                 gr_set_current_canvas(NULL)
483         );
484
485         game_flush_inputs();
486         
487 }