rcar $car = get local car

rcarmodel $bonnet = get generic model 10 of $car	//Bonnet or hood
set $bonnet scriptcontrolled 1
rcarmodel $boot = get generic model 11 of $car		//Bootlid or hatch
set $boot scriptcontrolled 1
rcarmodel $doorfl = get generic model 6 of $car		//Front Left door
set $doorfl scriptcontrolled 1
rcarmodel $doorfr = get generic model 7 of $car		//Front Right door
set $doorfr scriptcontrolled 1
rcarmodel $doorrl = get generic model 8 of $car		//Rear Left door
set $doorrl scriptcontrolled 1
rcarmodel $doorrr = get generic model 9 of $car		//Rear Right door
set $doorrr scriptcontrolled 1
rcarmodel $lrev = get generic model 12 of $car   	//Reverse Lights
set $lrev scriptcontrolled 1
rcarmodel $lnum = get generic model 13 of $car   	//Plate Lights
set $lnum scriptcontrolled 1
rcarmodel $lfog = get generic model 14 of $car   	//Fog Lights
set $lfog scriptcontrolled 1

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

shared int $fogon=0					//Fog lights on
shared int $revon=0					//Reverse lights on
shared int $liton=0					//Normal 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					//[ - FL Door
  {
    if $keysdown[2]==0
    {
      $keysdown[2]=1
      $open[2]=1-$open[2]
    }
  }
  else {$keysdown[2]=0}

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

  if is key 186 pressed					//[;] - RL Door
  {
    if $keysdown[4]==0
    {
      $keysdown[4]=1
      $open[4]=1-$open[4]
    }
  }
  else {$keysdown[4]=0}

  if is key 222 pressed					//["] - RR Door
  {
    if $keysdown[5]==0
    {
      $keysdown[5]=1
      $open[5]=1-$open[5]
    }
  }
  else {$keysdown[5]=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]<75.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]<75.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}

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

  if $rotations[5]<60.0
   {if $open[5]==1 {$rotations[5] += $delta*$ms} }
  if $rotations[5]>0.0
   {if $open[5]==0 {$rotations[5] -= $delta*$ms} }
  if $rotations[5]<0
   {$rotations[5] = 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.3513,0.9369}		//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.4202,-1.7610}

  float $doorflrot = -1.0 * ($rotations[2] / 180.0) * $pi
  set $doorfl rotation float[3]{$doorflrot,0,0}
  set $doorfl position float[3]{0.9907,0.0536,0.8926}

  float $doorfrrot = 1.0 * ($rotations[3] / 180.0) * $pi
  set $doorfr rotation float[3]{$doorfrrot,0,0}
  set $doorfr position float[3]{-0.9858,0.0536,0.8926}

  float $doorrlrot = -1.0 * ($rotations[4] / 180.0) * $pi
  set $doorrl rotation float[3]{$doorrlrot,0,0}
  set $doorrl position float[3]{0.9551,0.0808,-0.3022}

  float $doorrrrot = 1.0 * ($rotations[5] / 180.0) * $pi
  set $doorrr rotation float[3]{$doorrrrot,0,0}
  set $doorrr position float[3]{-0.9495,0.0808,-0.3022}

  if $fogon == 1
  {
    set $lfog rotation float[3]{0,$bootrot,0}
    set $lfog position float[3]{0,1.0202,-2.0808}
  }
  else
  {
    set $lfog rotation float[3]{0,0,0}
    set $lfog position float[3]{0,0.75,-1.75}
  }
  if $revon == 1
  {
    set $lrev rotation float[3]{0,$bootrot,0}
    set $lrev position float[3]{0,1.0202,-2.0808}
  }
  else
  {
    set $lrev rotation float[3]{0,0,0}
    set $lrev position float[3]{0,0.65,-1.75}
  }
  if $liton == 1
  {
    set $lnum rotation float[3]{0,$bootrot,0}
    set $lnum position float[3]{0,1.0202,-2.0808}
  }
  else
  {
    set $lnum rotation float[3]{0,0,0}
    set $lnum position float[3]{0,0.65,-1.75}
  }
}

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

  interrupt
}