bZoomed = false

function onInit() _SWEPSetSound(MyIndex, "single_shot", "Weapon_AWP.Single") end
function onThink() end

function math.frandom(minn, maxn)
	minn = minn * 100
	maxn = maxn * 100
	return math.random(minn, maxn)*0.01
end

	function onPrimaryAttack()
		if not _PlayerInfo(Owner, "alive") or not _PlayerInfo( Owner, "connected") then return end
		local accuracy = 0.0
		if not bZoomed then accuracy = accuracy+0.1 end
		if not _PlayerIsCrouching(Owner) then accuracy = accuracy+0.12 end
		local stuff = _EntGetVelocity(Owner)
		stuff = math.abs(stuff.x)+math.abs(stuff.y)+math.abs(stuff.z)
		if stuff > 50 then accuracy = accuracy+0.09 end
		local vecpos = _PlayerGetShootPos(Owner)
		local plyang = 0
		if accuracy < 1.0 then
			plyang = vecAdd(_PlayerGetShootAng(Owner), vector3(math.frandom(-accuracy, accuracy), math.frandom(-accuracy, accuracy), math.frandom(-accuracy, accuracy)))
		else
			plyang = _PlayerGetShootAng(Owner)
		end
		_TraceSetMask(MASK_SHOT)
		_TraceLine(vecpos, plyang, 8000, Owner)
	   
	   local hitpos = _TraceEndPos()
	 
	   if not _TraceHit() then
			hitpos = vecAdd( vecpos, vecMul( plyang, vector3(8000,8000,8000) ) )
	   end
 
	   vecpos = vecAdd( vecpos, vector3( 0, 0, -20 ) )

		_EffectInit()
		_EffectSetEnt( Owner )
		_EffectSetOrigin( hitpos )
		_EffectSetStart( vecpos )
		_EffectSetScale( 15 )
		_EffectSetMagnitude( 3)
		_EffectDispatch( "FadingLineTeam" )
		_EffectSetMagnitude( 4 )
		_EffectSetScale( 9 )
		_EffectDispatch( "FadingLineTeam" )
        _EffectInit()
		_EffectSetEnt(Owner)
		_EffectSetOrigin( hitpos )
		_EffectSetStart( vecpos )
		_EffectSetScale(4000)
		_EffectSetMagnitude(70)
		_EffectSetRadius(90)
		_EffectDispatch("GaussTracer")
		local damage = 70
		if _TraceHitNonWorld() then
			if _TraceGetEnt() <= _MaxPlayers() then
				local vecz = _EntGetPos(_TraceGetEnt()).z
				local endz = _TraceEndPos().z
				if _PlayerIsCrouching(_TraceGetEnt()) then
					if endz > vecz+43 then damage = damage*3 end
				else
					if endz > vecz+82 then damage = damage*3 end
				end
			end
			local tracedEntity = _TraceGetEnt()
			_RunString("_TraceAttack("..tracedEntity..", "..MyIndex..", "..Owner..", "..damage..", 'bullet')")
		end
		_SWEPUseAmmo(MyIndex, 0, 1)
		_EntSetVelocity(Owner, vecAdd(_EntGetVelocity(Owner), vecMul(_PlayerGetShootAng(Owner), vector3(-250, -250, -325))))
	end

	function onSecondaryAttack() ToggleZoom() end
	
	function ToggleZoom ( )
		if not bZoomed then
			_PlayerSetFOV(Owner, 15, 0.3)
			bZoomed = true
			_SWEPUpdateVariables( MyIndex )
		else
			EndZoom()
		end
	end

	function EndZoom()
		_PlayerStopZooming( Owner )
		bZoomed = false
		_SWEPUpdateVariables( MyIndex )
	end

function Deploy() EndZoom() end
function Holster() EndZoom() end 
function onReload() EndZoom() return true end
function getWeaponSwapHands() return true end
function getWeaponFOV() return 74 end
function getWeaponSlot() return 5 end
function getWeaponSlotPos() return 5 end
function getFiresUnderwater() return true end
function getReloadsSingly() return false end
function getDamage() return 70 end
function getPrimaryShotDelay() return 1.75	end
function getSecondaryShotDelay() return 0.5 end
function getPrimaryIsAutomatic() return false end
function getSecondaryIsAutomatic() return true end
function getBulletSpread() return vector3(0.0, 0.0, 0.0) end
function getViewKick() return vector3(-6.0, 0.0, 0.0) end
function getViewKickRandom() return vector3(1.5, 2.5, 2.5) end
function getBulletSpreadSecondary() return vector3(0.001, 0.001, 0.001) end
function getViewKickSecondary() return vector3(0.0, 0.0, 0.0) end
function getViewKickRandomSecondary() return vector3(0.0, 0.0, 0.0) end
function getViewModel() return "models/weapons/v_snip_g3sg1.mdl" end
function getWorldModel() return "models/weapons/w_snip_g3sg1.mdl" end
function getClassName() return "weapon_sniperrifle" end
function getPrimaryAmmoType() return "357" end	
function getSecondaryAmmoType() return "357" end
function getMaxClipPrimary() return 1 end
function getMaxClipSecondary() return -1 end
function getDefClipPrimary() return 1000 end
function getDefClipSecondary() return -1 end
function getAnimPrefix() return "rpg" end
function getPrintName() return "Sniper Rifle" end
function getPrimaryScriptOverride() return 1 end
function getSecondaryScriptOverride() return 3 end
function getDeathIcon() return "d_awp" end
