function NavigateVehicle(%slot)
{
   %id = $vehicles[%slot, battleid];
   
//   echo("Navigate player vehicle ");
   
   if(%id.coords !$= "")
      %angle = t2dAngleToPoint(%id.GetPosition(), %id.coords);
   else
      %angle = t2dAngleToPoint(%id.GetPosition(), %id.target.GetPosition());   
   
   %rand = getRandom(1, 2);
   
   // if there is a building to one of the sides then turn the other way
   
   if(%rand == 1)
   {
      // right turn
      %rand = getRandom(15, 30);
      
      if(%id.rightbuilding $= 1)
         %rand = getRandom(-15, -30);
   }
   else
   {
      // left turn
      %rand = getRandom(-15, -30);

      if(%id.leftbuilding $= 1)
         %rand = getRandom(15, 30);      
   }
   
   %angle = %angle+%rand;
            
   %turnspeed = GetPlayerTurnSpeed(%slot);
   
   %id.RotateTo(%angle, %turnspeed);   
   
   %id.rotateblock = true;

   schedule($NavBlockTime, 0, "RemoveRotateBlock", %id);
}

function NavigatePlayerVehicleForRam(%slot)
{
   %id = $vehicles[%slot, battleid];

   // we need to go faster then enemy so check their current speed and up it by 10 if possible
   
   if(%id.target.currentspeed >= %id.setspeed)
   {
      %id.boardspeedchange = true;  // so the system knows the AI changed the vehicle speed
      %id.SetSpeed = %id.currentspeed;
      %id.SetSpeed = %id.SetSpeed+10;
   }   

//   echo("Navigate player vehicle ");
   
   %angle = t2dAngleToPoint(%id.GetPosition(), %id.target.GetPosition());
   
   echo("ANGLE " @ %angle);
   
   %turnspeed = GetPlayerTurnSpeed(%slot);
   
   %id.RotateTo(%angle, %turnspeed);      
}

function NavigatePlayerVehicleForBoard(%slot)
{
   %id = $vehicles[%slot, battleid];

   // we need to go faster then enemy so check their current speed and up it by 10 if possible
   
   if(%id.target.currentspeed >= %id.setspeed)
   {
      %id.boardspeedchange = true;  // so the system knows the AI changed the vehicle speed
      %id.SetSpeed = %id.currentspeed;
      %id.SetSpeed = %id.SetSpeed+10;
   }   

   %dist = VectorDist(%id.GetPosition(), %id.target.GetPosition());
   
//   echo("Navigate player vehicle ");

   %rand = getRandom(1, 2);
   
   if(%rand == 1)
      %rand = getRandom(10, 25);
   else
      %rand = getRandom(-10, -25);

   %angle = t2dAngleToPoint(%id.GetPosition(), %id.target.GetPosition());
   
   %angle = %angle+%rand;
      
   %turnspeed = GetPlayerTurnSpeed(%slot);
   
   %id.RotateTo(%angle, %turnspeed); 
   
   %id.boardnavlock = true;
   
   schedule($boardnavTime, 0, "RemoveBoardBlock", %id);        
}

// acceleration /////////////////////////////////////////////////////////////////////////
// accelerate only goes to level 5
/////////////////////////////////////////////////////////////////////////////////////////

function SchedulePlayerVehicleSpeed(%slot)
{
   %acctime = GetAccelerationTime($vehicles[%slot, battleid]);     

   // deduct some points for bad tires
   
   %factor = $vehicles[%slot, badtires]*1000;
       
   %speed = %acctime+$vehicles[%slot, badtires];
   
   schedule(%speed, 0, "AcceleratePlayerVehicle", %slot);

   $vehicles[%slot, battleid].accinprogress = true;
}

function AcceleratePlayerVehicle(%slot)
{   
   // just prevents an error caused by a vehicle being destroyed while a acc is scheduled

   if(isObject($vehicles[%slot, battleid]) $= false)
      return;

   $vehicles[%slot, battleid].currentspeed = $vehicles[%slot, battleid].currentspeed+10;
   $vehicles[%slot, battleid].setForwardSpeed(GetSpeedTranslation($vehicles[%slot, battleid].currentspeed));         

   $vehicles[%slot, battleid].accinprogress = "";
}

//////////////////////////////////////////////////////////////////////////////////////////

function ScheduleBrakePlayerVehicle(%slot)
{
   %braketime = GetBrakeTime($vehicles[%slot, vehicle], $vehicles[%slot, brakes]);
   
   // deduct some points for bad tires
   
   %factor = $vehicles[%slot, badtires]*1000;
       
   %brake = %braketime+$vehicles[%slot, badtires];
   
   schedule(%brake, 0, "BrakePlayerVehicle", %slot);

   $vehicles[%slot, battleid].brakeinprogress = true;
}

function BrakePlayerVehicle(%slot)
{
   // just prevents an error caused by a vehicle being destroyed while a brake is scheduled

   if($vehicles[%slot, battleid] $= "")
      return;

   $vehicles[%slot, battleid].currentspeed = $vehicles[%slot, battleid].currentspeed-10;
   $vehicles[%slot, battleid].setForwardSpeed(GetSpeedTranslation($vehicles[%slot, battleid].currentspeed));         

   $vehicles[%slot, battleid].brakeinprogress = "";
}

function GetPlayerTurnSpeed(%slot)
{
   // turn speed code
   
   %id = $vehicles[%slot, battleid];
       
   switch$ ($vehicles[%slot, steering])
   {
      case 1:
         %bonus = 3;
      case 2:
         %bonus = 6;
      case 3:
         %bonus = 9;
      case 4:
         %bonus = 13;
      case 5:
         %bonus = 18;
   }

   if(%id.currentspeed $= 10)
      %turn = 50+%bonus;            
   if(%id.currentspeed $= 20)
      %turn = 45+%bonus;            
   if(%id.currentspeed $= 30)
      %turn = 40+%bonus;            
   if(%id.currentspeed $= 40)
      %turn = 35+%bonus;            
   if(%id.currentspeed $= 50)
      %turn = 30+%bonus;            
   if(%id.currentspeed $= 60)
      %turn = 25+%bonus;            
   if(%id.currentspeed $= 70)
      %turn = 20+%bonus;            
   if(%id.currentspeed $= 80)
      %turn = 15+%bonus;            
   if(%id.currentspeed $= 90)
      %turn = 10+%bonus;            
   if(%id.currentspeed >= 100)
      %turn = 5+%bonus;            
   
   // for each bad tire they have deduct some points depending on what turn speed they have
   
   %reduction = $vehicles[%slot, badtires]*2;
      
   %turn = %turn-%reduction;
   
   if(%turn < 5)
      %turn = 5;
      
   return %turn;
}

// makes the vehicle go up to whatever the cruise speed is set at

function CheckPlayerVehicleSpeed(%id, %slot)
{
   %cruisespeed = GetVehicleCruiseSpeed(%id);
         
   // if there is a set speed in place that means the player set the speed of this vehicle
      
   if(%id.setspeed $= "")
   {
      if(%id.currentspeed < %cruisespeed)
         SchedulePlayerVehicleSpeed(%slot);      
   }
   else
   {
      if(%id.currentspeed < %id.setspeed)
         SchedulePlayerVehicleSpeed(%slot); 
      if(%id.currentspeed > %id.setspeed)
         ScheduleBrakePlayerVehicle(%slot);
   }
}