rcar $car = get local car
rcar $localcar = get local car
float $throttle
float $finalbrake
float $wheel0_vel
float $wheel1_vel
float $wheel2_vel
float $wheel3_vel
float $brake
int $gear
int $fam = 1
rcarmodel $parking = get generic model 54 of $car
rcarmodel $message_serre = get generic model 15 of $car
rcarmodel $message_desserre = get generic model 14 of $car
int $compteur = 0
int $compteur_2 = 0
int $time = 5000
float $offset_z=-0.001
shared float $spin_rear
float $speed
float $factor = 0.16
while 1
{
	if $car == $localcar
	{
		$throttle = get system "car0.controls_throttle"
		$gear = get $car gear
		$wheel0_vel = get system "car0.wheel0.rotvel"
		$wheel1_vel = get system "car0.wheel1.rotvel"
		$wheel2_vel = get system "car0.wheel2.rotvel"
		$wheel3_vel = get system "car0.wheel3.rotvel"
		$speed=($wheel0_vel+$wheel1_vel+$wheel2_vel+$wheel3_vel)/4
		$brake = get $car brake
		float $gforce[3] = get $car gforce
		float $gforce_lon=$gforce.z/10.0
		float $deccel = -0.295
		set $parking scriptcontrolled 1
		//set $message_serre scriptcontrolled 1
		//set $message_desserre scriptcontrolled 1
		if is key 32 pressed 
		{
			//is space is pressed
			$fam+= 1
			wait(200)
		}
		if $fam > 1
		{
			$fam = 0
			set $message_serre position float[3]{0.0,-999.0,$offset_z}
			set $message_desserre position float[3]{0.0,-999.0,$offset_z}
			$compteur = 0
			$compteur_2 = 0
			$finalbrake = 0.0
		}
		if $fam == 1
		{
			set $parking position float[3]{0.0,0.0,-0.00}
			set $message_serre position float[3]{0.0,0.0,$offset_z}
			set $message_desserre position float[3]{0.0,-999.0,$offset_z}
			$compteur++
			$compteur_2 = 0
			$factor = 1.00
			//-0.15g
			if abs($speed) > 10.0 and $gforce_lon > $deccel
			{
				if $finalbrake < 1.0
				{
					$finalbrake+=0.005
				}
			}
			if abs($speed) > 10.0 and $gforce_lon <  $deccel
			{
				if $finalbrake > 0.0
				{
					$finalbrake-=0.005
				}
			}
			if abs($speed) < 10.0
			{
				if $finalbrake < 1.0
				{
					$finalbrake+=0.05
				}
			}
		}
		if $compteur > $time
		{
			set $message_serre position float[3]{0.0,-999.0,$offset_z}
		}
		if $throttle > 0.02 and ($wheel2_vel+$wheel3_vel > 0.2) and $gear > 1 and $gear !=0
		{
			$fam = 0
			$finalbrake = 0.00
		}
		if $throttle > 0.02 and ($wheel2_vel+$wheel3_vel < -0.2) and $gear == 1 and $gear !=0
		{
			$fam = 0
			$finalbrake = 0.00
		}
		if $fam == 0
		{
			set $message_desserre position float[3]{0.0,0.0,$offset_z}
			set $message_serre position float[3]{0.0,-999.0,$offset_z}
			$compteur_2++
			$compteur = 0
			$factor = 0.40
			if $compteur_2 > $time
			{
				set $message_desserre position float[3]{0.0,-999.0,$offset_z}
			}
			set $parking position float[3]{0.0,-100.0,0.0}
			$finalbrake = 0.00
		}
		set system "car0.wheel0.braking_factor" to 1.0*(1.0-$factor);
		set system "car0.wheel1.braking_factor" to 1.0*(1.0-$factor);
		set system "car0.wheel2.braking_factor" to 1.0*$factor;
		set system "car0.wheel3.braking_factor" to 1.0*$factor;
		set $car brake $finalbrake+$brake
	}
	interrupt
}
		
	