From fe2d0bdddb8061910256ffdc97531586be8dd502 Mon Sep 17 00:00:00 2001 From: EvilFactory Date: Wed, 14 Dec 2022 13:57:20 -0300 Subject: [PATCH] Fix Voip causing crashes --- .../Networking/Voip/VoipServer.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs index 97c7777dd..acc9cfff3 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs @@ -9,7 +9,7 @@ namespace Barotrauma.Networking { private readonly ServerPeer netServer; private readonly List queues; - private readonly Dictionary lastSendTime; + private readonly Dictionary lastSendTime; public VoipServer(ServerPeer server) { @@ -59,7 +59,7 @@ namespace Barotrauma.Networking msg.WriteByte((byte)queue.QueueID); msg.WriteRangedSingle(distanceFactor, 0.0f, 1.0f, 8); queue.Write(msg); - + netServer.Send(msg, recipient.Connection, DeliveryMethod.Unreliable); } } @@ -67,10 +67,10 @@ namespace Barotrauma.Networking private static bool CanReceive(Client sender, Client recipient, out float distanceFactor) { - if (Screen.Selected != GameMain.GameScreen) + if (Screen.Selected != GameMain.GameScreen) { distanceFactor = 0.0f; - return true; + return true; } distanceFactor = 0.0f; @@ -98,12 +98,22 @@ namespace Barotrauma.Networking { var canUse = GameMain.LuaCs.Hook.Call("canUseVoiceRadio", new object[] { sender, recipient }); - if (canUse != null) + if (canUse != null) { return canUse.Value; } - if (recipientRadio.CanReceive(senderRadio)) { return true; } + if (recipientSpectating) + { + if (recipient.SpectatePos == null) { return true; } + distanceFactor = MathHelper.Clamp(Vector2.Distance(sender.Character.WorldPosition, recipient.SpectatePos.Value) / senderRadio.Range, 0.0f, 1.0f); + return distanceFactor < 1.0f; + } + else if (recipientRadio != null && recipientRadio.CanReceive(senderRadio)) + { + distanceFactor = MathHelper.Clamp(Vector2.Distance(sender.Character.WorldPosition, recipient.Character.WorldPosition) / senderRadio.Range, 0.0f, 1.0f); + return true; + } } float range = GameMain.LuaCs.Hook.Call("changeLocalVoiceRange", sender, recipient) ?? 1.0f;