//Signals
float $clutch
float $brake
shared float $esp
float $gear
float $throttle
rcar $car = get scriptowner car
rcar $localcar = get local car

//Variables
//100% unthrottle in 1 second is 0.001, 50%/sec is 0.0005 etc.
float $throttlerate = 0.005
float $unitconv = 3.60
float $finalthrottle = 0
float $corrected_throttle = 0.0
float $curspeed
int $compteur_limiteur = 0
shared string $p_mode = "---"
shared int $speed
int $mode


while 1
{
	if $car == $localcar
	{
		$curspeed = (get $car tachovelocity)*$unitconv
		$throttle = get system "car0.controls_throttle"
		$clutch = get $car clutch
		$brake = get $car brake
		$gear = get $car gear
		//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 = "---"
			$corrected_throttle = 0.0
		}
		if $mode == 1 //cruise is enabled
		{
			$p_mode = "Regulateur"
		}
		if $mode == 2 //limiter is enabled
		{
			$p_mode = "Limiteur"
		}
		if $mode > 5
		{
			$mode=0
		}
		if $curspeed < 30 //Cruise is automaticly disabled under 30km/h
		{
			$mode = 0
			$speed = 0    
			$corrected_throttle=0
		}
		
		
		
		
		//Memory modes
		if $mode == 3 or $mode == 4 or $mode == 5
		{
			$p_mode = "En memoire"
			$corrected_throttle = 0
		}
		if $mode == 5 and $speed > $curspeed
		{
			$mode = 1
		}
		
		
		
		//Cruise control part
		//We are under desired speed
		if $mode == 1  and $speed > $curspeed 
		{
			if $corrected_throttle < 1.0
			{
				$corrected_throttle+=$throttlerate
			}
		}
		
		//We are more than desired speed
		if $mode == 1 and ($speed-0.05) < $curspeed   
		{
			if $corrected_throttle > -1.0
			{
				$corrected_throttle-=$throttlerate
			}
		}
		if $mode == 1 and $speed < 1.05*$curspeed
		{
			if $corrected_throttle > -1.0
			{
				$corrected_throttle-=2.0*$throttlerate
			}
		}
		
		//Disable cruise control if driver clutch
		if $clutch > 10.1 and $mode==1
		{
			$mode = 4
		}
		
		//disable cruise control is driver brakes
		if $brake > 0.1 and $mode==1 
		{
			$mode = 4
		}

		//disable cruise when driver touch the throttle
		if $throttle > 0.05 and $mode == 1 
		{
			$compteur_limiteur++
			if $compteur_limiteur % 50 == 0
			{	
				$mode=5
				wait(200)
			}
		}
		
		//disable if Abs kicks on, or TC, and disable cruise control for lower gears
		if $esp > 0 or $gear <= 1 and $mode==1  
		{
			$mode = 4
		}
		
		
		
		
		
		
		
		
		
		//Speed limiter
		if $mode == 2
		{
			//We are more than desired speed (limiter case)
			if ($speed+0.5) < $curspeed   
			{
				if $corrected_throttle > -1.00
				{
					$corrected_throttle-=$throttlerate
				}
			}
			
			//Under target speed
			if $mode == 2 and $curspeed < ($speed + 0.5)
			{
				if $corrected_throttle < 0.00
				{
					$corrected_throttle+=$throttlerate
				}
			}
			if abs($speed-$curspeed) < 2.0 and $throttle > 0.97
			{
				$compteur_limiteur ++
				if $compteur_limiteur % 50 == 0
				{
					$mode=3
					wait(1000)
				}
			}
		}
		
		
		
		
		//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
		$finalthrottle = $throttle+$corrected_throttle
		if $finalthrottle < 0.00
		{
			$finalthrottle = 0.0
		}
		if $finalthrottle > 1.00
		{
			$finalthrottle = 1.0
		}
		
		
		set $car throttle $finalthrottle
	}
	interrupt
}