From 29f902f0bb68bf07c057c03516d25b3d46937143 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Mon, 15 Apr 2019 12:01:48 +0300 Subject: [PATCH] (18bb55660) Add unreachable targets on the unreachables list. Reset the list when a new combat objective begins. Don't immediately find a new hull target if the current hull is unsafe. Instead wait a sec at max. Fixes #1417. --- .../AI/Objectives/AIObjectiveCombat.cs | 8 +++++--- .../AI/Objectives/AIObjectiveFindSafety.cs | 20 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs index 128992a3e..9acf4bc99 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveCombat.cs @@ -44,6 +44,7 @@ namespace Barotrauma private AIObjectiveContainItem reloadWeaponObjective; private Hull retreatTarget; private AIObjectiveGoTo retreatObjective; + private AIObjectiveFindSafety findSafety; private float coolDownTimer; @@ -60,7 +61,9 @@ namespace Barotrauma { Enemy = enemy; coolDownTimer = CoolDown; - HumanAIController.ObjectiveManager.GetObjective().Priority = 0; + findSafety = HumanAIController.ObjectiveManager.GetObjective(); + findSafety.Priority = 0; + findSafety.unreachable.Clear(); Mode = mode; if (Enemy == null) { @@ -175,7 +178,7 @@ namespace Barotrauma { if (retreatTarget == null || (retreatObjective != null && !retreatObjective.CanBeCompleted)) { - retreatTarget = HumanAIController.ObjectiveManager.GetObjective().FindBestHull(new List() { character.CurrentHull }); + retreatTarget = findSafety.FindBestHull(new List() { character.CurrentHull }); } if (retreatTarget != null) { @@ -277,7 +280,6 @@ namespace Barotrauma { abandon = true; SteeringManager.Reset(); - //HumanAIController.ObjectiveManager.GetObjective().Priority = 100; } public override bool IsCompleted() diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs index 5474ed813..ae793f08f 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveFindSafety.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Barotrauma.Extensions; namespace Barotrauma { @@ -17,10 +18,7 @@ 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(); + public readonly List unreachable = new List(); private float currenthullSafety; private float unreachableClearTimer; @@ -67,6 +65,11 @@ namespace Barotrauma } } + if (currenthullSafety < HumanAIController.HULL_SAFETY_THRESHOLD) + { + searchHullTimer = Math.Min(1, searchHullTimer); + } + if (unreachableClearTimer > 0) { unreachableClearTimer -= deltaTime; @@ -190,15 +193,17 @@ 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); - if (path.Unreachable) { continue; } + if (path.Unreachable) + { + unreachable.Add(hull); + continue; + } int unsafeNodes = path.Nodes.Count(n => n.CurrentHull != character.CurrentHull && HumanAIController.UnsafeHulls.Contains(n.CurrentHull)); hullSafety /= 1 + unsafeNodes; // If the target is not inside a friendly submarine, considerably reduce the hull safety. @@ -226,7 +231,6 @@ namespace Barotrauma } } } - // Huge preference for closer targets float distance = Vector2.DistanceSquared(character.WorldPosition, hull.WorldPosition); float distanceFactor = MathHelper.Lerp(1, 0.2f, MathUtils.InverseLerp(0, MathUtils.Pow(100000, 2), distance));