rcar $car = get local car	//get car

rcarmodel $ifl = get generic model 3 of $car   //Front Left
set $ifl  scriptcontrolled 1
rcarmodel $ifr = get generic model 3 of $car   //Front Right
set $ifr scriptcontrolled 1
rcarmodel $irl = get generic model 3 of $car   //Rear Left
set $irl  scriptcontrolled 1
rcarmodel $irr = get generic model 3 of $car   //Rear Right
set $irr scriptcontrolled 1
rcarmodel $iil = get generic model 3 of $car   //Inside Left
set $iil  scriptcontrolled 1
rcarmodel $iir = get generic model 3 of $car   //Inside Right
set $iir scriptcontrolled 1
rcarmodel $fogf = get generic model 0 of $car //Fog Lights - front
set $fogf scriptcontrolled 1
rcarmodel $fogi = get generic model 0 of $car //Fog Lights - indicator
set $fogi scriptcontrolled 1
rcarmodel $hbml = get generic model 1 of $car //High Beam  - Lights
set $hbml scriptcontrolled 1
rcarmodel $hbmi = get generic model 1 of $car //High Beam  - Indicator
set $hbmi scriptcontrolled 1

int $ldown=0
int $rdown=0
int $hdown=0
int $fdown=0
int $litdown=0
int $hbmdown=0

int $lefton=0			//Are they on?
int $righton=0
int $hazon=0
int $hbeamon=0
shared int $fogon=0
shared int $revon=0
shared int $liton=0

int $duration=0			//Stay lit duration, max 500ms
int $direction=1		//Lit or Pause
string $play = "sampleplay data/cars/vw_golf_mk3/audio/blinker.wav"

int $gear=0
int $lastgear=0			//Last known gear

func void CheckInput()		//Check for input
{
  if is key 84 pressed		//Hazards key [T]
  {
    if $hdown == 0		//First press
    {
      $hdown = 1		//Mark as held down
      if $hazon > 0		//If hazards on,
      {
        $lefton = 0		//Cancel them
        $righton = 0
        $hazon = 0
      }
      else
      {
        $lefton = 1		//Turn them on
        $righton = 1
        $hazon = 1
        send $play to console
      }
    }
  }
  else
  {
    $hdown = 0			//Mark as released
  }

  if $hazon
  {
    return
  }

  if is key 81 pressed		//Left signal key [Q]
  {
    if $ldown == 0		//First press
    {
      $ldown = 1		//Mark as held down
      if $lefton == 1
      {
        $lefton = 0		//If On, cancel it
      }
      else 
      {
         $lefton = 1		//Turn it on 
         $righton = 0		//Cancel oposite signal
         send $play to console
      }
    }
  }
  else
  {
    $ldown = 0			//Mark as released
  }

  if is key 69 pressed		//Right signal key [E]
  {
    if $rdown == 0		//First press
    {
      $rdown = 1		//Mark as held down
      if $righton == 1
      {
        $righton = 0		//If On, cancel it
      }
      else 
      {
         $righton = 1		//Turn it on 
         $lefton = 0		//Cancel oposite signal
         send $play to console
      }
    }
  }
  else
  {
    $rdown = 0			//Mark as released
  }

  if is key 75 pressed		//Fog lights key [k]
  {
    if $fdown == 0		//First press
    {
      $fdown = 1		//Mark as held down
      $fogon = 1 - $fogon	//Toggle
    }
  }
  else
  {
    $fdown = 0			//Mark as released
  }

  if is key 70 pressed		//High Beam key [F]
  {
    if $hbmdown == 0		//First press
    {
      $hbmdown = 1		//Mark as held down
      $hbeamon = 1 - $hbeamon	//Toggle
    }
  }
  else
  {
    $hbmdown = 0		//Mark as released
  }

  if is key 76 pressed		//Lights key [L]
  {
    if $litdown == 0		//First press
    {
      $litdown = 1		//Mark as held down
      $liton = 1 - $liton	//Toggle
    }
  }
  else
  {
    $litdown = 0		//Mark as released
  }
}

func void CheckReverse()	//Check for reverse
{
  $gear = get $car gear		//If reverse selected,
  if $gear == 1			//Turn on both signals (hazard lights)
  {
    $lefton = 0
    $righton = 0
    $revon = 1
  }
  else if $lastgear == 1	//Just came out of reverse? 
    {
      $lefton = 0		//Reset them
      $righton = 0
      $revon = 0
    }
  $lastgear = $gear		//update last gear
}

func void TimeBlinking()	//Compute blinking time
{
  int $oneactive = $lefton + $righton
  if $oneactive > 0		//Least one of them active
  {
				//Compute time passed
    $duration = $duration + interval * $direction

    if $duration > 500		//Has been lit for half a sec  
    { 
      $duration = 500 		//Cap it
      $direction = -1		//Switch bulb off
    }
    if $duration < 0 		//Has been sleeping for half a sec
    { 
      $duration = 0		//Cap it  
      $direction =  1		//Turn bulb on
      send $play to console
    }
  }
  else				//None is active
  {
    $duration = 0
    $direction = 1
  }
}
func void ShowSignals()		//Show models
{
  if $lefton + $direction > 1	//On and lit, show 3d model
  {
    set $ifl position float[3]{0,0,0}
    set $irl position float[3]{0,0,0}
    set $iil position float[3]{0,0,0}
  }
  else				//Hide it
  {
    set $ifl position float[3]{0,-9,-0.00}
    set $irl position float[3]{0,-9, 0.00}
    set $iil position float[3]{0,-9, 0.00}
  }

  if $righton + $direction > 1	//On and lit, show 3d model
  {
    set $ifr position float[3]{0,0,0}
    set $irr position float[3]{0,0,0}
    set $iir position float[3]{0,0,0}
  }
  else				//Hide it
  {
    set $ifr position float[3]{0,-9,-0.00}
    set $irr position float[3]{0,-9, 0.00}
    set $iir position float[3]{0,-9, 0.00}
  }
}

func void ShowLights()
{
  if $fogon == 1
  {
    set $fogf position float[3]{0,0,0}
    set $fogi position float[3]{0,0,0}
  }
  else
  {
    set $fogf position float[3]{-0,-9,-0.0}
    set $fogi position float[3]{-0,-9, -0.0}
  }

  int $hbeamlit = $liton * $hbeamon
  if $hbeamlit == 1
  {
    set $hbml position float[3]{0,0,0}
    set $hbmi position float[3]{0,0,0}
  }
  else
  {
    set $hbml position float[3]{-0,-9,-0.0}
    set $hbmi position float[3]{-0,-9, -0.0}
  }
}

while 1
{
  CheckInput()
  CheckReverse()
  TimeBlinking()
  ShowSignals()
  ShowLights()
  
  interrupt
}