--------------------[ STANCE FUNCTIONS ]----------------------------------------------


function Stand(onDeselected)
	local LHip = Torso["Left Hip"]
	local RHip = Torso["Right Hip"]
	LLegWeld.Part1 = nil
	LHip.Part1 = LLeg
	RLegWeld.Part1 = nil
	RHip.Part1 = RLeg
	Stance = 0
	spreadStance = "Stand"
	baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
	if S.stanceSettings.Anim and (not onDeselected) then
		spawn(function()
			local prevStanceSway = stanceSway
			local X = 0
			local Increment = 1.5 / S.stanceSettings.Speed
			while true do
				RS.RenderStepped:wait()
				local newX = X + Increment
				X = (newX > 90 and 90 or newX)
				if Stance ~= 0 then break end
				stanceSway = numLerp(prevStanceSway, 1, Sine(X))
				if X == 90 then break end
			end
		end)
		tweenJoint(ABWeld, CF(), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(LLegWeld, legC0.Stand[1], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(RLegWeld, legC0.Stand[2], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(LHip, CF(-1, -1, 0) * CFANG(0, RAD(-90), 0), CF(-0.5, 1, 0) * CFANG(0, RAD(-90), 0), Sine, S.stanceSettings.Speed)
		tweenJoint(RHip, CF(1, -1, 0) * CFANG(RAD(-180), RAD(90), 0), CF(0.5, 1, 0) * CFANG(RAD(-180), RAD(90), 0), Sine, S.stanceSettings.Speed)
		tweenJoint(Root, CFANG(RAD(-90), 0, RAD(180)), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(headWeld, CF(0, 1.5, 0), nil, Sine, S.stanceSettings.Speed)
	elseif onDeselected or (not S.stanceSettings.Anim) then
		ABWeld.C0 = CF()
		LLegWeld.C0 = legC0.Stand[1]
		RLegWeld.C0 = legC0.Stand[2]
		LHip.C0, LHip.C1 = CF(-1, -1, 0) * CFANG(0, RAD(-90), 0), CF(-0.5, 1, 0) * CFANG(0, RAD(-90), 0)
		RHip.C0, RHip.C1 = CF(1, -1, 0) * CFANG(RAD(-180), RAD(90), 0), CF(0.5, 1, 0) * CFANG(RAD(-180), RAD(90), 0)
		Root.C0 = CFANG(RAD(-90), 0, RAD(180))
		headWeld.C0 = CF(0, 1.5, 0)
	end
end

function Crouch()
	local LHip = Torso["Left Hip"]
	local RHip = Torso["Right Hip"]
	LHip.Part1 = nil
	LLegWeld.Part1 = LLeg
	RHip.Part1 = nil
	RLegWeld.Part1 = RLeg
	Stance = 1
	spreadStance = "Crouch"
	baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
	if S.stanceSettings.Anim then
		spawn(function()
			local prevStanceSway = stanceSway
			local X = 0
			local Increment = 1.5 / S.stanceSettings.Speed
			while true do
				RS.RenderStepped:wait()
				local newX = X + Increment
				X = (newX > 90 and 90 or newX)
				if Stance ~= 1 then break end
				stanceSway = numLerp(prevStanceSway, 0.75, Sine(X))
				if X == 90 then break end
			end
		end)
		tweenJoint(ABWeld, CF(0, 0, -0.05), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(LLegWeld, legC0.Crouch[1], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(RLegWeld, legC0.Crouch[2], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(LHip, CF(-1, -0.5, 0) * CFANG(0, RAD(-90), 0), CF(-0.5, 0.5, 1) * CFANG(0, RAD(-90), RAD(-90)), Sine, S.stanceSettings.Speed)
		tweenJoint(RHip, CF(1, -0.5, 0.25) * CFANG(RAD(-180), RAD(90), 0), CF(0.5, 0.5, 1) * CFANG(RAD(-180), RAD(90), 0), Sine, S.stanceSettings.Speed)
		tweenJoint(Root, CF(0, -1, 0) * CFANG(RAD(-90), 0, RAD(180)), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(headWeld, CF(0, 1.5, 0), nil, Sine, S.stanceSettings.Speed)
	else
		ABWeld.C0 = CF(0, 0, -1 / 16)
		LLegWeld.C0 = legC0.Crouch[1]
		RLegWeld.C0 = legC0.Crouch[2]
		LHip.C0, LHip.C1 = CF(-1, -0.5, 0) * CFANG(0, RAD(-90), 0), CF(-0.5, 0.5, 1) * CFANG(0, RAD(-90), RAD(-90))
		RHip.C0, RHip.C1 = CF(1, -0.5, 0.25) * CFANG(RAD(-180), RAD(90), 0), CF(0.5, 0.5, 1) * CFANG(RAD(-180), RAD(90), 0)
		Root.C0 = CF(0, -1, 0) * CFANG(RAD(-90), 0, RAD(180))
		headWeld.C0 = CF(0, 1.5, 0)
	end
end

function Prone()
	local LHip = Torso["Left Hip"]
	local RHip = Torso["Right Hip"]
	LHip.Part1 = nil
	LLegWeld.Part1 = LLeg
	RHip.Part1 = nil
	RLegWeld.Part1 = RLeg
	Stance = 2
	spreadStance = "Prone"
	baseSpread = S.spreadSettings[spreadZoom][spreadStance][spreadMotion]
	if S.stanceSettings.Anim then
		spawn(function()
			local prevStanceSway = stanceSway
			local X = 0
			local Increment = 1.5 / S.stanceSettings.Speed
			while true do
				RS.RenderStepped:wait()
				local newX = X + Increment
				X = (newX > 90 and 90 or newX)
				if Stance ~= 2 then break end
				stanceSway = numLerp(prevStanceSway, 0.5, Sine(X))
				if X == 90 then break end
			end
		end)
		tweenJoint(ABWeld, CF(0, 0, -0.1), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(LLegWeld, legC0.Prone[1], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(RLegWeld, legC0.Prone[2], nil, Sine, S.stanceSettings.Speed)
		tweenJoint(Root, CF(0, -2.5, 1) * CFANG(RAD(180), 0, RAD(180)), nil, Sine, S.stanceSettings.Speed)
		tweenJoint(headWeld, CF(0, 1, 1) * CFANG(RAD(90), 0, 0), nil, Sine, S.stanceSettings.Speed)
	else
		ABWeld.C0 = CF(0, 0, -1 / 8)
		LLegWeld.C0 = legC0.Prone[1]
		RLegWeld.C0 = legC0.Prone[2]
		Root.C0 = CF(0, -2.5, 1) * CFANG(RAD(180), 0, RAD(180))
		headWeld.C0 = CF(0, 1, 1) * CFANG(RAD(90), 0, 0)
	end
end

function Dive()
	onGround = false
	local diveDirection = (HRP.CFrame * CFANG(S.diveSettings.Angle, 0, 0)).lookVector * S.walkSpeeds.Sprinting * S.diveSettings.Force
	local BF = Instance.new("BodyForce")
	BF.force = diveDirection + Vector3.new(0, playerMass * 196.2, 0)
	BF.Parent = HRP
	--[[spawn(function()
		HRP.Velocity = HRP.CFrame.lookVector * 60 + V3(0, 40, 0)
		wait(0.1)
		HRP.Velocity = HRP.CFrame.lookVector * 70 + V3(0, 30, 0)
		wait(0.4)
		HRP.Velocity = HRP.CFrame.lookVector * 30 + V3(0, -10, 0)
	end)]]
	delay(0.05, function()
		spawn(function()
			while true do
				local newRay = Ray.new(HRP.Position, V3(0, -3.1, 0))
				local H, _ = workspace:FindPartOnRayWithIgnoreList(newRay, Ignore)
				if H then
					onGround = true
					break
				end
				wait()
			end
		end)
		Prone()
		wait(0.1)
		BF:Destroy()
	end)
end
