summaryrefslogtreecommitdiff
path: root/entities/weapons/rad_hammer/shared.lua
blob: 6a7999f193770f24af3176eae4bf6853f013818d (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
if SERVER then

	AddCSLuaFile("shared.lua")

end

if CLIENT then

	SWEP.ViewModelFOV		= 80
	SWEP.ViewModelFlip		= false

	SWEP.PrintName = "Hammer"
	SWEP.IconLetter = "j"
	SWEP.Slot = 1
	SWEP.Slotpos = 0

end

SWEP.HoldType = "melee"

SWEP.Base = "rad_base"

SWEP.ViewModel			= "models/weapons/v_hammer/v_hammer.mdl"
SWEP.WorldModel			= "models/weapons/w_hammer.mdl"

SWEP.Barricade = Model( "models/props_debris/wood_board04a.mdl" )

SWEP.HoldPos = Vector (1.1747, -16.6759, -5.7913)
SWEP.HoldAng = Vector (23.7548, -8.0105, -5.154)

SWEP.IsSniper = false
SWEP.AmmoType = "Knife"

SWEP.Click = Sound( "Buttons.snd14" )
SWEP.Deny = Sound( "HL1/fvox/buzz.wav" )

SWEP.Primary.Hit            = Sound( "weapons/crowbar/crowbar_impact1.wav" )
SWEP.Primary.Sound			= Sound( "weapons/iceaxe/iceaxe_swing1.wav" )
SWEP.Primary.Recoil			= 6.5
SWEP.Primary.Damage			= 50
SWEP.Primary.NumShots		= 1
SWEP.Primary.Delay			= 1.100

SWEP.Primary.ClipSize		= 1
SWEP.Primary.Automatic		= true

SWEP.Position = 35
SWEP.BuildAng = 0

function SWEP:GetViewModelPosition( pos, ang )

	return self.Weapon:MoveViewModelTo( self.HoldPos, self.HoldAng, pos, ang, 1 )

end

function SWEP:ReleaseGhost()

	if IsValid( self.GhostEntity ) then

		self.GhostEntity:Remove()
		self.GhostEntity = nil

	end

end

function SWEP:MakeGhost( model, pos, angle )

	self.GhostEntity = ents.CreateClientProp( model )
	//self.GhostEntity:SetModel( model )
	self.GhostEntity:SetPos( pos )
	self.GhostEntity:SetAngles( angle )
	self.GhostEntity:Spawn()

	self.GhostEntity:SetSolid( SOLID_VPHYSICS )
	self.GhostEntity:SetMoveType( MOVETYPE_NONE )
	self.GhostEntity:SetNotSolid( true )
	self.GhostEntity:SetRenderMode( RENDERMODE_TRANSALPHA )
	self.GhostEntity:SetColor( Color( 255, 255, 255, 200 ) )

end

function SWEP:UpdateGhost()

	if not IsValid( self.GhostEntity ) then return end

	local tr = util.GetPlayerTrace( self.Owner )
	local trace = util.TraceLine( tr )

	if not trace.Hit then return end

	local ang = ( trace.HitNormal * -1 ):Angle() + Angle( 0, 0, math.NormalizeAngle( 90 + self.Weapon:GetNWInt( "BuildAng", 0 ) ) )
	local pos = trace.HitPos

	local trlength = self.Weapon:GetOwner():GetPos() - trace.HitPos
	trlength = trlength:Length()

	if trlength < 150 and ( trace.HitWorld or trace.Entity:GetClass() == "prop_door_rotating" ) then

		self.GhostEntity:SetColor( Color( 50, 255, 50, 200 ) )

	else

		self.GhostEntity:SetColor( Color( 255, 50, 50, 200 ) )

	end

	if not trace.HitWorld and not trace.Entity:GetClass() == "prop_door_rotating" then

		self.GhostEntity:SetColor( Color( 255, 50, 50, 200 ) )
		self.GhostEntity:SetModel( self.Barricade )
		self.GhostEntity:SetPos( pos + ( self.GhostEntity:GetUp() * self.Position ) )
		self.GhostEntity:SetAngles( ang )

	elseif string.find( trace.Entity:GetClass(), "prop_door" ) then

		self.GhostEntity:SetModel( "models/props_c17/tools_wrench01a.mdl" )
		self.GhostEntity:SetPos( trace.HitPos + trace.HitNormal * 5 )
		self.GhostEntity:SetAngles( ( trace.HitNormal * -1 ):Angle() + Angle(0,90,90) )

	else

		self.GhostEntity:SetModel( self.Barricade )
		self.GhostEntity:SetPos( pos + ( self.GhostEntity:GetUp() * self.Position ) )
		self.GhostEntity:SetAngles( ang )

	end

end

function SWEP:SetPlacePosition( ent )

	local tr = util.GetPlayerTrace( self:GetOwner() )
	local trace = util.TraceLine( tr )

	if not trace.Hit then return end

	local ang = ( trace.HitNormal * -1 ):Angle() + Angle( 0, 0, math.NormalizeAngle( 90 + self.Weapon:GetNWInt( "BuildAng", 0 ) ) )
	ent:SetAngles( ang )

	local pos = trace.HitPos
	ent:SetPos( pos + ( ent:GetUp() * self.Position ) )

	local phys = ent:GetPhysicsObject()

	if IsValid( phys ) then

		phys:EnableMotion( false )

	end

end

function SWEP:Deploy()

	if SERVER then

		self.Owner:DrawViewModel( true )
		self.Owner:NoticeOnce( "Toggle build mode with your reload key", GAMEMODE.Colors.Blue, 5, 2 )

	else

		self.Weapon:ReleaseGhost()

	end

	self.InIron = false

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	return true

end

function SWEP:Holster()

	if CLIENT then

		self.Weapon:ReleaseGhost()

	end

	return true

end

function SWEP:Reload()

	if CLIENT then return end

	if ( self.ReloadTime or 0 ) < CurTime() then

		self.ReloadTime = CurTime() + 0.75

		self.Weapon:SetNWBool( "BuildMode", !self.Weapon:GetNWBool( "BuildMode", false ) )

		self.Owner:EmitSound( self.Click, 50, 120 )
		self.Owner:NoticeOnce( "Rotate your barricade by right clicking", GAMEMODE.Colors.Blue, 5, 2 )

	end

end

function SWEP:SecondaryAttack()

	if CLIENT then return end

	if ( self.SecondDelay or 0 ) < CurTime() and self.Weapon:GetNWBool( "BuildMode", false ) then

		self.SecondDelay = CurTime() + 0.25
		self.BuildAng = self.BuildAng + 45

		if self.BuildAng == 360 then

			self.BuildAng = 0

		end

		self.Weapon:SetNWInt( "BuildAng", self.BuildAng )
		self.Owner:EmitSound( self.Click )

	end

end

function SWEP:PrimaryAttack()

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

		if SERVER then

			self.Owner:AddStamina( -2 )

		end

		self.Owner:SetAnimation( PLAYER_ATTACK1 )

		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self.Weapon:MeleeTrace( self.Primary.Damage )

	else

		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
		self.Weapon:BarricadeTrace()

	end

end

function SWEP:BarricadeTrace()

	if CLIENT then return end

	local trace = util.GetPlayerTrace( self.Owner )
	local tr = util.TraceLine( trace )
	local trlength = self.Weapon:GetOwner():GetPos() - tr.HitPos
	trlength = trlength:Length()

	local has, id = self.Owner:GetWood()

	if not has then

		self.Owner:Notice( "You don't have enough wood", GAMEMODE.Colors.Red )
		self.Owner:EmitSound( self.Deny, 50, 100 )

		return

	end

	if tr.HitWorld and trlength < 150 then

		self.Owner:Notice( "Built a barricade using 1 piece of wood", GAMEMODE.Colors.Green )
		self.Owner:NoticeOnce( "You can also repair doors", GAMEMODE.Colors.Blue, 5, 2 )

		local prop = ents.Create( "prop_physics" )
		prop:SetModel( self.Barricade )
		prop:Spawn()
		prop:SetHealth( 350 )
		prop:SetCustomCollisionCheck()
		prop.IsWood = true

		self.Weapon:SetPlacePosition( prop )

	elseif string.find( tr.Entity:GetClass(), "prop_door" ) and trlength < 150 then

		self.Owner:Notice( "Fortified a door using 1 piece of wood", GAMEMODE.Colors.Green )

		tr.Entity.MaxHits = 30
		tr.Entity.Hits = 0

	else

		self.Owner:Notice( "You can't build a barricade here", GAMEMODE.Colors.Red )
		self.Owner:EmitSound( self.Deny, 50, 100 )

		return

	end

	self.Owner:AddStamina( -15 )
	self.Owner:RemoveFromInventory( id )
	self.Owner:AddStat( "Wood" )
	self.Owner:AddCash( 1 )

	self.Owner:EmitSound( table.Random( GAMEMODE.Drill ), 100, math.random(90,110) )
	self.Owner:EmitSound( table.Random( GAMEMODE.WoodHammer ), 100, math.random(90,110) )

end

function SWEP:MeleeTrace( dmg )

	self.Weapon:SendWeaponAnim( ACT_VM_MISSCENTER )

	if CLIENT then return end

	local pos = self.Owner:GetShootPos()
	local aim = self.Owner:GetAimVector() * 64

	local line = {}
	line.start = pos
	line.endpos = pos + aim
	line.filter = self.Owner

	local linetr = util.TraceLine( line )

	local tr = {}
	tr.start = pos + self.Owner:GetAimVector() * -5
	tr.endpos = pos + aim
	tr.filter = self.Owner
	tr.mask = MASK_SHOT_HULL
	tr.mins = Vector(-20,-20,-20)
	tr.maxs = Vector(20,20,20)

	local trace = util.TraceHull( tr )
	local ent = trace.Entity
	local ent2 = linetr.Entity

	if not IsValid( ent ) and IsValid( ent2 ) then

		ent = ent2

	end

	if not IsValid( ent ) then

		self.Owner:EmitSound( self.Primary.Sound, 100, math.random(60,80) )
		return

	elseif not ent:IsWorld() then

		self.Weapon:SendWeaponAnim( ACT_VM_HITCENTER )

		if ent:IsPlayer() then

			local snd = table.Random( GAMEMODE.BluntHit )
			ent:EmitSound( snd, 100, math.random(90,110) )

			if ent:Team() != self.Owner:Team() then

				ent:TakeDamage( dmg, self.Owner, self.Weapon )

				self.Owner:DrawBlood()

				local ed = EffectData()
				ed:SetOrigin( trace.HitPos )
				util.Effect( "BloodImpact", ed, true, true )

			end

		elseif string.find( ent:GetClass(), "npc" ) then

			local snd = table.Random( GAMEMODE.BluntHit )
			ent:EmitSound( snd, 100, math.random(90,110) )

			if math.random(1,3) == 1 then

				ent:SetHeadshotter( self.Owner, true )

			end

			ent:TakeDamage( dmg, self.Owner, self.Weapon )

			self.Owner:DrawBlood()

			local ed = EffectData()
			ed:SetOrigin( trace.HitPos )
			util.Effect( "BloodImpact", ed, true, true )

		elseif !ent:IsPlayer() then

			if string.find( ent:GetClass(), "breakable" ) then

				ent:TakeDamage( 50, self.Owner, self.Weapon )

				if ent:GetClass() == "func_breakable_surf" then

					ent:Fire( "shatter", "1 1 1", 0 )

				end

			end

			ent:EmitSound( self.Primary.Hit, 100, math.random(90,110) )

			local phys = ent:GetPhysicsObject()

			if IsValid( phys ) then

				if ent.IsWooden or (ent:GetMaterialType() == MAT_WOOD) then

					ent:Fire( "break", 0, 0 )

				else

					ent:SetPhysicsAttacker( self.Owner )
					ent:TakeDamage( 10, self.Owner, self.Weapon )

					phys:Wake()
					phys:ApplyForceCenter( self.Owner:GetAimVector() * phys:GetMass() * 200 )

				end

			end

		end

	end

end

function SWEP:Think()

	if CLIENT then

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

			if not self.GhostEntity then

				self.Weapon:MakeGhost( self.Barricade, self.Owner:GetPos() + Vector(0,0,100), Angle(0,0,0))

			else

				self.Weapon:UpdateGhost()

			end

		elseif IsValid( self.GhostEntity ) then

			self.Weapon:ReleaseGhost()

		end

	end

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

		if self.Owner:KeyDown( IN_SPEED ) then

			self.LastRunFrame = CurTime() + 0.3

		end

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

			self.Weapon:UnZoom()

		end

	end

	if self.MoveTime and self.MoveTime < CurTime() and SERVER then

		self.MoveTime = nil
		self.Weapon:SetZoomMode( self.Weapon:GetZoomMode() + 1 )
		self.Owner:DrawViewModel( false )

	end

end

function SWEP:DrawHUD()

end