CASW_Location_Grid::CASW_Location_Grid()
{
    m_pMissionDatabase = new CASW_KeyValuesDatabase();
    m_pMissionDatabase->LoadFiles( "tilegen/new_missions/" );

    // Preprocess all of the new missions.

    // Load the rules.
    CASW_KeyValuesDatabase *pRulesDatabase = new CASW_KeyValuesDatabase();
    pRulesDatabase->LoadFiles( "tilegen/rules/" );

    // Create a mission preprocessor from the rules.
    m_pPreprocessor = new CTilegenMissionPreprocessor();
    for ( int i = 0; i < pRulesDatabase->GetFileCount(); ++ i )
    {
        m_pPreprocessor->ParseAndStripRules( pRulesDatabase->GetFile( i ) );
    }
    delete pRulesDatabase;

    // Preprocess the missions in-memory.
    for ( int i = 0; i < m_pMissionDatabase->GetFileCount(); ++ i )
    {
        KeyValues *pOriginalMission = m_pMissionDatabase->GetFile( i );
        if ( !m_pPreprocessor->SubstituteRules( pOriginalMission ) )
        {
            Warning( "Error pre-processing mission '%s'.\n", m_pMissionDatabase->GetFilename( i ) );
        }
    }

    LoadLocationGrid();
}
//Logic from CASW_Random_Missions::BuildAndLaunchRandomLevel
void MOD_Level_Builder::BuildMapFromLayoutFile( const char *szMissionRuleFile, const char *szOutputLayoutFile, const char *szThemeName, bool bCompileLevel)
{
	if (IsBuildingLevel())
	{
		Msg("mod_level_builder is already building a level!\n");
		return;
	}
	
	Msg("Building Map.  MR File: [%s], Layout File: [%s], Theme: [%s]\n\n", 
		szMissionRuleFile, szOutputLayoutFile, szThemeName);

	/*Code taken from TileGenDialog.cpp*/
	KeyValues *pGenerationOptions = new KeyValues("EmptyOptions");
	pGenerationOptions->Clear();
	pGenerationOptions->LoadFromFile(g_pFullFileSystem, szMissionRuleFile, GAME_DIRECTORY);	

	CLayoutSystem *pLayoutSystem = new CLayoutSystem();

	CTilegenMissionPreprocessor *pMissionPreprocessor = new CTilegenMissionPreprocessor();

	CASW_KeyValuesDatabase *pRulesDatabase = new CASW_KeyValuesDatabase();
	pRulesDatabase->LoadFiles( "tilegen/rules/" );
	for ( int i = 0; i < pRulesDatabase->GetFileCount(); ++ i )
	{
		pMissionPreprocessor->ParseAndStripRules( pRulesDatabase->GetFile( i ) );
	}
	
	if (pMissionPreprocessor->SubstituteRules(pGenerationOptions) )
	{
		KeyValues *pMissionSettings = pGenerationOptions->FindKey( "mission_settings" ); 

		if ( pMissionSettings == NULL )
		{
			Warning("Mission is missing a Global Options Block!\n");
			return;
		}

		// Copy the filename key over
		pMissionSettings->SetString( "Filename", pGenerationOptions->GetString( "Filename", "invalid_filename" ) );
					
		AddListeners( pLayoutSystem );
		if ( !pLayoutSystem->LoadFromKeyValues( pGenerationOptions ) )
		{
			Warning("Failed to load mission from pre-processed key-values.");			
			return;
		}		

		int safteyCounter = 0;
		CMapLayout *pMapLayout = NULL;
		while (pMapLayout == NULL)
		{
			pMapLayout = GenerateMapLayout(pLayoutSystem, pMissionSettings);
			safteyCounter++;
			if (safteyCounter > 10)
			{
				Warning("MOD_Level_Builder failed generating a layout from layout file [%s]", szMissionRuleFile);
				return;
			}				
		}
			
		

		char* fullPath = (char*)malloc(sizeof(char)*1024);
		strcat(fullPath, g_gamedir);
		strcat(fullPath, szOutputLayoutFile);

		Q_strcpy(pMapLayout->m_szFilename, fullPath);	

		Msg("Saving layout file to [%s]", fullPath);
		if (!pMapLayout->SaveMapLayout( fullPath ))
		{
			Warning("Failed to save Layout file to [%s]", fullPath);
			return;
		}

		if (bCompileLevel)
			CompileLevel(szOutputLayoutFile);
	}
	else
	{
		Warning("Failed Initializting Mission Preprocessor\n");
		return;
	}		
}