//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
float $temps_coupure
float $timer_start
float $max_rpm2 = 8000
int $compteur = 0
int $gear
int $coupure = 0
float $speed
//rcarmodel $cam = get generic model 24 of $car
//set $cam scriptcontrolled 1

//Son rupteur
string $play = "sampleplay data/cars/renault_megane_ii_rs/audio/thr-off.wav"


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.187 or $indice_moteur == 0.1861
	{
		$idle = 750
		$temps_coupure = 50.0
		if $gear > 3
		{
			$max_rpm2 = 6500.0
		}
		if $gear < 2
		{
			$max_rpm2 = 6500.0
		}
		if $gear == 2 or $gear == 3
		{
			$max_rpm2 = 6950.0
		}
	}
	
	//2.0 8v F3R
	if $indice_moteur == 0.177
	{
		$idle = 850
	}
	
	//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
			}
		}
	}
	//2.9 V6 L7X
	if $indice_moteur == 0.200
	{
		if $gear == 0
		{
			$idle = 650
		}
		else
		{
			$idle = 700
		}
		if $clutch > 0.00 and $rpm < 700 and $gear !=0
		{
			$idle = 700
			if $rpm < 700
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
		}
	}
	//3.0 V6 Z7X
	if $indice_moteur == 0.223
	{
		if $gear == 0
		{
			$idle = 700
		}
		else
		{
			$idle = 850
		}
		if $clutch > 0.00 and $rpm < 850 and $gear !=0
		{
			$idle = 850
			if $rpm < 850
			{
				$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
		}
	}
	//1.9 dTi
	if $indice_moteur == 0.270
	{
		$idle = 850
	}
	//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
		$temps_coupure=1
		if $gear == 0
		{
			$max_rpm2 = 3000.0
		}
		else if $clutch > 0.50 and $gear <=2
		{
			$max_rpm2 = 3000.0
		}
		else
		{
			$max_rpm2=5200.0
		}
	}
	//2.2 dCi
	if $indice_moteur == 0.295 or $indice_moteur == 0.294
	{
		$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
		}
	}
	
	//2.2 dT
	if $indice_moteur == 0.300 or $indice_moteur == 0.301
	{
		$idle = 725.0
	}
	
	//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 and $coupure == 0
		{
			$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 is key 70 pressed and $speed < 5
	{
		//Key F to stop engine
		send "stop engine" to console
	}
	
	//rupteur
	if $rpm >= ($max_rpm2+0.0)
	{
		if $coupure == 0
		{
			//send $play to console
			$coupure = 1
			$timer_start = simtime
		}
	}
	if simtime >= ($timer_start+$temps_coupure) and $rpm < $max_rpm2
	{
		$coupure = 0
	}
	if $coupure == 1
	{
		$upthrottle=-1.0*$throttle
	}
	
	set $car throttle $finalthrottle
	interrupt
}