local z=_Tune.FinalDrive*_Tune.FDMult local y=(z*30)/math.pi local H=(workspace.Gravity*_Tune.InclineComp)/32.2 local d=(wDia*math.pi)/60 local m=CFrame.Angles(math.pi/2,-math.pi/2,0)local X=CFrame.Angles(0,math.pi,0)local b=.70710678118655 local q=_Tune.Horsepower/100 local i=_Tune.PeakRPM/1000 local U=_Tune.PeakSharpness local N=_Tune.CurveMult function FGC(z)z=z/1000 return(-(z-i)^2*math.min(q/i^2,N^(i/q))+q)*(z-z^U/(U*i)^(U-1))end local j=FGC(_Tune.PeakRPM)function GetCurve(y,H)local d=math.max((FGC(y)*_Tune.Horsepower)/j,0)return d,(((d*(_Tune.EqPoint/y))*_Tune.Ratios[H+2])*z)*hpScaling end local M=true local P={}local C=100 if M then for z,y in pairs(_Tune.Ratios)do local H={}for y=math.floor(_Tune.IdleRPM/C),math.ceil((_Tune.Redline+100)/C),1 do local d={}d.Horsepower,d.Torque=GetCurve(y*C,z-2)hp1,tq1=GetCurve((y+1)*C,z-2)d.HpSlope=(hp1-d.Horsepower)/(C/1000)d.TqSlope=(tq1-d.Torque)/(C/1000)H[y]=d end table.insert(P,H)end end function RPM()if _CGear==0 then _ClutchOn=false end local z=_Tune.IdleRPM if not _IsOn then z=0 _CGear=0 _ClutchOn=false _GThrot=_Tune.IdleThrottle/100 end local H=0 for z,y in pairs(Drive)do if y.RotVelocity.Magnitude>H then H=y.RotVelocity.Magnitude end end if _ClutchOn then local d=math.max(math.min((H*_Tune.Ratios[_CGear+2])*y,_Tune.Redline+100),z)local m=math.min(math.abs(d-_RPM)/_Tune.ClutchTol,.9)_RPM=_RPM*m+d*(1-m)else if _GThrot-_Tune.IdleThrottle/100>0 then if _RPM>_Tune.Redline then _RPM=_RPM-_Tune.RevBounce*2 else _RPM=math.min(_RPM+_Tune.RevAccel*_GThrot,_Tune.Redline+100)end else _RPM=math.max(_RPM-_Tune.RevDecay,z)end end _spLimit=(_Tune.Redline+100)/(y*_Tune.Ratios[_CGear+2])if _RPM>_Tune.Redline then if _CGear<#_Tune.Ratios-2 then _RPM=_RPM-_Tune.RevBounce else _RPM=_RPM-_Tune.RevBounce*.5 end end end function Engine()local q=0 for z,y in pairs(Drive)do if y.RotVelocity.Magnitude>q then q=y.RotVelocity.Magnitude end end if _ClutchOn then if M then local z=P[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/C)]_HP=z.Horsepower+(z.HpSlope*(_RPM-math.floor(_RPM/C)))/1000 _OutTorque=z.Torque+(z.TqSlope*(_RPM-math.floor(_RPM/C)))/1000 else _HP,_OutTorque=GetCurve(_RPM,_CGear)end local z=car.DriveSeat.CFrame.lookVector.y*H if _CGear==-1 then z=-z end _OutTorque=_OutTorque*math.max(.3,.3+z)else _HP,_OutTorque=0,0 end if _TMode=="\065\117\116\111"and _IsOn then _ClutchOn=true if _CGear==0 then _CGear=1 end if _CGear>=1 then if _CGear==1 and(_GBrake>0 and car.DriveSeat.Velocity.Magnitude<0)then _CGear=-1 else if _Tune.AutoShiftMode=="\082\080\077"then if _RPM>_Tune.PeakRPM+_Tune.AutoUpThresh then _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif math.max(math.min((q*_Tune.Ratios[_CGear+1])*y,_Tune.Redline+100),_Tune.IdleRPM)<_Tune.PeakRPM-_Tune.AutoDownThresh then _CGear=math.max(_CGear-1,1)end else if car.DriveSeat.Velocity.Magnitude>math.ceil(((d*(_Tune.PeakRPM+_Tune.AutoUpThresh))/_Tune.Ratios[_CGear+2])/z)then _CGear=math.min(_CGear+1,#_Tune.Ratios-2)elseif car.DriveSeat.Velocity.Magnitude<math.ceil(((d*(_Tune.PeakRPM-_Tune.AutoDownThresh))/_Tune.Ratios[_CGear+1])/z)then _CGear=math.max(_CGear-1,1)end end end else if _GThrot-_Tune.IdleThrottle/100>0 and car.DriveSeat.Velocity.Magnitude<0 then _CGear=1 end end end local i=0 local U=0 local N=0 local j=0 for z,y in pairs(car.Wheels:GetChildren())do if y.Name=="\070\076"or y.Name=="\070\082"or y.Name=="\070"then i=i+y.RotVelocity.Magnitude U=U+1 elseif y.Name=="\082\076"or y.Name=="\082\082"or y.Name=="\082"then N=N+y.RotVelocity.Magnitude j=j+1 end end i=i/U N=N/j local E=(i+N)/2 for z,y in pairs(car.Wheels:GetChildren())do local H=(CFrame.new(y.Position-(y.Arm.CFrame*m).lookVector,y.Position)*X).lookVector local d=1 local q=1 if y.Name=="\070\076"or y.Name=="\082\076"then d=-1 end if _Tune.Config=="\065\087\068"then _OutTorque=_OutTorque*b end if y.Name=="\070\076"or y.Name=="\070\082"then q=math.max(0,math.min(1,1+(((y.RotVelocity.Magnitude-i)/i)/(math.max(_Tune.FDiffSlipThres,1)/100))*((_Tune.FDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then q=math.max(0,math.min(1,q*(1+(((i-E)/E)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end elseif y.Name=="\082\076"or y.Name=="\082\082"then q=math.max(0,math.min(1,1+(((y.RotVelocity.Magnitude-N)/N)/(math.max(_Tune.RDiffSlipThres,1)/100))*((_Tune.RDiffLockThres-50)/50)))if _Tune.Config=="\065\087\068"then q=math.max(0,math.min(1,q*(1+(((N-E)/E)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50))))end end _TCSActive=false _ABSActive=false if _PBrake and(_Tune.Config~="\070\087\068"and((y.Name=="\070\076"or y.Name=="\070\082")and car.DriveSeat.Velocity.Magnitude<20 or(y.Name=="\082\082"or y.Name=="\082\076")and car.DriveSeat.Velocity.Magnitude>=20)or _Tune.Config=="\070\087\068"and(y.Name=="\082\082"or y.Name=="\082\076"))then y["\035\065\086"].maxTorque=Vector3.new(math.abs(H.x),math.abs(H.y),math.abs(H.z))*PBrakeForce y["\035\065\086"].angularvelocity=Vector3.new()else if(_TMode=="\077\097\110\117\097\108"or _TMode=="\083\101\109\105")and _GBrake==0 or _TMode=="\065\117\116\111"and(_CGear>-1 and _GBrake==0 or _CGear==-1 and _GThrot-_Tune.IdleThrottle/100==0)then local z=false for H,d in pairs(Drive)do if d==y then z=true end end if z then local z=1 if not car.DriveSeat.IsOn.Value then z=0 end local m=_GThrot if _TMode=="\065\117\116\111"and _CGear==-1 then m=_GBrake end local X=1 if _TCS then X=1-math.min(math.max(0,math.abs(y.RotVelocity.Magnitude*(y.Size.x/2)-y.Velocity.Magnitude)-_Tune.TCSThreshold)/_Tune.TCSGradient,1)*(1-_Tune.TCSLimit/100)end if X<1 then _TCSActive=true end local b=1 if _CGear==-1 then b=-1 end y["\035\065\086"].maxTorque=(((((Vector3.new(math.abs(H.x),math.abs(H.y),math.abs(H.z))*_OutTorque)*(1+(y.RotVelocity.Magnitude/60)^1.15))*m)*X)*q)*z y["\035\065\086"].angularvelocity=((H*d)*_spLimit)*b else y["\035\065\086"].maxTorque=Vector3.new()y["\035\065\086"].angularvelocity=Vector3.new()end else local z=_GBrake if _TMode=="\065\117\116\111"and _CGear==-1 then z=_GThrot end local d=1 if _ABS and math.abs(y.RotVelocity.Magnitude*(y.Size.x/2)-y.Velocity.Magnitude)-_Tune.ABSThreshold>0 then d=0 end if d<1 then _ABSActive=true end if y.Name=="\070\076"or y.Name=="\070\082"or y.Name=="\070"then y["\035\065\086"].maxTorque=((Vector3.new(math.abs(H.x),math.abs(H.y),math.abs(H.z))*FBrakeForce)*z)*d else y["\035\065\086"].maxTorque=((Vector3.new(math.abs(H.x),math.abs(H.y),math.abs(H.z))*RBrakeForce)*z)*d end y["\035\065\086"].angularvelocity=Vector3.new()end end end end


	local fFD = _Tune.FinalDrive*_Tune.FDMult
	local fFDr = fFD*30/math.pi
	local cGrav = workspace.Gravity*_Tune.InclineComp/32.2
	local wDRatio = wDia*math.pi/60
	local cfWRot = CFrame.Angles(math.pi/2,-math.pi/2,0)
	local cfYRot = CFrame.Angles(0,math.pi,0)
	local rtTwo = (2^.5)/2

	--Horsepower Curve
	local fgc_h=_Tune.Horsepower/100
	local fgc_n=_Tune.PeakRPM/1000
	local fgc_a=_Tune.PeakSharpness
	local fgc_c=_Tune.CurveMult
	
	function FGC(x)
		x=x/1000
		return (((-(x-fgc_n)^2)*math.min(fgc_h/(fgc_n^2),fgc_c^(fgc_n/fgc_h)))+fgc_h)*(x-((x^fgc_a)/((fgc_a*fgc_n)^(fgc_a-1))))
	end
	local PeakFGC = FGC(_Tune.PeakRPM)
	
	--Plot Current Horsepower
	function GetCurve(x,gear)
		local hp=math.max((FGC(x)*_Tune.Horsepower)/PeakFGC,0)
		return hp,hp*(_Tune.EqPoint/x)*_Tune.Ratios[gear+2]*fFD*hpScaling
	end
	
	--Output Cache
	local CacheTorque = true
	local HPCache = {}
	local HPInc = 100
	
	if CacheTorque then
		for gear,ratio in pairs(_Tune.Ratios) do
			local hpPlot = {}
			for rpm = math.floor(_Tune.IdleRPM/HPInc),math.ceil((_Tune.Redline+100)/HPInc) do
				local tqPlot = {}
				tqPlot.Horsepower,tqPlot.Torque = GetCurve(rpm*HPInc,gear-2)
				hp1,tq1 = GetCurve((rpm+1)*HPInc,gear-2)
				tqPlot.HpSlope = (hp1 - tqPlot.Horsepower)/(HPInc/1000)
				tqPlot.TqSlope = (tq1 - tqPlot.Torque)/(HPInc/1000)
				hpPlot[rpm] = tqPlot
			end
			table.insert(HPCache,hpPlot)
		end
	end
	
	--Powertrain
	
	--Update RPM
	function RPM()
		--Neutral Gear
		if _CGear==0 then _ClutchOn = false end
	
		--Car Is Off
		local revMin = _Tune.IdleRPM
		if not _IsOn then 
			revMin = 0 
			_CGear = 0
			_ClutchOn = false
			_GThrot = _Tune.IdleThrottle/100
		end
		
		--Determine RPM
		local maxSpin=0
		for i,v in pairs(Drive) do
			if v.RotVelocity.Magnitude>maxSpin then maxSpin = v.RotVelocity.Magnitude end
		end
		if _ClutchOn then
			local aRPM = math.max(math.min(maxSpin*_Tune.Ratios[_CGear+2]*fFDr,_Tune.Redline+100),revMin)
			local clutchP = math.min(math.abs(aRPM-_RPM)/_Tune.ClutchTol,.9)
			_RPM = _RPM*clutchP  +  aRPM*(1-clutchP)
		else
			if _GThrot-(_Tune.IdleThrottle/100)>0 then
				if _RPM>_Tune.Redline then
					_RPM = _RPM-_Tune.RevBounce*2
				else
					_RPM = math.min(_RPM+_Tune.RevAccel*_GThrot,_Tune.Redline+100)
				end
			else
				_RPM = math.max(_RPM-_Tune.RevDecay,revMin)
			end
		end
		
		--Rev Limiter
		_spLimit = (_Tune.Redline+100)/(fFDr*_Tune.Ratios[_CGear+2])
		if _RPM>_Tune.Redline then 
			if _CGear<#_Tune.Ratios-2 then
				_RPM = _RPM-_Tune.RevBounce
			else
				_RPM = _RPM-_Tune.RevBounce*.5
			end
		end
	end
	
	--Apply Power
	function Engine()
		--Get Torque
		local maxSpin=0
		for i,v in pairs(Drive) do
			if v.RotVelocity.Magnitude>maxSpin then maxSpin = v.RotVelocity.Magnitude end
		end
		if _ClutchOn then
			if CacheTorque then
				local cTq = HPCache[_CGear+2][math.floor(math.min(_Tune.Redline,math.max(_Tune.IdleRPM,_RPM))/HPInc)]
				_HP = cTq.Horsepower+(cTq.HpSlope*(_RPM-math.floor(_RPM/HPInc))/1000)
				_OutTorque = cTq.Torque+(cTq.TqSlope*(_RPM-math.floor(_RPM/HPInc))/1000)
			else
				_HP,_OutTorque = GetCurve(_RPM,_CGear)
				
			end
			local iComp =(car.DriveSeat.CFrame.lookVector.y)*cGrav
			if _CGear==-1 then iComp=-iComp end
			_OutTorque = _OutTorque*math.max(.3,(.3+iComp))--here dum dum
		else
			_HP,_OutTorque = 0,0
		end
		
		--Automatic Transmission
		if _TMode == "Auto" and _IsOn then
			_ClutchOn = true
			if _CGear == 0 then _CGear = 1 end
			if _CGear >= 1 then
				if _CGear==1 and _GBrake > 0  and car.DriveSeat.Velocity.Magnitude < 0 then
					_CGear = -1
				else
					if _Tune.AutoShiftMode == "RPM" then
						if _RPM>(_Tune.PeakRPM+_Tune.AutoUpThresh) then
							_CGear=math.min(_CGear+1,#_Tune.Ratios-2) 
						elseif math.max(math.min(maxSpin*_Tune.Ratios[_CGear+1]*fFDr,_Tune.Redline+100),_Tune.IdleRPM)<(_Tune.PeakRPM-_Tune.AutoDownThresh) then
							_CGear=math.max(_CGear-1,1)
						end
					else
						if car.DriveSeat.Velocity.Magnitude > math.ceil(wDRatio*(_Tune.PeakRPM+_Tune.AutoUpThresh)/_Tune.Ratios[_CGear+2]/fFD) then
							_CGear=math.min(_CGear+1,#_Tune.Ratios-2)
						elseif car.DriveSeat.Velocity.Magnitude < math.ceil(wDRatio*(_Tune.PeakRPM-_Tune.AutoDownThresh)/_Tune.Ratios[_CGear+1]/fFD) then
							_CGear=math.max(_CGear-1,1)
						end
					end
				end
			else
				if _GThrot-(_Tune.IdleThrottle/100) > 0 and car.DriveSeat.Velocity.Magnitude < 0 then
					_CGear = 1
				end
			end
		end 
		
		--Average Rotational Speed Calculation
		local fwspeed=0
		local fwcount=0
		local rwspeed=0
		local rwcount=0
		
		for i,v in pairs(car.Wheels:GetChildren()) do
			if v.Name=="FL" or v.Name=="FR" or v.Name == "F" then
				fwspeed=fwspeed+v.RotVelocity.Magnitude
				fwcount=fwcount+1
			elseif v.Name=="RL" or v.Name=="RR" or v.Name == "R" then
				rwspeed=rwspeed+v.RotVelocity.Magnitude
				rwcount=rwcount+1
			end
		end
		fwspeed=fwspeed/fwcount
		rwspeed=rwspeed/rwcount	
		local cwspeed=(fwspeed+rwspeed)/2
		
		--Update Wheels
		for i,v in pairs(car.Wheels:GetChildren()) do
			--Reference Wheel Orientation
			local Ref=(CFrame.new(v.Position-((v.Arm.CFrame*cfWRot).lookVector),v.Position)*cfYRot).lookVector
			local aRef=1
			local diffMult=1
			if v.Name=="FL" or v.Name=="RL" then aRef=-1 end
			
			--AWD Torque Scaling
			if _Tune.Config == "AWD" then _OutTorque = _OutTorque*rtTwo end
			
			--Differential/Torque-Vectoring
			if v.Name=="FL" or v.Name=="FR" then
				diffMult=math.max(0,math.min(1,1+((((v.RotVelocity.Magnitude-fwspeed)/fwspeed)/(math.max(_Tune.FDiffSlipThres,1)/100))*((_Tune.FDiffLockThres-50)/50))))
				if _Tune.Config == "AWD" then
					diffMult=math.max(0,math.min(1,diffMult*(1+((((fwspeed-cwspeed)/cwspeed)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50)))))
				end
			elseif v.Name=="RL" or v.Name=="RR" then
				diffMult=math.max(0,math.min(1,1+((((v.RotVelocity.Magnitude-rwspeed)/rwspeed)/(math.max(_Tune.RDiffSlipThres,1)/100))*((_Tune.RDiffLockThres-50)/50))))
				if _Tune.Config == "AWD" then
					diffMult=math.max(0,math.min(1,diffMult*(1+((((rwspeed-cwspeed)/cwspeed)/(math.max(_Tune.CDiffSlipThres,1)/100))*((_Tune.CDiffLockThres-50)/50)))))
				end
			end
			
			_TCSActive = false
			_ABSActive = false
			--Output
			if _PBrake and ((_Tune.Config ~= "FWD" and (((v.Name=="FL" or v.Name=="FR") and car.DriveSeat.Velocity.Magnitude<20) or ((v.Name=="RR" or v.Name=="RL") and car.DriveSeat.Velocity.Magnitude>=20))) or (_Tune.Config == "FWD" and (v.Name=="RR" or v.Name=="RL"))) then
				--PBrake
				v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*PBrakeForce
				v["#AV"].angularvelocity=Vector3.new()
			else
				--Apply Power
				if ((_TMode == "Manual" or _TMode == "Semi") and _GBrake==0) or (_TMode == "Auto" and ((_CGear>-1 and _GBrake==0 ) or (_CGear==-1 and _GThrot-(_Tune.IdleThrottle/100)==0 )))then
					local driven = false
					for _,a in pairs(Drive) do if a==v then driven = true end end
					if driven then
						local on=1
						if not car.DriveSeat.IsOn.Value then on=0 end
						local throt = _GThrot
						if _TMode == "Auto" and _CGear==-1 then throt = _GBrake end
						
						--Apply TCS
						local tqTCS = 1
						if _TCS then
							tqTCS = 1-(math.min(math.max(0,math.abs(v.RotVelocity.Magnitude*(v.Size.x/2) - v.Velocity.Magnitude)-_Tune.TCSThreshold)/_Tune.TCSGradient,1)*(1-(_Tune.TCSLimit/100)))
						end
						if tqTCS < 1 then
							_TCSActive = true
						end
						
						--Update Forces
						local dir = 1
						if _CGear==-1 then dir = -1 end
						v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*_OutTorque*(1+(v.RotVelocity.Magnitude/60)^1.15)*throt*tqTCS*diffMult*on
						v["#AV"].angularvelocity=Ref*aRef*_spLimit*dir
					else
						v["#AV"].maxTorque=Vector3.new()
						v["#AV"].angularvelocity=Vector3.new()
					end
					
				--Brakes
				else
					local brake = _GBrake
					if _TMode == "Auto" and _CGear==-1 then brake = _GThrot end
					
					--Apply ABS
					local tqABS = 1
					if _ABS and math.abs(v.RotVelocity.Magnitude*(v.Size.x/2) - v.Velocity.Magnitude)-_Tune.ABSThreshold>0 then
						tqABS = 0
					end
					if tqABS < 1 then
						_ABSActive = true
					end
					
					--Update Forces
					if v.Name=="FL" or v.Name=="FR" or v.Name=="F" then
						v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*FBrakeForce*brake*tqABS
					else
						v["#AV"].maxTorque=Vector3.new(math.abs(Ref.x),math.abs(Ref.y),math.abs(Ref.z))*RBrakeForce*brake*tqABS
					end
					v["#AV"].angularvelocity=Vector3.new()
				end
			end
		end
	end


