func void shift(float $mindownrpm,float $maxdownrpm,float $minuprpm,float $maxuprpm,float $factor)
{
	rcar $car = get scriptowner car
	int $i
	float $rpm = get $car rpm
	float $tc = get $car tc active
	float $gear = get $car gear
	float $brake = get $car brake
  	float $throttle = get $car throttle
	float $unthrottle = 1-$throttle
	float $shiftdown = $unthrottle*$mindownrpm+$throttle*$throttle*$maxdownrpm
    float $shiftup = $unthrottle*$minuprpm+$throttle*$maxuprpm*$factor //Used variables
	if ($shiftdown < $mindownrpm) //Avoid to low downshift...
	{
		$shiftdown=$mindownrpm
	}
	if $rpm < 100 //Put neutral when stall
	{
		set $car gear 0
		wait(0)
	}
	if $throttle > 0.80
	{
		$shiftdown-=300
		$shiftup+=300
	}
	if $rpm < $shiftdown //Downshift
	{
		if $throttle > 0.8 
		{
			set $car gear ($gear-1)
			
			wait(600)
		}
		else if $brake > 0
		{
			set $car gear ($gear-1)
                	wait(1100)
		}
		else
		{
			set $car gear ($gear-1)
			wait(1000*$unthrottle)
			if $unthrottle < 0.1
			{
				$unthrottle = 0.1
			}
		}
	}
	if $rpm < 1300 and $throttle == 0 and $gear > 3 //Downshift when braking
	{
		if $brake > 0
		{
			set $car gear ($gear-1)
			wait(1000)
		}
		else
		{
			set $car gear ($gear+0)
		}
	}
	if $rpm > $shiftup //Up shift
	{
		if $tc == 0 and $brake == 0 and $throttle > 0.11
		{
			set $car gear ($gear +1)
			if $unthrottle < 0.1
			{
				$unthrottle = 0.1
			}
                	wait(1000*$unthrottle)
		}
		else if $tc==0 and $rpm > 6000
		{
			set $car gear ($gear +1)
                	wait(1)
		}
	}
}
func void impulsionel(float $shiftdown,float $shiftup)
{
	rcar $car = get scriptowner car
	float $rpm = get $car rpm
	float $gear = get $car gear
	if $rpm < $shiftdown and $gear > 2 //Downshift
	{
		set $car gear ($gear-1)
	}
	if $rpm > $shiftup //Up shift
	{
		set $car gear ($gear +1)
	}
}
rcar $car = get scriptowner car
float $gear
shared int $shiftup = 0
shared int $shiftdown = 0
float $tc = 0
float $brake = 0
shared int $mode = 2
shared string $gmode = "drive"
float $speed
rcarmodel $drive
rcarmodel $auto
rcarmodel $neutral
rcarmodel $reverse
while 1
{
  	$tc = get $car tc active
  	$brake = get $car brake
  	$speed = get $car tachovelocity
  	$speed = $speed * 3.71
	$gear = get $car gear
	$reverse = get generic model 14 of $car
	set $reverse scriptcontrolled 1
	$neutral = get generic model 15 of $car
	set $neutral scriptcontrolled 1
	$drive = get generic model 16 of $car
	set $drive scriptcontrolled 1
	$auto = get generic model 17 of $car
	set $auto scriptcontrolled 1
  	if is key (90) pressed
  	{
		if $mode <3
		{
			$mode += 1
			wait (250)
		}
		else
		{
			$mode = 2
			wait (250)
		}
  	}
  	if is key 78 pressed
  	{
		$mode = 0
		set $car gear 0
        $gmode = "N"
		set $reverse position float[3]{0,-10,0}
		set $neutral position float[3]{0,0,0.0}
		set $drive position float[3]{0,-10,0.0}
		set $auto position float[3]{0,0,-0.005}
  	}
  	if is key 82 pressed
  	{
		if $speed < 5 and $brake > 0
		{	
			$mode = 1
			set $car gear 1
			wait (250)
        	$gmode ="R"
			set $reverse position float[3]{0,0,0}
			set $neutral position float[3]{0,-10,0.0}
			set $drive position float[3]{0,-10,0.0}
			set $auto position float[3]{0,0,-0.005}
		}
  	}
	if $mode == 2
	{
        $gmode ="Drive"
		set $reverse position float[3]{0,-10,0}
		set $neutral position float[3]{0,-10,0.0}
		set $drive position float[3]{0,0,0.0}
		set $auto position float[3]{0,0,-0.005}
		if $gear < 2
		{
			set $car gear (2)
		}
		if $gear== 2 //Gear 1
		{
			shift(-100000,-100000,1000,6100,1.00) //0 and 0 first argument to avoid downshift in 1st :D
		}
		if $gear == 3 //Gear 2
		{
			shift(680,3400,1100,5900,1.00)
		}
		if $gear == 4 //Gear 3
		{
			shift(900,3700,1200,5900,1.00)
		}
		if $gear == 5 //Gear 4
		{
			shift(755,3700,780,6000,1.00)
		}
		if $gear == 6 //Gear 5
		{
			shift(755,4400,1900,6000,1.00)
		}
		if $gear == 7 // Gear 6
		{
			shift(900,4000,2000,6000,1.00)
		}
	}
	else if $mode == 3
	{
		set $reverse position float[3]{0,-10,0}
		set $neutral position float[3]{0,-10,0.0}
		set $drive position float[3]{0,-10,0.0}
		set $auto position float[3]{0,-10,0.0}
		if $speed < 5 and $gear < 2
		{
			set $car gear 2
			wait(250)
		}
        	$gmode ="Impulsionel"
		if $gear > 1
		{
			impulsionel(1100,6000)
		}
  	}
interrupt
}
