rcar $car = get scriptowner car
rcar $localcar = get local 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.80   // 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
int $compteur_limiteur = 0
while 1
{
	if $car == $localcar
	{
		$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.00
		{
			$throttle = 1.00
		}
		else  if $finalthrottle < 0
		{
			$finalthrottle = 0
		}
		//Differents modes
		if is key (66) pressed // Key B
		{
			$mode ++
			if $mode !=0
			{
				$speed = $curspeed
			}
			if $mode == 0
			{
				$mode = 1
				$speed = $curspeed
			}
			wait 200
		}
		if is key (77) pressed // Key M
		{
			if $mode == 3
			{
				if $curspeed < ($speed+0.5)
				{
					$mode = 2
				}
			}
			if $mode == 4
			{
				if $curspeed < ($speed+0.5)
				{
					$mode = 1
				}
			}
		}
		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 > 4
		{
			$mode=0
		}
		if $curspeed < 30 //Cruise is automaticly disabled under 30km/h
		{
			$mode = 0
			$speed = 0    
			$finalthrottle=0
		}
		
		
		
		
		//Memory modes
		if $mode == 3 or $mode == 4
		{
			$p_mode = "En memoire"
			$unthrottle = 0
			$finalthrottle = 0
		}
		
		
		
		//Cruise control part
		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-=(10.00*$throttlerate)
		}
		if $clutch > 10.1 and $mode==1 //Disable cruise control if driver clutch
		{
			$mode = 4
		}
		if $brake > 0.1 and $mode==1 //disable cruise control is driver brakes
		{
			$mode = 4
		}
		if $throttle > 0.99 and $mode == 1 //disable cruise when driver touch the throttle
		{
			if $throttle > $finalthrottle
			{
				$compteur_limiteur++
				if $compteur_limiteur % 500 == 0
				{
					
					$mode=4
					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 = 4
		}
		
		
		
		
		
		
		
		
		
		//Speed limiter
		if $mode == 2
		{
			if ($speed+0.5) < $curspeed   //We are more than desired speed (limiter case)
			{
				$unthrottle-=$throttlerate
				if ($unthrottle < - 1)
				{
					$unthrottle=-1
				}
			}
			if $mode == 2 and $curspeed < ($speed + 0.5) //Under target speed
			{
				$unthrottle+=$throttlerate
				if $unthrottle > 0
				{
					$unthrottle = 0
				}
				if $throttle > 0.97
				{
					$compteur_limiteur ++
					if $compteur_limiteur % 500 == 0
					{
						$mode=3
						wait(1000)
					}
				}
			}
			$finalthrottle=$unthrottle
		}
		
		
		
		
		//Keys and target speeds
		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
		}
		
		
		//Throttle limitations
		if $finalthrottle > 1.10 //To avoid to much finalthrottle
		{
			$finalthrottle=1.101
		}
		if $finalthrottle < -1 //To avoid negative throttle
		{
			$finalthrottle = -1
		}
		set $car throttle $throttle+$finalthrottle
	}
	interrupt
}