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