rcar $car = get scriptowner car	//get car
float $brake = 0
float $speed = 0

rcarmodel $ileft   = get generic model 6 of $car   //Left indicators
rcarmodel $iright  = get generic model 7 of $car   //Right ones

set $ileft  scriptcontrolled 1
set $iright scriptcontrolled 1

int $ldown=0
int $rdown=0
int $hdown=0

int $lefton=0			//Are they on?
int $righton=0
int $hazon=0

int $duration=0			//Stay lit duration, max 500ms
int $direction=1		//Lit or Pause

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

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
      }
    }
  }
  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
         $righton = 0			//Cancel oposite signal
      }
    }
  }
  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
      }
    }
  }
  else
  {
    $rdown = 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 > 375		//Has been lit for half a sec  
    { 
      $duration = 275		//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
    }
  }
  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}
  }
  else	//Hide it
  {
    set $ileft position float[3]{0,-10,0}
  }

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

}
func void steer()		//Check for reverse
{
$steerpos = get $car steerpos
$steerpos = abs($steerpos)
  if $steerpos < 0.01 and $hazon == 0			//Turn on both signals (hazard lights)
  {
    $righton = 0
    $lefton = 0
  }
}
while 1
{
  CheckInput()
  TimeBlinking()
  ShowSignals()
  steer()
  interrupt
}