if ( SERVER ) then
	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "smg"
end

SWEP.Author = "Silverlan"
SWEP.Contact = "Silverlan@gmx.de"
SWEP.Purpose = ""
SWEP.Instructions = ""
SWEP.Base = "base_swep_tf_melee"
SWEP.Category		= "Team Fortress 2"
SWEP.GotGlobalClip = false

SWEP.GotSecondary = false

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.ViewModel = "models/weapons/v_models/v_sapper_spy.mdl"
SWEP.WorldModel = "models/weapons/w_models/w_sapper.mdl"

SWEP.Primary.Delay			= 0.4

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

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

/*---------------------------------------------------------
   Name: SWEP:Holster( weapon_to_swap_to )
   Desc: Weapon wants to holster
   RetV: Return true to allow the weapon to holster
---------------------------------------------------------*/
function SWEP:Holster( wep )
	if self.sapper then self.sapper:Remove(); self.sapper = nil end
	return true
end

function SWEP:CheckTeam(ent)
	local ent_owner = ent.owner
	if !ent_owner or !ValidEntity(ent_owner) then return true end
	if ent_owner != self.Owner and ((ent_owner:Team() != 1 and ent_owner:Team() != 2 and self.Owner:Team() != 1 and self.Owner:Team() != 2) or ent_owner:Team() != self.Owner:Team()) then return true end
	return false
end

function SWEP:Think()
	if CLIENT then return end
	if self.idledelay and CurTime() > self.idledelay then
		self.idledelay = nil
		self:SendWeaponAnim( ACT_VM_IDLE )
	end
	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 pp = self.Owner:GetViewModel():GetPoseParameter("valid_build_pos")
	if tr.Entity and ValidEntity(tr.Entity) and table.HasValue(buildings,tr.Entity:GetClass()) and !tr.Entity.sapped and self:CheckTeam(tr.Entity) then
		if pp +0.03 < 1.03 then
			self.Owner:GetViewModel():SetPoseParameter("valid_build_pos", pp +0.03)
		end
		if !self.sapper then
			self.sapper = ents.Create("prop_dynamic")
			local sapper_model
			local class = tr.Entity:GetClass()
			if class == "obj_dispenser" then
				sapper_model = "dispenser.mdl"
			elseif class == "obj_sentrygun" then
				sapper_model = "sentry" .. tr.Entity.level .. ".mdl"
			else
				sapper_model = "teleporter.mdl"
			end
			self.sapper:SetModel("models/buildables/sapper_placement_" .. sapper_model)
			self.sapper:SetParent(tr.Entity)
			self.sapper:Spawn()
			self.sapper:Activate()
			self.sapper:Fire("SetParentAttachment","build_point_0",0)
			for k, v in pairs(player:GetAll()) do
				if v != self.Owner then
					v:SendLua( "ents.GetByIndex(" .. self.sapper:EntIndex() .. "):SetColor(255,255,255,0)" )
				end
			end
		end
	elseif pp -0.045 > -0.045 then
		local pp = self.Owner:GetViewModel():GetPoseParameter("valid_build_pos") -0.045
		self.Owner:GetViewModel():SetPoseParameter("valid_build_pos", pp)
		if self.sapper then self.sapper:Remove(); self.sapper = nil end
	end
end
