· 8 years ago · Apr 07, 2018, 08:36 PM
1SKYRIZE_TAZER = {}
2SKYRIZE_TAZER.NextFire = 5
3SKYRIZE_TAZER.KnockoutTime = 5
4SKYRIZE_TAZER.FreezeTime = 3
5
6
7SWEP.PrintName = "Tazer"
8SWEP.Slot = 1
9SWEP.SlotPos = 1
10SWEP.DrawAmmo = true
11SWEP.DrawCrosshair = true
12SWEP.Author = "Uranium"
13SWEP.Contact = ""
14SWEP.Purpose = ""
15SWEP.Instructions = ""
16SWEP.Category = "Uranium"
17
18SWEP.Spawnable = true -- Whether regular players can see it
19SWEP.AdminSpawnable = true -- Whether Admins/Super Admins can see it
20
21SWEP.Primary.ClipSize = -1
22SWEP.Primary.DefaultClip = -1
23SWEP.Primary.Automatic = false
24SWEP.Primary.Ammo = "none"
25
26SWEP.Secondary.ClipSize = -1
27SWEP.Secondary.DefaultClip = -1
28SWEP.Secondary.Automatic = false
29SWEP.Secondary.Ammo = "none"
30
31SWEP.HoldType = "pistol"
32SWEP.UseHands = true
33SWEP.ShowWorldModel = false
34SWEP.ShowViewModel = false
35SWEP.VElements = {
36 ["element_nameg"] = { type = "Quad", bone = "ValveBiped.Bip01", rel = "element_name", pos = Vector(-0.134, -5.737, 0.837), angle = Angle(180, 0, -90), size = 0.05, draw_func = function(self) end},
37 ["element_name"] = { type = "Model", model = "models/codeandmodeling_antho_taser/codeandmodeling_antho_taser.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(7.269, 1.25, -1.843), angle = Angle(180, 90, 0), size = Vector(1, 1, 1), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} },
38}
39
40SWEP.WElements = {
41 ["element_name_wolrd"] = { type = "Model", model = "models/codeandmodeling_antho_taser/codeandmodeling_antho_taser.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(7.706, 1.675, -2.254), angle = Angle(180, 84.375, -9.917), size = Vector(1.08, 1.08, 1.08), color = Color(255, 255, 255, 255), surpresslightning = false, material = "", skin = 0, bodygroup = {} }
42}
43
44function SWEP:DrawHUD()
45end
46
47
48function SWEP:PostDrawViewModel(vm, wep, ply)
49
50 // init view model bone build function
51 if IsValid(self.Owner) then
52 local vm = self.Owner:GetViewModel()
53 if IsValid(vm) then
54 self:ResetBonePositions(vm)
55
56 // Init viewmodel visibility
57 if (self.ShowViewModel == nil or self.ShowViewModel) then
58 vm:SetColor(Color(255,255,255,255))
59 else
60 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
61 vm:SetColor(Color(255,255,255,1))
62 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
63 // 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
64 vm:SetMaterial("models/effects/vol_light001")
65 vm:SetColor(Color(0,0,0,1))
66 end
67 end
68 end
69
70end
71
72/********************************************************
73 SWEP Construction Kit base code
74 Created by Clavus
75 Available for public use, thread at:
76 facepunch.com/threads/1032378
77
78
79 DESCRIPTION:
80 This script is meant for experienced scripters
81 that KNOW WHAT THEY ARE DOING. Dont come to me
82 with basic Lua questions.
83
84 Just copy into your SWEP or SWEP base of choice
85 and merge with your own code.
86
87 The SWEP.VElements, SWEP.WElements and
88 SWEP.ViewModelBoneMods tables are all optional
89 and only have to be visible to the client.
90********************************************************/
91
92function SWEP:Initialize()
93
94 // other initialize code goes here
95
96 if CLIENT then
97
98 // Create a new table for every weapon instance
99 self.VElements = table.FullCopy( self.VElements )
100 self.WElements = table.FullCopy( self.WElements )
101 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
102
103 self:CreateModels(self.VElements) // create viewmodels
104 self:CreateModels(self.WElements) // create worldmodels
105
106 // init view model bone build function
107 if IsValid(self.Owner) then
108 local vm = self.Owner:GetViewModel()
109 if IsValid(vm) then
110 self:ResetBonePositions(vm)
111
112 // Init viewmodel visibility
113 if (self.ShowViewModel == nil or self.ShowViewModel) then
114 vm:SetColor(Color(255,255,255,255))
115 else
116 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
117 vm:SetColor(Color(255,255,255,1))
118 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
119 // 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
120 vm:SetMaterial("models/effects/vol_light001")
121 end
122 end
123 end
124
125 end
126
127end
128
129function SWEP:Holster()
130
131 if CLIENT and IsValid(self.Owner) then
132 local vm = self.Owner:GetViewModel()
133 if IsValid(vm) then
134 self:ResetBonePositions(vm)
135 vm:SetMaterial("")
136 vm:SetColor(Color(255,255,255,255))
137 end
138 end
139
140 return true
141end
142
143function SWEP:OnRemove()
144 self:Holster()
145end
146
147if CLIENT then
148
149 SWEP.vRenderOrder = nil
150 function SWEP:ViewModelDrawn()
151
152 local vm = self.Owner:GetViewModel()
153 if !IsValid(vm) then return end
154
155 if (!self.VElements) then return end
156
157 self:UpdateBonePositions(vm)
158
159 if (!self.vRenderOrder) then
160
161 // we build a render order because sprites need to be drawn after models
162 self.vRenderOrder = {}
163
164 for k, v in pairs( self.VElements ) do
165 if (v.type == "Model") then
166 table.insert(self.vRenderOrder, 1, k)
167 elseif (v.type == "Sprite" or v.type == "Quad") then
168 table.insert(self.vRenderOrder, k)
169 end
170 end
171
172 end
173
174 for k, name in ipairs( self.vRenderOrder ) do
175
176 local v = self.VElements[name]
177 if (!v) then self.vRenderOrder = nil break end
178 if (v.hide) then continue end
179
180 local model = v.modelEnt
181 local sprite = v.spriteMaterial
182
183 if (!v.bone) then continue end
184
185 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
186
187 if (!pos) then continue end
188
189 if (v.type == "Model" and IsValid(model)) then
190
191 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
192 ang:RotateAroundAxis(ang:Up(), v.angle.y)
193 ang:RotateAroundAxis(ang:Right(), v.angle.p)
194 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
195
196 model:SetAngles(ang)
197 //model:SetModelScale(v.size)
198 local matrix = Matrix()
199 matrix:Scale(v.size)
200 model:EnableMatrix( "RenderMultiply", matrix )
201
202 if (v.material == "") then
203 model:SetMaterial("")
204 elseif (model:GetMaterial() != v.material) then
205 model:SetMaterial( v.material )
206 end
207
208 if (v.skin and v.skin != model:GetSkin()) then
209 model:SetSkin(v.skin)
210 end
211
212 if (v.bodygroup) then
213 for k, v in pairs( v.bodygroup ) do
214 if (model:GetBodygroup(k) != v) then
215 model:SetBodygroup(k, v)
216 end
217 end
218 end
219
220 if (v.surpresslightning) then
221 render.SuppressEngineLighting(true)
222 end
223
224 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
225 render.SetBlend(v.color.a/255)
226 model:DrawModel()
227 render.SetBlend(1)
228 render.SetColorModulation(1, 1, 1)
229
230 if (v.surpresslightning) then
231 render.SuppressEngineLighting(false)
232 end
233
234 elseif (v.type == "Sprite" and sprite) then
235
236 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
237 render.SetMaterial(sprite)
238 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
239
240 elseif (v.type == "Quad" and v.draw_func) then
241
242 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
243 ang:RotateAroundAxis(ang:Up(), v.angle.y)
244 ang:RotateAroundAxis(ang:Right(), v.angle.p)
245 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
246
247
248 if !self.nextfire then self.nextfire = CurTime() end
249 local _x,_y = 35,10
250 local __x,__y = -21,-18
251 local seconde = self.nextfire-CurTime()
252 local width = math.Clamp(math.abs(seconde-SKYRIZE_TAZER.NextFire)*_x/5, 0,_x)
253 cam.Start3D2D(drawpos, ang, v.size)
254 v.draw_func( self )
255 draw.RoundedBox(0,__x,__y,_x,_y,Color(25,25,25,150))
256 if width < _x then
257 draw.RoundedBox(0,__x,__y,width,_y,Color(127,0,0))
258 else
259 draw.RoundedBox(0,__x,__y,width,_y,Color(40,200,20))
260 end
261
262 cam.End3D2D()
263
264
265 end
266
267 end
268
269 end
270
271 SWEP.wRenderOrder = nil
272 function SWEP:DrawWorldModel()
273
274 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
275 self:DrawModel()
276 end
277
278 if (!self.WElements) then return end
279
280 if (!self.wRenderOrder) then
281
282 self.wRenderOrder = {}
283
284 for k, v in pairs( self.WElements ) do
285 if (v.type == "Model") then
286 table.insert(self.wRenderOrder, 1, k)
287 elseif (v.type == "Sprite" or v.type == "Quad") then
288 table.insert(self.wRenderOrder, k)
289 end
290 end
291
292 end
293
294 if (IsValid(self.Owner)) then
295 bone_ent = self.Owner
296 else
297 // when the weapon is dropped
298 bone_ent = self
299 end
300
301 for k, name in pairs( self.wRenderOrder ) do
302
303 local v = self.WElements[name]
304 if (!v) then self.wRenderOrder = nil break end
305 if (v.hide) then continue end
306
307 local pos, ang
308
309 if (v.bone) then
310 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
311 else
312 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
313 end
314
315 if (!pos) then continue end
316
317 local model = v.modelEnt
318 local sprite = v.spriteMaterial
319
320 if (v.type == "Model" and IsValid(model)) then
321
322 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
323 ang:RotateAroundAxis(ang:Up(), v.angle.y)
324 ang:RotateAroundAxis(ang:Right(), v.angle.p)
325 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
326
327 model:SetAngles(ang)
328 //model:SetModelScale(v.size)
329 local matrix = Matrix()
330 matrix:Scale(v.size)
331 model:EnableMatrix( "RenderMultiply", matrix )
332
333 if (v.material == "") then
334 model:SetMaterial("")
335 elseif (model:GetMaterial() != v.material) then
336 model:SetMaterial( v.material )
337 end
338
339 if (v.skin and v.skin != model:GetSkin()) then
340 model:SetSkin(v.skin)
341 end
342
343 if (v.bodygroup) then
344 for k, v in pairs( v.bodygroup ) do
345 if (model:GetBodygroup(k) != v) then
346 model:SetBodygroup(k, v)
347 end
348 end
349 end
350
351 if (v.surpresslightning) then
352 render.SuppressEngineLighting(true)
353 end
354
355 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
356 render.SetBlend(v.color.a/255)
357 model:DrawModel()
358 render.SetBlend(1)
359 render.SetColorModulation(1, 1, 1)
360
361 if (v.surpresslightning) then
362 render.SuppressEngineLighting(false)
363 end
364
365 elseif (v.type == "Sprite" and sprite) then
366
367 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
368 render.SetMaterial(sprite)
369 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
370
371 elseif (v.type == "Quad" and v.draw_func) then
372
373 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
374 ang:RotateAroundAxis(ang:Up(), v.angle.y)
375 ang:RotateAroundAxis(ang:Right(), v.angle.p)
376 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
377
378 cam.Start3D2D(drawpos, ang, v.size)
379 v.draw_func( self )
380 cam.End3D2D()
381
382 end
383
384 end
385
386 end
387
388 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
389
390 local bone, pos, ang
391 if (tab.rel and tab.rel != "") then
392
393 local v = basetab[tab.rel]
394
395 if (!v) then return end
396
397 // Technically, if there exists an element with the same name as a bone
398 // you can get in an infinite loop. Let's just hope nobody's that stupid.
399 pos, ang = self:GetBoneOrientation( basetab, v, ent )
400
401 if (!pos) then return end
402
403 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
404 ang:RotateAroundAxis(ang:Up(), v.angle.y)
405 ang:RotateAroundAxis(ang:Right(), v.angle.p)
406 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
407
408 else
409
410 bone = ent:LookupBone(bone_override or tab.bone)
411
412 if (!bone) then return end
413
414 pos, ang = Vector(0,0,0), Angle(0,0,0)
415 local m = ent:GetBoneMatrix(bone)
416 if (m) then
417 pos, ang = m:GetTranslation(), m:GetAngles()
418 end
419
420 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
421 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
422 ang.r = -ang.r // Fixes mirrored models
423 end
424
425 end
426
427 return pos, ang
428 end
429
430 function SWEP:CreateModels( tab )
431
432 if (!tab) then return end
433
434 // Create the clientside models here because Garry says we cant do it in the render hook
435 for k, v in pairs( tab ) do
436 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
437 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
438
439 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
440 if (IsValid(v.modelEnt)) then
441 v.modelEnt:SetPos(self:GetPos())
442 v.modelEnt:SetAngles(self:GetAngles())
443 v.modelEnt:SetParent(self)
444 v.modelEnt:SetNoDraw(true)
445 v.createdModel = v.model
446 else
447 v.modelEnt = nil
448 end
449
450 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
451 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
452
453 local name = v.sprite.."-"
454 local params = { ["$basetexture"] = v.sprite }
455 // make sure we create a unique name based on the selected options
456 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
457 for i, j in pairs( tocheck ) do
458 if (v[j]) then
459 params["$"..j] = 1
460 name = name.."1"
461 else
462 name = name.."0"
463 end
464 end
465
466 v.createdSprite = v.sprite
467 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
468
469 end
470 end
471
472 end
473
474 local allbones
475 local hasGarryFixedBoneScalingYet = false
476
477 function SWEP:UpdateBonePositions(vm)
478
479 if self.ViewModelBoneMods then
480
481 if (!vm:GetBoneCount()) then return end
482
483 // !! WORKAROUND !! //
484 // We need to check all model names :/
485 local loopthrough = self.ViewModelBoneMods
486 if (!hasGarryFixedBoneScalingYet) then
487 allbones = {}
488 for i=0, vm:GetBoneCount() do
489 local bonename = vm:GetBoneName(i)
490 if (self.ViewModelBoneMods[bonename]) then
491 allbones[bonename] = self.ViewModelBoneMods[bonename]
492 else
493 allbones[bonename] = {
494 scale = Vector(1,1,1),
495 pos = Vector(0,0,0),
496 angle = Angle(0,0,0)
497 }
498 end
499 end
500
501 loopthrough = allbones
502 end
503 // !! ----------- !! //
504
505 for k, v in pairs( loopthrough ) do
506 local bone = vm:LookupBone(k)
507 if (!bone) then continue end
508
509 // !! WORKAROUND !! //
510 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
511 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
512 local ms = Vector(1,1,1)
513 if (!hasGarryFixedBoneScalingYet) then
514 local cur = vm:GetBoneParent(bone)
515 while(cur >= 0) do
516 local pscale = loopthrough[vm:GetBoneName(cur)].scale
517 ms = ms * pscale
518 cur = vm:GetBoneParent(cur)
519 end
520 end
521
522 s = s * ms
523 // !! ----------- !! //
524
525 if vm:GetManipulateBoneScale(bone) != s then
526 vm:ManipulateBoneScale( bone, s )
527 end
528 if vm:GetManipulateBoneAngles(bone) != v.angle then
529 vm:ManipulateBoneAngles( bone, v.angle )
530 end
531 if vm:GetManipulateBonePosition(bone) != p then
532 vm:ManipulateBonePosition( bone, p )
533 end
534 end
535 else
536 self:ResetBonePositions(vm)
537 end
538
539 end
540
541 function SWEP:ResetBonePositions(vm)
542
543 if (!vm:GetBoneCount()) then return end
544 for i=0, vm:GetBoneCount() do
545 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
546 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
547 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
548 end
549
550 end
551
552 /**************************
553 Global utility code
554 **************************/
555
556 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
557 // Does not copy entities of course, only copies their reference.
558 // WARNING: do not use on tables that contain themselves somewhere down the line or youll get an infinite loop
559 function table.FullCopy( tab )
560
561 if (!tab) then return nil end
562
563 local res = {}
564 for k, v in pairs( tab ) do
565 if (type(v) == "table") then
566 res[k] = table.FullCopy(v) // recursion ho!
567 elseif (type(v) == "Vector") then
568 res[k] = Vector(v.x, v.y, v.z)
569 elseif (type(v) == "Angle") then
570 res[k] = Angle(v.p, v.y, v.r)
571 else
572 res[k] = v
573 end
574 end
575
576 return res
577
578 end
579
580end
581
582local function CreateRag(player)
583if CLIENT then return end
584 local ragdoll = ents.Create("prop_ragdoll")
585 ragdoll:SetPos(player:GetPos())
586 ragdoll:SetAngles(Angle(0,player:GetAngles().Yaw,0))
587 local model = player:GetModel()
588 ragdoll:SetModel(model)
589 ragdoll:Spawn()
590 ragdoll:Activate()
591 ragdoll:SetVelocity(player:GetVelocity())
592 ragdoll.OwnerINT = player:EntIndex()
593 ragdoll.PhysgunPickup = false
594 ragdoll.CanTool = false
595
596 local inv = {}
597 for _,v in pairs(player:GetWeapons()) do
598 table.insert(inv, v:GetClass())
599 end
600 ragdoll.WeaponINV = inv
601 player:StripWeapons()
602 player:Spectate(OBS_MODE_CHASE)
603 player:SpectateEntity(ragdoll)
604 player.Ragdoll = ragdoll
605 player.KnockoutTimer = CurTime()
606
607 timer.Simple(SKYRIZE_TAZER.KnockoutTime,function()
608 player:UnSpectate()
609 player:Spawn()
610 player:SetPos(player.Ragdoll:GetPos())
611 player:StripWeapons()
612 player:SetModel(player.Ragdoll:GetModel())
613 for k,v in pairs(player.Ragdoll.WeaponINV) do
614 player:Give(v)
615 end
616 if player.Ragdoll:IsValid() then player.Ragdoll:Remove() end;
617 player:Freeze(true)
618 timer.Simple(SKYRIZE_TAZER.FreezeTime, function() player:Freeze(false) end)
619 end)
620
621 for y=1, 14 do
622 timer.Simple(y*math.random(1,3)/10, function()
623 player:EmitSound("weapons/stunstick/spark"..math.random(1,3)..".wav")
624 end)
625 end
626
627
628end
629
630function SWEP:PrimaryAttack()
631 if (!self.nextfire or CurTime() >= self.nextfire) then
632 self.nextfire = CurTime() +SKYRIZE_TAZER.NextFire
633 for i=1, 3 do
634 self:EmitSound("weapons/stunstick/spark"..i..".wav")
635end
636 local vm = self.Owner:GetViewEntity()
637 local atch = vm:GetAttachment(1)
638 if not atch then return end
639 local pos = atch.Pos
640
641 local ply = self.Owner
642 local vecSrc = ply:GetShootPos();
643 local vecDirection = ply:GetAimVector();
644
645 local effectdata = EffectData()
646 effectdata:SetOrigin(vecSrc + ( vecDirection * 32*5 ))
647 effectdata:SetStart( self.Owner:GetShootPos() )
648 effectdata:SetAttachment( 1 )
649 effectdata:SetEntity( self )
650
651
652 util.Effect( "ToolTracer", effectdata )
653
654local tr = util.TraceLine( {
655 start = self.Owner:EyePos(),
656 endpos = self.Owner:EyePos() + self.Owner:EyeAngles():Forward() * 32*8,
657 filter = self.Owner,
658 } )
659 if ( !tr.Hit ) then return end
660 local ent = tr.Entity
661 if not ent:IsPlayer() then return end
662
663 CreateRag(ent)
664end
665end
666
667function SWEP:SecondaryAttack()
668
669end