-
Notifications
You must be signed in to change notification settings - Fork 3
/
gs_switcher.lua
409 lines (323 loc) · 9.43 KB
/
gs_switcher.lua
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
--[[ Config ]]--
local MAX_SLOTS = 6 -- Max number of weapon slots. Expects Integer [0, inf)
local CACHE_TIME = 1 -- Time in seconds between updating the weapon cache. RealTime is used for comparisons. Expects Decimal [0, inf]. 0 = update every frame, inf = never update
local MOVE_SOUND = "Player.WeaponSelectionMoveSlot" -- Sound to play when the player moves between weapon slots. Expects String soundscape or sound file path. "" = no sound
local SELECT_SOUND = "Player.WeaponSelected" -- Sound to play when the player selects a weapon. Expects String soundscape or sound file path. "" = no sound
local CANCEL_SOUND = "" -- Sound to play when the player cancels the weapon selection. Expects String soundscape or sound file path. "" = no sound
--[[ Instance variables - do not edit ]]--
local iCurSlot = 0 -- Currently selected slot. Will be an Integer [0, MAX_SLOTS]. 0 = no selection
local iCurPos = 1 -- Current position in that slot. Will be an Integer [0, inf)
local flNextPrecache = 0 -- Time until next precache. Will be a Decimal [0, inf) representing a RealTime
local flSelectTime = 0 -- Time the weapon selection changed slot/visibility states. Can be used to close the weapon selector after a certain amount of idle time. Will be a Decimal [0, inf) representing a RealTime
local iWeaponCount = 0 -- Total number of weapons on the player. Will be an Integer [0, inf)
-- Weapon cache; table of tables. tCache[Weapon:GetSlot() + 1] or tCacheLength[iCurSlot] contains a table containing that slot's weapons. The table's length is tCacheLength[Slot + 1]
local tCache = {}
-- Weapon cache length. tCacheLength[Weapon:GetSlot() + 1] or tCacheLength[iCurSlot] will contain the number of weapons that slot has
local tCacheLength = {}
--[[ Weapon switcher ]]--
--[[ Guarentees when this function is called:
- cl_drawhud != 0
- iCurSlot >= 1
- 1 <= iCurPos <= tCacheLength[iCurSlot]
- iWeaponCount >= 1
- LocalPlayer():IsValid()
- LocalPlayer():Alive()
- not LocalPlayer():InVehicle() or LocalPlayer():GetAllowWeaponsInVehicle()
]]
local function DrawWeaponHUD()
-- Draw here!
end
--[[ Implementation - do not edit ]]--
-- Initialize tables with slot number
for i = 1, MAX_SLOTS do
tCache[i] = {}
tCacheLength[i] = 0
end
local tonumber = tonumber
local RealTime = RealTime
local hook_Add = hook.Add
local math_floor = math.floor
local string_sub = string.sub
local LocalPlayer = LocalPlayer
local string_lower = string.lower
local input_SelectWeapon = input.SelectWeapon
-- Hide the default weapon selection
hook_Add("HUDShouldDraw", "GS_WeaponSelector", function(sName)
if (sName == "CHudWeaponSelection") then
return false
end
end)
local function PrecacheWeps()
-- Reset all table values
for i = 1, MAX_SLOTS do
for j = 1, tCacheLength[i] do
tCache[i][j] = nil
end
tCacheLength[i] = 0
end
-- Update the cache time
flNextPrecache = RealTime() + CACHE_TIME
local tWeapons = LocalPlayer():GetWeapons()
iWeaponCount = #tWeapons
if (iWeaponCount == 0) then
iCurSlot = 0
iCurPos = 1
else
for i = 1, iWeaponCount do
local pWeapon = tWeapons[i]
-- Weapon slots start internally at 0
-- Here, we will start at 1 to match the slot binds
local iSlot = pWeapon:GetSlot() + 1
if (iSlot <= MAX_SLOTS) then
-- Cache number of weapons in each slot
local iLen = tCacheLength[iSlot] + 1
tCacheLength[iSlot] = iLen
tCache[iSlot][iLen] = pWeapon
end
end
end
-- Make sure we're not pointing out of bounds
if (iCurSlot ~= 0) then
local iLen = tCacheLength[iCurSlot]
if (iLen == 0) then
iCurSlot = 0
iCurPos = 1
elseif (iCurPos > iLen) then
iCurPos = iLen
end
end
end
local function CheckBounds()
if (iCurSlot < 0 or iCurSlot > MAX_SLOTS) then
iCurSlot = 0
else
iCurSlot = math_floor(iCurSlot)
end
if (iCurPos < 1) then
iCurPos = 1
else
iCurPos = math_floor(iCurPos)
end
if (iWeaponCount < 0) then
iWeaponCount = 0
else
iWeaponCount = math_floor(iWeaponCount)
end
end
local cl_drawhud = GetConVar("cl_drawhud")
hook_Add("HUDPaint", "GS_WeaponSelector", function()
if (not cl_drawhud:GetBool()) then
return
end
CheckBounds()
if (iCurSlot == 0) then
return
end
local pPlayer = LocalPlayer()
-- Don't draw in vehicles unless weapons are allowed to be used
-- Or while dead!
if (pPlayer:IsValid() and pPlayer:Alive() and (not pPlayer:InVehicle() or pPlayer:GetAllowWeaponsInVehicle())) then
if (flNextPrecache <= RealTime()) then
PrecacheWeps()
end
if (iCurSlot ~= 0) then
DrawWeaponHUD()
end
else
iCurSlot = 0
iCurPos = 1
end
end)
hook_Add("PlayerBindPress", "GS_WeaponSelector", function(pPlayer, sBind, bPressed)
if (not pPlayer:Alive() or pPlayer:InVehicle() and not pPlayer:GetAllowWeaponsInVehicle()) then
return
end
sBind = string_lower(sBind)
-- Last weapon switch
if (sBind == "lastinv") then
if (bPressed) then
local pLastWeapon = pPlayer:GetPreviousWeapon()
if (pLastWeapon:IsWeapon()) then
input_SelectWeapon(pLastWeapon)
end
end
return true
end
-- Close the menu
if (sBind == "cancelselect") then
if (bPressed and iCurSlot ~= 0) then
iCurSlot = 0
iCurPos = 1
flSelectTime = RealTime()
pPlayer:EmitSound(CANCEL_SOUND)
end
return true
end
-- Move to the weapon before the current
if (sBind == "invprev") then
if (not bPressed) then
return true
end
CheckBounds()
PrecacheWeps()
if (iWeaponCount == 0) then
return true
end
local bLoop = iCurSlot == 0
if (bLoop) then
local pActiveWeapon = pPlayer:GetActiveWeapon()
if (pActiveWeapon:IsValid()) then
local iSlot = pActiveWeapon:GetSlot() + 1
local tSlotCache = tCache[iSlot]
if (tSlotCache[1] ~= pActiveWeapon) then
iCurSlot = iSlot
iCurPos = 1
for i = 2, tCacheLength[iSlot] do
if (tSlotCache[i] == pActiveWeapon) then
iCurPos = i - 1
break
end
end
flSelectTime = RealTime()
pPlayer:EmitSound(MOVE_SOUND)
return true
end
iCurSlot = iSlot
end
end
if (bLoop or iCurPos == 1) then
repeat
if (iCurSlot <= 1) then
iCurSlot = MAX_SLOTS
else
iCurSlot = iCurSlot - 1
end
until(tCacheLength[iCurSlot] ~= 0)
iCurPos = tCacheLength[iCurSlot]
else
iCurPos = iCurPos - 1
end
flSelectTime = RealTime()
pPlayer:EmitSound(MOVE_SOUND)
return true
end
-- Move to the weapon after the current
if (sBind == "invnext") then
if (not bPressed) then
return true
end
CheckBounds()
PrecacheWeps()
-- Block the action if there aren't any weapons available
if (iWeaponCount == 0) then
return true
end
-- Lua's goto can't jump between child scopes
local bLoop = iCurSlot == 0
-- Weapon selection isn't currently open, move based on the active weapon's position
if (bLoop) then
local pActiveWeapon = pPlayer:GetActiveWeapon()
if (pActiveWeapon:IsValid()) then
local iSlot = pActiveWeapon:GetSlot() + 1
local iLen = tCacheLength[iSlot]
local tSlotCache = tCache[iSlot]
if (tSlotCache[iLen] ~= pActiveWeapon) then
iCurSlot = iSlot
iCurPos = 1
for i = 1, iLen - 1 do
if (tSlotCache[i] == pActiveWeapon) then
iCurPos = i + 1
break
end
end
flSelectTime = RealTime()
pPlayer:EmitSound(MOVE_SOUND)
return true
end
-- At the end of a slot, move to the next one
iCurSlot = iSlot
end
end
if (bLoop or iCurPos == tCacheLength[iCurSlot]) then
-- Loop through the slots until one has weapons
repeat
if (iCurSlot == MAX_SLOTS) then
iCurSlot = 1
else
iCurSlot = iCurSlot + 1
end
until(tCacheLength[iCurSlot] ~= 0)
-- Start at the beginning of the new slot
iCurPos = 1
else
-- Bump up the position
iCurPos = iCurPos + 1
end
flSelectTime = RealTime()
pPlayer:EmitSound(MOVE_SOUND)
return true
end
-- Keys 1-6
if (string_sub(sBind, 1, 4) == "slot") then
local iSlot = tonumber(string_sub(sBind, 5))
-- If the command is slot#, use it for the weapon HUD
-- Otherwise, let it pass through to prevent false positives
if (iSlot == nil) then
return
end
if (not bPressed) then
return true
end
CheckBounds()
PrecacheWeps()
-- Play a sound even if there aren't any weapons in that slot for "haptic" (really auditory) feedback
if (iWeaponCount == 0) then
pPlayer:EmitSound(MOVE_SOUND)
return true
end
-- If the slot number is in the bounds
if (iSlot <= MAX_SLOTS) then
-- If the slot is already open
if (iSlot == iCurSlot) then
-- Start back at the beginning
if (iCurPos == tCacheLength[iCurSlot]) then
iCurPos = 1
-- Move one up
else
iCurPos = iCurPos + 1
end
-- If there are weapons in this slot, display them
elseif (tCacheLength[iSlot] ~= 0) then
iCurSlot = iSlot
iCurPos = 1
end
flSelectTime = RealTime()
pPlayer:EmitSound(MOVE_SOUND)
end
return true
end
-- If the weapon selection is currently open
if (iCurSlot ~= 0) then
if (sBind == "+attack") then
-- Hide the selection
local pWeapon = tCache[iCurSlot][iCurPos]
iCurSlot = 0
iCurPos = 1
-- If the weapon still exists and isn't the player's active weapon
if (pWeapon:IsValid() and pWeapon ~= pPlayer:GetActiveWeapon()) then
input_SelectWeapon(pWeapon)
end
flSelectTime = RealTime()
pPlayer:EmitSound(SELECT_SOUND)
return true
end
-- Another shortcut for closing the selection
if (sBind == "+attack2") then
flSelectTime = RealTime()
pPlayer:EmitSound(CANCEL_SOUND)
iCurSlot = 0
iCurPos = 1
return true
end
end
end)