// player and enemy boarding //////////////////////////////////////////////////////////////////

function BoardOtherVehicleTB(%id)
{
   if(%id.player $= 1)
   {
      %id.boardvehicle = "";
      DisplayEnemyGang();      
      UpdateCustomOrders();      
   }
         
   %count = getWordCount(%id.crew);   
      
   if(%count <= 2)
   {
      %id.boardvehicle = "";
      return;      
   }

   AttachBoardingText(%id, %id.target);

   // decide on how many people can board which will be a random percentage of the vehicle crew
        
   %rand = getRandom(1, 3);
   
   switch$ (%rand)
   {
      case 1:
         %val = 30;
      case 2:
         %val = 40;
      case 3:
         %val = 50;
   }
   
   %val = "."@%val;
      
   %num = %count*%val;
   %num = mCeil(%num);
         
   // actual combat
   
   while(%num > 0)
   {
      %soldier = GetBoardingSoldier(%id);
      
      // find out if the soldier actually makes it across or not, get the difference in speed
      
      if(%id.currentspeed > %id.target.currentspeed)   
         %speeddiff = %id.currentspeed-%id.target.currentspeed;
      else
         %speeddiff = %id.target.currentspeed-%id.currentspeed;
      
      switch$ (%speeddiff)
      {
         case 0:
            %mod = 1;      
         case 10:
            %mod = -1;            
         case 20:
            %mod = -1;            
         case 30:
            %mod = -1;            
         case 40:
            %mod = -2;            
         case 50:
            %mod = -2;            
         case 60:
            %mod = -2;            
         case 70:      
            %mod = -3;            
         default:
            %mod = -4;      
      }
      
      
      // decide if the soldier actually makes it across or not
      
      %roll = getRandom(1, 20);
      
      // 20 is auto success
      
      if(%roll $= 1)
         %dead = true;
      else
      {
         %roll = %roll+%mod;
         
         if(%roll >= 8)
            %dead = false;
         else
            %dead = true;            
      }      
      
      %attackbonus = GetSoldierAttackBonus(%soldier);
      
      // a soldier will keep getting to attack until he dies or the vehicle is taken over
            
      while(%dead $= false)
      {         
         // decide who he is fighting

         while(%enemysoldier $= "")
         {
            // if the vehicle doesn't have anyone left decide what to do
            
            %count = trim(getWordCount(%id.target.crew));
            
            if(%count == 0)
            {
               schedule(3000,0, "CaptureVehicle", %id, %id.target, %num, %soldier);
               return;
            }
            
            %rand = getRandom(0, %count-1);
            %enemysoldier = getWord(%id.target.crew, %rand);
         }
         
         // the actual combat roll
                           
         %defensebonus = GetSoldierDefenseBonus(%enemysoldier);
         
         %defense = %defensebonus+10;
         
         %attack = getRandom(1, 20);
         
         %attack = %attack+%attackbonus;
         
         if(%attack >= %defense)
         {
            %id.target.crew = RemoveWord(%id.target.crew, %rand);
            %enemysoldier = "";
         }
         else
            %dead = true;
      }
      
      if(%dead $= true)
         %num--;
   }
}

function GetBoardingSoldierTB(%id)
{
   %soldier = getWord(%id.crew, 0);               
   %id.crew = removeWord(%id.crew, 0);
   return %soldier;
}

function AttachBoardingTextTB(%attackid, %defendid)
{
   if(%attackid.player $= 1)
   {
      playerboardingtext.visible = true;
      enemyboardedtext.visible = true;

      playerboardingtext.mount(%attackid, "0 0", 0, false, true, true, true);   
      enemyboardedtext.mount(%defendid, "0 0", 0, false, true, true, true);
   }
   else
   {
      enemyboardingtext.visible = true;
      playerboardedtext.visible = true;
      
      enemyboardingtext.mount(%attackid, "0 0", 0, false, true, true, true);   
      playerboardedtext.mount(%defendid, "0 0", 0, false, true, true, true);   
   }
   
   schedule(2000, 0, "DetachBoardingText");
}

function DetachBoardingTextTB()
{
   if(playerboardingtext.GetIsMounted() $= true)
   {
      playerboardingtext.Dismount();
      playerboardingtext.visible = false;
   }
   
   if(enemyboardedtext.GetIsMounted() $= true)   
   {
      enemyboardedtext.DisMount();
      enemyboardedtext.visible = false;
   }
   
   if(enemyboardingtext.GetIsMounted() $= true)
   {   
      enemyboardingtext.DisMount();
      enemyboardingtext.visible = false;
   }
   
   if(playerboardedtext.GetIsMounted() $= true)
   {
      playerboardedtext.DisMount();
      playerboardedtext.visible = false;
   }
}

// this happens when a vehicle gets captured. If a player or enemy has room to take on another vehicle
// they will take it, if they don't it will be destroyed
// num is how many people the attacking vehicle can still send to take over this vehicle

function CaptureVehicleTB(%attacker, %defender, %num, %soldier)
{
   // if the target vehicle was destroyed by other means somehow
   
   if(VehicleVerification(%defender) $= false)
      return;
       
   echo("CAPTURED VEHICLE");
   
   // taking a player vehicle
   
   if(%defender.player $= 1)
   {
      for(%i=1;%i<=10;%i++)
      {
         if($Enemyvehicles[%i, battleid] $= "")
            break;         
      }
      
      // they don't have any room to add a vehicle
            
      if(%i == 11)
      {
         DestroyPlayerVehicle(%defender);
         
         // also kill 1 soldier from the boarding vehicle since this soldier would have already boarded         

         %attacker.crew = RemoveWord(%id.crew, 0);         
         return;
      }
      
      // if this was the last player vehicle end the game
      
      %EndGame = true;
      
      for(%j=1;%j<=10;%j++)
      {
         if($vehicles[%j, battleid] !$= "")
            %EndGame = false;
      }
      
      if(%EndGame $= true)
      {
         EndGame();
         return;
      }
      
      %vehicle = TakePlayerVehicle(%defender, %i);
            
      DisplayEnemyGang();
      
      DisplayPlayerGang();
            
      // add the boarding soldier
      
      %defender.crew = %soldier; 

      // don't send over the vehicle capacity      
   
      %limit = GetVehiclePeopleCapacity(%vehicle.vehiclename);
      
      if(%num > %limit)
         %num = %limit-1;

      // add the rest of the boarding soldiers if any
      
      for(%j=0;%j<%num;%j++)
      {
         %soldier = GetBoardingSoldier(%attacker);
         %defender.crew = %defender.crew SPC %soldier;
      }
      
      RotateCamera();
   }
   
   // taking an enemy vehicle
      
   if(%defender.enemy $= 1)
   {
      for(%i=1;%i<=10;%i++)
      {
         if($vehicles[%i, battleid] $= "")
            break;         
      }
      
      // they don't have any room to add a vehicle
            
      if(%i == 11)
      {
         DestroyEnemyVehicle(%defender);
         
         // also kill 1 soldier from the boarding vehicle since this soldier would have already boarded         

         %attacker.crew = RemoveWord(%id.crew, 0);         
         return;
      }
      
      // tonnage allocation
      
      %currenttonnage = GetVehicleTonnage();
      %totaltonnage = GetMaxVehicleTonnage();   
      
      %vehicletonnage = GetSingleVehicleTonnage($Enemyvehicles[%defender.slot, vehicle]);
      
      %currenttonnage = %currenttonnage + %vehicletonnage;
      
      if(%currenttonnage > %totaltonnage)
      {
         DestroyEnemyVehicle(%defender);
         
         // also kill 1 soldier from the boarding vehicle since this soldier would have already boarded         

         %attacker.crew = removeWord(%id.crew, 0);         
         return;
      }
      
      %vehicle = TakeEnemyVehicle(%defender, %i);
            
      DisplayEnemyGang();
      
      DisplayPlayerGang();
      
      // add the boarding soldier
      
      %defender.crew = %soldier; 
      
      // don't send over the vehicle capacity      
   
      %limit = GetVehiclePeopleCapacity(%vehicle.vehiclename);
      
      if(%num > %limit)
         %num = %limit-1;

      // add the rest of the boarding soldiers if any
      
      for(%j=0;%j<%num;%j++)
      {
         %soldier = GetBoardingSoldier(%attacker);
         
         if(%vehicle.crew $= "")
            %vehicle.crew = %soldier;
         else
            %vehicle.crew = %vehicle.crew SPC %soldier;         
      }
   }
}

function TakePlayerVehicleTB(%id, %slot)
{
   $Enemyvehicles[%slot, currentarmor] = $vehicles[%id.slot, currentarmor];
   $Enemyvehicles[%slot, stockarmor] = $vehicles[%id.slot, stockarmor];
   $Enemyvehicles[%slot, acceleration] = $vehicles[%id.slot, acceleration];
   $Enemyvehicles[%slot, brakes] = $vehicles[%id.slot, brakes];
   $Enemyvehicles[%slot, steering] = $vehicles[%id.slot, steering];
   $Enemyvehicles[%slot, currenttirearmor] = $vehicles[%id.slot, currenttirearmor];
   $Enemyvehicles[%slot, stocktirearmor] = $vehicles[%id.slot, stocktirearmor];
   $Enemyvehicles[%slot, stockgastankarmor] = $vehicles[%id.slot, stockgastankarmor];
   $Enemyvehicles[%slot, currentgastankarmor] = $vehicles[%id.slot, currentgastankarmor];
   $Enemyvehicles[%slot, currentenginehp] = $vehicles[%id.slot, currentenginehp];   
   $Enemyvehicles[%slot, stockenginehp] = $vehicles[%id.slot, stockenginehp];   
   $Enemyvehicles[%slot, tireupgrade] = $vehicles[%id.slot, tireupgrade];
   $Enemyvehicles[%slot, badtires] = $vehicles[%id.slot, badtires];
   $Enemyvehicles[%slot, tires] = $vehicles[%id.slot, tires];
   $Enemyvehicles[%slot, stockwindowarmor] = $vehicles[%id.slot, stockwindowarmor];
   $Enemyvehicles[%slot, currentwindowarmor] = $vehicles[%id.slot, currentwindowarmor];
   $Enemyvehicles[%slot, currentramhp] = $vehicles[%id.slot, currentramhp];
   $Enemyvehicles[%slot, stockramhp] = $vehicles[%id.slot, stockramhp];
   $Enemyvehicles[%slot, currentvehiclehp] = $vehicles[%id.slot, currentvehiclehp];
   $Enemyvehicles[%slot, stockvehiclehp] = $vehicles[%id.slot, stockvehiclehp];
   $Enemyvehicles[%slot, vehicle] = $vehicles[%id.slot, vehicle];
   $Enemyvehicles[%slot, topspeed] = $vehicles[%id.slot, topspeed];
   $Enemyvehicles[%slot, mountedweapon] = $vehicles[%id.slot, mountedweapon];
   $Enemyvehicles[%slot, currentmountedweaponhp] = $vehicles[%id.slot, currentmountedweaponhp];
   $Enemyvehicles[%slot, stockmountedweaponhp] = $vehicles[%id.slot, stockmountedweaponhp];
   $Enemyvehicles[%slot, topspeedlevel] = $vehicles[%id.slot, topspeedlevel];
   $Enemyvehicles[%slot, capacitylevel] = $vehicles[%id.slot, capacitylevel];
   
   %enemyvehicle = CreateEnemyVehicle($vehicles[%id.slot, vehicle], %slot);
   
   %enemyvehicle.SetRotation(%id.GetRotation());
   %enemyvehicle.currentspeed = %id.currentspeed;
   %enemyvehicle.setForwardSpeed(GetSpeedTranslation(%id.currentspeed));   
   %enemyvehicle.SetPosition(%id.GetPosition());      
   %enemyvehicle.ammo = %id.ammo;
   $Enemyvehicles[%slot, battleid] = %enemyvehicle;
   LimitedDestroyPlayerVehicle(%id);
   
   EnemyVehicleLogic(%slot);
   
   return %enemyvehicle;
}

function TakeEnemyVehicleTB(%id, %slot)
{
   $vehicles[%slot, currentarmor] = $Enemyvehicles[%id.slot, currentarmor];
   $vehicles[%slot, stockarmor] = $Enemyvehicles[%id.slot, stockarmor];
   $vehicles[%slot, acceleration] = $Enemyvehicles[%id.slot, acceleration];
   $vehicles[%slot, brakes] = $Enemyvehicles[%id.slot, brakes];
   $vehicles[%slot, steering] = $Enemyvehicles[%id.slot, steering];
   $vehicles[%slot, currenttirearmor] = $Enemyvehicles[%id.slot, currenttirearmor];
   $vehicles[%slot, stocktirearmor] = $Enemyvehicles[%id.slot, stocktirearmor];
   $vehicles[%slot, stockgastankarmor] = $Enemyvehicles[%id.slot, stockgastankarmor];
   $vehicles[%slot, currentgastankarmor] = $Enemyvehicles[%id.slot, currentgastankarmor];
   $vehicles[%slot, currentenginehp] = $Enemyvehicles[%id.slot, currentenginehp];   
   $vehicles[%slot, stockenginehp] = $Enemyvehicles[%id.slot, stockenginehp];   
   $vehicles[%slot, tireupgrade] = $Enemyvehicles[%id.slot, tireupgrade];
   $vehicles[%slot, badtires] = $Enemyvehicles[%id.slot, badtires];
   $vehicles[%slot, tires] = $Enemyvehicles[%id.slot, tires];
   $vehicles[%slot, stockwindowarmor] = $Enemyvehicles[%id.slot, stockwindowarmor];
   $vehicles[%slot, currentwindowarmor] = $Enemyvehicles[%id.slot, currentwindowarmor];
   $vehicles[%slot, currentramhp] = $Enemyvehicles[%id.slot, currentramhp];
   $vehicles[%slot, stockramhp] = $Enemyvehicles[%id.slot, stockramhp];
   $vehicles[%slot, currentvehiclehp] = $Enemyvehicles[%id.slot, currentvehiclehp];
   $vehicles[%slot, stockvehiclehp] = $Enemyvehicles[%id.slot, stockvehiclehp];
   $vehicles[%slot, vehicle] = $Enemyvehicles[%id.slot, vehicle];
   $vehicles[%slot, topspeed] = $Enemyvehicles[%id.slot, topspeed];
   $vehicles[%slot, mountedweapon] = $Enemyvehicles[%id.slot, mountedweapon];
   $vehicles[%slot, currentmountedweaponhp] = $Enemyvehicles[%id.slot, currentmountedweaponhp];
   $vehicles[%slot, stockmountedweaponhp] = $Enemyvehicles[%id.slot, stockmountedweaponhp];
   $vehicles[%slot, topspeedlevel] = $Enemyvehicles[%id.slot, topspeedlevel];
   $vehicles[%slot, capacitylevel] = $Enemyvehicles[%id.slot, capacitylevel];
   
   %playervehicle = CreatePlayerVehicle($enemyvehicles[%id.slot, vehicle], %slot);
   
   %playervehicle.SetRotation(%id.GetRotation());
   %playervehicle.currentspeed = %id.currentspeed;
   %playervehicle.setForwardSpeed(GetSpeedTranslation(%id.currentspeed));   
   %playervehicle.SetPosition(%id.GetPosition());      
   %playervehicle.ammo = %id.ammo;
   $vehicles[%slot, battleid] = %playervehicle;
   LimitedDestroyEnemyVehicle(%id);
   
   PlayerVehicleLogic(%slot);
   
   return %playervehicle;
}

function LimitedDestroyEnemyVehicleTB(%id)
{
   cancel(%id.logic);
   
   %id.target = "";
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid] $= %id)
         $Enemyvehicles[%i, battleid] = "";
   }
   
   // if any player vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid].target $= %id)
      {
         $vehicles[%i, battleid].target = "";
         
         // eliminate custom attack orders if any
         
         $vehicles[%i, battleid].firelocation = ""; 
         
         if($CombatCamera $= %i)
         {
            UpdateCustomOrders(%id);
            UpdateEnemyGroup();
         }
      }
   }
      
   // 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)
      {
         EndQuickBattleText.setText("You have won the battle!");
         canvas.pushdialog(EndQuickBattle);
      }
      else
      {
         $combat = "";
         AfterCombat();
         return;
      }
   }
   
   if(isObject(%id) $= true)   
      %id.Safedelete();
   
   DisplayEnemyGang();
}

// called from boarding code, destroys it without actually deleting the vehicle and creating wreckage

function LimitedDestroyPlayerVehicleTB(%id)
{   
   cancel(%id.logic);
   
   // eliminat the battle id
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] $= %id)
      {
         $vehicles[%i, battleid] = "";
         $vehicles[%i, vehicle] = "";
      }
   }

   // if any enemy vehicle has this vehicle as a target now eliminate it
   
   for(%i=1;%i<=10;%i++)
   {
      if($Enemyvehicles[%i, battleid].target $= %id)
         $Enemyvehicles[%i, battleid].target = "";
   }
   
   // check count to see if this was the last player vehicle
   
   // End battle will also be used to stop ai threads
   
   $EndGame = true;
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, battleid] !$= "")
         $EndGame = false;
   }
   
   DisplayPlayerGang();
      
   if($EndGame $= true)
   {
      canvas.pushdialog(endGamemenu);
      
      for(%i=1;%i<=10;%i++)
      {
         // stop all remaining ai enemy vehicles

         if($Enemyvehicles[%i, battleid] !$= "")
            $Enemyvehicles[%i, battleid].SetAtRest();
      }
   }
   
   if(isObject(%id) $= true)
      %id.Safedelete();
}
