rcar $car = 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 = 15
// fudge factor to determine how fast it spins up.  Higher = slower.
float $turboinertia = 0.025
// static fric. to get the turbo spinning.  If exhaust pressure is lower than this, turbo stays at 0 rpm.
float $staticfric = 0.58
// how much air the turbo naturally pushes per rpm (subtract from exhaust pressure when spinning)
float $turbofactor = 0.0002
// how much extra intake pressure is generated per rpm
float $compressorfactor = 0.00038
// what's the max pressure it can put out? Assume above this it just goes to a wastegate or something.
float $maxpressure = 30
while 1
{
  set $car maxtorque factor 0.5
  $throttle = get $car throttle
  $maxtorque = get $car maxtorque factor
  $rpm = get $car rpm
  // 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.12*$turborpm + ($exhaustforce * interval / $turboinertia)
  if $turborpm < 0
  {
    $turborpm = 0
  }
  $pressure = $basepressure + ($turborpm * $compressorfactor)
  if $pressure > $maxpressure
  {
    $pressure = $maxpressure
  }
  if $turborpm > 150000
  {
    $turborpm = 150000
  }
  else if $pressure < $basepressure
  {
    $pressure = $basepressure
  }
  $maxtorque = $pressure / $maxpressure
  set $car maxtorque factor $maxtorque
  interrupt
}