func float regulation(int $regime_ralenti, int $regime_maxi,rcar $car,float $rpm)
{
	float $throttle_input = get system "car0.controls_throttle"
	float $desired_rpm=(((1.0-$throttle_input)*$regime_ralenti)+($throttle_input*$regime_maxi))
	float $sortie
	//echo("regime ralenti : "+$regime_ralenti+"\n Regime maxi : "+$regime_maxi+"\n Regime voulu : "+$desired_rpm)
	//echo ($throttle_output)
	if $throttle_input < 0.1 and $rpm < $regime_ralenti
	{
		//echo "regulation ralenti"
		$sortie+=0.10
		echo $sortie
	}
	else
	{
		if ($rpm > $desired_rpm)
		{
			$sortie=$sortie-0.005
		}
		else
		{
			$sortie=$sortie+0.005
		}
		echo $sortie
	}
	//echo ("je renvoie"+$sortie)
	return $sortie
}
//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
float $acceleration = 0.00
float $desired_rpm
float $throttle_input
//rcarmodel $cam = get generic model 24 of $car
//set $cam scriptcontrolled 1
shared float $temp = 2.0
float $regime_maxi = 4000.0
int $coupure = 0
float $temps_coupure
float $timer_start
int $compteur = 0
while 1
{
	$rpm = get $car rpm
	$throttle = get $car throttle
	$clutch = get $car clutch
	float $idle = 1000.0
	$diff=($desired_rpm/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"
	$throttle_input = get system "car0.controls_throttle"
	$desired_rpm=(((1.0-$throttle_input)*$idle)+($throttle_input*$regime_maxi))
	if $rpm > 700
	{
			$temp+=0.009*((2.0*$throttle+0.01)*$rpm/50000.0)
			$compteur++
	}
	//Temperature maxi
	if $temp > 89.0
	{
		$temp = 89
	}
	
	//Moteurs essence BVM
	
	//2.0T F4R et 1.6 16V
	if $indice_moteur == 0.1998 or $indice_moteur==0.1598 or $indice_moteur==0.1398
	{
		$idle = 755.0
		$temps_coupure = 75.0
		if $indice_moteur == 0.1398
		{
			if $temp < 20
			{
				$idle = (-2.6*$temp)+1024.0
				if $idle > 1072.0
				{
					$idle = 1072.0
				}
			}
			if $temp > 20 and $temp < 40
			{
				$idle = (-2.0*$temp)+976.0
			}
			if $temp > 40.0 and $temp <= 80.0
			{
				$idle = (-5.2*$temp)+1168.0
			}
			if $temp < 60.0
			{
				$regime_maxi = 5800.0
			}
			else
			{
				$regime_maxi = 6500.0
			}
		}
		if $indice_moteur == 0.1598
		{
			if $temp < 20
			{
				$idle = (-5.15*$temp)+1047.0
				if $idle > 1047.0
				{
					$idle = 1047.0
				}
			}
			if $temp > 20 and $temp < 40
			{
				$idle = (-2.35*$temp)+944.0
			}
			if $temp > 40.0 and $temp <= 80.0
			{
				$idle = (-3.75*$temp)+1000.0
			}
			if $temp < 75.0
			{
				$regime_maxi = 5800.0
			}
			else
			{
				$regime_maxi = 6500.0
			}
		}
		if $indice_moteur == 0.1998
		{
			if $temp < 20
			{
				$idle = (-2.6*$temp)+1024.0
				if $idle > 1072.0
				{
					$idle = 1072.0
				}
			}
			if $temp > 20 and $temp < 40
			{
				$idle = (-2.0*$temp)+976.0
			}
			if $temp > 40.0 and $temp <= 80.0
			{
				$idle = (-3.6*$temp)+1040.0
			}
			if $temp < 75.0
			{
				$regime_maxi = 5900.0
			}
			if $temp > 75.0
			{
				$regime_maxi = 6000.0
			}
		}
	}
	//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.0 V6
	if $indice_moteur == 0.200
	{
		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
			}
		}
	}
	
	
	//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.2482 or $indice_moteur == 0.2486 or $indice_moteur == 0.24101 or $indice_moteur == 0.24106
	{
		$idle = 800.0
		if $indice_moteur == 0.24101 or $indice_moteur == 0.2482
		{
			$regime_maxi = 4900.0
			$idle = 805.0
		}
		if $indice_moteur == 0.2486 or $indice_moteur == 0.24106
		{
			$idle = 800.0
			$regime_maxi = 5000.0
		}
		if $gear == 0
		{
			$idle = 805.0
		}
		if $clutch < 0.15
		{
			if $gear == 2 or $gear == 3
			{
				$idle = 875.0
			}
			if $gear == 4 or $gear == 5
			{
				$idle = 900.0
			}
			if $gear > 5
			{
				$idle = 950
			}
			if $gear == 7
			{
				$idle = 1000
			}
		}
		$regime_maxi = 4900.0
		$temps_coupure = 1.0
	}
	//1.9 dCi
	if $indice_moteur == 0.280 or $indice_moteur == 0.279 or $indice_moteur == 0.278 or $indice_moteur == 0.281 or $indice_moteur == 0.2792 or $indice_moteur == 0.2795
	{
		$idle = 775
		$regime_maxi=4800.0
		$temps_coupure = 1.0
		if $indice_moteur == 0.281 or $indice_moteur == 0.280
		{
			if $clutch < 0.15
			{
				if $gear == 0
				{
					$idle = 775
				}
				if $gear == 1 or $gear>3
				{
					$idle = 870.0
				}
				if $gear == 3 and $speed > 2.5
				{
					$idle = 850.0
				}
			}
		}
	}
	//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 
	{
		$temps_coupure = 1.0
		$idle = 825.0
		$regime_maxi=5000.0
	}
	//2.2 dCi
	if $indice_moteur == 0.295
	{
		$regime_maxi=4600.0
		$idle = 850.0
		if $gear == 0
		{
			$regime_maxi = 4000.0
		}
		else
		{
			$regime_maxi = 4600.0
		}
	}
	
	//Diesel boite auto
	
	
	//2.0dCi BVA
	if $indice_moteur == 0.320 or $indice_moteur == 0.293
	{
		$regime_maxi = 5200.0
		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
			}
		}
	}
	//1.9dCi BVA
	if $indice_moteur == 0.2795
	{
		$regime_maxi=4800.0
		if $gear == 0
		{
			$idle = 800
		}
		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
			}
		}
	}
	//2.2dCi BVA
	if $indice_moteur == 0.331
	{
		$regime_maxi=4600.0
		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
			}
		}
	}
	
	//Regulation de l'acceleration
		
	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 < 0
	{
		$upthrottle = 0
	}
	if ($rpm > 600.0)
	{
		$desired_rpm=(((1.0-$throttle_input)*$idle)+($throttle_input*$regime_maxi))
		if ($throttle_input<=0.01)
		{
			if ($rpm <= $idle)
			{
				$upthrottle+=0.05*$diff
			}
			else
			{
				$upthrottle-=0.05*$diff
			}
			$acceleration=$upthrottle
		}
		else
		{
			float $test
			if ($rpm > $idle)
			{
				$upthrottle-=0.05*$diff
				$test = $desired_rpm/($regime_maxi-$idle)
				$acceleration=$test
			}
			else
			{
				$upthrottle+=0.05*$diff
				$test = ($desired_rpm/($regime_maxi-$idle))+$upthrottle
				$acceleration=$test
			}
		}
	}
	
	//Rupteur
	
	if $rpm >= ($regime_maxi+0.0)
	{
		if $coupure == 0
		{
			$coupure = 1
			$timer_start = simtime
		}
	}
	if simtime >= ($timer_start+$temps_coupure) and $rpm < $regime_maxi
	{
		if $rpm < $regime_maxi
		{
			$coupure = 0
		}
		else
		{
			$coupure = 1
			$timer_start = simtime
		}
	}
	if $coupure == 1
	{
		$acceleration=0.00
	}
	
	if ($rpm > $regime_maxi)
	{
		$acceleration=0.00
	}
	if $gear > 2 and $clutch < 0.01 and $speed < 5 and $rpm < 850
	{
		$idle*=1.10
	}
	if $rpm < 600
	{
		$upthrottle = 0.00
		$acceleration = $throttle
	}
	if is key 70 pressed and $speed < 5
	{
		//Key F to stop engine
		send "stop engine" to console
	}
	if $acceleration>1.0
	{
		$acceleration=1.0
	}
	if $acceleration < 0.0
	{
		$acceleration=0.0
	}
	set $car throttle $acceleration
	interrupt
}