· 7 years ago · Jan 18, 2019, 02:06 PM
1-- //Variables that are used on both client and server
2SWEP.Category = ""
3SWEP.Gun = ""
4SWEP.Author = "Generic Default, Worshipper, Clavus, and Bob"
5SWEP.Contact = ""
6SWEP.Purpose = ""
7SWEP.Instructions = ""
8SWEP.MuzzleAttachment = "1" -- Should be "1" for CSS models or "muzzle" for hl2 models
9SWEP.DrawCrosshair = true -- Hell no, crosshairs r 4 nubz!
10SWEP.ViewModelFOV = 65 -- How big the gun will look
11SWEP.ViewModelFlip = true -- True for CSS models, False for HL2 models
12
13SWEP.Spawnable = false
14SWEP.AdminSpawnable = false
15
16SWEP.Primary.Sound = Sound("") -- Sound of the gun
17SWEP.Primary.Round = ("") -- What kind of bullet?
18SWEP.Primary.Cone = 0.2 -- Accuracy of NPCs
19SWEP.Primary.Recoil = 10
20SWEP.Primary.Damage = 10
21SWEP.Primary.Spread = .01 --define from-the-hip accuracy (1 is terrible, .0001 is exact)
22SWEP.Primary.NumShots = 1
23SWEP.Primary.RPM = 0 -- This is in Rounds Per Minute
24SWEP.Primary.ClipSize = 0 -- Size of a clip
25SWEP.Primary.DefaultClip = 0 -- Default number of bullets in a clip
26SWEP.Primary.KickUp = 0 -- Maximum up recoil (rise)
27SWEP.Primary.KickDown = 0 -- Maximum down recoil (skeet)
28SWEP.Primary.KickHorizontal = 0 -- Maximum side recoil (koolaid)
29SWEP.Primary.Automatic = true -- Automatic/Semi Auto
30SWEP.Primary.Ammo = "none" -- What kind of ammo
31
32-- SWEP.Secondary.ClipSize = 0 -- Size of a clip
33-- SWEP.Secondary.DefaultClip = 0 -- Default number of bullets in a clip
34-- SWEP.Secondary.Automatic = false -- Automatic/Semi Auto
35SWEP.Secondary.Ammo = ""
36--//HAHA! GOTCHA, YA BASTARD!
37
38-- SWEP.Secondary.IronFOV = 0 -- How much you 'zoom' in. Less is more!
39
40SWEP.Penetration = true
41SWEP.Ricochet = true
42SWEP.MaxRicochet = 1
43SWEP.RicochetCoin = 1
44SWEP.BoltAction = false
45SWEP.Scoped = false
46SWEP.ShellTime = .35
47SWEP.Tracer = 0
48SWEP.CanBeSilenced = false
49SWEP.Silenced = false
50SWEP.NextSilence = 0
51SWEP.SelectiveFire = false
52SWEP.NextFireSelect = 0
53SWEP.OrigCrossHair = true
54
55local PainMulti = 1
56
57if GetConVar("M9KDamageMultiplier") == nil then
58 PainMulti = 1
59 print("M9KDamageMultiplier is missing! You may have hit the lua limit! Reverting multiplier to 1.")
60else
61 PainMulti = GetConVar("M9KDamageMultiplier"):GetFloat()
62 if PainMulti < 0 then
63 PainMulti = PainMulti * -1
64 print("Your damage multiplier was in the negatives. It has been reverted to a positive number. Your damage multiplier is now "..PainMulti)
65 end
66end
67
68function NewM9KDamageMultiplier(cvar, previous, new)
69 print("multiplier has been changed ")
70 if GetConVar("M9KDamageMultiplier") == nil then
71 PainMulti = 1
72 print("M9KDamageMultiplier is missing! You may have hit the lua limit! Reverting multiplier to 1, you will notice no changes.")
73 else
74 PainMulti = GetConVar("M9KDamageMultiplier"):GetFloat()
75 if PainMulti < 0 then
76 PainMulti = PainMulti * -1
77 print("Your damage multiplier was in the negatives. It has been reverted to a positive number. Your damage multiplier is now "..PainMulti)
78 end
79 end
80end
81cvars.AddChangeCallback("M9KDamageMultiplier", NewM9KDamageMultiplier)
82
83function NewDefClips(cvar, previous, new)
84 print("Default clip multiplier has changed. A server restart will be required for these changes to take effect.")
85end
86cvars.AddChangeCallback("M9KDefaultClip", NewDefClips)
87
88if GetConVar("M9KDefaultClip") == nil then
89 print("M9KDefaultClip is missing! You may have hit the lua limit!")
90else
91 if GetConVar("M9KDefaultClip"):GetInt() >= 0 then
92 print("M9K Weapons will now spawn with "..GetConVar("M9KDefaultClip"):GetFloat().." clips.")
93 else
94 print("Default clips will be not be modified")
95 end
96end
97
98SWEP.IronSightsPos = Vector (2.4537, 1.0923, 0.2696)
99SWEP.IronSightsAng = Vector (0.0186, -0.0547, 0)
100
101SWEP.VElements = {}
102SWEP.WElements = {}
103
104function SWEP:Initialize()
105 self.Reloadaftershoot = 0 -- Can't reload when firing
106 self:SetHoldType(self.HoldType)
107 self.OrigCrossHair = self.DrawCrosshair
108 if SERVER and self.Owner:IsNPC() then
109 self:SetNPCMinBurst(3)
110 self:SetNPCMaxBurst(10) -- None of this really matters but you need it here anyway
111 self:SetNPCFireRate(1/(self.Primary.RPM/60))
112 -- //self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_VERY_GOOD )
113 end
114
115 if CLIENT then
116
117 -- // Create a new table for every weapon instance
118 self.VElements = table.FullCopy( self.VElements )
119 self.WElements = table.FullCopy( self.WElements )
120 self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
121
122 self:CreateModels(self.VElements) -- create viewmodels
123 self:CreateModels(self.WElements) -- create worldmodels
124
125 -- // init view model bone build function
126 if IsValid(self.Owner) and self.Owner:IsPlayer() then
127 if self.Owner:Alive() then
128 local vm = self.Owner:GetViewModel()
129 if IsValid(vm) then
130 self:ResetBonePositions(vm)
131 -- // Init viewmodel visibility
132 if (self.ShowViewModel == nil or self.ShowViewModel) then
133 vm:SetColor(Color(255,255,255,255))
134 else
135 -- // 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
136 vm:SetMaterial("Debug/hsv")
137 end
138 end
139
140 end
141 end
142
143 end
144
145 if CLIENT then
146 local oldpath = "vgui/hud/name" -- the path goes here
147 local newpath = string.gsub(oldpath, "name", self.Gun)
148 self.WepSelectIcon = surface.GetTextureID(newpath)
149 end
150
151end
152
153function SWEP:Equip()
154 self:SetHoldType(self.HoldType)
155end
156
157function SWEP:Deploy()
158 self:SetIronsights(false, self.Owner) -- Set the ironsight false
159 self:SetHoldType(self.HoldType)
160
161 if self.Silenced then
162 self.Weapon:SendWeaponAnim( ACT_VM_DRAW_SILENCED )
163 else
164 self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
165 end
166
167 self.Weapon:SetNWBool("Reloading", false)
168
169 if !self.Owner:IsNPC() and self.Owner != nil then
170 if self.ResetSights and self.Owner:GetViewModel() != nil then
171 self.ResetSights = CurTime() + self.Owner:GetViewModel():SequenceDuration()
172 end
173 end
174 return true
175end
176
177function SWEP:Holster()
178
179 if CLIENT and IsValid(self.Owner) and not self.Owner:IsNPC() then
180 local vm = self.Owner:GetViewModel()
181 if IsValid(vm) then
182 self:ResetBonePositions(vm)
183 end
184 end
185
186 return true
187end
188
189function SWEP:OnRemove()
190
191 if CLIENT and IsValid(self.Owner) and not self.Owner:IsNPC() then
192 local vm = self.Owner:GetViewModel()
193 if IsValid(vm) then
194 self:ResetBonePositions(vm)
195 end
196 end
197
198end
199
200function SWEP:GetCapabilities()
201 return CAP_WEAPON_RANGE_ATTACK1, CAP_INNATE_RANGE_ATTACK1
202end
203
204function SWEP:Precache()
205 util.PrecacheSound(self.Primary.Sound)
206 util.PrecacheModel(self.ViewModel)
207 util.PrecacheModel(self.WorldModel)
208end
209
210function SWEP:PrimaryAttack()
211 OkaySoFar = true
212 if not IsValid(self) then
213 OkaySoFar = false
214 else if not IsValid(self.Weapon) then
215 OkaySoFar = false
216 else if not IsValid(self.Owner) then
217 OkaySoFar = false
218 end end end
219
220 if not OkaySoFar then return end
221
222 if self:CanPrimaryAttack() and self.Owner:IsPlayer() then
223 if !self.Owner:KeyDown(IN_SPEED) and !self.Owner:KeyDown(IN_RELOAD) then
224 self:ShootBulletInformation()
225 self.Weapon:TakePrimaryAmmo(1)
226
227 if self.Silenced then
228 self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK_SILENCED )
229 self.Weapon:EmitSound(self.Primary.SilencedSound)
230 else
231 self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
232 self.Weapon:EmitSound(self.Primary.Sound)
233 end
234
235 local fx = EffectData()
236 fx:SetEntity(self.Weapon)
237 fx:SetOrigin(self.Owner:GetShootPos())
238 fx:SetNormal(self.Owner:GetAimVector())
239 fx:SetAttachment(self.MuzzleAttachment)
240 if GetConVar("M9KGasEffect") != nil then
241 if GetConVar("M9KGasEffect"):GetBool() then
242 util.Effect("m9k_rg_muzzle_rifle",fx)
243 end
244 end
245 self.Owner:SetAnimation( PLAYER_ATTACK1 )
246 self.Owner:MuzzleFlash()
247 self.Weapon:SetNextPrimaryFire(CurTime()+1/(self.Primary.RPM/60))
248 self:CheckWeaponsAndAmmo()
249 self.RicochetCoin = (math.random(1,4))
250 if self.BoltAction then self:BoltBack() end
251 end
252 elseif self:CanPrimaryAttack() and self.Owner:IsNPC() then
253 self:ShootBulletInformation()
254 self.Weapon:TakePrimaryAmmo(1)
255 self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
256 self.Weapon:EmitSound(self.Primary.Sound)
257 self.Owner:SetAnimation( PLAYER_ATTACK1 )
258 self.Owner:MuzzleFlash()
259 self.Weapon:SetNextPrimaryFire(CurTime()+1/(self.Primary.RPM/60))
260 self.RicochetCoin = (math.random(1,4))
261 end
262end
263
264function SWEP:CheckWeaponsAndAmmo()
265 if SERVER and self.Weapon != nil and (GetConVar("M9KWeaponStrip"):GetBool()) then
266 if self.Weapon:Clip1() == 0 && self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() ) == 0 then
267 timer.Simple(.1, function() if SERVER then if not IsValid(self) then return end
268 if self.Owner == nil then return end
269 self.Owner:StripWeapon(self.Gun)
270 end end)
271 end
272 end
273end
274
275
276/*---------------------------------------------------------
277 Name: SWEP:ShootBulletInformation()
278 Desc: This func add the damage, the recoil, the number of shots and the cone on the bullet.
279-----------------------------------------------------*/
280function SWEP:ShootBulletInformation()
281
282 local CurrentDamage
283 local CurrentRecoil
284 local CurrentCone
285 local basedamage
286
287 if (self:GetIronsights() == true) and self.Owner:KeyDown(IN_ATTACK2) then
288 CurrentCone = self.Primary.IronAccuracy
289 else
290 CurrentCone = self.Primary.Spread
291 end
292 local damagedice = math.Rand(.85,1.3)
293
294 basedamage = PainMulti * self.Primary.Damage
295 CurrentDamage = basedamage * damagedice
296 CurrentRecoil = self.Primary.Recoil
297
298 -- //Player is aiming
299 if (self:GetIronsights() == true) and self.Owner:KeyDown(IN_ATTACK2) then
300 self:ShootBullet(CurrentDamage, CurrentRecoil / 6, self.Primary.NumShots, CurrentCone)
301 -- //Player is not aiming
302 else
303 if IsValid(self) then
304 if IsValid(self.Weapon) then
305 if IsValid(self.Owner) then
306 self:ShootBullet(CurrentDamage, CurrentRecoil, self.Primary.NumShots, CurrentCone)
307 end
308 end
309 end
310 end
311
312end
313
314/*---------------------------------------------------------
315 Name: SWEP:ShootBullet()
316 Desc: A convenience func to shoot bullets.
317-----------------------------------------------------*/
318local TracerName = "Tracer"
319
320function SWEP:ShootBullet(damage, recoil, num_bullets, aimcone)
321
322 num_bullets = num_bullets or 1
323 aimcone = aimcone or 0
324
325 self:ShootEffects()
326
327 if self.Tracer == 1 then
328 TracerName = "Ar2Tracer"
329 elseif self.Tracer == 2 then
330 TracerName = "AirboatGunHeavyTracer"
331 else
332 TracerName = "Tracer"
333 end
334
335 local bullet = {}
336 bullet.Num = num_bullets
337 bullet.Src = self.Owner:GetShootPos() -- Source
338 bullet.Dir = self.Owner:GetAimVector() -- Dir of bullet
339 bullet.Spread = Vector(aimcone, aimcone, 0) -- Aim Cone
340 bullet.Tracer = 3 -- Show a tracer on every x bullets
341 bullet.TracerName = TracerName
342 bullet.Force = damage * 0.25 -- Amount of force to give to phys objects
343 bullet.Damage = damage
344 bullet.Callback = function(attacker, tracedata, dmginfo)
345 return self:RicochetCallback(0, attacker, tracedata, dmginfo)
346 end
347 if IsValid(self) then
348 if IsValid(self.Weapon) then
349 if IsValid(self.Owner) then
350 self.Owner:FireBullets(bullet)
351 end
352 end
353 end
354 -- //if SERVER and !self.Owner:IsNPC() then
355 -- // local anglo = Angle(math.Rand(-self.Primary.KickDown,-self.Primary.KickUp), math.Rand(-self.Primary.KickHorizontal,self.Primary.KickHorizontal), 0)
356 -- // self.Owner:ViewPunch(anglo)
357
358 -- // local eyes = self.Owner:EyeAngles()
359 -- // eyes.pitch = eyes.pitch + anglo.pitch
360 -- // eyes.yaw = eyes.yaw + anglo.yaw
361 -- // if game.SinglePlayer() then self.Owner:SetEyeAngles(eyes) end
362 -- //end
363
364 local anglo1 = Angle(math.Rand(-self.Primary.KickDown,-self.Primary.KickUp), math.Rand(-self.Primary.KickHorizontal,self.Primary.KickHorizontal), 0)
365 self.Owner:ViewPunch(anglo1)
366
367 if SERVER and game.SinglePlayer() and !self.Owner:IsNPC() then
368 local offlineeyes = self.Owner:EyeAngles()
369 offlineeyes.pitch = offlineeyes.pitch + anglo1.pitch
370 offlineeyes.yaw = offlineeyes.yaw + anglo1.yaw
371 if GetConVar("M9KDynamicRecoil"):GetBool() then
372 self.Owner:SetEyeAngles(offlineeyes)
373 end
374 end
375
376 if CLIENT and !game.SinglePlayer() and !self.Owner:IsNPC() then
377 local anglo = Angle(math.Rand(-self.Primary.KickDown,-self.Primary.KickUp), math.Rand(-self.Primary.KickHorizontal,self.Primary.KickHorizontal), 0)
378
379 local eyes = self.Owner:EyeAngles()
380 eyes.pitch = eyes.pitch + (anglo.pitch/3)
381 eyes.yaw = eyes.yaw + (anglo.yaw/3)
382 if GetConVar("M9KDynamicRecoil"):GetBool() then
383 self.Owner:SetEyeAngles(eyes)
384 end
385 end
386
387end
388
389/*---------------------------------------------------------
390 Name: SWEP:RicochetCallback()
391-----------------------------------------------------*/
392
393function SWEP:RicochetCallback(bouncenum, attacker, tr, dmginfo)
394
395 if not IsFirstTimePredicted() then
396 return {damage = false, effects = false}
397 end
398
399 local PenetrationChecker = false
400
401 if GetConVar("M9KDisablePenetration") == nil then
402 PenetrationChecker = false
403 else
404 PenetrationChecker = GetConVar("M9KDisablePenetration"):GetBool()
405 end
406
407 if PenetrationChecker then return {damage = true, effects = DoDefaultEffect} end
408
409 bulletmiss = {}
410 bulletmiss[1]=Sound("weapons/fx/nearmiss/bulletLtoR03.wav")
411 bulletmiss[2]=Sound("weapons/fx/nearmiss/bulletLtoR04.wav")
412 bulletmiss[3]=Sound("weapons/fx/nearmiss/bulletLtoR06.wav")
413 bulletmiss[4]=Sound("weapons/fx/nearmiss/bulletLtoR07.wav")
414 bulletmiss[5]=Sound("weapons/fx/nearmiss/bulletLtoR09.wav")
415 bulletmiss[6]=Sound("weapons/fx/nearmiss/bulletLtoR10.wav")
416 bulletmiss[7]=Sound("weapons/fx/nearmiss/bulletLtoR13.wav")
417 bulletmiss[8]=Sound("weapons/fx/nearmiss/bulletLtoR14.wav")
418
419 local DoDefaultEffect = true
420 if (tr.HitSky) then return end
421
422 // -- Can we go through whatever we hit?
423 if (self.Penetration) and (self:BulletPenetrate(bouncenum, attacker, tr, dmginfo)) then
424 return {damage = true, effects = DoDefaultEffect}
425 end
426
427 // -- Your screen will shake and you'll hear the savage hiss of an approaching bullet which passing if someone is shooting at you.
428 if (tr.MatType != MAT_METAL) then
429 if (SERVER) then
430 util.ScreenShake(tr.HitPos, 5, 0.1, 0.5, 64)
431 sound.Play(table.Random(bulletmiss), tr.HitPos, 75, math.random(75,150), 1)
432 end
433
434 if self.Tracer == 0 or self.Tracer == 1 or self.Tracer == 2 then
435 local effectdata = EffectData()
436 effectdata:SetOrigin(tr.HitPos)
437 effectdata:SetNormal(tr.HitNormal)
438 effectdata:SetScale(20)
439 util.Effect("AR2Impact", effectdata)
440 elseif self.Tracer == 3 then
441 local effectdata = EffectData()
442 effectdata:SetOrigin(tr.HitPos)
443 effectdata:SetNormal(tr.HitNormal)
444 effectdata:SetScale(20)
445 util.Effect("StunstickImpact", effectdata)
446 end
447
448 return
449 end
450
451 if (self.Ricochet == false) then return {damage = true, effects = DoDefaultEffect} end
452
453 if self.Primary.Ammo == "SniperPenetratedRound" then -- .50 Ammo
454 self.MaxRicochet = 12
455 elseif self.Primary.Ammo == "pistol" then -- pistols
456 self.MaxRicochet = 2
457 elseif self.Primary.Ammo == "357" then -- revolvers with big ass bullets
458 self.MaxRicochet = 4
459 elseif self.Primary.Ammo == "smg1" then -- smgs
460 self.MaxRicochet = 5
461 elseif self.Primary.Ammo == "ar2" then -- assault rifles
462 self.MaxRicochet = 8
463 elseif self.Primary.Ammo == "buckshot" then -- shotguns
464 self.MaxRicochet = 1
465 elseif self.Primary.Ammo == "slam" then -- secondary shotguns
466 self.MaxRicochet = 1
467 elseif self.Primary.Ammo == "AirboatGun" then -- metal piercing shotgun pellet
468 self.MaxRicochet = 8
469 end
470
471 if (bouncenum > self.MaxRicochet) then return end
472
473 // -- Bounce vector
474 local trace = {}
475 trace.start = tr.HitPos
476 trace.endpos = trace.start + (tr.HitNormal * 16384)
477
478 local trace = util.TraceLine(trace)
479
480 local DotProduct = tr.HitNormal:Dot(tr.Normal * -1)
481
482 local ricochetbullet = {}
483 ricochetbullet.Num = 1
484 ricochetbullet.Src = tr.HitPos + (tr.HitNormal * 5)
485 ricochetbullet.Dir = ((2 * tr.HitNormal * DotProduct) + tr.Normal) + (VectorRand() * 0.05)
486 ricochetbullet.Spread = Vector(0, 0, 0)
487 ricochetbullet.Tracer = 1
488 ricochetbullet.TracerName = "m9k_effect_mad_ricochet_trace"
489 ricochetbullet.Force = dmginfo:GetDamage() * 0.15
490 ricochetbullet.Damage = dmginfo:GetDamage() * 0.5
491 ricochetbullet.Callback = function(a, b, c)
492 if (self.Ricochet) then
493 local impactnum
494 if tr.MatType == MAT_GLASS then impactnum = 0 else impactnum = 1 end
495 return self:RicochetCallback(bouncenum + impactnum, a, b, c) end
496 end
497
498 timer.Simple(0, function() attacker:FireBullets(ricochetbullet) end)
499
500 return {damage = true, effects = DoDefaultEffect}
501end
502
503
504/*---------------------------------------------------------
505 Name: SWEP:BulletPenetrate()
506-----------------------------------------------------*/
507function SWEP:BulletPenetrate(bouncenum, attacker, tr, paininfo)
508
509 local MaxPenetration
510
511 if self.Primary.Ammo == "SniperPenetratedRound" then -- .50 Ammo
512 MaxPenetration = 20
513 elseif self.Primary.Ammo == "pistol" then -- pistols
514 MaxPenetration = 9
515 elseif self.Primary.Ammo == "357" then -- revolvers with big ass bullets
516 MaxPenetration = 12
517 elseif self.Primary.Ammo == "smg1" then -- smgs
518 MaxPenetration = 14
519 elseif self.Primary.Ammo == "ar2" then -- assault rifles
520 MaxPenetration = 16
521 elseif self.Primary.Ammo == "buckshot" then -- shotguns
522 MaxPenetration = 5
523 elseif self.Primary.Ammo == "slam" then -- secondary shotguns
524 MaxPenetration = 5
525 elseif self.Primary.Ammo == "AirboatGun" then -- metal piercing shotgun pellet
526 MaxPenetration = 17
527 else
528 MaxPenetration = 14
529 end
530
531 local DoDefaultEffect = true
532 // -- Don't go through metal, sand or player
533
534 if self.Primary.Ammo == "pistol" or
535 self.Primary.Ammo == "buckshot" or
536 self.Primary.Ammo == "slam" then self.Ricochet = true
537 else
538 if self.RicochetCoin == 1 then
539 self.Ricochet = true
540 elseif self.RicochetCoin >= 2 then
541 self.Ricochet = false
542 end
543 end
544
545 if self.Primary.Ammo == "SniperPenetratedRound" then self.Ricochet = true end
546
547 if self.Primary.Ammo == "SniperPenetratedRound" then -- .50 Ammo
548 self.MaxRicochet = 10
549 elseif self.Primary.Ammo == "pistol" then -- pistols
550 self.MaxRicochet = 2
551 elseif self.Primary.Ammo == "357" then -- revolvers with big ass bullets
552 self.MaxRicochet = 5
553 elseif self.Primary.Ammo == "smg1" then -- smgs
554 self.MaxRicochet = 4
555 elseif self.Primary.Ammo == "ar2" then -- assault rifles
556 self.MaxRicochet = 5
557 elseif self.Primary.Ammo == "buckshot" then -- shotguns
558 self.MaxRicochet = 0
559 elseif self.Primary.Ammo == "slam" then -- secondary shotguns
560 self.MaxRicochet = 0
561 elseif self.Primary.Ammo == "AirboatGun" then -- metal piercing shotgun pellet
562 self.MaxRicochet = 8
563 end
564
565 if (tr.MatType == MAT_METAL and self.Ricochet == true and self.Primary.Ammo != "SniperPenetratedRound" ) then return false end
566
567 // -- Don't go through more than 3 times
568 if (bouncenum > self.MaxRicochet) then return false end
569
570 // -- Direction (and length) that we are going to penetrate
571 local PenetrationDirection = tr.Normal * MaxPenetration
572
573 if (tr.MatType == MAT_GLASS or tr.MatType == MAT_PLASTIC or tr.MatType == MAT_WOOD or tr.MatType == MAT_FLESH or tr.MatType == MAT_ALIENFLESH) then
574 PenetrationDirection = tr.Normal * (MaxPenetration * 2)
575 end
576
577 local trace = {}
578 trace.endpos = tr.HitPos
579 trace.start = tr.HitPos + PenetrationDirection
580 trace.mask = MASK_SHOT
581 trace.filter = {self.Owner}
582
583 local trace = util.TraceLine(trace)
584
585 // -- Bullet didn't penetrate.
586 if (trace.StartSolid or trace.Fraction >= 1.0 or tr.Fraction <= 0.0) then return false end
587
588 // -- Damage multiplier depending on surface
589 local fDamageMulti = 0.5
590
591 if self.Primary.Ammo == "SniperPenetratedRound" then
592 fDamageMulti = 1
593 elseif(tr.MatType == MAT_CONCRETE or tr.MatType == MAT_METAL) then
594 fDamageMulti = 0.3
595 elseif (tr.MatType == MAT_WOOD or tr.MatType == MAT_PLASTIC or tr.MatType == MAT_GLASS) then
596 fDamageMulti = 0.8
597 elseif (tr.MatType == MAT_FLESH or tr.MatType == MAT_ALIENFLESH) then
598 fDamageMulti = 0.9
599 end
600
601 local damagedice = math.Rand(.85,1.3)
602 local newdamage = self.Primary.Damage * damagedice
603
604 // -- Fire bullet from the exit point using the original trajectory
605 local penetratedbullet = {}
606 penetratedbullet.Num = 1
607 penetratedbullet.Src = trace.HitPos
608 penetratedbullet.Dir = tr.Normal
609 penetratedbullet.Spread = Vector(0, 0, 0)
610 penetratedbullet.Tracer = 2
611 penetratedbullet.TracerName = "m9k_effect_mad_penetration_trace"
612 penetratedbullet.Force = 5
613 penetratedbullet.Damage = paininfo:GetDamage() * fDamageMulti
614 penetratedbullet.Callback = function(a, b, c) if (self.Ricochet) then
615 local impactnum
616 if tr.MatType == MAT_GLASS then impactnum = 0 else impactnum = 1 end
617 return self:RicochetCallback(bouncenum + impactnum, a,b,c) end end
618
619 timer.Simple(0, function() if attacker != nil then attacker:FireBullets(penetratedbullet) end end)
620
621 return true
622end
623
624
625function SWEP:SecondaryAttack()
626 return false
627end
628
629function SWEP:Reload()
630 if not IsValid(self) then return end if not IsValid(self.Owner) then return end
631
632 if self.Owner:IsNPC() then
633 self.Weapon:DefaultReload(ACT_VM_RELOAD)
634 return end
635
636 if self.Owner:KeyDown(IN_USE) then return end
637
638 if self.Silenced then
639 self.Weapon:DefaultReload(ACT_VM_RELOAD_SILENCED)
640 else
641 self.Weapon:DefaultReload(ACT_VM_RELOAD)
642 end
643
644 if !self.Owner:IsNPC() then
645 if self.Owner:GetViewModel() == nil then self.ResetSights = CurTime() + 3 else
646 self.ResetSights = CurTime() + self.Owner:GetViewModel():SequenceDuration()
647 end
648 end
649
650 if SERVER and self.Weapon != nil then
651 if ( self.Weapon:Clip1() < self.Primary.ClipSize ) and !self.Owner:IsNPC() then
652 -- //When the current clip < full clip and the rest of your ammo > 0, then
653 self.Owner:SetFOV( 0, 0.3 )
654 -- //Zoom = 0
655 self:SetIronsights(false)
656 -- //Set the ironsight to false
657 self.Weapon:SetNWBool("Reloading", true)
658 end
659 local waitdammit = (self.Owner:GetViewModel():SequenceDuration())
660 timer.Simple(waitdammit + .1,
661 function()
662 if self.Weapon == nil then return end
663 self.Weapon:SetNWBool("Reloading", false)
664 if self.Owner:KeyDown(IN_ATTACK2) and self.Weapon:GetClass() == self.Gun then
665 if CLIENT then return end
666 if self.Scoped == false then
667 self.Owner:SetFOV( self.Secondary.IronFOV, 0.3 )
668 self.IronSightsPos = self.SightsPos -- Bring it up
669 self.IronSightsAng = self.SightsAng -- Bring it up
670 self:SetIronsights(true, self.Owner)
671 self.DrawCrosshair = false
672 else return end
673 elseif self.Owner:KeyDown(IN_SPEED) and self.Weapon:GetClass() == self.Gun then
674 if self.Weapon:GetNextPrimaryFire() <= (CurTime() + .03) then
675 self.Weapon:SetNextPrimaryFire(CurTime()+0.3) -- Make it so you can't shoot for another quarter second
676 end
677 self.IronSightsPos = self.RunSightsPos -- Hold it down
678 self.IronSightsAng = self.RunSightsAng -- Hold it down
679 self:SetIronsights(true, self.Owner) -- Set the ironsight true
680 self.Owner:SetFOV( 0, 0.3 )
681 else return end
682 end)
683 end
684end
685
686function SWEP:PostReloadScopeCheck()
687 if self.Weapon == nil then return end
688 self.Weapon:SetNWBool("Reloading", false)
689 if self.Owner:KeyDown(IN_ATTACK2) and self.Weapon:GetClass() == self.Gun then
690 if CLIENT then return end
691 if self.Scoped == false then
692 self.Owner:SetFOV( self.Secondary.IronFOV, 0.3 )
693 self.IronSightsPos = self.SightsPos -- Bring it up
694 self.IronSightsAng = self.SightsAng -- Bring it up
695 self:SetIronsights(true, self.Owner)
696 self.DrawCrosshair = false
697 else return end
698 elseif self.Owner:KeyDown(IN_SPEED) and self.Weapon:GetClass() == self.Gun then
699 if self.Weapon:GetNextPrimaryFire() <= (CurTime() + .03) then
700 self.Weapon:SetNextPrimaryFire(CurTime()+0.3) -- Make it so you can't shoot for another quarter second
701 end
702 self.IronSightsPos = self.RunSightsPos -- Hold it down
703 self.IronSightsAng = self.RunSightsAng -- Hold it down
704 self:SetIronsights(true, self.Owner) -- Set the ironsight true
705 self.Owner:SetFOV( 0, 0.3 )
706 else return end
707end
708
709function SWEP:Silencer()
710
711 if self.NextSilence > CurTime() then return end
712
713 if self.Weapon != nil then
714 self.Owner:SetFOV( 0, 0.3 )
715 self:SetIronsights(false)
716 self.Weapon:SetNWBool("Reloading", true) -- i know we're not reloading but it works
717 end
718
719 if self.Silenced then
720 self:SendWeaponAnim(ACT_VM_DETACH_SILENCER)
721 self.Silenced = false
722 elseif not self.Silenced then
723 self:SendWeaponAnim(ACT_VM_ATTACH_SILENCER)
724 self.Silenced = true
725 end
726
727 siltimer = CurTime() + (self.Owner:GetViewModel():SequenceDuration()) + 0.1
728 if self.Weapon:GetNextPrimaryFire() <= siltimer then
729 self.Weapon:SetNextPrimaryFire(siltimer)
730 end
731 self.NextSilence = siltimer
732
733 timer.Simple( ((self.Owner:GetViewModel():SequenceDuration()) + 0.1),
734 function()
735 if self.Weapon != nil then
736 self.Weapon:SetNWBool("Reloading", false)
737 if self.Owner:KeyDown(IN_ATTACK2) and self.Weapon:GetClass() == self.Gun then
738 if CLIENT then return end
739 if self.Scoped == false then
740 self.Owner:SetFOV( self.Secondary.IronFOV, 0.3 )
741 self.IronSightsPos = self.SightsPos -- Bring it up
742 self.IronSightsAng = self.SightsAng -- Bring it up
743 self:SetIronsights(true, self.Owner)
744 self.DrawCrosshair = false
745 else return end
746 elseif self.Owner:KeyDown(IN_SPEED) and self.Weapon:GetClass() == self.Gun then
747 if self.Weapon:GetNextPrimaryFire() <= (CurTime()+0.3) then
748 self.Weapon:SetNextPrimaryFire(CurTime()+0.3) -- Make it so you can't shoot for another quarter second
749 end
750 self.IronSightsPos = self.RunSightsPos -- Hold it down
751 self.IronSightsAng = self.RunSightsAng -- Hold it down
752 self:SetIronsights(true, self.Owner) -- Set the ironsight true
753 self.Owner:SetFOV( 0, 0.3 )
754 else return end
755 end
756 end)
757
758end
759
760function SWEP:SelectFireMode()
761
762 if self.Primary.Automatic then
763 self.Primary.Automatic = false
764 self.NextFireSelect = CurTime() + .5
765 if CLIENT then
766 self.Owner:PrintMessage(HUD_PRINTTALK, "Semi-automatic selected.")
767 end
768 self.Weapon:EmitSound("Weapon_AR2.Empty")
769 else
770 self.Primary.Automatic = true
771 self.NextFireSelect = CurTime() + .5
772 if CLIENT then
773 self.Owner:PrintMessage(HUD_PRINTTALK, "Automatic selected.")
774 end
775 self.Weapon:EmitSound("Weapon_AR2.Empty")
776 end
777end
778
779
780/*---------------------------------------------------------
781IronSight
782-----------------------------------------------------*/
783function SWEP:IronSight()
784
785 if not IsValid(self) then return end
786 if not IsValid(self.Owner) then return end
787
788 if !self.Owner:IsNPC() then
789 if self.ResetSights and CurTime() >= self.ResetSights then
790 self.ResetSights = nil
791
792 if self.Silenced then
793 self:SendWeaponAnim(ACT_VM_IDLE_SILENCED)
794 else
795 self:SendWeaponAnim(ACT_VM_IDLE)
796 end
797 end end
798
799 if self.CanBeSilenced and self.NextSilence < CurTime() then
800 if self.Owner:KeyDown(IN_USE) and self.Owner:KeyPressed(IN_ATTACK2) then
801 self:Silencer()
802 end
803 end
804
805 if self.SelectiveFire and self.NextFireSelect < CurTime() and not (self.Weapon:GetNWBool("Reloading")) then
806 if self.Owner:KeyDown(IN_USE) and self.Owner:KeyPressed(IN_RELOAD) then
807 self:SelectFireMode()
808 end
809 end
810
811-- //copy this...
812 if self.Owner:KeyPressed(IN_SPEED) and not (self.Weapon:GetNWBool("Reloading")) then -- If you are running
813 if self.Weapon:GetNextPrimaryFire() <= (CurTime()+0.3) then
814 self.Weapon:SetNextPrimaryFire(CurTime()+0.3) -- Make it so you can't shoot for another quarter second
815 end
816 self.IronSightsPos = self.RunSightsPos -- Hold it down
817 self.IronSightsAng = self.RunSightsAng -- Hold it down
818 self:SetIronsights(true, self.Owner) -- Set the ironsight true
819 self.Owner:SetFOV( 0, 0.3 )
820 self.DrawCrosshair = false
821 end
822
823 if self.Owner:KeyReleased (IN_SPEED) then -- If you release run then
824 self:SetIronsights(false, self.Owner) -- Set the ironsight true
825 self.Owner:SetFOV( 0, 0.3 )
826 self.DrawCrosshair = self.OrigCrossHair
827 end -- Shoulder the gun
828
829-- //down to this
830 if !self.Owner:KeyDown(IN_USE) and !self.Owner:KeyDown(IN_SPEED) then
831 -- //If the key E (Use Key) is not pressed, then
832
833 if self.Owner:KeyPressed(IN_ATTACK2) and not (self.Weapon:GetNWBool("Reloading")) then
834 self.Owner:SetFOV( self.Secondary.IronFOV, 0.3 )
835 self.IronSightsPos = self.SightsPos -- Bring it up
836 self.IronSightsAng = self.SightsAng -- Bring it up
837 self:SetIronsights(true, self.Owner)
838 self.DrawCrosshair = false
839 -- //Set the ironsight true
840
841 if CLIENT then return end
842 end
843 end
844
845 if self.Owner:KeyReleased(IN_ATTACK2) and !self.Owner:KeyDown(IN_USE) and !self.Owner:KeyDown(IN_SPEED) then
846 -- //If the right click is released, then
847 self.Owner:SetFOV( 0, 0.3 )
848 self.DrawCrosshair = self.OrigCrossHair
849 self:SetIronsights(false, self.Owner)
850 -- //Set the ironsight false
851
852 if CLIENT then return end
853 end
854
855 if self.Owner:KeyDown(IN_ATTACK2) and !self.Owner:KeyDown(IN_USE) and !self.Owner:KeyDown(IN_SPEED) then
856 self.SwayScale = 0.05
857 self.BobScale = 0.05
858 else
859 self.SwayScale = 1.0
860 self.BobScale = 1.0
861 end
862end
863
864/*---------------------------------------------------------
865Think
866-----------------------------------------------------*/
867function SWEP:Think()
868
869self:IronSight()
870
871end
872
873/*---------------------------------------------------------
874GetViewModelPosition
875-----------------------------------------------------*/
876local IRONSIGHT_TIME = 0.3
877-- //Time to enter in the ironsight mod
878
879function SWEP:GetViewModelPosition(pos, ang)
880
881 if (not self.IronSightsPos) then return pos, ang end
882
883 local bIron = self.Weapon:GetNWBool("M9K_Ironsights")
884
885 if (bIron != self.bLastIron) then
886 self.bLastIron = bIron
887 self.fIronTime = CurTime()
888
889 end
890
891 local fIronTime = self.fIronTime or 0
892
893 if (not bIron and fIronTime < CurTime() - IRONSIGHT_TIME) then
894 return pos, ang
895 end
896
897 local Mul = 1.0
898
899 if (fIronTime > CurTime() - IRONSIGHT_TIME) then
900 Mul = math.Clamp((CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1)
901
902 if not bIron then Mul = 1 - Mul end
903 end
904
905 local Offset = self.IronSightsPos
906
907 if (self.IronSightsAng) then
908 ang = ang * 1
909 ang:RotateAroundAxis(ang:Right(), self.IronSightsAng.x * Mul)
910 ang:RotateAroundAxis(ang:Up(), self.IronSightsAng.y * Mul)
911 ang:RotateAroundAxis(ang:Forward(), self.IronSightsAng.z * Mul)
912 end
913
914 local Right = ang:Right()
915 local Up = ang:Up()
916 local Forward = ang:Forward()
917
918 pos = pos + Offset.x * Right * Mul
919 pos = pos + Offset.y * Forward * Mul
920 pos = pos + Offset.z * Up * Mul
921
922 return pos, ang
923end
924
925/*---------------------------------------------------------
926SetIronsights
927-----------------------------------------------------*/
928function SWEP:SetIronsights(b)
929 self.Weapon:SetNWBool("M9K_Ironsights", b)
930end
931
932function SWEP:GetIronsights()
933 return self.Weapon:GetNWBool("M9K_Ironsights")
934end
935
936
937if CLIENT then
938
939 SWEP.vRenderOrder = nil
940 function SWEP:ViewModelDrawn()
941
942 if not IsValid(self) then return end
943 if not IsValid(self.Owner) then return end
944 local vm = self.Owner:GetViewModel()
945 if !IsValid(vm) then return end
946
947 if (!self.VElements) then return end
948
949 self:UpdateBonePositions(vm)
950
951 if (!self.vRenderOrder) then
952
953 -- // we build a render order because sprites need to be drawn after models
954 self.vRenderOrder = {}
955
956 for k, v in pairs( self.VElements ) do
957 if (v.type == "Model") then
958 table.insert(self.vRenderOrder, 1, k)
959 elseif (v.type == "Sprite" or v.type == "Quad") then
960 table.insert(self.vRenderOrder, k)
961 end
962 end
963
964 end
965
966 for k, name in ipairs( self.vRenderOrder ) do
967
968 local v = self.VElements[name]
969 if (!v) then self.vRenderOrder = nil break end
970 if (v.hide) then continue end
971
972 local model = v.modelEnt
973 local sprite = v.spriteMaterial
974
975 if (!v.bone) then continue end
976
977 local pos, ang = self:GetBoneOrientation( self.VElements, v, vm )
978
979 if (!pos) then continue end
980
981 if (v.type == "Model" and IsValid(model)) then
982
983 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
984 ang:RotateAroundAxis(ang:Up(), v.angle.y)
985 ang:RotateAroundAxis(ang:Right(), v.angle.p)
986 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
987
988 model:SetAngles(ang)
989 -- //model:SetModelScale(v.size)
990 local matrix = Matrix()
991 matrix:Scale(v.size)
992 model:EnableMatrix( "RenderMultiply", matrix )
993
994 if (v.material == "") then
995 model:SetMaterial("")
996 elseif (model:GetMaterial() != v.material) then
997 model:SetMaterial( v.material )
998 end
999
1000 if (v.skin and v.skin != model:GetSkin()) then
1001 model:SetSkin(v.skin)
1002 end
1003
1004 if (v.bodygroup) then
1005 for k, v in pairs( v.bodygroup ) do
1006 if (model:GetBodygroup(k) != v) then
1007 model:SetBodygroup(k, v)
1008 end
1009 end
1010 end
1011
1012 if (v.surpresslightning) then
1013 render.SuppressEngineLighting(true)
1014 end
1015
1016 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
1017 render.SetBlend(v.color.a/255)
1018 model:DrawModel()
1019 render.SetBlend(1)
1020 render.SetColorModulation(1, 1, 1)
1021
1022 if (v.surpresslightning) then
1023 render.SuppressEngineLighting(false)
1024 end
1025
1026 elseif (v.type == "Sprite" and sprite) then
1027
1028 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
1029 render.SetMaterial(sprite)
1030 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
1031
1032 elseif (v.type == "Quad" and v.draw_func) then
1033
1034 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
1035 ang:RotateAroundAxis(ang:Up(), v.angle.y)
1036 ang:RotateAroundAxis(ang:Right(), v.angle.p)
1037 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
1038
1039 cam.Start3D2D(drawpos, ang, v.size)
1040 v.draw_func( self )
1041 cam.End3D2D()
1042
1043 end
1044
1045 end
1046
1047 end
1048
1049 SWEP.wRenderOrder = nil
1050 function SWEP:DrawWorldModel()
1051
1052 if (self.ShowWorldModel == nil or self.ShowWorldModel) then
1053 self:DrawModel()
1054 end
1055
1056 if (!self.WElements) then return end
1057
1058 if (!self.wRenderOrder) then
1059
1060 self.wRenderOrder = {}
1061
1062 for k, v in pairs( self.WElements ) do
1063 if (v.type == "Model") then
1064 table.insert(self.wRenderOrder, 1, k)
1065 elseif (v.type == "Sprite" or v.type == "Quad") then
1066 table.insert(self.wRenderOrder, k)
1067 end
1068 end
1069
1070 end
1071
1072 if (IsValid(self.Owner)) then
1073 bone_ent = self.Owner
1074 else
1075 -- // when the weapon is dropped
1076 bone_ent = self
1077 end
1078
1079 for k, name in pairs( self.wRenderOrder ) do
1080
1081 local v = self.WElements[name]
1082 if (!v) then self.wRenderOrder = nil break end
1083 if (v.hide) then continue end
1084
1085 local pos, ang
1086
1087 if (v.bone) then
1088 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent )
1089 else
1090 pos, ang = self:GetBoneOrientation( self.WElements, v, bone_ent, "ValveBiped.Bip01_R_Hand" )
1091 end
1092
1093 if (!pos) then continue end
1094
1095 local model = v.modelEnt
1096 local sprite = v.spriteMaterial
1097
1098 if (v.type == "Model" and IsValid(model)) then
1099
1100 model:SetPos(pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z )
1101 ang:RotateAroundAxis(ang:Up(), v.angle.y)
1102 ang:RotateAroundAxis(ang:Right(), v.angle.p)
1103 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
1104
1105 model:SetAngles(ang)
1106 -- //model:SetModelScale(v.size)
1107 local matrix = Matrix()
1108 matrix:Scale(v.size)
1109 model:EnableMatrix( "RenderMultiply", matrix )
1110
1111 if (v.material == "") then
1112 model:SetMaterial("")
1113 elseif (model:GetMaterial() != v.material) then
1114 model:SetMaterial( v.material )
1115 end
1116
1117 if (v.skin and v.skin != model:GetSkin()) then
1118 model:SetSkin(v.skin)
1119 end
1120
1121 if (v.bodygroup) then
1122 for k, v in pairs( v.bodygroup ) do
1123 if (model:GetBodygroup(k) != v) then
1124 model:SetBodygroup(k, v)
1125 end
1126 end
1127 end
1128
1129 if (v.surpresslightning) then
1130 render.SuppressEngineLighting(true)
1131 end
1132
1133 render.SetColorModulation(v.color.r/255, v.color.g/255, v.color.b/255)
1134 render.SetBlend(v.color.a/255)
1135 model:DrawModel()
1136 render.SetBlend(1)
1137 render.SetColorModulation(1, 1, 1)
1138
1139 if (v.surpresslightning) then
1140 render.SuppressEngineLighting(false)
1141 end
1142
1143 elseif (v.type == "Sprite" and sprite) then
1144
1145 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
1146 render.SetMaterial(sprite)
1147 render.DrawSprite(drawpos, v.size.x, v.size.y, v.color)
1148
1149 elseif (v.type == "Quad" and v.draw_func) then
1150
1151 local drawpos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
1152 ang:RotateAroundAxis(ang:Up(), v.angle.y)
1153 ang:RotateAroundAxis(ang:Right(), v.angle.p)
1154 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
1155
1156 cam.Start3D2D(drawpos, ang, v.size)
1157 v.draw_func( self )
1158 cam.End3D2D()
1159
1160 end
1161
1162 end
1163
1164 end
1165
1166 function SWEP:GetBoneOrientation( basetab, tab, ent, bone_override )
1167
1168 local bone, pos, ang
1169 if (tab.rel and tab.rel != "") then
1170
1171 local v = basetab[tab.rel]
1172
1173 if (!v) then return end
1174
1175 -- // Technically, if there exists an element with the same name as a bone
1176 -- // you can get in an infinite loop. Let's just hope nobody's that stupid.
1177 pos, ang = self:GetBoneOrientation( basetab, v, ent )
1178
1179 if (!pos) then return end
1180
1181 pos = pos + ang:Forward() * v.pos.x + ang:Right() * v.pos.y + ang:Up() * v.pos.z
1182 ang:RotateAroundAxis(ang:Up(), v.angle.y)
1183 ang:RotateAroundAxis(ang:Right(), v.angle.p)
1184 ang:RotateAroundAxis(ang:Forward(), v.angle.r)
1185
1186 else
1187
1188 bone = ent:LookupBone(bone_override or tab.bone)
1189
1190 if (!bone) then return end
1191
1192 pos, ang = Vector(0,0,0), Angle(0,0,0)
1193 local m = ent:GetBoneMatrix(bone)
1194 if (m) then
1195 pos, ang = m:GetTranslation(), m:GetAngles()
1196 end
1197
1198 if (IsValid(self.Owner) and self.Owner:IsPlayer() and
1199 ent == self.Owner:GetViewModel() and self.ViewModelFlip) then
1200 ang.r = -ang.r --// Fixes mirrored models
1201 end
1202
1203 end
1204
1205 return pos, ang
1206 end
1207
1208 function SWEP:CreateModels( tab )
1209
1210 if (!tab) then return end
1211
1212 -- // Create the clientside models here because Garry says we can't do it in the render hook
1213 for k, v in pairs( tab ) do
1214 if (v.type == "Model" and v.model and v.model != "" and (!IsValid(v.modelEnt) or v.createdModel != v.model) and
1215 string.find(v.model, ".mdl") and file.Exists (v.model, "GAME") ) then
1216
1217 v.modelEnt = ClientsideModel(v.model, RENDER_GROUP_VIEW_MODEL_OPAQUE)
1218 if (IsValid(v.modelEnt)) then
1219 v.modelEnt:SetPos(self:GetPos())
1220 v.modelEnt:SetAngles(self:GetAngles())
1221 v.modelEnt:SetParent(self)
1222 v.modelEnt:SetNoDraw(true)
1223 v.createdModel = v.model
1224 else
1225 v.modelEnt = nil
1226 end
1227
1228 elseif (v.type == "Sprite" and v.sprite and v.sprite != "" and (!v.spriteMaterial or v.createdSprite != v.sprite)
1229 and file.Exists ("materials/"..v.sprite..".vmt", "GAME")) then
1230
1231 local name = v.sprite.."-"
1232 local params = { ["$basetexture"] = v.sprite }
1233 -- // make sure we create a unique name based on the selected options
1234 local tocheck = { "nocull", "additive", "vertexalpha", "vertexcolor", "ignorez" }
1235 for i, j in pairs( tocheck ) do
1236 if (v[j]) then
1237 params["$"..j] = 1
1238 name = name.."1"
1239 else
1240 name = name.."0"
1241 end
1242 end
1243
1244 v.createdSprite = v.sprite
1245 v.spriteMaterial = CreateMaterial(name,"UnlitGeneric",params)
1246
1247 end
1248 end
1249
1250 end
1251
1252 local allbones
1253 local hasGarryFixedBoneScalingYet = false
1254
1255 function SWEP:UpdateBonePositions(vm)
1256
1257 if self.ViewModelBoneMods then
1258
1259 if (!vm:GetBoneCount()) then return end
1260
1261 -- // !! WORKAROUND !! --//
1262 -- // We need to check all model names :/
1263 local loopthrough = self.ViewModelBoneMods
1264 if (!hasGarryFixedBoneScalingYet) then
1265 allbones = {}
1266 for i=0, vm:GetBoneCount() do
1267 local bonename = vm:GetBoneName(i)
1268 if (self.ViewModelBoneMods[bonename]) then
1269 allbones[bonename] = self.ViewModelBoneMods[bonename]
1270 else
1271 allbones[bonename] = {
1272 scale = Vector(1,1,1),
1273 pos = Vector(0,0,0),
1274 angle = Angle(0,0,0)
1275 }
1276 end
1277 end
1278
1279 loopthrough = allbones
1280 end
1281 //!! ----------- !! --
1282
1283 for k, v in pairs( loopthrough ) do
1284 local bone = vm:LookupBone(k)
1285 if (!bone) then continue end
1286
1287 -- // !! WORKAROUND !! --//
1288 local s = Vector(v.scale.x,v.scale.y,v.scale.z)
1289 local p = Vector(v.pos.x,v.pos.y,v.pos.z)
1290 local ms = Vector(1,1,1)
1291 if (!hasGarryFixedBoneScalingYet) then
1292 local cur = vm:GetBoneParent(bone)
1293 while(cur >= 0) do
1294 local pscale = loopthrough[vm:GetBoneName(cur)].scale
1295 ms = ms * pscale
1296 cur = vm:GetBoneParent(cur)
1297 end
1298 end
1299
1300 s = s * ms
1301 //!! ----------- !! --
1302
1303 if vm:GetManipulateBoneScale(bone) != s then
1304 vm:ManipulateBoneScale( bone, s )
1305 end
1306 if vm:GetManipulateBoneAngles(bone) != v.angle then
1307 vm:ManipulateBoneAngles( bone, v.angle )
1308 end
1309 if vm:GetManipulateBonePosition(bone) != p then
1310 vm:ManipulateBonePosition( bone, p )
1311 end
1312 end
1313 else
1314 self:ResetBonePositions(vm)
1315 end
1316
1317 end
1318
1319 function SWEP:ResetBonePositions(vm)
1320
1321 if (!vm:GetBoneCount()) then return end
1322 for i=0, vm:GetBoneCount() do
1323 vm:ManipulateBoneScale( i, Vector(1, 1, 1) )
1324 vm:ManipulateBoneAngles( i, Angle(0, 0, 0) )
1325 vm:ManipulateBonePosition( i, Vector(0, 0, 0) )
1326 end
1327
1328 end
1329
1330 /**************************
1331 Global utility code
1332 **************************/
1333
1334 -- // Fully copies the table, meaning all tables inside this table are copied too and so on (normal table.Copy copies only their reference).
1335 -- // Does not copy entities of course, only copies their reference.
1336 -- // WARNING: do not use on tables that contain themselves somewhere down the line or you'll get an infinite loop
1337 function table.FullCopy( tab )
1338
1339 if (!tab) then return nil end
1340
1341 local res = {}
1342 for k, v in pairs( tab ) do
1343 if (type(v) == "table") then
1344 res[k] = table.FullCopy(v) --// recursion ho!
1345 elseif (type(v) == "Vector") then
1346 res[k] = Vector(v.x, v.y, v.z)
1347 elseif (type(v) == "Angle") then
1348 res[k] = Angle(v.p, v.y, v.r)
1349 else
1350 res[k] = v
1351 end
1352 end
1353
1354 return res
1355
1356 end
1357
1358end