here my custom weapon base code
SWEP.PrintName = "ZETA Weapon Base"
SWEP.WorldModel = ""
SWEP.ViewModel = ""
SWEP.Primary.Automatic = true
SWEP.Primary.FireRate = 0
SWEP.Primary.takeclip = 1
SWEP.Primary.DefaultClip = 30
SWEP.Primary.Sound = ""
SWEP.Primary.ClipSize = 30
SWEP.Reloading = 0
SWEP.ReloadingTimer = CurTime()
SWEP.Secondary.Automatic = false
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.Ammo = "none"
SWEP.fireData = {
dmgMin = 15,
dmgMax = 35,
force = 1,
spread = 0.02,
ammo = "Pistol",
num = 1,
tracer = "Trace"
}
SWEP.ViewModelReloadAnim = ACT_VM_RELOAD
SWEP.ViewModelFireAnim = ACT_VM_PRIMARYATTACK
SWEP.ReloadTime = 1
SWEP.Primary.delay = CurTime()
SWEP.ReloadSounds = {}
SWEP.DeploySound = ""
SWEP.NoBullet = false
SWEP.Anim = {
idle = ACT_HL2MP_IDLE_PHYSGUN,
move = ACT_HL2MP_RUN_PHYSGUN,
moveslow = ACT_HL2MP_WALK_PHYSGUN,
jump = ACT_HL2MP_JUMP_PHYSGUN,
crouch = ACT_HL2MP_WALK_CROUCH_PHYSGUN,
crouchidle = ACT_HL2MP_IDLE_CROUCH_PHYSGUN,
reload = ACT_HL2MP_GESTURE_RELOAD_REVOLVER,
attack = ACT_HL2MP_GESTURE_RANGE_ATTACK_REVOLVER
}
local ActIndex = {
[ "pistol" ] = ACT_HL2MP_IDLE_PISTOL,
[ "smg" ] = ACT_HL2MP_IDLE_SMG1,
[ "grenade" ] = ACT_HL2MP_IDLE_GRENADE,
[ "ar2" ] = ACT_HL2MP_IDLE_AR2,
[ "shotgun" ] = ACT_HL2MP_IDLE_SHOTGUN,
[ "rpg" ] = ACT_HL2MP_IDLE_RPG,
[ "physgun" ] = ACT_HL2MP_IDLE_PHYSGUN,
[ "crossbow" ] = ACT_HL2MP_IDLE_CROSSBOW,
[ "melee" ] = ACT_HL2MP_IDLE_MELEE,
[ "slam" ] = ACT_HL2MP_IDLE_SLAM,
[ "normal" ] = ACT_HL2MP_IDLE,
[ "fist" ] = ACT_HL2MP_IDLE_FIST,
[ "melee2" ] = ACT_HL2MP_IDLE_MELEE2,
[ "passive" ] = ACT_HL2MP_IDLE_PASSIVE,
[ "knife" ] = ACT_HL2MP_IDLE_KNIFE,
[ "duel" ] = ACT_HL2MP_IDLE_DUEL,
[ "camera" ] = ACT_HL2MP_IDLE_CAMERA,
[ "magic" ] = ACT_HL2MP_IDLE_MAGIC,
[ "revolver" ] = ACT_HL2MP_IDLE_REVOLVER
}
--[[---------------------------------------------------------
Name: SetWeaponHoldType
Desc: Sets up the translation table, to translate from normal
standing idle pose, to holding weapon pose.
-----------------------------------------------------------]]
function SWEP:SetWeaponHoldType( t )
t = string.lower( t )
local index = ActIndex[ t ]
if ( index == nil ) then
Msg( "SWEP:SetWeaponHoldType - ActIndex[ \"" .. t .. "\" ] isn't set! (defaulting to normal)\n" )
t = "normal"
index = ActIndex[ t ]
end
self.ActivityTranslate = {}
self.ActivityTranslate[ ACT_MP_STAND_IDLE ] = self.Anim.idle
self.ActivityTranslate[ ACT_MP_WALK ] = self.Anim.moveslow
self.ActivityTranslate[ ACT_MP_RUN ] = self.Anim.move
self.ActivityTranslate[ ACT_MP_CROUCH_IDLE ] = self.Anim.crouchidle
self.ActivityTranslate[ ACT_MP_CROUCHWALK ] = self.Anim.crouch
self.ActivityTranslate[ ACT_MP_ATTACK_STAND_PRIMARYFIRE ] = self.Anim.attack
self.ActivityTranslate[ ACT_MP_ATTACK_CROUCH_PRIMARYFIRE ] = self.Anim.attack
self.ActivityTranslate[ ACT_MP_RELOAD_STAND ] = self.Anim.reload
self.ActivityTranslate[ ACT_MP_RELOAD_CROUCH ] = self.Anim.reload
self.ActivityTranslate[ ACT_MP_JUMP ] = self.Anim.jump
self.ActivityTranslate[ ACT_RANGE_ATTACK1 ] = index + 8
self.ActivityTranslate[ ACT_MP_SWIM ] = self.Anim.idle
-- "normal" jump animation doesn't exist
if ( t == "normal" ) then
self.ActivityTranslate[ ACT_MP_JUMP ] = ACT_HL2MP_JUMP_FIST
end
self:SetupWeaponHoldTypeForAI( t )
end
function SWEP:CustomDeploy()
end
function SWEP:Deploy()
self.Reloading = 0
self.ReloadingTimer = CurTime()
self:EmitSound(Sound(self.DeploySound))
self:CustomDeploy()
return true
end
function SWEP:OnHolster()
end
function SWEP:Initialize()
self:SetWeaponHoldType("ar2") --quick fix
--other initialize code goes here
if CLIENT then
--Create a new table for every weapon instance
self.VElements = table.FullCopy( self.VElements )
self.WElements = table.FullCopy( self.WElements )
self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
self:CreateModels(self.VElements) --create viewmodels
self:CreateModels(self.WElements) --create worldmodels
--init view model bone build function
if IsValid(self.Owner) then
local vm = self.Owner:GetViewModel()
if IsValid(vm) then
self:ResetBonePositions(vm)
--Init viewmodel visibility
if (self.ShowViewModel == nil or self.ShowViewModel) then
vm:SetColor(Color(255,255,255,255))
else
--we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
vm:SetColor(Color(255,255,255,1))
--^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
--however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
vm:SetMaterial("Debug/hsv")
end
end
end
end
end
function SWEP:Holster()
if CLIENT and IsValid(self.Owner) then
local vm = self.Owner:GetViewModel()
if IsValid(vm) then
self:ResetBonePositions(vm)
end
end
self:OnHolster()
return true
end
function SWEP:OnRemove()
self:Holster()
end
if CLIENT then
SWEP.vRenderOrder = nil
function SWEP:ViewModelDrawn()
local vm = self.Owner:GetViewModel()
if !IsValid(vm) then return end
if (!self.VElements) then return end
self:UpdateBonePositions(vm)
if (!self.vRenderOrder) then
--we build a render order because sprites need to be drawn after models
self.vRenderOrder = {}
for k, v in pairs( self.VElements ) do
if (v.type == "Model") then
table.insert(self.vRenderOrder, 1, k)
elseif (v.type == "Sprite" or v.type == "Quad") then
table.insert(self.vRenderOrder, k)
end
end
end
for k, name in ipairs( self.vRenderOrder ) do
local v = self.VElements[name]
if (!v) then self.vRenderOrder = nil break end
if (v.hide) then continue end
local model = v.modelEnt
local sprite = v.spriteMaterial
if (!v.bone) then continue end
local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
if (!pos) then continue end
if (v.type == "Model" and IsValid(model)) then
model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
model:SetAngles(ang)
--model:SetModelScale(v.size)
local matrix = Matrix()
matrix:Scale(v.size)
model:EnableMatrix( "RenderMultiply", matrix )
if (v.material == "") then
model:SetMaterial("")
elseif (model:GetMaterial() != v.material) then
model:SetMaterial( v.material )
end
if (v.skin and v.skin != model:GetSkin()) then
model:SetSkin(v.skin)
end
if (v.bodygroup) then
for k, v in pairs( v.bodygroup ) do
if (model:GetBodygroup(k) != v) then
model:SetBodygroup(k, v)
end
end
end
if (v.surpresslightning) then
render.SuppressEngineLighting(true)
end
render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
render.SetBlend(v.color.a/255)
model:DrawModel()
render.SetBlend(1)
render.SetColorModulation(1, 1, 1)
if (v.surpresslightning) then
render.SuppressEngineLighting(false)
end
elseif (v.type == "Sprite" and sprite) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
render.SetMaterial(sprite)
render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
elseif (v.type == "Quad" and v.draw_func) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
cam.Start3D2D(drawpos, ang, v.size)
v.draw_func( self )
cam.End3D2D()
end
end
end
SWEP.wRenderOrder = nil
function SWEP:DrawWorldModel()
if (self.ShowWorldModel == nil or self.ShowWorldModel) then
self:DrawModel()
end
if (!self.WElements) then return end
if (!self.wRenderOrder) then
self.wRenderOrder = {}
for k, v in pairs( self.WElements ) do
if (v.type == "Model") then
table.insert(self.wRenderOrder, 1, k)
elseif (v.type == "Sprite" or v.type == "Quad") then
table.insert(self.wRenderOrder, k)
end
end
end
if (IsValid(self.Owner)) then
bone_ent = self.Owner
else
--when the weapon is dropped
bone_ent = self
end
for k, name in pairs( self.wRenderOrder ) do
local v = self.WElements[name]
if (!v) then self.wRenderOrder = nil break end
if (v.hide) then continue end
local pos, ang
if (v.bone) then
pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
else
pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
end
if (!pos) then continue end
local model = v.modelEnt
local sprite = v.spriteMaterial
if (v.type == "Model" and IsValid(model)) then
model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
model:SetAngles(ang)
--model:SetModelScale(v.size)
local matrix = Matrix()
matrix:Scale(v.size)
model:EnableMatrix( "RenderMultiply", matrix )
if (v.material == "") then
model:SetMaterial("")
elseif (model:GetMaterial() != v.material) then
model:SetMaterial( v.material )
end
if (v.skin and v.skin != model:GetSkin()) then
model:SetSkin(v.skin)
end
if (v.bodygroup) then
for k, v in pairs( v.bodygroup ) do
if (model:GetBodygroup(k) != v) then
model:SetBodygroup(k, v)
end
end
end
if (v.surpresslightning) then
render.SuppressEngineLighting(true)
end
render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
render.SetBlend(v.color.a/255)
model:DrawModel()
render.SetBlend(1)
render.SetColorModulation(1, 1, 1)
if (v.surpresslightning) then
render.SuppressEngineLighting(false)
end
elseif (v.type == "Sprite" and sprite) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
render.SetMaterial(sprite)
render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
elseif (v.type == "Quad" and v.draw_func) then
local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
cam.Start3D2D(drawpos, ang, v.size)
v.draw_func( self )
cam.End3D2D()
end
end
end
function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
local bone, pos, ang
if (tab.rel and tab.rel != "") then
local v = basetab[tab.rel]
if (!v) then return end
--Technically, if there exists an element with the same name as a bone
--you can get in an infinite loop. Let's just hope nobody's that stupid.
pos, ang = self:GetBoneOrientation( basetab, v, ent )
if (!pos) then return end
pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
ang:RotateAroundAxis(ang:Up(), v.angle.y)
ang:RotateAroundAxis(ang:Right(), v.angle.p)
ang:RotateAroundAxis(ang:Forward(), v.angle.r)
else
bone = ent:LookupBone(bone_override or tab.bone)
if (!bone) then return end
pos, ang = Vector(0,0,0), Angle(0,0,0)
local m = ent:GetBoneMatrix(bone)
if (m) then
pos, ang = m:GetTranslation(), m:GetAngles()
end
if (IsValid(self.Owner) and self.Owner:IsPlayer() and
ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
ang.r = -ang.r --Fixes mirrored models
end
end
return pos, ang
end
function SWEP:CreateModels( tab )
if (!tab) then return end
--Create the clientside models here because Garry says we can't do it in the render hook
for k, v in pairs( tab ) do
if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
if (IsValid(v.modelEnt)) then
v.modelEnt:SetPos(self:GetPos())
v.modelEnt:SetAngles(self:GetAngles())
v.modelEnt:SetParent(self)
v.modelEnt:SetNoDraw(true)
v.createdModel = v.model
else
v.modelEnt = nil
end
elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
local name = v.sprite.."-"
local params = { ["$basetexture"] = v.sprite }
--make sure we create a unique name based on the selected options
local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
for i, j in pairs( tocheck ) do
if (v[j]) then
params["$"..j] = 1
name = name.."1"
else
name = name.."0"
end
end
v.createdSprite = v.sprite
v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
end
end
end
local allbones
local hasGarryFixedBoneScalingYet = false
function SWEP:UpdateBonePositions(vm)
if self.ViewModelBoneMods then
if (!vm:GetBoneCount()) then return end
--!! WORKAROUND !!
--We need to check all model names :/
local loopthrough = self.ViewModelBoneMods
if (!hasGarryFixedBoneScalingYet) then
allbones = {}
for i=0, vm:GetBoneCount() do
local bonename = vm:GetBoneName(i)
if (self.ViewModelBoneMods[bonename]) then
allbones[bonename] = self.ViewModelBoneMods[bonename]
else
allbones[bonename] = {
scale = Vector(1,1,1),
pos = Vector(0,0,0),
angle = Angle(0,0,0)
}
end
end
loopthrough = allbones
end
--!! ----------- !!
for k, v in pairs( loopthrough ) do
local bone = vm:LookupBone(k)
if (!bone) then continue end
--!! WORKAROUND !!
local s = Vector(v.scale.x,v.scale.y,v.scale.z)
local p = Vector(v.pos.x,v.pos.y,v.pos.z)
local ms = Vector(1,1,1)
if (!hasGarryFixedBoneScalingYet) then
local cur = vm:GetBoneParent(bone)
while(cur >= 0) do
local pscale = loopthrough[vm:GetBoneName(cur)].scale
ms = ms * pscale
cur = vm:GetBoneParent(cur)
end
end
s = s * ms
--!! ----------- !!
if vm:GetManipulateBoneScale(bone) != s then
vm:ManipulateBoneScale( bone, s )
end
if vm:GetManipulateBoneAngles(bone) != v.angle then
vm:ManipulateBoneAngles( bone, v.angle )
end
if vm:GetManipulateBonePosition(bone) != p then
vm:ManipulateBonePosition( bone, p )
end
end
else
self:ResetBonePositions(vm)
end
end
function SWEP:ResetBonePositions(vm)
if (!vm:GetBoneCount()) then return end
for i=0, vm:GetBoneCount() do
vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
end
end
--[[
Global utility code
]]--
--Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
--Does not copy entities of course, only copies their reference.
--WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
function table.FullCopy( tab )
if (!tab) then return nil end
local res = {}
for k, v in pairs( tab ) do
if (type(v) == "table") then
res[k] = table.FullCopy(v) --recursion ho!
elseif (type(v) == "Vector") then
res[k] = Vector(v.x, v.y, v.z)
elseif (type(v) == "Angle") then
res[k] = Angle(v.p, v.y, v.r)
else
res[k] = v
end
end
return res
end
end
function SWEP:CanPrimaryAttack() --i think this looks good
if ( self:Clip1() <= 0 ) then self:Reload() return false end
if CurTime() < self.Primary.delay then return false end
if self.Reloading == 1 then return false end
end
function SWEP:CustomNoBulletPrimaryAttack()
end
function SWEP:PrimaryAttack()
if self:CanPrimaryAttack() == false then return end
self:SetClip1( self:Clip1() - self.Primary.takeclip )
self:SendWeaponAnim( self.ViewModelFireAnim )
if self.NoBullet == false then
FireBulletsTABLE = {}
FireBulletsTABLE.Attacker = self:GetOwner()
FireBulletsTABLE.Damage = math.random(self.fireData.dmgMin,self.fireData.dmgMax)
FireBulletsTABLE.Force = self.fireData.force
FireBulletsTABLE.HullSize = 5
FireBulletsTABLE.Spread = Vector(self.fireData.spread,self.fireData.spread,0)
FireBulletsTABLE.AmmoType = self.fireData.ammo
FireBulletsTABLE.Num = self.fireData.num
FireBulletsTABLE.TracerName = self.fireData.tracer
FireBulletsTABLE.Dir = self:GetOwner():GetAimVector()
FireBulletsTABLE.Src = self:GetOwner():GetShootPos()
FireBulletsTABLE.IgnoreEntity = self:GetOwner()
self:FireBullets(FireBulletsTABLE)
else
self:CustomNoBulletPrimaryAttack()
end
self:GetOwner():SetAnimation( PLAYER_ATTACK1)
self.Primary.delay = CurTime() + self.Primary.FireRate
self:SetNextPrimaryFire(CurTime() + self.Primary.FireRate)
self:EmitSound(Sound(self.Primary.Sound))
end
function SWEP:CustomReload()
end
function SWEP:BaseReload()
if self.Reloading == 0 and self.Weapon:Clip1() < self.Primary.ClipSize then
self.Weapon:SendWeaponAnim( self.ViewModelReloadAnim )
self.Owner:SetAnimation( PLAYER_RELOAD )
self.Reloading = 1
self.ReloadingTimer = CurTime() + self.ReloadTime
if self.ReloadSounds != nil then
for i = 1, #self.ReloadSounds do
timer.Simple(self.ReloadSounds[i][1], function() if !IsValid(self) then return end self.Owner:EmitSound(self.ReloadSounds[i][2], 80) end)
end
end
end
end
function SWEP:OnThink()
end
function SWEP:Think()
if self.Reloading == 1 and self.ReloadingTimer <= CurTime() then
self.Weapon:SetClip1( self.Primary.ClipSize )
self.Reloading = 0
end
self:OnThink()
end
function SWEP:Reload()
self:BaseReload()
end