-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_dosound.txt
324 lines (244 loc) · 8.09 KB
/
cl_dosound.txt
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
--@name cl_dosound
--@author legokidlogan
--@client
----- THIS WILL BE REFACTORED SOON -----
soundTbl = soundTbl or {
Template = {
Path = "",
Pitch = 1,
Volume = 1,
Duration = nil,
Loop = false,
FadeMin = 700,
FadeMax = 1500,
Delay = nil,
},
}
permissionSatisfied = permissionSatisfied or false
local soundKeys = table.getKeys( soundTbl )
local soundTypeCount = #soundKeys
function soundIsUrl( path )
return string.sub( path, 1, 4 ) == "http"
end
function doSound( soundType, entityOrPos, override, ... )
if not permissionSatisfied then return end
local snd = soundTbl[soundType]
if not snd then return end
if bass.soundsLeft() == 0 then return end
override = override or {}
local path = override.Path or snd.Path
local bassList = snd._bassList
local entList = snd._entList
local posList = snd._posList
local extraArgs = { ... }
if not bassList then
bassList = {}
snd._bassList = bassList
end
if not entList then
entList = {}
snd._entList = entList
end
if not posList then
posList = {}
snd._posList = posList
end
if type( path ) == "function" then
path = path( ... )
end
if not path or path == "" then return end
local entity = type( entityOrPos ) ~= "Vector" and isValid( entityOrPos ) and entityOrPos
local pos = type( entityOrPos ) == "Vector" and entityOrPos
local flags = "noplay"
if entity or pos then
flags = flags .. " 3d"
end
local loop = snd.Loop
if type( loop ) == "function" then
loop = loop( ... )
end
if loop then
flags = flags .. " noblock"
end
if soundIsUrl( path ) then
bass.loadURL( path, flags, function( s )
local bassObj = s
local count = ( snd._bassCount or 0 ) + 1
snd._bassCount = count
bassList[count] = bassObj
entList[count] = entity
posList[count] = pos
hook.run( "LKL_DoSound_SoundLoaded", soundType, override, path, count, unpack( extraArgs ) )
end )
else
path = "sound/" .. path
bass.loadFile( path, flags, function( s )
local bassObj = s
local count = ( snd._bassCount or 0 ) + 1
snd._bassCount = count
bassList[count] = bassObj
entList[count] = entity
posList[count] = pos
hook.run( "LKL_DoSound_SoundLoaded", soundType, override, path, count, unpack( extraArgs ) )
end )
end
end
-- Removes all sounds of a given type if ind is not given
function stopSound( sound, ind )
local timerName = "LKL_DoSound_PlayDelayedSound_" .. sound
local snd = soundTbl[sound]
if not snd then return end -- Not a registered sound
timer.remove( timerName )
local bassList = snd._bassList
local count = snd._bassCount
if not bassList or count < 1 then return end -- Sound has never been played or currently has no active sounds
local entList = snd._entList
local posList = snd._posList
if ind then
if ind < 1 or ind > count then return end -- Out of bounds
local bassObj = bassList[ind]
if isValid( bassObj ) then
bassObj:stop()
end
table.remove( bassList, ind )
table.remove( entList, ind )
table.remove( posList, ind )
snd._bassCount = count - 1
else
for i = count, 1, -1 do
local bassObj = bassList[count]
if isValid( bassObj ) then
bassObj:stop()
end
bassList[i] = nil
entList[i] = nil
posList[i] = nil
end
snd._bassCount = 0
end
end
function stopAllSounds()
for snd in pairs( soundTbl ) do
stopSound( snd )
end
end
hook.add( "LKL_DoSound_SoundLoaded", "LKL_DoSound_PlaySound", function( soundType, override, path, ind, ... )
local snd = soundTbl[soundType]
local bassList = snd._bassList
local bassObj = bassList[ind]
if not bassObj or not bassObj:isValid() then
stopSound( soundType, ind )
return
end
local entList = snd._entList
local posList = snd._posList
local entity = entList[ind]
local pos = posList[ind]
local pitch = override.Pitch or snd.Pitch or 1
local volume = override.Volume or snd.Volume or 1
local fadeMin = override.FadeMin or snd.FadeMin or 700
local fadeMax = override.FadeMax or snd.FadeMax or 1500
local delay = override.Delay or snd.Delay
local duration
local loop
if override.Duration ~= nil then
duration = override.Duration
else
duration = snd.Duration or false
end
if override.Loop ~= nil then
loop = override.Loop
else
loop = snd.Loop or false
end
if type( pitch ) == "function" then
pitch = pitch( entityOrPos, soundType, path, ... )
end
if type( volume ) == "function" then
volume = volume( entityOrPos, soundType, path, pitch, ... )
end
if type( duration ) == "function" then
duration = duration( entityOrPos, soundType, path, pitch, ... )
end
if type( loop ) == "function" then
loop = loop( entityOrPos, soundType, path, pitch, ... )
end
if type( fadeMin ) == "function" then
fadeMin = fadeMin( entityOrPos, soundType, path, pitch, ... )
end
if type( fadeMax ) == "function" then
fadeMax = fadeMax( entityOrPos, soundType, path, pitch, ... )
end
if type( delay ) == "function" then
delay = delay( entityOrPos, soundType, path, pitch, ... )
end
bassObj:setPitch( pitch )
bassObj:setVolume( volume )
bassObj:setFade( fadeMin, fadeMax )
bassObj:setLooping( loop )
--bassObj:play()
if entity then
bassObj:setPos( entity:getPos() )
elseif pos then
bassObj:setPos( pos )
end
local timerName = "LKL_DoSound_PlayDelayedSound_" .. soundType
timer.create( timerName, delay and delay > 0 or 0, 1, function()
bassObj:play()
if not duration or duration <= 0 then return end
timer.simple( duration, function()
if bassObj and bassObj:isValid() then
ind = table.keyFromValue( bassList, bassObj ) -- In case the index changed from a lower-ind sound stopping
table.remove( bassList, ind )
table.remove( entList, ind )
table.remove( posList, ind )
snd._bassCount = math.max( snd._bassCount - 1, 0 )
bassObj:stop()
end
hook.run( "LKL_DoSound_SoundFinished", entity or pos, soundType )
end )
end )
end )
hook.add( "think", "LKL_DoSound_MoveSounds", function()
for i = 1, soundTypeCount do
local snd = soundTbl[soundKeys[i]]
local count = snd._bassCount
if count and count > 0 then
local bassList = snd._bassList
local entList = snd._entList
for i2 = 1, count do
local ent = entList[i2]
if isValid( ent ) then
local bassObj = bassList[i2]
if bassObj and bassObj:isValid() then
bassObj:setPos( ent:getPos() )
end
end
end
end
end
end )
net.receive( "LKL_DoSound_ExtendSoundTbl", function()
local svTbl = net.readTable()
for catName, cat in pairs( svTbl ) do
if cat.Path then
soundTbl["sv_" .. catName] = cat -- Individual sound entry
else
for sound, snd in pairs( cat ) do -- Sound category
soundTbl["sv_" .. catName .. "_" .. sound] = snd
end
end
end
local soundKeys = table.getKeys( soundTbl )
local soundTypeCount = #soundKeys
end )
net.receive( "LKL_DoSound_PlayServerSound", function()
local soundType = net.readString()
local ent = net.readEntity()
local override = net.readTable()
doSound( "sv_" .. soundType, ent, override )
end )
timer.simple( 0.1, function()
net.start( "LKL_DoSound_ClientExists" )
net.send()
end )