int $compteur_shift
// Indicateur de changement de rapport
func void eco(float $mindownrpm,float $maxdownrpm,float $minuprpm,float $maxuprpm,float $factor)
{
	rcar $car = get scriptowner car
	int $i
	int $gear = get $car gear
	float $rpm = get $car rpm
	float $brake = get $car brake
  	float $throttle = get $car throttle
	float $unthrottle = 1-$throttle
	float $shiftdown = $unthrottle*$mindownrpm+$throttle*$throttle*$maxdownrpm*$factor
    float $shiftup = $unthrottle*$minuprpm+$throttle*$maxuprpm*$factor //Used variables
	int $kickdown = 0
	rcarmodel $up = get generic model 10 of $car
	rcarmodel $down = get generic model 11 of $car
	set $up scriptcontrolled 1
	set $down scriptcontrolled 1
	float $clutch = get $car clutch
	if $gear < 2 or $clutch > 0.00
	{
		set $up position float[3]{0.0,-0.1,0.0}
		set $down position float[3]{0.0,-0.1,0.0}
	}
	if $rpm > $shiftdown and $rpm < $shiftup
	{
		set $up position float[3]{0.0,-0.1,0.0}
		set $down position float[3]{0.0,-0.1,0.0}
	}
	if ($shiftdown < $mindownrpm) //Avoid to low downshift...
	{
		$shiftdown=$mindownrpm
	}
	if ($shiftdown > $maxdownrpm)
	{
		$shiftdown=$maxdownrpm
	}
	if ($shiftup > $maxuprpm)
	{
		$shiftup = $maxuprpm
	}
	if $shiftup < $minuprpm
	{
		$shiftup = $minuprpm
	}
	if $rpm < $shiftdown 
	{
		//Downshift
		set $down position float[3]{0.0,0.0,0.0}
		set $up position float[3]{0.0,-0.1,0.0}
	}
	if $rpm > $shiftup and $throttle > 0.10 and $clutch == 0.0
	{
		//Up shift
		set $up position float[3]{0.0,00.0,0.0}
		set $down position float[3]{0.0,-0.1,0.0}
	}
}









//Script Boite automatique










func void shift(float $mindownrpm,float $maxdownrpm,float $minuprpm,float $maxuprpm,float $factor)
{
	rcar $car = get scriptowner car
	float $temps = 800.0
	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 $clutch = get $car clutch
	float $unthrottle = 1-$throttle
	float $shiftdown = $unthrottle*$mindownrpm+$throttle*$throttle*$maxdownrpm
    float $shiftup = $unthrottle*$minuprpm+$throttle*$maxuprpm //Used variables
	if ($shiftdown < $mindownrpm) //Avoid to low downshift...
	{
		$shiftdown=$mindownrpm
	}
	if ($shiftup > $maxuprpm)
	{
		$shiftup = $maxuprpm
	}
	if $shiftup < $minuprpm
	{
		$shiftup = $minuprpm
	}
	if $rpm >= $maxuprpm
	{
		set $car gear ($gear+1)
		$compteur_shift=0
		wait(2)
	}
	if $rpm <= $mindownrpm
	{
		set $car gear ($gear-1)
		$compteur_shift=0
		wait(2)
	}
	if $rpm < 100 //Put neutral when stall
	{
		set $car gear 0
	}
	if $rpm < $shiftdown //Downshift
	{
		if $rpm < $mindownrpm
		{
			$compteur_shift++
			if $compteur_shift > $factor
			{
				set $car gear ($gear-1)
				$compteur_shift=0
			}
		}
		if $throttle > 0.40
		{
			$compteur_shift+=4
			if $compteur_shift > $factor
			{
				set $car gear ($gear-1)
				$compteur_shift=0
			}
		}
	}
	if $rpm < 900 and $throttle == 0 and $gear > 3 //Downshift when braking
	{
		if $brake > 0
		{
			set $car gear ($gear-1)
		}
		else
		{
			set $car gear ($gear+0)
		}
	}
	//Shifting
	if $rpm > 4800
	{
		set $car gear($gear+1)
	}
	if $rpm > $shiftup and $clutch == 0 //Up shift
	{
		if $tc == 0 and $brake == 0 and $throttle > 0.125
		{
			$compteur_shift++
			if $compteur_shift > $factor
			{
				set $car gear ($gear+1)
				$compteur_shift=0
			}
		}
	}
	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
rcar $localcar = get local car
float $gear
float $brake
float $throttle
int $interval = 500
int $compteur_affichage = 0
int $compteur_point_mort = 0
float $shifting = 60
int $mode = 2
while 1
{
	if $car == $localcar
	{
		float $indice_moteur = get system "car0.engine.inertia"
		$brake = get $car brake
		$throttle = get $car throttle
		float $rpm = get $car rpm
		$gear = get $car gear
		rcarmodel $reverse = get generic model 17 of $car
		set $reverse scriptcontrolled 1
		rcarmodel $neutral = get generic model 16 of $car
		set $neutral scriptcontrolled 1
		rcarmodel $drive = get generic model 18 of $car
		set $drive scriptcontrolled 1
		rcarmodel $auto = get generic model 19 of $car
		set $auto scriptcontrolled 1
		rcarmodel $up = get generic model 10 of $car
		rcarmodel $down = get generic model 11 of $car
		float $offset_z=-0.0015
		string $gmode = "D"
		if $indice_moteur == 0.291
		{
			//Moteur 2.0dCi 180 et 173
			set $reverse position float[3]{0,-10,$offset_z}
			set $neutral position float[3]{0,-10,$offset_z}
			set $drive position float[3]{0,-10,$offset_z}
			set $auto position float[3]{0,-10,0.0}
			if $gear== 0 //Neutral
			{
				eco(-10000,-10000,10000,10000,1.00)
				wait($interval)
			}
			if $gear== 2 //Gear 1
			{
				eco(-10000,-10000,1700,3400,1.00) //0 and 0 first argument to avoid downshift in 1st :D
				wait($interval)
			}
			if $gear == 3 //Gear 2
			{
				eco(1200,2200,1800,3500,1.0)
				wait($interval)
			}
			if $gear == 4 //Gear 3
			{
				eco(1250,2100,1800,3650,1.0)
				wait($interval)
			}
			if $gear == 5 //Gear 4
			{
				eco(1275,2200,1800,3300,1.0)
				wait($interval)
			}
			if $gear == 6 //Gear 5
			{
				eco(1400,2300,1700,3750,1.0)
				wait($interval)
			}
			if $gear == 7 // Gear 6
			{
				eco(1600,2500,10000,10000,1.0)
				wait($interval)
			}	
		}
		if $indice_moteur == 0.290
		{
			//Moteur 2.0dCi 130 et 150
			set $reverse position float[3]{0,-10,$offset_z}
			set $neutral position float[3]{0,-10,$offset_z}
			set $drive position float[3]{0,-10,$offset_z}
			set $auto position float[3]{0,-10,0.0}
			if $gear== 0 //Neutral
			{
				eco(-10000,-10000,10000,10000,1.00)
				wait($interval)
			}
			if $gear== 2 //Gear 1
			{
				eco(-10000,-10000,1700,3900,1.00) //0 and 0 first argument to avoid downshift in 1st :D
				wait($interval)
			}
			if $gear == 3 //Gear 2
			{
				eco(1200,2200,1800,3750,1.0)
				wait($interval)
			}
			if $gear == 4 //Gear 3
			{
				eco(1250,2100,1800,3800,1.0)
				wait($interval)
			}
			if $gear == 5 //Gear 4
			{
				eco(1275,2200,1800,3800,1.0)
				wait($interval)
			}
			if $gear == 6 //Gear 5
			{
				eco(1400,2300,1700,3750,1.0)
				wait($interval)
			}
			if $gear == 7 // Gear 6
			{
				eco(1600,2600,10000,10000,1.0)
				wait($interval)
			}	
		}
		if $indice_moteur == 0.185
		{
			set $reverse position float[3]{0,-10,$offset_z}
			set $neutral position float[3]{0,-10,$offset_z}
			set $drive position float[3]{0,-10,$offset_z}
			set $auto position float[3]{0,-10,0.0}
			if $gear== 0 //Neutral
			{
				eco(-10000,-10000,10000,10000,1.00)
				wait($interval)
			}
			//Moteur 2.0T 205
			
			if $gear== 2 //Gear 1
			{
				eco(-10000,-10000,1700,5000,1.00) //0 and 0 first argument to avoid downshift in 1st :D
				wait($interval)
			}
			if $gear == 3 //Gear 2
			{
				eco(1200,2900,1800,5000,1.0)
				wait($interval)
			}
			if $gear == 4 //Gear 3
			{
				eco(1250,3100,1800,5000,1.0)
				wait($interval)
			}
			if $gear == 5 //Gear 4
			{
				eco(1275,3300,1800,5000,1.0)
				wait($interval)
			}
			if $gear == 6 //Gear 5
			{
				eco(1400,3500,1700,5000,1.0)
				wait($interval)
			}
			if $gear == 7 // Gear 6
			{
				eco(1600,3600,10000,10000,1.0)
				wait($interval)
			}	
		}
		if $indice_moteur == 0.160
		{
			set $reverse position float[3]{0,-10,$offset_z}
			set $neutral position float[3]{0,-10,$offset_z}
			set $drive position float[3]{0,-10,$offset_z}
			set $auto position float[3]{0,-10,0.0}
			if $gear== 0 //Neutral
			{
				eco(-10000,-10000,10000,10000,1.00)
				wait($interval)
			}
			//Moteur 2.0 16V 140
			
			if $gear== 2 //Gear 1
			{
				eco(-10000,-10000,1800,3700,1.00) //0 and 0 first argument to avoid downshift in 1st :D
				wait($interval)
			}
			if $gear == 3 //Gear 2
			{
				eco(1200,2900,1800,5000,1.0)
				wait($interval)
			}
			if $gear == 4 //Gear 3
			{
				eco(1250,3600,1800,5500,1.0)
				wait($interval)
			}
			if $gear == 5 //Gear 4
			{
				eco(1275,3800,1800,5500,1.0)
				wait($interval)
			}
			if $gear == 6 //Gear 5
			{
				eco(1300,3900,1700,5500,1.0)
				wait($interval)
			}
			if $gear == 7 // Gear 6
			{
				eco(1300,4100,10000,10000,1.0)
				wait($interval)
			}	
		}
		if $indice_moteur == 0.220
		{
			$brake = get $car brake
			float $speed = get $car tachovelocity
			$speed = $speed * 3.71
			$gear = get $car gear
			set $up scriptcontrolled 1
			set $down scriptcontrolled 1
			set $up position float[3]{0.0,-0.1,0.0}
			set $down position float[3]{0.0,-0.1,0.0}
			if is key (90) pressed
			{
				$compteur_affichage = 0
				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"
				$compteur_affichage = 0
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,0,$offset_z}
				set $drive position float[3]{0,-10,$offset_z}
				set $auto position float[3]{0,-10,0.0}
				wait(250)
			}
			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,$offset_z}
					set $neutral position float[3]{0,-10,$offset_z}
					set $drive position float[3]{0,-10,$offset_z}
					set $auto position float[3]{0,-10,0.00}
					$compteur_affichage = 0
				}
			}
			if $mode == 2
			{
				$gmode ="Drive"
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,-10,$offset_z}
				set $drive position float[3]{0,0,$offset_z}
				set $auto position float[3]{0,-10,0.00}
				if $gear == 1
				{
					set $car gear (2)
				}
				if $gear == 0 and $throttle > 0.05
				{
					set $car gear 2
				}
				if $gear== 2 //Gear 1
				{
					shift(-100000,-100000,1900,6000,$shifting) //0 and 0 first argument to avoid downshift in 1st :D
					if $brake > 0 and abs($speed) < 2 and $throttle < 0.05
					{	
						$compteur_point_mort++
						if $compteur_point_mort % 10 == 0
						{
							//Mise au point mort a l'arret
							set $car gear 0
						}
					}
				}
				if $gear == 3 //Gear 2
				{
					shift(750,3400,1800,6000,$shifting)
				}
				if $gear == 4 //Gear 3
				{
					shift(900,3700,2200,6000,$shifting)
				}
				if $gear == 5 //Gear 4
				{
					shift(755,3700,2000,6000,$shifting)
				}
				if $gear == 6 //Gear 5
				{
					shift(755,4400,1800,6000,$shifting)
				}
				if $gear == 7 // Gear 6
				{
					shift(900,4000,1600,6000,$shifting)
				}
			}
			else if $mode == 3
			{
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,-10,$offset_z}
				set $drive position float[3]{0,-10,$offset_z}
				set $auto position float[3]{0,0,0.0}
				if $speed < 5 and $gear < 2
				{
					set $car gear 2
					wait(250)
				}
				$gmode ="Impulsionel"
				if $gear > 1
				{
					impulsionel(1100,6400)
				}
			}
			//Affichage du mode 
			if ($compteur_affichage < 1)
			{
				echo "Mode : "+$gmode
				$compteur_affichage ++
			}
		}
		if $indice_moteur == 0.292
		{
			$brake = get $car brake
			float $speed = get $car tachovelocity
			$speed = $speed * 3.71
			$gear = get $car gear
			set $up scriptcontrolled 1
			set $down scriptcontrolled 1
			set $up position float[3]{0.0,-0.1,0.0}
			set $down position float[3]{0.0,-0.1,0.0}
			$shifting = 60
			if is key (90) pressed
			{
				$compteur_affichage = 0
				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"
				$compteur_affichage = 0
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,0,$offset_z}
				set $drive position float[3]{0,-10,$offset_z}
				set $auto position float[3]{0,-10,0.0}
				wait(250)
			}
			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,$offset_z}
					set $neutral position float[3]{0,-10,$offset_z}
					set $drive position float[3]{0,-10,$offset_z}
					set $auto position float[3]{0,-10,0.00}
					$compteur_affichage = 0
				}
			}
			if $mode == 2
			{
				$gmode ="Drive"
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,-10,$offset_z}
				set $drive position float[3]{0,0,$offset_z}
				set $auto position float[3]{0,-10,0.00}
				if $gear == 1
				{
					set $car gear (2)
				}
				if $gear == 0 and $throttle > 0.05
				{
					set $car gear 2
				}
				if $gear== 2 //Gear 1
				{
					shift(-100000,-100000,1900,4400,$shifting) //0 and 0 first argument to avoid downshift in 1st :D
					if $brake > 0 and abs($speed) < 2
					{	
						$compteur_point_mort++
						if $compteur_point_mort % 10 == 0
						{
							//Mise au point mort a l'arret
							set $car gear 0
						}
					}
				}
				if $gear == 3 //Gear 2
				{
					shift(1200,1800,1800,4370,$shifting)
				}
				if $gear == 4 //Gear 3
				{
					shift(1300,2600,2200,4350,$shifting)
				}
				if $gear == 5 //Gear 4
				{
					shift(1350,2800,2000,4300,$shifting)
				}
				if $gear == 6 //Gear 5
				{
					shift(1300,2800,1800,4200,$shifting)
				}
				if $gear == 7 // Gear 6
				{
					shift(1280,3200,1600,6000,$shifting)
				}
			}
			else if $mode == 3
			{
				set $reverse position float[3]{0,-10,$offset_z}
				set $neutral position float[3]{0,-10,$offset_z}
				set $drive position float[3]{0,-10,$offset_z}
				set $auto position float[3]{0,0,0.0}
				if $speed < 5 and $gear < 2
				{
					set $car gear 2
					wait(250)
				}
				$gmode ="Impulsionel"
				if $gear > 1
				{
					impulsionel(1100,4800)
				}
			}
			//Affichage du mode 
			if ($compteur_affichage < 1)
			{
				echo "Mode : "+$gmode
				$compteur_affichage ++
			}
		}
	}
interrupt
wait(30)
}
