function StartTurnBasedBattle(%map)
{   
   $EndGame = "";
   $EndBattle = "";
   $lastCombat = $mastertime;
   $combat = true;
   $CameraLevel = 0;
   $TurnBased = true;   // for keymap
   alxStopAll();
   
   // if it's a normal battle
      
   if(%map !$= "")
   {
      $playerPosition = player.getPosition();
      UpdateTime();
   }
   
   // map will be blank if this is a quick battle
   
   if(%map!$= "")
   {
      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;
      }
   }
   
   Infowindow.Visible = false;
   
   // quickbattle the $battlegrid will already be filled in
   
   if($Battlegrid $= "")
      $Battlegrid = "battle" @ %map;
   
   echo("BATTLEGRID " @ $Battlegrid);
   
   for(%i=1;%i<=$Enemyvehicles[numvehicles];%i++)
      CreateEnemyVehicle($Enemyvehicles[%i, vehicle], %i);
   
   echo("gen vehicles");   
   GenerateEnemyPoints();
   echo("gen enemy points");
   GenerateEnemyPersonnel(); // needs to come last, battleid is needed
   echo("gen people");
   GenerateEnemyGuns(); // needs battle id too and needs personel first
   echo("gen enemy guns");

   // 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]);          
   }
   
   
   // player /////////////////////////////////////////////////////////////////////////////////////////////////
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")   
         CreatePlayerVehicle($vehicles[%i, vehicle], %i);
   }
   echo("spawn player vehicles");
   BuildPlayerVehicles();
   echo("build player vehicles");
   
   // see if there needs to be any special weapons mounted
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, mountedweapon] !$= "")
         MountSpecialWeapon($vehicles[%i, vehicle], $vehicles[%i, battleid], $vehicles[%i, mountedweapon]);          
   }
   
   // once again, don't do this if it's a quick battle, it's been done already
      
   if(%map !$= "")
   {   
      if(%addrocks $= "")
      {
         AddRocks();
         AddFoilage();
      }
   }
   
   // we can use the normal function for this
   
   PlaceVehicles();

   battlewindow.Visible = true;
   mainbar.Visible = false;
   
   canvas.popdialog(BattleConfig);
 
   UpdateBattleShortCutsTB();
  
   // call this last so it updates the battlewindow with car selected
   SetupTBCombatCamera();
   
   DisplayEnemyGang();
   DisplayPlayerGang();
   
   // hide the flags
   
   greenflag.visible = false;
   redflag.visible = false;
   
   canvas.CursorOn();   
   ChangeLoadingBar(200);
   schedule(500, 0, "KillSplashScreen");
   
   // quick battle needs this
      
   if(%map $= "")
   {
      if(isObject(moveMap) $= true)
         moveMap.delete();

      new ActionMap(QuickBattleMap);   
      QuickBattleMap.push();
      exec("~/gamescripts/quickcombat/keymap.cs");   
   }
   
   echo("START VEHICLE INIT AND NEXT TURN");
   
   VehicleInit();
   
   CallNextTurn();
      
   echo("FINISH START BATTLE FUNCTION");
}

// this will decide who goes when
// decide by simple init

function VehicleInit()
{
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
      {
         while(%placement $= "")
         {
            %slot = getRandom(1, 20);
            
            if($TurnOrder[%slot] $= "")
            { 
               $TurnOrder[%slot] = $vehicles[%i, battleid];
               break;          
            }         
         }         
      }
   }
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
      {
         while(%placement $= "")
         {
            %slot = getRandom(1, 20);
            
            if($TurnOrder[%slot] $= "")
            { 
               $TurnOrder[%slot] = $Enemyvehicles[%i, battleid];
               break;          
            }         
         }         
      }
   }
   
   $CurrentTurn = 1;
}

// decide who goes next

function CallNextTurn()
{
   while(%match $= "")
   {
      if($turnOrder[$CurrentTurn] !$= "")
         break;            
      
      $CurrentTurn++;
            
      if($CurrentTurn == 21)
         $CurrentTurn = 1;               
   }
   
   RotateTBCamera();
}

function DestroyEnemyVehicleTB(%id)
{   
   $CombatVehicles--;
   
   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();

   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();
   
   if(%id.gastankfire !$= "")
      %id.gastankfire.SafeDelete();
      
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   // add the guns and ammo it add to the pool
   
   $enemyAmmo = $enemyAmmo+%id.ammo;   
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] $= %id)
         $Enemyvehicles[%i, battleid] = "";
   }
   
   %id.enemy = "";
   %id.SetClassNameSpace("");
   %id.block = true;
   
   CreateDeathExplosion(%id);
   DisplayEnemyGang();

   // check to see if the battle is over
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have won the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         alxStop($vehicleSound);
         alxPlay(EndInfantryBattle);
         schedule(3000, 0, "AfterCombat");
         return;
      }
   }
   
   echo("END DESTROYED ENEMY VEHICLE");
}

function StopEnemyVehicleTB(%id)
{
   echo("START STOP ENEMY VEHICLE");
   
   $CombatVehicles--;

   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   %id.enemy = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($enemyvehicles[%i, battleid] $= %id)
      {
         $enemyvehicles[%i, battleid] = "";
         $enemyvehicles[%i, vehicle] = "";
      }
   }

   // End battle will also be used to stop ai threads
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayEnemyGang();

   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have won the battle!");
         canvas.pushdialog(EndQuickBattle);         
         %id.SetAtRest();
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         %id.SetAtRest();
         alxStop($vehicleSound);
         alxPlay(EndInfantryBattle);
         schedule(3000, 0, "AfterCombat");
         return;
      }
   }

   // put some dead guys around it 
   
   %rand = getRandom(1, 4);
   
   if(%id.vehiclename $= "bike")
      %rand = getRandom(1, 2);
      
   for(%i=1;%i<=%rand;%i++)
   {
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "4 4";
               layer = 6;  
            };
            
      $ObjectList = $ObjectList SPC %body;
      
      switch$ (%i)
      {
         case 1:
            %x = getWord(%id.GetPosition(), 0)-2;
            %y = getWord(%id.GetPosition(), 1)+2;         
         case 2:
            %x = getWord(%id.GetPosition(), 0)+2;
            %y = getWord(%id.GetPosition(), 1)-2;                  
         case 3:
            %x = getWord(%id.GetPosition(), 0)+4;
            %y = getWord(%id.GetPosition(), 1)-4;                  
         case 4:
            %x = getWord(%id.GetPosition(), 0)-4;
            %y = getWord(%id.GetPosition(), 1)+4;                  
      }
      
      %body.SetPosition(%x SPC %y);
   }

   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($enemyvehicles[%i, battleid] !$= "")
         %counter++;      
   }

   echo("END STOP ENEMY VEHICLE - VEHICLES LEFT " @ %counter);
}

function StopPlayerVehicleTB(%id)
{
   echo("START STOP PLAYER VEHICLE");

   $CombatVehicles--;

   $vehicles[%id.slot, currentvehiclehp] = 0;   // just so it doesn't display a negative hp number
   
   UpdateBattleWindow($CombatCamera);
   
   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);

   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();

   %id.player = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] $= %id)
      {
         $vehicles[%i, battleid] = "";
         $vehicles[%i, vehicle] = "";
      }
   }

   // End battle will also be used to stop ai threads
   
   $EndBattle= true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayPlayerGang();

   if($EndBattle $= true)
   {            
      if($quickbattle $= true)
      {
         DeleteBattle();
         %id.SetAtRest(); // stop the vehicle before we return
         EndQuickBattleText.setText("You have lost the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);
         return;
      }
      else
      {
         $combat = "";
         %id.SetatRest();
         alxStop($vehicleSound);
         EndPlayerGame();
         return;
      }
   }

   if($CombatCamera $= %id.slot)
      RotateCamera();
   
   // put some dead guys around it 
   
   %rand = getRandom(1, 4);

   if(%id.vehiclename $= "bike")
      %rand = getRandom(1, 2);
   
   for(%i=1;%i<=%rand;%i++)
   {
      %body =    new t2dStaticSprite() {
               canSaveDynamicFields = "1";
               imageMap = "deadsoldier";
               scenegraph = "sg";
               size = "4 4";
               layer = 6;  
            };
            
      switch$ (%i)
      {
         case 1:
            %x = getWord(%id.GetPosition(), 0)-2;
            %y = getWord(%id.GetPosition(), 1)+2;         
         case 2:
            %x = getWord(%id.GetPosition(), 0)+2;
            %y = getWord(%id.GetPosition(), 1)-2;                  
         case 3:
            %x = getWord(%id.GetPosition(), 0)+4;
            %y = getWord(%id.GetPosition(), 1)-4;                  
         case 4:
            %x = getWord(%id.GetPosition(), 0)-4;
            %y = getWord(%id.GetPosition(), 1)+4;                  
      }
      
      %body.SetPosition(%x SPC %y);
   }

   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         %counter++;      
   }

   echo("END STOP PLAYER VEHICLE - VEHICLES LEFT " @ %counter);
}

function DestroyPlayerVehicleTB(%id, %board)
{
   echo("START DESTROYED PLAYER VEHICLE");

   if(VehicleVerification(%id) $= false)
   {
      echo("PLAYER VEHICLE DOES NOT EXIST");
      return;      
   }
   
   $CombatVehicles--;

   $vehicles[%id.slot, currentvehiclehp] = 0;   // just so it doesn't display a negative hp number
   
   UpdateBattleWindow($CombatCamera);

   if(isObject(%id.marker) $= true)
      %id.marker.SafeDelete();
   
   if(%id.gastankexplosion !$= "")
      cancel(%id.gastankexplosion);
   
   if(%id.smoke !$= "")
      %id.smoke.SafeDelete();

   if(%id.enginefiresprite !$= "")
      %id.enginefiresprite.SafeDelete();

   if(%id.gastankfire !$= "")
      %id.gastankfire.SafeDelete();
         
   %id.player = "";
   %id.SetClassNameSpace("");
   %id.block = true;
         
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] $= %id)
      {
         $vehicles[%i, battleid] = "";
         $vehicles[%i, vehicle] = "";
      }
   }
   
   // check count to see if this was the last player vehicle
   
   // End battle will also be used to stop ai threads
   
   $EndBattle = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         $EndBattle = false;
   }
   
   DisplayPlayerGang();

   CreateDeathExplosion(%id);
      
   if($EndBattle $= true)
   {
      if($quickbattle $= true)
      {
         DeleteBattle();
         EndQuickBattleText.setText("You have lost the battle!");
         canvas.pushdialog(EndQuickBattle);
         alxStop($vehicleSound);               
         return;
      }
      else
      {
         $combat = "";
         alxStop($vehicleSound);
         EndPlayerGame();
         return;
      }
   }

   if($CombatCamera $= %id.slot)
      RotateCamera();
   
   // strictly for debug
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         %counter++;      
   }

   echo("END DESTROYED PLAYER VEHICLE - VEHICLES LEFT " @ %counter);
}


function AttachTrailerTB(%id)
{
   if(%id.vehiclename $= "flatbedtruck")   
   {
      %list = GetVehiclePolyList("flatbed_trailer");
      %name = "flatbedtrailer";
      %size = GetSize("flatbedtrailer");
   }
   
   if(%id.vehiclename $= "trailertruck")
   {
      %list = GetVehiclePolyList("trailertruck_trailer");
      %name = "trailertrucktrailer";
      %size = GetSize("trailertrucktrailer");
   }
   
   %trailer =    new t2dStaticSprite() {
                  canSaveDynamicFields = "1";
                  layer = 1; 
                  CollisionPolyList = %list;
                  scenegraph = "sg";
                  imageMap = %name;
               };
   
   %trailer.SetCollisionActive(true, true);               
   %trailer.SetSize(%size);
   %trailer.Mount(%id, "0 2", 0, true, true, true, true);   
   %id.trailer = %trailer;
   %trailer.slot = %id.slot;    
   %trailer.cab = %id;     
   %trailer.SetRotation(%id.GetRotation());
}

function UpdateBattleShortCutsTB()
{
   button_accelerate.tooltip = "Shortcut is " @ $keys_Accelerate @ " - Accelerate"; 
   button_attackcrew.ToolTip = "Shortcut is " @ $Keys_AttackCrew @ " - Attack Crew";
   button_attackengine.tooltip = "Shortcut is " @ $keys_AttackEngine @ " - Attack Engine";
   button_attackgastank.ToolTip = "Shortcut is " @ $Keys_AttackGasTank @ " - Attack Gas Tank";
   button_attackmountedweapon.tooltip = "Shortcut is " @ $Keys_AttackWeapon @ " - Attack Mounted Weapon";
   button_attacktires.tooltip = "Shortcut is " @ $Keys_AttackTires @ " - Attack Tires";
   button_boardvehicle.tooltip = "Shortcut is " @ $Keys_BoardVehicle @ " - Board Enemy Vehicle";
   button_brake.tooltip = "Shortcut is " @ $keys_Brake @ " - Brake";
   button_selecttarget.tooltip = "Shortcut is " @ $Keys_SelectTarget @ " - Select Target";
   button_ramvehicle.tooltip = "Shortcut is " @ $keys_RamVehicle @ " - Ram Vehicle";
}