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