rcar $car = get scriptowner car	//get car

rcarmodel $ileft   = get generic model 5 of $car   //Left indicators
rcarmodel $iright  = get generic model 6 of $car   //Right ones
rcarmodel $imleft  = get generic model 20 of $car   //Left mirror indicator
rcarmodel $imright = get generic model 21 of $car   //Right mirror one
set $ileft  scriptcontrolled 1
set $iright scriptcontrolled 1
set $imleft  scriptcontrolled 1
set $imright scriptcontrolled 1
rcarmodel $fogf = get generic model 7 of $car //Fog Lights - front
set $fogf scriptcontrolled 1
rcarmodel $fogi = get generic model 13 of $car //Fog Lights - indicator
set $fogi scriptcontrolled 1
rcarmodel $hbml = get generic model 4 of $car //High Beam  - Lights
set $hbml scriptcontrolled 1
rcarmodel $hbmi = get generic model 14 of $car //High Beam  - Indicator
set $hbmi scriptcontrolled 1
rcarmodel $strl = get generic model 15 of $car //Zapłon  - Lights
set $strl scriptcontrolled 1
rcarmodel $radio = get generic model 16 of $car //Radio  - Lights
set $radio scriptcontrolled 1

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

int $lefton=0			//Are they on?
int $righton=0
int $hazon=0
int $hbeamon=0
shared int $fogon=0
shared int $stron=0
shared int $radioon=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/audi_rs6_avant/sounds/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 83 pressed		//Zapłon lights key [S]
  {
    if $hbmdown == 0		//First press
    {
      $strdown = 1		//Mark as held down
      $stron = 1 - $stron	//Toggle
    }
  }
  else
  {
    $strdown = 0		//Mark as released
  }

  if is key 68 pressed		//Radio lights key [R]
  {
    if $radiodown == 0		//First press
    {
      $radiodown = 1		//Mark as held down
      $radioon = 1 - $radioon	//Toggle
    }
  }
  else
  {
    $radiodown = 0		//Mark as released
  }

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

  if is key 75 pressed		//High Beam key [K]
  {
    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 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 $ileft position float[3]{0,0,0}
    set $imleft position float[3]{0,0,0}
  }
  else				//Hide it
  {
    set $ileft position float[3]{0,-10,0}
    set $imleft position float[3]{0,-10,0}
  }

  if $righton + $direction > 1	//On and lit, show 3d model
  {
    set $iright position float[3]{0,0,0}
    set $imright position float[3]{0,0,0}
  }
  else				//Hide it
  {
    set $iright position float[3]{0,-10,0}
    set $imright position float[3]{0,-10,0}
  }

}

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,-10,0}
    set $fogi position float[3]{0,-10,0}
  }

  if $stron == 1
  {
    set $strl position float[3]{0,0,0}
  }
  else
  {
    set $strl position float[3]{0,-10,0}
  }

  if $radioon == 1
  {
    set $radio position float[3]{0,0,0}
  }
  else
  {
    set $radio position float[3]{0,-10,0}
  }

  int $hbeamlit = $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,-10,0}
    set $hbmi position float[3]{0,-10,0}
  }
}

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