From 92a74808feafed93bd194e54cee3bc51f03784f7 Mon Sep 17 00:00:00 2001 From: Regalis Date: Tue, 18 Apr 2017 21:37:23 +0300 Subject: [PATCH] Fix to oxygen distribution logic: gaps used to move a fixed amount of oxygen between hulls every frame, causing the oxygen levels to fluctuate constantly. While it wasn't noticeable in regular sized hulls, smaller hulls created excessive amounts of NetworkEvents due to large fluctuations in the oxygen percentage. Now the gaps make the oxygen percentages settle to the average value, and hulls have a minimum delay of 0.5s between the creation of NetworkEvents. --- Subsurface/Source/Map/Gap.cs | 7 +++++-- Subsurface/Source/Map/Hull.cs | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Subsurface/Source/Map/Gap.cs b/Subsurface/Source/Map/Gap.cs index f7b23a11b..e2aa312f2 100644 --- a/Subsurface/Source/Map/Gap.cs +++ b/Subsurface/Source/Map/Gap.cs @@ -629,9 +629,12 @@ namespace Barotrauma float totalOxygen = hull1.Oxygen + hull2.Oxygen; float totalVolume = (hull1.FullVolume + hull2.FullVolume); + + float deltaOxygen = (totalOxygen * hull1.FullVolume / totalVolume) - hull1.Oxygen; + deltaOxygen = MathHelper.Clamp(deltaOxygen, -Hull.OxygenDistributionSpeed, Hull.OxygenDistributionSpeed); - hull1.Oxygen += Math.Sign(totalOxygen * hull1.FullVolume / (totalVolume) - hull1.Oxygen) * Hull.OxygenDistributionSpeed; - hull2.Oxygen += Math.Sign(totalOxygen * hull2.FullVolume / (totalVolume) - hull2.Oxygen) * Hull.OxygenDistributionSpeed; + hull1.Oxygen += deltaOxygen; + hull2.Oxygen -= deltaOxygen; } public static Gap FindAdjacent(List gaps, Vector2 worldPos, float allowedOrthogonalDist) diff --git a/Subsurface/Source/Map/Hull.cs b/Subsurface/Source/Map/Hull.cs index 5708719ea..94cbdec9b 100644 --- a/Subsurface/Source/Map/Hull.cs +++ b/Subsurface/Source/Map/Hull.cs @@ -15,6 +15,8 @@ namespace Barotrauma class Hull : MapEntity, IPropertyObject, IServerSerializable { + const float NetworkUpdateInterval = 0.5f; + public static List hullList = new List(); private static List entityGrids = new List(); public static List EntityGrids @@ -74,7 +76,7 @@ namespace Barotrauma float[] rightDelta; private float lastSentVolume, lastSentOxygen; - private float lastNetworkUpdate; + private float sendUpdateTimer; public List ConnectedGaps; @@ -361,8 +363,6 @@ namespace Barotrauma entityGrid.RemoveEntity(this); } } - - } public void AddFireSource(FireSource fireSource) @@ -460,7 +460,17 @@ namespace Barotrauma if (Math.Abs(lastSentVolume - volume) > FullVolume * 0.1f || Math.Abs(lastSentOxygen - OxygenPercentage) > 5f) { - if (GameMain.Server != null) GameMain.Server.CreateEntityEvent(this); + if (GameMain.Server != null) + { + sendUpdateTimer -= deltaTime; + if (sendUpdateTimer < 0.0f) + { + GameMain.Server.CreateEntityEvent(this); + lastSentVolume = volume; + lastSentOxygen = OxygenPercentage; + sendUpdateTimer = NetworkUpdateInterval; + } + } } if (!update) @@ -622,7 +632,7 @@ namespace Barotrauma isHighlighted ? Color.LightBlue * 0.5f : Color.Red * 0.5f, true, 0, (int)Math.Max((1.5f / GameScreen.Selected.Cam.Zoom), 1.0f)); } } - + public void Render(GraphicsDevice graphicsDevice, Camera cam) { if (renderer.PositionInBuffer > renderer.vertices.Length - 6) return; @@ -842,9 +852,6 @@ namespace Barotrauma message.WriteRangedSingle(MathHelper.Clamp(fireSource.Size.X / rect.Width, 0.0f, 1.0f), 0, 1.0f, 8); } } - - lastSentVolume = volume; - lastSentOxygen = OxygenPercentage; } public void ClientRead(ServerNetObject type, NetBuffer message, float sendingTime)