// Worthless Bums, 2014-2018.

function AppCore::create (%this)
{//echo("AppCore::create()");
echo("User Data Directory =" SPC getUserDataDirectory());
	setDefaultFont();//$defaultFontSize = 16;	// Need at least this so no crashing from (font) textures at startup!
	
    // Load scripts.
	exec("./scripts/console.cs");
    exec("./scripts/constants.cs");
    exec("./scripts/defaultPreferences.cs");
    exec("./scripts/canvas.cs");
    exec("./scripts/openal.cs");
	exec("./scripts/scenewindow.cs");
	exec("./scripts/scene.cs");
    exec("./gui/guiProfiles.cs");
	// NOTE: This is from the old T2D 1.7.5 /game/main.cs - load points here!
    exec("./scripts/main.cs");
	exec("./scripts/defaultPrefs.cs");   
	exec("./scripts/xml.cs");
	exec("./scripts/properties.cs");
	exec("./scripts/common.cs");
	exec("./scripts/commonMain.cs");
	exec("./scripts/xml.cs");
	
    // Initialization.
    //initializeCanvas("Steam Marines");	// Done in common.cs!
	//initializeOpenAL();					// Done in common.cs!
	//initializeSteamAchievements();
    ModuleDatabase.loadGroup("gameBase");
	
	// Old T2D v1.7.5 (modified) start point!
	onStart();
	// New T2D v3.0 start point!
	_initProjectResources();				// main.cs (below!)
	createSceneWindow();
	createDefaultCursor();
	
	// Debug visualization.
	//myScene.setDebugOn("collision", "position", "aabb");
	
    // Load and configure the console.
    AppCore.add(TamlRead("./gui/ConsoleDialog.gui.taml"));
	GlobalActionMap.bind(keyboard, "ctrl tilde", activateConsole);
}
function AppCore::destroy (%this)
{//echo("AppCore::destroy()");
	alxStopAll();
    destroySceneWindow();
}
function AppCore::createBackground (%this)
{//echo("AppCore::createBackground()");
	%sprite = new Sprite();
	%sprite.Size = "100 75";
    %sprite.setImage("Sprites:mainMenuBackground");
	addObjectToScene(%sprite);
}
function addObjectToScene (%sprite)
{
	myScene.add(%sprite);
}
function getScriptTimer ()
{//echo("getScriptTimer()");
	return getRealTime();		// You are such a lazy shit, Bums. Just do a fucking search/replace.
}

//--------------------------------
// Old (non /game) main.cs file!
//--------------------------------
function loadPath (%path)
{//echo("loadPath()");
	setModPaths(getModPaths() @ ";" @ %path);
	exec(%path @ "/main.cs");
}
function _initProjectResources ()
{//echo("_initProjectResources()");
	//---------------------------------------------
	// Do some bootstrap voodoo to get the game to 
	// the initializeProject phase of loading and 
	// pass off to the user
	//---------------------------------------------
	
	// If your hardware/driver setup is having OpenGL issues,
	// set "$ForceOpenGL = false;" to force Steam Marines
	// to boot in D3D instead!
	// NOTE: If you force D3D there will still be an OpenGL item in the dropdown menu in setup.
	//       The converse is not true; forcing OpenGL does not supply a D3D option in setup.
	$ForceOpenGL = true;
	
	// Output a console log.
	//setLogMode(6);
	//trace(0);	// This is overriden in main.cs!

	// Frame rate limiter.
	// USE: Higher values mean lower frame rate caps.
	//		A value of 0 is the default.
	//		A value of 17 roughly caps at 60 fps.
	//		A value of 34 roughly caps at 30 fps.
	$Pref::timeManagerProcessInterval = 0;
	
	// Default game font/size used.
	setDefaultFont();
	
	//loadPath("common");
	//loadPath("game");
	//onStart();	// No longer required in T2D v3.0?
	
	// Initialized.
	//echo("\nTorque Game Builder (" @ getT2DVersion() @ ") initialized...");
	// Where the hell is the new Torque version stored?
	echo("\nTorque Game Builder (v3.0) initialized...");
	
	if (!isFunction("initializeProject")	||
		!isFunction("_initializeProject"))
	{
		messageBox("Game Startup Error", "'initializeProject' function could not be found." @
				   "\nThis could indicate a bad or corrupt common directory for your game." @
				   "\n\nThe Game will now shutdown because it cannot properly function", "Ok", "MIStop");
		quit();
	}
	
	// Start the project.
	_initializeProject();
	initializeProject();
	// Do this to solve Linux issue of cursor locked in at 800x600 when game is launched at over 800x600 resolution!
	setGameResolution($Game::Resolution, $Game::FullScreen);	// New with T2D v3.0!
	
	// Vertical Sync.
	setverticalsync($Game::VerticalSync);
}
function setDefaultFont ()
{//echo("setDefaultFont()");
	$Gui::fontCacheDirectory = expandPath("^AppCore/fonts");	// Need to strip the /1/ since "^AppCore" automatically brings you to /AppCore/1/ !
	$GameFont = "04b03";
	$defaultFontSize = 16;
	if (isFunction("populateFonts"))
	{
		populateFonts();
	}
}


//---------------------


// Steam achievement functions!
function tche ()
{//echo("tche()");
	if (!isObject($so))
	{
		$SteamCallbackDelay = 50;
		$so = new SteamObject();
		$so.initializeSteam();
		executeSteamCallbacks($so, $SteamCallbackDelay);
	}
}
function initializeSteamAchievements ()
{//echo("initializeSteamAchievements()");
	if (!isObject($so))
	{
		$SteamCallbackDelay = 50;
		$so = new SteamObject();
		$so.initializeSteam();
		executeSteamCallbacks($so, $SteamCallbackDelay);
		
		if (isObject($so))
		{
			echo("     Steam Object created!");
		}
		else
		{
			error("     Failed to initialize Steam Object!");
		}
	}
}
function executeSteamCallbacks (%so, %callbackDelay)
{//echo("executeSteamCallbacks()");
	if (isObject(%so))
	{
		%so.executeSteamCallbacks();
		schedule(%callbackDelay, 0, "executeSteamCallbacks", %so, %callbackDelay);
	}
}
function shutdownSteamAchievements ()
{//echo("shutdownSteamAchievements()");
	if (isObject($so))
	{
		$so.shutdownSteamAchievements();
	}
}
// Steam Achievement specific event triggers!
function triggerWinGameAchievements ()
{//echo("triggerWinGameAchievements()");
	if (isObject($so))
	{
		switch ($difficultyLevel)
		{
			case 0:	// Easy.
				$so.triggerSteamAchievementEasyDifficulty();
			case 1:	// Normal.
				$so.triggerSteamAchievementNormalDifficulty();
			case 2:	// Hard.
				$so.triggerSteamAchievementHardDifficulty();
			case 3:	// Brutal.
				$so.triggerSteamAchievementBrutalDifficulty();
		}
	}
}
function triggerSquadDeathAchievements ()
{//echo("triggerSquadDeathAchievements()");echo("$overworldLocation =" SPC $overworldLocation);
	schedule(1, 0, "trigger" @ strreplace($overworldLocation, " Deck", "") @ "Death");
}
function triggerKnockEnemyOutOfShip ()
{//echo("triggerKnockEnemyOutOfShip()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementKnockEnemyIntoSpace();
	}
}
function triggerLasershotKill ()
{//echo("triggerLasershotKill()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementLasershotKill();
	}
}
function triggerLaserSquad ()
{//echo("triggerLaserSquad()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementLaserSquad();
	}
}
function triggerFullSergeant ()
{//echo("triggerFullSergeant()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementFullSergeant();
	}
}
function triggerReachSergeant ()
{//echo("triggerReachSergeant()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementReachSergeant();
	}
}
function triggerCommandDeath ()
{//echo("triggerCommandDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementCommandDeath();
	}
}
function triggerEngineeringDeath ()
{//echo("triggerEngineeringDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementEngineeringDeath();
	}
}
function triggerMedicalDeath ()
{//echo("triggerMedicalDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementMedicalDeath();
	}
}
function triggerArmoryDeath ()
{//echo("triggerArmoryDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementArmoryDeath();
	}
}
function triggerBridgeDeath ()
{//echo("triggerBridgeDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementBridgeDeath();
	}
}
function triggerAlienDeath ()
{//echo("triggerAlienDeath()");
	if (isObject($so))
	{
		$so.triggerSteamAchievementAlienDeath();
	}
}