rcar $car = get local car
rcar $localcar = get local car
// variables to use on the car
float $throttle = 0
float $maxtorque
float $rpm = 0
// variables to use on the turbo
shared float $turborpm = 0
float $pressure = 0
float $exhaustforce = 0
//---- the rest are constants
// atmosphere is around 15 psi, right?
float $basepressure = 20
// fudge factor to determine how fast it spins up.  Higher = slower.
float $turboinertia = 0.0045
// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
float $staticfric = 1.25
// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
float $turbofactor = 0.000050
// how much extra intake pressure is generated per rpm
float $compressorfactor = 0.00040
// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
float $maxpressure = 36
int $gear = 0
float $clutch = 0
int $free = 0
float $indice_moteur
while 1
{
   if $car == $localcar
   {
	$throttle = get $car throttle
	$maxtorque = get $car maxtorque factor
	$rpm = get $car rpm
	$gear = get $car gear
	$clutch = get $car clutch
  	// calculate exhaust force coming from the engine
  	$exhaustforce = (0.1 + $throttle) * $rpm * $pressure * 0.001 / $basepressure
  	// find power compared to turbo velocity
  	$exhaustforce = ($exhaustforce - $staticfric) - ($turborpm * $turbofactor)
  	$turborpm = $turborpm + ($exhaustforce / $turboinertia)
	$indice_moteur = get system "car0.engine.inertia"
	//
	//
	//
	//
	//
	//Diesel
	//
	//
	//
	//
	//
	if $indice_moteur == 0.28
	{
																																					//Moteur 1.9dCi
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0042
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.00
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000020
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00036
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 50
		$maxtorque = 1.000
	}
	else if $indice_moteur == 0.270
	{
																																					//Moteur 1.9dTi 100
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0036
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.9
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.0000090
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00040
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 31.68
		$maxtorque = 1
	}
	else if $indice_moteur == 0.25
	{
																																					//Moteur 1.5dCi 100
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0045
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.25
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000050
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00040
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 40
		$maxtorque = 1
	}	
	else if $indice_moteur == 0.248
	{
																																					//Moteur 1.5dCi 82
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0036
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.15
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.0000090
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00040
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 37
		$maxtorque = 1
	}
	else if $indice_moteur == 0.253
	{
																																					//Moteur 1.5dCi 106
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0180
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.6
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.00000025
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00055
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 45
		$maxtorque = 1.750
	}
	else if $indice_moteur == 0.29
	{
																																					//Moteur 2.0dCi 150
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0042
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.30
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000020
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00026
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 52.3
		if $gear <= 4
		{
			$maxtorque = 1.00
		}
		else
		{
			$maxtorque = 1.00
		}
	}
	else if $indice_moteur == 0.291
	{
																																					//Moteur 2.0dCi 180
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0042
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.10
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000015
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00026
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 61.5
		if $gear <= 4
		{
			$maxtorque = 1.00
		}
		else
		{
			$maxtorque = 1.00
		}
	}
	else if $indice_moteur == 0.295 or $indice_moteur == 0.331
	{
																																					//Moteur 2.2dCi 150
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0095
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.20
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000021
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00028
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 42.67
		$maxtorque = 1.000
		if $gear <= 5 and $indice_moteur == 0.295
		{
			$maxtorque = 0.946
		}
		else
		{
			$maxtorque = 1.00
		}
	}
	else if $indice_moteur == 0.294
	{
																																					//Moteur 2.2dCi 115/130
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0095
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.20
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000021
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00028
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 43.70
		$maxtorque = 1.000
	}
	else if $indice_moteur == 0.300
	{
																																					//Moteur 2.2dT 235Nm
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0040
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 2.2
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000080
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00090
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 32.38
		$maxtorque = 1.000
	}
	else if $indice_moteur == 0.301
	{
																																					//Moteur 2.2dTv 250Nm
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0040
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.5
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000020
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00035
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 35.21
		$maxtorque = 1.000
	}
	else if $indice_moteur == 0.340
	{
																																					//Moteur 3.0dCi
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0042
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 0.90
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000012
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00026
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 32
		$maxtorque = 1.00
	}
	//
	//
	//
	//
	//
	//Petrol
	//
	//
	//
	//
	//
	else if $indice_moteur == 0.185 or $indice_moteur == 0.190
	{
																																					//Moteur 2.0T 205
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0035
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.6
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000030
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00015
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 27
		$maxtorque = 1
	}
	else if $indice_moteur == 0.1861
	{
																																					//Moteur 2.0T 225
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.00035
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.90
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000018
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00015
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		//$maxpressure = 31.41
		$maxpressure = 31.41
		$maxtorque = 1.00
	}
	else if $indice_moteur == 0.187
	{
																																					//Moteur 2.0T 230
		// fudge factor to determine how fast it spins up.  Higher = slower.
		$turboinertia = 0.0035
		// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
		$staticfric = 1.9
		// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
		$turbofactor = 0.000018
		// how much extra intake pressure is generated per rpm
		$compressorfactor = 0.00015
		// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
		$maxpressure = 32.46
		$maxtorque = 1
	}
	else
	{
		$maxpressure = 20
	}
  	if $turborpm < 0
  	{
    		$turborpm = 0
  	}
  	$pressure = $basepressure + ($turborpm * $compressorfactor)
  	if $pressure > $maxtorque*$maxpressure
  	{
    		$pressure = $maxtorque*$maxpressure
  	}
  	else if $pressure < $basepressure
  	{
    		$pressure = $basepressure
  	}
  	if $turborpm > 250000
  	{
    		$turborpm = 250000
  	}
	}
  	$maxtorque = $pressure / $maxpressure
  	set $car maxtorque factor $maxtorque
  //paint "Throttle" at float[2]{20,10}
  //paint $throttle at float[2]{100,10}
  //paint "Max Torq." at float[2]{20,30}
  //paint $maxtorque at float[2]{100,30}
  //paint "RPM" at float[2]{20,50}
  //paint $rpm at float[2]{100,50}
  //paint "Turbo RPM" at float[2]{20,90}
  //paint $turborpm at float[2]{100,90}
  interrupt
}