--------------------[ ADS FUNCTIONS ]-------------------------------------------------



function aimGun()
	if Reloading or Knifing or isCrawling or (not S.canADS) then return end
	
	mouseSensitivity = aimSensitivity
	
	for _, Plugin in pairs(Plugins.Aimed) do
		spawn(function()
			Plugin()
		end)
	end
	
	Aimed = true
	Aiming = true
	Running = false
	spreadZoom = "Aimed"
	baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
	if S.aimSettings.Anim then
		local currentFOV = Cam.FieldOfView
		local currentTrans = Scope.BackgroundTransparency
		tweenJoint(LWeld, armC0[1], S.aimedC1.leftArm, Sine, S.aimSettings.Speed)
		tweenJoint(RWeld, armC0[2], S.aimedC1.rightArm, Sine, S.aimSettings.Speed)
		tweenJoint(LWeld2, nil, CF(), Sine, S.aimSettings.Speed)
		tweenJoint(RWeld2, nil, CF(), Sine, S.aimSettings.Speed)
		tweenJoint(Grip, nil, aimedGripCF, Sine, S.aimSettings.Speed)
		tweenJoint(headWeld2, nil, CF(0, -0.5, 0) * CFANG(0, 0, S.aimSettings.headTilt) * CF(0, 0.5, 0), Sine, S.aimSettings.Speed)
		local t0 = tick()
		while true do
			RS.RenderStepped:wait()
			local Alpha = math.min((tick() - t0) / S.aimSettings.Speed, 1) * 90
			if (not Aimed) then break end
			if (not Selected) then break end
			aimAlpha = Sine(Alpha)
			aimHeadOffset = headOffset.X * aimAlpha
			jumpAnimMultiplier = numLerp(1, S.fallSettings.aimEffect, aimAlpha)
			translationDivisor = numLerp(7, 20, aimAlpha)
			rotationMultiplier = numLerp(S.momentumSettings.Amplitude.unAimed, S.momentumSettings.Amplitude.Aimed, aimAlpha)
			armTiltMultiplier = numLerp(1, 0.2, aimAlpha)
			Cam.FieldOfView = numLerp(currentFOV, S.aimSettings.FOV, aimAlpha)
			if S.guiScope then
				Scope.BackgroundTransparency = numLerp(currentTrans, 0, aimAlpha)
			end
			if Alpha == 90 then break end
		end
	else
		LWeld.C0, LWeld.C1 = armC0[1], S.aimedC1.leftArm
		RWeld.C0, RWeld.C1 = armC0[2], S.aimedC1.rightArm
		LWeld2.C1, RWeld2.C1 = CF(), CF()
		animWeld.C0 = CF(0, 1, 0)
		Grip.C1 = aimedGripCF
		headWeld2.C1 = CF(0, -0.5, 0) * CFANG(0, 0, S.aimSettings.headTilt) * CF(0, 0.5, 0)
		aimAlpha = 1
		aimHeadOffset = headOffset.X
		jumpAnimMultiplier = S.fallSettings.aimEffect
		translationDivisor = 20
		rotationMultiplier = S.momentumSettings.Amplitude.Aimed
		armTiltMultiplier = 0.2
		Cam.FieldOfView = S.aimSettings.FOV
	end
	Aiming = (not Aimed)
	if (not Aiming) and S.guiScope then
		spawn(function()
			scopeSteady.Visible = true
			Scope.BackgroundTransparency = 1
			scopeMain.Visible = true
			
			if armTable then
				for _, Obj in pairs(armTable[1].Model:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 1
					end
				end
				for _, Obj in pairs(armTable[2].Model:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 1
					end
				end
			elseif armModel then
				for _, Obj in pairs(armModel:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 1
					end
				end
			end
			for _, Obj in pairs(playerFolder:GetChildren()) do
				if Obj:IsA("BasePart") then
					Obj.LocalTransparencyModifier = 1
				end
			end
			for _, Obj in pairs(Gun:GetChildren()) do
				if Obj:IsA("BasePart") then
					Obj.LocalTransparencyModifier = 1
				end
			end
		end)
		spawn(function()
			local camAng = 0
			local idleCam = function()
				return V3(
					RAD(SIN(camAng * S.scopeSettings.Frequency.Idling)) * stanceSway * camSway * S.scopeSettings.Amplitude.Idling,
					RAD(SIN(camAng * 5 / 2 * S.scopeSettings.Frequency.Idling)) * stanceSway * camSway * S.scopeSettings.Amplitude.Idling * 0.75,
					0
				)
			end
			local walkCam = function()
				return V3(
					RAD(SIN(camAng * S.scopeSettings.Frequency.Walking)) * camSway * stanceSway * S.scopeSettings.Amplitude.Walking,
					RAD(SIN(camAng * 5 / 2 * S.scopeSettings.Frequency.Walking)) * camSway * stanceSway * S.scopeSettings.Amplitude.Walking * 0.75,
					0
				)
			end
			while Aimed do
				local dt = RS.RenderStepped:wait()
				camOffsets.guiScope.Rot = (Sine(idleAlpha) * idleCam()) + (Sine(walkAlpha) * walkCam())
				camAng = camAng + RAD(105 * dt) * stanceSway * camSway
			end
		end)
	end
end

function unAimGun(Exception)
	if (not S.canADS) then return end
	
	mouseSensitivity = S.sensitivitySettings.Default
	
	for _, Plugin in pairs(Plugins.UnAimed) do
		spawn(function()
			Plugin()
		end)
	end
	
	if S.guiScope then
		spawn(function()
			if armTable then
				for _, Obj in pairs(armTable[1].Model:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 0
					end
				end
				for _, Obj in pairs(armTable[2].Model:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 0
					end
				end
			elseif armModel then
				for _, Obj in pairs(armModel:GetChildren()) do
					if Obj:IsA("BasePart") then
						Obj.LocalTransparencyModifier = 0
					end
				end
			end
			for _, Obj in pairs(playerFolder:GetChildren()) do
				if Obj:IsA("BasePart") then
					Obj.LocalTransparencyModifier = 0
				end
			end
			for _, Obj in pairs(Gun:GetChildren()) do
				if Obj:IsA("BasePart") then
					Obj.LocalTransparencyModifier = 0
				end
			end
		end)
	end
	
	if (not Exception) then
		if (not Aimed) then return end
		if (Reloading and Exception) or Knifing then return end
		spreadZoom = "unAimed"
		baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
		Aimed = false
		Aiming = true
		if S.aimSettings.Anim then
			local currentFOV = Cam.FieldOfView
			local currentTrans = (Scope.BackgroundTransparency == 1 and (S.guiScope and 0 or 1) or Scope.BackgroundTransparency)
			scopeMain.Visible = false
			scopeSteady.Visible = false
			tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, S.aimSettings.Speed)
			tweenJoint(RWeld, armC0[2], S.unAimedC1.rightArm, Sine, S.aimSettings.Speed)
			tweenJoint(headWeld2, nil, CF(), Sine, S.aimSettings.Speed)
			tweenJoint(Grip, nil, S.unAimedC1.Grip, Sine, S.aimSettings.Speed)
			local t0 = tick()
			while true do
				RS.RenderStepped:wait()
				local Alpha = math.min((tick() - t0) / S.aimSettings.Speed, 1) * 90
				if Aimed then break end
				if (not Selected) then break end
				aimAlpha = 1 - Sine(Alpha)--1 - COS(RAD(X))
				aimHeadOffset = headOffset.X * aimAlpha
				jumpAnimMultiplier = numLerp(1, S.fallSettings.aimEffect, aimAlpha)
				translationDivisor = numLerp(7, 20, aimAlpha)
				rotationMultiplier = numLerp(S.momentumSettings.Amplitude.unAimed, S.momentumSettings.Amplitude.Aimed, aimAlpha)
				armTiltMultiplier = numLerp(1, 0.2, aimAlpha)
				Cam.FieldOfView = numLerp(80, currentFOV, aimAlpha)
				Scope.BackgroundTransparency = numLerp(1, currentTrans, aimAlpha)
				if Alpha == 90 then break end
			end
		else
			scopeMain.Visible = false
			scopeSteady.Visible = false
			LWeld.C0, LWeld.C1 = armC0[1], S.unAimedC1.leftArm
			RWeld.C0, RWeld.C1 = armC0[2], S.unAimedC1.rightArm
			headWeld2.C0 = CF()
			Grip.C1 = S.unAimedC1.Grip
			aimAlpha = 0
			aimHeadOffset = 0
			jumpAnimMultiplier = 1
			translationDivisor = 7
			rotationMultiplier = S.momentumSettings.Amplitude.unAimed
			armTiltMultiplier = 1
			Cam.FieldOfView = 80
			Scope.BackgroundTransparency = 1
		end
		Aiming = Aimed
	else
		spawn(function()
			Aimed = false
			Aiming = false
			spreadZoom = "unAimed"
			baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
			local currentFOV = Cam.FieldOfView
			local currentTrans = (Scope.BackgroundTransparency == 1 and (S.guiScope and 0 or 1) or Scope.BackgroundTransparency)
			scopeMain.Visible = false
			scopeSteady.Visible = false
			tweenJoint(headWeld2, nil, CF(), Sine, S.aimSettings.Speed)
			if S.aimSettings.Anim then
				local t0 = tick()
				while true do
					RS.RenderStepped:wait()
					local Alpha = math.min((tick() - t0) / S.aimSettings.Speed, 1) * 90
					if Aimed then break end
					if (not Selected) then break end
					aimAlpha = 1 - Sine(Alpha)--1 - COS(RAD(90 - Alpha))
					aimHeadOffset = headOffset.X * aimAlpha
					jumpAnimMultiplier = numLerp(1, S.fallSettings.aimEffect, aimAlpha)
					translationDivisor = numLerp(7, 20, aimAlpha)
					rotationMultiplier = numLerp(S.momentumSettings.Amplitude.unAimed, S.momentumSettings.Amplitude.Aimed, aimAlpha)
					armTiltMultiplier = numLerp(1, 0.2, aimAlpha)
					Cam.FieldOfView = numLerp(80, currentFOV, aimAlpha)
					Scope.BackgroundTransparency = numLerp(1, currentTrans, aimAlpha)
					if Alpha == 90 then break end
				end
			else
				scopeMain.Visible = false
				scopeSteady.Visible = false
				aimAlpha = 0
				aimHeadOffset = 0
				jumpAnimMultiplier = 1
				translationDivisor = 7
				rotationMultiplier = S.momentumSettings.Amplitude.unAimed
				armTiltMultiplier = 1
				Cam.FieldOfView = 80
				Scope.BackgroundTransparency = 1
			end
		end)
	end
end