]> icculus.org git repositories - btb/d2x.git/blob - main/scores.c
Created header for MAKE_SIG macro
[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 #include <conf.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include "error.h"
22 #include "pstypes.h"
23 #include "gr.h"
24 #include "mono.h"
25 #include "key.h"
26 #include "palette.h"
27 #include "game.h"
28 #include "gamefont.h"
29 #include "u_mem.h"
30 #include "songs.h"
31 #include "newmenu.h"
32 #include "menu.h"
33 #include "player.h"
34 #include "screens.h"
35 #include "gamefont.h"
36 #include "mouse.h"
37 #include "joy.h"
38 #include "timer.h"
39 #include "text.h"
40 #include "d_io.h"
41 #include "strutil.h"
42
43 #define VERSION_NUMBER          1
44 #define SCORES_FILENAME         "DESCENT.HI"
45 #define COOL_MESSAGE_LEN        50
46 #define MAX_HIGH_SCORES         10
47
48 typedef struct stats_info {
49         char            name[CALLSIGN_LEN+1];
50         int             score;
51         byte            starting_level;
52         byte            ending_level;
53         byte    diff_level;
54         short   kill_ratio;     // 0-100
55         short           hostage_ratio;   // 
56         int             seconds;                        // How long it took in seconds...
57 } stats_info;
58
59 typedef struct all_scores {
60         char                    signature[3];                   // DHS
61         byte                    version;                                        // version
62         char                    cool_saying[COOL_MESSAGE_LEN];
63         stats_info      stats[MAX_HIGH_SCORES];
64 } all_scores;
65
66 static all_scores Scores;
67
68 stats_info Last_game;
69
70 char scores_filename[128];
71
72 #define XX  (7)
73 #define YY  (-3)
74
75 #define LHX(x)          ((x)*(MenuHires?2:1))
76 #define LHY(y)          ((y)*(MenuHires?2.4:1))
77
78 void scores_view(int citem);
79
80
81 char * get_scores_filename()
82 {
83 #ifndef RELEASE
84         // Only use the MINER variable for internal developement
85         char *p;
86         p=getenv( "MINER" );
87         if (p)  {
88                 sprintf( scores_filename, "%s\\game\\%s", p, SCORES_FILENAME );
89                 Assert(strlen(scores_filename) < 128);
90                 return scores_filename;
91         }
92 #endif
93         #ifdef MACINTOSH                // put the high scores into the data folder
94         sprintf( scores_filename, ":Data:%s", SCORES_FILENAME );
95         #else
96         sprintf( scores_filename, "%s", SCORES_FILENAME );
97         #endif
98         return scores_filename;
99 }
100
101 #ifndef D2_OEM
102 #define COOL_SAYING TXT_REGISTER_DESCENT
103 #else
104 #define COOL_SAYING "Get all 30 levels of D2 from 1-800-INTERPLAY"
105 #endif
106
107 void scores_read()
108 {
109         FILE * fp;
110         int fsize;
111
112         // clear score array...
113         memset( &Scores, 0, sizeof(all_scores) );
114
115         fp = fopen( get_scores_filename(), "rb" );
116         if (fp==NULL) {
117                 int i;
118
119                 // No error message needed, code will work without a scores file
120                 sprintf( Scores.cool_saying, COOL_SAYING );
121                 sprintf( Scores.stats[0].name, "Parallax" );
122                 sprintf( Scores.stats[1].name, "Matt" );
123                 sprintf( Scores.stats[2].name, "Mike" );
124                 sprintf( Scores.stats[3].name, "Adam" );
125                 sprintf( Scores.stats[4].name, "Mark" );
126                 sprintf( Scores.stats[5].name, "Jasen" );
127                 sprintf( Scores.stats[6].name, "Samir" );
128                 sprintf( Scores.stats[7].name, "Doug" );
129                 sprintf( Scores.stats[8].name, "Dan" );
130                 sprintf( Scores.stats[9].name, "Jason" );
131
132                 for (i=0; i<10; i++)
133                         Scores.stats[i].score = (10-i)*1000;
134                 return;
135         }
136                 
137         fsize = filelength( fileno( fp ));
138
139         if ( fsize != sizeof(all_scores) )      {
140                 fclose(fp);
141                 return;
142         }
143         // Read 'em in...
144         fread( &Scores, sizeof(all_scores),1, fp );
145         fclose(fp);
146
147         if ( (Scores.version!=VERSION_NUMBER)||(Scores.signature[0]!='D')||(Scores.signature[1]!='H')||(Scores.signature[2]!='S') )     {
148                 memset( &Scores, 0, sizeof(all_scores) );
149                 return;
150         }
151 }
152
153 void scores_write()
154 {
155         FILE * fp;
156
157         fp = fopen( get_scores_filename(), "wb" );
158         if (fp==NULL) {
159                 nm_messagebox( TXT_WARNING, 1, TXT_OK, "%s\n'%s'", TXT_UNABLE_TO_OPEN, get_scores_filename()  );
160                 return;
161         }
162
163         Scores.signature[0]='D';
164         Scores.signature[1]='H';
165         Scores.signature[2]='S';
166         Scores.version = VERSION_NUMBER;
167         fwrite( &Scores,sizeof(all_scores),1, fp );
168         fclose(fp);
169 }
170
171 void int_to_string( int number, char *dest )
172 {
173         int i,l,c;
174         char buffer[20],*p;
175
176         sprintf( buffer, "%d", number );
177
178         l = strlen(buffer);
179         if (l<=3) {
180                 // Don't bother with less than 3 digits
181                 sprintf( dest, "%d", number );
182                 return;
183         }
184
185         c = 0;
186         p=dest;
187         for (i=l-1; i>=0; i-- ) {
188                 if (c==3) {
189                         *p++=',';
190                         c = 0;
191                 }
192                 c++;
193                 *p++ = buffer[i];
194         }
195         *p++ = '\0';
196         strrev(dest);
197 }
198
199 void scores_fill_struct(stats_info * stats)
200 {
201                 strcpy( stats->name, Players[Player_num].callsign );
202                 stats->score = Players[Player_num].score;
203                 stats->ending_level = Players[Player_num].level;
204                 if (Players[Player_num].num_robots_total > 0 )  
205                         stats->kill_ratio = (Players[Player_num].num_kills_total*100)/Players[Player_num].num_robots_total;
206                 else
207                         stats->kill_ratio = 0;
208
209                 if (Players[Player_num].hostages_total > 0 )    
210                         stats->hostage_ratio = (Players[Player_num].hostages_rescued_total*100)/Players[Player_num].hostages_total;
211                 else
212                         stats->hostage_ratio = 0;
213
214                 stats->seconds = f2i(Players[Player_num].time_total)+(Players[Player_num].hours_total*3600);
215
216                 stats->diff_level = Difficulty_level;
217                 stats->starting_level = Players[Player_num].starting_level;
218 }
219
220 //char * score_placement[10] = { TXT_1ST, TXT_2ND, TXT_3RD, TXT_4TH, TXT_5TH, TXT_6TH, TXT_7TH, TXT_8TH, TXT_9TH, TXT_10TH };
221
222 void scores_maybe_add_player(int abort_flag)
223 {
224         char text1[COOL_MESSAGE_LEN+10];
225         newmenu_item m[10];
226         int i,position;
227
228         #ifdef APPLE_DEMO               // no high scores in apple oem version
229         return;
230         #endif
231
232         if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
233                 return;
234   
235         scores_read();
236         
237         position = MAX_HIGH_SCORES;
238         for (i=0; i<MAX_HIGH_SCORES; i++ )      {
239                 if ( Players[Player_num].score > Scores.stats[i].score )        {
240                         position = i;
241                         break;
242                 }
243         }
244         
245         if ( position == MAX_HIGH_SCORES ) {
246                 if (abort_flag)
247                         return;
248                 scores_fill_struct( &Last_game );
249         } else {
250 //--            if ( Difficulty_level < 1 )     {
251 //--                    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 );
252 //--                    return;
253 //--            }
254
255                 if ( position==0 )      {
256                         strcpy( text1,  "" );
257                         m[0].type = NM_TYPE_TEXT; m[0].text = TXT_COOL_SAYING;
258                         m[1].type = NM_TYPE_INPUT; m[1].text = text1; m[1].text_len = COOL_MESSAGE_LEN-5;
259                         newmenu_do( TXT_HIGH_SCORE, TXT_YOU_PLACED_1ST, 2, m, NULL );
260                         strncpy( Scores.cool_saying, text1, COOL_MESSAGE_LEN );
261                         if (strlen(Scores.cool_saying)<1)
262                                 sprintf( Scores.cool_saying, "No Comment" );
263                 } else {
264                         nm_messagebox( TXT_HIGH_SCORE, 1, TXT_OK, "%s %s!", TXT_YOU_PLACED, *(&TXT_1ST + position) );
265                 }
266         
267                 // move everyone down...
268                 for ( i=MAX_HIGH_SCORES-1; i>position; i-- )    {
269                         Scores.stats[i] = Scores.stats[i-1];
270                 }
271
272                 scores_fill_struct( &Scores.stats[position] );
273         
274                 scores_write();
275
276         }
277         scores_view(position);
278 }
279
280 void scores_rprintf(int x, int y, char * format, ... )
281 {
282         va_list args;
283         char buffer[128];
284         int w, h, aw;
285         char *p;
286
287         va_start(args, format );
288         vsprintf(buffer,format,args);
289         va_end(args);
290
291         //replace the digit '1' with special wider 1
292         for (p=buffer;*p;p++)
293                 if (*p=='1') *p=132;
294
295         gr_get_string_size(buffer, &w, &h, &aw );
296
297         gr_string( LHX(x)-w, LHY(y), buffer );
298 }
299
300
301 void scores_draw_item( int  i, stats_info * stats )
302 {
303         char buffer[20];
304
305                 int y;
306
307         WIN(DDGRLOCK(dd_grd_curcanv));
308                 y = 7+70+i*9;
309
310                 if (i==0) y -= 8;
311
312                 if ( i==MAX_HIGH_SCORES )       {
313                         y += 8;
314                         //scores_rprintf( 17+33+XX, y+YY, "" );
315                 } else {
316                         scores_rprintf( 17+33+XX, y+YY, "%d.", i+1 );
317                 }
318
319                 if (strlen(stats->name)==0) {
320                         gr_printf( LHX(26+33+XX), LHY(y+YY), TXT_EMPTY );
321                         WIN(DDGRUNLOCK(dd_grd_curcanv));
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         WIN(DDGRUNLOCK(dd_grd_curcanv));
348 }
349
350 void scores_view(int citem)
351 {
352         fix t1;
353         int i,done,looper;
354         int k;
355         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 };
356
357 ReshowScores:
358         scores_read();
359
360         set_screen_mode(SCREEN_MENU);
361  
362         WINDOS( dd_gr_set_current_canvas(NULL),
363                                 gr_set_current_canvas(NULL)
364         );
365         
366         nm_draw_background(0,0,grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
367
368         grd_curcanv->cv_font = MEDIUM3_FONT;
369
370 WIN(DDGRLOCK(dd_grd_curcanv));
371         gr_string( 0x8000, LHY(15), TXT_HIGH_SCORES );
372
373         grd_curcanv->cv_font = SMALL_FONT;
374
375         gr_set_fontcolor( BM_XRGB(31,26,5), -1 );
376         gr_string(  LHX(31+33+XX), LHY(46+7+YY), TXT_NAME );
377         gr_string(  LHX(82+33+XX), LHY(46+7+YY), TXT_SCORE );
378         gr_string( LHX(127+33+XX), LHY(46+7+YY), TXT_SKILL );
379         gr_string( LHX(170+33+XX), LHY(46+7+YY), TXT_LEVELS );
380 //      gr_string( 202, 46, "Kills" );
381 //      gr_string( 234, 46, "Rescues" );
382         gr_string( LHX(288-42+XX), LHY(46+7+YY), TXT_TIME );
383
384         if ( citem < 0 )        
385                 gr_string( 0x8000, LHY(175), TXT_PRESS_CTRL_R );
386
387         gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
388
389         gr_printf( 0x8000, LHY(31), "%c%s%c  - %s", 34, Scores.cool_saying, 34, Scores.stats[0].name );
390 WIN(DDGRUNLOCK(dd_grd_curcanv));        
391
392         for (i=0; i<MAX_HIGH_SCORES; i++ )              {
393                 //@@if (i==0)   {
394                 //@@    gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
395                 //@@} else {
396                 //@@    gr_set_fontcolor( gr_fade_table[BM_XRGB(28,28,28)+((28-i*2)*256)], -1 );
397                 //@@}                                                                                                            
398
399                 gr_set_fontcolor( BM_XRGB(28-i*2,28-i*2,28-i*2), -1 );
400                 scores_draw_item( i, &Scores.stats[i] );
401         }
402
403         gr_palette_fade_in( gr_palette,32, 0);
404
405         game_flush_inputs();
406
407         done = 0;
408         looper = 0;
409
410         while(!done)    {
411                 if ( citem > -1 )       {
412         
413                         t1      = timer_get_fixed_seconds();
414                         while ( timer_get_fixed_seconds() < t1+F1_0/128 );      
415
416                         //@@gr_set_fontcolor( gr_fade_table[fades[looper]*256+BM_XRGB(28,28,28)], -1 );
417                         gr_set_fontcolor( BM_XRGB(7+fades[looper],7+fades[looper],7+fades[looper]), -1 );
418                         looper++;
419                         if (looper>63) looper=0;
420                         if ( citem ==  MAX_HIGH_SCORES )
421                                 scores_draw_item( MAX_HIGH_SCORES, &Last_game );
422                         else
423                                 scores_draw_item( citem, &Scores.stats[citem] );
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         #ifdef WINDOWS
435                 {
436                         MSG msg;
437
438                         DoMessageStuff(&msg);
439
440                         if (_RedrawScreen) {
441                                 _RedrawScreen = FALSE;
442                                 goto ReshowScores;
443                         }
444
445                         DDGRRESTORE;
446                 }
447         #endif
448
449                 k = key_inkey();
450                 switch( k )     {
451                 case KEY_CTRLED+KEY_R:          
452                         if ( citem < 0 )                {
453                                 // Reset scores...
454                                 if ( nm_messagebox( NULL, 2,  TXT_NO, TXT_YES, TXT_RESET_HIGH_SCORES )==1 )     {
455                                         remove( get_scores_filename() );
456                                         gr_palette_fade_out( gr_palette, 32, 0 );
457                                         goto ReshowScores;
458                                 }
459                         }
460                         break;
461                 case KEY_BACKSP:                                Int3(); k = 0; break;
462                 case KEY_PRINT_SCREEN:          save_screen_shot(0); k = 0; break;
463                         
464                 case KEY_ENTER:
465                 case KEY_SPACEBAR:
466                 case KEY_ESC:
467                         done=1;
468                         break;
469                 }
470         }
471
472 // Restore background and exit
473         gr_palette_fade_out( gr_palette, 32, 0 );
474
475 #ifdef WINDOWS
476         DDGRRESTORE;
477 #endif
478
479         WINDOS( dd_gr_set_current_canvas(NULL),
480                                 gr_set_current_canvas(NULL)
481         );
482
483         game_flush_inputs();
484         
485 }