rcar $car = get local car
float $throttle = 0
float $gear = 2
float $rpm = 1000
float $mindownrpm = 0
float $maxdownrpm = 0
float $minuprpm = 0
float $maxuprpm = 0
float $shiftup = 0
float $shiftdown = 0
float $tc = 0
float $brake = 0
int $mode = 2
string $gmode = "drive"
while 1
{
  $throttle = get $car throttle
  $gear = get $car gear
  $rpm = get $car rpm
  $tc = get $car tc active
  $brake = get $car brake
  if is key (77) pressed
  {
	if $mode <3
	{
	$mode += 1
	}
	else
	{
	$mode = 0
	}
	wait (250)
	if $mode==0
	{
	set $car gear 0
        $gmode = "N"
	}
	else if $mode==1
	{
	set $car gear 1
        $gmode ="R"
	}
  }
if $rpm < 900
{
set $car tc  0
}
else
{
set $car tc 1
}
if $mode ==2 and $tc ==0
	{
        $gmode ="Drive"
	if $gear < 2
	{
	set $car gear ($gear+1)
	}
		if $gear==2
		{
			$mindownrpm=0
			$minuprpm=2000
			$maxdownrpm=0
			$maxuprpm=6500
			$shiftdown = 0
                	$shiftup = $minuprpm + 0.5 * $throttle*((0.8 * $maxuprpm)+(1.9*$minuprpm))
			if $rpm < $shiftdown
			{
				if $brake==0
				{
				set $car gear 2
                                wait(500)
				}
				else
				{
				set $car gear 2
                                wait(500)
				}
			}
			if $rpm > $shiftup
			{
				if $tc==0 and $brake==0
				{
				set $car gear ($gear +1)
                                wait(500)
				}
				else if $tc==0 and $brake <=1 and $rpm == 4500
				{
				set $car gear ($gear +1)
                                wait(500)
				}
			}
			if $rpm < 2000 and $throttle == 0
				{
				if $brake > 0
				{
				set $car gear (2)
				}
			}
		}
		if $gear ==3
		{
			$mindownrpm=950
			$minuprpm=2000
			$maxdownrpm=4400
			$maxuprpm=6000
			$shiftdown = $mindownrpm + 0.4*$throttle*($mindownrpm+$maxdownrpm)
                	$shiftup = $minuprpm + 0.50 * $throttle*((1 * $maxuprpm)+(1*$minuprpm))
			if $rpm < $shiftdown
			{
				if $brake == 0 
				{
				set $car gear ($gear-1)
                                wait(500)
				}
				else
				{
				set $car gear 2
                                wait(500)
				}
			}
			if $rpm < 2000 and $throttle == 0
			{
				if $brake > 0
				{
				set $car gear ($gear-1)
				wait(500)
				}
				else
				{
				set $car gear ($gear+0)
				}
			}
			if $rpm > $shiftup
			{
				if $tc==0 and $brake==0
				{
				set $car gear ($gear +1)
                                wait(500)
				}
				else if $tc==0 and $brake <=1 and $rpm == 4500
				{
				set $car gear ($gear +1)
                                wait(500)
				}
			}
		}
		if $gear > 3
		{
			$mindownrpm=1050
			$minuprpm=1300
			$maxdownrpm=4400
			$maxuprpm=6000
			$shiftdown = $mindownrpm + 0.44*$throttle*($mindownrpm+$maxdownrpm)
                	$shiftup = $minuprpm + 0.52 * $throttle*((1 * $maxuprpm)+(1.0*$minuprpm))
			if $rpm < $shiftdown
			{
				if $brake ==0
				{
				set $car gear ($gear-1)
                                wait(500)
				}
				else
				{
				set $car gear 2
                                wait(500)
				}
			}
			if $rpm > $shiftup
			{
				if $tc==0 and $brake==0
				{
				set $car gear ($gear +1)
                                wait(1000)
				}
				else if $tc==0 and $brake <=1 and $rpm == 4500
				{
				set $car gear ($gear +1)
                                wait(1000)
				}
			}
		}
	}
	else if $mode ==3 and $tc==0
	{
        $gmode ="Impulsionel"
		if $gear > 1
		{
			$mindownrpm=1100
			$maxuprpm=6000
			$shiftdown = 1100
                	$shiftup = 4500
			if $rpm < $shiftdown
			{
				if $brake ==0
				{
				set $car gear 2
                                wait(500)
				}
				else
				{
				set $car gear 2
                                wait(500)
				}
			}
			if $rpm > $shiftup
				{
				if $brake==0
				{
				set $car gear ($gear +1)
                                wait(500)
				}
			}
		}
  }
  paint "Mode :" at float[2]{10,90}
  paint $gmode at float[2]{60,90}
interrupt
}
