// Loads a game from file.
// Worthless Bums 2012.


// Loads a saved game.
function loadSaveGame (%filespec, %callback, %currentFile)
{//echo("loadSaveGame()");

	// Get old load file name.
	getLoadFilenameold(%filespec, %callback, %currentFile);		// Where the heck is this function definition?!? Is this in engine code? This entire "loadSaveGame" function doesn't look like something I'd write!
	
	// Initialize the keybind options.
	initializeKeybindOptions();
	
	// Create the open file dialog.
	%dlg = new OpenFileDialog()
	{
		Filters = %filespec @ "|" @ %filespec;
		DefaultFile = %currentFile;
		ChangePath = false;
		MustExist = true;
		MultipleFiles = false;
	};
	
	// Check the filepath.
	if (filePath(%currentFile) !$= "")
	{
		%dlg.DefaultPath = filePath(%currentFile);
	}
	else
	{
		%userDirectory = $UserDirectory;
		%savegameDirectory = %userDirectory @ "/Worthless Bums/Steam Marines/";
		%dlg.DefaultPath = %savegameDirectory;
	}
	
	// Execute the open file dialog.
	// PROBLEM: This is invisible when in fullscreen mode!
	%dialogExecute = %dlg.Execute();
	//echo("%dialogExecute =" SPC %dialogExecute);
	
	// Check if it executed properly.
	if (%dialogExecute)
	{
		eval(%callback @ "(\"" @ %dlg.FileName @ "\");");
	}
	
	// We're done with the dialog - delete it.
	%dlg.delete();
}
function echoEngineFlags ()
{//echo("echoEngineFlags()");
	echo("ENGINE FLAGS:");
	echo("----->$EngineFlags::TextureResurrection =" SPC $EngineFlags::TextureResurrection);
	echo("----->$EngineFlags::LinuxPooling =" SPC $EngineFlags::LinuxPooling);
	echo("----->$EngineFlags::OpenGLExplicitUnload =" SPC $EngineFlags::OpenGLExplicitUnload);
	echo("----->$EngineFlags::RenderContextRevive =" SPC $EngineFlags::RenderContextRevive);
}
// Initialize our global save values to defaults.
function initializeGlobalSaveVariables ()
{//echo("initializeGlobalSaveVariables()");

	schedule(1000, 0, "echoEngineFlags");

	// DEFAULTS FOR NEW GAME
	// Statistics we track for end of level/deck/game screens!
	$GlobalStats = new ScriptObject ("Global Stats")
	{
		// These should all be implemented!
		currentTurn = 1;
		totalLevelsPassed = 0;
		totalPrimaryMissionObjectivesCompleted = 0;
		totalSecondaryMissionObjectivesCompleted = 0;
		totalGuardModes = 0;
		totalSquadMemberDeaths = 0;
		totalSurvivorsRecruited = 0;
		totalKills = 0;
		totalAmmunitionExpended = 0;
		totalAmmunitionMassExpended = 0;
		totalMeleeAttacks = 0;
		totalDamageDealt = 0;
		totalDamageTaken = 0;
		totalEnemiesKnockedIntoSpace = 0;
		totalWallsSmashed = 0;
		totalCratesSmashed = 0;
		totalDispensersUsed = 0;
		totalConsolesUsed = 0;
		totalDoorsOpened = 0;
		totalDoorsClosed = 0;
		totalItemsGrabbed = 0;
		totalItemsUsed = 0;
		totalChestsOpened = 0;
		totalLightSwitchesUsed = 0;
	};
	// END DEFAULTS.
}
// Loads the save game data into a new game.
function LoadSaveGameData (%filePath)
{//echo("LoadSaveGameData()");
	initializeKeybindOptions();
	%saveFile = new fileobject();
	%ret = %saveFile.OpenForRead(%filePath);
	if (!%ret)
	{//echo("bad save file!");
		return false;
	}
	for (%line = %saveFile.ReadLine(); !(%line $= ""); %line = %saveFile.ReadLine())
	{
		%word = GetWord(%line, 0);
		switch$ (%word)
		{
			case "playerName":	$playerName = restWords(%line);
		}
	}
	beginNewGame(false);			// This needs to be called AFTER loading the save file data into memory!
}