Skip to content

Commit

Permalink
fix(voice): improve responsiveness of talking on phones
Browse files Browse the repository at this point in the history
  • Loading branch information
AvarianKnight committed Dec 29, 2022
1 parent 62cd520 commit ba0f895
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 58 deletions.
8 changes: 3 additions & 5 deletions client/init/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ if gameVersion == 'fivem' then
submixIndicies['call'] = callEffectId

-- Callback is expected to return data in an array, this is for compatibility sake with js, index 0 should be the name and index 1 should be the submixId
-- the callback is sent the effectSlot it can register to, not sure if this is needed, but its here for safety
exports("registerCustomSubmix", function(callback)
local submixTable = callback()
type_check({submixTable, "table"})
Expand All @@ -101,9 +102,6 @@ if gameVersion == 'fivem' then
logger.info("Creating submix %s with submixId %s", submixName, submixId)
submixIndicies[submixName] = submixId
end)

-- Trigger an event so resources can know when to register their submix
-- Implementers will still have to do custom logic if their script has to restart during runtime
TriggerEvent("pma-voice:registerCustomSubmixes")
end

Expand Down Expand Up @@ -167,8 +165,8 @@ end

local function updateVolumes(voiceTable, override)
for serverId, talking in pairs(voiceTable) do
if not talking or serverId == playerServerId then goto skip_iter end
MumbleSetVolumeOverrideByServerId(serverId, override)
if serverId == playerServerId then goto skip_iter end
MumbleSetVolumeOverrideByServerId(serverId, talking and override or -1.0)
::skip_iter::
end
end
Expand Down
38 changes: 38 additions & 0 deletions client/init/proximity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ function addNearbyPlayers()
if LocalPlayer.state.disableProximity then return end
MumbleAddVoiceChannelListen(playerServerId)
MumbleAddVoiceTargetChannel(voiceTarget, playerServerId)

for source, _ in pairs(callData) do
if source ~= playerServerId then
MumbleAddVoiceTargetChannel(voiceTarget, source)
end
end


local players = GetActivePlayers()
for i = 1, #players do
local ply = players[i]
Expand Down Expand Up @@ -169,3 +177,33 @@ AddEventHandler("onClientResourceStop", function(resource)
end
end
end)

exports("addVoiceMode", function(distance, name)
for i = 1, #Cfg.voiceModes do
local voiceMode = Cfg.voiceModes[i]
if voiceMode[2] == name then
logger.verbose("Already had %s, overwritting instead", name)
voiceMode[1] = distance
return
end
end
Cfg.voiceModes[#Cfg.voiceModes + 1] = {distance, name}
end)

exports("removeVoiceMode", function(name)
for i = 1, #Cfg.voiceModes do
local voiceMode = Cfg.voiceModes[i]
if voiceMode[2] == name then
table.remove(Cfg.voiceModes, i)
-- Reset our current range if we had it
if mode == i then
local newMode = Cfg.voiceModes[1]
mode = 1
setProximityState(newMode[i], false)
end
return true
end
end

return false
end)
35 changes: 3 additions & 32 deletions client/module/phone.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
local callChannel = 0

---function createCallThread
---creates a call thread to listen for key presses
local function createCallThread()
CreateThread(function()
local changed = false
while callChannel ~= 0 do
-- check if they're pressing voice keybinds
if MumbleIsPlayerTalking(PlayerId()) and not changed then
changed = true
playerTargets(radioPressed and radioData or {}, callData)
TriggerServerEvent('pma-voice:setTalkingOnCall', true)
elseif changed and MumbleIsPlayerTalking(PlayerId()) ~= 1 then
changed = false
MumbleClearVoiceTargetPlayers(voiceTarget)
TriggerServerEvent('pma-voice:setTalkingOnCall', false)
end
Wait(0)
end
end)
end

RegisterNetEvent('pma-voice:syncCallData', function(callTable, channel)
callData = callTable
for tgt, enabled in pairs(callTable) do
for tgt, _ in pairs(callTable) do
if tgt ~= playerServerId then
toggleVoice(tgt, enabled, 'call')
toggleVoice(tgt, true, 'call')
end
end
end)

RegisterNetEvent('pma-voice:setTalkingOnCall', function(tgt, enabled)
if tgt ~= playerServerId then
callData[tgt] = enabled
toggleVoice(tgt, enabled, 'call')
end
end)

RegisterNetEvent('pma-voice:addPlayerToCall', function(plySource)
callData[plySource] = false
callData[plySource] = true
end)

RegisterNetEvent('pma-voice:removePlayerFromCall', function(plySource)
Expand Down Expand Up @@ -68,7 +40,6 @@ function setCallChannel(channel)
sendUIMessage({
callInfo = channel
})
createCallThread()
end

exports('setCallChannel', setCallChannel)
Expand Down
22 changes: 1 addition & 21 deletions server/module/phone.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function addPlayerToCall(source, callChannel)
TriggerClientEvent('pma-voice:addPlayerToCall', player, source)
end
end
callData[callChannel][source] = false
callData[callChannel][source] = true
voiceData[source] = voiceData[source] or defaultTable(source)
voiceData[source].call = callChannel
TriggerClientEvent('pma-voice:syncCallData', source, callData[callChannel])
Expand Down Expand Up @@ -72,23 +72,3 @@ exports('setPlayerCall', setPlayerCall)
RegisterNetEvent('pma-voice:setPlayerCall', function(callChannel)
setPlayerCall(source, callChannel)
end)

function setTalkingOnCall(talking)
if GetConvarInt('voice_enableCalls', 1) ~= 1 then return end
local source = source
voiceData[source] = voiceData[source] or defaultTable(source)
local plyVoice = voiceData[source]
local callTbl = callData[plyVoice.call]
if callTbl then
logger.verbose('[call] %s %s talking in call %s', source, talking and 'started' or 'stopped', plyVoice.call)
for player, _ in pairs(callTbl) do
if player ~= source then
logger.verbose('[call] Sending event to %s to tell them that %s is talking', player, source)
TriggerClientEvent('pma-voice:setTalkingOnCall', player, source, talking)
end
end
else
logger.verbose('[call] %s tried to talk in call %s, but it doesnt exist.', source, plyVoice.call)
end
end
RegisterNetEvent('pma-voice:setTalkingOnCall', setTalkingOnCall)

2 comments on commit ba0f895

@Legacy-TacticalGamingInteractive

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to improve functionality with scripts like NPWD (New Phone Who Dis) or?

@AvarianKnight
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this change only improves the responsiveness of being on a call, before the player would have to tell the other players on the call that they were talking which could take 200-700ms to actually be received by other clients.

The new functionality is that users on the call will always listen to other players on the call, removing the delay between when you actually start talking vs when other clients actually hear you start taking

Please sign in to comment.