
//Focus on the car used by the script owner
rcar $car=get scriptowner car
rcar $localcar = get local car

//
float $gforce[3]
float $gforce_lon
float $sample_hold

//Signaux script frein a main
shared int $fam_desserre = 0
shared int $fam_serre = 0
shared int $fam_temoin = 0

//Signaux script BVA
shared int $point_mort = 0
shared int $marche_arriere = 0
shared int $drive = 0
shared int $impulsionel = 0

//Signaux script ESP 
shared int $esp_action = 0
shared int $esp_desactive = 0
shared int $esp_active = 0

//Temps d'affichage
int $time = 200
int $compteur = 0
shared int $display = 1
float $offset_z = -0.0012
int $refresh

func int calculate_sample_hold(rcar $target,int $max,int $min,int $speed)
{
	$gforce = get $target gforce
	$gforce_lon=1.0*($gforce.z/1.0)
	$sample_hold = 3200/abs(3.25*$gforce_lon)
	$sample_hold+=(0.00000025*$speed*$speed*$speed*$speed)
	if $sample_hold < $min
	{
		$sample_hold = $min+0.01
	}
	$max+=0.10*($speed+$speed*$speed)
	if $sample_hold > $max
	{
		$sample_hold = $max+0.01
	}
	return floor($sample_hold)
}

func int[3] decompose(int $speed)
{
	int $centaines
	int $dizaines
	int $unite
	if $speed > 100
	{
		$centaines = floor($speed/100)
		$dizaines = floor(($speed-($centaines*100))/10)
		$unite = floor($speed-(10*$dizaines+100*$centaines))
	}
	if $speed > 10 and $speed < 100
	{
		$centaines = 0
		$dizaines = floor($speed/10)
		$unite = floor($speed-(10*$dizaines))
	}
	if $speed < 10
	{
		$centaines = 0
		$dizaines = 0
		$unite = $speed
	}
	int $resultat[3]=int[3]{$centaines,$dizaines,$unite}
	return $resultat
}

func void show_speed(int $unites,int $dizaines,int $centaines,int $speed)
{
	rcarmodel $u_0 = get generic model 30 of $car
	rcarmodel $u_1 = get generic model 31 of $car
	rcarmodel $u_2 = get generic model 32 of $car
	rcarmodel $u_3 = get generic model 33 of $car
	rcarmodel $u_4 = get generic model 34 of $car
	rcarmodel $u_5 = get generic model 35 of $car
	rcarmodel $u_6 = get generic model 36 of $car
	
	set $u_0 scriptcontrolled 1
	set $u_1 scriptcontrolled 1
	set $u_2 scriptcontrolled 1
	set $u_3 scriptcontrolled 1
	set $u_4 scriptcontrolled 1
	set $u_5 scriptcontrolled 1
	set $u_6 scriptcontrolled 1
	
	rcarmodel $d_0 = get generic model 37 of $car
	rcarmodel $d_1 = get generic model 38 of $car
	rcarmodel $d_2 = get generic model 39 of $car
	rcarmodel $d_3 = get generic model 40 of $car
	rcarmodel $d_4 = get generic model 41 of $car
	rcarmodel $d_5 = get generic model 42 of $car
	rcarmodel $d_6 = get generic model 43 of $car
	
	set $d_0 scriptcontrolled 1
	set $d_1 scriptcontrolled 1
	set $d_2 scriptcontrolled 1
	set $d_3 scriptcontrolled 1
	set $d_4 scriptcontrolled 1
	set $d_5 scriptcontrolled 1
	set $d_6 scriptcontrolled 1
	
	rcarmodel $c_1 = get generic model 44 of $car
	rcarmodel $c_2 = get generic model 45 of $car
	rcarmodel $c_3 = get generic model 46 of $car
	
	set $c_1 scriptcontrolled 1
	set $c_2 scriptcontrolled 1
	set $c_3 scriptcontrolled 1
	
	if $unites == 0
	{
		set $u_0 position float[3]{0,-50,$offset_z}
		set $u_1 position float[3]{0,0,$offset_z}
		set $u_2 position float[3]{0,0,$offset_z}
		set $u_3 position float[3]{0,0,$offset_z}
		set $u_4 position float[3]{0,0,$offset_z}
		set $u_5 position float[3]{0,0,$offset_z}
		set $u_6 position float[3]{0,0,$offset_z}
	}
	if $unites == 1
	{
		set $u_0 position float[3]{0,-50,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-50,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-50,$offset_z}
		set $u_5 position float[3]{0,-50,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 2
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-50,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-0,$offset_z}
		set $u_4 position float[3]{0,-50,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 3
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-50,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 4
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-50,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-0,$offset_z}
		set $u_5 position float[3]{0,-50,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 5
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-0,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-50,$offset_z}
	}
	if $unites == 6
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-0,$offset_z}
		set $u_4 position float[3]{0,-0,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-50,$offset_z}
	}
	if $unites == 7
	{
		set $u_0 position float[3]{0,-50,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-50,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-50,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 8
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-0,$offset_z}
		set $u_4 position float[3]{0,-0,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	if $unites == 9
	{
		set $u_0 position float[3]{0,-0,$offset_z}
		set $u_1 position float[3]{0,-0,$offset_z}
		set $u_2 position float[3]{0,-0,$offset_z}
		set $u_3 position float[3]{0,-50,$offset_z}
		set $u_4 position float[3]{0,-0,$offset_z}
		set $u_5 position float[3]{0,-0,$offset_z}
		set $u_6 position float[3]{0,-0,$offset_z}
	}
	
	if $dizaines == 0
	{
		set $d_0 position float[3]{0,-50,$offset_z}
		set $d_1 position float[3]{0,0,$offset_z}
		set $d_2 position float[3]{0,0,$offset_z}
		set $d_3 position float[3]{0,0,$offset_z}
		set $d_4 position float[3]{0,0,$offset_z}
		set $d_5 position float[3]{0,0,$offset_z}
		set $d_6 position float[3]{0,0,$offset_z}
	}
	if $dizaines == 1
	{
		set $d_0 position float[3]{0,-50,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-50,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-50,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 2
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-50,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-0,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 3
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 4
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-50,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-0,$offset_z}
		set $d_5 position float[3]{0,-50,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 5
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-0,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-50,$offset_z}
	}
	if $dizaines == 6
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-0,$offset_z}
		set $d_4 position float[3]{0,-0,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-50,$offset_z}
	}
	if $dizaines == 7
	{
		set $d_0 position float[3]{0,-50,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-50,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 8
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-0,$offset_z}
		set $d_4 position float[3]{0,-0,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	if $dizaines == 9
	{
		set $d_0 position float[3]{0,-0,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-0,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-0,$offset_z}
		set $d_5 position float[3]{0,-0,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
	}
	
	if $centaines == 0
	{
		set $c_1 position float[3]{0,-50,$offset_z}
		set $c_2 position float[3]{0,-50,$offset_z}
		set $c_3 position float[3]{0,-50,$offset_z}
	}
	if $centaines == 1
	{
		set $c_1 position float[3]{0,-0,$offset_z}
		set $c_2 position float[3]{0,-50,$offset_z}
		set $c_3 position float[3]{0,-50,$offset_z}
	}
	if $centaines == 2
	{
		set $c_1 position float[3]{0,-50,$offset_z}
		set $c_2 position float[3]{0,-0,$offset_z}
		set $c_3 position float[3]{0,-50,$offset_z}
	}
	if $centaines == 3
	{
		set $c_1 position float[3]{0,-50,$offset_z}
		set $c_2 position float[3]{0,-50,$offset_z}
		set $c_3 position float[3]{0,-00,$offset_z}
	}
	if $speed < 10
	{
		set $d_0 position float[3]{0,-50,$offset_z}
		set $d_1 position float[3]{0,-50,$offset_z}
		set $d_2 position float[3]{0,-50,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-50,$offset_z}
		set $d_6 position float[3]{0,-50,$offset_z}
	}
	if $speed == 10
	{
		set $d_0 position float[3]{0,-50,$offset_z}
		set $d_1 position float[3]{0,-0,$offset_z}
		set $d_2 position float[3]{0,-50,$offset_z}
		set $d_3 position float[3]{0,-50,$offset_z}
		set $d_4 position float[3]{0,-50,$offset_z}
		set $d_5 position float[3]{0,-50,$offset_z}
		set $d_6 position float[3]{0,-0,$offset_z}
		
		
		set $c_1 position float[3]{0,-50,$offset_z}
		set $c_2 position float[3]{0,-50,$offset_z}
		set $c_3 position float[3]{0,-50,$offset_z}
	}
	if $speed == 100
	{
		set $c_1 position float[3]{0,-0,$offset_z}
		set $c_2 position float[3]{0,-50,$offset_z}
		set $c_3 position float[3]{0,-50,$offset_z}
	}
}
func int arrondi(float $raw_speed)
{
	float $fDecimal
	int $res
	if ($raw_speed<0)
	{
		$fDecimal=$raw_speed-ceil($raw_speed)
		if ($fDecimal > -0.5)
		{
			$res=ceil($raw_speed)
		}
		else
		{
			$res=floor($raw_speed)
		}
	}
	else
	{
		$fDecimal=$raw_speed-floor($raw_speed)
		if ($fDecimal< 0.5)
		{
			$res=floor($raw_speed)
		}
		else
		{
			$res=ceil($raw_speed)
		}
	}
	return $res
}	
float $throttle
float $brake
float $clutch
int $timer_reset = 100		
while 1
{
	if $car == $localcar
	{
		
		$throttle = get system "car0.controls_throttle"
		$brake = get system "car0.controls_brakes"
		$clutch = get system "car0.controls_clutch"
		//Actions
		
		//Definition des modeles 3D
		rcarmodel $fam_temoin_model = get generic model 21 of $car
		rcarmodel $fam_serre_model = get generic model 22 of $car
		rcarmodel $fam_desserre_model = get generic model 23 of $car
		
		rcarmodel $esp_desactive_model = get generic model 4 of $car
		rcarmodel $esp_active_model = get generic model 23 of $car
		rcarmodel $esp_action_model = get generic model 5 of $car
		
		rcarmodel $marche_arriere_temoin = get generic model 11 of $car
		rcarmodel $point_mort_temoin = get generic model 12 of $car
		rcarmodel $drive_temoin = get generic model 13 of $car
		rcarmodel $impulsionel_temoin = get generic model 14 of $car
		
		rcarmodel $conso_temoin = get generic model 16 of $car
		
		set $fam_temoin_model scriptcontrolled 1
		set $fam_serre_model scriptcontrolled 1
		set $fam_desserre_model scriptcontrolled 1
		
		set $esp_desactive_model scriptcontrolled 1
		set $esp_active_model scriptcontrolled 1
		set $esp_action_model scriptcontrolled 1
		
		set $marche_arriere_temoin scriptcontrolled 1
		set $point_mort_temoin scriptcontrolled 1
		set $drive_temoin scriptcontrolled 1
		set $impulsionel_temoin scriptcontrolled 1
		
		set $conso_temoin scriptcontrolled 1
		
		int $speed = arrondi(get $car tachovelocity*3.71)
		
		$refresh = calculate_sample_hold($car,1000,100,$speed)
		if $speed > 30
		{
			set $conso_temoin position float[3]{0,-50,$offset_z}
		}
		if $speed < 30 and $display !=1
		{
			set $conso_temoin position float[3]{0,0,-0.001}
		}
		if $point_mort == 1
		{
			set $point_mort_temoin position float[3]{0,0,$offset_z}
		}
		if $point_mort == 0
		{
			set $point_mort_temoin position float[3]{0,-10,$offset_z}
		}
		
		if $marche_arriere == 1
		{
			set $marche_arriere_temoin position float[3]{0,0,$offset_z}
		}
		if $marche_arriere == 0
		{
			set $marche_arriere_temoin position float[3]{0,-10,$offset_z}
		}
		if $drive == 1
		{
			set $drive_temoin position float[3]{0,0,$offset_z}
		}
		if $drive == 0
		{
			set $drive_temoin position float[3]{0,-10,$offset_z}
		}
		
		if $impulsionel == 0
		{
			set $impulsionel_temoin position float[3]{0,0,-0.005}
		}
		if $impulsionel == 1
		{
			set $impulsionel_temoin position float[3]{0,-10,0}
		}
		
		if $fam_temoin == 1
		{
			set $fam_temoin_model position float[3]{0,0.0,$offset_z}
		}
		if $fam_temoin == 0
		{
			set $fam_temoin_model position float[3]{0,-10.0,$offset_z}
		}
		if $fam_serre == 1
		{
			if $display == 1
			{
				set $fam_serre_model position float[3]{0,0.0,$offset_z}
				set $fam_desserre_model position float[3]{0,-10.0,$offset_z}
				set $esp_active_model position float[3]{0,-10.0,$offset_z}
				set $esp_desactive_model position float[3]{0,-10.0,$offset_z}
				$compteur++
				if $compteur >= ($time-1)
				{
					$compteur = 0
					$display = 0
					$fam_serre = 0
					set $fam_serre_model position float[3]{0,-10.0,$offset_z}
				}
			}
		}
		if $fam_desserre == 1
		{
			if $display == 1
			{
				set $fam_desserre_model position float[3]{0,0.0,$offset_z}
				set $fam_serre_model position float[3]{0,-10.0,$offset_z}
				set $esp_active_model position float[3]{0,-10.0,$offset_z}
				set $esp_desactive_model position float[3]{0,-10.0,$offset_z}
				$compteur++
				if $compteur >= ($time-1) and $display == 1
				{
					$compteur = 0
					$display = 0
					set $fam_desserre_model position float[3]{0,-10.0,$offset_z}
					$fam_desserre = 0
				}
			}
		}
		if $speed < 4
		{
			$speed = 0
			$refresh = 0
		}
		int $vitesse_decomposee[3]=decompose($speed)
		int $vitesse_u=$vitesse_decomposee.z
		int $vitesse_d=$vitesse_decomposee.y
		int $vitesse_c=$vitesse_decomposee.x
		if $speed > -1
		{
			show_speed($vitesse_u,$vitesse_d,$vitesse_c,$speed)
		}
		if $throttle > 0.00
		{
			$compteur++
			if $throttle > 0.1 and $compteur < 400
			{
				//$refresh = 0
				$compteur = 0
			}
		}
		if $throttle > 0.50
		{
			$compteur++
			if $throttle < 0.35 and $compteur < 400
			{
				//$refresh = 0
				$compteur = 0
			}
		}
		if $clutch > 0.20
		{
			$refresh = 200
		}
		if $brake > 0.01
		{
			$compteur++
			if $brake > 0.60 and $compteur < 400
			{
				$refresh = 200
				$compteur = 0
			}
		}
		if $speed > -1
		{
			int $speed_prev = $speed
			$compteur++
			if $speed > ($speed_prev+4) and $compteur < 400
			{
				$refresh = 0
				$compteur = 0
			}
			if $speed < ($speed_prev-4) and $compteur < 400
			{
				$refresh = 0
				$compteur = 0
			}
		}
		wait($refresh)
	}
	interrupt
}