· 10 years ago · Sep 04, 2016, 02:36 PM
1SWEP.Spawnable = true
2SWEP.AdminOnly = true
3
4SWEP.BounceWeaponIcon = true
5
6SWEP.Author = ""
7SWEP.Instructions = ""
8SWEP.Purpose = "Ban bad things"
9SWEP.Contact = ""
10
11if CLIENT then
12
13 SWEP.PrintName = "Ban-Hammer"
14 SWEP.HoldType = "grenade"
15 SWEP.ViewModelFOV = 69
16 SWEP.Slot = 1
17 SWEP.SlotPos = 5
18 SWEP.DrawCrosshair = true
19 SWEP.UseHands = true
20
21 SWEP.WepSelectIcon = surface.GetTextureID( "weapons/banhammer" )
22
23 killicon.Add( "banhammer", "hammer/hammer", color_white )
24
25 SWEP.ViewModelBoneMods = {}
26
27 SWEP.VElements = {
28 ["element_name"] = { type = "Model", model = "models/banhammer/banhammer.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(3, 1.2, 6), angle = Angle(3.506, -8.183, 178.83), size = Vector(0.755, 0.755, 0.755), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
29}
30
31 SWEP.WElements = {
32 ["hammer"] = { type = "Model", model = "models/banhammer/banhammer.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.273, 1.363, 3.181), angle = Angle(0, -113.524, -164.659), size = Vector(0.82, 0.82, 0.82), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
33}
34end
35
36SWEP.Category = "Dr.Hax"
37
38SWEP.ViewModel = "models/weapons/c_stunstick.mdl"
39SWEP.WorldModel = "models/weapons/w_crowbar.mdl"
40SWEP.ShowWorldModel = false
41
42SWEP.Primary.Delay = 0.9
43SWEP.Primary.Recoil = 0
44SWEP.Primary.Damage = 0
45SWEP.Primary.Force = 0
46SWEP.Primary.NumShots = 5
47SWEP.Primary.Cone = 0.02
48SWEP.Primary.ClipSize = -1
49SWEP.Primary.DefaultClip = -1
50SWEP.Primary.Automatic = true
51SWEP.Primary.Ammo = "none"
52
53SWEP.Secondary.Delay = 0.5
54SWEP.Secondary.Recoil = 0
55SWEP.Secondary.Damage = 0
56SWEP.Secondary.NumShots = 0
57SWEP.Secondary.Cone = 0
58SWEP.Secondary.ClipSize = -1
59SWEP.Secondary.DefaultClip = -1
60SWEP.Secondary.Automatic = false
61SWEP.Secondary.Ammo = "none"
62
63SWEP.IronSightsPos = Vector(0, 0, 0)
64SWEP.IronSightsAng = Vector(0, 0, 0)
65
66function SWEP:PrimaryAttack()
67
68self.Weapon:EmitSound("weapons/hammer/swing" .. math.random( 1, 2 ) .. ".wav")
69self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
70
71self.Owner:ViewPunch(Angle( -2, -3, 0 ))
72
73local trace = {}
74 trace.start = self.Owner:GetShootPos()
75 trace.endpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 24^14
76 trace.filter = self.Owner
77 local tr = util.TraceLine(trace)
78
79 tr.Entity:SetKeyValue("targetname", "disTarg")
80 local dis = ents.Create("env_entity_dissolver")
81 dis:SetKeyValue("magnitude", "5")
82 dis:SetKeyValue("dissolvetype", "3")
83 dis:SetKeyValue("target", "disTarg")
84 dis:Spawn()
85 dis:Fire("Dissolve", "disTarg", 0)
86 dis:Fire("kill", "", 0)
87
88if tr.HitPos:Distance(self.Owner:GetShootPos()) <= 95 then
89 bullet = {}
90 bullet.Num = 1
91 bullet.Src = self.Owner:GetShootPos()
92 bullet.Dir = self.Owner:GetAimVector()
93 bullet.Spread = Vector(0, 0, 0) --0,0,0
94 bullet.Tracer = 1
95 bullet.Force = 0
96 bullet.Damage = 1
97 bullet.AmmoType = "357"
98local hit1, hit2 = tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal
99self.Owner:SetAnimation( PLAYER_ATTACK1 );
100 local randomsounds = {
101 "weapons/hammer/hammer_impact1.wav",
102 "weapons/hammer/hammer_impact2.wav",
103 "weapons/hammer/hammer_impact3.wav",
104 "weapons/hammer/hammer_impact4.wav",
105 "weapons/hammer/hammer_impact5.wav",
106 "weapons/hammer/hammer_impact6.wav",
107}
108 local random = math.random(1, #randomsounds)
109 self.Owner:EmitSound(randomsounds[random])
110 util.ScreenShake( Vector(0,0,0), 5, 5, 0.6, 5000 )
111 self.Owner:ViewPunch(Angle( -5, -3, 4 ))
112
113 local effect = EffectData()
114 effect:SetOrigin(tr.HitPos)
115 effect:SetNormal(tr.HitNormal)
116 effect:SetScale(10)
117 util.Effect("StunstickImpact", effect)
118 self:ShootEffects()
119 self.Owner:FireBullets( bullet )
120
121 self.Owner:FireBullets(bullet)
122 self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
123 self.Owner:SetAnimation( PLAYER_ATTACK1 )
124else
125 self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
126 self.Owner:SetAnimation( PLAYER_ATTACK1 )
127end
128
129end
130
131function SWEP:SecondaryAttack()
132
133self.Weapon:SetNextSecondaryFire(CurTime() + self.Secondary.Delay)
134self.Owner:EmitSound("weapons/hammer/hammer_impact_ban1.wav")
135
136end
137
138function SWEP:Reload()
139end
140
141function SWEP:Deploy()
142 self.Owner:EmitSound("weapons/hammer/hammer_impact_special"..math.random(1,2)..".wav" )
143 return true
144end
145
146function SWEP:Initialize()
147
148 // other initialize code goes here
149
150 if CLIENT then
151
152 // Create a new table for every weapon instance
153 self.VElements = table.FullCopy( self.VElements )
154 self.WElements = table.FullCopy( self.WElements )
155 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
156 self:SetWeaponHoldType( self.HoldType )
157
158 self:CreateModels(self.VElements) // create viewmodels
159 self:CreateModels(self.WElements) // create worldmodels
160
161 // init view model bone build function
162 if IsValid(self.Owner) then
163 local vm = self.Owner:GetViewModel()
164 if IsValid(vm) then
165 self:ResetBonePositions(vm)
166
167 // Init viewmodel visibility
168 if (self.ShowViewModel == nil or self.ShowViewModel) then
169 vm:SetColor(Color(255,255,255,255))
170 else
171 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
172 vm:SetColor(Color(255,255,255,1))
173 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
174 // 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
175 vm:SetMaterial("Debug/hsv")
176 end
177 end
178 end
179
180 end
181
182end
183
184function SWEP:Holster()
185
186 if CLIENT and IsValid(self.Owner) then
187 local vm = self.Owner:GetViewModel()
188 if IsValid(vm) then
189 self:ResetBonePositions(vm)
190 end
191 end
192
193 return true
194end
195
196function SWEP:OnRemove()
197 self:Holster()
198end
199
200if CLIENT then
201
202 SWEP.vRenderOrder = nil
203 function SWEP:ViewModelDrawn()
204
205 local vm = self.Owner:GetViewModel()
206 if !IsValid(vm) then return end
207
208 if (!self.VElements) then return end
209
210 self:UpdateBonePositions(vm)
211
212 if (!self.vRenderOrder) then
213
214 // we build a render order because sprites need to be drawn after models
215 self.vRenderOrder = {}
216
217 for k, v in pairs( self.VElements ) do
218 if (v.type == "Model") then
219 table.insert(self.vRenderOrder, 1, k)
220 elseif (v.type == "Sprite" or v.type == "Quad") then
221 table.insert(self.vRenderOrder, k)
222 end
223 end
224
225 end
226
227 for k, name in ipairs( self.vRenderOrder ) do
228
229 local v = self.VElements[name]
230 if (!v) then self.vRenderOrder = nil break end
231 if (v.hide) then continue end
232
233 local model = v.modelEnt
234 local sprite = v.spriteMaterial
235
236 if (!v.bone) then continue end
237
238 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
239
240 if (!pos) then continue end
241
242 if (v.type == "Model" and IsValid(model)) then
243
244 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
245 ang:RotateAroundAxis(ang:Up(), v.angle.y)
246 ang:RotateAroundAxis(ang:Right(), v.angle.p)
247 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
248
249 model:SetAngles(ang)
250 //model:SetModelScale(v.size)
251 local matrix = Matrix()
252 matrix:Scale(v.size)
253 model:EnableMatrix( "RenderMultiply", matrix )
254
255 if (v.material == "") then
256 model:SetMaterial("")
257 elseif (model:GetMaterial() != v.material) then
258 model:SetMaterial( v.material )
259 end
260
261 if (v.skin and v.skin != model:GetSkin()) then
262 model:SetSkin(v.skin)
263 end
264
265 if (v.bodygroup) then
266 for k, v in pairs( v.bodygroup ) do
267 if (model:GetBodygroup(k) != v) then
268 model:SetBodygroup(k, v)
269 end
270 end
271 end
272
273 if (v.surpresslightning) then
274 render.SuppressEngineLighting(true)
275 end
276
277 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
278 render.SetBlend(v.color.a/255)
279 model:DrawModel()
280 render.SetBlend(1)
281 render.SetColorModulation(1, 1, 1)
282
283 if (v.surpresslightning) then
284 render.SuppressEngineLighting(false)
285 end
286
287 elseif (v.type == "Sprite" and sprite) then
288
289 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
290 render.SetMaterial(sprite)
291 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
292
293 elseif (v.type == "Quad" and v.draw_func) then
294
295 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
296 ang:RotateAroundAxis(ang:Up(), v.angle.y)
297 ang:RotateAroundAxis(ang:Right(), v.angle.p)
298 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
299
300 cam.Start3D2D(drawpos, ang, v.size)
301 v.draw_func( self )
302 cam.End3D2D()
303
304 end
305
306 end
307
308 end
309
310 SWEP.wRenderOrder = nil
311 function SWEP:DrawWorldModel()
312
313 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
314 self:DrawModel()
315 end
316
317 if (!self.WElements) then return end
318
319 if (!self.wRenderOrder) then
320
321 self.wRenderOrder = {}
322
323 for k, v in pairs( self.WElements ) do
324 if (v.type == "Model") then
325 table.insert(self.wRenderOrder, 1, k)
326 elseif (v.type == "Sprite" or v.type == "Quad") then
327 table.insert(self.wRenderOrder, k)
328 end
329 end
330
331 end
332
333 if (IsValid(self.Owner)) then
334 bone_ent = self.Owner
335 else
336 // when the weapon is dropped
337 bone_ent = self
338 end
339
340 for k, name in pairs( self.wRenderOrder ) do
341
342 local v = self.WElements[name]
343 if (!v) then self.wRenderOrder = nil break end
344 if (v.hide) then continue end
345
346 local pos, ang
347
348 if (v.bone) then
349 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
350 else
351 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
352 end
353
354 if (!pos) then continue end
355
356 local model = v.modelEnt
357 local sprite = v.spriteMaterial
358
359 if (v.type == "Model" and IsValid(model)) then
360
361 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
362 ang:RotateAroundAxis(ang:Up(), v.angle.y)
363 ang:RotateAroundAxis(ang:Right(), v.angle.p)
364 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
365
366 model:SetAngles(ang)
367 //model:SetModelScale(v.size)
368 local matrix = Matrix()
369 matrix:Scale(v.size)
370 model:EnableMatrix( "RenderMultiply", matrix )
371
372 if (v.material == "") then
373 model:SetMaterial("")
374 elseif (model:GetMaterial() != v.material) then
375 model:SetMaterial( v.material )
376 end
377
378 if (v.skin and v.skin != model:GetSkin()) then
379 model:SetSkin(v.skin)
380 end
381
382 if (v.bodygroup) then
383 for k, v in pairs( v.bodygroup ) do
384 if (model:GetBodygroup(k) != v) then
385 model:SetBodygroup(k, v)
386 end
387 end
388 end
389
390 if (v.surpresslightning) then
391 render.SuppressEngineLighting(true)
392 end
393
394 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
395 render.SetBlend(v.color.a/255)
396 model:DrawModel()
397 render.SetBlend(1)
398 render.SetColorModulation(1, 1, 1)
399
400 if (v.surpresslightning) then
401 render.SuppressEngineLighting(false)
402 end
403
404 elseif (v.type == "Sprite" and sprite) then
405
406 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
407 render.SetMaterial(sprite)
408 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
409
410 elseif (v.type == "Quad" and v.draw_func) then
411
412 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
413 ang:RotateAroundAxis(ang:Up(), v.angle.y)
414 ang:RotateAroundAxis(ang:Right(), v.angle.p)
415 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
416
417 cam.Start3D2D(drawpos, ang, v.size)
418 v.draw_func( self )
419 cam.End3D2D()
420
421 end
422
423 end
424
425 end
426
427 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
428
429 local bone, pos, ang
430 if (tab.rel and tab.rel != "") then
431
432 local v = basetab[tab.rel]
433
434 if (!v) then return end
435
436 // Technically, if there exists an element with the same name as a bone
437 // you can get in an infinite loop. Let's just hope nobody's that stupid.
438 pos, ang = self:GetBoneOrientation( basetab, v, ent )
439
440 if (!pos) then return end
441
442 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
443 ang:RotateAroundAxis(ang:Up(), v.angle.y)
444 ang:RotateAroundAxis(ang:Right(), v.angle.p)
445 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
446
447 else
448
449 bone = ent:LookupBone(bone_override or tab.bone)
450
451 if (!bone) then return end
452
453 pos, ang = Vector(0,0,0), Angle(0,0,0)
454 local m = ent:GetBoneMatrix(bone)
455 if (m) then
456 pos, ang = m:GetTranslation(), m:GetAngles()
457 end
458
459 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
460 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
461 ang.r = -ang.r // Fixes mirrored models
462 end
463
464 end
465
466 return pos, ang
467 end
468
469 function SWEP:CreateModels( tab )
470
471 if (!tab) then return end
472
473 // Create the clientside models here because Garry says we can't do it in the render hook
474 for k, v in pairs( tab ) do
475 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
476 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
477
478 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
479 if (IsValid(v.modelEnt)) then
480 v.modelEnt:SetPos(self:GetPos())
481 v.modelEnt:SetAngles(self:GetAngles())
482 v.modelEnt:SetParent(self)
483 v.modelEnt:SetNoDraw(true)
484 v.createdModel = v.model
485 else
486 v.modelEnt = nil
487 end
488
489 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
490 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
491
492 local name = v.sprite.."-"
493 local params = { ["$basetexture"] = v.sprite }
494 // make sure we create a unique name based on the selected options
495 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
496 for i, j in pairs( tocheck ) do
497 if (v[j]) then
498 params["$"..j] = 1
499 name = name.."1"
500 else
501 name = name.."0"
502 end
503 end
504
505 v.createdSprite = v.sprite
506 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
507
508 end
509 end
510
511 end
512
513 local allbones
514 local hasGarryFixedBoneScalingYet = false
515
516 function SWEP:UpdateBonePositions(vm)
517
518 if self.ViewModelBoneMods then
519
520 if (!vm:GetBoneCount()) then return end
521
522 // !! WORKAROUND !! //
523 // We need to check all model names :/
524 local loopthrough = self.ViewModelBoneMods
525 if (!hasGarryFixedBoneScalingYet) then
526 allbones = {}
527 for i=0, vm:GetBoneCount() do
528 local bonename = vm:GetBoneName(i)
529 if (self.ViewModelBoneMods[bonename]) then
530 allbones[bonename] = self.ViewModelBoneMods[bonename]
531 else
532 allbones[bonename] = {
533 scale = Vector(1,1,1),
534 pos = Vector(0,0,0),
535 angle = Angle(0,0,0)
536 }
537 end
538 end
539
540 loopthrough = allbones
541 end
542 // !! ----------- !! //
543
544 for k, v in pairs( loopthrough ) do
545 local bone = vm:LookupBone(k)
546 if (!bone) then continue end
547
548 // !! WORKAROUND !! //
549 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
550 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
551 local ms = Vector(1,1,1)
552 if (!hasGarryFixedBoneScalingYet) then
553 local cur = vm:GetBoneParent(bone)
554 while(cur >= 0) do
555 local pscale = loopthrough[vm:GetBoneName(cur)].scale
556 ms = ms * pscale
557 cur = vm:GetBoneParent(cur)
558 end
559 end
560
561 s = s * ms
562 // !! ----------- !! //
563
564 if vm:GetManipulateBoneScale(bone) != s then
565 vm:ManipulateBoneScale( bone, s )
566 end
567 if vm:GetManipulateBoneAngles(bone) != v.angle then
568 vm:ManipulateBoneAngles( bone, v.angle )
569 end
570 if vm:GetManipulateBonePosition(bone) != p then
571 vm:ManipulateBonePosition( bone, p )
572 end
573 end
574 else
575 self:ResetBonePositions(vm)
576 end
577
578 end
579
580 function SWEP:ResetBonePositions(vm)
581
582 if (!vm:GetBoneCount()) then return end
583 for i=0, vm:GetBoneCount() do
584 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
585 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
586 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
587 end
588
589 end
590
591 /**************************
592 Global utility code
593 **************************/
594
595 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
596 // Does not copy entities of course, only copies their reference.
597 // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
598 function table.FullCopy( tab )
599
600 if (!tab) then return nil end
601
602 local res = {}
603 for k, v in pairs( tab ) do
604 if (type(v) == "table") then
605 res[k] = table.FullCopy(v) // recursion ho!
606 elseif (type(v) == "Vector") then
607 res[k] = Vector(v.x, v.y, v.z)
608 elseif (type(v) == "Angle") then
609 res[k] = Angle(v.p, v.y, v.r)
610 else
611 res[k] = v
612 end
613 end
614
615 return res
616
617 end
618
619end