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

ENT.Type = "anim"
ENT.Base = "base_gmodentity"
ENT.Category = "Equipment"
ENT.PrintName = "BF2 Ammokit"
ENT.Author = "V92"
ENT.Spawnable = true
ENT.AdminOnly = true

local DeployModel = Model("models/v92/bf2/weapons/handheld/ammokit_w.mdl") 
local SupplySound = Sound( "BF2.Weapon.Handheld.Ammokit.Pickup" )
ENT.SupplyAmount = 5

------------------------------------------------------------
-- None of the shit under this concerns the client,
-- so we aren't even going to scope it to save resources.
------------------------------------------------------------
if CLIENT then

	return

end

function ENT:Initialize()

	self:SetModel( DeployModel )
	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	self:SetCollisionGroup( COLLISION_GROUP_NONE )

	local phys = self:GetPhysicsObject()
	if phys and phys:IsValid() then	phys:Wake()	end

	util.PrecacheModel( DeployModel )
	util.PrecacheSound( SupplySound )

	-- This is sloppy - but it works - so kiss my ass.
	timer.Simple( 1.5 , function()

		if IsValid( self ) then

			-- Need to delay this or you eat it immediately on spawn, 
			-- which is an issue if you're trying to throw it to someone.
			self:SetTrigger( true )

		end

	end)

	-- I understand people might want to spawn a bunch of these as set dressing,
	-- but honestly it becomes a clusterfuck very quickly.
	-- How do I track how many a player has thrown? Max of 2?
	timer.Simple( 300 , function()

		if IsValid( self ) then

			SafeRemoveEntity( self )

		end

	end)

end

------------------------------------------------------------
-- TOUCH! Patrick, don't touch. TOUCH!
------------------------------------------------------------
function ENT:StartTouch( toucher )

	-- The toucher isn't a player or NPC and the toucher is at/exceeding full health...
	if !IsValid( toucher ) or !toucher:IsPlayer() or toucher:GetActiveWeapon():GetPrimaryAmmoType() == -1 then

		-- DE-NIED!
		return false
		
	end

	local cw = toucher:GetActiveWeapon()
	local need = cw:GetMaxClip1()

	if cw:Clip1() > -1 then

		toucher:GiveAmmo( need , cw:GetPrimaryAmmoType() , false )
		toucher:EmitSound( SupplySound )

		self.SupplyAmount = self.SupplyAmount - 1

	end

	-- Is the charge empty?
	if self.SupplyAmount <= 0 then

		-- Fuck off to dev/null with your ass
		self:Remove()

	end

	-- Don't let the function directly run again to prevent cascading use
	return

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