rcar $car = get local car
rcar $localcar = get local car
// variables to use on the car
float $throttle = 0
float $maxtorque = 1
float $rpm = 0
// 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 = 20
// fudge factor to determine how fast it spins up.  Higher = slower.
float $turboinertia = 0.0032
// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
float $staticfric = 0.80
// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
float $turbofactor = 0.000025
// how much extra intake pressure is generated per rpm
float $compressorfactor = 0.00017
// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
float $maxpressure = 45
int $gear = 0
float $clutch = 0
int $free = 0
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)
  	if $turborpm < 0
  	{
    		$turborpm = 0
  	}
  	$pressure = $basepressure + ($turborpm * $compressorfactor)
  	if $pressure > $maxpressure
  	{
    		$pressure = 1.001*$maxpressure
  	}
  	else if $pressure < $basepressure
  	{
    		$pressure = $basepressure
  	}
  	if $turborpm > 200000
  	{
    		$turborpm = 200000
  	}
	if $gear = 1
        {
 		$maxtorque = $basepressure/$maxpressure
	}
	if $clutch > 0.90
	{
		$maxtorque = $basepressure/$maxpressure
	}
	else
	{
  	$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
}