-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
AddCSLuaFile()

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.AmmoHere",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_ammohere.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_ammohere_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.OutOfAmmo",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_outofammo.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_outofammo_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.RequestAmmo",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/PLAYER_REQUEST_TM_requestammo.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/PLAYER_REQUEST_TM_requestammo_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Ammokit.Pickup",
	["channel"] = CHAN_ITEM ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = "v92/bf2/common/pick_up_ammopack.mp3"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Ammokit.Deploy" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 },
	["sound"] = "v92/bf2/weapons/handheld/ammokit/medikit_deploy.wav"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Ammokit.Holster" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 } ,
	["sound"] = "v92/bf2/weapons/handheld/ammokit/medikit_deploy.wav"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Ammokit.Throw" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 } ,
	["sound"] = "v92/bf2/weapons/handheld/ammokit/throw.wav"
})

if CLIENT then

	local strEntityName = "v92_bf2_ammokit"
	local strInfoName = "Ammo Kit"

	SWEP.Category = "Utilities"
	SWEP.PrintName = strInfoName
	SWEP.Author = "V92"
	SWEP.Contact = "Steam"
	SWEP.Purpose = "Resupplying Nearby Units"
	SWEP.Instructions = "Primary: Supply Target\nSecondary: Throw Consumable Kit\nUSE+Secondary: Drop Consumable Kit"
	SWEP.Slot = 4
	SWEP.SlotPos = 32
	SWEP.WepSelectIcon = surface.GetTextureID("vgui/hud/" .. strEntityName )
	SWEP.ViewModelFOV = 67
	SWEP.BounceWeaponIcon = false

	language.Add( strEntityName, strInfoName )	
	killicon.Add( strEntityName, "vgui/entities/".. strEntityName , Color( 255, 255, 255 ) )

end

SWEP.Weight = 1
SWEP.Spawnable = true
SWEP.AdminOnly = false
SWEP.UseHands = true
SWEP.ViewModel = Model("models/v92/bf2/weapons/handheld/ammokit_c.mdl")
SWEP.WorldModel = Model("models/v92/bf2/weapons/handheld/ammokit_w.mdl")

SWEP.Primary.ClipSize = 100
SWEP.Primary.DefaultClip = 100
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "None"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "None"

SWEP.SupplyAmount = 25
SWEP.MaxAmmo = 100 -- Maximum ammo

local ThrowSound = Sound( "BF2.Weapon.Handheld.Ammokit.Throw" )
local SupplySound = Sound( "BF2.Weapon.Handheld.Ammokit.Pickup" )
local AmmoSoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.AmmoHere" )
local EmptySoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.OutOfAmmo" )
local RequestSoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.RequestAmmo" )
local DeployEntity = "v92_bf2_ammokit_ent"

function SWEP:Initialize()

	self:SetHoldType( "slam" )

	self.SupplyAmount = self.SupplyAmount or 25

	if CLIENT then 

		return

	end

	timer.Create( "ammokit_ammo" .. self:EntIndex() , 5 , 0 , function()
		if ( self:Clip1() < self.MaxAmmo ) then self:SetClip1( math.min( self:Clip1() + 5 , self.MaxAmmo ) ) end
	end )

end

function SWEP:PrimaryAttack()

	if CLIENT then 

		return

	end

	if self.Owner:IsPlayer() then

		self.Owner:LagCompensation( true )

	end

	local tr = util.TraceLine( {
		["start"] = self.Owner:GetShootPos(),
		["endpos"] = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 64,
		["filter"] = self.Owner
	} )

	if self.Owner:IsPlayer() then

		self.Owner:LagCompensation( false )

	end

	local ent = tr.Entity

	local need = 25

	if IsValid( ent ) and self:Clip1() >= need then

		self:TakePrimaryAmmo( need )

		local cw = ent:GetActiveWeapon()
		if not IsValid(cw) then
			self.Owner:EmitSound( EmptySoundVO )
			self:SetNextSecondaryFire( CurTime() + 3 )
			return
		end
		local ammoType = cw:GetPrimaryAmmoType()
		if ammoType == "rpg_round" or ammoType == "PanzerFaust3 Rocket" then
			-- Не давать патроны для ракетниц
			self.Owner:EmitSound( EmptySoundVO )
			self:SetNextSecondaryFire( CurTime() + 3 )
			return
		end
		ent:GiveAmmo( cw:GetMaxClip1() , ammoType , false )
		ent:EmitSound( SupplySound )

		if ent == tr.Entity then

			self.Owner:EmitSound( AmmoSoundVO )

		end

		self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )

		self:SetNextPrimaryFire( CurTime() + self:SequenceDuration() + 1 )
		self.Owner:SetAnimation( PLAYER_ATTACK1 )

		-- Even though the viewmodel has looping IDLE anim at all times, we need this to make fire animation work in multiplayer
		-- timer.Create( "weapon_idle" .. self:EntIndex(), self:SequenceDuration(), 1, function() if ( IsValid( self ) ) then self:SendWeaponAnim( ACT_VM_IDLE ) end end )

	else

		self.Owner:EmitSound( EmptySoundVO )

		self:SetNextSecondaryFire( CurTime() + 3 )

	end

end

function SWEP:SecondaryAttack()

	if CLIENT then 

		return

	end

	local ent = self.Owner

	local need = 25

	if IsValid( ent ) and self:Clip1() >= need then

		self:TakePrimaryAmmo( need )

		ent:EmitSound( AmmoSoundVO )

		self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )

		local deployEnt = ents.Create( DeployEntity )
		deployEnt:SetPos( ent:EyePos() + ent:GetAimVector() * 16 + ent:GetRight() * 2 + ent:GetUp() * -3 )
		deployEnt:SetAngles( ent:EyeAngles() - Angle( 45 , 0 , 0 ) )
		deployEnt:SetOwner( self.Owner )
		deployEnt:Spawn()
		
		if deployEnt:GetPhysicsObject():IsValid() then

			deployEnt:GetPhysicsObject():SetMass( 5 )
			
			if self.Owner:KeyDown( IN_USE ) then
			deployEnt:GetPhysicsObject():SetVelocity( ent:GetAimVector() * 50 )
			else
			deployEnt:GetPhysicsObject():SetVelocity( ent:GetAimVector() * 400 )
			end

		end

		self:SetNextSecondaryFire( CurTime() + self:SequenceDuration() + 1 )
		self.Owner:SetAnimation( PLAYER_ATTACK1 )

		-- timer.Create( "weapon_idle" .. self:EntIndex(), self:SequenceDuration(), 1, function() if ( IsValid( self ) ) then self:SendWeaponAnim( ACT_VM_IDLE ) end end )

	else

		self.Owner:EmitSound( EmptySoundVO )

		self:SetNextSecondaryFire( CurTime() + 3 )

	end

end

function SWEP:OnRemove()

	timer.Stop( "ammokit_ammo" .. self:EntIndex() )
	-- timer.Stop( "weapon_idle" .. self:EntIndex() )

end

function SWEP:Holster()

	-- timer.Stop( "weapon_idle" .. self:EntIndex() )

	return true

end

function SWEP:CustomAmmoDisplay()

	self.AmmoDisplay = self.AmmoDisplay or {} 

	self.AmmoDisplay.Draw = true -- draw the display?

	if self.Primary.ClipSize > 0 then

		self.AmmoDisplay.PrimaryClip = self:Clip1() -- amount in clip
		self.AmmoDisplay.PrimaryAmmo = self:Ammo1() -- amount in reserve

	end
	
	if self.Secondary.ClipSize > 0 then

		self.AmmoDisplay.SecondaryAmmo = self:Ammo2() -- amount of secondary ammo

	end

	return self.AmmoDisplay -- return the table

end
-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
