From e715fdc835d3312d23fa53a1c56baad5587d031a Mon Sep 17 00:00:00 2001 From: NotAlwaysTrue <2136846186@qq.com> Date: Fri, 26 Dec 2025 01:16:05 +0800 Subject: [PATCH] Fixed #10 Fixed #12 --- .../Map/Levels/LevelObjects/LevelTrigger.cs | 2 +- .../SharedSource/Map/MapEntity.cs | 71 ++++++++----------- .../SharedSource/Screens/GameScreen.cs | 56 ++++++--------- .../Dynamics/Body.cs | 2 +- 4 files changed, 56 insertions(+), 75 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/LevelObjects/LevelTrigger.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/LevelObjects/LevelTrigger.cs index 21a71b22b..b05cc6be7 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/LevelObjects/LevelTrigger.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Levels/LevelObjects/LevelTrigger.cs @@ -479,7 +479,7 @@ namespace Barotrauma { foreach (Fixture fixture in triggerBody.FarseerBody.FixtureList) { - ContactEdge contactEdge = fixture.Body.ContactList; + ContactEdge contactEdge = fixture.Body.ContactList.CreateCopy(); while (contactEdge != null) { if (contactEdge.Contact != null && diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs index 48ea30861..91cfc04cc 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs @@ -652,34 +652,28 @@ namespace Barotrauma bool shouldUpdatePower = mapEntityUpdateTick % PoweredUpdateInterval == 0; // Buffer lists to avoid repeated allocations - var hullList = shouldUpdateMapEntities ? Hull.HullList.ToList() : null; - var structureList = shouldUpdateMapEntities ? Structure.WallList.ToList() : null; - var gapList = Gap.GapList.ToList(); - var itemList = shouldUpdateMapEntities ? Item.ItemList.ToList() : null; + List hullList = new List(Hull.HullList); + List structureList = new List(Structure.WallList); + List gapList = new List(Gap.GapList); + List itemList = new List(Item.ItemList); // First phase: parallel updates that have no order dependencies Parallel.Invoke(parallelOptions, // Hull parallel update () => { - if (shouldUpdateMapEntities && hullList != null) + Parallel.ForEach(hullList, parallelOptions, hull => { - Parallel.ForEach(hullList, parallelOptions, hull => - { - hull.Update(deltaTime * MapEntityUpdateInterval, cam); - }); - } + hull.Update(deltaTime, cam); + }); }, // Structure parallel update () => { - if (shouldUpdateMapEntities && structureList != null) + Parallel.ForEach(structureList, parallelOptions, structure => { - Parallel.ForEach(structureList, parallelOptions, structure => - { - structure.Update(deltaTime * MapEntityUpdateInterval, cam); - }); - } + structure.Update(deltaTime, cam); + }); }, // Gap reset (must be done before update) () => @@ -694,7 +688,7 @@ namespace Barotrauma { if (shouldUpdatePower) { - Powered.UpdatePower(deltaTime * PoweredUpdateInterval); + Powered.UpdatePower(deltaTime); } } ); @@ -721,33 +715,30 @@ namespace Barotrauma #endif // Item update (Item.Update() is not thread-safe and must be executed on the main thread) - if (shouldUpdateMapEntities && itemList != null) + Item.UpdatePendingConditionUpdates(deltaTime); + + float scaledDeltaTime = deltaTime; + Item lastUpdatedItem = null; + + try { - Item.UpdatePendingConditionUpdates(deltaTime); - - float scaledDeltaTime = deltaTime * MapEntityUpdateInterval; - Item lastUpdatedItem = null; - - try + foreach (Item item in itemList) { - foreach (Item item in itemList) - { - lastUpdatedItem = item; - item.Update(scaledDeltaTime, cam); - } + lastUpdatedItem = item; + item.Update(scaledDeltaTime, cam); } - catch (InvalidOperationException e) - { - GameAnalyticsManager.AddErrorEventOnce( - "MapEntity.UpdateAll:ItemUpdateInvalidOperation", - GameAnalyticsManager.ErrorSeverity.Critical, - $"Error while updating item {lastUpdatedItem?.Name ?? "null"}: {e.Message}"); - throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e); - } - - UpdateAllProjSpecific(scaledDeltaTime); - Spawner?.Update(); } + catch (InvalidOperationException e) + { + GameAnalyticsManager.AddErrorEventOnce( + "MapEntity.UpdateAll:ItemUpdateInvalidOperation", + GameAnalyticsManager.ErrorSeverity.Critical, + $"Error while updating item {lastUpdatedItem?.Name ?? "null"}: {e.Message}"); + throw new InvalidOperationException($"Error while updating item {lastUpdatedItem?.Name ?? "null"}", innerException: e); + } + + UpdateAllProjSpecific(scaledDeltaTime); + Spawner?.Update(); #if CLIENT sw.Stop(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs b/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs index e29da7114..4d4634e31 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs @@ -151,23 +151,16 @@ namespace Barotrauma var physicsBodies = PhysicsBody.List.ToList(); - Parallel.Invoke(parallelOptions, - () => + Parallel.ForEach(physicsBodies, parallelOptions, body => { - Parallel.ForEach(physicsBodies, parallelOptions, body => - { - if ((body.Enabled || body.UserData is Character) && - body.BodyType != BodyType.Static) - { - body.Update(); - } - }); - }, - () => - { - GameMain.GameSession?.Update((float)deltaTime); - } - ); + if ((body.Enabled || body.UserData is Character) && + body.BodyType != BodyType.Static) + { + body.Update(); + } + }); + + GameMain.GameSession?.Update((float)deltaTime); foreach (PhysicsBody body in physicsBodies) { @@ -183,10 +176,8 @@ namespace Barotrauma var sw = new System.Diagnostics.Stopwatch(); sw.Start(); - Parallel.Invoke(parallelOptions, - () => GameMain.ParticleManager.Update((float)deltaTime), - () => { if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, cam); } - ); + GameMain.ParticleManager.Update((float)deltaTime); + if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, cam); sw.Stop(); GameMain.PerformanceCounter.AddElapsedTicks("Update:Particles+Level", sw.ElapsedTicks); @@ -252,25 +243,25 @@ namespace Barotrauma Character.Controlled?.UpdateLocalCursor(cam); #elif SERVER - Parallel.Invoke(parallelOptions, - () => { if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, Camera.Instance); }, - () => Character.UpdateAll((float)deltaTime, Camera.Instance) - ); + if (Level.Loaded != null) Level.Loaded.Update((float)deltaTime, Camera.Instance); + + Character.UpdateAll((float)deltaTime, Camera.Instance); #endif var submarines = Submarine.Loaded.ToList(); - Parallel.ForEach(submarines, parallelOptions, sub => + foreach(Submarine sub in submarines) { sub.SetPrevTransform(sub.Position); - }); + } - Parallel.ForEach(physicsBodies, parallelOptions, body => + // + foreach (PhysicsBody body in physicsBodies) { if (body.Enabled && body.BodyType != FarseerPhysics.BodyType.Static) - { - body.SetPrevTransform(body.SimPosition, body.Rotation); + { + body.SetPrevTransform(body.SimPosition, body.Rotation); } - }); + } #if CLIENT MapEntity.UpdateAll((float)deltaTime, cam, parallelOptions); @@ -302,11 +293,10 @@ namespace Barotrauma GameMain.PerformanceCounter.AddElapsedTicks("Update:Ragdolls", sw.ElapsedTicks); sw.Restart(); #endif - - Parallel.ForEach(submarines, parallelOptions, sub => + foreach (var sub in submarines) { sub.Update((float)deltaTime); - }); + } #if CLIENT sw.Stop(); diff --git a/Libraries/Farseer Physics Engine 3.5/Dynamics/Body.cs b/Libraries/Farseer Physics Engine 3.5/Dynamics/Body.cs index 81faa0121..a35808d92 100644 --- a/Libraries/Farseer Physics Engine 3.5/Dynamics/Body.cs +++ b/Libraries/Farseer Physics Engine 3.5/Dynamics/Body.cs @@ -1409,4 +1409,4 @@ namespace FarseerPhysics.Dynamics return body; } } -} \ No newline at end of file +}