if( SERVER ) then
   
 	AddCSLuaFile( "shared.lua" ) 
 	SWEP.HoldType = "pistol";
 
 end 

 
 SWEP.Base = "ts2_base";
   
 SWEP.Spawnable			= false 
 SWEP.AdminSpawnable		= false 
   


SWEP.Primary.Sound = Sound( "Weapon_SMG1.Single" );

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


SWEP.PrintName = "Hands";
SWEP.TS2Desc = "Pick up things or punch someone.\n(HOLSTERED) Reload - Carry a suitcase/briefcase\n(HOLSTERED) Left click - Knock  on door/Throw\n(HOLSTERED) Right click - Pick up/drop\n(UNHOLSTERED) Left click - Punch\n(UNHOLSTERED) Right click - Block";


 SWEP.Primary.ViewPunchMul = .5;
 SWEP.Primary.Damage			= 5 
 SWEP.Primary.NumShots		= 1 
 
 SWEP.TS2HoldType = "FIST";

SWEP.Slot = 1;

SWEP.Primary.ClipSize = 0;
SWEP.Primary.DefaultClip = 0;
SWEP.Primary.Delay = .07;
SWEP.Primary.Automatic = true;

 SWEP.Primary.IronSightPos = Vector( 0, 5.5, -5.0 );
 SWEP.Primary.IronSightAng = Vector( 0.0, 0.0, 0.0 );

SWEP.Primary.HolsteredPos = Vector( -2.4, -4, -12.0 );

SWEP.LastPickUp = 0;

SWEP.LastReload = 0;

HitSounds = {
	
	Sound( "npc/vort/foot_hit.wav" ),
	Sound( "weapons/crossbow/hitbod1.wav" ),
	Sound( "weapons/crossbow/hitbod2.wav" )
}
	
SwingSounds = {
	
	Sound( "npc/vort/claw_swing1.wav" ),
	Sound( "npc/vort/claw_swing2.wav" )
	
}

function SWEP:Reload()

	if( CLIENT ) then return; end

	if( not self.Owner:GetPlayerHolstered() ) then return; end
	
	if( CurTime() - self.LastReload < .5 ) then
		return;
	end 
	
	self.LastReload = CurTime();

	if( self.Owner:GetTable().RightHandEntity and self.Owner:GetTable().RightHandEntity:IsValid() ) then
	
		self.Owner:GetTable().RightHandEntity:Remove();
		self.Owner:GetTable().RightHandEntity = nil;
		return;
	
	end

	local trace = { }
	trace.start = self.Owner:EyePos();
	trace.endpos = trace.start + self.Owner:GetAimVector() * 90;
	trace.filter = self.Owner;
	
	local tr = util.TraceLine( trace );
	
	if( tr.Entity and tr.Entity:IsValid() ) then
	
		if( self.Owner:IsTied() ) then return; end 
	
		if( tr.Entity:GetTable().ItemData and tr.Entity:GetTable().ItemData.CanHoldInHand ) then

			self.Owner:GiveContainerEntity( tr.Entity, true );
			local ent = self.Owner:AttachProp( tr.Entity:GetTable().ItemData.Model, "anim_attachment_RH" );
			
			self.Owner:GetTable().RightHandEntity = ent;
			self.Owner:GetTable().RightHandItemName = tr.Entity:GetTable().ItemData.Name;
			self.Owner:GetTable().RightHandItemID = tr.Entity:GetTable().ItemData.ID;
			
			local ply = self.Owner;
			
			local remove = function( pos, ang )
				
				local itemprop = TS.CreateItemProp( ply:GetTable().RightHandItemID, pos, ang );
				itemprop = TS.ItemToContainer( itemprop:GetTable().ItemData );
		
				local itemdata = TS.ItemsData[ply:GetTable().RightHandItemID];
				local inv = 0;
				
				for n = 1, TS.MaxInventories do
				
					if( ply:GetTable().Inventories[n].Name == itemdata.Name ) then
					
						inv = n;	
					
					end
				
				end
				
				local activeweapx = -1;
				local activeweapy = -1;
				
				for k = 0, itemdata.Height - 1 do
			
					for j = 0, itemdata.Width - 1 do
					
						local port = true;
					
						if( ply:GetTable().InventoryGrid[inv][j][k].x == activeweapx and
							ply:GetTable().InventoryGrid[inv][j][k].y == activeweapy ) then
							
							port = false;
							
						end
					
						if( ply:GetTable().InventoryGrid[inv][j][k].ItemData and ply:GetActiveWeapon():IsValid() ) then
						
							if( ply:GetTable().InventoryGrid[inv][j][k].ItemData.ID == ply:GetActiveWeapon():GetClass() ) then
							
								if( ply:HasWeaponOnlyFrom( ply:GetTable().InventoryGrid[inv][j][k].ItemData.ID, inv ) ) then
								
									port = false;
									
									activeweapx = ply:GetTable().InventoryGrid[inv][j][k].x;
									activeweapy = ply:GetTable().InventoryGrid[inv][j][k].y;
									
									if( not ply:GiveAnyInventoryItemBut( ply:GetTable().InventoryGrid[inv][j][k].ItemData.ID, inv ) ) then
									
										ply:GetTable().HasTempWeapon = true;
										ply:GetTable().TempWeaponClass = ply:GetActiveWeapon():GetClass();
										ply:HandleWeaponChangeTo( ply:GetTable().TempWeaponClass );
										
									end
									
									ply:GetTable().InventoryGrid[inv][j][k].ItemData = nil;
								
								end
							
							end
						
						end
					
						if( port ) then
					
							itemprop.InventoryGrid[j][k].Filled = ply:GetTable().InventoryGrid[inv][j][k].Filled;
							itemprop.InventoryGrid[j][k].SX = ply:GetTable().InventoryGrid[inv][j][k].SX;
							itemprop.InventoryGrid[j][k].SY = ply:GetTable().InventoryGrid[inv][j][k].SY;
							itemprop.InventoryGrid[j][k].ItemData  = ply:GetTable().InventoryGrid[inv][j][k].ItemData;

						else
						
							itemprop.InventoryGrid[j][k].Filled = false;
							itemprop.InventoryGrid[j][k].SX = -1;
							itemprop.InventoryGrid[j][k].SY = -1;
							itemprop.InventoryGrid[j][k].ItemData = nil;
						
						end

					end
				
				end
				
				ply:RemoveInventory( itemdata.Name );
			
			end
			
			ent:SetRemoveFunc( remove );
			
			tr.Entity:Remove();
			
		end
	
	end

end

SWEP.NextPrimaryAttack = 0;

function SWEP:PrimaryAttack()

	if( SERVER and self.Owner:IsTied() ) then return; end 

	if( CurTime() < self.NextPrimaryAttack ) then return; end

	if( ( SERVER and self.Owner:GetPlayerHolstered() ) or ( CLIENT and ClientVars["Holstered"] ) ) then
	
		if( SERVER ) then
	
			if( self.Owner:GetTable().HandPickUpSent and self.Owner:GetTable().HandPickUpSent:IsValid() and self.Owner:GetTable().HandPickUpTarget and self.Owner:GetTable().HandPickUpTarget:IsValid() ) then
			
				local ent = self.Owner:GetTable().HandPickUpTarget;
				self.Owner:RemoveHandPickUp();
				self.Owner:GetTable().HandPickUpTarget:GetPhysicsObject():ApplyForceCenter( self.Owner:GetAimVector() * 1000 );
	
				return;
	
			end
			
		end
		
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 30;
		trace.filter = self.Owner;
		
		local tr = util.TraceLine( trace );
		
		if( tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
		
			self.Weapon:EmitSound( Sound( "physics/wood/wood_crate_impact_hard2.wav" ) );
			self.NextPrimaryAttack = CurTime() + math.Rand( .2, .7 );
			
		end
	
	else
	
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 30;
		trace.filter = self.Owner;
	
		local tr = util.TraceLine( trace );
		
		if( tr.Hit ) then
		self.Weapon:EmitSound( HitSounds[math.random( 1, #HitSounds )] );
		end
		
		if( SERVER ) then
		
		local power = 0.5;
		local norm = ( self.Owner:EyePos() - tr.HitPos ):Normalize();
		
		if( tr.Entity:IsValid() ) then
			if( not tr.Entity:IsPlayer() ) then
				tr.Entity:GetPhysicsObject():ApplyForceOffset( norm * -9000 * math.Clamp( power, .4, 1 ), tr.HitPos );
			else
				self.NextPrimaryAttack = CurTime() + math.Rand( .2, .7 );
				tr.Entity:HandleStunStickHit( ply, power, tr.HitGroup, 2 );
			end
		end
		
		else
	
		RunConsoleCommand( "eng_dorealprimaryattack", "" );
	
		end
		
		self.Weapon:EmitSound( SwingSounds[math.random( 1, #SwingSounds )] );
		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER );
		self.NextPrimaryAttack = CurTime() + math.Rand( .4, .6 );
	
	end

	

end

function SWEP:SecondaryAttack()

	if( CLIENT ) then return; end
	
	if( self.Owner:IsTied() ) then return; end
	
	if( self.Owner:GetPlayerHolstered() ) then

		if( self.Owner:GetTable().HandPickUpSent and self.Owner:GetTable().HandPickUpSent:IsValid() ) then
			self.Owner:RemoveHandPickUp();
		elseif( CurTime() - self.LastPickUp > 1 ) then
	
			local trace = { }
			trace.start = self.Owner:EyePos();
			trace.endpos = trace.start + self.Owner:GetAimVector() * 60;
			trace.filter = self.Owner;
			
			local tr = util.TraceLine( trace );
			
			if( tr.Entity and tr.Entity:IsValid() ) then
			
				self.Owner:HandPickUp( tr.Entity, tr.PhysicsBone );
			
			end
			
			self.LastPickUp = CurTime();
			
		end

	else
	
	end

end

function SWEP:HolsterToggle()

	if( self.Owner:GetTable().HandPickUpSent and self.Owner:GetTable().HandPickUpSent:IsValid() ) then
		self.Owner:RemoveHandPickUp();
	end
	
	if( self.Owner:GetTable().RightHandEntity and self.Owner:GetTable().RightHandEntity:IsValid() ) then
	
		self.Owner:GetTable().RightHandEntity:Remove();
		self.Owner:GetTable().RightHandEntity = nil;
	
	end 

end

SWEP.LastRam = 0;

function SWEP:Think()
	
	if( CLIENT ) then return; end
	
	if( not self.Owner:GetPlayerHolstered() ) then
  	
  	if( CurTime() - self.LastRam > 3 and self.Owner:KeyDown( IN_RELOAD ) ) then
  		self.LastRam = CurTime();
		self.Owner:TryDoorRam();
	end
	
	end

end

