Simple Custom Ammo Script

[lua]

CAM = {}

– Get Ammo Name. Just useless for now.
function CAM.AmmoName(ammo_name)
return “CA_”… ammo_name
end

– Creating Ammo using single function.
– ex) CAM.CreateAmmo( “5.56MM NATO”, 40, “models/penis.mdl”, Vector(0,0,0))
– Angle is Optional.

function CAM.CreateAmmo( ammo_name, amount, model, vector, angle )

if not model then print('NEED MODEL') return end
if not vector then print('NEED VECTOR POS') return end

local ammo = ents.Create("cam_ammo")

ammo:SetModel(model)
ammo:SetPos(vector)
if angle then
   ammo:SetAngles(angle)
end
CAM.SetAmmoEnt( ammo_name, amount, ammo )

end

– Setting Ammo Status
– ex) CAM.SetAmmoEnt( “5.56MM NATO”, 40, v)
function CAM.SetAmmoEnt( ammo_name, amount, ent )
ent.Amount = amount
ent.Ammo = ammo_name
ent:SetNWString(“CA_AmmoName”, ammo_name)
end

– Part 2. Setting Up Extention function
local meta = FindMetaTable( “Player” )

– Sending Ammo using single function.
– ex) ply:CA_SendAmmo( “5.56MM NATO”, 40, tr.Entity)

function meta:CA_SendAmmo( ammo_name, amount, target )
meta:CA_TakeAmmo( ammo_name, amount )
target:CA_GiveAmmo( ammo_name, amount )
end

– Counting Ammo.
– ex) ply:CA_GetAmmoCount( “5.56MM NATO” )

function meta:CA_GetAmmoCount( ammo_name )
return self:GetNWInt(CAM.AmmoName(ammo_name))
end

– Giving Ammo.
– ex) ply:CA_GiveAmmo( “5.56MM NATO”, 55, true )
– then ammo sound won’t emit.
– Silent is optional.

function meta:CA_GiveAmmo( ammo_name, amount, silent )

if not table.HasValue(self.AmmoTable, CAM.AmmoName(ammo_name)) then
    table.Add(self.AmmoTable,{ Name = ammo_name })
end
self:SetNWInt(CAM.AmmoName(ammo_name) ,self:CA_GetAmmoCount( ammo_name ) + amount )
if self:CA_GetAmmoCount( ammo_name ) < 0 then
    self:SetNWInt(CAM.AmmoName(ammo_name) ,0 )
end
if not silent then
    ply:EmitSound("BaseCombatCharacter.AmmoPickup")
end

end

– Same. and Lame.

function meta:CA_TakeAmmo( ammo_name, amount )

if not table.HasValue(self.AmmoTable, CAM.AmmoName(ammo_name)) then
    table.Add(self.AmmoTable,{ Name = ammo_name })
end
self:SetNWInt(CAM.AmmoName(ammo_name) ,self:CA_GetAmmoCount( ammo_name ) - amount )
if self:CA_GetAmmoCount( ammo_name ) < 0 then
    self:SetNWInt(CAM.AmmoName(ammo_name) ,0 )
end

end

– Same. and Lame.

function meta:CA_SetAmmo( ammo_name, amount )

if not table.HasValue(self.AmmoTable, CAM.AmmoName(ammo_name)) then
    table.Add(self.AmmoTable,{ Name = ammo_name })
end
self:SetNWInt(CAM.AmmoName(ammo_name) , amount )

end

– Strip All of Custom Ammo.
function meta:CA_StripAmmo()
if #self.AmmoTable > 0 then
for k,v in ipairs( self.AmmoTable ) do
self:SetNWInt(CAM.AmmoName(v) , 0 )
end
end
self.AmmoTable = {}
end

– Returns Custom Ammo list player have.
function meta:CA_GetAmmoList()
local Result = {}
if #self.AmmoTable > 0 then
for k,v in ipairs( self.AmmoTable ) do
table.insert(Result, v)
end
else
print(" I can’t get any of ammo. ")
end
return Result
end

[/lua]

So… What I need to add here?
Gimme some advice for this. :fuckyou:

Add ammo callbacks for damaging entities, impact effects and shit

You should be overriding default functions to make it work with other scripts

Oh rly?

Yes it would be a lot cleaner if you could get the ammo count and such in the same way you can with standard ammo types