rcar $car=get scriptowner car
float $throttle
float $finalbrake
float $brake
float $wheel0_vel
float $wheel1_vel
float $wheel2_vel
float $wheel3_vel
float $rearbrake2
float $rearbrake3
float $brake0stat = 7000
float $brake1stat = 7000
int $braking = 2500
int $gear
int $fam = 1
float $front
float $rear
rcarmodel $parking = get generic model 25 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
while 1
{
	$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"
	$rearbrake2 = get system "car0.wheel2.max_braking"
	$rearbrake3 = get system "car0.wheel3.max_braking"
	$brake = get $car brake
	$throttle = get $car throttle
	$gear = get $car gear
	//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
	}
	if $fam == 1
	{
		$rearbrake2 = $braking
		$rearbrake3 = $braking
		set $parking position float[3]{0.0,0.0,0.0}
		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++
		if $compteur > $time
		{
			set $message_serre position float[3]{0.0,-999.0,$offset_z}
		}
		if $finalbrake > 1.0
		{
			$finalbrake = 1.0
		}
		$brake0stat = 0
		$brake1stat = 0
		$rear = 1
		$front = 0
		if ($wheel2_vel) < $wheel0_vel-3
		{
			$rearbrake2-=10
			$finalbrake-=0.010
			if $rearbrake2 < 0
			{
				$rearbrake2 = 0
			}
		}
		else
		{
			$rearbrake2+=10
			$finalbrake+=0.010
			if $rearbrake2 > $braking
			{
				$rearbrake2 = $braking
			}
		}
		if ($wheel3_vel) < $wheel1_vel-3
		{
			$rearbrake3-=10
			$finalbrake-=0.010
			if $rearbrake3 < 0
			{
				$rearbrake3= 0
			}
		}
		else
		{
			$rearbrake3+=10
			$finalbrake+=0.010
			if $rearbrake3 > $braking
			{
				$rearbrake3 = $braking
			}
		}
		if $finalbrake < 0
		{
			$finalbrake = 0
		}
		if $throttle > 0.02 and ($wheel2_vel+$wheel3_vel > 0.2) and $gear > 1
		{
			$fam = 0
		}
		if $throttle > 0.02 and ($wheel2_vel+$wheel3_vel < -0.2) and $gear == 1
		{
			$fam = 0
		}
	}
	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++
		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 = $brake
		$rear = 0.4
		$front = 0.6
	}
	set system "car0.wheel0.braking_factor" to $front;
	set system "car0.wheel1.braking_factor" to $front;
	set system "car0.wheel2.max_braking" to $rearbrake2;
	set system "car0.wheel3.max_braking" to $rearbrake3;
	set system "car0.wheel2.braking_factor" to $rear;
	set system "car0.wheel3.braking_factor" to $rear;
	set $car brake $finalbrake
	interrupt
}
		
	