local recoils = {


	-- pistols
	[416676503] = 0.9,
	[453432689] = 0.9,            --['weapon_pistol']
    [-1075685676] = 0.6,        --['weapon_pistol_mk2']
    [1593441988] = 0.9,            --['weapon_combatpistol']
    [584646201] = 0.9,            --['weapon_appistol']
    [911657153] = 0.1,            --['weapon_stungun']
    [-1716589765] = 0.9,        --['weapon_pistol50']
    [-1076751822] = 0.7,        --['weapon_snspistol']
    [-771403250] = 0.9,            --['weapon_heavypistol']
    [137902532] = 0.9,            --['weapon_vintagepistol']
    [1198879012] = 0.9,            --['weapon_flaregun']
    [-598887786] = 0.9,            --['weapon_marksmanpistol']
    [-1045183535] = 0.9,        --['weapon_revolver']
    [-879347409] = 0.9,            --['weapon_revolver_mk2']
    [-1746263880] = 0.9,        --['weapon_doubleaction']
    [-2009644972] = 0.9,        --['weapon_snspistol_mk2']
    [-1355376991] = 0.3,        --['weapon_raypistol']
    [727643628] = 0.9,            --['weapon_ceramicpistol']
    [-1853920116] = 0.9,        --['weapon_navyrevolver']
    [1470379660] = 0.9,            --['weapon_gadgetpistol']

	--smg
	[-957766203] = 0.7,
	[324215364] = 0.7,            --['weapon_microsmg']
    [736523883] = 0.3,            --['weapon_smg']
    [2024373456] = 0.7,            --['weapon_smg_mk2']
    [-270015777] = 0.7,            --['weapon_assaultsmg']
    [171789620] = 0.7,            --['weapon_combatpdw']
    [-619010992] = 0.7,            --['weapon_machinepistol']
    [-1121678507] = 0.7,        --['weapon_minismg']
    [1198256469] = 0.7,            --['weapon_raycarbine']

	-- rifles
	[970310034] = 0.5,
	[-1074790547] = 0.5,        --['weapon_assaultrifle']
    [961495388] = 0.5,            --['weapon_assaultrifle_mk2']
    [-2084633992] = 0.3,        --['weapon_carbinerifle']
    [-86904375] = 0.5,            --['weapon_carbinerifle_mk2']
    [-1357824103] = 0.5,        --['weapon_advancedrifle']
    [-1063057011] = 0.5,        --['weapon_specialcarbine']
    [2132975508] = 0.5,            --['weapon_bullpuprifle']
    [1649403952] = 0.5,            --['weapon_compactrifle']
    [-1768145561] = 0.5,        --['weapon_specialcarbine_mk2']
    [-2066285827] = 0.5,        --['weapon_bullpuprifle_mk2']
    [-1658906650] = 0.5,        --['weapon_militaryrifle']


	-- Shotguns
    [487013001] = 0.7,            --['weapon_pumpshotgun']
    [2017895192] = 0.7,            --['weapon_sawnoffshotgun']
    [-494615257] = 0.7,            --['weapon_assaultshotgun']
    [-1654528753] = 0.7,        --['weapon_bullpupshotgun']
    [-1466123874] = 0.7,        --['weapon_musket']
    [984333226] = 0.7,            --['weapon_heavyshotgun']
    [-275439685] = 0.7,            --['weapon_dbshotgun']
    [317205821] = 0.7,            --['weapon_autoshotgun']
    [1432025498] = 0.7,            --['weapon_pumpshotgun_mk2']
    [94989220] = 0.7,            --['weapon_combatshotgun']

}


local myRecoilFactor = 0.0
RegisterNetEvent("recoil:updateposition")
AddEventHandler("recoil:updateposition", function(sendFactor)

    myRecoilFactor = sendFactor / 5 + 0.0

end)


function quickmafs(dir)
	local x = 0.0
	local y = 0.0
	local dir = dir
	if dir >= 0.0 and dir <= 90.0 then
		local factor = (dir/9.2) / 10
		x = -1.0 + factor
		y = 0.0 - factor
	end

	if dir > 90.0 and dir <= 180.0 then
		dirp = dir - 90.0
		local factor = (dirp/9.2) / 10
		x = 0.0 + factor
		y = -1.0 + factor
	end

	if dir > 180.0 and dir <= 270.0 then
		dirp = dir - 180.0
		local factor = (dirp/9.2) / 10
		x = 1.0 - factor
		y = 0.0 + factor
	end

	if dir > 270.0 and dir <= 360.0 then
		dirp = dir - 270.0
		local factor = (dirp/9.2) / 10	
		x = 0.0 - factor
		y = 1.0 - factor
	end
	return x,y
end

Citizen.CreateThread( function()

	
	while true do 

		 if IsPedArmed(PlayerPedId(), 6) then
		 	Citizen.Wait(1)
		 else
		 	Citizen.Wait(1500)
		 end  

	    if IsPedShooting(PlayerPedId()) then
	    	local ply = PlayerPedId()
	    	local GamePlayCam = GetFollowPedCamViewMode()
	    	local Vehicled = IsPedInAnyVehicle(ply, false)
	    	local MovementSpeed = math.ceil(GetEntitySpeed(ply))

	    	if MovementSpeed > 69 then
	    		MovementSpeed = 69
	    	end

	        local _,wep = GetCurrentPedWeapon(ply)

	        local group = GetWeapontypeGroup(wep)

	        local p = GetGameplayCamRelativePitch()

	        local cameraDistance = #(GetGameplayCamCoord() - GetEntityCoords(ply))

	        local recoil = math.random(100,140+MovementSpeed)/100
	        local rifle = false


          	if group == 970310034 then
          		rifle = true
          	end

          	if cameraDistance < 5.3 then
          		cameraDistance = 1.5
          	else
          		if cameraDistance < 8.0 then
          			cameraDistance = 4.0
          		else
          			cameraDistance = 7.0
          		end
          	end


	        if Vehicled then
	        	recoil = recoil + (recoil * cameraDistance)
	        else
	        	recoil = recoil * 0.8
	        end

	        if GamePlayCam == 4 then

	        	recoil = recoil * 0.7
		        if rifle then
		        	recoil = recoil * 0.1
		        end

	        end

	        if rifle then
	        	recoil = recoil * 0.7
	        end

	        local rightleft = math.random(4)
	        local h = GetGameplayCamRelativeHeading()
	        local hf = math.random(10,40+MovementSpeed)/100

	        if Vehicled then
	        	hf = hf * 2.0

	        end

	        if rightleft == 1 then
	        	SetGameplayCamRelativeHeading(h+hf)
	        elseif rightleft == 2 then
	        	SetGameplayCamRelativeHeading(h-hf)
	        end 
        
	        local set = p+recoil

	       	SetGameplayCamRelativePitch(set,0.8)    	       	

	       	
	      -- 	print(GetGameplayCamRelativePitch())

	    end
	end

end)