rcar $car = get local car

rcarmodel $bonnet = get generic model 7 of $car		//Bonnet or hood
set $bonnet scriptcontrolled 1
rcarmodel $boot = get generic model 8 of $car		//Bootlid or hatch
set $boot scriptcontrolled 1
rcarmodel $doorl = get generic model 9 of $car		//Left door
set $doorl scriptcontrolled 1
rcarmodel $doorr = get generic model 10 of $car		//Right door
set $doorr scriptcontrolled 1
rcarmodel $lnum = get generic model 6 of $car   	//Plate Lights
set $lnum scriptcontrolled 1
rcarmodel $idkdoors = get generic model 11 of $car	//Doors indicator
set $idkdoors scriptcontrolled 1
rcarmodel $idktrunk = get generic model 12 of $car	//Trunk indicator
set $idktrunk scriptcontrolled 1

int $open[4] = int[4]{0,0,0,0}				//Are they open or closed
float $rotations[4] = float[4]{0,0,0,0}			//Their current position
int $keysdown[4] = int[4]{0,0,0,0}			//Keys held down

shared int $liton=0					//Lights on

float $pi = acos(-1)					//3.14159...
float $delta = 0.1					//60 degrees in 600ms

func void CheckInput()
{
  if is key 220 pressed					//[\] - Bonnet
  {
    if $keysdown[0]==0					//First Press
    {
      $keysdown[0]=1					//Mark as held down
      $open[0]=1-$open[0]				//Toggle
    }
  }
  else {$keysdown[0]=0}					//Released

  if is key 191 pressed					//[/] - Bootlid
  {
    if $keysdown[1]==0
    {
      $keysdown[1]=1
      $open[1]=1-$open[1]
    }
  }
  else {$keysdown[1]=0}

  if is key 219 pressed					//[ - L Door
  {
    if $keysdown[2]==0
    {
      $keysdown[2]=1
      $open[2]=1-$open[2]
    }
  }
  else {$keysdown[2]=0}

  if is key 221 pressed					//] - R Door
  {
    if $keysdown[3]==0
    {
      $keysdown[3]=1
      $open[3]=1-$open[3]
    }
  }
  else {$keysdown[3]=0}
}

func void UpdateRotations()
{
  int $ms = interval					//Frame interval

  if $rotations[0]<75.0					//Not at maximum
   {if $open[0]==1 {$rotations[0] += $delta*$ms} }	//Raise
  if $rotations[0]>0.0					//Not at minimum
   {if $open[0]==0 {$rotations[0] -= $delta*$ms} }	//Lower
  if $rotations[0]<0					//Cap at 0
   {$rotations[0] = 0.0}

  if $rotations[1]<75.0
   {if $open[1]==1 {$rotations[1] += $delta*$ms} }
  if $rotations[1]>0.0
   {if $open[1]==0 {$rotations[1] -= $delta*$ms} }
  if $rotations[1]<0
   {$rotations[1] = 0.0}

  if $rotations[2]<60.0
   {if $open[2]==1 {$rotations[2] += $delta*$ms} }
  if $rotations[2]>0.0
   {if $open[2]==0 {$rotations[2] -= $delta*$ms} }
  if $rotations[2]<0
   {$rotations[2] = 0.0}

  if $rotations[3]<60.0
   {if $open[3]==1 {$rotations[3] += $delta*$ms} }
  if $rotations[3]>0.0
   {if $open[3]==0 {$rotations[3] -= $delta*$ms} }
  if $rotations[3]<0
   {$rotations[3] = 0.0}
}

func void SetModels()
{
  float $bonnetrot = -1.0 * ($rotations[0] / 180.0) * $pi	//Degrees to rads
  set $bonnet rotation float[3]{0,$bonnetrot,0}			//Rotation
  set $bonnet position float[3]{0,0.4668,1.289}			//Offset

  float $bootrot = 1.0 * ($rotations[1] / 180.0) * $pi
  set $boot rotation float[3]{0,$bootrot,0}
  set $boot position float[3]{0,0.6141,-1.8678}

  float $doorlrot = -1.0 * ($rotations[2] / 180.0) * $pi
  set $doorl rotation float[3]{$doorlrot,0,0}
  set $doorl position float[3]{0.9838,0.2317,0.9087}

  float $doorrrot = 1.0 * ($rotations[3] / 180.0) * $pi
  set $doorr rotation float[3]{$doorrrot,0,0}
  set $doorr position float[3]{-0.9838,0.2317,0.9087}

  if $liton == 1
  {
    set $lnum rotation float[3]{0,$bootrot,0}
    set $lnum position float[3]{0,0.6141,-1.8678}
  }
  else
  {
    set $lnum rotation float[3]{0,0,0}
    set $lnum position float[3]{0,0.35,-1.75}
  }

  int $doorsopen = $open[2] + $open[3]
  if $doorsopen > 0
    { set $idkdoors position float[3]{0.0,0.0,0.0} }
  else 
    { set $idkdoors position float[3]{0.0,0.0,0.2} }

  int $trunkopen = $open[0] + $open[1]
  if $trunkopen > 0
    { set $idktrunk position float[3]{0.0,0.0,0.0} }
  else 
    { set $idktrunk position float[3]{0.0,0.0,0.2} }
}

while 1
{
  CheckInput()
  UpdateRotations()
  SetModels()

  interrupt
}