-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.lua
319 lines (238 loc) · 8.79 KB
/
client.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
local Config = require 'Config'
-- soundId Tables --
local sirenVehicles = {}
local hornVehicles = {}
-- Natives Used --
local Wait = Wait
local TriggerServerEvent = TriggerServerEvent
local SetVehRadioStation = SetVehRadioStation
local SetVehicleRadioEnabled = SetVehicleRadioEnabled
local DisableControlAction = DisableControlAction
local GetVehicleClass = GetVehicleClass
local IsPedInAnyHeli = IsPedInAnyHeli
local IsPedInAnyPlane = IsPedInAnyPlane
local PlaySoundFromEntity = PlaySoundFromEntity
local GetSoundId = GetSoundId
local DoesEntityExist = DoesEntityExist
local IsEntityDead = IsEntityDead
local StopSound = StopSound
local ReleaseSoundId = ReleaseSoundId
local AddStateBagChangeHandler = AddStateBagChangeHandler
local NetworkGetEntityOwner = NetworkGetEntityOwner
local SetVehicleHasMutedSirens = SetVehicleHasMutedSirens
local SetVehicleSiren = SetVehicleSiren
-- Localized Functions --
local function releaseSound(veh, soundId, forced)
if forced and (DoesEntityExist(veh) and not IsEntityDead(veh)) then return end
StopSound(soundId)
ReleaseSoundId(soundId)
return true
end
local function isVehAllowed()
if cache.seat ~= -1 or GetVehicleClass(cache.vehicle) ~= 18 or IsPedInAnyHeli(cache.vehicle) or IsPedInAnyPlane(cache.vehicle) then
return false
end
return true
end
-- Cleanup Loop --
CreateThread(function()
while true do
for veh, soundId in pairs(sirenVehicles) do
if releaseSound(veh, soundId, true) then
sirenVehicles[veh] = nil
end
end
for veh, soundId in pairs(hornVehicles) do
if releaseSound(veh, soundId, true) then
hornVehicles[veh] = nil
end
end
Wait(1000)
end
end)
-- Cache Events --
lib.onCache('seat', function(seat)
if seat ~= -1 then return end
SetTimeout(0, function()
if not isVehAllowed() then return end
SetVehRadioStation(cache.vehicle, 'OFF')
SetVehicleRadioEnabled(cache.vehicle, false)
if not Entity(cache.vehicle).state.stateEnsured then
TriggerServerEvent('Renewed-Sirensync:server:SyncState', VehToNet(cache.vehicle))
end
while cache.seat == -1 do
DisableControlAction(0, 80, true) -- R
DisableControlAction(0, 81, true) -- .
DisableControlAction(0, 82, true) -- ,
DisableControlAction(0, 83, true) -- =
DisableControlAction(0, 84, true) -- -
DisableControlAction(0, 85, true) -- Q
DisableControlAction(0, 86, true) -- E
DisableControlAction(0, 172, true) -- Up arrow
Wait(0)
end
end)
end)
lib.onCache('vehicle', function(value)
if value or cache.seat ~= -1 then return end
local state = Entity(cache.vehicle).state
if not state.stateEnsured then return end
if Config.sirenShutOff then
if state.sirenMode ~= 0 then
state:set('sirenMode', 0, true)
end
end
if state.horn then
state:set('horn', false, true)
end
end)
-- Statebags & Keybinds --
local function stateBagWrapper(keyFilter, cb)
return AddStateBagChangeHandler(keyFilter, '', function(bagName, _, value, _, replicated)
local netId = tonumber(bagName:gsub('entity:', ''), 10)
local loaded = netId and lib.waitFor(function()
if NetworkDoesEntityExistWithNetworkId(netId) then return true end
end, 'Timeout while waiting for entity to exist', 5000)
local entity = loaded and NetToVeh(netId)
if entity then
local amOwner = NetworkGetEntityOwner(entity) == cache.playerId
if amOwner == replicated then
cb(entity, value)
end
end
end)
end
-- Police Lights --
stateBagWrapper('lightsOn', function(veh, value)
SetVehicleHasMutedSirens(veh, true)
SetVehicleSiren(veh, value)
end)
local policeLights = lib.addKeybind({
name = 'policeLights',
description = 'Press this button to use your siren',
defaultKey = Config.Controls.PoliceLights,
onPressed = function()
if not isVehAllowed() then return end
local state = Entity(cache.vehicle).state
if not state.stateEnsured then return end
PlaySoundFrontend(-1, 'NAV_UP_DOWN', 'HUD_FRONTEND_DEFAULT_SOUNDSET', true)
local curMode = state.lightsOn
state:set('lightsOn', not curMode, true)
if not curMode or state.sirenMode == 0 then return end
state:set('sirenMode', 0, true)
end
})
-- Police Horns --
local restoreSiren = 0
stateBagWrapper('horn', function(veh, value)
local relHornId = hornVehicles[veh]
if relHornId then
if releaseSound(veh, relHornId) then
hornVehicles[veh] = nil
end
end
if not value then return end
local soundId = GetSoundId()
hornVehicles[veh] = soundId
local soundName = Config.addonHorns[GetEntityModel(veh)] or 'SIRENS_AIRHORN'
PlaySoundFromEntity(soundId, soundName, veh, 0, false, 0)
end)
local policeHorn = lib.addKeybind({
name = 'policeHorn',
description = 'Hold this button to use your vehicle Horn',
defaultKey = Config.Controls.policeHorn,
onPressed = function()
if not isVehAllowed() then return end
local state = Entity(cache.vehicle).state
if not state.stateEnsured then return end
if state.sirenMode == 0 then
restoreSiren = state.sirenMode
state:set('sirenMode', 0, true)
end
state:set('horn', not state.horn, true)
end,
onReleased = function()
if not cache.vehicle or GetVehicleClass(cache.vehicle) ~= 18 then return end
local state = Entity(cache.vehicle).state
SetTimeout(0, function()
if state.horn then
state:set('horn', false, true)
end
if state.lightsOn and state.sirenMode == 0 and restoreSiren > 0 then
state:set('sirenMode', restoreSiren, true)
restoreSiren = 0
end
end)
end,
})
-- Siren Modes and Toggles --
stateBagWrapper('sirenMode', function(veh, soundMode)
local usedSound = sirenVehicles[veh]
if usedSound then
if releaseSound(veh, usedSound) then
sirenVehicles[veh] = nil
end
end
if soundMode == 0 or not soundMode then return end
local soundId = GetSoundId()
sirenVehicles[veh] = soundId
if soundMode == 1 then
PlaySoundFromEntity(soundId, 'VEHICLES_HORNS_SIREN_1', veh, 0, false, 0)
elseif soundMode == 2 then
PlaySoundFromEntity(soundId, 'VEHICLES_HORNS_SIREN_2', veh, 0, false, 0)
elseif soundMode == 3 then
PlaySoundFromEntity(soundId, 'VEHICLES_HORNS_POLICE_WARNING', veh, 0, false, 0)
end
end)
local sirenToggle = lib.addKeybind({
name = 'sirenToggle',
description = 'Press this button to use your siren',
defaultKey = Config.Controls.sirenToggle,
onPressed = function()
if not isVehAllowed() then return end
local state = Entity(cache.vehicle).state
if not state.stateEnsured or not state.lightsOn or state.horn then return end
PlaySoundFrontend(-1, 'NAV_UP_DOWN', 'HUD_FRONTEND_DEFAULT_SOUNDSET', true)
local newSiren = state.sirenMode > 0 and 0 or 1
state:set('sirenMode', newSiren, true)
end
})
local Rpressed = false
lib.addKeybind({
name = 'sirenCycle',
description = 'Press this button to cycle through your sirens',
defaultKey = Config.Controls.sirenCycle,
onPressed = function()
if not isVehAllowed() then return end
local state = Entity(cache.vehicle).state
if not state.stateEnsured then return end
PlaySoundFrontend(-1, 'NAV_UP_DOWN', 'HUD_FRONTEND_DEFAULT_SOUNDSET', true)
if state.sirenMode == 0 and not Rpressed then
local sirenMode = state.sirenMode > 0 and 0 or 1
state:set('sirenMode', sirenMode, true)
sirenToggle:disable(true)
policeLights:disable(true)
policeHorn:disable(true)
Rpressed = true
elseif state.sirenMode > 0 and state.lightsOn and not Rpressed then
local newSiren = state.sirenMode + 1 > 3 and 1 or state.sirenMode + 1
state:set('sirenMode', newSiren, true)
end
end,
onReleased = function()
if not Rpressed then return end
PlaySoundFrontend(-1, 'NAV_UP_DOWN', 'HUD_FRONTEND_DEFAULT_SOUNDSET', true)
sirenToggle:disable(false)
policeLights:disable(false)
policeHorn:disable(false)
if cache.vehicle then
SetTimeout(0, function()
local state = Entity(cache.vehicle).state
if state.sirenMode > 0 then
state:set('sirenMode', 0, true)
end
Rpressed = false
end)
end
end
})