rcar $car = get scriptowner car

rcarmodel $bonnet = get generic model 4 of $car		//Bonnet or hood
set $bonnet scriptcontrolled 1
rcarmodel $boot = get generic model 5 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

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

string $sfxdir = "sampleplay data/cars/galant/sfx/"
string $dooropen = $sfxdir + "door_open.wav"
string $doorclosef = $sfxdir + "door_close_f.wav"
string $doorcloser = $sfxdir + "door_close_r.wav"
string $bonnetpop = $sfxdir + "bonnet_pop.wav"
string $bonnetopen = $sfxdir + "bonnet_open.wav"
string $bonnetclose = $sfxdir + "bonnet_close.wav"
string $trunkopen = $sfxdir + "trunk_open.wav"
string $trunkclose = $sfxdir + "trunk_close.wav"

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]=($open[0]+1)%3				//Toggle
      if $open[0]==1
       {send $bonnetpop to console}
      if $open[0]==2
       {send $bonnetopen to console}
      if $open[0]==0
       {send $bonnetclose to console}
    }
  }
  else {$keysdown[0]=0}					//Released

  if is key 191 pressed					//[/] - Bootlid
  {
    if $keysdown[1]==0
    {
      $keysdown[1]=1
      $open[1]=1-$open[1]
      if $open[1]==1
       {send $trunkopen to console}
      if $open[1]==0
       {send $trunkclose to console}
    }
  }
  else {$keysdown[1]=0}

  if is key 219 pressed					//[ - FL Door
  {
    if $keysdown[2]==0
    {
      $keysdown[2]=1
      $open[2]=1-$open[2]
      if $open[2]==1
       {send $dooropen to console}
      if $open[2]==0
       {send $doorclosef to console}
    }
  }
  else {$keysdown[2]=0}

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

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

  if is key 222 pressed					//["] - RR Door
  {
    if $keysdown[5]==0
    {
      $keysdown[5]=1
      $open[5]=1-$open[5]
      if $open[5]==1
       {send $dooropen to console}
      if $open[5]==0
       {send $doorcloser to console}
    }
  }
  else {$keysdown[5]=0}
}

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

  if $rotations[0]<2.0					//Not popped
  {
   if $open[0]==1 
   {
     $rotations[0] += $delta*$ms			//Raise
     if $rotations[0] > 2.0				//cap it.
      { $rotations[0] = 2.0 }
   }			
  }
  if $rotations[0]<80.0					//Not at maximum
   {if $open[0]==2 {$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]<60.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] / 200.0) * $pi	//Degrees to rads
  set $bonnet rotation float[3]{0,$bonnetrot,0}			//Rotation
  set $bonnet position float[3]{0,0.445,0.951}			//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.482,-1.525}

  float $doorflrot = -1.0 * ($rotations[2] / 210.0) * $pi
  set $doorfl rotation float[3]{$doorflrot,0,0}
  set $doorfl position float[3]{ 0.861,0.338,0.885}

  float $doorfrrot = 1.0 * ($rotations[3] / 210.0) * $pi
  set $doorfr rotation float[3]{$doorfrrot,0,0}
  set $doorfr position float[3]{-0.861,0.338,0.885}

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

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

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

  interrupt
}