AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false 
SWEP.MetalToGive = 100

SWEP.SequenceDelay = 1.25
SWEP.DrawDelay = 0.6

local buildings = { "obj_dispenser", "obj_sentrygun", "obj_teleporter_entrance", "obj_teleporter_exit" }

/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	self.idledelay = CurTime() +self:SequenceDuration()
	
	local pos = self.Owner:GetShootPos()
	local ang = self.Owner:GetAimVector()
	local tracedata = {}
	tracedata.start = pos
	tracedata.endpos = pos +(self.Owner:GetAimVector( ) *80)
	tracedata.filter = self.Owner
	local tr = util.TraceLine(tracedata)
	local ammo = self.Owner:GetNetworkedInt( "ammo_" .. self.Primary.Ammo )
	if tr.Entity and ValidEntity(tr.Entity) and table.HasValue( buildings, tr.Entity:GetClass() ) and !tr.Entity.sapped and self:CheckTeam(tr.Entity) then
		self.Owner:EmitSound( "weapons/sapper_plant.wav", 100, 100 )
		
		local sapper = ents.Create( "obj_sapper" )
		sapper:SetPos( tr.Entity:GetPos() )
		sapper:SetAngles( tr.Entity:GetAngles() )
		sapper.sapent = tr.Entity
		sapper.owner = self.Owner
		sapper:Spawn()
		sapper:Activate()
		
		if self.sapper then self.sapper:Remove(); self.sapper = nil end
		
		tr.Entity.sapper = sapper
		tr.Entity:Sap()
	end
	
	// 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:SecondaryAttack()
end

function SWEP:StopPrimaryAttack()
	self.validbuildpos = false
end
