1 /* $Id: scores.c,v 1.7 2004-12-01 12:48:13 btb Exp $ */
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.
17 * Inferno High Scores and Statistics System
52 #define VERSION_NUMBER 1
53 #define SCORES_FILENAME "descent.hi"
54 #define COOL_MESSAGE_LEN 50
55 #define MAX_HIGH_SCORES 10
57 typedef struct stats_info {
58 char name[CALLSIGN_LEN+1];
63 short kill_ratio; // 0-100
64 short hostage_ratio; //
65 int seconds; // How long it took in seconds...
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];
75 static all_scores Scores;
79 char scores_filename[128];
84 #define LHX(x) ((x)*(MenuHires?2:1))
85 #define LHY(y) ((y)*(MenuHires?2.4:1))
88 char * get_scores_filename()
91 // Only use the MINER variable for internal developement
95 sprintf( scores_filename, "%s\\game\\%s", p, SCORES_FILENAME );
96 Assert(strlen(scores_filename) < 128);
97 return scores_filename;
100 #ifdef MACINTOSH // put the high scores into the data folder
101 sprintf( scores_filename, ":Data:%s", SCORES_FILENAME );
103 sprintf( scores_filename, "%s", SCORES_FILENAME );
105 return scores_filename;
109 #define COOL_SAYING TXT_REGISTER_DESCENT
111 #define COOL_SAYING "Get all 30 levels of D2 from 1-800-INTERPLAY"
119 // clear score array...
120 memset( &Scores, 0, sizeof(all_scores) );
122 fp = PHYSFS_openRead(get_scores_filename());
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" );
140 Scores.stats[i].score = (10-i)*1000;
144 fsize = PHYSFS_fileLength(fp);
146 if ( fsize != sizeof(all_scores) ) {
151 PHYSFS_read(fp, &Scores, sizeof(all_scores), 1);
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) );
164 fp = PHYSFS_openWrite(get_scores_filename());
166 nm_messagebox( TXT_WARNING, 1, TXT_OK, "%s\n'%s'", TXT_UNABLE_TO_OPEN, get_scores_filename() );
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);
178 void int_to_string( int number, char *dest )
183 sprintf( buffer, "%d", number );
187 // Don't bother with less than 3 digits
188 sprintf( dest, "%d", number );
194 for (i=l-1; i>=0; i-- ) {
206 void scores_fill_struct(stats_info * stats)
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;
214 stats->kill_ratio = 0;
216 if (Players[Player_num].hostages_total > 0 )
217 stats->hostage_ratio = (Players[Player_num].hostages_rescued_total*100)/Players[Player_num].hostages_total;
219 stats->hostage_ratio = 0;
221 stats->seconds = f2i(Players[Player_num].time_total)+(Players[Player_num].hours_total*3600);
223 stats->diff_level = Difficulty_level;
224 stats->starting_level = Players[Player_num].starting_level;
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 };
229 void scores_maybe_add_player(int abort_flag)
231 char text1[COOL_MESSAGE_LEN+10];
235 #ifdef APPLE_DEMO // no high scores in apple oem version
239 if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP))
244 position = MAX_HIGH_SCORES;
245 for (i=0; i<MAX_HIGH_SCORES; i++ ) {
246 if ( Players[Player_num].score > Scores.stats[i].score ) {
252 if ( position == MAX_HIGH_SCORES ) {
255 scores_fill_struct( &Last_game );
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 );
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" );
271 nm_messagebox( TXT_HIGH_SCORE, 1, TXT_OK, "%s %s!", TXT_YOU_PLACED, *(&TXT_1ST + position) );
274 // move everyone down...
275 for ( i=MAX_HIGH_SCORES-1; i>position; i-- ) {
276 Scores.stats[i] = Scores.stats[i-1];
279 scores_fill_struct( &Scores.stats[position] );
284 scores_view(position);
287 void scores_rprintf(int x, int y, char * format, ... )
294 va_start(args, format );
295 vsprintf(buffer,format,args);
298 //replace the digit '1' with special wider 1
299 for (p=buffer;*p;p++)
302 gr_get_string_size(buffer, &w, &h, &aw );
304 gr_string( LHX(x)-w, LHY(y), buffer );
308 void scores_draw_item( int i, stats_info * stats )
314 WIN(DDGRLOCK(dd_grd_curcanv));
319 if ( i==MAX_HIGH_SCORES ) {
321 //scores_rprintf( 17+33+XX, y+YY, "" );
323 scores_rprintf( 17+33+XX, y+YY, "%d.", i+1 );
326 if (strlen(stats->name)==0) {
327 gr_printf( LHX(26+33+XX), LHY(y+YY), TXT_EMPTY );
328 WIN(DDGRUNLOCK(dd_grd_curcanv));
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 );
335 gr_printf( LHX(125+33+XX), LHY(y+YY), "%s", MENU_DIFFICULTY_TEXT(stats->diff_level) );
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 );
348 h = stats->seconds/3600;
349 s = stats->seconds%3600;
352 scores_rprintf( 311-42+XX, y+YY, "%d:%02d:%02d", h, m, s );
354 WIN(DDGRUNLOCK(dd_grd_curcanv));
357 void scores_view(int citem)
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 };
367 set_screen_mode(SCREEN_MENU);
369 WINDOS( dd_gr_set_current_canvas(NULL),
370 gr_set_current_canvas(NULL)
373 nm_draw_background(0,0,grd_curcanv->cv_bitmap.bm_w, grd_curcanv->cv_bitmap.bm_h );
375 grd_curcanv->cv_font = MEDIUM3_FONT;
377 WIN(DDGRLOCK(dd_grd_curcanv));
378 gr_string( 0x8000, LHY(15), TXT_HIGH_SCORES );
380 grd_curcanv->cv_font = SMALL_FONT;
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 );
392 gr_string( 0x8000, LHY(175), TXT_PRESS_CTRL_R );
394 gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
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));
399 for (i=0; i<MAX_HIGH_SCORES; i++ ) {
401 //@@ gr_set_fontcolor( BM_XRGB(28,28,28), -1 );
403 //@@ gr_set_fontcolor( gr_fade_table[BM_XRGB(28,28,28)+((28-i*2)*256)], -1 );
406 gr_set_fontcolor( BM_XRGB(28-i*2,28-i*2,28-i*2), -1 );
407 scores_draw_item( i, &Scores.stats[i] );
410 gr_palette_fade_in( gr_palette,32, 0);
424 t1 = timer_get_fixed_seconds();
425 while ( timer_get_fixed_seconds() < t1+F1_0/128 );
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 );
430 if (looper>63) looper=0;
431 if ( citem == MAX_HIGH_SCORES )
432 scores_draw_item( MAX_HIGH_SCORES, &Last_game );
434 scores_draw_item( citem, &Scores.stats[citem] );
439 if (joy_get_button_down_cnt(i)>0) done=1;
441 if (mouse_button_down_count(i)>0) done=1;
443 //see if redbook song needs to be restarted
444 songs_check_redbook_repeat();
450 DoMessageStuff(&msg);
453 _RedrawScreen = FALSE;
463 case KEY_CTRLED+KEY_R:
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 );
473 case KEY_BACKSP: Int3(); k = 0; break;
474 case KEY_PRINT_SCREEN: save_screen_shot(0); k = 0; break;
484 // Restore background and exit
485 gr_palette_fade_out( gr_palette, 32, 0 );
491 WINDOS( dd_gr_set_current_canvas(NULL),
492 gr_set_current_canvas(NULL)