function PrepQuickBattle()
{
   canvas.SetContent(loadingscreen);   
   canvas.cursorOff();
   ResetLoadingBar();
   ChangeLoadingBar(200);
   schedule(500, 0 , "StartQuickBattlePhase1");   
}

function StartQuickBattlePhase1()
{   
   $CameraLevel = 0;
   $GameStarted = true; // this is just used for the quit function
   
   Infowindow.Visible = false;
   mainbar.Visible = false;
   PeopleBattle.Visible = false;
   battlewindow.Visible = true;
   
   UpdateCustomOrders();
   UpdateBattleShortCuts();
   
   switch$(qb3terrain.text)
   {
      case "country":
         %map = getRandom(1, 6);
      case "city":
         %map = 7;      
      case "highway":
         %map = getRandom(8, 9);
   
   }
   
   switch$ (%map)
   {
      case 1:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle1.t2d"));      
      case 2:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle2.t2d"));      
      case 3:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle3.t2d"));      
      case 4:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle4.t2d"));      
      case 5:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle5.t2d"));      
      case 6:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle6.t2d"));      
      case 7:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle7.t2d"));      
         %addrocks = false;
      case 8:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle8.t2d"));      
         %addrocks = false;
      case 9:
         Screen2D.loadLevel(expandFilename("~/data/levels/battle9.t2d"));      
         %addrocks = false;
   }
   
   $Battlegrid = "battle" @ %map;
   $spawnid = getRandom(1, 4);
   $combat = true;
   $quickbattle = true;
   $EndBattle = "";
   $EndGame = "";
   $GameStarted = true;
   $TurnBased = "";

   //////////////// Scenery ////////////////////////////////////////////////////
   
   if(%addrocks $= "")
   {
      AddRocks();
      AddFoilage();
   }
      
   deleteVariables("$enemyVehicles*");
   deleteVariables("$Vehicles*");
      
   // enemy ////////////////
   SpawnQuickEnemyVehicles();
   echo("spawn enemy vehicles");

   GenerateQuickEnemyVehicles();
   echo("gen vehicles");

   SpawnQuickPlayerVehicles();
   echo("spawn player vehicles");
   
   GenerateQuickPlayerVehicles();
   echo("gen player vehicles");

   ChangeLoadingBar(300);   

   schedule(500, 0, "StartQuickBattlePhase2");
}

function StartQuickBattlePhase2()
{
   // see if there needs to be any special weapons mounted
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, mountedweapon] !$= "")
         MountSpecialWeapon($Enemyvehicles[%i, vehicle], $Enemyvehicles[%i, battleid], $Enemyvehicles[%i, mountedweapon]);          

      if($vehicles[%i, mountedweapon] !$= "")
         MountSpecialWeapon($vehicles[%i, vehicle], $vehicles[%i, battleid], $vehicles[%i, mountedweapon]);          
   }

   canvas.popdialog(BattleConfig);

   // start enemy logic 
   
   %start = 1000;
   
  for(%i=1;%i<=10;%i++)
  {
      if($Enemyvehicles[%i, vehicle] !$= "")
      {
         $Enemyvehicles[%i, battleid].SetForwardMovementOnly(true);
         $Enemyvehicles[%i, battleid].setForwardSpeed(GetSpeedTranslation(GetVehicleCruiseSpeed($Enemyvehicles[%i, battleid])));         
         $Enemyvehicles[%i, battleid].currentspeed = GetVehicleCruiseSpeed($Enemyvehicles[%i, battleid]);       
         
         schedule(%start, 0, "EnemyVehicleLogic", %i);
         %start = %start+200;
      }
      
      %start = 1000;
      
      if($Enemyvehicles[%i, mountedweapon] !$= "")
      {
         schedule(%start, 0, "EnemyAutoFireSpecialWeapon", $Enemyvehicles[%i, battleid]);
         %start=%start+100;
      }
  }
   
  // start player logic
   
   %start = 500;
   
  for(%i=1;%i<=10;%i++)
  {
      if($vehicles[%i, vehicle] !$= "")
      {
         $vehicles[%i, battleid].SetForwardMovementOnly(true);
         $vehicles[%i, battleid].setForwardSpeed(GetSpeedTranslation(GetVehicleCruiseSpeed($vehicles[%i, battleid])));         
         $vehicles[%i, battleid].currentspeed = GetVehicleCruiseSpeed($vehicles[%i, battleid]);       
         
         schedule(%start, 0, "PlayerVehicleLogic", %i);
         %start=%start+100;
      }
      
      %start = 500;
      
      if($Playervehicles[%i, mountedweapon] !$= "")
      {
         schedule(%start, 0, "PlayerAutoFireSpecialWeapon", $Playervehicles[%i, battleid]);
         %start=%start+100;
      }
  }
   setAcceptableScreenCombatRatio();
      
   DisplayEnemyGang();
   DisplayPlayerGang();
   
   PlaceVehicles(friendly_formation.text, enemy_formation.formation);
   
   // set the canvas as the last step
   
   canvas.SetCursor(GameCursor);
   canvas.SetContent(MainMap);
   canvas.CursorOn();

   // call this last so it updates the battlewindow with car selected
   SetupCombatCamera();
   
   if(isObject(moveMap) $= true)
      moveMap.delete();

   new ActionMap(QuickBattleMap);   
   QuickBattleMap.push();
   exec("./keymap.cs");   

   schedule(1000, 0, "DisplayTutorial", "VehicleBattle");

   UpdateCameraThread();   
}

// end of quick battle functions //

function ReplayQuickBattle()
{
   Screen2D.endLevel();
   PrepQuickBattle();
   canvas.popdialog(EndQuickBattle);
}

function StartNewQuickBattle()
{
   screen2D.endLevel();
   canvas.SetContent(QuickBattle);   
   canvas.popdialog(EndQuickBattle);
}

function MainMenuFromQuickBattle()
{
   $combat = "";
   $quickbattle = "";
   $EndBattle = "";
   $EndGame = "";
   Screen2D.endLevel();
   canvas.SetContent(RoadGangsMain);   
   canvas.popdialog(EndQuickBattle);
   alxStopAll();
}

function ChooseQuickBattleType()
{
   if(qb_battletype.text $= "Real Time")
      qb_battletype.setText("Turn Based");
   else
      qb_battletype.setText("Real Time");
}

