// the AI will automatically assign men, guns and supplies to each vehicle
// after that the player can override the settings

// Note - if a vehicle is destroyed the guns and ammo in that vehicle will be lost

function GenerateQuickPlayerVehicles()
{   
   // generate vehicle stats
   
   for(%i=1;%i<=10;%i++)
   {
      if($PlayerQuickVehicles[%i] !$= "")
      {
         %slot++;
         $vehicles[%slot, vehicle] = $PlayerQuickVehicles[%i];
         $vehicles[%slot, stockarmor] = 0;
         $vehicles[%slot, currentarmor] = 0;
         $vehicles[%slot, acceleration] = 0;
         $vehicles[%slot, brakes] = 0;
         $vehicles[%slot, steering] = 0;
         $vehicles[%slot, currenttirearmor] = 0;
         $vehicles[%slot, stocktirearmor] = 0;
         $vehicles[%slot, currentgastankarmor] = 0;
         $vehicles[%slot, stockgastankarmor] = 0;
         $vehicles[%slot, tireupgrade] = 0;
         $vehicles[%slot, currentwindowarmor] = 0;
         $vehicles[%slot, stockwindowarmor] = 0;
         $vehicles[%slot, badtires] = 0;
         $vehicles[%slot, topspeed] = GetVehicleTopSpeed($PlayerQuickVehicles[%i]);
         $vehicles[%slot, tires] = GetTires($PlayerQuickVehicles[%i]);
         $vehicles[%slot, currentramhp] = 0;                              
         $vehicles[%slot, stockramhp] = 0;                              
         $vehicles[%slot, currentvehiclehp] = GetStockVehicleHitPoints($vehicles[%slot, vehicle]);         
         $vehicles[%slot, stockvehiclehp] = GetStockVehicleHitPoints($vehicles[%slot, vehicle]);         
         $vehicles[%slot, currentenginehp] = GetStockEngineHitPoints($vehicles[%slot, vehicle]);

         // because this comes after when the cars are generated we have to update the health bars again
         
         UpdateHealthBar($vehicles[%slot, battleid]);
      }      
   }
   
   // generate personel
   
   echo("Generate Friendly Soldiers");
      
   %SeedSoldiers = friendly_soldiers.text;
      
   while(%SeedSoldiers > 0)
   {
      %i = getRandom(1, %slot);
      
      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
                  
         if(getWordCount(%id.crew) < GetVehiclePeopleCapacity($vehicles[%i, vehicle]))
         {
            %class = getRandom(1, 6);
            
            switch$ (%class)
            {
               case 1:
                  %class = "refuges";
               case 2:
                  %class = "brigands";
               case 3:
                  %class = "militia";
               case 4:
                  %class = "police";
               case 5:
                  %class = "soldiers";
               case 6:
                  %class = "mercenaries";
            }
            
            if(%id.crew $= "")
               %id.crew = %class;
            else
               %id.crew = %id.crew SPC %class;
               
            %SeedSoldiers--;
         }
      }
   }   
   
   // generate guns
   
   echo("Generate Player Guns");
      
   %SeedGuns = friendly_guns.text;
     
   while(%SeedGuns > 0)
   {
      %i = getRandom(1, %slot);

      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         %id.guns++;
         
         %SeedGuns--;      
      }
   }
   
   // generate ammo   
   
   echo("Generate Ammo");
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         %count = getWordCount(%id.crew);
         
         %id.ammo = %count*friendly_ammo.text;      
      }
   }
   
   // apply upgrades
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %control = "qbupgrade" @ %i;
         %armorgroup = %control.armorgroup;
         %systemsgroup = %control.systemsgroup;
         %ram = %control.ram;
         %mountedweapon = %control.mountedweapon;
         
         if($vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")
            $vehicles[%i, currentarmor] = GetUpgradeArmorPoints($vehicles[%i, vehicle])*%armorgroup;

         if($vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")
            $vehicles[%i, currenttirearmor] = GetUpgradeTireArmorPoints($vehicles[%i, vehicle])*%armorgroup;              

         if($vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy" && $vehicles[%i, vehicle] !$= "jeep" )
         {
            if(%ram > 0)
            {
               $vehicles[%i, currentramhp] = GetUpgradeRamPoints($vehicles[%i, vehicle])*%ram;
               AttachRam($vehicles[%i, battleid]);
            }
         }

         if($vehicles[%i, vehicle] !$= "bike" && $vehicles[%i, vehicle] !$= "dunebuggy")
            $vehicles[%i, currentwindowarmor] = GetUpgradewindowArmorPoints($vehicles[%i, vehicle])*%armorgroup;

         $vehicles[%i, tireupgrade] = %systemsgroup;              
         
         if($vehicles[%i, tireupgrade] > 3)
            $vehicles[%i, tireupgrade] = 3;
         
         $vehicles[%i, brakes] = %systemsgroup;              
         $vehicles[%i, acceleration] = %systemsgroup;              
         $vehicles[%i, steering] = %systemsgroup;              
         $vehicles[%i, currentgastankarmor] = GetUpgradeGasTankArmorPoints($vehicles[%i, vehicle])*%armorgroup;              
         $vehicles[%i, topspeed] = $vehicles[%i, topspeed]+%systemsgroup*10;
         
         if(%mountedweapon > 0)
         {
            %rand = getRandom(1, 4);
            
            switch$ (%rand)
            {
               case 1:
                     $vehicles[%i, mountedweapon] = "machinegun";
               case 2:
                     $vehicles[%i, mountedweapon] = "crossbow";
               case 3:
                     $vehicles[%i, mountedweapon] = "flamethrower";
               case 4:
                     $vehicles[%i, mountedweapon] = "rpg";                  
            }
            
            $vehicles[%i, currentmountedweaponhp] = GetUpgradeSpecialWeaponHitPoints($vehicles[%i, mountedweapon]);

         }
      }
   }
}

function SpawnQuickPlayerVehicles()
{
   // get original spawn position

   $StartX = getWord($Battlegrid.GetPosition(), 0);
   $StartY = getWord($Battlegrid.GetPosition(), 1);
   
   for(%i=1;%i<=10;%i++)
   {
      if($PlayerQuickVehicles[%i] !$= "")
         CreatePlayerVehicle($PlayerQuickVehicles[%i], %i);
   }
}