if ( SERVER ) then
	AddCSLuaFile( "shared.lua" );
	SWEP.Weight				= 5;
	SWEP.AutoSwitchTo		= false;
	SWEP.AutoSwitchFrom		= false;
	
	local ActIndex = {};
	ActIndex[ "knife" ]			= ACT_HL2MP_IDLE_KNIFE;
	ActIndex[ "fists" ]			= ACT_HL2MP_IDLE_FIST;
	ActIndex[ "passive" ]		= ACT_HL2MP_IDLE_PASSIVE;
	ActIndex[ "pistol" ] 		= ACT_HL2MP_IDLE_PISTOL;
	ActIndex[ "smg" ] 			= ACT_HL2MP_IDLE_SMG1;
	ActIndex[ "grenade" ] 		= ACT_HL2MP_IDLE_GRENADE;
	ActIndex[ "ar2" ] 			= ACT_HL2MP_IDLE_AR2;
	ActIndex[ "shotgun" ] 		= ACT_HL2MP_IDLE_SHOTGUN;
	ActIndex[ "rpg" ]	 		= ACT_HL2MP_IDLE_RPG;
	ActIndex[ "physgun" ] 		= ACT_HL2MP_IDLE_PHYSGUN;
	ActIndex[ "crossbow" ] 		= ACT_HL2MP_IDLE_CROSSBOW;
	ActIndex[ "melee" ] 		= ACT_HL2MP_IDLE_MELEE;
	ActIndex[ "slam" ] 			= ACT_HL2MP_IDLE_SLAM;
	ActIndex[ "normal" ]		= ACT_HL2MP_IDLE;
	
	function SWEP:SetWeaponHoldType( t )
		local index = ActIndex[ t ];
		
		if ( index == nil ) then
			Msg( "SWEP:SetWeaponHoldType - ActIndex[ \""..t.."\" ] isn't set!\n" );
			return;
		end;
		
		self.ActivityTranslate = {};
		self.ActivityTranslate [ ACT_HL2MP_IDLE ] = index;
		self.ActivityTranslate [ ACT_HL2MP_WALK ] = index + 1;
		self.ActivityTranslate [ ACT_HL2MP_RUN ] = index + 2;
		self.ActivityTranslate [ ACT_HL2MP_IDLE_CROUCH ] = index + 3;
		self.ActivityTranslate [ ACT_HL2MP_WALK_CROUCH ] = index + 4;
		self.ActivityTranslate [ ACT_HL2MP_GESTURE_RANGE_ATTACK ] = index + 5;
		self.ActivityTranslate [ ACT_HL2MP_GESTURE_RELOAD ] = index + 6;
		self.ActivityTranslate [ ACT_HL2MP_JUMP ] = index + 7;
		self.ActivityTranslate [ ACT_RANGE_ATTACK1 ] = index + 8;
		self:SetupWeaponHoldTypeForAI( t );
	end;
end;

if ( CLIENT ) then
	SWEP.DrawAmmo			= true;
	SWEP.DrawCrosshair		= false;
	SWEP.ViewModelFOV		= 82;
	SWEP.ViewModelFlip		= false;
	SWEP.CSMuzzleFlashes	= true;
	SWEP.IconLetter = "";
	
	-- 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" );
end

SWEP.Category			= "epsilonScript";

SWEP.Spawnable			= false;
SWEP.AdminSpawnable		= false;

SWEP.Primary.Sound			= "";
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";

function SWEP:Initialize()
	if ( SERVER ) then
		self:SetNPCMinBurst( 30 );
		self:SetNPCMaxBurst( 30 );
		self:SetNPCFireRate( 0.01 );
	end;
	
	self.Weapon:SetNetworkedBool( "Ironsights", false );
end;

function SWEP:Deploy()
	if	( SERVER ) then
		epsilonScript.Holster.PlayerHolster( self.Owner );
	end;
	
	return;
end;

function SWEP:Reload()
	if ( self.Owner:GetNWBool( "Holstered" ) ) then return end;
	
	self.Weapon:DefaultReload( ACT_VM_RELOAD );
	self:SetIronsights( false );
end;

function SWEP:Think()
end;

function SWEP:PrimaryAttack()
	if ( self.Owner:GetNWBool( "Holstered" ) ) then return end;
	
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay );
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay );
	
	if ( !self:CanPrimaryAttack() ) 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 );
	
	-- Remove 1 bullet from our clip
	self:TakePrimaryAmmo( 1 );
	
	if ( self.Owner:IsNPC() ) then return end;
	
	-- Punch the player's view
	self.Owner:ViewPunch( Angle( math.Rand(-2,2) * self.Primary.Recoil, math.Rand(-1.5,1.5) *self.Primary.Recoil, 0 ) );
	
	-- 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 ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() );
	end;
end;

function SWEP:CSShootBullet( dmg, recoil, numbul, cone )
	if ( self.Owner:GetNWBool( "Holstered" ) ) then return end;
	
	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	= 13;							-- 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;
	
	if ( ( SinglePlayer() and SERVER ) or ( !SinglePlayer() && CLIENT ) ) then
		local eyeang = self.Owner:EyeAngles();
		eyeang.pitch = eyeang.pitch - recoil;
		self.Owner:SetEyeAngles( eyeang );
	end;
end;

function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
	-- Set us up the texture
	surface.SetDrawColor( 255, 255, 255, alpha );
	surface.SetTexture( self.WepSelectIcon );
	
	-- Lets get a sin wave to make it bounce
	local fsin = 0;
	
	if ( self.BounceWeaponIcon == true ) then
		fsin = math.sin( CurTime() * 10 ) * 5;
	end;
	
	-- Borders
	y = y + 10;
	x = x + 10;
	wide = wide - 20;
	
	-- Draw that mother
	surface.DrawTexturedRect( x + (fsin), y - (fsin),  wide-fsin*2 , ( wide / 2 ) + (fsin) );
	
	-- Draw weapon info box
	self:PrintWeaponInfo( x + wide + 20, y + tall * 0.95, alpha );
end;

local IRONSIGHT_TIME = 0.25;
function SWEP:GetViewModelPosition( pos, ang )
	if ( self.Owner:GetNWBool( "Holstered" ) ) then return end;
	if ( !self.IronSightsPos ) then return pos, ang end;

	local bIron = self.Weapon:GetNetworkedBool( "Ironsights" );
	
	if ( bIron != self.bLastIron ) then
		self.bLastIron = bIron;
		self.fIronTime = CurTime();
		if ( bIron ) then 
			self.SwayScale 	= 0.3;
			self.BobScale 	= 0.9;
		else 
			self.SwayScale 	= 2.0;
			self.BobScale 	= 3.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;

function SWEP:SetIronsights( b )
	self.Weapon:SetNetworkedBool( "Ironsights", b );
end;

SWEP.NextSecondaryAttack = 0;
function SWEP:SecondaryAttack()
	if ( self.Owner:GetNWBool( "Holstered" ) ) then return end;
	
	if ( !self.IronSightsPos ) then return end;
	if ( self.NextSecondaryAttack > CurTime() ) then return end;
	
	bIronsights = !self.Weapon:GetNetworkedBool( "Ironsights", false );
	
	self:SetIronsights( bIronsights );
	
	self.NextSecondaryAttack = CurTime() + 0.3;
end;

function SWEP:DrawHUD()
end;

function SWEP:OnRestore()
	self.NextSecondaryAttack = 0;
	self:SetIronsights( false );
end;