--[[
	PowerZoom.lua
	SuperZero | (STEAM_0:1:20482849)
	===DStream===
]]

// ##################################################
// Power Zoom
// By RabidToaster
// ##################################################

if ( SERVER ) then
	AddCSLuaFile( "PowerZoom.lua" )
	return
end

// #########################
// Settings!
// #########################
local iZoomSpeed = 90 // Speed to zoom in FOV/s
local bScopeOverlay = true // Draw scope overlay?

// ##################################################
// Don't change down here!
// ##################################################

local iZoom = 0
local iZoomAdd = 0
local iBaseSensitivity = GetConVarNumber( "sensitivity" )
local iCurSensitivity = iBaseSensitivity

local function CalcView( pPlayer, vOrigin, aAngle, iFOV )
	if ( iZoom > 0 ) then
		return { fov = 90 - iZoom }
	end	
end
hook.Add( "CalcView", "PowerZoom.CalcView", CalcView )

local txScope = surface.GetTextureID( "overlays/scope_lens" )
local function HUDPaint()
	if ( iZoom != 0 && bScopeOverlay ) then
		surface.SetTexture( txScope )
		surface.DrawTexturedRect( 0, 0, ScrW(), ScrH() )
	end
end
hook.Add( "HUDPaint", "PowerZoom.HUDPaint", HUDPaint )

local function Think()
	iZoom = math.Clamp( iZoom + ( iZoomAdd * iZoomSpeed * FrameTime() ), 0, 80 )
	local iSensitivity = iBaseSensitivity
	if ( iZoom != 0 ) then
		iSensitivity = iBaseSensitivity * ( 1 - ( iZoom / 120 ) )
	elseif ( GetConVarNumber( "sensitivity" ) != iBaseSensitivity ) then
		iBaseSensitivity = GetConVarNumber( "sensitivity" )
	end
	if ( iSensitivity != iCurSensitivity ) then
		LocalPlayer():ConCommand( "sensitivity " .. iSensitivity .. "\n" )
		iCurSensitivity = iSensitivity
	end
end
hook.Add( "Think", "PowerZoom.Think", Think )

local function In()
	iZoomAdd = iZoomAdd + 1
end
concommand.Add( "+zoom_in", In )
concommand.Add( "-zoom_out", In )
local function Out()
	iZoomAdd = iZoomAdd - 1
end
concommand.Add( "+zoom_out", Out )
concommand.Add( "-zoom_in", Out )

local function Reset()
	iZoom = 0
end
concommand.Add( "zoom_reset", Reset )
