bool CLevel::LoadFromFile()
{
    bool ErrorOccurred = false;    

    // Open the existing level file for reading
    ifstream in;
    in.open( m_Filename_full.c_str(), ios_base::in );

    // If it failed
    if (!in.is_open())
    {
        theLog.WriteLine ("Options         => Loading level file %s failed.", m_Filename_full.c_str() );
        // Stop loading levels
        return false;
    }


    // This is the first line for the level files beginning with version 2 (therefore "V2plus")
    string headerV2plus( "; Bombermaaan level file version=" );

    string s;
    getline( in, s );
    int LevelVersion;

    // When header string is found at the beginning of the string, find() returns 0 (offset 0)
    if ( s.find( headerV2plus ) == 0 ) {
        // We can look for the level version now
        LevelVersion = atoi( s.substr( headerV2plus.length() ).c_str() );
    }
    else
    {
        LevelVersion = 1;
    }
    
    switch ( LevelVersion ) {

        case 1:
            if (!LoadVersion1( in ) ) {
                ErrorOccurred = true;
            }
            break;

        case 2:
            if (!LoadVersion2( m_Filename_full ) ) {
                ErrorOccurred = true;
            }
            break;

        default:
            theLog.WriteLine ("Options         => !!! Unsupported version of level file %s.", m_Filename_short.c_str());
            ErrorOccurred = true;
            break;

    }

	// Close the level file
    in.close();
    
    // Validate this level if no error occurred so far
    if (!ErrorOccurred)
    {
        ErrorOccurred = !Validate();
    }

    // If there wasn't any problem
    if (!ErrorOccurred)
    {
        theLog.WriteLine ("Options         => Level file %s was successfully loaded (version %d).", m_Filename_short.c_str(), LevelVersion);
    }
    // If there was a problem
    else
    {
        theLog.WriteLine ("Options         => !!! Could not load level file %s (version %d).", m_Filename_short.c_str(), LevelVersion);
    }

    // If we had to stop then there is a problem.
    if (ErrorOccurred)
        return false;

    // Everything went right
    return true;
}
Exemple #2
0
bool COptions::LoadLevel( const char *filename )
{
    //-----------------------------------
    // Allocate data and name for 1 level
    //-----------------------------------

    AllocateLevels(1);
    
    //------------------------------------------------------
    // Load all the level files detected earlier
    //------------------------------------------------------
    
    bool ErrorOccurred = false;    
    
    // Open the existing level file for reading
    ifstream in;
    in.open( filename, ios_base::in );

    // If it failed
    if (!in.is_open())
    {
        cerr << "Loading level file " << filename << " failed." << endl;
        // Stop loading levels
        return false;
    }

    // This is the first line for the level files beginning with version 2 (therefore "V2plus")
    string headerV2plus( "; Bombermaaan level file version=" );

    string s;
    getline( in, s );
    int LevelVersion;

    // When header string is found at the beginning of the string, find() returns 0 (offset 0)
    if ( s.find( headerV2plus ) == 0 ) {
        // We can look for the level version now
        LevelVersion = atoi( s.substr( headerV2plus.length() ).c_str() );
    }
    else
    {
        LevelVersion = 1;
    }
    
    switch ( LevelVersion ) {

        case 1:
            if (!LoadLevel_Version1( in, 0 ) ) {
                ErrorOccurred = true;
            }
            break;

        case 2:
            if (!LoadLevel_Version2( filename, 0 ) ) {
                ErrorOccurred = true;
            }
            break;

        default:
            printf ("Options         => !!! Unsupported version of level file %s.", filename);
            ErrorOccurred = true;
            break;

    }

	// Close the level file
    in.close();
    
    unsigned int sumOfMaxItems;

    // too many items?
    if (!ErrorOccurred && !CheckMaxNumberOfItems( 0, &sumOfMaxItems ))
    {
        // Log there is a problem
        cerr << "!!! Level file is incorrect (Too many items: " << sumOfMaxItems << " of " << MAX_ITEMS << " allowed)." << endl;

        // Stop loading levels
        ErrorOccurred = true;
    }

    // If we had to stop then there is a problem.
    if (ErrorOccurred)
        return false;

    // Everything went right
    return true;
}