--[[Run]]

	--Print Version
	local ver=require(car["A-Chassis Tune"].README)
	print("//INSPARE: AC6 Loaded - Build "..ver)
	
	--Runtime Loops
	
	-- ~60 c/s
	game["Run Service"].Stepped:connect(function()
		--Steering
		Steering()
		
		--RPM
		RPM()
		
		--Power
		Engine()
		
		--Update External Values
		_IsOn = script.Parent.IsOn.Value
		_InControls = script.Parent.ControlsOpen.Value
		script.Parent.Values.Gear.Value = _CGear
		script.Parent.Values.RPM.Value = _RPM
		script.Parent.Values.Horsepower.Value = _HP
		script.Parent.Values.Torque.Value = _HP * _Tune.EqPoint / _RPM
		script.Parent.Values.TransmissionMode.Value = _TMode
		script.Parent.Values.Throttle.Value = _GThrot
		script.Parent.Values.Brake.Value = _GBrake
		script.Parent.Values.SteerC.Value = _GSteerC*(1-math.min(car.DriveSeat.Velocity.Magnitude/_Tune.SteerDecay,1-(_Tune.MinSteer/100)))
		script.Parent.Values.SteerT.Value = _GSteerT
		script.Parent.Values.PBrake.Value = _PBrake
		script.Parent.Values.TCS.Value = _TCS
		script.Parent.Values.TCSActive.Value = _TCSActive
		script.Parent.Values.ABS.Value = _ABS
		script.Parent.Values.ABSActive.Value = _ABSActive
		script.Parent.Values.MouseSteerOn.Value = _MSteer
		script.Parent.Values.Velocity.Value = car.DriveSeat.Velocity
	end)
	--Bertox part
	local UNITS	= {{units = "MPH" ,scaling = (10/12) * (60/88) ,maxSpeed = 230,spInc = 20,}}
	local busyPart = nil
	local busyPart2 = nil
	local SpeedDistance = 0
	local AEBActive = false
	local BSMLeft = false
	local BSMRight = false
	local AEBBrake = 0
	local AEBFull = false
	local ACCEnabled = false
	local ACCOriginalSpeed = 75
	local ACCSpeed = ACCOriginalSpeed
	local ACCStop = false
	local bsmbusyPart = nil
	script.Parent.CruiseControl.Set.MouseButton1Down:Connect(function()
		if UNITS[1].scaling*car.DriveSeat.Velocity.Magnitude > 2 then
			ACCOriginalSpeed = UNITS[1].scaling*car.DriveSeat.Velocity.Magnitude
			ACCEnabled = true
			script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255, 85, 0)
			script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255, 85, 0)
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = true
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = true
			script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
		end
	end)
	script.Parent.CruiseControl.Res.MouseButton1Down:Connect(function()
		ACCEnabled = true
		script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
		script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255, 85, 0)
		script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255, 85, 0)
		car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = true
		car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = true
	end)
	script.Parent.CruiseControl.Up.MouseButton1Down:Connect(function()
		ACCOriginalSpeed = ACCOriginalSpeed + 1
		script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
	end)
	script.Parent.CruiseControl.Return.MouseButton1Down:Connect(function()
		if ACCEnabled == true then
			ACCEnabled = false
			_GThrot = 0
			script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
			if (ACCStop == true or ACCEasyBrake == true) then
				ACCStop = false
				ACCEasyBrake = false
				_GBrake = 0
			end
			script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255,255,255)
			script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255,255,255)
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = false
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = false
			
		else
			ACCEnabled = true
			script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
			script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255, 85, 0)
			script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255, 85, 0)
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = true
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = true
		end
	end)
	script.Parent.CruiseControl.Down.MouseButton1Down:Connect(function()
		ACCOriginalSpeed = ACCOriginalSpeed - 1
		script.Parent.CruiseControl.CruiseSpeed.Text = math.floor(ACCOriginalSpeed)
		if ACCOriginalSpeed <= 2 then
			ACCEnabled = false
			_GThrot = 0
			if (ACCStop == true or ACCEasyBrake == true) then
				ACCStop = false
				ACCEasyBrake = false
				_GBrake = 0
			end
			script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255,255,255)
			script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255,255,255)
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = false
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = false
		end
	end)
	--Bertox part end
	-- ~15 c/s
	
	while wait(.0667) do
		--Flip
		if _Tune.AutoFlip then Flip() end
		
		--[[Bertox'Scripts begins here]]
		
	    --AEB
		if _GBrake > 0 and AEBBrake == 0 and ACCStop == false and ACCEasyBrake == false and AEBFull == false and AEBActive == false then
			ACCEnabled = false
			_GThrot = 0
			if ACCStop == true then
				ACCStop = false
				_GBrake = 0
			end
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl.Visible = false
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.CruiseControl2.Visible = false
			script.Parent.CruiseControl.CruiseSpeed.TextColor3 = Color3.fromRGB(255,255,255)
			script.Parent.CruiseControl.Return.ImageColor3 = Color3.fromRGB(255,255,255)
		end
		busyPart = nil
		busyPart2 = nil
		bsmbusyPart = nil
		speed = UNITS[1].scaling*car.DriveSeat.Velocity.Magnitude
		for i,v in pairs(car.Body.Rays:GetChildren()) do
			if speed > 40 then
				ray = Ray.new(v.Position, v.CFrame.lookVector*(speed*speed+speed)/50)
			elseif speed <= 40 then
				ray = Ray.new(v.Position, v.CFrame.lookVector*(speed*speed+speed )/50)
			end
			local part, endPoint = workspace:FindPartOnRayWithIgnoreList(ray,{car.Body,car.Wheels,car.Misc})
			if part then
				if part.Anchored == false then
					busyPart = part
				end
			end
		end
		if busyPart and speed > 3 and (speed < 60 or ACCEnabled == true) and _GSteerT == 0 and _CGear > 0 and car.DriveSeat.Values.AEBisOn.Value == true and car.Electrics.Value == true then
			--print("BLOCKING PART: " ,busyPart.Name, " DISTANCE: " , math.floor((busyPart.Position - car.DriveSeat.Position).Magnitude), " CAR SPEED: ", math.floor(speed), "  -  ", math.floor((busyPart.Position - car.DriveSeat.Position).Magnitude-speed))
			_GBrake = 1
			car.SafetySystems:FireServer("AEB",true)
			_GThrot = 0
			AEBActive = true
			if ACCEnabled == false then
				if AEBBrake == 0 then
					script.AssistantAlarm:Play()
					car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.BRAKE.Visible = true
					script.Parent.AC6_Stock_Gauges.EBrake.Visible = true
				end
			end
			AEBBrake = 1
			if busyPart.Velocity.Magnitude <= 3 then
				AEBFull = true
			elseif ACCEnabled == true then
				TargetSpeed = UNITS[1].scaling*busyPart.Velocity.Magnitude
				ACCSpeed = TargetSpeed
			end
		elseif AEBActive == true then
			AEBActive = false
		elseif AEBActive == false and AEBBrake == 1 then
			AEBBrake = 2
		elseif AEBActive == false and AEBBrake == 2 and AEBFull == false then
			AEBBrake = 0
			car.SafetySystems:FireServer("AEB",false)
			script.Parent.AC6_Stock_Gauges.EBrake.Visible = false
			car.Body.Display.GaugeDisplay.BMWGauges.BMWSystem.SymbolLights.BRAKE.Visible = false
			_GBrake = 0
		elseif AEBFull == true and speed <= 0.1 then
			AEBFull = false
			_GBrake = 0
		elseif ACCEnabled == true then
			if speed < ACCSpeed-5 and ACCStop == false and AEBBrake == 0 then
				_GThrot = 1
				_GBrake = 0
				ACCEasyBrake = false
			elseif speed < ACCSpeed and ACCStop == false and AEBBrake == 0 then
				_GThrot = 0.3
				_GBrake = 0
				ACCEasyBrake = false
			elseif speed > ACCSpeed+5 and ACCStop == false and AEBBrake == 0 then
				_GThrot = 0
				_GBrake = 1
				ACCEasyBrake = true
			elseif speed > ACCSpeed+3 and ACCStop == false and AEBBrake == 0 then
				_GThrot = 0
				_GBrake = 0.4
				ACCEasyBrake = true
			elseif speed > ACCSpeed+2 and ACCStop == false and AEBBrake == 0 then
				_GThrot = 0
				_GBrake = 0.1
				ACCEasyBrake = true
			elseif ACCStop == false and AEBBrake == 0 then
				_GThrot = 0.08
				_GBrake = 0
				ACCEasyBrake = false
			end
			if AEBBrake == 0 then
				if ACCStop == false then
					for i,v in pairs(car.Body.Rays:GetChildren()) do
						if speed > 40 then
							ray = Ray.new(v.Position, v.CFrame.lookVector*(speed*speed+speed)/25)
						elseif speed <= 40 then
							ray = Ray.new(v.Position, v.CFrame.lookVector*(speed*speed+speed )/10)
						end
						local part, endPoint = workspace:FindPartOnRayWithIgnoreList(ray,{car.Body,car.Wheels,car.Misc})
						if part then
							if part.Anchored == false then
								busyPart2 = part
							end
						end
					end
				else
					for i,v in pairs(car.Body.Rays:GetChildren()) do
						ray = Ray.new(v.Position, v.CFrame.lookVector*20)
						local part, endPoint = workspace:FindPartOnRayWithIgnoreList(ray,{car.Body,car.Wheels,car.Misc})
						if part then
							if part.Anchored == false then
								busyPart2 = part
							end
						end
					end
				end
				if busyPart2 == nil then
					ACCSpeed = ACCOriginalSpeed
					if ACCEasyBrake == false then
						_GBrake = 0
					end
					ACCStop = false
				elseif busyPart2.Velocity.Magnitude < 3 then
					ACCStop = true
					_GThrot = 0
					_GBrake = 1
					ACCSpeed = UNITS[1].scaling*busyPart2.Velocity.Magnitude
				else
					ACCStop = false
					ACCSpeed = UNITS[1].scaling*busyPart2.Velocity.Magnitude
				end
			end
				end
		BSMLeft = false
		BSMRight = false
		for i,v in pairs(car.Body.BSM:GetChildren()) do
			ray = Ray.new(v.Position, v.CFrame.lookVector*25)
			local part, endPoint = workspace:FindPartOnRayWithIgnoreList(ray,{car.Body,car.Wheels,car.Misc})
			if part then
				if part.Anchored == false then
					bsmbusyPart = part
					if v.Name:sub(1,1) == "L" then
						BSMLeft = true
					else
						BSMRight = true
					end
				end
			end
		end
		car.SafetySystems:FireServer("BSM",BSMLeft,BSMRight)
		--[[Bertox'Scripts ends here]]
	end