if CLIENT then

	SWEP.WepSelectIcon = surface.GetTextureID("vgui/hud/weapon_doietoolus")
	SWEP.DrawWeaponInfoBox	= false
	SWEP.BounceWeaponIcon = false 
	killicon.Add( "weapon_doietoolus", "vgui/hud/weapon_doietoolus", Color( 0, 0, 0, 255 ) )
	end
	
	SWEP.PrintName = "Entrenching Tool FPV"
	
	SWEP.Category = "Engiener Tools"
	
	SWEP.Spawnable= true
	SWEP.AdminSpawnable= true
	SWEP.AdminOnly = false
	
	SWEP.ViewModelFOV = 60
	SWEP.ViewModel = "models/weapons/doi/v_etool_us.mdl" 
	SWEP.WorldModel = "models/weapons/doi/w_etool_us.mdl"
	SWEP.ViewModelFlip = false
	
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false
	
	SWEP.Slot = 0
	SWEP.SlotPos = 0
	 
	SWEP.UseHands = true
	
	SWEP.HoldType = "melee2" 
	
	SWEP.FiresUnderwater = false
	
	SWEP.DrawCrosshair = false
	
	SWEP.DrawAmmo = true
	
	SWEP.Base = "weapon_base"
	
	SWEP.CSMuzzleFlashes = true
	
	SWEP.Sprint = 0
	
	SWEP.Primary.ClipSize = 0
	SWEP.Primary.DefaultClip = 0
	SWEP.Primary.Automatic = false
	SWEP.Primary.Ammo = "none"
	
	SWEP.Secondary.ClipSize = 0
	SWEP.Secondary.DefaultClip = 0
	SWEP.Secondary.Automatic = false
	SWEP.Secondary.Ammo = "none"
	
	SWEP.Structures =  	{	
							{
								name = "FPV-дрон",
								icon = "entities/entity.png",
								cat = 0,
								entity = "entity_drone_bomb",
								count = 2,
								id = 1,
								corrangle = Angle(90,0,0)
							},
							{
								name = "Разведывательный дрон",
								icon = "entities/entity.png",
								cat = 0,
								entity = "entity_drone_base",
								count = 1,
								id = 2,
								corrangle = Angle(90,0,0)
							},
							{
								name = "РЭБ",
								icon = "entities/entity.png",
								cat = 0,
								entity = "entity_dronejammer",
								count = 1,
        						id = 3,
								corrangle = Angle(90,0,0)
							},
							{
								name = "Зарядная станция для дрона",
								icon = "entities/entity.png",
								cat = 0,
								entity = "entity_dronepad",
								count = 1,
        						id = 4,
								corrangle = Angle(90,0,0)
							},
						}
	
	function SWEP:Initialize()
		self:SetWeaponHoldType( self.HoldType )
		self:GetOwner().buildtime = 0
	end 
	
	function SWEP:Deploy()
		self:SetNextPrimaryFire( CurTime() + self.Owner:GetViewModel():SequenceDuration() )
		self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
		Sprint = 0
	end
	
	function SWEP:Holster()
		self.NextSecondaryAttack = 0
		Sprint = 0 
		return true
	end
	
	function SWEP:PrimaryAttack()
		if !(self.Weapon:GetNextPrimaryFire() < CurTime()) || not(Sprint == 0) then return end
		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
		self.Owner:SetAnimation( PLAYER_ATTACK1 )
		self:SetNextPrimaryFire( CurTime() + 1)
		self:SetNextSecondaryFire( CurTime() + 1)
	
		if SERVER then
			net.Start("openmenu")
			net.WriteTable(self.Structures)
			net.Send(self:GetOwner())	
		end
	
	end
	
	function SWEP:SecondaryAttack()
	end
	
	function SWEP:Reload()
    if SERVER then
        local item = self:GetOwner():GetEyeTrace().Entity
        if item:IsValid() then
            local structure_id = item.structure_id
            if structure_id != nil then
                if item:GetOwner() == self:GetOwner() then
                    local struct = self.Structures[structure_id]
                    if (item:GetClass() == struct.entity) or (item:GetModel() == struct.model) then
                        item:Remove()
                        struct.count = struct.count + 1
                    end
                end
            end
        end
    end
end
	function SWEP:DrawHUDBackground()
		if self:GetOwner().buildtime > 0 then
			draw.RoundedBox( 20, ScrW()/2-150, ScrH()/2-30, 0+300-self:GetOwner().buildtime*15, 60, Color( 50, 50, 50 , 180) )
			draw.RoundedBox( 20, ScrW()/2-145, ScrH()/2-25, 0+290-self:GetOwner().buildtime*14.5, 50, Color( 0, 255, 0 ,125) )
			draw.DrawText( tostring(100 - self:GetOwner().buildtime*5) .. '%', "Font3", ScrW()/2-20, ScrH()/2-15, Color( 255, 255, 255 ,255), 0 )
		end
	end
	
	local delay = 0.5
	local nextOccurance = 0
	local flag1 = true 
		if CLIENT then
	function SWEP:RenderScreen()
					if self.Owner.buildtime < 0  or Sprint == 2  then
						self.Weapon:SendWeaponAnim(ACT_VM_IDLE)
					end
	
	
				if input.IsKeyDown(KEY_F) and flag1 then
					net.Start("buildstop12")
					net.SendToServer()	
					self.Owner.building = false
					self.Owner.buildtime = -3
					flag1 = false
					timer.Simple(1, function() flag1 = true end)
				end
	 
				local timeLeft = nextOccurance - CurTime()
				if timeLeft < 0 and self:GetOwner().buildtime >= 0 then 
					self:GetOwner().buildtime = self:GetOwner().buildtime - 0.5
					self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
					self:GetOwner():SetAnimation( PLAYER_ATTACK1 )
					self:SetNextPrimaryFire( CurTime() + delay)
					nextOccurance = CurTime() + delay
				end
				net.Receive("buildanim", function(len, ply)
					self:GetOwner().buildtime = 20
				end)	
		end
	end
	if SERVER then
	function SWEP:Think()
				net.Receive("buildstop12", function(len, ply)
                    ply:Freeze(false)
                        if self.LastEnt:IsValid() then
                            self.Structures[self.LastEnt.id].count = self.Structures[self.LastEnt.id].count + 1
                            self.LastEnt:Remove()
                        end
                    end)
				net.Receive("setentity", function(len, ply)
    if ply:GetActiveWeapon():GetClass() ~= "engineertoolfpv" then return end
    local id = net.ReadInt(13)
    local struct = self.Structures[id]

    if struct and struct.count > 0 then
        local trace = ply:GetEyeTrace()
        if (trace.HitPos - trace.StartPos):Length() > 300 then return end

        local item = ents.Create(struct.entity)
        item:SetPos(trace.HitPos + trace.HitNormal * 10)
        item:SetAngles(trace.HitNormal:Angle() + struct.corrangle)
        item:Spawn()
        struct.count = struct.count - 1
        item.structure_id = id
        if IsValid(ply) and ply:IsPlayer() then
            item:SetOwner(ply)
        end
    end
end)
	if (Sprint == 0)  then
		if self.Owner:KeyDown(IN_SPEED) and (self.Owner:KeyDown(IN_FORWARD) || self.Owner:KeyDown(IN_BACK) || self.Owner:KeyDown(IN_MOVELEFT) || self.Owner:KeyDown(IN_MOVERIGHT)) then
			Sprint = 1
		end
	end
	if (Sprint == 1) then
		self.Weapon:SendWeaponAnim(ACT_VM_SPRINT_IDLE)
		Sprint = 2
	end
	if (Sprint == 2)  then
		if self.Owner:KeyReleased(IN_SPEED)  then
			self.Weapon:SendWeaponAnim(ACT_VM_IDLE)
				Sprint = 0
			end
		end
	end
end

