
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= true
	SWEP.ViewModelFOV		= 82
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true
	
	-- This is the font that's used to draw the death icons
	/*
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )
	*/
	
	--Garry changed fonting from GMod 12, so here we go.
	surface.CreateFont( "CSKillIcons",
	{
		font      = "csd",
		size      = ScreenScale( 30 ),
		weight    = 500,
		antialias 	= true,
		additive 	= true,
		underline = 0
	}
	)
	
	surface.CreateFont( "CSSelectIcons",
	{
		font      = "csd",
		size      = ScreenScale( 60 ),
		weight    = 500,
		antialias 	= true,
		additive 	= true,
		underline = 0
	}
	)
	
end

SWEP.Author			= "Counter-Strike"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""

-- Note: This is how it should have worked. The base weapon would set the category
-- then all of the children would have inherited that.
-- But a lot of SWEPS have based themselves on this base (probably not on purpose)
-- So the category name is now defined in all of the child SWEPS.
--SWEP.Category			= "Counter-Strike"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.Type = ""

SWEP.Primary.Sound			= Sound( "Weapon_AK47.Single" )
SWEP.Primary.Recoil			= 1.5
SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
/*---------------------------------------------------------
---------------------------------------------------------*/
local IRONSIGHT_TIME = 0.25
function SWEP:SetIronsights( b ) self.Weapon:SetNWBool( "Ironsights", b ) self.Weapon:SetNextPrimaryFire( 10 ) end
function SWEP:GetIronsights( b ) return self.Weapon:GetNWBool( "Ironsights", b ) end


local scopeDraw = false;

function SWEP:Initialize()
	if (SERVER) then self:SetNPCMinBurst(30);self:SetNPCMaxBurst(30);self:SetNPCFireRate(0.001) end
	
	self:SetWeaponHoldType( self.HoldType )
	self.Weapon:SetNWBool( "Ironsights", false )
end

/*---------------------------------------------------------
	Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
	if (self.Weapon:Clip1() == self.Primary.ClipSize) or (self.Weapon:Ammo1() <= 0)
	then return end
	
	self.Weapon:DefaultReload( ACT_VM_RELOAD );
	self.Weapon:EmitSound(self.Primary.Reload)
	self:SetIronsights( false )
	self.Owner:SetFOV(0, 0.3)
end

/*---------------------------------------------------------
   Think does nothing
---------------------------------------------------------*/
function SWEP:Think()	
end


SWEP.NextPrimaryAttack = 0
/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	if ( !self:CanPrimaryAttack() ) then return end
	if ( self.NextSecondaryAttack > CurTime() ) then return end
	
	-- Play shoot sound
	self.Weapon:EmitSound( self.Primary.Sound )
	
	-- Shoot the bullet
	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone )

	
	if ( self.Owner:IsNPC() ) then return end
	
		
	-- Remove 1 bullet from our clip
	self:TakePrimaryAmmo( 1 )
	
	-- Punch the player's view
	self.Owner:ViewPunch(
		Angle(
			math.Rand(-1,-0.1) * self.Primary.Recoil, -- Vertical
			math.Rand(-1,1) * self.Primary.Recoil, -- Horizontal
			math.Rand(-1,1) * self.Primary.Recoil -- Rotate
		)
	)
	
	-- In singleplayer this function doesn't get called on the client, so we use a networked float
	-- to send the last shoot time. In multiplayer this is predicted clientside so we don't need to 
	-- send the float.
	if ( (game.SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
	
end

/*---------------------------------------------------------
   Name: SWEP:CSShootBullet( )
---------------------------------------------------------*/
function SWEP:CSShootBullet( dmg, recoil, numbul, cone )

	numbul 	= numbul 	or 1
	cone 	= cone 		or 0.01

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos()			-- Source
	bullet.Dir 		= self.Owner:GetAimVector()			-- Dir of bullet
	bullet.Spread 	= Vector( cone, cone, 0 )			-- Aim Cone
	bullet.Tracer	= 4									-- Show a tracer on every x bullets 
	bullet.Force	= 5									-- Amount of force to give to phys objects
	bullet.Damage	= dmg
	
	self.Owner:FireBullets( bullet )
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		-- View model animation
	self.Owner:MuzzleFlash()								-- Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )				-- 3rd Person Animation
	
	if ( self.Owner:IsNPC() ) then return end
	
	-- CUSTOM RECOIL !
	if ( (game.SinglePlayer() && SERVER) || ( !game.SinglePlayer() && CLIENT && IsFirstTimePredicted() ) ) then
	
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles( eyeang )
	
	end

end


/*---------------------------------------------------------
	Checks the objects before any action is taken
	This is to make sure that the entities haven't been removed
---------------------------------------------------------*/
function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
	
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2, y + tall*0.2, Color( 255, 210, 0, 255 ), TEXT_ALIGN_CENTER )
	
	-- try to fool them into thinking they're playing a Tony Hawks game
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2 + math.Rand(-4, 4), y + tall*0.2+ math.Rand(-14, 14), Color( 255, 210, 0, math.Rand(10, 120) ), TEXT_ALIGN_CENTER )
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2 + math.Rand(-4, 4), y + tall*0.2+ math.Rand(-9, 9), Color( 255, 210, 0, math.Rand(10, 120) ), TEXT_ALIGN_CENTER )
	
end

/*---------------------------------------------------------
   Name: GetViewModelPosition
   Desc: Allows you to re-position the view model
---------------------------------------------------------*/
function SWEP:GetViewModelPosition( pos, ang )

	if ( !self.IronSightsPos ) then return pos, ang end

	local bIron = self.Weapon:GetNWBool( "Ironsights" )
	
	if ( bIron != self.bLastIron ) then
	
		self.bLastIron = bIron 
		self.fIronTime = CurTime()
		
		if ( bIron ) then 
			self.SwayScale 	= 0.3
			self.BobScale 	= 0.1
		else 
			self.SwayScale 	= 1.0
			self.BobScale 	= 1.0
		end
	
	end
	
	local fIronTime = self.fIronTime or 0

	if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
		return pos, ang 
	end
	
	local Mul = 1.0
	
	if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
	
		Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
		
		if (!bIron) then Mul = 1 - Mul end
	
	end

	local Offset	= self.IronSightsPos
	
	if ( self.IronSightsAng ) then
	
		ang = ang * 1
		ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
		ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
		ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )
	
	
	end
	
	local Right 	= ang:Right()
	local Up 		= ang:Up()
	local Forward 	= ang:Forward()
	
	

	pos = pos + Offset.x * Right * Mul
	pos = pos + Offset.y * Forward * Mul
	pos = pos + Offset.z * Up * Mul

	return pos, ang
	
end

SWEP.NextSecondaryAttack = 0
/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()

	if ( !self.IronSightsPos ) then return end
	if ( self.NextSecondaryAttack > CurTime() ) then return end
	
	bIronsights = !self:GetIronsights( false )
	self:SetIronsights( bIronsights )
	
	--print("Ironsights:"..tostring(self.Weapon:GetNWBool("Ironsights")))
	
	--if (CLIENT) then print("Ironsights:"..tostring(self.Weapon:GetIronsights())) end
	if (CLIENT) then local pl = self:GetOwner();print("playername:"..pl:Name()) end
	if (SERVER) then print("Ironsights:"..tostring(self:GetIronsights())) end
	
	if(self.Type=="sniper") then self.Weapon:EmitSound("csgo/awp/zoom.wav");
		if bIronsights then -- if we're not zoomed when we fire secondary
			self.Owner:ScreenFade( SCREENFADE.IN, Color( 0, 0, 0, 255 ), 2, 0 )
			self.Owner:DrawViewModel(false);self.Owner:SetFOV(10, 0);
		else
			self.Owner:ScreenFade( SCREENFADE.IN, Color( 0, 0, 0, 255 ), .5, 0 )
			self.Owner:DrawViewModel(true);self.Owner:SetFOV(0, 0);
		end
	elseif not(self.Type=="sniper") then
		if bIronsights then self.Owner:SetFOV(70, IRONSIGHT_TIME+.20)else self.Owner:SetFOV(0, IRONSIGHT_TIME+.20) end
	end

	
	
	self.NextSecondaryAttack = CurTime() + 0.35 -- this is the thing that m9k lacks which makes it so you can't glitch the ironsights
end


--hook.Add( "RenderScreenspaceEffects", "ClassiqBokehDOF", function() end )
--[[---------------------------------------------------------
	DrawHUD
	
	Since GMod as of March 2024 update supports custom crosshairs out the box , 
	I am simply going to make use of this default option in lieu of a built-in crosshair.
	This ensures compatibility with custom crosshair overrides, so it's a win-win for everyone.
---------------------------------------------------------]]

function SWEP:Holster() self:SetNWBool("Ironsights", false);return true end

function SWEP:DrawHUD()
	if not(self.Weapon:GetNWBool("Ironsights")) then self.DrawCrosshair = true else self.DrawCrosshair = false return end
	if(self.Type=="sniper") then
		local scopeOverlay = Material("ph_scope/ph_scope_lens5")
		hook.Add(
			"RenderScreenspaceEffects",
			"ClassiqScopeOverlay",
			function()
				if IsValid(self.Weapon) and LocalPlayer():Alive() then
					if not self.Weapon:GetNWBool("Ironsights") then return end
					if (scopeOverlay == nil || scopeOverlay:IsError()) then return end
					render.UpdateScreenEffectTexture()
					scopeOverlay:SetFloat( "$envmap", 0 )
					scopeOverlay:SetFloat( "$envmaptint", 0 )
					scopeOverlay:SetFloat( "$refractamount", .3 )
					scopeOverlay:SetInt( "$ignorez", 1 )
							
					render.SetMaterial( scopeOverlay )
					render.DrawScreenQuad()
				end
			end
		)
	end
end


/*---------------------------------------------------------
	onRestore
	Loaded a saved game (or changelevel)
---------------------------------------------------------*/
function SWEP:OnRestore()

	self.NextSecondaryAttack = 0
	self:SetIronsights( false )
	
end




/*
--Whee NPC's...
function SWEP:GetCapabilities()
 
	return CAP_WEAPON_RANGE_ATTACK1 | CAP_INNATE_RANGE_ATTACK1
 
end
*/