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