forked from FrazzIe/mumble-voip-fivem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
326 lines (275 loc) · 8.55 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
320
321
322
323
324
325
326
local voiceData = {}
local radioData = {}
local callData = {}
local playerServerId = GetPlayerServerId(PlayerId())
-- Functions
function SetVoiceData(key, value)
TriggerServerEvent("mumble:SetVoiceData", key, value)
end
-- Events
RegisterNetEvent("mumble:SetVoiceData")
AddEventHandler("mumble:SetVoiceData", function(voice, radio, call)
voiceData = voice
if radio then
radioData = radio
end
if call then
callData = call
end
end)
RegisterNetEvent("mumble:RadioSound")
AddEventHandler("mumble:RadioSound", function(snd, channel)
if channel <= mumbleConfig.radioClickMaxChannel then
if mumbleConfig.micClicks then
if (snd and mumbleConfig.micClickOn) or (not snd and mumbleConfig.micClickOff) then
SendNUIMessage({ sound = (snd and "audio_on" or "audio_off"), volume = mumbleConfig.micClickVolume })
end
end
end
end)
AddEventHandler("onClientResourceStart", function (resourceName)
if GetCurrentResourceName() ~= resourceName then
return
end
TriggerServerEvent("mumble:Initialise")
end)
-- Simulate PTT when radio is active
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
local playerData = voiceData[playerServerId]
local playerMode = 2
local playerRadio = 0
local playerRadioActive = false
local playerCall = 0
local playerCallSpeaker = false
if playerData ~= nil then
playerMode = playerData.mode or 2
playerRadio = playerData.radio or 0
playerRadioActive = playerData.radioActive or false
playerCall = playerData.call or 0
playerCallSpeaker = playerData.callSpeaker or false
end
if playerRadioActive then -- Force PTT enabled
SetControlNormal(0, 249, 1.0)
SetControlNormal(1, 249, 1.0)
SetControlNormal(2, 249, 1.0)
end
if IsControlJustPressed(0, mumbleConfig.controls.proximity.key) then
if mumbleConfig.controls.speaker.key == mumbleConfig.controls.proximity.key and not ((mumbleConfig.controls.speaker.secondary == nil) and true or IsControlPressed(0, mumbleConfig.controls.speaker.secondary)) then
local voiceMode = playerMode
local newMode = voiceMode + 1
if newMode > #mumbleConfig.voiceModes then
voiceMode = 1
else
voiceMode = newMode
end
SetVoiceData("mode", voiceMode)
end
end
if mumbleConfig.radioEnabled then
if not mumbleConfig.controls.radio.pressed then
if IsControlJustPressed(0, mumbleConfig.controls.radio.key) then
if playerRadio > 0 then
SetVoiceData("radioActive", true)
playerData.radioActive = true
mumbleConfig.controls.radio.pressed = true
Citizen.CreateThread(function()
while IsControlPressed(0, mumbleConfig.controls.radio.key) do
Citizen.Wait(0)
end
SetVoiceData("radioActive", false)
playerData.radioActive = false
mumbleConfig.controls.radio.pressed = false
end)
end
end
end
else
if playerRadioActive then
SetVoiceData("radioActive", false)
playerData.radioActive = false
end
end
if mumbleConfig.radioSpeakerEnabled then
if ((mumbleConfig.controls.speaker.secondary == nil) and true or IsControlPressed(0, mumbleConfig.controls.speaker.secondary)) then
if IsControlJustPressed(0, mumbleConfig.controls.speaker.key) then
if playerCall > 0 then
SetVoiceData("callSpeaker", not playerCallSpeaker)
end
end
end
end
end
end)
-- UI
Citizen.CreateThread(function()
while true do
Citizen.Wait(200)
local playerId = PlayerId()
local playerData = voiceData[playerServerId]
local playerTalking = NetworkIsPlayerTalking(playerId)
local playerMode = 2
local playerRadio = 0
local playerRadioActive = false
local playerCall = 0
local playerCallSpeaker = false
if playerData ~= nil then
playerMode = playerData.mode or 2
playerRadio = playerData.radio or 0
playerRadioActive = playerData.radioActive or false
playerCall = playerData.call or 0
playerCallSpeaker = playerData.callSpeaker or false
end
-- Update UI
SendNUIMessage({
talking = playerTalking,
mode = mumbleConfig.voiceModes[playerMode][2],
radio = mumbleConfig.radioChannelNames[playerRadio] ~= nil and mumbleConfig.radioChannelNames[playerRadio] or playerRadio,
radioActive = playerRadioActive,
call = playerCall,
speaker = playerCallSpeaker,
})
end
end)
-- Main thread
Citizen.CreateThread(function()
local talkingAnim = { "mic_chatter", "mp_facial" }
local normalAnim = { "mood_normal_1", "facials@gen_male@base" }
RequestAnimDict(talkingAnim[3])
while not HasAnimDictLoaded(talkingAnim[2]) do
Citizen.Wait(150)
end
RequestAnimDict(normalAnim[2])
while not HasAnimDictLoaded(normalAnim[2]) do
Citizen.Wait(150)
end
while true do
Citizen.Wait(500)
local playerId = PlayerId()
local playerPed = PlayerPedId()
local playerPos = GetPedBoneCoords(playerPed, headBone)
local playerList = GetActivePlayers()
local playerData = voiceData[playerServerId]
local playerRadio = 0
local playerCall = 0
if playerData ~= nil then
playerRadio = playerData.radio or 0
playerCall = playerData.call or 0
end
local voiceList = {}
local muteList = {}
local callList = {}
local radioList = {}
for i = 1, #playerList do -- Proximity based voice (probably won't work for infinity?)
local remotePlayerId = playerList[i]
if playerId ~= remotePlayerId then
local remotePlayerServerId = GetPlayerServerId(remotePlayerId)
local remotePlayerPed = GetPlayerPed(remotePlayerId)
local remotePlayerPos = GetPedBoneCoords(remotePlayerPed, headBone)
local remotePlayerData = voiceData[remotePlayerServerId]
local distance = #(playerPos - remotePlayerPos)
local mode = 2
local radio = 0
local radioActive = false
local call = 0
local callSpeaker = false
if remotePlayerData ~= nil then
mode = remotePlayerData.mode or 2
radio = remotePlayerData.radio or 0
radioActive = remotePlayerData.radioActive or false
call = remotePlayerData.call or 0
callSpeaker = remotePlayerData.callSpeaker or false
end
-- Check if player is in range
if distance < mumbleConfig.voiceModes[mode][1] then
local volume = 1.0 - (distance / mumbleConfig.voiceModes[mode][1])^0.5
if volume < 0 then
volume = 0.0
end
voiceList[#voiceList + 1] = {
id = remotePlayerServerId,
player = remotePlayerId,
volume = volume,
}
if mumbleConfig.callSpeakerEnabled then
if call > 0 then -- Collect all players in the phone call
if callSpeaker then
local callParticipants = callData[call]
if callParticipants ~= nil then
for id, _ in pairs(callParticipants) do
if id ~= remotePlayerServerId then
callList[id] = true
end
end
end
end
end
end
if mumbleConfig.radioSpeakerEnabled then
if radio > 0 then -- Collect all players in the radio channel
local radioParticipants = radioData[radio]
if radioParticipants then
for id, _ in pairs(radioParticipants) do
if id ~= remotePlayerServerId then
radioList[id] = true
end
end
end
end
end
else
muteList[#muteList + 1] = {
id = remotePlayerServerId,
player = remotePlayerId,
volume = 0.0,
radio = radio,
radioActive = radioActive,
distance = distance,
call = call,
}
end
end
end
for j = 1, #voiceList do
MumbleSetVolumeOverride(voiceList[j].player, voiceList[j].volume)
end
for j = 1, #muteList do
if callList[muteList[j].id] or radioList[muteList[j].id] then
if muteList[j].distance < mumbleConfig.speakerRange then
muteList[j].volume = 1.0 - (muteList[j].distance / mumbleConfig.speakerRange)^0.5
end
end
if muteList[j].radio > 0 and muteList[j].radio == playerRadio and muteList[j].radioActive then
muteList[j].volume = 1.0
end
if muteList[j].call > 0 and muteList[j].call == playerCall then
muteList[j].volume = 1.2
end
MumbleSetVolumeOverride(muteList[j].player, muteList[j].volume)
end
end
end)
-- Exports
function SetRadioChannel(channel)
local channel = tonumber(channel)
if channel ~= nil then
SetVoiceData("radio", channel)
end
end
function SetCallChannel(channel)
local channel = tonumber(channel)
if channel ~= nil then
SetVoiceData("call", channel)
end
end
exports("SetRadioChannel", SetRadioChannel)
exports("addPlayerToRadio", SetRadioChannel)
exports("removePlayerFromRadio", function()
SetRadioChannel(0)
end)
exports("SetCallChannel", SetCallChannel)
exports("addPlayerToCall", SetCallChannel)
exports("removePlayerFromCall", function()
SetCallChannel(0)
end)