function FindRandomVehicle()
{
   // if they can't take any more vehicles
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         %counter++;      
   }
   
   // if they are at capacity
   
   if(GetVehicleTonnage() $= GetMaxVehicleTonnage())
      return;
      
   if(%counter == 10)
      return;

   %list = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.city !$= "")
         %city = true;   
   }
   
   if(%city $= true)
      %chance = 8;
   else
   {      
      %distance = FindNearestCity();
      
      if(%distance >= 120)
         %chance = 4;
      if(%distance >= 60 && %distance <= 120)
         %chance = 5;
      if(%distance >= 0 && %distance < 40)
         %chance = 6;
   }
   
   switch$ ($Difficulty)
   {
      case "easy":
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance-%mod;         
   }
      
   // increase with scouts
   
   %rand = getRandom(1, 100);
   
   if(%rand <= %chance)
   {
      FindVehicle();
      return true;
   }
}

function SearchVehicles()
{
   echo("SEARCH VEHICLES CLICK");
   alxPlay(click);
   
   DisplayTutorial(vehiclesearch);
      
   // if they can't take any more vehicles
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] !$= "")
         %counter++;      
   }
   
   if(%counter == 10)
   {   
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
      
      infocontinuebutton.command = "Infowindow.Visible = false;";
      
      InfoWindow.Visible = true;
      
      %text = "You can't take any more vehicles.";

      InfoText.setText(%text);
      
      return;
   }
   
   if(UpdateTime() $= true)
      return;

   if(CheckAllEvents(search) $= true)
      return;

   %list = sg.PickPoint(player.GetPosition());
   
   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.city !$= "")
         %city = true;   
   }
   
   if(%city $= true)
      %chance = 60;
   else
   {      
      %distance = FindNearestCity();
      
      if(%distance >= 120)
         %chance = 40;
      if(%distance >= 60 && %distance < 120)
         %chance = 45;
      if(%distance >= 0 && %distance < 60)
         %chance = 50;
   }
   
   // if the player is near a highway the chances are the same
   
   %position = player.GetPosition();
   %x = getWord(%position, 0);
   %y = getWord(%position, 1);
         
   %list = sg.pickrect(%x-30 SPC %y-30, %x+30 SPC %y+30);

   %count = getWordCount(%list);
   
   for(%i=0;%i<%count;%i++)
   {
      %obj = getWord(%list, %i);
      
      if(%obj.road $= 1)
      {
         %chance = 60;
         break;         
      }
   }   

   switch$ ($Difficulty)
   {
      case "easy":
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance+%mod;         
      case "hard":      
         %mod = %chance * ".15";
         %mod = mCeil(%mod);
         %chance = %chance-%mod;         
   }
      
   // increase with scouts
   
   %chance = %chance + CheckScout();
   
   %rand = getRandom(1, 100);
   
   if(%rand <= %chance)
      FindVehicle();
   else
   {
      if(CheckForRandomPeopleEncounter(vehicle) $= true)
         return;
   }
}

// they actually found a vehicle

function FindVehicle()
{
   PromoteScout();
   
   $Vehiclefound = "";
      
   %rand = getRandom(1, 100);
   
   if(CheckScout() > 0)
      %rand = %rand+CheckScout();
      
   if(%rand < 30)
   {
      %num = getRandom(1, 3);
         
      switch$ (%num)
      {         
         case 1:
            %vehicle = "dunebuggy";           
         case 2:
            %vehicle = "bike";           
         case 3:
            %vehicle = "jeep";           
      }
   }

   if(%rand >= 30 && %rand < 60)              
   {
      %num = getRandom(1, 9);
      
      switch$ (%num)
      {
         case 1:
            %vehicle = "compact";           
         case 2:
            %vehicle = "minivan";           
         case 3:
            %vehicle = "towtruck";           
         case 4:
            %vehicle = "sportscar";           
         case 5:
            %vehicle = "van";           
         case 6:
            %vehicle = "sedan";           
         case 7:
            %vehicle = "pickuptruck";           
         case 8:
            %vehicle = "musclecar";           
         case 9:
            %vehicle = "stationwagon";           
      }
   }
   
   if(%rand >= 60 && %rand < 85)             
   {
      %num = getRandom(1, 5);
      
      switch$ (%num)
      {
         case 1:
            %vehicle = "limo";           
         case 2:
            %vehicle = "humvee";           
         case 3:
            %vehicle = "suv";           
         case 4:
            %vehicle = "cargovan";
         case 5:
            %vehicle = "ambulance";
      }
   }
   if(%rand >= 85)           
   { 
      %num = getRandom(1, 6);
      
      switch$ (%num)
      {
         case 1:
            %vehicle = "flatbedtruck";           
         case 2:
            %vehicle = "trailertruck";           
         case 3:
            %vehicle = "fireengine";           
         case 4:
            %vehicle = "garbagetruck";           
         case 5:
            %vehicle = "Construction";           
         case 6:
            %vehicle = "bus";           
      }
   }
                   
   // ask them if they want it and reset buttons
   
   InfoYesbutton.Visible = true;
   InfoNobutton.Visible = true;
   Infocontinuebutton.Visible = false;
   
   infoyesbutton.command = "AddVehicle();";
   infonobutton.command = "NoVehicle();";
         
   InfoWindow.Visible = true;
   
   // if the vehicle will have damage
   
   %rand = getRandom(1, 4);
   
   if(%rand $= 1)
      $DamagedVehicle = true;
   else
      $DamagedVehicle = false;
   
   if($DamagedVehicle $= true)
      %text = "You have found a damaged " @ GetVehicleDisplayName(%vehicle) @ ". Do you want to take it?";
   else
      %text = "You have found a " @ GetVehicleDisplayName(%vehicle) @ ". Do you want to take it?";

   InfoText.setText(%text);
   
   $foundvehicle = %vehicle;   
}

// if they don't take the vehicle

function NoVehicle()
{
   Infowindow.Visible = false;
}

// if they decide to take the vehicle

function AddVehicle()
{
   if(Infowindow.Visible $= false)
      return;
      
   // check to see if they can even take the vehicle
   
   // if they have more then 10 don't let them take it
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] $= "")
         break;
   }
   
   // if they have exceeded their tonnage

   %currenttonnage = GetVehicleTonnage();
   %totaltonnage = GetMaxVehicleTonnage();   
   
   %vehicletonnage = GetSingleVehicleTonnage($foundvehicle);
   
   %currenttonnage = %currenttonnage + %vehicletonnage;
   
   if(%currenttonnage > %totaltonnage)
   {
      InfoWindow.Visible = true;      
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
      
      if($Mechanic $= "")
      {
         InfoText.setText("You can't take this vehicle, it's exceeds your current gang tonnage capacity. If a Mechanic joins your gang the tonnage capacity will increase.");
         infocontinuebutton.command = "Infowindow.Visible = false;";      
      }
      else
      {
         InfoText.setText("You can't take this vehicle, it's exceeds your current gang tonnage capacity. As your Mechanic becomes more experienced your gang tonnage capacity will increase.");
         infocontinuebutton.command = "Infowindow.Visible = false;";      
      }
      
      return;      
   }   
   
   // if they don't have enough people
   
   %totalpeople = GetTotalNumPeople();
   
   if(%totalpeople < %i)
   {
      InfoWindow.Visible = true;      
      InfoYesbutton.Visible = false;
      InfoNobutton.Visible = false;
      Infocontinuebutton.Visible = true;
      
      InfoText.setText("You can't take this vehicle because you don't have enough soldiers to drive it.");
      infocontinuebutton.command = "Infowindow.Visible = false;";      
      return;      
   }

   // search through for an open slot
   
   for(%i=1;%i<=10;%i++)
   {
      if($vehicles[%i, vehicle] $= "")
         break;      
   }
      
   AddStockVehicle(%i);      
   
   UpdateStatsBar();
   
   Infowindow.Visible = false;
}

