summaryrefslogtreecommitdiff
path: root/entities/weapons/rad_base/shared.lua
blob: 684572f71827f4863391fedbffa963a83a89892b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
if SERVER then

	AddCSLuaFile( "shared.lua" )

	SWEP.Weight				= 1
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if CLIENT then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.CSMuzzleFlashes	= true

	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= true

	SWEP.PrintName = "BASE WEAPON"
	SWEP.IconLetter = "c"
	SWEP.Slot = 0
	SWEP.Slotpos = 0

	SWEP.IconFont = "CSSelectIcons"

	function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
		//draw.SimpleText( self.IconLetter, self.IconFont, x + wide/2, y + tall/2.5, Color( 15, 20, 200, 255 ), TEXT_ALIGN_CENTER )
	end

end

SWEP.HoldType = "pistol"

SWEP.ViewModel	= "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"

SWEP.SprintPos = Vector(0,0,0)
SWEP.SprintAng = Vector(0,0,0)

SWEP.ZoomModes = { 0, 50, 10 }
SWEP.ZoomSpeeds = { 5, 5, 5 }

SWEP.IsSniper = false
SWEP.AmmoType = "SMG"

SWEP.Primary.Empty          = Sound( "weapons/clipempty_rifle.wav" )
SWEP.Primary.Sound			= Sound( "Weapon_USP.Single" )
SWEP.Primary.Recoil			= 3.5
SWEP.Primary.Damage			= 10
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.025
SWEP.Primary.SniperCone     = 0.025
SWEP.Primary.Delay			= 0.150

SWEP.Primary.Ammo			= "Pistol"
SWEP.Primary.ClipSize		= 50
SWEP.Primary.DefaultClip	= 200
SWEP.Primary.Automatic		= false

SWEP.Secondary.Sound        = Sound( "weapons/zoom.wav" )
SWEP.Secondary.Laser        = Sound( "weapons/ar2/ar2_empty.wav" )
SWEP.Secondary.Delay  		= 0.5

SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false

SWEP.LaserOffset = Vector(0,0,0)
SWEP.HolsterMode = false
SWEP.HolsterTime = 0
SWEP.LastRunFrame = 0

SWEP.MinShellDelay = 0.3
SWEP.MaxShellDelay = 0.6

SWEP.FalloffDistances = {}
SWEP.FalloffDistances[ "Sniper" ] = { Range = 3500, Falloff = 8000 }
SWEP.FalloffDistances[ "Rifle" ] = { Range = 2000, Falloff = 2000 }
SWEP.FalloffDistances[ "SMG" ] = { Range = 1500, Falloff = 1500 }
SWEP.FalloffDistances[ "Pistol" ] = { Range = 1000, Falloff = 500 }
SWEP.FalloffDistances[ "Buckshot" ] = { Range = 300, Falloff = 500 }

SWEP.UseShellSounds = true
SWEP.ShellSounds = { "player/pl_shell1.wav", "player/pl_shell2.wav", "player/pl_shell3.wav" }
SWEP.BuckshotShellSounds = { "weapons/fx/tink/shotgun_shell1.wav", "weapons/fx/tink/shotgun_shell2.wav", "weapons/fx/tink/shotgun_shell3.wav" }

SWEP.Pitches = {}
SWEP.Pitches[ "Pistol" ] = 100
SWEP.Pitches[ "SMG" ] = 90
SWEP.Pitches[ "Rifle" ] = 80
SWEP.Pitches[ "Sniper" ] = 70
SWEP.Pitches[ "Buckshot" ] = 100

function SWEP:GetViewModelPosition( pos, ang )

    local newpos = nil
    local newang = nil
    local movetime = 0.25
    local duration = 0//self.Weapon:GetNWInt( "ViewTime", 0 )

    local pang = self.Owner:EyeAngles()
    local vel = self.Owner:GetVelocity()

    //if not newpos or not newang then

       // newpos = pos
       // newang = ang

    //end

    local mul = 0

    self.SwayScale = 1.0
    self.BobScale = 1.0

    if ( self.Owner:KeyDown( IN_SPEED ) and self.Owner:GetVelocity():Length() > 0 ) or GAMEMODE:ElementsVisible() or self.HolsterMode then

        self.SwayScale = 1.25
        self.BobScale = 1.25

        if not self.SprintStart then

            self.SprintStart = CurTime()

        end

        mul = math.Clamp( ( CurTime() - self.SprintStart ) / movetime, 0, 1 )
        mul = -( mul - 1 ) ^ 2 + 1

        newang = self.SprintAng
        newpos = self.SprintPos

    else

        if self.SprintStart then

            self.SprintEnd = CurTime()
            self.SprintStart = nil

        end

        if self.SprintEnd then

            mul = math.Clamp( ( CurTime() - self.SprintEnd ) / movetime, 0, 1 )
            mul = ( mul - 1 ) ^ 2

            newang = self.SprintAng
            newpos = self.SprintPos

            if mul == 0 then

                self.SprintEnd = nil

            end

        else

            mul = self:GetMoveScale( movetime, duration, mul )

        end
    end

    return self:MoveViewModelTo( newpos, newang, pos, ang, mul )

end

function SWEP:GetMoveScale( movetime, duration, mul )

	mul = 1

	if CurTime() - movetime < duration then

		mul = math.Clamp( ( CurTime() - duration ) / movetime, 0, 1 )

	end

	if self.Weapon:GetNWBool( "ReverseAnim", false ) then

		return -( mul - 1 ) ^ 3

	end

	return ( mul - 1 ) ^ 3 + 1

end

function SWEP:AngApproach( newang, ang, mul )

	if not newang then return ang end

	ang:RotateAroundAxis( ang:Right(), 		newang.x * mul )
	ang:RotateAroundAxis( ang:Up(), 		newang.y * mul )
	ang:RotateAroundAxis( ang:Forward(), 	newang.z * mul )

	return ang

end

function SWEP:PosApproach( newpos, pos, ang, mul )

	local right 	= ang:Right()
	local up 		= ang:Up()
	local forward 	= ang:Forward()

	if not newpos then return pos end

	pos = pos + newpos.x * right * mul
	pos = pos + newpos.y * forward * mul
	pos = pos + newpos.z * up * mul

	return pos

end

function SWEP:MoveViewModelTo( newpos, newang, pos, ang, mul )

	ang = self:AngApproach( newang, ang, mul )
	pos = self:PosApproach( newpos, pos, ang, mul )

	return pos, ang

end

function SWEP:Initialize()

	self.Weapon:SetWeaponHoldType( self.HoldType )

end

function SWEP:Deploy()

	if SERVER then

		self.Weapon:SetZoomMode( 1 )
		self.Owner:DrawViewModel( true )

	end

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	return true

end

function SWEP:Holster()

	return true

end

function SWEP:Think()

	self.Weapon:ReloadThink()

	if self.Owner:GetVelocity():Length() > 0 and self.Owner:KeyDown( IN_SPEED ) then

		self.LastRunFrame = CurTime() + 0.3

		self.Weapon:UnZoom()

	end

end

function SWEP:SetZoomMode( num )

	if num > #self.ZoomModes then

		num = 1

		self.Weapon:UnZoom()

	end

	self.Weapon:SetNWInt( "Mode", num )

	if self.Owner:GetFOV() != self.ZoomModes[num] then

		self.Owner:SetFOV( self.ZoomModes[num], self.ZoomSpeeds[num] )

	end

end

function SWEP:GetZoomMode()

	return self.Weapon:GetNWInt( "Mode", 1 )

end

function SWEP:UnZoom()

	if CLIENT then return end

	self.Weapon:SetZoomMode( 1 )
	self.Weapon:SetNWBool( "ReverseAnim", true )

	self.Owner:DrawViewModel( true )

end

function SWEP:Reload()

	if self.Weapon:Clip1() == self.Primary.ClipSize or self.Weapon:Clip1() > self.Owner:GetNWInt( "Ammo" .. self.AmmoType, 0 ) or self.HolsterMode or self.ReloadTime then return end

	if self.Owner:GetNWInt( "Ammo" .. self.AmmoType, 0 ) < 1 then

		self.Weapon:SetClip1( self.Primary.ClipSize )

		return

	end

	if self.Weapon:GetZoomMode() != 1 then

		self.Weapon:UnZoom()

	end

	self.Weapon:DoReload()

end

function SWEP:StartWeaponAnim( anim )

	if IsValid( self.Owner ) then

		local vm = self.Owner:GetViewModel()

		local idealSequence = self:SelectWeightedSequence( anim )
		local nextSequence = self:FindTransitionSequence( self.Weapon:GetSequence(), idealSequence )

		//vm:RemoveEffects( EF_NODRAW )
		//vm:SetPlaybackRate( pbr )

		if nextSequence > 0 then

			vm:SendViewModelMatchingSequence( nextSequence )

		else

			vm:SendViewModelMatchingSequence( idealSequence )

		end

		return vm:SequenceDuration( vm:GetSequence() )

	end

end

function SWEP:DoReload()

	local time = self.Weapon:StartWeaponAnim( ACT_VM_RELOAD )

	self.Weapon:SetNextPrimaryFire( CurTime() + time + 0.080 )

	self.ReloadTime = CurTime() + time

end

function SWEP:ReloadThink()

	if self.ReloadTime and self.ReloadTime <= CurTime() then

		self.ReloadTime = nil
		self.Weapon:SetClip1( self.Primary.ClipSize )

	end

end

function SWEP:CanSecondaryAttack()

	if self.HolsterMode or self.Owner:KeyDown( IN_SPEED ) or self.LastRunFrame > CurTime() then return false end

	if self.Weapon:Clip1() <= 0 and self.IsSniper then

		if self.Weapon:GetZoomMode() != 1 then

			self.Weapon:UnZoom()

		end

		return false

	end

	return true

end

function SWEP:CanPrimaryAttack()

	if self.HolsterMode or self.ReloadTime or self.LastRunFrame > CurTime() then return false end

	if self.Owner:GetNWInt( "Ammo" .. self.AmmoType, 0 ) < 1 then

		self.Weapon:EmitSound( self.Primary.Empty )

		return false

	end

	if self.Weapon:Clip1() <= 0 then

		self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 )
		self.Weapon:DoReload()

		if self.Weapon:GetZoomMode() != 1 then

			self.Weapon:UnZoom()

		end

		return false

	end

	return true

end

function SWEP:ShootEffects()

	if IsFirstTimePredicted() then

		self.Owner:ViewPunch( Angle( math.Rand( -0.2, -0.1 ) * self.Primary.Recoil, math.Rand( -0.05, 0.05 ) * self.Primary.Recoil, 0 ) )

	end

	self.Owner:MuzzleFlash()
	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )

	if CLIENT then return end

	if self.UseShellSounds then

		local pitch = self.Pitches[ self.AmmoType ] + math.random( -3, 3 )
		local tbl = self.ShellSounds
		local pos = self.Owner:GetPos()

		if self.AmmoType == "Buckshot" then

			tbl = self.BuckshotShellSounds

		end

		timer.Simple( math.Rand( self.MinShellDelay, self.MaxShellDelay ), function() sound.Play( table.Random( tbl ), pos, 50, pitch ) end )

	end

end

function SWEP:PrimaryAttack()

	if not self.Weapon:CanPrimaryAttack() then

		self.Weapon:SetNextPrimaryFire( CurTime() + 0.25 )

		return

	end

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:EmitSound( self.Primary.Sound, 100, math.random(95,105) )
	self.Weapon:SetClip1( self.Weapon:Clip1() - self.Primary.NumShots )
	self.Weapon:ShootEffects()

	if self.IsSniper and self.Weapon:GetZoomMode() == 1 then

		self.Weapon:ShootBullets( self.Primary.Damage, self.Primary.NumShots, self.Primary.SniperCone, 1 )

	else

		self.Weapon:ShootBullets( self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone, self.Weapon:GetZoomMode() )

	end

	if self.Weapon:GetZoomMode() > 1 then

		self.Weapon:UnZoom()

	end

	if SERVER then

		self.Owner:AddAmmo( self.AmmoType, self.Primary.NumShots * -1 )

	end

end

function SWEP:SecondaryAttack()

	if not self.Weapon:CanSecondaryAttack() then return end

	self.Weapon:SetNextSecondaryFire( CurTime() + 0.25 )

	if not self.IsSniper then

		self.Weapon:ToggleLaser()

		return

	end

	if SERVER then

		if self.Weapon:GetZoomMode() == 1 then

			self.Owner:DrawViewModel( false )

		end

		self.Weapon:SetZoomMode( self.Weapon:GetZoomMode() + 1 )

	end

	self.Weapon:EmitSound( self.Secondary.Sound )

end

function SWEP:ToggleLaser()

	self.Weapon:EmitSound( self.Secondary.Laser )
	self.Weapon:SetNWBool( "Laser", !self.Weapon:GetNWBool( "Laser", false ) )

end

function SWEP:AdjustMouseSensitivity()

	local num = self.Weapon:GetNWInt( "Mode", 1 )
	local scale = ( self.ZoomModes[ num ] or 0 ) / 100

	if scale == 0 then

		return nil

	end

	return scale

end

function SWEP:GetDamageFalloffScale( distance )

	local scale = 1

	if distance > self.FalloffDistances[ self.AmmoType ].Range then

		scale = ( 1 - ( ( distance - self.FalloffDistances[ self.AmmoType ].Range ) / self.FalloffDistances[ self.AmmoType ].Falloff ) )

	end

	return math.Clamp( scale, 0.1, 1.0 )

end

function SWEP:ShootBullets( damage, numbullets, aimcone, zoommode )

	if SERVER then

		self.Owner:AddStat( "Bullets", numbullets )

	end

	local scale = aimcone

	if self.Owner:KeyDown( IN_FORWARD ) or self.Owner:KeyDown( IN_BACK ) or self.Owner:KeyDown( IN_MOVELEFT ) or self.Owner:KeyDown( IN_MOVERIGHT ) then

		scale = aimcone * 1.75

	elseif self.Owner:KeyDown( IN_DUCK ) or self.Owner:KeyDown( IN_WALK ) then

		scale = math.Clamp( aimcone / 1.75, 0, 10 )

	end

	local bullet = {}
	bullet.Num 		= numbullets
	bullet.Src 		= self.Owner:GetShootPos()
	bullet.Dir 		= self.Owner:GetAimVector()
	bullet.Spread 	= Vector( scale, scale, 0 )
	bullet.Tracer	= 0
	bullet.Force	= damage * 2
	bullet.Damage	= damage
	bullet.AmmoType = "Pistol"

	//if self.IsSniper and self.AmmoType == "Sniper" then

		//bullet.TracerName = "sniper_tracer"

	//end

	bullet.Callback = function( attacker, tr, dmginfo )

		dmginfo:ScaleDamage( self:GetDamageFalloffScale( tr.HitPos:Distance( self.Owner:GetShootPos() ) ) )

		if tr.Entity.NextBot then

			tr.Entity:OnLimbHit( tr.HitGroup, dmginfo )

		end

		if ( IsValid( tr.Entity ) and tr.Entity:IsPlayer() ) or math.random(1,5) != 1 then return end

		self.Weapon:BulletPenetration( attacker, tr, dmginfo, 0 )

	end

	self.Owner:LagCompensation( true )

	self.Owner:FireBullets( bullet )

	self.Owner:LagCompensation( false )

end

function SWEP:GetPenetrationDistance( mat_type )

	if ( mat_type == MAT_PLASTIC || mat_type == MAT_WOOD || mat_type == MAT_ALIENFLESH || mat_type == MAT_FLESH || mat_type == MAT_GLASS ) then

		return 64

	end

	return 32

end

function SWEP:GetPenetrationDamageLoss( mat_type, distance, damage )

	if ( mat_type == MAT_GLASS || mat_type == MAT_ALIENFLESH || mat_type == MAT_FLESH ) then
		return damage
	elseif ( mat_type == MAT_PLASTIC  || mat_type == MAT_WOOD ) then
		return damage - distance
	elseif( mat_type == MAT_TILE || mat_type == MAT_SAND || mat_type == MAT_DIRT ) then
		return damage - ( distance * 1.2 )
	end

	return damage - ( distance * 1.8 )

end

--[[function SWEP:BulletPenetration( attacker, tr, dmginfo, bounce )

	if ( !self or !IsValid( self.Weapon ) ) then return end

	if IsValid( tr.Entity ) and string.find( tr.Entity:GetClass(), "npc" ) then

		local effectdata = EffectData()
		effectdata:SetOrigin( tr.HitPos )
		util.Effect( "BloodImpact", effectdata )

	end

	// Don't go through more than 3 times
	if ( bounce > 3 ) then return false end

	// Direction (and length) that we are gonna penetrate
	local PeneDir = tr.Normal * self:GetPenetrationDistance( tr.MatType )

	local PeneTrace = {}
	   PeneTrace.endpos = tr.HitPos
	   PeneTrace.start = tr.HitPos + PeneDir
	   PeneTrace.mask = MASK_SHOT
	   PeneTrace.filter = { self.Owner }

	local PeneTrace = util.TraceLine( PeneTrace )

	// Bullet didn't penetrate.
	if ( PeneTrace.StartSolid or PeneTrace.Fraction >= 1.0 or tr.Fraction <= 0.0 ) then return false end

	local distance = ( PeneTrace.HitPos - tr.HitPos ):Length()
	local new_damage = self:GetPenetrationDamageLoss( tr.MatType, distance, dmginfo:GetDamage() )

	if new_damage > 0 then

		local bullet =
		{
			Num 		= 1,
			Src 		= PeneTrace.HitPos,
			Dir 		= tr.Normal,
			Spread 		= Vector( 0, 0, 0 ),
			Tracer		= 0,
			Force		= 5,
			Damage		= new_damage,
			AmmoType 	= "Pistol",
		}

		bullet.Callback = function ( attacker, tr, dmginfo )

			if IsValid( self ) and IsValid( self.Weapon ) then

				self.Weapon:BulletPenetration( attacker, tr, dmginfo, bounce + 1 )

			end

		end

		local effectdata = EffectData()
		effectdata:SetOrigin( PeneTrace.HitPos )
		effectdata:SetNormal( PeneTrace.Normal )
		util.Effect( "Impact", effectdata )

		local func = function( attacker, bullet )

			if IsValid( attacker ) then

				attacker.FireBullets( attacker, bullet, true )

			end

		end

		timer.Simple( 0.05, function() func( attacker, bullet ) end )

		if SERVER and tr.MatType != MAT_FLESH then

			sound.Play( table.Random( GAMEMODE.Ricochet ), tr.HitPos, 100, math.random(90,120) )

		end

	end

end]]

function SWEP:BulletPenetration( attacker, tr, dmginfo, bounce )

	if ( !self or not IsValid( self.Weapon ) ) then return end

	if IsValid( tr.Entity ) and string.find( tr.Entity:GetClass(), "npc" ) then

		local effectdata = EffectData()
		effectdata:SetOrigin( tr.HitPos )
		util.Effect( "BloodImpact", effectdata )

	end

	if ( bounce > 3 ) then return false end

	local PeneDir = tr.Normal * self:GetPenetrationDistance( tr.MatType )

	local PeneTrace = {}
	   PeneTrace.endpos = tr.HitPos
	   PeneTrace.start = tr.HitPos + PeneDir
	   PeneTrace.mask = MASK_SHOT
	   PeneTrace.filter = { self.Owner }

	local PeneTrace = util.TraceLine( PeneTrace )

	if ( PeneTrace.StartSolid || PeneTrace.Fraction >= 1.0 || tr.Fraction <= 0.0 ) then return false end

	local distance = ( PeneTrace.HitPos - tr.HitPos ):Length()
	local new_damage = self:GetPenetrationDamageLoss( tr.MatType, distance, dmginfo:GetDamage() )

	if new_damage > 0 then

		local bullet =
		{
			Num 		= 1,
			Src 		= PeneTrace.HitPos,
			Dir 		= tr.Normal,
			Spread 		= Vector( 0, 0, 0 ),
			Tracer		= 0,
			Force		= 5,
			Damage		= new_damage,
			AmmoType 	= "Pistol",
		}

		bullet.Callback = function( a, b, c )

			if IsValid( self ) and IsValid( self.Weapon ) then

				self.Weapon:BulletPenetration( attacker, tr, dmginfo, bounce + 1 )

			end

		end

		local effectdata = EffectData()
		effectdata:SetOrigin( PeneTrace.HitPos );
		effectdata:SetNormal( PeneTrace.Normal );
		util.Effect( "Impact", effectdata )

		timer.Simple( 0.01, function() attacker:FireBullets( bullet, true ) end )

		if SERVER and tr.MatType != MAT_FLESH and bounce == 0 then

			sound.Play( table.Random( GAMEMODE.Ricochet ), tr.HitPos, 100, math.random(90,120) )

		end

	end

end

function SWEP:ShouldNotDraw()

	return self.Weapon:GetNWBool( "Laser", false )

end

if CLIENT then

	SWEP.CrossRed = CreateClientConVar( "cl_redead_crosshair_r", 255, true, false )
	SWEP.CrossGreen = CreateClientConVar( "cl_redead_crosshair_g", 255, true, false )
	SWEP.CrossBlue = CreateClientConVar( "cl_redead_crosshair_b", 255, true, false )
	SWEP.CrossAlpha = CreateClientConVar( "cl_redead_crosshair_a", 255, true, false )
	SWEP.CrossLength = CreateClientConVar( "cl_redead_crosshair_length", 10, true, false )

	SWEP.DotMat = Material( "Sprites/light_glow02_add_noz" )
	SWEP.LasMat = Material( "sprites/bluelaser1" )

end

SWEP.CrosshairScale = 1

function SWEP:LaserDraw()

	local vm = self.Owner:GetViewModel()

	if IsValid( vm ) then

		local idx = vm:LookupAttachment( "1" )

		if idx == 0 then idx = vm:LookupAttachment( "muzzle" ) end

		local trace = util.GetPlayerTrace( ply )
		local tr = util.TraceLine( trace )
		local tbl = vm:GetAttachment( idx )

		local pos = tr.HitPos

		if vm:GetSequence() != ACT_VM_IDLE then

			self.AngDiff = ( tbl.Ang - self.LastGoodAng ):Forward()

			trace = {}
			trace.start = tbl.Pos or Vector(0,0,0)
			trace.endpos = trace.start + ( ( EyeAngles() + self.AngDiff ):Forward() * 99999 )
			trace.filter = { self.Owner, self.Weapon }

			local tr2 = util.TraceLine( trace )

			pos = tr2.HitPos

		else

			self.LastGoodAng = tbl.Ang

		end

		cam.Start3D( EyePos(), EyeAngles() )

			local dir = ( tbl.Ang ):Forward()
			local start = tbl.Pos

			render.SetMaterial( self.LasMat )

			for i=0,254 do

				render.DrawBeam( start, start + dir * 5, 2, 0, 12, Color( 255, 0, 0, 255 - i ) )

				start = start + dir * 5

			end

			local dist = tr.HitPos:Distance( EyePos() )
			local size = math.Rand( 6, 7 )
			local dotsize = dist / size ^ 2

			render.SetMaterial( self.DotMat )
			render.DrawQuadEasy( pos, ( EyePos() - tr.HitPos ):GetNormal(), dotsize, dotsize, Color( 255, 0, 0, 255 ), 0 )

		cam.End3D()

	end

end

function SWEP:DrawHUD()

	if self.Weapon:ShouldNotDraw() then return end

	if self.Weapon:GetNWBool( "Laser", false ) then return end

	local mode = self.Weapon:GetZoomMode()

	if not self.IsSniper or mode == 1 then

		local cone = self.Primary.Cone
		local scale = cone

		if self.IsSniper then

			cone = self.Primary.SniperCone
			scale = cone

		end

		local x = ScrW() * 0.5
		local y = ScrH() * 0.5
		local scalebywidth = ( ScrW() / 1024 ) * 10

		if self.Owner:KeyDown( IN_FORWARD ) or self.Owner:KeyDown( IN_BACK ) or self.Owner:KeyDown( IN_MOVELEFT ) or self.Owner:KeyDown( IN_MOVERIGHT ) then

			scale = cone * 1.75

		elseif self.Owner:KeyDown( IN_DUCK ) or self.Owner:KeyDown( IN_WALK ) then

			scale = math.Clamp( cone / 1.75, 0, 10 )

		end

		scale = scale * scalebywidth

		local dist = math.abs( self.CrosshairScale - scale )
		self.CrosshairScale = math.Approach( self.CrosshairScale, scale, FrameTime() * 2 + dist * 0.05 )

		local gap = 40 * self.CrosshairScale
		local length = gap + self.CrossLength:GetInt() //20 * self.CrosshairScale

		surface.SetDrawColor( self.CrossRed:GetInt(), self.CrossGreen:GetInt(), self.CrossBlue:GetInt(), self.CrossAlpha:GetInt() )
		surface.DrawLine( x - length, y, x - gap, y )
		surface.DrawLine( x + length, y, x + gap, y )
		surface.DrawLine( x, y - length, x, y - gap )
		surface.DrawLine( x, y + length, x, y + gap )

		return

	end

	if mode != 1 then

		local w = ScrW()
		local h = ScrH()
		local wr = ( h / 3 ) * 4

		surface.SetTexture( surface.GetTextureID( "gmod/scope" ) )
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawTexturedRect( ( w / 2 ) - wr / 2, 0, wr, h )

		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawRect( 0, 0, ( w / 2 ) - wr / 2, h )
		surface.DrawRect( ( w / 2 ) + wr / 2, 0, w - ( ( w / 2 ) + wr / 2 ), h )
		surface.DrawLine( 0, h * 0.50, w, h * 0.50 )
		surface.DrawLine( w * 0.50, 0, w * 0.50, h )

	end

end