rcar $car = get scriptowner car
shared int $speed
int $mode
shared string $p_mode = "---"
float $curspeed
float $throttle
float $throttlerate = 0.001 // 100% unthrottle in 1 second is 0.001, 50%/sec is 0.0005 etc.
float $unitconv = 3.695   // Cruise is based on gauges/ABS speed, so here put how the gauges "lies" in game, if the gauges in 5% inaccurate put 3.6*1.05=3.78 etc
float $finalthrottle = 0
float $rpm
float $clutch
float $brake
float $tc
float $abs
float $gear
float $unthrottle = 0
while 1
{
  $curspeed = (get $car tachovelocity)*$unitconv
  $throttle = get $car throttle
  $rpm = get $car rpm
  $clutch = get $car clutch
  $brake = get $car brake
  $tc = get $car tc active
  $abs = get $car abs active
  $gear = get $car gear
  if $throttle > 1.05
  {
    $throttle = 1.05
  }
  else  if $finalthrottle < 0
  {
   $finalthrottle = 0
  }
  if is key (66) pressed // Key B
  {
    $mode = $mode +1
    $speed = $curspeed
    wait 200
  }
  if is key (79) pressed // Key O
  {
    $mode = 0
    wait 200
  }
  if $mode == 0 //cruise is disabled
  {
    $p_mode = "---"
    $finalthrottle=0
  }
  if $mode == 1 //cruise is enabled
  {
    $p_mode = "Regulateur"
  }
  if $mode == 2 //limiter is enabled
  {
    $p_mode = "Limiteur"
  }
  if $mode > 2
  {
  $mode=0
  }
  if $curspeed < 30 //Cruise is automaticly disabled under 30km/h
  {
     $mode = 0
     $speed = 0    
     $finalthrottle=0
  }
  if $mode == 1  and $speed > $curspeed //We are under desired speed
  {
    $finalthrottle=$finalthrottle+$throttlerate
  }
  if $mode == 1 and ($speed-0.05) < $curspeed   //We are more than desired speed
  {
   $finalthrottle-=$throttlerate
  }
  if $mode == 2 and ($speed+0.5) < $curspeed   //We are more than desired speed (limiter case)
  {
    $unthrottle=$unthrottle-0.5*$throttlerate
    if ($unthrottle < - 1)
    {
       $unthrottle=-1
    }
    $finalthrottle=$unthrottle
  }
  if $finalthrottle > 1.00 //To avoid to much finalthrottle, now max finalthrottle is 105%, the car feels more powerfull with cruise control
  {
     $finalthrottle=1.00
  }
  if $finalthrottle < -1 //To avoid negative throttle
  {
    $finalthrottle = -1
  }
  if $clutch > 10 and $mode==1 //Disable cruise control if driver clutch
  {
     $mode = 0
  }
  if $brake > 0.1 and $mode==1 //disable cruise control is driver brakes
  {
     $mode = 0
  }
  if is key 74 pressed //Key J increase target speed
  {
    $speed=$speed+2
    wait (100)
  }
  if is key 75 pressed //Key K decrease target speed
  {
    $speed=$speed-2
    wait (100)
  }
  if $speed > 200 //Avoid too much speed for cruise control (In Renault cruise controls the max is 200 and min is 30km/h, was 45km/h in earlier ones)
  {
  $speed=200
  }
  if $speed%2>0  //Only pair speeds
  {
   $speed=$speed+1
  }
  if $throttle > 0.05 and $mode == 1 //disable cruise when driver touch the throttle
  {
   if $throttle > $finalthrottle
   {
    $mode=0
   }
  }
  if $mode == 2 and ($speed+0.5)<$curspeed //disable limiter when driver touch the throttle
  {
    if $finalthrottle<-0.8
    {
     if $throttle > 0.95
     {
      $mode=0
      wait(1000)
     }
    }
  }
  if $abs > 0 or $tc > 0 or $gear <= 1 and $mode==1  //disable if Abs kicks on, or TC, and disable cruise control for lower gears
  {
  $mode = 0
  }
  set $car throttle $throttle+$finalthrottle
  interrupt
}