· 8 years ago · May 08, 2018, 01:24 PM
1include("config_mining.lua")
2SWEP.PrintName = "OreBag"
3SWEP.Author = "Peklenc"
4SWEP.Purpose = "Holds Raw and Refined ores"
5SWEP.Slot = 0
6SWEP.SlotPos = 4
7SWEP.Spawnable = true
8SWEP.ViewModelFOV = 88
9SWEP.ViewModelFlip = false
10SWEP.UseHands = false
11SWEP.ViewModel = "models/weapons/c_arms.mdl"
12SWEP.WorldModel = ""
13SWEP.ShowViewModel = true
14SWEP.ShowWorldModel = true
15SWEP.Category = "Mining"
16
17SWEP.ViewModelBoneMods = {
18 ["l-upperarm-movement"] = { scale = Vector(1.052, 1.052, 1.052), pos = Vector(0, 0, 0), angle = Angle(0, 0, 0) }
19}
20SWEP.WElements = {
21 ["Bagg"] = { type = "Model", model = "models/Advisor.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(2.6, 12, 5.8), angle = Angle(-101, -6, 1), size = Vector(.30, .30, 0.30), color = Color(150, 50, 50, 255), material = "xeon133/slider_12x12x12"}
22}
23SWEP.VElements = {
24 ["bag"] = { type = "Model", model = "models/Advisor.mdl", bone = "l-upperarm-movement", rel = "", pos = Vector(2.6, 20, 7.8), angle = Angle(160, -30, -165), size = Vector(0.43, 0.43, 0.43), color = Color(0, 0, 0, 255) , material = "xeon133/slider_12x12x12"}
25}
26local OpenOreBag
27if CLIENT then
28 function SWEP:CustomAmmoDisplay()
29 self.AmmoDisplay = self.AmmoDisplay or {}
30 self.AmmoDisplay.Draw = false
31 return self.AmmoDisplay
32 end
33 local OreBagMenu
34 OpenOreBag = function(ply)
35 if LocalPlayer().Orebag != nil then
36 if OreBagMenu == nil then
37 OreBagMenu = vgui.Create( "PekFrame" )
38 OreBagMenu:SetSize( 445, 451)
39 OreBagMenu:SetTitle( SMS.LangTab["Ore Bag"] )
40 OreBagMenu:Configured()
41 OreBagMenu:ShowCloseButton(true)
42 OreBagMenu:SetVisible( true )
43 OreBagMenu:Center()
44 OreBagMenu:SetDraggable( true )
45 OreBagMenu:MakePopup()
46 local _, orebagmenusize = OreBagMenu:GetSize()
47 function OreBagMenu:OnClose()
48 OreBagMenu = nil
49 end
50 local panel = vgui.Create( "DPanel", OreBagMenu )
51 panel:SetSize( OreBagMenu:GetSize() , orebagmenusize/2 - 12.5 )
52 panel:SetBackgroundColor(Color(50,50,50))
53 panel:Dock(TOP)
54 local Dragbase = vgui.Create( "DDragBase" , panel )
55 Dragbase:SetSize(panel:GetSize()*(2/3))
56 Dragbase:Dock(RIGHT)
57 local panelleft = vgui.Create( "DPanel", panel )
58 panelleft:SetSize( panel:GetSize()*(1/3), orebagmenusize/2 -12.5)
59 panelleft:SetBackgroundColor(Color(25,25,25))
60 panelleft:Dock(LEFT)
61 -- function panelleft:Think()
62 -- local Defaulttext = SMS.LangTab["Raw ores can either be sold for"] .." ".. SMS.LangTab["quick cash or smelted into bars."]
63 -- end
64 local newtext = ""
65 function panelleft:SetText(name)
66 newtext = name
67 end
68 function panelleft:Paint(w,h)
69 local RawTotalSalesPrice = 0
70 for v,k in pairs (LocalPlayer().Orebag.Raw) do
71 if SMSRocks[v] != nil then
72 RawTotalSalesPrice = (RawTotalSalesPrice + (math.Round(SMSRocks[v].Value) * k))
73 end
74 end
75 draw.RoundedBox(0,0,0,w,h,Color(40,40,40))
76 draw.DrawText( SMS.LangTab["Raw ores"], "Trebuchet24", 5, 5, Color(255,255,255), TEXT_ALIGN_LEFT )
77 -- draw.DrawText( SMS.LangTab["quick cash or smelted into bars."], "default", 5, 15, Color(255,255,255), TEXT_ALIGN_LEFT )
78 draw.DrawText( SMS.LangTab["Total Value:"] .. " " .. SMS.Currancy .. RawTotalSalesPrice, "default", 5, 30, Color(255,255,255), TEXT_ALIGN_LEFT )
79 if newtext != "" then
80 draw.DrawText( tostring(newtext).." "..SMS.LangTab["Ore"], "default", 5, 50, Color(150,255,150), TEXT_ALIGN_LEFT )
81 draw.RoundedBox( 0, 5, 65, w -10, 2, Color(150,150,150) )
82 draw.DrawText( SMS.LangTab["Value:"].." "..SMS.Currancy..SMSRocks[newtext].Value, "default", 5, 70, Color(150,150,150), TEXT_ALIGN_LEFT )
83 draw.DrawText( SMS.LangTab["Quantity:"].." "..LocalPlayer().Orebag.Raw[newtext], "default", 5, 85, Color(150,150,150), TEXT_ALIGN_LEFT )
84 end
85 end
86 local Scroll = vgui.Create( "DScrollPanel", Dragbase )
87 Scroll:SetSize( panel:GetSize()*(2/3) , orebagmenusize/2 - 12.5 )
88 Scroll:SetPos( 0, 0 )
89
90 local bar = Scroll:GetVBar()
91 function Scroll:Paint( w, h )
92 --draw.RoundedBox( 0, 0, h-2, w , 2, Color(150,150,150) )
93 draw.RoundedBox( 0, w-15, -2, 15, h, Color( 0, 0, 0, 100 ) )
94 draw.RoundedBox( 0, w-15, h-15, 15, h, Color( 0, 0, 0, 200 ) )
95 draw.RoundedBox( 0, w-15, 0, 15, 15, Color( 0, 0, 0, 200 ) )
96 end
97
98 function bar:Paint( w, h )
99 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 100 ) )
100 end
101 function bar.btnUp:Paint( w, h )
102 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 200 ) )
103 end
104 function bar.btnDown:Paint( w, h )
105 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 200 ) )
106 end
107 function bar.btnGrip:Paint( w, h )
108 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 125 ) )
109 end
110
111
112
113
114
115 local List = vgui.Create( "DIconLayout", Scroll )
116 List:SetSize( panel:GetSize()*(2/3), orebagmenusize/2 - 12.5 )
117 List:SetPos( 3, 3 )
118 List:SetSpaceY( 5 )
119 List:SetSpaceX( 5 )
120
121
122
123
124
125
126 for v,k in pairs(LocalPlayer().Orebag.Raw) do
127 if SMSRocks[v] != nil then
128 if k > 0 then
129 local ListItem = List:Add( "DPanel" )
130 ListItem:SetSize( 65, 65 )
131 ListItem:SetBackgroundColor( Color(0,0,0, 150) )
132 local icon = vgui.Create( "DModelPanel", ListItem )
133 icon:SetSize( 65, 65 )
134 icon:Dock( TOP )
135 icon:SetModel(SMSRocks[v].CustomRawOreModel or "models/props_junk/rock001a.mdl")
136 icon:GetEntity():SetMaterial(SMSRocks[v].RawOreMaterial)
137 icon:SetColor(SMSRocks[v].Color)
138 local mn, mx = icon.Entity:GetRenderBounds()
139 local size = 0
140 size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) )
141 size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) )
142 size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) )
143 icon:SetFOV( 45 )
144 icon:SetCamPos( Vector( size, size, size) )
145 icon:SetLookAt( ( mn/5 + mx/5 ) * 0.5 )
146 function icon:LayoutEntity( Entity ) return end
147 local Dropbutton
148 local count = vgui.Create( "DLabel", icon )
149 count:SetPos( 5, 0 )
150 count:SetText( k )
151 count:SetColor(Color(255,255,255))
152 function count:Think()
153 count:SetText( LocalPlayer().Orebag.Raw[v] )
154 end
155
156
157 local count2 = vgui.Create( "DLabel", icon )
158 count2:SetPos( 5, icon:GetSize() - 20 )
159 count2:SetText( v )
160 count2:SetColor(Color(255,255,255))
161 function icon:Think()
162 if icon:IsHovered() or icon:IsChildHovered() then
163 panelleft:SetText(v)
164 if not IsValid(Dropbutton) then
165 Dropbutton = vgui.Create( "PekButton", icon )
166 Dropbutton:SetSize( 65, 65 )
167 Dropbutton:SetColor( Color(150,150,150) )
168 Dropbutton:SetText(SMS.LangTab["Drop"])
169 function Dropbutton.DoClick()
170 net.Start("Dropfrombag")
171 net.WriteTable( {v , "Raw"} )
172 net.SendToServer( )
173 end
174 else
175 Dropbutton:Show()
176 end
177 else
178 if IsValid(Dropbutton) then
179 Dropbutton:Hide()
180 end
181 end
182 end
183
184 end
185 end
186 end
187
188
189
190
191
192
193
194
195
196
197
198 local panel = vgui.Create( "DPanel", OreBagMenu )
199 panel:SetSize( OreBagMenu:GetSize() , orebagmenusize/2 - 12.5 )
200 panel:SetBackgroundColor(Color(50,50,50))
201 panel:Dock(TOP)
202 local Dragbase = vgui.Create( "DDragBase" , panel )
203 Dragbase:SetSize(panel:GetSize()*(2/3))
204 Dragbase:Dock(RIGHT)
205 local panelleft = vgui.Create( "DPanel", panel )
206 panelleft:SetSize( panel:GetSize()*(1/3), orebagmenusize/2 - 12.5)
207 panelleft:SetBackgroundColor(Color(25,25,25))
208 panelleft:Dock(LEFT)
209 local newtext = ""
210 function panelleft:SetText(name)
211 newtext = name
212 end
213 function panelleft:Paint(w,h)
214 local RefinedTotalSalesPrice = 0
215 for v,k in pairs (LocalPlayer().Orebag.Refined) do
216 if SMSRocks[v] != nil then
217 RefinedTotalSalesPrice = (RefinedTotalSalesPrice + (math.Round(SMSRocks[v].Value * SMSRocks[v].Refinedmultiplier) * k))
218 end
219 end
220 draw.RoundedBox(0,0,0,w,h,Color(40,40,40))
221 draw.RoundedBox( 0, 0, 0, w , 2, Color(150,150,150) )
222 draw.DrawText( SMS.LangTab["Refined ores"], "Trebuchet24", 5, 5, Color(255,255,255), TEXT_ALIGN_LEFT )
223 -- draw.DrawText( SMS.LangTab["quick cash or smelted into bars."], "default", 5, 15, Color(255,255,255), TEXT_ALIGN_LEFT )
224 draw.DrawText( SMS.LangTab["Total Value:"] .. " " .. SMS.Currancy .. RefinedTotalSalesPrice, "default", 5, 30, Color(255,255,255), TEXT_ALIGN_LEFT )
225 if newtext != "" then
226 draw.DrawText(SMS.LangTab["Refined"].." ".. tostring(newtext), "default", 5, 50, Color(150,255,150), TEXT_ALIGN_LEFT )
227 draw.RoundedBox( 0, 5, 65, w -10, 2, Color(150,150,150) )
228 draw.DrawText( SMS.LangTab["Value:"].." "..SMS.Currancy..math.Round(SMSRocks[newtext].Value * SMSRocks[newtext].Refinedmultiplier), "default", 5, 70, Color(150,150,150), TEXT_ALIGN_LEFT )
229 draw.DrawText( SMS.LangTab["Quantity:"].." "..LocalPlayer().Orebag.Refined[newtext], "default", 5, 85, Color(150,150,150), TEXT_ALIGN_LEFT )
230 end
231 end
232 local Scroll = vgui.Create( "DScrollPanel", Dragbase )
233 Scroll:SetSize( panel:GetSize()*(2/3) , orebagmenusize/2 - 12.5 )
234 Scroll:SetPos( 0, 0 )
235 function Scroll:Paint( w, h )
236 draw.RoundedBox( 0, 0, 0, w , 2, Color(150,150,150) )
237 draw.RoundedBox( 0, w-15, 2, 15, h, Color( 0, 0, 0, 100 ) )
238 draw.RoundedBox( 0, w-15, h-15, 15, h, Color( 0, 0, 0, 200 ) )
239 draw.RoundedBox( 0, w-15, 2, 15, 13, Color( 0, 0, 0, 200 ) )
240 end
241 local bar = Scroll:GetVBar()
242 function bar:Paint( w, h )
243 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 100 ) )
244 end
245 function bar.btnUp:Paint( w, h )
246 draw.RoundedBox( 0, 0, 2, w, h, Color( 0, 0, 0, 200 ) )
247 end
248 function bar.btnDown:Paint( w, h )
249 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 200 ) )
250 end
251 function bar.btnGrip:Paint( w, h )
252 draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 125 ) )
253 end
254
255
256 local List = vgui.Create( "DIconLayout", Scroll )
257 List:SetSize( panel:GetSize()*(2/3), orebagmenusize/2 - 12.5 )
258 List:SetPos( 3, 5 )
259 List:SetSpaceY( 5 )
260 List:SetSpaceX( 5 )
261
262
263
264
265
266
267
268
269
270 for v,k in pairs(LocalPlayer().Orebag.Refined) do
271 if SMSRocks[v] != nil then
272 if k > 0 then
273 local ListItem = List:Add( "DPanel" )
274 ListItem:SetSize( 65, 65 )
275 ListItem:SetBackgroundColor( Color(0,0,0, 150) )
276 local icon = vgui.Create( "DModelPanel", ListItem )
277 icon:SetSize( 65, 65 )
278 icon:Dock( TOP )
279 icon:SetModel(SMSRocks[v].CustomRefinedOreModel or "models/mining/metalbar.mdl")
280 icon:GetEntity():SetMaterial(SMSRocks[v].RefinedOreMaterial or "models/shiny")
281 icon:SetColor(SMSRocks[v].Color)
282 local mn, mx = icon.Entity:GetRenderBounds()
283 local size = 0
284 size = math.max( size, math.abs( mn.x ) + math.abs( mx.x ) )
285 size = math.max( size, math.abs( mn.y ) + math.abs( mx.y ) )
286 size = math.max( size, math.abs( mn.z ) + math.abs( mx.z ) )
287 icon:SetFOV( 40 )
288 icon:SetCamPos( Vector( size, size, size ) )
289 icon:SetLookAt( ( mn/5 + mx/5 ) * .5 )
290 function icon:LayoutEntity( Entity ) return end
291 local Dropbutton
292 function icon:Think()
293 if icon:IsHovered() or icon:IsChildHovered() then
294 panelleft:SetText(v)
295 if not IsValid(Dropbutton) then
296 Dropbutton = vgui.Create( "PekButton", icon )
297 Dropbutton:SetSize( 65, 65 )
298 Dropbutton:SetColor( Color(150,150,150) )
299 Dropbutton:SetText( SMS.LangTab["Drop"])
300 function Dropbutton.DoClick()
301 net.Start("Dropfrombag")
302 net.WriteTable( {v , "Refined"} )
303 net.SendToServer( )
304 end
305 else
306 Dropbutton:Show()
307 end
308 else
309 if IsValid(Dropbutton) then
310 Dropbutton:Hide()
311 end
312 end
313 end
314 local count = vgui.Create( "DLabel", icon )
315 count:SetPos( 5, 0 )
316 count:SetText( k )
317 count:SetColor(Color(255,255,255))
318
319 local count2 = vgui.Create( "DLabel", icon )
320 count2:SetPos( 5, icon:GetSize() - 20 )
321 count2:SetText( v )
322 count2:SetColor(Color(255,255,255))
323
324 function count:Think()
325 count:SetText( LocalPlayer().Orebag.Refined[v] )
326 end
327 end
328 end
329 end
330
331
332
333
334
335
336
337
338 end
339 else
340 LocalPlayer():ChatPrint( SMS.LangTab["Please wait orebag still loading"])
341 end
342 end
343end
344
345function SWEP:Deploy()
346 local ply = self.Owner
347 if SERVER then
348 Mining_SaveOrebag(ply)
349 end
350end
351
352function SWEP:PrimaryAttack()
353 if SERVER then
354 SendOreBag(self:GetOwner())
355 end
356 if CLIENT then
357 OpenOreBag()
358 end
359end
360function SWEP:SecondaryAttack()
361 if SERVER then
362 SendOreBag(self:GetOwner())
363 end
364 if CLIENT then
365 OpenOreBag()
366 end
367end
368function SWEP:Initialize()
369 self:SetHoldType( "normal" )
370 if CLIENT then
371 self.VElements = table.FullCopy( self.VElements )
372 self.WElements = table.FullCopy( self.WElements )
373 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
374 self:CreateModels(self.VElements)
375 self:CreateModels(self.WElements)
376 if IsValid(self.Owner) then
377 local vm = self.Owner:GetViewModel()
378 if IsValid(vm) then
379 self:ResetBonePositions(vm)
380 if (self.ShowViewModel == nil or self.ShowViewModel) then
381 vm:SetColor(Color(255,255,255,255))
382 else
383 vm:SetColor(Color(255,255,255,1))
384 vm:SetMaterial("Debug/hsv")
385 end
386 end
387 end
388 end
389end
390function SWEP:Holster()
391 if CLIENT and IsValid(self.Owner) then
392 local vm = self.Owner:GetViewModel()
393 if IsValid(vm) then
394 self:ResetBonePositions(vm)
395 end
396 end
397 return true
398end
399function SWEP:OnRemove()
400 self:Holster()
401end
402if CLIENT then
403
404 SWEP.vRenderOrder = nil
405 function SWEP:ViewModelDrawn()
406
407 local vm = self.Owner:GetViewModel()
408 if !IsValid(vm) then return end
409
410 if (!self.VElements) then return end
411
412 self:UpdateBonePositions(vm)
413
414 if (!self.vRenderOrder) then
415
416 // we build a render order because sprites need to be drawn after models
417 self.vRenderOrder = {}
418
419 for k, v in pairs( self.VElements ) do
420 if (v.type == "Model") then
421 table.insert(self.vRenderOrder, 1, k)
422 elseif (v.type == "Sprite" or v.type == "Quad") then
423 table.insert(self.vRenderOrder, k)
424 end
425 end
426
427 end
428
429 for k, name in ipairs( self.vRenderOrder ) do
430
431 local v = self.VElements[name]
432 if (!v) then self.vRenderOrder = nil break end
433 if (v.hide) then continue end
434
435 local model = v.modelEnt
436 local sprite = v.spriteMaterial
437
438 if (!v.bone) then continue end
439
440 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
441
442 if (!pos) then continue end
443
444 if (v.type == "Model" and IsValid(model)) then
445
446 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
447 ang:RotateAroundAxis(ang:Up(), v.angle.y)
448 ang:RotateAroundAxis(ang:Right(), v.angle.p)
449 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
450
451 model:SetAngles(ang)
452 //model:SetModelScale(v.size)
453 local matrix = Matrix()
454 matrix:Scale(v.size)
455 model:EnableMatrix( "RenderMultiply", matrix )
456
457 if (v.material == "") then
458 model:SetMaterial("")
459 elseif (model:GetMaterial() != v.material) then
460 model:SetMaterial( v.material )
461 end
462
463 if (v.skin and v.skin != model:GetSkin()) then
464 model:SetSkin(v.skin)
465 end
466
467 if (v.bodygroup) then
468 for k, v in pairs( v.bodygroup ) do
469 if (model:GetBodygroup(k) != v) then
470 model:SetBodygroup(k, v)
471 end
472 end
473 end
474
475 if (v.surpresslightning) then
476 render.SuppressEngineLighting(true)
477 end
478
479 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
480 render.SetBlend(v.color.a/255)
481 model:DrawModel()
482 render.SetBlend(1)
483 render.SetColorModulation(1, 1, 1)
484
485 if (v.surpresslightning) then
486 render.SuppressEngineLighting(false)
487 end
488
489 elseif (v.type == "Sprite" and sprite) then
490
491 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
492 render.SetMaterial(sprite)
493 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
494
495 elseif (v.type == "Quad" and v.draw_func) then
496
497 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
498 ang:RotateAroundAxis(ang:Up(), v.angle.y)
499 ang:RotateAroundAxis(ang:Right(), v.angle.p)
500 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
501
502 cam.Start3D2D(drawpos, ang, v.size)
503 v.draw_func( self )
504 cam.End3D2D()
505
506 end
507
508 end
509
510 end
511
512 SWEP.wRenderOrder = nil
513 function SWEP:DrawWorldModel()
514
515 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
516 self:DrawModel()
517 end
518
519 if (!self.WElements) then return end
520
521 if (!self.wRenderOrder) then
522
523 self.wRenderOrder = {}
524
525 for k, v in pairs( self.WElements ) do
526 if (v.type == "Model") then
527 table.insert(self.wRenderOrder, 1, k)
528 elseif (v.type == "Sprite" or v.type == "Quad") then
529 table.insert(self.wRenderOrder, k)
530 end
531 end
532
533 end
534
535 if (IsValid(self.Owner)) then
536 bone_ent = self.Owner
537 else
538 // when the weapon is dropped
539 bone_ent = self
540 end
541
542 for k, name in pairs( self.wRenderOrder ) do
543
544 local v = self.WElements[name]
545 if (!v) then self.wRenderOrder = nil break end
546 if (v.hide) then continue end
547
548 local pos, ang
549
550 if (v.bone) then
551 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
552 else
553 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
554 end
555
556 if (!pos) then continue end
557
558 local model = v.modelEnt
559 local sprite = v.spriteMaterial
560
561 if (v.type == "Model" and IsValid(model)) then
562
563 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
564 ang:RotateAroundAxis(ang:Up(), v.angle.y)
565 ang:RotateAroundAxis(ang:Right(), v.angle.p)
566 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
567
568 model:SetAngles(ang)
569 //model:SetModelScale(v.size)
570 local matrix = Matrix()
571 matrix:Scale(v.size)
572 model:EnableMatrix( "RenderMultiply", matrix )
573
574 if (v.material == "") then
575 model:SetMaterial("")
576 elseif (model:GetMaterial() != v.material) then
577 model:SetMaterial( v.material )
578 end
579
580 if (v.skin and v.skin != model:GetSkin()) then
581 model:SetSkin(v.skin)
582 end
583
584 if (v.bodygroup) then
585 for k, v in pairs( v.bodygroup ) do
586 if (model:GetBodygroup(k) != v) then
587 model:SetBodygroup(k, v)
588 end
589 end
590 end
591
592 if (v.surpresslightning) then
593 render.SuppressEngineLighting(true)
594 end
595
596 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
597 render.SetBlend(v.color.a/255)
598 model:DrawModel()
599 render.SetBlend(1)
600 render.SetColorModulation(1, 1, 1)
601
602 if (v.surpresslightning) then
603 render.SuppressEngineLighting(false)
604 end
605
606 elseif (v.type == "Sprite" and sprite) then
607
608 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
609 render.SetMaterial(sprite)
610 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
611
612 elseif (v.type == "Quad" and v.draw_func) then
613
614 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
615 ang:RotateAroundAxis(ang:Up(), v.angle.y)
616 ang:RotateAroundAxis(ang:Right(), v.angle.p)
617 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
618
619 cam.Start3D2D(drawpos, ang, v.size)
620 v.draw_func( self )
621 cam.End3D2D()
622
623 end
624
625 end
626
627 end
628
629 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
630
631 local bone, pos, ang
632 if (tab.rel and tab.rel != "") then
633
634 local v = basetab[tab.rel]
635
636 if (!v) then return end
637
638 // Technically, if there exists an element with the same name as a bone
639 // you can get in an infinite loop. Let's just hope nobody's that stupid.
640 pos, ang = self:GetBoneOrientation( basetab, v, ent )
641
642 if (!pos) then return end
643
644 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
645 ang:RotateAroundAxis(ang:Up(), v.angle.y)
646 ang:RotateAroundAxis(ang:Right(), v.angle.p)
647 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
648
649 else
650
651 bone = ent:LookupBone(bone_override or tab.bone)
652
653 if (!bone) then return end
654
655 pos, ang = Vector(0,0,0), Angle(0,0,0)
656 local m = ent:GetBoneMatrix(bone)
657 if (m) then
658 pos, ang = m:GetTranslation(), m:GetAngles()
659 end
660
661 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
662 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
663 ang.r = -ang.r // Fixes mirrored models
664 end
665
666 end
667
668 return pos, ang
669 end
670
671 function SWEP:CreateModels( tab )
672
673 if (!tab) then return end
674
675 // Create the clientside models here because Garry says we can't do it in the render hook
676 for k, v in pairs( tab ) do
677 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
678 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
679
680 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
681 if (IsValid(v.modelEnt)) then
682 v.modelEnt:SetPos(self:GetPos())
683 v.modelEnt:SetAngles(self:GetAngles())
684 v.modelEnt:SetParent(self)
685 v.modelEnt:SetNoDraw(true)
686 v.createdModel = v.model
687 else
688 v.modelEnt = nil
689 end
690
691 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
692 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
693
694 local name = v.sprite.."-"
695 local params = { ["$basetexture"] = v.sprite }
696 // make sure we create a unique name based on the selected options
697 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
698 for i, j in pairs( tocheck ) do
699 if (v[j]) then
700 params["$"..j] = 1
701 name = name.."1"
702 else
703 name = name.."0"
704 end
705 end
706
707 v.createdSprite = v.sprite
708 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
709
710 end
711 end
712
713 end
714
715 local allbones
716 local hasGarryFixedBoneScalingYet = false
717
718 function SWEP:UpdateBonePositions(vm)
719
720 if self.ViewModelBoneMods then
721
722 if (!vm:GetBoneCount()) then return end
723
724 // !! WORKAROUND !! //
725 // We need to check all model names :/
726 local loopthrough = self.ViewModelBoneMods
727 if (!hasGarryFixedBoneScalingYet) then
728 allbones = {}
729 for i=0, vm:GetBoneCount() do
730 local bonename = vm:GetBoneName(i)
731 if (self.ViewModelBoneMods[bonename]) then
732 allbones[bonename] = self.ViewModelBoneMods[bonename]
733 else
734 allbones[bonename] = {
735 scale = Vector(1,1,1),
736 pos = Vector(0,0,0),
737 angle = Angle(0,0,0)
738 }
739 end
740 end
741
742 loopthrough = allbones
743 end
744 // !! ----------- !! //
745
746 for k, v in pairs( loopthrough ) do
747 local bone = vm:LookupBone(k)
748 if (!bone) then continue end
749
750 // !! WORKAROUND !! //
751 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
752 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
753 local ms = Vector(1,1,1)
754 if (!hasGarryFixedBoneScalingYet) then
755 local cur = vm:GetBoneParent(bone)
756 while(cur >= 0) do
757 local pscale = loopthrough[vm:GetBoneName(cur)].scale
758 ms = ms * pscale
759 cur = vm:GetBoneParent(cur)
760 end
761 end
762
763 s = s * ms
764 // !! ----------- !! //
765
766 if vm:GetManipulateBoneScale(bone) != s then
767 vm:ManipulateBoneScale( bone, s )
768 end
769 if vm:GetManipulateBoneAngles(bone) != v.angle then
770 vm:ManipulateBoneAngles( bone, v.angle )
771 end
772 if vm:GetManipulateBonePosition(bone) != p then
773 vm:ManipulateBonePosition( bone, p )
774 end
775 end
776 else
777 self:ResetBonePositions(vm)
778 end
779
780 end
781
782 function SWEP:ResetBonePositions(vm)
783
784 if (!vm:GetBoneCount()) then return end
785 for i=0, vm:GetBoneCount() do
786 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
787 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
788 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
789 end
790
791 end
792
793 /**************************
794 Global utility code
795 **************************/
796
797 // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
798 // Does not copy entities of course, only copies their reference.
799 // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
800 function table.FullCopy( tab )
801
802 if (!tab) then return nil end
803
804 local res = {}
805 for k, v in pairs( tab ) do
806 if (type(v) == "table") then
807 res[k] = table.FullCopy(v) // recursion ho!
808 elseif (type(v) == "Vector") then
809 res[k] = Vector(v.x, v.y, v.z)
810 elseif (type(v) == "Angle") then
811 res[k] = Angle(v.p, v.y, v.r)
812 else
813 res[k] = v
814 end
815 end
816
817 return res
818
819 end
820
821end