summaryrefslogtreecommitdiff
path: root/entities/weapons/rad_itemplacer/shared.lua
blob: 12dad13fe89e338b082c0d14ace0a35fc1934f02 (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
if SERVER then

	AddCSLuaFile( "shared.lua" )
	
	SWEP.Weight				= 1
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	
end

if CLIENT then

	ClientItemPlacerTbl = {}
	ClientItemPlacerTbl[ "teh" ] = {}

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

	SWEP.ViewModelFOV		= 74
	SWEP.ViewModelFlip		= false
	
	SWEP.PrintName = "Item Placement Tool"
	SWEP.Slot = 5
	SWEP.Slotpos = 5
	
	function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
		
	end
	
end

SWEP.HoldType = "pistol"

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

SWEP.Primary.Swap           = Sound( "weapons/clipempty_rifle.wav" )
SWEP.Primary.Sound			= Sound( "NPC_CombineCamera.Click" )
SWEP.Primary.Delete1		= Sound( "Weapon_StunStick.Melee_Hit" )
SWEP.Primary.Delete			= Sound( "Weapon_StunStick.Melee_HitWorld" )

SWEP.Primary.ClipSize		= 1
SWEP.Primary.DefaultClip	= 99999
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "pistol"

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

SWEP.AmmoType = "Knife"

SWEP.ItemTypes = { "info_player_zombie",
"info_player_army",
"info_lootspawn",
"info_npcspawn",
"info_evac",
"point_radiation" }

SWEP.ServersideItems = { "info_player_zombie",
"info_player_army",
"info_lootspawn",
"info_npcspawn",
"info_evac",
"point_radiation" }

SWEP.SharedItems = {}

function SWEP:Initialize()

	if SERVER then
	
		self.Weapon:SetWeaponHoldType( self.HoldType )
		
	end
	
end

function SWEP:Synch()

	for k,v in pairs( self.ServersideItems ) do
	
		local ents = ents.FindByClass( v )
		local postbl = {}
		
		for c,d in pairs( ents ) do
		
			table.insert( postbl, d:GetPos() )
		
		end
		
		net.Start( "ItemPlacerSynch" )
		net.WriteString( v )
		net.WriteTable( postbl )
		net.Send( self.Owner )
	
		//local tbl = { Name = v, Ents = postbl }
		
		//datastream.StreamToClients( { self.Owner }, "ItemPlacerSynch", tbl )
		
	end

end

net.Receive( "ItemPlacerSynch", function( len )

	ClientItemPlacerTbl[ net.ReadString() ] = net.ReadTable()

end )

--[[function PlacerSynch( handler, id, encoded, decoded )

	ClientItemPlacerTbl[ decoded.Name ] = decoded.Ents

end
datastream.Hook( "ItemPlacerSynch", PlacerSynch )]]

function SWEP:Deploy()

	if SERVER then
	
		self.Weapon:Synch()
	
	end

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	
	return true
	
end  

function SWEP:Think()	

	if CLIENT then return end

	if self.Owner:KeyDown( IN_USE ) and ( ( self.NextDel or 0 ) < CurTime() ) then
	
		self.NextDel = CurTime() + 1
		
		local tr = util.TraceLine( util.GetPlayerTrace( self.Owner ) )
		
		local closest
		local dist = 1000
		
		for k,v in pairs( ents.FindByClass( self.ItemTypes[ self.Weapon:GetNWInt( "ItemType", 1 ) ] ) ) do
		
			if v:GetPos():Distance( tr.HitPos ) < dist then
			
				dist = v:GetPos():Distance( tr.HitPos )
				closest = v
			
			end
		
		end
		
		if IsValid( closest ) then
		
			closest:Remove()
			
			self.Owner:EmitSound( self.Primary.Delete1 )
			
			self.Weapon:Synch()
		
		end
		
	end

end

function SWEP:Reload()

	if CLIENT then return end
	
	for k,v in pairs( ents.FindByClass( self.ItemTypes[ self.Weapon:GetNWInt( "ItemType", 1 ) ] ) ) do
	
		v:Remove()
	
	end
	
	self.Weapon:Synch()
	
	self.Owner:EmitSound( self.Primary.Delete )
	
end

function SWEP:Holster()

	return true

end

function SWEP:ShootEffects()	
	
	self.Owner:MuzzleFlash()								
	self.Owner:SetAnimation( PLAYER_ATTACK1 )	
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 
	
end

function SWEP:PlaceItem()

	local itemtype = self.ItemTypes[ self.Weapon:GetNWInt( "ItemType", 1 ) ]
	
	local tr = util.TraceLine( util.GetPlayerTrace( self.Owner ) )
	
	local ent = ents.Create( itemtype )
	ent:SetPos( tr.HitPos + tr.HitNormal * 5 )
	ent:Spawn()
	ent.AdminPlaced = true

end

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + 1 )
	self.Weapon:EmitSound( self.Primary.Sound, 100, math.random(95,105) )
	self.Weapon:ShootEffects()
	
	if SERVER then
	
		self.Weapon:PlaceItem()
		
		self.Weapon:Synch()
		
	end

end

function SWEP:SecondaryAttack()

	self.Weapon:SetNextSecondaryFire( CurTime() + 0.5 )
	
	self.Weapon:EmitSound( self.Primary.Swap )
	
	if SERVER then
	
		self.Weapon:SetNWInt( "ItemType", self.Weapon:GetNWInt( "ItemType", 1 ) + 1 )
		
		if self.Weapon:GetNWInt( "ItemType", 1 ) > #self.ItemTypes then
		
			self.Weapon:SetNWInt( "ItemType", 1 )
		
		end
	
	end
	
end

function SWEP:DrawHUD()

	draw.SimpleText( "PRIMARY FIRE: Place Item          SECONDARY FIRE: Change Item Type          +USE: Delete Nearest Item Of Current Type          RELOAD: Remove All Of Current Item Type", "AmmoFontSmall", ScrW() * 0.5, ScrH() - 120, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
	draw.SimpleText( "CURRENT ITEM TYPE: "..self.ItemTypes[ self.Weapon:GetNWInt( "ItemType", 1 ) ], "AmmoFontSmall", ScrW() * 0.5, ScrH() - 100, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
	
	for k,v in pairs( self.SharedItems ) do

		for c,d in pairs( ents.FindByClass( v ) ) do
		
			local pos = d:GetPos():ToScreen()
			
			if pos.visible then
			
				draw.SimpleText( v, "AmmoFontSmall", pos.x, pos.y - 15, Color(80,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.RoundedBox( 0, pos.x - 2, pos.y - 2, 4, 4, Color(255,255,255) )
			
			end
		
		end
	
	end
	
	for k,v in pairs( ClientItemPlacerTbl ) do
	
		for c,d in pairs( v ) do
	
			local vec = Vector( d[1], d[2], d[3] )
		
			local pos = vec:ToScreen()
				
			if pos.visible then
				
				draw.SimpleText( k, "AmmoFontSmall", pos.x, pos.y - 15, Color(80,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
				draw.RoundedBox( 0, pos.x - 2, pos.y - 2, 4, 4, Color(255,255,255) )
				
			end
			
		end
	
	end
	
end