forked from Parow/prw_radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_voice.lua
340 lines (289 loc) · 10.2 KB
/
cl_voice.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
local VoiceMode = {
{ dist = 3, message = "Voice range set on 3 meters." },
{ dist = 8, message = "Voice range set on 8 meters." },
{ dist = 14, message = "Voice range set on 14 meters." },
{ veh = true, dist = 4, func = function(ped) return IsPedInAnyVehicle(ped) end, message = "Voice range set to your vehicle." },
}
local Keys = {
["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57,
["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177,
["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["["] = 39, ["]"] = 40, ["ENTER"] = 18,
["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182,
["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81,
["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70,
["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178,
["LEFT"] = 174, ["RIGHT"] = 175, ["TOP"] = 27, ["DOWN"] = 173,
["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118
}
local Voice = {}
Voice.Listeners = {}
Voice.ListenersRadio = {}
Voice.Mode = 2
Voice.distance = 8.0
local currentFreq = 0
Voice.onlyVehicle = false
local muted = false
local function SendVoiceToPlayer(intPlayer, boolSend)
Citizen.InvokeNative(0x97DD4C5944CC2E6A, intPlayer, boolSend)
end
local function GetPlayers()
local players = {}
for i = 0, 64 do
if NetworkIsPlayerActive(i) then
players[#players + 1] = i
end
end
return players
end
function round2(num, numDecimalPlaces)
return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end
function Voice:UpdateVoices()
--print(json.encode(Voice.Listeners))
local ped = GetPlayerPed(-1)
local InVeh = IsPedInAnyVehicle(ped)
if Voice.onlyVehicle and not InVeh then
Voice.Mode = 1
Voice:OnModeModified()
end
for _,v in pairs(GetPlayers()) do
local otherPed, serverID = GetPlayerPed(v), GetPlayerServerId(v)
if otherPed and Voice:CanPedBeListened(ped, otherPed) or Voice.ListenersRadio[serverID] == true then
if not Voice.Listeners[serverID] then
Voice.Listeners[serverID] = true
end
if Voice.ListenersRadio[serverID] ~= true then
-- print(v)
SendVoiceToPlayer(v, true)
elseif Voice.ListenersRadio[serverID] then
local playerIdx = GetPlayerFromServerId(serverID)
SendVoiceToPlayer(playerIdx,true)
else
SendVoiceToPlayer(v, true)
end
elseif Voice.Listeners[serverID] then
Voice.Listeners[serverID] = false
SendVoiceToPlayer(v, false)
elseif Voice.ListenersRadio[serverID] ~= true then
local playerIdx = GetPlayerFromServerId(serverID)
SendVoiceToPlayer(playerIdx,false)
end
end
if Voice.onlyVehicle and not InVeh then
Voice.Mode = 1
Voice:OnModeModified()
end
end
local function ShowAboveRadarMessage(message)
SetNotificationTextEntry("jamyfafi")
AddTextComponentString(message)
return DrawNotification(0, 1)
end
local notifID
function Voice:OnModeModified()
local modeData = VoiceMode[self.Mode]
if modeData then
self.distance = modeData.dist
self.onlyVehicle = modeData.veh
if modeData.message then
if notifID then RemoveNotification(notifID) end
notifID = ShowAboveRadarMessage(modeData.message)
Citizen.SetTimeout(4000, function() if notifID then RemoveNotification(notifID) end end)
end
self:UpdateVoices()
end
end
function Voice:CanPedBeListened(ped, otherPed)
local listenerHeadPos, InSameVeh = GetPedBoneCoords(otherPed, 12844, .0, .0, .0), IsPedInAnyVehicle(ped) and GetVehiclePedIsUsing(ped) == GetVehiclePedIsUsing(otherPed)
local distance = GetDistanceBetweenCoords(listenerHeadPos, GetEntityCoords(ped))
local bypassVOIP, checkDistance = InSameVeh, self.distance
return bypassVOIP or (not self.onlyVehicle and (HasEntityClearLosToEntityInFront(ped, otherPed) or distance < (math.max(0, math.min(18, checkDistance)) * .6)) and distance < checkDistance)
end
function Voice:ShouldSendVoice()
return NetworkIsPlayerTalking(PlayerId()) or IsControlPressed(0, 249)
end
local shouldReset = false
Citizen.CreateThread(function()
for i = 0, 63 do SendVoiceToPlayer(i, false) end
NetworkSetTalkerProximity(-1000.0)
while true do
Citizen.Wait(300)
local sendVoice = Voice:ShouldSendVoice()
if sendVoice then
if not shouldReset then
shouldReset = true
end
elseif not sendVoice and shouldReset then
shouldReset = false
for i = 0, 63 do
SendVoiceToPlayer(i, false)
end
end
Voice:UpdateVoices()
end
end)
local function DrawText3D(x,y,z, canSee)
local _, _, _ = World3dToScreen2d(x,y,z)
local px, py, pz = table.unpack(GetGameplayCamCoords())
local dist = GetDistanceBetweenCoords(px,py,pz, x,y,z, 1)
local scale = ( 1 / dist ) * 20
scale = scale * ( ( 1 / GetGameplayCamFov() ) * 100 )
local color = canSee and {0, 70, 200} or {255, 255, 255}
SetDrawOrigin(x,y,z, 0)
DrawRect(.0, .02, .0003 * scale, .0375 * scale, color[1], color[2], color[3], 255)
ClearDrawOrigin()
end
local function UpdateVocalMode(mode)
local nextMode = mode or Voice.Mode + 1
while not VoiceMode[nextMode] or (VoiceMode[nextMode] and VoiceMode[nextMode].func and not VoiceMode[nextMode].func(GetPlayerPed(-1))) do
nextMode = VoiceMode[nextMode + 1] or 1
end
Voice.Mode = nextMode
Voice:OnModeModified()
end
RegisterNetEvent('parow:mute')
AddEventHandler('parow:mute', function(bool,sour)
Voice.ListenersRadio[sour] = bool
end)
RegisterNetEvent('parow:SyncRadio')
AddEventHandler('parow:SyncRadio', function(azz,bool)
for i = 1,#azz,1 do
Voice.ListenersRadio[azz[i].source] = bool
end
end)
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsControlJustPressed(1, 56) then
UpdateVocalMode()
end
if IsControlPressed(0, Keys["LEFTCTRL"]) and IsControlPressed(0, Keys["H"]) then
OpenRadioMenu()
end
if IsControlPressed(1, 56) then
local ped = GetPlayerPed(-1)
local headPos = GetPedBoneCoords(ped, 12844, .0, .0, .0)
for _,v in pairs(GetPlayers()) do
local otherPed = GetPlayerPed(v)
if otherPed and Voice.Listeners[GetPlayerServerId(v)] then
local entPos = GetEntityCoords(otherPed)
DrawText3D(entPos.x, entPos.y, entPos.z, true)
end
end
local distance = Voice.distance + .0
DrawMarker(28, headPos, 0.0, 0.0, 0.0, 0.0, 0.0, .0, distance + .0, distance + .0, distance + .0, 20, 192, 255, 70, 0, 0, 2, 0, 0, 0, 0)
end
end
end)
RegisterNetEvent('parow:SyncRadio2')
AddEventHandler('parow:SyncRadio2', function(azz)
-- print(json.encode(azz))
for i = 1,#azz,1 do
-- print(azz[i].freq)
-- print(CurrentFreq)
if azz[i].freq == currentFreq then
Voice.ListenersRadio[azz[i].source] = true
else
Voice.ListenersRadio[azz[i].source] = false
end
end
end)
--------
--MENU--
--------
local _menuPool = NativeUI.CreatePool()
_menuPool:RefreshIndex()
local radioMenu = nil
local muet = true
local ppks = nil
local freq = nil
function OpenRadioMenu()
if radioMenu == nil then
radioMenu = NativeUI.CreateMenu("Radio", "Frequency", 5, 200)
_menuPool:Add(radioMenu)
AddRadioMenu(radioMenu)
radioMenu:Visible(true)
end
if not _menuPool:IsAnyMenuOpen() then
radioMenu:Visible(true)
else
_menuPool:CloseAllMenus()
end
end
function AddRadioMenu(menu)
pp = NativeUI.CreateItem('Frequency', "")
pp:RightLabel(freq)
menu:AddItem(pp)
menu.OnItemSelect = function(m, i, ind)
if ind == 1 then
drawnotifcolor("Frequency range incorrect (range 0 to 100)", 26)
dm = gettxt2(freq)
drawnotifcolor("Frequency preset : " .. tostring(dm), 18)
dm = tonumber(dm)
if dm ~= nil then
if dm > 0 then
dm = round2(dm, 2)
if dm <= 100 then
if freq == nil then
vali = NativeUI.CreateItem("Valider", "")
m:AddItem(vali)
_menuPool:RefreshIndex()
m:CurrentSelection(ind - 1)
end
freq = tonumber(dm)
pp:RightLabel(dm)
else
drawnotifcolor("Frequency range incorrect (range 0 to 100)", 6)
end
end
end
end
if ind == 2 then
drawnotifcolor("Frequency set to " .. freq, 18)
TriggerServerEvent("parow:SetFreq", freq)
_menuPool:RefreshIndex()
currentFreq = freq
if ppks == nil then
--menu:RemoveItemAt(3)
ppks = NativeUI.CreateCheckboxItem('Radio status', muet, "")
menu:AddItem(ppks)
_menuPool:RefreshIndex()
end
end
end
menu.OnCheckboxChange = function(_, _, checked)
TriggerServerEvent("parow:ToggleRadio", freq, checked)
end
_menuPool:RefreshIndex()
end
function drawnotifcolor(text, color)
Citizen.InvokeNative(0x92F0DA1E27DB96DC, tonumber(color))
SetNotificationTextEntry("STRING")
AddTextComponentString(text)
DrawNotification(false, true)
end
function gettxt2(txtt)
AddTextEntry('FMMC_MPM_NA', "Texte")
DisplayOnscreenKeyboard(1, "FMMC_MPM_NA", "", txtt, "", "", "", 100)
while (UpdateOnscreenKeyboard() == 0) do
DisableAllControlActions(0);
Wait(0);
end
if (GetOnscreenKeyboardResult()) then
local result = GetOnscreenKeyboardResult()
if tonumber(result) ~= nil then
if tonumber(result) > 1 then
return result
else
end
else
return result
end
end
end
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
_menuPool:ProcessMenus()
end
end)