// 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 BuildPlayerVehiclesTB()
{
   // we need to hand out men, guns and ammo
   
   %totalmen = GetTotalCombatPeople();
   %totalguns = $guns;
   %totalammo = $ammo;
      
   %refuges = $refuges;
   %brigands = $Brigands;
   %militia = $Militia;
   %police = $Police;
   %soldiers = $soldiers;
   %mercenaries = $Mercenaries;
   
   // now assign the men
   
   %i=1; // needs to reset back to zero

   $VehicleSeating = ""; // reset it so the assignment function starts fresh
   
   while(%totalmen > 0)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         if(getWordCount(%id.crew) < GetVehiclePeopleCapacity($vehicles[%i, vehicle]))
         {
            %rand = getRandom(1, 6);
            
            switch$ (%rand)
            {
               case 1:
                  if(%refuges > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "refuges";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "refuges";
                     
                     %totalmen--;
                     %refuges--;
                  }
               case 2:
                  if(%brigands > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "brigands";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "brigands";

                     %totalmen--;
                     %brigands--;
                  }
               case 3:
                  if(%militia > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "militia";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "militia";

                     %totalmen--;
                     %militia--;
                  }
               case 4:
                  if(%police > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "police";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "police";

                     %totalmen--;
                     %police--;
                  }
               case 5:
                  if(%soldiers > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "soldiers";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "soldiers";

                     %totalmen--;
                     %soldiers--;
                  }
               case 6:
                  if(%mercenaries > 0)
                  {
                     if($vehicles[%i, battleid].crew $= "")
                        $vehicles[%i, battleid].crew = "mercenaries";
                     else
                        $vehicles[%i, battleid].crew = $vehicles[%i, battleid].crew SPC "mercenaries";

                     %totalmen--;
                     %mercenaries--;
                  }
            }
         }
      }

      %i++;

      if(%i == 11)
         %i = 1;
   }
   
   echo("Men Assigned");
   
   // now assign the guns

   %totalmen = GetTotalCombatPeople();
   
   if(%totalguns > %totalmen)
      %totalguns = %totalmen;
         
   %i=1;
   
   while(%totalguns > 0)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         // add up the total men in the vehicle

         %id = $vehicles[%i, battleid];         

         if(%id.guns < getWordCount(%id.crew))
         {
            %id.guns++;
            %totalguns--;
         }
      }
      
      %i++;
      if(%i == 11)
         %i = 1;
   }
   
   echo("Guns Assigned");
      
   // now assign the ammo, pretty much keep going through the loop and assign 10 rounds per gun
   // until all the ammo has been given out
   
   %share = $ammo / GetTotalCombatPeople();   
   %share = mCeil(%share);
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         if(%id.guns !$= "")
         {
            %count = getWordCount(%id.crew);
            %id.ammo = %share*%count;
            %totalammo = %totalammo-(%share*%count);
         }
      }
   }
   
   // if there is any ammo left evenly disperse it
   
   %i=1;
      
   while(%totalammo > 0 && $guns > 0)
   {
      if($vehicles[%i, vehicle] !$= "")
      {
         %id = $vehicles[%i, battleid];
         
         if(%id.guns !$= "")
         {
            %id.ammo++;
            %totalammo--;
         }
      }

      %i++;
      
      if(%i == 11)
         %i = 1;
   }
   
   echo("Ammo Assigned");
}

function CreatePlayerVehicleTB(%name, %slot)
{
   %list = GetVehiclePolyList(%name);

   %starterpos = getRandom(1, 10000) SPC getRandom(1, 10000);  // this is needed so vehicles aren't created on top of each other
   
   %vehicle =    new t2dStaticSprite() {
                  canSaveDynamicFields = "1";
                  Position = %starterpos;
                  player = "1";
                  class = "playerVehicle";
                  layer = 3;  // the vehicle is beneath the pause text
                  CollisionPolyList = %list;
                  scenegraph = "sg";
               };
   
   %vehicle.SetCollisionActive(true, true);
   %vehicle.vehiclename = %name;
   %vehicle.SetImageMap(%name);
   %vehicle.SetForwardMovementOnly(true);
               
   $vehicles[%slot, battleid] = %vehicle;      
   
   %vehicle.slot = %slot;

   if($quickbattle $= "")
      %vehicle.topspeed = $vehicles[%slot, topspeed];
   else
      %vehicle.topspeed = GetVehicleTopSpeed(%name);
   
   // attach a ram if needed

   if($vehicles[%slot, stockramhp] > 0)
      AttachRam(%vehicle);
   
   // check for bad tires
   
   if($vehicles[%slot, badtires] > 0)
      AdjustSpeedWithBadTires(%vehicle);
                                          
   %vehicle.SetSize(GetSize(%name));
   
   MountPlayerTriggers(%vehicle);
   
   // mount bar
   
   %obj = CreateGreenBar();
   if(%name $= "flatbedtruck" || %name $= "trailertruck")
      %obj.mount(%vehicle, "0 6", 0, true, true, true, true);   
   else
      %obj.mount(%vehicle, "0 1.5", 0, true, true, true, true);   
   
   %vehicle.marker = %obj;
   
   UpdateHealthBar(%vehicle);
   
   if(%name $= "trailertruck" || %name $= "flatbedtruck")            
      AttachTrailer(%vehicle);
   
   return %vehicle;
}