· 10 years ago · Aug 21, 2016, 09:30 AM
1// Variables that are used on both client and server
2if (CLIENT) then
3
4 SWEP.PrintName = "Golden Gun";
5 SWEP.Slot = 1;
6 SWEP.SlotPos = 1;
7 SWEP.DrawAmmo = true;
8 SWEP.DrawCrosshair = false;
9
10 SWEP.WepSelectIcon = surface.GetTextureID("vgui/entities/weapon_goldengun")
11 SWEP.BounceWeaponIcon = false
12 SWEP.Icon = "VGUI/ttt/icon_gold_gun"
13end
14
15SWEP.VElements = {
16 ["element_name"] = { type = "Model", model = "models/goldengun.mdl", bone = "Object03", rel = "", pos = Vector(16.1, -0.057, 0.5), angle = Angle(-1.065, -180, -90.282), size = Vector(1,1,1), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/player/shared/gold_player", skin = 0, bodygroup = {} }
17}
18SWEP.WElements = {
19 ["element_name"] = { type = "Model", model = "models/goldengun.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(-12.855, 1.567, 0.054), angle = Angle(-3.962, 0, 173.091), size = Vector(0.935, 0.935, 0.935), color = Color(255, 255, 255, 255), surpresslightning = false, material = "models/player/shared/gold_player", skin = 0, bodygroup = {} }
20}
21
22
23SWEP.RunArmOffset = Vector (0, -4.919, -2.623)
24SWEP.RunArmAngle = Vector (33.852, 0, 0)
25
26
27SWEP.Category = "Babel Industries"
28
29SWEP.Author = "Babel Ind. (model ripped by Asteroidrules)";
30SWEP.Contact = "Babel Industries";
31SWEP.Purpose = "";
32SWEP.Instructions = "If you've ever played Goldeneye 64's multiplayer into the morning hours with friends, you know what this does.";
33SWEP.IconLetter = "m"
34
35
36SWEP.Base = "weapon_mad_base"
37
38SWEP.HoldType = "pistol"
39SWEP.ViewModelFOV = 80
40SWEP.ViewModelFlip = true
41SWEP.ViewModel = "models/weapons/v_pist_xiveseven.mdl"
42SWEP.WorldModel = "models/weapons/w_pistol.mdl"
43SWEP.ShowViewModel = true
44SWEP.ShowWorldModel = false
45SWEP.ViewModelBoneMods = {
46 ["R 09"] = { scale = Vector(1, 1, 1), pos = Vector(0.901, 0.603, -0.12), angle = Angle(0, 0, 0) },
47 ["Object03"] = { scale = Vector(0.128, 0.128, 0.128), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) }
48}
49
50 // TTT Convertion Code \\
51
52SWEP.Base = "weapon_tttbase"
53SWEP.Kind = WEAPON_PISTOL
54SWEP.AutoSpawnable = false
55SWEP.AmmoEnt = "item_ammo_revolver_ttt"
56SWEP.InLoadoutFor = nil
57SWEP.AllowDrop = true
58SWEP.IsSilent = false
59SWEP.NoSights = false
60
61SWEP.IronSightsPos = Vector(4, 5, 1.519)
62SWEP.IronSightsAng = Vector(0, 1, 0.068)
63
64function SWEP:Reload()
65 if !( ( self.Weapon:Clip1() ) < ( self.Primary.ClipSize ) ) then return end
66 self.Weapon:DefaultReload( ACT_VM_RELOAD )
67 self:EmitSound( "weapons/goldengun/reload2.wav" )
68
69end
70function SWEP:Deploy()
71 self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
72
73
74 return true;
75end
76
77
78
79
80SWEP.Spawnable = true
81SWEP.AdminSpawnable = false
82
83SWEP.Primary.Sound = Sound("weapons/goldengun/gg-shoot2.wav")
84SWEP.Primary.Recoil = .35
85SWEP.Primary.Damage = 35
86SWEP.Primary.NumShots = 2
87SWEP.Primary.Cone = 0
88SWEP.Primary.Delay = .1
89SWEP.Primary.Force = 0;
90
91SWEP.Primary.ClipSize = 1 // Size of a clip
92SWEP.Primary.DefaultClip = 30 // Default number of bullets in a clip
93SWEP.Primary.Automatic = false // Automatic/Semi Auto
94SWEP.Primary.Ammo = "Pistol"
95
96SWEP.Secondary.ClipSize = -1 // Size of a clip
97SWEP.Secondary.DefaultClip = -1 // Default number of bullets in a clip
98SWEP.Secondary.Automatic = false // Automatic/Semi Auto
99SWEP.Secondary.Ammo = "none"
100
101
102
103function SWEP:Initialize()
104
105 // other initialize code goes here
106
107 if CLIENT then
108
109 // Create a new table for every weapon instance
110 self.VElements = table.FullCopy( self.VElements )
111 self.WElements = table.FullCopy( self.WElements )
112 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
113
114 self:CreateModels(self.VElements) // create viewmodels
115 self:CreateModels(self.WElements) // create worldmodels
116
117 // init view model bone build function
118 local vm = self.Owner:GetViewModel()
119 if IsValid(vm) then
120 self:ResetBonePositions(vm)
121 end
122
123 // Init viewmodel visibility
124 if (self.ShowViewModel == nil or self.ShowViewModel) then
125 vm:SetColor(Color(255,255,255,255))
126 else
127 // we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
128 vm:SetColor(Color(255,255,255,1))
129 // ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
130 // 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
131 vm:SetMaterial("Debug/hsv")
132 end
133
134 end
135
136end
137
138function SWEP:Holster()
139
140 if CLIENT then
141 local vm = self.Owner:GetViewModel()
142 if IsValid(vm) then
143 self:ResetBonePositions(vm)
144 end
145 end
146
147 return true
148end
149
150if CLIENT then
151
152 SWEP.vRenderOrder = nil
153 function SWEP:ViewModelDrawn()
154
155 local vm = self.Owner:GetViewModel()
156 if !IsValid(vm) then return end
157
158 if (!self.VElements) then return end
159
160 self:UpdateBonePositions(vm)
161
162 if (!self.vRenderOrder) then
163
164 // we build a render order because sprites need to be drawn after models
165 self.vRenderOrder = {}
166
167 for k, v in pairs( self.VElements ) do
168 if (v.type == "Model") then
169 table.insert(self.vRenderOrder, 1, k)
170 elseif (v.type == "Sprite" or v.type == "Quad") then
171 table.insert(self.vRenderOrder, k)
172 end
173 end
174
175 end
176
177 for k, name in ipairs( self.vRenderOrder ) do
178
179 local v = self.VElements[name]
180 if (!v) then self.vRenderOrder = nil break end
181 if (v.hide) then continue end
182
183 local model = v.modelEnt
184 local sprite = v.spriteMaterial
185
186 if (!v.bone) then continue end
187
188 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
189
190 if (!pos) then continue end
191
192 if (v.type == "Model" and IsValid(model)) then
193
194 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
195 ang:RotateAroundAxis(ang:Up(), v.angle.y)
196 ang:RotateAroundAxis(ang:Right(), v.angle.p)
197 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
198
199 model:SetAngles(ang)
200 //model:SetModelScale(v.size)
201 local matrix = Matrix()
202 matrix:Scale(v.size)
203 model:EnableMatrix( "RenderMultiply", matrix )
204
205 if (v.material == "") then
206 model:SetMaterial("")
207 elseif (model:GetMaterial() != v.material) then
208 model:SetMaterial( v.material )
209 end
210
211 if (v.skin and v.skin != model:GetSkin()) then
212 model:SetSkin(v.skin)
213 end
214
215 if (v.bodygroup) then
216 for k, v in pairs( v.bodygroup ) do
217 if (model:GetBodygroup(k) != v) then
218 model:SetBodygroup(k, v)
219 end
220 end
221 end
222
223 if (v.surpresslightning) then
224 render.SuppressEngineLighting(true)
225 end
226
227 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
228 render.SetBlend(v.color.a/255)
229 model:DrawModel()
230 render.SetBlend(1)
231 render.SetColorModulation(1, 1, 1)
232
233 if (v.surpresslightning) then
234 render.SuppressEngineLighting(false)
235 end
236
237 elseif (v.type == "Sprite" and sprite) then
238
239 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
240 render.SetMaterial(sprite)
241 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
242
243 elseif (v.type == "Quad" and v.draw_func) then
244
245 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
246 ang:RotateAroundAxis(ang:Up(), v.angle.y)
247 ang:RotateAroundAxis(ang:Right(), v.angle.p)
248 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
249
250 cam.Start3D2D(drawpos, ang, v.size)
251 v.draw_func( self )
252 cam.End3D2D()
253
254 end
255
256 end
257
258 end
259
260 SWEP.wRenderOrder = nil
261 function SWEP:DrawWorldModel()
262
263 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
264 self:DrawModel()
265 end
266
267 if (!self.WElements) then return end
268
269 if (!self.wRenderOrder) then
270
271 self.wRenderOrder = {}
272
273 for k, v in pairs( self.WElements ) do
274 if (v.type == "Model") then
275 table.insert(self.wRenderOrder, 1, k)
276 elseif (v.type == "Sprite" or v.type == "Quad") then
277 table.insert(self.wRenderOrder, k)
278 end
279 end
280
281 end
282
283 if (IsValid(self.Owner)) then
284 bone_ent = self.Owner
285 else
286 // when the weapon is dropped
287 bone_ent = self
288 end
289
290 for k, name in pairs( self.wRenderOrder ) do
291
292 local v = self.WElements[name]
293 if (!v) then self.wRenderOrder = nil break end
294 if (v.hide) then continue end
295
296 local pos, ang
297
298 if (v.bone) then
299 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
300 else
301 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
302 end
303
304 if (!pos) then continue end
305
306 local model = v.modelEnt
307 local sprite = v.spriteMaterial
308
309 if (v.type == "Model" and IsValid(model)) then
310
311 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
312 ang:RotateAroundAxis(ang:Up(), v.angle.y)
313 ang:RotateAroundAxis(ang:Right(), v.angle.p)
314 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
315
316 model:SetAngles(ang)
317 //model:SetModelScale(v.size)
318 local matrix = Matrix()
319 matrix:Scale(v.size)
320 model:EnableMatrix( "RenderMultiply", matrix )
321
322 if (v.material == "") then
323 model:SetMaterial("")
324 elseif (model:GetMaterial() != v.material) then
325 model:SetMaterial( v.material )
326 end
327
328 if (v.skin and v.skin != model:GetSkin()) then
329 model:SetSkin(v.skin)
330 end
331
332 if (v.bodygroup) then
333 for k, v in pairs( v.bodygroup ) do
334 if (model:GetBodygroup(k) != v) then
335 model:SetBodygroup(k, v)
336 end
337 end
338 end
339
340 if (v.surpresslightning) then
341 render.SuppressEngineLighting(true)
342 end
343
344 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
345 render.SetBlend(v.color.a/255)
346 model:DrawModel()
347 render.SetBlend(1)
348 render.SetColorModulation(1, 1, 1)
349
350 if (v.surpresslightning) then
351 render.SuppressEngineLighting(false)
352 end
353
354 elseif (v.type == "Sprite" and sprite) then
355
356 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
357 render.SetMaterial(sprite)
358 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
359
360 elseif (v.type == "Quad" and v.draw_func) then
361
362 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
363 ang:RotateAroundAxis(ang:Up(), v.angle.y)
364 ang:RotateAroundAxis(ang:Right(), v.angle.p)
365 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
366
367 cam.Start3D2D(drawpos, ang, v.size)
368 v.draw_func( self )
369 cam.End3D2D()
370
371 end
372
373 end
374
375 end
376
377 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
378
379 local bone, pos, ang
380 if (tab.rel and tab.rel != "") then
381
382 local v = basetab[tab.rel]
383
384 if (!v) then return end
385
386 // Technically, if there exists an element with the same name as a bone
387 // you can get in an infinite loop. Let's just hope nobody's that stupid.
388 pos, ang = self:GetBoneOrientation( basetab, v, ent )
389
390 if (!pos) then return end
391
392 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
393 ang:RotateAroundAxis(ang:Up(), v.angle.y)
394 ang:RotateAroundAxis(ang:Right(), v.angle.p)
395 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
396
397 else
398
399 bone = ent:LookupBone(bone_override or tab.bone)
400
401 if (!bone) then return end
402
403 pos, ang = Vector(0,0,0), Angle(0,0,0)
404 local m = ent:GetBoneMatrix(bone)
405 if (m) then
406 pos, ang = m:GetTranslation(), m:GetAngles()
407 end
408
409 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
410 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
411 ang.r = -ang.r // Fixes mirrored models
412 end
413
414 end
415
416 return pos, ang
417 end
418
419 function SWEP:CreateModels( tab )
420
421 if (!tab) then return end
422
423 // Create the clientside models here because Garry says we can't do it in the render hook
424 for k, v in pairs( tab ) do
425 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
426 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
427
428 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
429 if (IsValid(v.modelEnt)) then
430 v.modelEnt:SetPos(self:GetPos())
431 v.modelEnt:SetAngles(self:GetAngles())
432 v.modelEnt:SetParent(self)
433 v.modelEnt:SetNoDraw(true)
434 v.createdModel = v.model
435 else
436 v.modelEnt = nil
437 end
438
439 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
440 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
441
442 local name = v.sprite.."-"
443 local params = { ["$basetexture"] = v.sprite }
444 // make sure we create a unique name based on the selected options
445 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
446 for i, j in pairs( tocheck ) do
447 if (v[j]) then
448 params["$"..j] = 1
449 name = name.."1"
450 else
451 name = name.."0"
452 end
453 end
454
455 v.createdSprite = v.sprite
456 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
457
458 end
459 end
460
461 end
462
463 local allbones
464 local hasGarryFixedBoneScalingYet = false
465
466 function SWEP:UpdateBonePositions(vm)
467
468 if self.ViewModelBoneMods then
469
470 if (!vm:GetBoneCount()) then return end
471
472 // !! WORKAROUND !! //
473 // We need to check all model names :/
474 local loopthrough = self.ViewModelBoneMods
475 if (!hasGarryFixedBoneScalingYet) then
476 allbones = {}
477 for i=0, vm:GetBoneCount() do
478 local bonename = vm:GetBoneName(i)
479 if (self.ViewModelBoneMods[bonename]) then
480 allbones[bonename] = self.ViewModelBoneMods[bonename]
481 else
482 allbones[bonename] = {
483 scale = Vector(1,1,1),
484 pos = Vector(0,0,0),
485 angle = Angle(0,0,0)
486 }
487 end
488 end
489
490 loopthrough = allbones
491 end
492 // !! ----------- !! //
493
494 for k, v in pairs( loopthrough ) do
495 local bone = vm:LookupBone(k)
496 if (!bone) then continue end
497
498 // !! WORKAROUND !! //
499 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
500 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
501 local ms = Vector(1,1,1)
502 if (!hasGarryFixedBoneScalingYet) then
503 local cur = vm:GetBoneParent(bone)
504 while(cur >= 0) do
505 local pscale = loopthrough[vm:GetBoneName(cur)].scale
506 ms = ms * pscale
507 cur = vm:GetBoneParent(cur)
508 end
509 end
510
511 s = s * ms
512 // !! ----------- !! //
513
514 if vm:GetManipulateBoneScale(bone) != s then
515 vm:ManipulateBoneScale( bone, s )
516 end
517 if vm:GetManipulateBoneAngles(bone) != v.angle then
518 vm:ManipulateBoneAngles( bone, v.angle )
519 end
520 if vm:GetManipulateBonePosition(bone) != p then
521 vm:ManipulateBonePosition( bone, p )
522 end
523 end
524 else
525 self:ResetBonePositions(vm)
526 end
527
528 end
529
530 function SWEP:ResetBonePositions(vm)
531
532 if (!vm:GetBoneCount()) then return end
533 for i=0, vm:GetBoneCount() do
534 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
535 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
536 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
537 end
538
539 end
540
541 /**************************
542 Global utility code
543 **************************/
544
545 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
546 // Does not copy entities of course, only copies their reference.
547 // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
548 function table.FullCopy( tab )
549
550 if (!tab) then return nil end
551
552 local res = {}
553 for k, v in pairs( tab ) do
554 if (type(v) == "table") then
555 res[k] = table.FullCopy(v) // recursion ho!
556 elseif (type(v) == "Vector") then
557 res[k] = Vector(v.x, v.y, v.z)
558 elseif (type(v) == "Angle") then
559 res[k] = Angle(v.p, v.y, v.r)
560 else
561 res[k] = v
562 end
563 end
564
565 return res
566
567 end
568
569end