--Powertrain
wait()

function Auto()
	local maxSpin=0
	if Rear.Wheel.RotVelocity.Magnitude>maxSpin then maxSpin = Rear.Wheel.RotVelocity.Magnitude end
	if _IsOn then
		if _CGear == 0 and not _Tune.NeutralRev then _CGear = 1 _ClPressing = false end
		if _CGear >= 1 then
			if (_CGear==1 and _InBrake > 0 and bike.DriveSeat.Velocity.Magnitude < 10) and _Tune.NeutralRev then
				_CGear = 0 _ClPressing = false
			elseif bike.DriveSeat.Velocity.Magnitude > 10 then
				if _Tune.AutoShiftMode == "RPM" and not _ClutchSlip then
					if _RPM>(_Tune.PeakRPM+_Tune.AutoUpThresh) then
						if not _ShiftUp and not _Shifting then _ShiftUp = true end
					elseif math.max(math.min(maxSpin*_Tune.Ratios[_CGear+1]*fFDr,_Tune.Redline+100),_Tune.IdleRPM)<(_Tune.PeakRPM-_Tune.AutoDownThresh) and _CGear>1 then
						if not _ShiftDn and not _Shifting then _ShiftDn = true end 
					end
				else
					if bike.DriveSeat.Velocity.Magnitude > math.ceil(wDRatio*(_Tune.PeakRPM+_Tune.AutoUpThresh)/_Tune.Ratios[_CGear+1]/fFD) then
						if not _ShiftUp and not _Shifting then _ShiftUp = true end
					elseif bike.DriveSeat.Velocity.Magnitude < math.ceil(wDRatio*(_Tune.PeakRPM-_Tune.AutoDownThresh)/_Tune.Ratios[_CGear+1]/fFD) and _CGear>1 then
						if not _ShiftDn and not _Shifting then _ShiftDn = true end
					end
				end
			end
		else
			if (_InThrot-(_Tune.IdleThrottle/100) > 0 and bike.DriveSeat.Velocity.Magnitude < 10) and _Tune.NeutralRev then
				_CGear = 1 _ClPressing = false
			end
		end
	end 
end

function Gear()
	local maxSpin=0
	if Rear.Wheel.RotVelocity.Magnitude>maxSpin then maxSpin = Rear.Wheel.RotVelocity.Magnitude end
	if _ShiftUp and not _Shifting then
		local AutoCheck if _TMode~="Manual" then AutoCheck = true end
		if (_TMode == "Manual" and ((not _Tune.QuickShifter and not _ClPressing) or (not _ClPressing and (_Tune.QuickShifter and tick()-_BTick>_Tune.QuickShiftTime)))) or _CGear == #_Tune.Ratios-1 or (_TMode ~= "Manual" and not _IsOn) then _ShiftUp = false return end
		local NextGear = math.min(_CGear+1,#_Tune.Ratios)
		if _TMode~="Manual" then
			_Shifting = true
			if _CGear>0 then 
				if _Tune.AutoShiftType=="DCT" then 
					wait(_Tune.ShiftUpTime)
				elseif _Tune.AutoShiftType=="Rev" then
					repeat wait() until _RPM<=math.max(math.min(maxSpin*_Tune.Ratios[NextGear]*fFDr,_Tune.Redline-_Tune.RevBounce),_Tune.IdleRPM) or not _IsOn or _ShiftDn 
				end
			end
		end
		_ShiftUp = false
		_Shifting = false
		if _TMode ~= "Manual" and not _IsOn then return end
		_CGear = math.min(_CGear+1,#_Tune.Ratios-1)
		if _TMode ~= "Manual" or (_TMode == "Manual" and (_CGear == 1 or AutoCheck)) and _IsOn then _ClPressing = false end
	end
	if _ShiftDn and not _Shifting then
		local AutoCheck if _TMode~="Manual" then AutoCheck = true end
		if (_TMode == "Manual" and ((not _Tune.QuickShifter and not _ClPressing) or (not _ClPressing and (_Tune.QuickShifter and tick()-_TTick>_Tune.QuickShiftTime)))) or _CGear == 0 or (_TMode ~= "Manual" and not _IsOn) then _ShiftDn = false return end
		local PrevGear = math.min(_CGear,#_Tune.Ratios)
		if _TMode~="Manual" then
			_Shifting = true
			if _CGear>1 then 
				if _Tune.AutoShiftType=="DCT" then 
					wait(_Tune.ShiftDnTime)
				elseif _Tune.AutoShiftType=="Rev" then
					repeat wait() until _RPM>=math.max(math.min(maxSpin*_Tune.Ratios[PrevGear]*fFDr,_Tune.Redline-_Tune.RevBounce),_Tune.IdleRPM) or not _IsOn or _ShiftUp
				end
			end
		end
		_ShiftDn = false
		_Shifting = false
		if _TMode ~= "Manual" and not _IsOn then return end
		_CGear = math.max(_CGear-1,0)
		if _TMode ~= "Manual" or (_TMode == "Manual" and (_CGear == 0 or AutoCheck)) and _IsOn then _ClPressing = false end
	end
end

local tqTCS = 1
local sthrot = 0
local _StallOK = false
local ticc = tick()
local bticc = tick()