rcar $car = get local car
// variables to use on the car
float $throttle = 0
float $maxtorque = 1
float $rpm = 0
float $gear
// variables to use on the turbo
float $turborpm = 0
float $pressure = 0
float $exhaustforce = 0
//---- the rest are constants
// atmosphere is around 15 psi, right?
float $basepressure = 15
// fudge factor to determine how fast it spins up.  Higher = slower.
float $turboinertia = 0.080
// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
float $staticfric = 0.35
// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
float $turbofactor = 0.00015
// how much extra intake pressure is generated per rpm
float $compressorfactor = 0.00060
// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
float $maxpressure = 22.5
int $compteur = 0
float $factor = 1.00
while 1
{
  set $car maxtorque factor 0.65
  $throttle = get $car throttle
  $maxtorque = get $car maxtorque factor
  $rpm = get $car rpm
  $gear = get $car gear
  // 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 = 1.00*$turborpm + ($exhaustforce * interval / $turboinertia)
  if $turborpm < 0
  {
    $turborpm = 0
  }
  $pressure = $basepressure + ($turborpm * $compressorfactor)
  if $pressure > $factor*$maxpressure
  {
    $pressure = $factor*$maxpressure
  }
  if $gear > 2 and $gear < 6
  {
	if $throttle >= 0.25
	{
		if $rpm > 4000 and $compteur <= 2000
		{
			$factor = 1.045
			$compteur++
			if $compteur > 2000
			{
				$factor = 1.0
			}
		}
		else
		{
			$factor = 1.00
		}
		if $rpm < 3500
		{
			$compteur = 0
		}
	}
  }
  //echo $compteur
  if $turborpm > 240000
  {
    $turborpm = 240000
  }
  else if $pressure < $basepressure
  {
    $pressure = $basepressure
  }
  $maxtorque = $pressure / $maxpressure
  set $car maxtorque factor $maxtorque
  //paint "Throttle" at float[2]{520,10}
  //paint $compressorfactor at float[2]{600,10}
  //paint "Max Torq." at float[2]{520,30}
  //paint $maxtorque at float[2]{600,30}
  //paint "RPM" at float[2]{520,50}
  //paint $rpm at float[2]{600,50}
  //paint "Turbo RPM" at float[2]{520,90}
  //paint $turborpm at float[2]{600,90}
  interrupt
}