//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame()
{
   // needed for default cursor
   
   exec("./cursors.cs"); 
   exec("~/gui/RoadGangsMain.gui");

   Canvas.setContent(RoadGangsMain);
   Canvas.CursorOn();
   Canvas.setCursor(GameCursor);
   
   echo("START SCREEN ACTIVE");
   $enableDirectInput = true;
   activateDirectInput();
   enableJoystick();
   LoadDifficulty();
   
   // needs to happen before we execute the key binding script
   exec("./screens/keys.cs"); 
   
   echo("ABOUT TO LOAD SHORTCUTS");
   LoadShortCuts();
   
   echo("ABOUT TO EXEC GAME SCRIPTS");
   ExecGameScripts();

   if(GetBlackWater() !$= "Blackwater Games")
   {
      echo("THIS EXECUTABLE IS NOT COMPATIBLE");
      alxPlay(buzzer);
      quit();
   }
      
   echo("STARTUP DONE");
}

function ExecGameScripts()
{
   // GUI
   
   exec("~/gui/MainMap.gui");
   exec("~/gui/map.gui");
   exec("~/gui/savegamelist.gui");
   exec("~/gui/gangstats.gui");
   exec("~/gui/vehiclestats.gui");
   exec("~/gui/endgame.gui");
   exec("~/gui/vehicleconfig.gui");
   exec("~/gui/battleconfig.gui");
   exec("~/gui/options.gui");
   exec("~/gui/splash1.gui");
   exec("~/gui/abandonvehicle.gui");
   exec("~/gui/endgamedialog.gui");
   exec("~/gui/introdialog.gui");
   exec("~/gui/black.gui");
   exec("~/gui/displaysettings.gui");
   exec("~/gui/quickbattle1.gui");
   exec("~/gui/quickbattle2.gui");
   exec("~/gui/quickbattle3.gui");
   exec("~/gui/EndQuickBattle.gui");
   exec("~/gui/credit.gui");
   exec("~/gui/keys.gui");
   exec("~/gui/setkey.gui");
   exec("~/gui/quitgame.gui");
   exec("~/gui/dropsupplies.gui");
   exec("~/gui/tutorial.gui");
   exec("~/gui/formation.gui");
   exec("~/gui/loadingscreen.gui");
   exec("~/gui/endgamescreen.gui");
   
   // Game Scripts   
   
   exec("./startgame.cs");
   exec("./Mouse.cs");
   exec("./vehicles.cs");
   exec("./people.cs");
   exec("./time.cs");
   exec("./LoadGame.cs");
   exec("./SaveGame.cs");
   exec("./SaveGameList.cs");
   exec("./Search/searchSupplies.cs");
   exec("./Search/searchVehicles.cs");
   exec("./Search/searchPeople.cs");
   exec("./screens/vehiclestats.cs");
   exec("./consumption.cs");
   exec("./Heal.cs");
   exec("./Morale.cs");
   exec("./mapastar.cs");
   exec("./combat/enemy/enemysetup.cs");
   exec("./combat/enemy/EnemyNav.cs");
   exec("./combat/enemy/EnemyCombat.cs");
   exec("./combat/enemy/enemysensors.cs");
   exec("./combat/enemy/enemyorders.cs");
   exec("./combat/enemy/battleformation.cs");
   exec("./combat/player/playersetup.cs");
   exec("./combat/player/playercombat.cs");
   exec("./combat/player/PlayerNav.cs");
   exec("./combat/player/playerorders.cs");
   exec("./combat/player/playersensors.cs");
   exec("./combat/player/battleformation.cs");
   exec("./combat/common/combat.cs");
   exec("./combat/common/particles.cs");
   exec("./combat/common/specialweapons.cs");
   exec("./combat/common/boarding.cs");
   exec("./combat/common/collision/collisions.cs");
   exec("./combat/common/collision/support.cs");
   exec("./combat/common/afterCombat.cs");
   exec("./combat/common/SpecialWeapons.cs");
   exec("./combat/common/Scenery.cs");
   exec("./combat/common/battleformations.cs");
   exec("./combat/common/radar.cs");
   exec("./combat/common/battlescreen.cs");
   exec("./combat/infantry/Playercombat.cs");
   exec("./combat/infantry/Enemycombat.cs");
   exec("./combat/infantry/player.cs");
   exec("./combat/infantry/enemy.cs");
   exec("./combat/infantry/props.cs");
   exec("./combat/infantry/astar.cs");
   exec("./combat/infantry/common.cs");
   exec("./combat/infantry/citycapture.cs");
   exec("./nukes.cs");
   exec("./events.cs");
   exec("./fow.cs");
   exec("./quitgame.cs"); 
   exec("./tutorials/tutorial.cs");
   exec("./tutorials/infantrytutorial.cs");
   exec("./tutorials/vehicletutorial.cs");
   exec("./tutorials/intro.cs");
   exec("./screens/worldmap.cs");      
   exec("./screens/displaysettings.cs"); 
   exec("./screens/options.cs");
   exec("./screens/gangstats.cs");
   exec("./screens/vehicleupgrades.cs");
   exec("./screens/keys.cs");
   exec("./screens/Mainscreen.cs");
   exec("./screens/supplydrop.cs");
   exec("./screens/battleconfig.cs");
   exec("./screens/formations.cs");
   exec("./screens/endgame.cs");
   exec("./guiProfiles.cs");
   exec("./loadingbar.cs");
   exec("./tempfiles.cs");
   exec("./Search/SpecialLocations.cs");
   exec("./Search/SpecialSearch.cs");
   exec("./extentchange.cs");

   // quick combat
   
   exec("./quickcombat/quicksetup.cs");
   exec("./quickcombat/EnemyQuickSetup.cs");
   exec("./quickcombat/PlayerQuickSetup.cs");
   exec("./quickcombat/qb1.cs");
   exec("./quickcombat/qb2.cs");
   exec("./quickcombat/qb3.cs");
   
   // turn based

/*   
   exec("./combat/turnbased/combatTB.cs");
   exec("./combat/turnbased/boardingTB.cs");
   exec("./combat/turnbased/SpecialWeaponsTB.cs");
   exec("./combat/turnbased/player/playercombatTB.cs");
   exec("./combat/turnbased/player/playernavTB.cs");
   exec("./combat/turnbased/player/playerordersTB.cs");
   exec("./combat/turnbased/player/playersetupTB.cs");
   exec("./combat/turnbased/player/combatTB.cs");
   exec("./combat/turnbased/enemy/enemycombatTB.cs");
   exec("./combat/turnbased/enemy/enemynavTB.cs");
   exec("./combat/turnbased/enemy/enemyordersTB.cs");
   exec("./combat/turnbased/enemy/enemysetupTB.cs");
   exec("./combat/turnbased/battlescreenTB.cs");
   exec("./combat/turnbased/collision/collisionsTB.cs");
   exec("./combat/turnbased/collision/supportTB.cs");   
   */
}

function QuitGame()
{
   alxPlay(click);
   quit();   
}

//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
   Screen2D.endLevel();
}

function LoadDifficulty()
{
   %fo = new fileobject();
  
   %file = "~/difficulty.sav";

   %ret = %fo.OpenForRead(%file);
   
   if(%ret == -1)
      return;
         
   for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
   {
      %word = getWord(%line, 0);
      
      switch$(%word)
      {
         case "difficulty":
            $Difficulty = GetWord(%line, 1);
         case "autosave":
            %value = getWord(%line, 1);
            
            if(%value $= 1)
               $AutoSave = true;      
      }
   }
   
   // if there is no saved file then change it
   
   if($Difficulty $= "")
      $Difficulty = "normal";
      
   %fo.close();   
}
// this is only because of the $GameStarted variable, if we come back to the main menu for whatever reason, reset it

function RoadGangsMain::OnWake()
{
   $IntroSong = alxPlay(introsong);
   $EndGame = true;
   $GameStarted = "";   
   $combat = "";
   $pause = "";
}

function BackToWorldStart()
{
   canvas.SetContent(LoadingScreen);
   canvas.CursorOff();
   ResetLoadingBar();
   ChangeLoadingBar(300);
   schedule(1000, 0, "BackToWorldFinish");
}

function BackToWorldFinish()
{
   Screen2D.loadLevel(expandFilename("~/data/levels/main.t2d"));   
   canvas.SetContent(mainmap);
   fow.Visible = true;
   ClearFow();
   $wind = alxPlay(wind);
   mainbar.Visible = true;
   PeopleBattle.Visible = false;
   battlewindow.Visible = false;
   LoadCitiesFromTemp();
   LoadSpecialLocationsFromTemp();
   LoadNukesFromTemp();
   HideSilos();
   player.setPosition($playerPosition);
   Screen2D.mount(player);
   canvas.SetCursor(GameCursor);
   canvas.CursorOn();
   CheckSiloCapture();
   
   ChangePlayerFacing($playerFacing);   
   UpdateStatsBar();
   setAcceptableScreenRatio();
}

function ChangeIntroButton()
{
   if(getWord($selectedRes, 0) $= 800)
      introbutton.setProfile(GuiButton24FF);   
   else
      introbutton.setProfile(GuiButton28FF);   
}
