Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
add vehiclePassengers to player targets, to ignore rapid zone changes
Browse files Browse the repository at this point in the history
  • Loading branch information
FrazzIe committed Sep 3, 2020
1 parent d71b6fb commit 99f1633
Showing 1 changed file with 53 additions and 30 deletions.
83 changes: 53 additions & 30 deletions client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local nearbySpeakerTargets = {}
local resetTargets = false
local playerChunk = nil
local voiceTarget = 2
local vehiclePassengers = {}
local vehicleTargets = {}
local wasPlayerInVehicle = false

-- Functions
Expand Down Expand Up @@ -92,7 +92,7 @@ function SetGridTargets(pos, reset) -- Used to set the players voice targets dep
DebugMsg("Current Chunk: " .. currentChunk .. ", Nearby Chunks: " .. nearbyChunksStr)

if reset then
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)

resetTargets = false
end
Expand Down Expand Up @@ -235,8 +235,12 @@ function GetVehiclePassengers(vehicle)
end

function MuteVehiclePassengers(playerData)
for id, exists in pairs(vehiclePassengers) do
local changed = false

for id, exists in pairs(vehicleTargets) do
if exists then
changed = true

if playerData.radio > 0 or playerData.call > 0 then -- Only mute player if they are not in call or radio channel with client
local remotePlayerData = voiceData[id]
if remotePlayerData ~= nil then
Expand All @@ -255,6 +259,8 @@ function MuteVehiclePassengers(playerData)
end
end
end

return changed
end

-- Events
Expand Down Expand Up @@ -343,7 +349,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
if CompareChannels(playerData, "radio", radioChannel) then
if playerServerId ~= player then
if unmutedPlayers[player] then
if not vehiclePassengers[player] then -- Mute if player is not in client vehicle
if not vehicleTargets[player] then -- Mute if player is not in client vehicle
if playerData.call > 0 then -- Check if the client is in a call
if not CompareChannels(voiceData[player], "call", playerData.call) then -- Check if the client is in a call with the unmuted player
TogglePlayerVoice(player, false)
Expand All @@ -361,7 +367,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
for id, _ in pairs(radioData[radioChannel]) do -- Mute players that aren't supposed to be unmuted
if id ~= playerServerId then
if unmutedPlayers[id] then -- Check if a player isn't muted
if not vehiclePassengers[id] then -- Mute if player is not in client vehicle
if not vehicleTargets[id] then -- Mute if player is not in client vehicle
if playerData.call > 0 then -- Check if the client is in a call
if not CompareChannels(voiceData[id], "call", playerData.call) then -- Check if the client is in a call with the unmuted player
TogglePlayerVoice(id, false)
Expand All @@ -377,7 +383,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
radioTargets = {} -- Remove all radio targets as client has left the radio channel

if playerData.radioActive then
SetPlayerTargets(callTargets, speakerTargets) -- Reset active targets if for some reason if the client was talking on the radio when the client left
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets) -- Reset active targets if for some reason if the client was talking on the radio when the client left
end
end
end
Expand All @@ -400,7 +406,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
radioTargets[player] = true

if playerData.radioActive then
SetPlayerTargets(callTargets, speakerTargets, radioTargets)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, radioTargets)
end
end
end
Expand All @@ -426,7 +432,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
if CompareChannels(playerData, "call", callChannel) then
if playerServerId ~= player then
if unmutedPlayers[player] then
if not vehiclePassengers[player] then -- Mute if player is not in client vehicle
if not vehicleTargets[player] then -- Mute if player is not in client vehicle
if playerData.radio > 0 then -- Check if the client is in a call
if not CompareChannels(voiceData[player], "radio", playerData.radio) then -- Check if the client is in a call with the unmuted player
TogglePlayerVoice(player, false)
Expand All @@ -439,13 +445,13 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)

if callTargets[player] then
callTargets[player] = nil
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
elseif playerServerId == player then
for id, _ in pairs(callData[callChannel]) do -- Mute players that aren't supposed to be unmuted
if id ~= playerServerId then
if unmutedPlayers[id] then -- Check if a player isn't muted
if not vehiclePassengers[id] then -- Mute if player is not in client vehicle
if not vehicleTargets[id] then -- Mute if player is not in client vehicle
if playerData.radio > 0 then -- Check if the client is in a radio channel
if not CompareChannels(voiceData[id], "radio", playerData.radio) then -- Check if the client isn't in the radio channel with the unmuted player
TogglePlayerVoice(id, false)
Expand All @@ -468,7 +474,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)

callTargets = {} -- Remove all call targets as client has left the call

SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil) -- Reset player targets
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil) -- Reset player targets
end
end
end
Expand All @@ -490,7 +496,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)

if not callTargets[player] then
callTargets[player] = true
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
end
end
Expand All @@ -504,7 +510,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)

if not callTargets[id] then
callTargets[id] = true
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
end
end
Expand Down Expand Up @@ -549,7 +555,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
if voiceData[player][key] ~= nil then
for id, _ in pairs(voiceData[player][key]) do
if playerServerId == id then -- Check if the client has been removed from a nearby call
if not vehiclePassengers[id] then -- Mute if player is not in client vehicle
if not vehicleTargets[id] then -- Mute if player is not in client vehicle
TogglePlayerVoice(player, false) -- Mute
end
end
Expand All @@ -565,7 +571,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
end

if speakerTargetsRemoved or #speakerTargetsAdded > 0 then
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
elseif key == "callSpeaker" and not value then
if voiceData[player] ~= nil then
Expand All @@ -579,7 +585,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)

for targetId, _ in pairs(voiceData[id].speakerTargets) do -- Loop through each call participants speaker targets
if playerServerId == targetId then -- Check if the client was a target and mute the call
if not vehiclePassengers[id] then -- Mute if player is not in client vehicle
if not vehicleTargets[id] then -- Mute if player is not in client vehicle
TogglePlayerVoice(id, false) -- Mute
end
end
Expand All @@ -593,7 +599,7 @@ AddEventHandler("mumble:SetVoiceData", function(player, key, value)
end

if speakerTargetsRemoved then
SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
end
end
Expand Down Expand Up @@ -658,7 +664,7 @@ AddEventHandler("mumble:RemoveVoiceData", function(player)
callTargets[player] = nil
speakerTargets[player] = nil

SetPlayerTargets(callTargets, speakerTargets, playerData.radioActive and radioTargets or nil)
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end

voiceData[player] = nil
Expand Down Expand Up @@ -715,7 +721,7 @@ Citizen.CreateThread(function()
if playerData.radio > 0 then
SetVoiceData("radioActive", true)
playerData.radioActive = true
SetPlayerTargets(callTargets, speakerTargets, radioTargets) -- Send voice to everyone in the radio and call
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, radioTargets) -- Send voice to everyone in the radio and call
PlayMicClick(playerData.radio, true)
mumbleConfig.controls.radio.pressed = true

Expand All @@ -725,7 +731,7 @@ Citizen.CreateThread(function()
end

SetVoiceData("radioActive", false)
SetPlayerTargets(callTargets, speakerTargets) -- Stop sending voice to everyone in the radio
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets) -- Stop sending voice to everyone in the radio
PlayMicClick(playerData.radio, false)
playerData.radioActive = false
mumbleConfig.controls.radio.pressed = false
Expand Down Expand Up @@ -930,6 +936,7 @@ Citizen.CreateThread(function()
local playerId = PlayerId()
local playerList = GetActivePlayers()
local targets = {}
local targetList = ""
local newPassengers = false

for i = 1, #playerList do
Expand All @@ -941,39 +948,55 @@ Citizen.CreateThread(function()

targets[remotePlayerServerId] = true

if vehiclePassengers[remotePlayerServerId] then
vehiclePassengers[remotePlayerServerId] = nil
if targetList == "" then
targetList = remotePlayerServerId
else
targetList = targetList .. ", " .. remotePlayerServerId
end

if vehicleTargets[remotePlayerServerId] then
vehicleTargets[remotePlayerServerId] = nil
else
newPassengers = true
end
end
end
end

MuteVehiclePassengers(playerData)
local removedPassengers = MuteVehiclePassengers(playerData)

if newPassengers then
vehicleTargets = targets
wasPlayerInVehicle = true

if newPassengers or removedPassengers then
for id, exists in pairs(targets) do
if exists then
if exists then
TogglePlayerVoice(id, true)
end
end
end

vehiclePassengers = targets
wasPlayerInVehicle = true
if targetList ~= "" then
DebugMsg("Unmuted passengers: " .. targetList)
else
DebugMsg("Unmuting 0 passengers")
end

SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
else
if wasPlayerInVehicle then
MuteVehiclePassengers(playerData)
vehiclePassengers = {}
vehicleTargets = {}
wasPlayerInVehicle = false
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
end
else
if wasPlayerInVehicle then
MuteVehiclePassengers(playerData)
vehiclePassengers = {}
vehicleTargets = {}
wasPlayerInVehicle = false
SetPlayerTargets(callTargets, speakerTargets, vehicleTargets, playerData.radioActive and radioTargets or nil)
end
end
end
Expand Down

0 comments on commit 99f1633

Please sign in to comment.