//Focus on the car used by the script owner
rcar $car=get scriptowner car
float $rpm
float $throttle
float $upthrottle = 0
float $unthrottle = 0
float $clutch
float $diff
float $finalthrottle
int $gear
float $speed
//rcarmodel $cam = get generic model 24 of $car
//set $cam scriptcontrolled 1
while 1
{
	$rpm = get $car rpm
	$throttle = get $car throttle
	$clutch = get $car clutch
	int $idle = 1000
	$diff=($idle/1.0*$rpm)
	$gear = get $car gear
	$speed = get $car tachovelocity
	$speed = $speed*3.60
	float $maxtorque = get $car maxtorque factor
	float $indice_moteur = get system "car0.engine.inertia"
	
	//Moteurs essence BVM
	
	
	//2.0T F4R
	if $indice_moteur == 0.185 or $indice_moteur == 0.181
	{
		$idle = 750
	}
	
	//2.0T BVA
	if $indice_moteur == 0.186
	{
		if $gear == 0
		{
			$idle = 750
		}
		else
		{
			$idle = 825
		}
		if $clutch > 0.00 and $rpm < 825 and $gear !=0
		{
			$idle = 800
			if $rpm < 825
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
		}
	}
	
	//3.5 V6
	if $indice_moteur == 0.220
	{
		if $gear == 0
		{
			$idle = 650
		}
		else
		{
			$idle = 675
		}
		if $clutch > 0.00 and $rpm < 655 and $gear !=0
		{
			$idle = 675
			if $rpm < 675
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
		}
	}
	
	//Moteur diesel BVM
	
	
	//1.5dCi
	if $indice_moteur == 0.253 or $indice_moteur == 0.254
	{
		$idle = 800
	}
	//1.9 dCi
	if $indice_moteur == 0.280 or $indice_moteur == 0.279 or $indice_moteur == 0.278
	{
		if $gear == 0
		{
			$idle = 775
		}
		else if $gear >= 4 or $speed > 2.5
		{
			$idle = 870
		}
		else if $gear <= 3
		{
			$idle = 840
		}
	}
	//2.0 dCi
	if $indice_moteur == 0.285 or $indice_moteur == 0.286 or $indice_moteur == 0.287 or $indice_moteur == 0.289 or $indice_moteur == 0.290 or $indice_moteur == 0.291 or $indice_moteur == 0.292 
	{
		$idle = 825
	}
	//2.2 dCi
	if $indice_moteur == 0.295
	{
		float $max_rpm2 = 4005.0
		$idle = 850
		if $gear == 0
		{
			if $rpm >= $max_rpm2
			{
				$unthrottle-=0.5
			}
			else if $rpm >= $max_rpm2*0.9999
			{
				$unthrottle-=0.5
			}
			else
			{
				$unthrottle = 0.00
			}
		}
		else
		{
			$unthrottle = 0.00
		}
	}
	
	//Diesel boite auto
	
	
	//2.0dCi BVA
	if $indice_moteur == 0.320 or $indice_moteur == 0.293
	{
		if $gear == 0
		{
			$idle = 825
		}
		else
		{	
			$idle = 875
		}
		if $clutch > 0.00 and $rpm < 876 and $gear !=0
		{
			if $rpm < 825
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
		}
	}
	//2.2dCi BVA
	if $indice_moteur == 0.331
	{
		if $gear == 0
		{
			$idle = 850
		}
		else
		{	
			$idle = 875
		}
		if $clutch > 0.00 and $rpm < 876 and $gear !=0
		{
			if $rpm < 875
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
		}
	}
	//3.0dCi P9X
	if $indice_moteur == 0.345
	{
		if $gear == 0
		{
			$idle = 705
		}
		else
		{	
			$idle = 752
		}
		if $clutch > 0.00 and $rpm < 752 and $gear !=0
		{
			if $rpm < 752
			{
				$upthrottle+=0.02*$diff
			}
			else
			{
				$upthrottle-=0.02*$diff
			}
		}
	}
	if $rpm < $idle
	{
		if $upthrottle < 1.0
		{
			$upthrottle+=0.05*$diff
		}
	}
	if $rpm > $idle
	{
		if $upthrottle >= -1.0
		{
			$upthrottle-=0.05*$diff
		}
		if $rpm > 1.00001*$idle
		{
			$upthrottle = 0.0
		}
	}
	$finalthrottle = $throttle + $upthrottle
	if $finalthrottle > 1.01
	{
		$finalthrottle = 1.01
	}
	if $finalthrottle < 0
	{
		$finalthrottle = 0
	}
	if $clutch > 0.05 and $rpm > 1.10*$idle
	{
		$finalthrottle = ($throttle + $upthrottle + $unthrottle )*(1.00/$maxtorque)
	}
	if $clutch <= 0.05 and $rpm > 1.10*$idle
	{
		$finalthrottle = ($throttle + $upthrottle + $unthrottle )*(1.00)
	}
	if $rpm < 600
	{
		$finalthrottle = $throttle
	}
	if $gear == 1
	{
		//set $cam position float[3]{0,0,0.0}
	}
	else
	{
		//set $cam position float[3]{0,-10,0.0}
	}
	if is key 70 pressed and $speed < 5
	{
		//Key F to stop engine
		send "stop engine" to console
	}
	set $car throttle $finalthrottle
	interrupt
}