 
func float rotateMovable(rcarmodel $targetObject, float $targetState, float $angle, float $pos[3], float $rotMask[3], float $incRate, float $decRate, float $minAngle, float $maxAngle)
{
  // targetObject: the model to be moved.
  // targetState: usually 0 (closed) to 1 (open), where 0 represents the smaller angle.
  // angle: current angle of the object.  Should be a persistent variable.  New value will be returned.
  // pos[3]: just move the object
  // rotMask[3]: determine which axes to use by component-wise mult.
  //    (rotMaskx, rotMasky, rotMaskz) * (a,a,a) = (x,y,z)
  // eg (       0,        1,        0) * (a,a,a) = (0,a,0)
  // incRate: angular change (per ms) in radians when opening.
  // decRate: angular change (per ms) in radians when closing. note: both should be positive.
  // minAngle: min value for the angle
  // maxAngle: max value for the angle
  // return float: new angle
  
  // figure out where we want to go
  float $targetAngle = $targetState*($maxAngle-$minAngle) + $minAngle
  if $targetAngle > $angle
  {
    $angle = $angle + $incRate * interval
  }
  if $targetAngle < $angle
  {
    $angle = $angle - $decRate * interval
  }
  // now make sure it's within the valid range
  if $angle < $minAngle
  {
    $angle = $minAngle
  }
  if $angle > $maxAngle
  {
    $angle = $maxAngle
  }
  // display the object correctly
  set $targetObject position $pos
  set $targetObject rotation float[3]{$angle*$rotMask.x,$angle*$rotMask.y,$angle*$rotMask.z}
  return $angle
}

rcar $car = get scriptowner car
rcar $localcar = get local car
rcarmodel $trunk = get generic model 8 of $car
rcarmodel $ldoor = get generic model 9 of $car
rcarmodel $rdoor = get generic model 10 of $car

set $trunk scriptcontrolled 1
float $trunkstate
float $trunkpos[3] = float[3]{0, 0.35, -1.31}
int $trunkopen
int $tkeydown = 0

set $rdoor scriptcontrolled 1
float $rdoorstate
float $rdoorpos[3] = float[3]{-0.739, -0.58, 0.628}
set $rdoor position $rdoorpos
int $rdooropen
int $rkeydown = 0

set $ldoor scriptcontrolled 1
float $ldoorstate
float $ldoorpos[3] = float[3]{0.739, -0.58, 0.628}
set $ldoor position $ldoorpos
int $ldooropen
int $lkeydown = 0
while 1
{
  if $car == $localcar
  { 
    // T, key 84 - trunk
    if is key 84 pressed
    {
      if $tkeydown == 0 // toggle activation
      {
        $trunkopen = 1 - $trunkopen
        $tkeydown = 1
      }
    }
    else
    {
      $tkeydown = 0
    }
    // Up 38
    if is key 38 pressed {
      // left 37
      if is key 37 pressed {
        if $lkeydown == 0 { // toggle
          $ldooropen = 1 - $ldooropen
          $lkeydown = 1
        }
      }
      else {
        $lkeydown = 0
      }
        
      // right 39
      if is key 39 pressed {
        if $rkeydown == 0 { // toggle
          $rdooropen = 1 - $rdooropen
          $rkeydown = 1
        }
      }
      else {
        $rkeydown = 0
      }
    }
  }
 // paint $trunkpos at float[2]{30,10}
 // paint $trunkopen at float[2]{10,10}
  $trunkstate = rotateMovable($trunk, $trunkopen, $trunkstate, $trunkpos, float[3]{0.0, 1.0, 0.0}, 0.003, 0.003, 0.0, 1.2) 
  $ldoorstate = rotateMovable($ldoor, $ldooropen, $ldoorstate, $ldoorpos, float[3]{-1.0, 0.0, 0.0}, 0.002, 0.002, 0.0, 1.1)
 $rdoorstate = rotateMovable($rdoor, $rdooropen, $rdoorstate, $rdoorpos, float[3]{1.0, 0.0, 0.0}, 0.002, 0.002, 0.0, 1.0)

//  int $i = 0
//  for $i < 100 {
//    if is key $i pressed {
//      paint $i at float[2]{30,10}
//    }
//    $i = $i + 1
//  }
  interrupt
}

