Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Variables that are used on both client and server
- SWEP.Category = ""
- SWEP.Gun = ""
- SWEP.Author = ""
- SWEP.Contact = ""
- SWEP.Purpose = ""
- SWEP.Instructions = ""
- SWEP.MuzzleAttachment = "1" -- Should be "1" for CSS models or "muzzle" for hl2 models
- SWEP.DrawCrosshair = false -- Hell no, crosshairs r 4 nubz!
- SWEP.ViewModelFOV = 65 -- How big the gun will look
- SWEP.ViewModelFlip = true -- True for CSS models, False for HL2 models
- SWEP.Base = "weapon_tttbase"
- SWEP.Spawnable = false
- SWEP.AdminSpawnable = false
- SWEP.Primary.Sound = Sound("") -- Sound of the gun
- SWEP.Primary.Round = ("") -- What kind of bullet?
- SWEP.Primary.Cone = 0.2 -- Accuracy of NPCs
- SWEP.Primary.Recoil = 10
- SWEP.Primary.Damage = 10
- SWEP.Primary.Spread = .01 --define from-the-hip accuracy (1 is terrible, .0001 is exact)
- SWEP.Primary.NumShots = 1
- SWEP.Primary.RPM = 0 -- This is in Rounds Per Minute
- SWEP.Primary.ClipSize = 0 -- Size of a clip
- SWEP.Primary.DefaultClip = 0 -- Default number of bullets in a clip
- SWEP.Primary.KickUp = 0 -- Maximum up recoil (rise)
- SWEP.Primary.KickDown = 0 -- Maximum down recoil (skeet)
- SWEP.Primary.KickHorizontal = 0 -- Maximum side recoil (koolaid)
- SWEP.Primary.Automatic = true -- Automatic/Semi Auto
- SWEP.Primary.Ammo = "none" -- What kind of ammo
- SWEP.Secondary.ClipSize = 0 -- Size of a clip
- SWEP.Secondary.DefaultClip = 0 -- Default number of bullets in a clip
- SWEP.Secondary.Automatic = false -- Automatic/Semi Auto
- SWEP.Secondary.Ammo = "none"
- SWEP.Secondary.IronFOV = 0 -- How much you 'zoom' in. Less is more!
- SWEP.Penetration = false
- SWEP.Ricochet = false
- SWEP.MaxRicochet = 1
- SWEP.RicochetCoin = 1
- SWEP.ShellTime = .35
- SWEP.Tracer = 0
- SWEP.CanBeSilenced = false
- SWEP.Silenced = false
- SWEP.NextSilence = 0
- SWEP.NextFireSelect = 0
- SWEP.Primary.RPM = 900
- SWEP.IronSightsPos = Vector (2.4537, 1.0923, 0.2696)
- SWEP.IronSightsAng = Vector (0.0186, -0.0547, 0)
- SWEP.VElements = {}
- SWEP.WElements = {}
- function SWEP:Initialize()
- self.Reloadaftershoot = 0 -- Can't reload when firing
- self:SetWeaponHoldType(self.HoldType)
- if CLIENT and self.Weapon:Clip1() == -1 then
- self.Weapon:SetClip1(self.Primary.DefaultClip)
- elseif SERVER then
- self.fingerprints = {}
- self:SetIronsights(false)
- end
- self:SetDeploySpeed(self.DeploySpeed)
- 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) and self.Owner:IsPlayer() then
- if self.Owner:Alive() 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
- -- // 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
- end
- function SWEP:Deploy()
- self:SetIronsights(false) -- Set the ironsight false
- self:SetWeaponHoldType(self.HoldType)
- self.Weapon:SetNetworkedBool("Reloading", false)
- return true
- end
- function SWEP:Holster()
- if CLIENT and IsValid(self.Owner) and not self.Owner:IsNPC() then
- local vm = self.Owner:GetViewModel()
- if IsValid(vm) then
- self:ResetBonePositions(vm)
- end
- end
- return true
- end
- function SWEP:OnRemove()
- if CLIENT and IsValid(self.Owner) and not self.Owner:IsNPC() then
- local vm = self.Owner:GetViewModel()
- if IsValid(vm) then
- self:ResetBonePositions(vm)
- end
- end
- end
- function SWEP:GetCapabilities()
- return CAP_WEAPON_RANGE_ATTACK1, CAP_INNATE_RANGE_ATTACK1
- end
- function SWEP:Precache()
- util.PrecacheSound(self.Primary.Sound)
- util.PrecacheModel(self.ViewModel)
- util.PrecacheModel(self.WorldModel)
- end
- function SWEP:PrimaryAttack()
- if self:CanPrimaryAttack() and self.Owner:IsPlayer() then
- self.Owner:ViewPunch( Angle( math.Rand(self.Primary.RecoilXRand,-self.Primary.RecoilXRand) * self.Primary.Recoil, math.Rand(self.Primary.RecoilYRand,-self.Primary.RecoilYRand) *self.Primary.Recoil, 0 ) )
- if !self.Owner:KeyDown(IN_SPEED) and !self.Owner:KeyDown(IN_RELOAD) then
- self:ShootBulletInformation()
- self.Weapon:TakePrimaryAmmo(1)
- local fx = EffectData()
- fx:SetEntity(self.Weapon)
- fx:SetOrigin(self.Owner:GetShootPos())
- fx:SetNormal(self.Owner:GetAimVector())
- fx:SetAttachment(self.MuzzleAttachment)
- if GetConVar("M9KGasEffect") != nil then
- if GetConVar("M9KGasEffect"):GetBool() then
- util.Effect("m9k_rg_muzzle_rifle",fx)
- end
- end
- self.Owner:SetAnimation( PLAYER_ATTACK1 )
- self.Owner:MuzzleFlash()
- self.Weapon:SetNextPrimaryFire(CurTime()+1/(self.Primary.RPM/60))
- self.RicochetCoin = (math.random(1,4))
- if self.BoltAction then self:BoltBack() end
- end
- elseif self:CanPrimaryAttack() and self.Owner:IsNPC() then
- self:ShootBulletInformation()
- self.Weapon:TakePrimaryAmmo(1)
- self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
- self.Weapon:EmitSound(self.Primary.Sound)
- self.Owner:SetAnimation( PLAYER_ATTACK1 )
- self.Owner:MuzzleFlash()
- self.Weapon:SetNextPrimaryFire(CurTime()+1/(self.Primary.RPM/60))
- self.RicochetCoin = (math.random(1,4))
- end
- end
- /*---------------------------------------------------------
- Name: SWEP:ShootBulletInformation()
- Desc: This func add the damage, the recoil, the number of shots and the cone on the bullet.
- ---------------------------------------------------------*/
- function SWEP:ShootBulletInformation()
- local CurrentDamage
- local CurrentRecoil
- local CurrentCone
- if (self:GetIronsights() == true) and self.Owner:KeyDown(IN_ATTACK2) then
- CurrentCone = self.Primary.IronAccuracy
- else
- CurrentCone = self.Primary.Spread
- end
- local damagedice = math.Rand(.85,1.3)
- CurrentDamage = self.Primary.Damage * damagedice
- CurrentRecoil = self.Primary.Recoil
- -- Player is aiming
- if (self:GetIronsights() == true) and self.Owner:KeyDown(IN_ATTACK2) then
- self:ShootBullet(CurrentDamage, CurrentRecoil * 0.6, self.Primary.NumShots, CurrentCone)
- -- Player is not aiming
- else
- self:ShootBullet(CurrentDamage, CurrentRecoil, self.Primary.NumShots, CurrentCone)
- end
- end
- function SWEP:SecondaryAttack()
- if self.NoSights or (not self.IronSightsPos) then return end
- --if self:GetNextSecondaryFire() > CurTime() then return end
- self:SetIronsights(not self:GetIronsights())
- self:SetNextSecondaryFire(CurTime() + 0.3)
- end
- function SWEP:Reload()
- self.Weapon:DefaultReload(self.ReloadAnim)
- self:SetIronsights( false )
- self.Owner:SetFOV( 0, 0.3 )
- end
- /*---------------------------------------------------------
- IronSight
- ---------------------------------------------------------*/
- function SWEP:IronSight()
- if !self.Owner:IsNPC() then
- if self.ResetSights and CurTime() >= self.ResetSights then
- self.ResetSights = nil
- if self.Silenced then
- self:SendWeaponAnim(ACT_VM_IDLE_SILENCED)
- else
- self:SendWeaponAnim(ACT_VM_IDLE)
- end
- end end
- if self.CanBeSilenced and self.NextSilence < CurTime() then
- if self.Owner:KeyDown(IN_USE) and self.Owner:KeyPressed(IN_ATTACK2) then
- self:Silencer()
- end
- end
- if self.Owner:KeyDown(IN_ATTACK2) and not (self.Weapon:GetNWBool("Reloading")) and (not self.NoSights) then
- self:SetIronsights(true)
- self.Owner:SetFOV( self.Secondary.IronFOV, 0.3 )
- self.DrawCrosshair = false
- end
- if self.Owner:KeyReleased (IN_ATTACK2) then -- If you release then
- self:SetIronsights(false) -- Set the ironsight true
- self.Owner:SetFOV( 0, 0.3 )
- self.DrawCrosshair = false
- end
- end
- /*---------------------------------------------------------
- Think
- ---------------------------------------------------------*/
- function SWEP:Think()
- self:IronSight()
- end
- /*---------------------------------------------------------
- GetViewModelPosition
- ---------------------------------------------------------*/
- local IRONSIGHT_TIME = 0.3
- -- Time to enter in the ironsight mod
- /*---------------------------------------------------------
- SetIronsights
- ---------------------------------------------------------*/
- function SWEP:SetIronsights(b)
- self.Weapon:SetNetworkedBool("Ironsights", b)
- end
- function SWEP:GetIronsights()
- return self.Weapon:GetNWBool("Ironsights")
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement