(a1ebc2642) Use lazy loading to ensure that the list is assigned when it's needed. Fixes null reference exceptions when pumping water or charging batteries.

This commit is contained in:
Joonas Rikkonen
2019-05-16 05:15:34 +03:00
parent 108e42edb0
commit 82d74f7481
11 changed files with 174 additions and 283 deletions
@@ -9,13 +9,21 @@ namespace Barotrauma
{
public override string DebugTag => "pump water";
public override bool KeepDivingGearOn => true;
private readonly IEnumerable<Pump> pumpList;
private IEnumerable<Pump> pumpList;
private IEnumerable<Pump> PumpList
{
get
{
if (pumpList == null)
{
pumpList = character.Submarine.GetItems(true).Select(i => i.GetComponent<Pump>()).Where(p => p != null);
}
return pumpList;
}
}
public AIObjectivePumpWater(Character character, AIObjectiveManager objectiveManager, string option, float priorityModifier = 1)
: base(character, objectiveManager, priorityModifier, option)
{
pumpList = character.Submarine.GetItems(true).Select(i => i.GetComponent<Pump>()).Where(p => p != null);
}
: base(character, objectiveManager, priorityModifier, option) { }
public override bool IsDuplicate(AIObjective otherObjective) => otherObjective is AIObjectivePumpWater && otherObjective.Option == Option;
@@ -44,7 +52,7 @@ namespace Barotrauma
}
return true;
}
protected override IEnumerable<Pump> GetList() => pumpList;
protected override IEnumerable<Pump> GetList() => PumpList;
protected override AIObjective ObjectiveConstructor(Pump pump) => new AIObjectiveOperateItem(pump, character, objectiveManager, Option, false) { IsLoop = true };
protected override float TargetEvaluation() => targets.Max(t => MathHelper.Lerp(100, 0, t.CurrFlow / t.MaxFlow));
}