//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"
	//3.5 V6
	if $indice_moteur == 0.220
	{
		$idle = 650
		if $clutch > 0.00 and $rpm < 655
		{
			if $rpm > 500
			{
				$upthrottle+=0.08
			}
			else
			{
				$upthrottle = -1
			}
		}
	}
	//2.0T
	if $indice_moteur == 0.185
	{
		$idle = 750
	}
	//2.0 16V
	if $indice_moteur == 0.160
	{
		$idle = 725
	}
	//2.0 dCi
	if $indice_moteur == 0.291 or $indice_moteur == 0.29
	{
		$idle = 825
	}
	//dCi BVA
	if $indice_moteur == 0.292
	{
		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
			}
		}
	}
	if $rpm < $idle
	{
		$upthrottle+=0.05*$diff
	}
	if $rpm > $idle
	{
		if $upthrottle > 0
		{
			$upthrottle-=0.05*$diff
		}
	}
	if $rpm > 1.12*$idle
	{
		$upthrottle = 0
	}
	if $upthrottle > 1
	{
		$upthrottle = 1
	}
	if $upthrottle < -1
	{
		$upthrottle = -1
	}
	$finalthrottle = $throttle + $upthrottle + $unthrottle
	if $finalthrottle > 1.01
	{
		$finalthrottle = 1.01
	}
	if $finalthrottle < 0
	{
		$finalthrottle = 0
	}
	if $gear > 2 and $clutch < 0.01 and $speed < 5 and $rpm < 850
	{
		$idle*=1.10
	}
	if $clutch > 0.05
	{
		$finalthrottle = ($throttle + $upthrottle + $unthrottle)*(1.00/$maxtorque)
	}
	if $clutch <= 0.05
	{
		$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
	}
	if $finalthrottle > 1
	{
		$finalthrottle = 1
	}
	if $finalthrottle < 0
	{
		$finalthrottle = 0
	}
	set $car throttle $finalthrottle
	interrupt
}