From f3cb071b1d6feceb8c0f0be10b7e2dfba2743838 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Thu, 23 May 2019 15:07:32 +0300 Subject: [PATCH] (2921cd18b) Fix potential null reference exceptions in the IsValidTarget of the rescue all method. --- .../AI/Objectives/AIObjectiveRescueAll.cs | 22 ++++++++++++++++--- .../BarotraumaShared/Source/DebugConsole.cs | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveRescueAll.cs b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveRescueAll.cs index 11d22f454..e520495bf 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveRescueAll.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/AI/Objectives/AIObjectiveRescueAll.cs @@ -10,7 +10,17 @@ namespace Barotrauma private const float vitalityThreshold = 0.8f; private const float vitalityThresholdForOrders = 0.95f; - public static float GetVitalityThreshold(AIObjectiveManager manager) => manager.CurrentOrder is AIObjectiveRescueAll ? vitalityThresholdForOrders : vitalityThreshold; + public static float GetVitalityThreshold(AIObjectiveManager manager) + { + if (manager == null) + { + return vitalityThreshold; + } + else + { + return manager.CurrentOrder is AIObjectiveRescueAll ? vitalityThresholdForOrders : vitalityThreshold; + } + } public AIObjectiveRescueAll(Character character, AIObjectiveManager objectiveManager, float priorityModifier = 1) : base(character, objectiveManager, priorityModifier) { } @@ -35,8 +45,14 @@ namespace Barotrauma { if (target == null || target.IsDead || target.Removed) { return false; } if (!HumanAIController.IsFriendly(character, target)) { return false; } - if (!(character.AIController is HumanAIController humanAI)) { return false; } - if (target.Bleeding < 1 && target.Vitality / target.MaxVitality > GetVitalityThreshold(humanAI.ObjectiveManager)) { return false; } + if (character.AIController is HumanAIController humanAI) + { + if (target.Bleeding < 1 && target.Vitality / target.MaxVitality > GetVitalityThreshold(humanAI.ObjectiveManager)) { return false; } + } + else + { + if (target.Bleeding < 1 && target.Vitality / target.MaxVitality > vitalityThreshold) { return false; } + } if (target.Submarine == null) { return false; } if (target.Submarine.TeamID != character.Submarine.TeamID) { return false; } if (target.CurrentHull == null) { return false; } diff --git a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs index b8c515cc1..e1acbd9d4 100644 --- a/Barotrauma/BarotraumaShared/Source/DebugConsole.cs +++ b/Barotrauma/BarotraumaShared/Source/DebugConsole.cs @@ -609,13 +609,13 @@ namespace Barotrauma } }, isCheat: true)); - commands.Add(new Command("eventmanager", "eventmanager: Toggle event manager on/off. No new random events are created when the event manager is disabled.", (string[] args) => + commands.Add(new Command("water|editwater", "water/editwater: Toggle water editing. Allows adding water into rooms by holding the left mouse button and removing it by holding the right mouse button.", (string[] args) => { Hull.EditWater = !Hull.EditWater; NewMessage(Hull.EditWater ? "Water editing on" : "Water editing off", Color.White); }, isCheat: true)); - commands.Add(new Command("water|editwater", "water/editwater: Toggle water editing. Allows adding water into rooms by holding the left mouse button and removing it by holding the right mouse button.", (string[] args) => + commands.Add(new Command("eventmanager", "eventmanager: Toggle event manager on/off. No new random events are created when the event manager is disabled.", (string[] args) => { Hull.EditFire = !Hull.EditFire; NewMessage(Hull.EditFire ? "Fire spawning on" : "Fire spawning off", Color.White);