//Script Boite automatique









int $compteur_shift = 0
func void shift(float $mindownrpm,float $maxdownrpm,float $minuprpm,float $maxuprpm,float $factor,int $minspeed)
{
	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
	float $speed = get $car tachovelocity*3.695
	if $speed < $minspeed
	{
		$compteur_shift+=4
			if $compteur_shift > $factor
			{
				set $car gear ($gear-1)
				$compteur_shift=0
			}
	}
	if ($shiftdown < $mindownrpm) 
	//Avoid to low downshift...
	{
		$shiftdown=$mindownrpm
	}
	if ($shiftup > $maxuprpm)
	{
		$shiftup = $maxuprpm
	}
	if $shiftup < $minuprpm
	{
		$shiftup = $minuprpm
	}
	if $rpm <= $mindownrpm
	{
		set $car gear ($gear-1)
		$compteur_shift=0
		wait(2)
	}
	if $clutch > 0.75
	{
		$compteur_shift = 0
	}
	if $rpm < $shiftdown 
	//Downshift
	{
		//echo  $compteur_shift
		if $rpm < $mindownrpm
		{
			$compteur_shift++
			if $compteur_shift > $factor
			{
				set $car gear ($gear-1)
				$compteur_shift=0
			}
		}
		if $throttle > 0.80
		{
			$compteur_shift+=1
			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 > $shiftup and $clutch < 0.01 and $speed > $minspeed 
	//Up shift
	{
		if $tc == 0 and $brake == 0 and $throttle > 0.075
		{
			$compteur_shift++
			if $compteur_shift > $factor
			{
				set $car gear ($gear+1)
				$compteur_shift=0
			}
		}
	}
	if $rpm >= $maxuprpm and $speed > $minspeed
	{
		set $car gear ($gear+1)
		$compteur_shift=0
		wait(2)
	}
	//echo $compteur_shift
	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 = 0

//Signaux tbd

shared int $point_mort = 0
shared int $marche_arriere = 0
shared int $drive = 0
shared int $impulsionel = 0


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
		float $speed = get $car tachovelocity
		$speed = $speed * 3.71
		float $offset_z=-0.001
		string $gmode
		if $indice_moteur == 0.345 or $indice_moteur == 0.331 or $indice_moteur == 0.186 or $indice_moteur == 0.220
		{
			$brake = get $car brake
			$gear = get $car gear
			$shifting = 60
			if is key (90) pressed
			{
				$compteur_affichage = 0
				if $mode <2
				{
					$mode += 1
					wait (250)
				}
				else
				{
					$mode = 1
					wait (250)
				}
			}
			if is key 78 pressed
			{
				$mode = 0
				set $car gear 0
				$gmode = "N"
				$compteur_affichage = 0
				wait(250)
			}
			if $mode == 0
			{
				set $car gear 0
				$gmode = "N"
				$point_mort = 1
				$marche_arriere = 0
				$drive = 0
				$impulsionel = 0
			}
			if $mode == 4
			{
				set $car gear 1
				$gmode ="R"
				$point_mort = 0
				$marche_arriere = 1
				$drive = 0
				$impulsionel = 0
			}
			if is key 82 pressed
			{
				if $speed < 5 and $brake > 0
				{	
					$mode = 4
					set $car gear 1
					wait (250)
					$gmode ="R"
					$compteur_affichage = 0
				}
			}
			if $mode == 1
			{
				$gmode ="Drive"
				$point_mort = 0
				$marche_arriere = 0
				$drive = 1
				$impulsionel = 0
				//3.0dCi
				if $indice_moteur == 0.345
				{
					$shifting = 60
					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,1400,4400,$shifting,-50) //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(975,1800,1400,4400,$shifting,10)
					}
					if $gear == 4 //Gear 3
					{
						shift(1000,2900,1420,4350,$shifting,22)
					}
					if $gear == 5 //Gear 4
					{
						shift(1055,3100,1400,4500,$shifting,34)
					}
					if $gear == 6 //Gear 5
					{
						shift(1155,3100,1380,4400,$shifting,48)
					}
					if $gear == 7 // Gear 6
					{
						shift(1280,3200,1700,6000,$shifting,65)
					}
				}
				//2.2dCi
				if $indice_moteur == 0.331
				{
					$shifting = 60
					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,1400,4000,$shifting,-50) //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(975,1800,1400,4050,$shifting,10)
					}
					if $gear == 4 //Gear 3
					{
						shift(1000,2900,1420,4050,$shifting,22)
					}
					if $gear == 5 //Gear 4
					{
						shift(1055,3000,1400,4075,$shifting,34)
					}
					if $gear == 6 //Gear 5
					{
						shift(1155,3100,1380,4075,$shifting,48)
					}
					if $gear == 7 // Gear 6
					{
						shift(1280,3200,1700,6000,$shifting,48)
					}
				}
				//2.0T
				if $indice_moteur == 0.186
				{
					$shifting = 60
					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,5200,$shifting,-50) //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(975,3300,2100,5250,$shifting,10)
					}
					if $gear == 4 //Gear 3
					{
						shift(1000,3300,1900,5300,$shifting,30)
					}
					if $gear == 5 //Gear 4
					{
						shift(1055,3400,1800,5800,$shifting,45)
					}
					if $gear == 6 //Gear 5
					{
						shift(1155,4100,1800,5200,$shifting,70)
					}
					if $gear == 7 // Gear 6
					{
						shift(1280,3200,1700,6000,$shifting,48)
					}
				}
				//3.5 V6
				if $indice_moteur == 0.220
				{
					$shifting = 60
					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,-50) //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(975,3300,2100,6000,$shifting,10)
					}
					if $gear == 4 //Gear 3
					{
						shift(1000,3300,1900,6000,$shifting,30)
					}
					if $gear == 5 //Gear 4
					{
						shift(1055,3400,1800,6200,$shifting,45)
					}
					if $gear == 6 //Gear 5
					{
						shift(1155,4500,1800,6500,$shifting,70)
					}
					if $gear == 7 // Gear 6
					{
						shift(1280,3200,1700,6000,$shifting,48)
					}
				}		
			}
			else if $mode == 2
			{
				$point_mort = 0
				$marche_arriere = 0
				$drive = 0
				$impulsionel = 1
				if $speed < 5 and $gear < 2
				{
					set $car gear 2
					wait(250)
				}
				$gmode ="Impulsionel"
				//3.0dCi
				if $indice_moteur == 0.345
				{
					if $gear > 1
					{
						impulsionel(1100,4800)
					}
				}
				//2.2dCi BVA
				if $indice_moteur == 0.331
				{
					if $gear > 1
					{
						impulsionel(1100,4100)
					}
				}
				//2.0T
				if $indice_moteur == 0.186
				{
					if $gear > 1
					{
						impulsionel(1100,5500)
					}
				}
				//3.5 V6
				if $indice_moteur == 0.220
				{
					if $gear > 1
					{
						impulsionel(1100,6400)
					}
				}
			}
			//Affichage du mode 
			if ($compteur_affichage < 1)
			{
				echo "Mode : "+$gmode
				//echo "numero :" +$mode
				$compteur_affichage ++
			}
		}
		else
		{
			//Moteurs BVM
			$point_mort = 0
			$marche_arriere = 0
			$drive = 0
			$impulsionel = 1
		}
	}
interrupt
wait(30)
}
