Ejemplo n.º 1
0
/*! 
 Call when a race has just been won
 \author  jfpatry
 \date    Created:  2000-09-24
 \date    Modified: 2000-09-24
 */
void update_for_won_race( void )
{
    race_data_t *race_data;
    
    check_assertion( g_game.practicing == False,
                    "Tried to update for won race in practice mode" );
    
    race_data = (race_data_t*)get_list_elem_data( cur_elem );
    
    if ( last_completed_race == NULL ||
        compare_race_positions( cup_data, last_completed_race, 
                               cur_elem ) > 0 )
    {
        last_completed_race = cur_elem;
		
        if ( cur_elem == get_list_tail( race_list ) ) {
            cup_complete = True;
            
            if ( !set_last_completed_cup( 
                                         plyr->name,
                                         g_game.current_event,
                                         g_game.difficulty,
                                         g_game.current_cup ) )
            {
                print_warning( IMPORTANT_WARNING,
                              "Couldn't save cup completion" );
            } else {
                print_debug( DEBUG_GAME_LOGIC, 
                            "Cup %s completed", 
                            g_game.current_cup );
            }
        }
    }
}
Ejemplo n.º 2
0
void init_saved_games( void ) 
{
    char dir_name[BUFF_LEN];
    FILE* save_stream;
    save_info_t this_save;
    char player_name[BUFF_LEN];
    int sav_index;
    char file_name[BUFF_LEN];
    int i;
    char magic[4];
    list_t dir_file_list = NULL;
    list_elem_t cur_dir_file = NULL;
    char *cur_dir_filename = NULL;


    progress_save_table = create_hash_table();

    for (i=0; i<DIFFICULTY_NUM_LEVELS; i++) {
	results_save_table[i] = create_hash_table();
    }
 
    if (get_config_dir_name( dir_name, sizeof(dir_name) ) != 0) {
	return;
    }

    dir_file_list = get_dir_file_list(dir_name);
    if ( dir_file_list == NULL ) {
	/* Config dir doesn't exist.  Don't print warning since this is a
	   normal condition the first time the program is run. */
	return;
    }

    for ( cur_dir_file = get_list_head( dir_file_list );
	  cur_dir_file != NULL;
	  cur_dir_file = get_next_list_elem( dir_file_list, cur_dir_file ) )
    {
	cur_dir_filename = (char*) get_list_elem_data( cur_dir_file );

	if (get_sav_index(cur_dir_filename, &sav_index)) {

	    strncpy(player_name, cur_dir_filename, sav_index);
	    player_name[sav_index] = '\0';

	    sprintf( file_name, "%s" DIR_SEPARATOR "%s", 
		     dir_name,
		     cur_dir_filename );

	    save_stream = fopen( file_name, "r" );

	    if ( fread( magic, sizeof(magic), 1, save_stream ) != 1 ||
		 strncmp( magic, SAVE_MAGIC_V1, sizeof(magic) ) != 0 ) 
	    {
		print_warning( IMPORTANT_WARNING,
			       "`%s' is not a valid saved game file",
			       file_name );
		fclose( save_stream);
		continue;
	    }


	    if (save_stream != NULL) {
		while (fread( &this_save, sizeof(this_save), 1, save_stream)) {
		    switch ( this_save.data_type ) {
		    case EVENT_INFO:
			set_last_completed_cup( player_name, 
						this_save.data.event.event, 
						this_save.data.event.difficulty, 
						this_save.data.event.cup );
			print_debug( DEBUG_SAVE,
				     "Read completed from `%s': "
				     "name: %s, event: %s, difficulty: %d, cup: %s",
				     cur_dir_filename, 
				     player_name,
				     this_save.data.event.event,
				     this_save.data.event.difficulty,
				     this_save.data.event.cup );
			break;

		    case RACE_RESULTS:
			set_saved_race_results( player_name,
						this_save.data.results.event,
						this_save.data.results.cup,
						this_save.data.results.race,
						this_save.data.results.difficulty,
						this_save.data.results.time,
						this_save.data.results.herring,
						this_save.data.results.score );
			print_debug( DEBUG_SAVE,
				     "Read results from `%s': "
				     "name: %s, event: %s, cup: %s, "
				     "race: %s, difficulty: %d, time: %g, "
				     "herring: %d, score: %d",
				     cur_dir_filename, 
				     player_name,
				     this_save.data.results.event,
				     this_save.data.results.cup,
				     this_save.data.results.race,
				     this_save.data.results.difficulty,
				     this_save.data.results.time,
				     this_save.data.results.herring,
				     this_save.data.results.score );
			break;

		    default:
			print_warning( IMPORTANT_WARNING, 
				       "Unrecognized data type in save file." );

		    }
		}


		if ( fclose( save_stream ) != 0 ) {
		    perror( "fclose" );
		}
	    } else {
		print_warning( IMPORTANT_WARNING,
			       "Couldn't read file `%s': %s",
			       file_name, strerror( errno ) );
	    }
	}
    }

    free_dir_file_list( dir_file_list );
}