--------------------[ SPRINTING FUNCTIONS ]-------------------------------------------


function canRun(midRun)
	return ((Forward and (not Backward)) and
		Walking and (Stamina > 0) and Running and
		Selected and (midRun and true or onGround) and
		runReady and (S.canFireWhileRunning and true or (not Firing))
	)
end

function monitorStamina()
	Running = true
	if (not canRun(false)) then
		Running = false
		return
	end
	if Aimed then unAimGun(true) end
	if Stance == 1 or Stance == 2 then Stand() end
	if (not (Reloading and S.reloadSettings.Anim)) then
		if S.canFireWhileRunning then
			tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.unAimedC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.unAimedC1.Grip, Sine, 0.4)
		else
			tweenJoint(LWeld, armC0[1], S.runningC1.leftArm, Sine, 0.4)
			tweenJoint(RWeld, armC0[2], S.runningC1.rightArm, Sine, 0.4)
			tweenJoint(Grip, nil, S.runningC1.Grip, Sine, 0.4)
		end
	end
	crossOffset = 50
	while runKeyPressed do
		if canRun(true) then
			if onGround then
				local newStamina = Stamina - 1
				Stamina = (newStamina < 0 and 0 or newStamina)
			end
		else
			break
		end
		RS.RenderStepped:wait()
	end
	Running = false
	if (not Aimed) and (not (Reloading and S.reloadSettings.Anim)) and (not S.canFireWhileRunning) then
		crossOffset = 0
		tweenJoint(LWeld, armC0[1], S.unAimedC1.leftArm, Sine, 0.4)
		tweenJoint(RWeld, armC0[2], S.unAimedC1.rightArm, Sine, 0.4)
		tweenJoint(Grip, nil, S.unAimedC1.Grip, Sine, 0.4)
	end
	
	rechargeStamina()
end

function rechargeStamina()
	chargingStamina = true
	while ((not runKeyPressed) or (Stamina < maxStamina)) and (not Running) do
		if Stamina < maxStamina then
			local newStamina = Stamina + (S.sprintTime / S.staminaCoolTime)
			Stamina = (newStamina > maxStamina and maxStamina or newStamina)
		elseif Stamina >= maxStamina then
			break
		end
		RS.RenderStepped:wait()
	end
	chargingStamina = false
end
