--[[
		___      _______                _     
	   / _ |____/ ___/ /  ___ ____ ___ (_)__ 
	  / __ /___/ /__/ _ \/ _ `(_-<(_-</ (_-<
	 /_/ |_|   \___/_//_/\_,_/___/___/_/___/
 						SecondLogic @ Inspare
]]

local FE = workspace.FilteringEnabled
local car = script.Parent.Car.Value
local handler = car:WaitForChild("AC6_FE_Sounds")
local _Tune = require(car["A-Chassis Tune"])
local on = 0
local mult=0
local det=.13
local trm=.4
local trmmult=0
local trmon=0
local throt=0
local redline=0
local shift=0

script:WaitForChild("Rev")
script:WaitForChild("Rel")

script.Parent.Values.Gear.Changed:connect(function()
	mult=1
	if script.Parent.Values.RPM.Value>5000 then
		shift=.2
	end
end)

for i,v in pairs(car.DriveSeat:GetChildren()) do
	for _,a in pairs(script:GetChildren()) do
		if v.Name==a.Name then v:Stop() wait() v:Destroy() end
	end
end

handler:FireServer("newSound","Rev",car.DriveSeat,script.Rev.SoundId,0,script.Rev.Volume,true)
handler:FireServer("newSound","Rel",car.DriveSeat,script.Rel.SoundId,0,script.Rel.Volume,true)
handler:FireServer("playSound","Rev")
handler:FireServer("playSound","Rel")
car.DriveSeat:WaitForChild("Rev")
car.DriveSeat:WaitForChild("Rel")

while wait() do
	mult=math.max(0,mult-.1)
	local _RPM = script.Parent.Values.RPM.Value
	
	if script.Parent.Values.Throttle.Value <= _Tune.IdleThrottle/100 then
		throt = math.max(.3,throt-.2)
		trmmult = math.max(0,trmmult-.05)
		trmon = 1		
	else
		throt = math.min(1,throt+.1)
		trmmult = 1
		trmon = 0		
	end	
	shift = math.min(1,shift+.2)	
	if script.Parent.Values.RPM.Value > _Tune.Redline-_Tune.RevBounce/4 and script.Parent.Values.Throttle.Value > _Tune.IdleThrottle/100 then
		redline=.5
	else
		redline=1
	end
	
	if script.Parent.Values.PreOn.Value then
		on=1
		local _Rev
		local t1 = tick()
		local check
		while script.Parent.Values.PreOn.Value do wait()
			local t2 = tick()
			local curr
			if not check then
				curr = t2-t1
				if curr>=.25 then
					check=true
					t1 = tick()
				else
					_Rev = curr*2
				end
			else
				curr = t2-t1
				_Rev = math.max(.5-(curr),0.12)
			end
			RevVolume = (throt*shift*redline)+(trm*trmon*trmmult*(1-throt)*math.sin(tick()*50)) * on
			RevPitch = (math.max((((script.Rev.SetPitch.Value + script.Rev.SetRev.Value*_Rev))*on^2)+(det*mult*math.sin(80*tick())),script.Rev.SetPitch.Value)) * on
			RelVolume = (1 - RevVolume) * on / 0.5
			RelPitch = (math.max((((script.Rel.SetPitch.Value + script.Rel.SetRev.Value*_Rev))*on^2)+(det*mult*math.sin(80*tick())),script.Rel.SetPitch.Value)) * on
			if FE then
				handler:FireServer("updateSound","Rev",script.Rev.SoundId,RevPitch,RevVolume)
				handler:FireServer("updateSound","Rel",script.Rel.SoundId,RelPitch,RelVolume)
			else
				car.DriveSeat.Rev.Volume = RevVolume
				car.DriveSeat.Rev.Pitch = RevPitch
				car.DriveSeat.Rel.Volume = RelVolume
				car.DriveSeat.Rel.Pitch = RelPitch
			end	
		end
	else
		if not script.Parent.IsOn.Value then on=math.max(on-.015,0) else on=1 end
		RevVolume = (throt*shift*redline)+(trm*trmon*trmmult*(1-throt)*math.sin(tick()*50)) * on
		RevPitch = (math.max((((script.Rev.SetPitch.Value + script.Rev.SetRev.Value*_RPM/_Tune.Redline))*on^2)+(det*mult*math.sin(80*tick())),script.Rev.SetPitch.Value)) * on
		RelVolume = (1 - RevVolume) * on / 0.5
		RelPitch = (math.max((((script.Rel.SetPitch.Value + script.Rel.SetRev.Value*_RPM/_Tune.Redline))*on^2)+(det*mult*math.sin(80*tick())),script.Rel.SetPitch.Value)) * on
	end
	if FE then
		handler:FireServer("updateSound","Rev",script.Rev.SoundId,RevPitch,RevVolume)
		handler:FireServer("updateSound","Rel",script.Rel.SoundId,RelPitch,RelVolume)
	else
		car.DriveSeat.Rev.Volume = RevVolume
		car.DriveSeat.Rev.Pitch = RevPitch
		car.DriveSeat.Rel.Volume = RelVolume
		car.DriveSeat.Rel.Pitch = RelPitch
	end
end
