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