

if( SERVER ) then

	AddCSLuaFile( "shared.lua" );

end

SWEP.PrintName = "Psyche";

if( CLIENT ) then

	SWEP.Slot = 2;
	SWEP.SlotPos = 2;
	SWEP.DrawAmmo = false;
	SWEP.DrawCrosshair = false;
	SWEP.DrawDotCrosshair = true;

end

// Variables that are used on both client and server

SWEP.Base = "weapon_ts_base";

SWEP.Author			= "Disseminate"
SWEP.Instructions	= "";
SWEP.Contact		= "http://necropolisrp.net"
SWEP.Purpose		= "PEW"
SWEP.NotHolsterAnim = ACT_HL2MP_IDLE;

SWEP.ViewModel = Model( "models/weapons/v_fists.mdl" );
SWEP.WorldModel = Model( "models/weapons/w_fists.mdl" );

SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false
SWEP.AnimPrefix		= "rpg"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.Primary.ClipSize = 9999;
SWEP.Primary.DefaultClip = 9999;
SWEP.Primary.Delay = 0;
SWEP.Primary.Damage = 100;
SWEP.Primary.Force = 500;
SWEP.Primary.NumBullets = 30;

SWEP.Primary.ClipSize		= -1					// Size of a clip
SWEP.Primary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Primary.Automatic		= false				// Automatic/Semi Auto
SWEP.Primary.Ammo			= ""

SWEP.Secondary.ClipSize		= -1				// Size of a clip
SWEP.Secondary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Secondary.Automatic	= false				// Automatic/Semi Auto
SWEP.Secondary.Ammo			= ""

SWEP.Primary.RunCone = Vector( 0, 0, 0 );
SWEP.Primary.SpreadCone = Vector( 0, 0, 0 );
SWEP.Primary.CrouchSpreadCone = Vector( 0, 0, 0 );
SWEP.Primary.ViewPunch = Angle( 0, 0, 0 );
SWEP.Primary.Automatic = false;

SWEP.AimStatNoEffect = true;

SWEP.GunMode = 1;

function SWEP:Shield()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	if( self.Owner:GetNWInt( "ShieldOn" ) == 0 ) then
		
		self.Owner:SetNWInt( "ShieldOn", 1 );
		self.Owner:PrintMessage( 3, "Shield on" );
		
		local ed = EffectData();
			ed:SetOrigin( BonePos );
			ed:SetAngle( BoneAng );
			ed:SetScale( 1 );
		util.Effect( "psi_rad", ed );
		
	else
	
		self.Owner:SetNWInt( "ShieldOn", 0 );
		self.Owner:PrintMessage( 3, "Shield off" );
		
	end
	
end

function SWEP:Cloak()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	if( self.Owner:GetNWInt( "CloakOn" ) == 0 ) then
	
		self.Owner:SetNWInt( "CloakOn", 1 );
		self.Owner:PrintMessage( 3, "Cloak on" );
		
		local ed = EffectData();
			ed:SetOrigin( BonePos );
			ed:SetAngle( BoneAng );
			ed:SetScale( 1 );
		util.Effect( "psi_rad", ed );
		
	else
	
		self.Owner:SetNWInt( "CloakOn", 0 );
		self.Owner:PrintMessage( 3, "Cloak off" );
		
	end
	
end

function SWEP:Push()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	local m = self.Owner:GetNWInt( "PsyRemaining" );
	
	if( m < 50 ) then return end
	
	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 256;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() ) then
		
		if( tr.Entity:GetClass() == "npc_arachnid" or tr.Entity:GetClass() == "npc_antlion" or tr.Entity:GetClass() == "npc_antlionguard" ) then
			
			if( tr.Entity:GetNWInt( "Disabled" ) == 0 ) then
			
				tr.Entity:SetVelocity( tr.Entity:GetVelocity() * -1 );
				tr.Entity:SetVelocity( ( self.Owner:GetAimVector() * math.random( 1000, 2000 ) ) + Vector( 0, 0, 64 ) );
				
			end
			
		end
		
		if( tr.Entity:IsPlayer() or tr.Entity:IsNPC() ) then
			
			tr.Entity:SetVelocity( tr.Entity:GetVelocity() * -1 );
			tr.Entity:SetVelocity( ( self.Owner:GetAimVector() * math.random( 500, 1000 ) ) + Vector( 0, 0, 64 ) );
			
		end
		
	end
	
	local ed = EffectData();
		ed:SetOrigin( BonePos );
		ed:SetAngle( BoneAng );
		ed:SetScale( 1 );
	util.Effect( "psi_rad", ed );
	
	self.Owner:SetNWInt( "PsyRemaining", math.Clamp( self.Owner:GetNWInt( "PsyRemaining" ) - 50, 0, 100 ) );
	self.Owner:EmitSound( "ambient/machines/thumper_hit.wav" );
	self.Owner:SetNWInt( "LastForce", CurTime() );
	
end

function SWEP:Incap()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	local m = self.Owner:GetNWInt( "PsyRemaining" );
	
	if( m < 50 ) then return end
	
	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 256;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() ) then
		
		if( tr.Entity:GetClass() == "npc_arachnid" or tr.Entity:IsPlayer() ) then
			
			if( tr.Entity:GetNWInt( "Disabled" ) == 0 ) then
				
				local function BugRevive()
				
					if( tr.Entity.rag ) then
						
						tr.Entity.rag:Remove();
						tr.Entity:SetPos( tr.Entity.rag:GetPos() + Vector( 0, 0, 25 ) );
						tr.Entity:SetNoDraw( false );
						tr.Entity:SetNotSolid( false );
						tr.Entity:SetNWInt( "Disabled", 0 );
						if( tr.Entity:IsPlayer() ) then
					
							tr.Entity:UnLock();
							
						end
						
					end
				
				end
				
				tr.Entity.rag = ents.Create( "prop_ragdoll" );
				tr.Entity.rag:SetModel( tr.Entity:GetModel() );
				tr.Entity.rag:SetPos( tr.Entity:GetPos() );
				tr.Entity.rag:SetAngles( tr.Entity:GetAngles() );
				tr.Entity.rag:Spawn();
				tr.Entity.rag:SetSkin( tr.Entity:GetSkin() );
				tr.Entity.rag:SetColor( tr.Entity:GetColor() );
				tr.Entity.rag:SetMaterial( tr.Entity:GetMaterial() );
				
				tr.Entity:SetNoDraw( true );
				tr.Entity:SetNotSolid( true );
				tr.Entity:SetNWInt( "Disabled", 1 );
				if( tr.Entity:IsPlayer() ) then
					
					tr.Entity:Lock();
					
				end
				
				timer.Simple( 8, BugRevive );
				
			end
			
		end
		
	end
	
	local ed = EffectData();
		ed:SetOrigin( BonePos );
		ed:SetAngle( BoneAng );
		ed:SetScale( 1 );
	util.Effect( "psi_rad", ed );
	
	self.Owner:SetNWInt( "PsyRemaining", math.Clamp( self.Owner:GetNWInt( "PsyRemaining" ) - 50, 0, 100 ) );
	self.Owner:EmitSound( "ambient/machines/thumper_hit.wav" );
	self.Owner:SetNWInt( "LastForce", CurTime() );
	
end

function SWEP:Terminate()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	local m = self.Owner:GetNWInt( "PsyRemaining" );
	
	if( m < 50 ) then return end
	
	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 256;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() ) then
		
		if( tr.Entity:GetClass() == "npc_arachnid" or tr.Entity:IsPlayer() ) then
			
			if( tr.Entity:GetNWInt( "Disabled" ) == 0 ) then
				
				tr.Entity:Kill();
				
			end
			
		end
		
	end
	
	local ed = EffectData();
		ed:SetOrigin( BonePos );
		ed:SetAngle( BoneAng );
		ed:SetScale( 1 );
	util.Effect( "psi_rad", ed );
	
	self.Owner:SetNWInt( "PsyRemaining", math.Clamp( self.Owner:GetNWInt( "PsyRemaining" ) - 50, 0, 100 ) );
	self.Owner:EmitSound( "ambient/machines/thumper_hit.wav" );
	self.Owner:SetNWInt( "LastForce", CurTime() );
	
end

function SWEP:Project()
	
	local BoneIndx = self.Owner:LookupBone( "ValveBiped.Bip01_Head1" );
	local BonePos, BoneAng = self.Owner:GetBonePosition( BoneIndx );
	
	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 256;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() ) then
		
		if( tr.Entity:IsPlayer() ) then
			
			self.Owner:PrintMessage( 3, "Projected through " .. tr.Entity:Nick() .. "'s eyes." );
			self.Owner.LastProjectPos = self.Owner:GetPos();
			self.Owner:Spectate( OBS_MODE_IN_EYE );
			self.Owner:SpectateEntity( tr.Entity );
			
			self.Owner:SetNWInt( "targViewEntity", tr.Entity:EntIndex() );
			
		end
		
	end
	
	local ed = EffectData();
		ed:SetOrigin( BonePos );
		ed:SetAngle( BoneAng );
		ed:SetScale( 1 );
	util.Effect( "psi_rad", ed );
	
	self.Owner:SetNWInt( "PsyRemaining", math.Clamp( self.Owner:GetNWInt( "PsyRemaining" ) - 50, 0, 100 ) );
	self.Owner:EmitSound( "ambient/machines/thumper_hit.wav" );
	self.Owner:SetNWInt( "LastForce", CurTime() );
	
end

SWEP.Abilities = {
	
	{	-- Occipital
		{
			"Shield",
			function( self ) self:Shield() end
		},
		{
			"Cloak",
			function( self ) self:Cloak() end
		}
	},
	{	-- Pariatal
		{
			"Shield",
			function( self ) self:Shield() end
		},
		{
			"Push",
			function( self ) self:Push() end
		},
		{
			"Incapacitate",
			function( self ) self:Incap() end
		},
		{
			"Kill",
			function( self ) self:Terminate() end
		},
		{
			"Project",
			function( self ) self:Project() end
		}
	},
	{	-- Temporal
		{
			"Shield",
			function( self ) self:Shield() end
		},
		{
			"Push",
			function( self ) self:Push() end
		},
		{
			"Incapacitate",
			function( self ) self:Incap() end
		},
		{
			"Kill",
			function( self ) self:Terminate() end
		}
	},
	{	-- Frontal
		{
			"Shield",
			function( self ) self:Shield() end
		},
		{
			"Push",
			function( self ) self:Push() end
		},
		{
			"Incapacitate",
			function( self ) self:Incap() end
		},
		{
			"Kill",
			function( self ) self:Terminate() end
		}
	}
}

/*---------------------------------------------------------
   Name: SWEP:Initialize( )
   Desc: Called when the weapon is first loaded
---------------------------------------------------------*/
function SWEP:Initialize()

	if( SERVER ) then
	
		self:SetWeaponHoldType( "ar2" );
	
	end

end

function SWEP:Deploy()
	
	self.Weapon:SetNWInt( "GunMode", self.GunMode, 0 );

end


/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack( )
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	if( CLIENT ) then
		self.GunMode = self.Weapon:GetNWInt( "GunMode" );
	end
	
	if( CLIENT ) then return; end
	
	local i = self.Owner:GetNWInt( "PsyLevel" );
	
	self.Abilities[i][self.Weapon:GetNWInt( "GunMode" )][2]( self );
	
end

/*---------------------------------------------------------
   Name: SWEP:Precache( )
   Desc: Use this function to precache stuff
---------------------------------------------------------*/
function SWEP:Precache()
end
	

/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack( )
   Desc: +attack1 has been pressed
---------------------------------------------------------*/

function SWEP:Reload()
end

  /*---------------------------------------------------------
  SecondaryAttack
  ---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	
  	if( CLIENT ) then self.GunMode = self.Weapon:GetNWInt( "GunMode" ) + 1; end
	
  	if( SERVER ) then self.GunMode = self.GunMode + 1; end
	
	local i = self.Owner:GetNWInt( "PsyLevel" );
	
  	if( self.GunMode > #self.Abilities[i] ) then
  	
  		self.GunMode = 1;
		
  	end
  	
  	if( CLIENT ) then return; end
  	
  	self.Weapon:SetNWInt( "GunMode", self.GunMode );
  
end
  

function SWEP:Think()


end

function SWEP:DrawHUD()

	local modename = "";
	
	self.GunMode = self.Weapon:GetNWInt( "GunMode" );
	local i = self.Owner:GetNWInt( "PsyLevel" );
	
	modename = self.Abilities[i][self.GunMode][1];

	draw.DrawText( modename, "GModToolName", ScrW() - 4, 11, Color( 0, 0, 0, 255 ), 2 );
	draw.DrawText( modename, "GModToolName", ScrW() - 5, 10, Color( 255, 255, 255, 255 ), 2 );

	draw.DrawText( "^", "TargetID", ScrW() / 2, ScrH() / 2, Color( 255, 255, 255, 255 ), 1, 1 );

end

