From e8c67f94c34a302af0ba711cb4b690cd4ee2f795 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 15 Apr 2019 12:01:04 +0300 Subject: [PATCH] (a8e725a96) Optimized AIObjectiveFindSafety.FindBestHull. TODO: optimize further, causes huge lag spikes if the character can't find a path (see #1417) --- .../Characters/AI/Objectives/AIObjectiveFindSafety.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs index 0b5032321..5474ed813 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs @@ -17,6 +17,9 @@ namespace Barotrauma const float SearchHullInterval = 3.0f; const float clearUnreachableInterval = 30; + private readonly List targetHulls = new List(20); + private readonly List hullWeights = new List(20); + private List unreachable = new List(); private float currenthullSafety; @@ -187,6 +190,11 @@ namespace Barotrauma float dist = Math.Abs(character.WorldPosition.X - hull.WorldPosition.X) + Math.Abs(character.WorldPosition.Y - hull.WorldPosition.Y) * 2.0f; float distanceFactor = MathHelper.Lerp(1, 0.9f, MathUtils.InverseLerp(0, 10000, dist)); hullSafety *= distanceFactor; + + //skip the hull if the safety is already less than the best hull + //(no need to do the expensive pathfinding if we already know we're not going to choose this hull) + if (hullSafety < bestValue) { continue; } + // Each unsafe node reduces the hull safety value. // Ignore current hull, because otherwise the would block all paths from the current hull to the target hull. var path = PathSteering.PathFinder.FindPath(character.SimPosition, hull.SimPosition);