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