Client LuaCsForBarotrauma
AIObjectivePumpWater.cs
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using Microsoft.Xna.Framework;
7 
8 namespace Barotrauma
9 {
11  {
12  public override Identifier Identifier { get; set; } = "pump water".ToIdentifier();
13  public override bool KeepDivingGearOn => true;
14  public override bool AllowAutomaticItemUnequipping => true;
15  protected override bool AllowWhileHandcuffed => false;
16 
17  private List<Pump> pumpList;
18 
19  public AIObjectivePumpWater(Character character, AIObjectiveManager objectiveManager, Identifier option, float priorityModifier = 1)
20  : base(character, objectiveManager, priorityModifier, option) { }
21 
22  protected override void FindTargets()
23  {
24  if (Option == null) { return; }
25  base.FindTargets();
26  }
27 
28  protected override bool IsValidTarget(Pump pump)
29  {
30  if (pump?.Item == null || pump.Item.Removed) { return false; }
31  if (pump.Item.IgnoreByAI(character)) { return false; }
32  if (!pump.Item.IsInteractable(character)) { return false; }
33  if (pump.IsAutoControlled) { return false; }
34  if (pump.Item.ConditionPercentage <= 0) { return false; }
35  if (pump.Item.CurrentHull == null) { return false; }
36  if (pump.Item.CurrentHull.FireSources.Count > 0) { return false; }
37  if (character.Submarine != null && pump.Item.Submarine != null)
38  {
39  if (!character.Submarine.IsConnectedTo(pump.Item.Submarine)) { return false; }
40  }
41  if (Character.CharacterList.Any(c => c.CurrentHull == pump.Item.CurrentHull && !HumanAIController.IsFriendly(c) && HumanAIController.IsActive(c))) { return false; }
42  if (pump.Item.IsClaimedByBallastFlora) { return false; }
43  if (IsReady(pump)) { return false; }
44  return true;
45  }
46  protected override IEnumerable<Pump> GetList()
47  {
48  if (pumpList == null)
49  {
50  if (character == null || character.Submarine == null) { return Array.Empty<Pump>(); }
51 
52  pumpList = new List<Pump>();
53  foreach (Item item in character.Submarine.GetItems(true))
54  {
55  var pump = item.GetComponent<Pump>();
56  if (pump == null || pump.Item.Submarine == null || pump.Item.CurrentHull == null) { continue; }
57  if (pump.Item.Submarine.TeamID != character.TeamID) { continue; }
58  if (pump.Item.HasTag(Tags.Ballast)) { continue; }
59  pumpList.Add(pump);
60  }
61  }
62  return pumpList;
63  }
64 
65  protected override float GetTargetPriority()
66  {
67  if (Targets.None()) { return 0; }
68  if (Option == "stoppumping")
69  {
70  return Targets.Max(t => MathHelper.Lerp(0, 100, Math.Abs(t.FlowPercentage / 100)));
71  }
72  else
73  {
74  return Targets.Max(t => MathHelper.Lerp(100, 0, Math.Abs(-t.FlowPercentage / 100)));
75  }
76  }
77 
78  private bool IsReady(Pump pump)
79  {
80  if (Option == "stoppumping")
81  {
82  return !pump.IsActive || MathUtils.NearlyEqual(pump.FlowPercentage, 0);
83  }
84  else
85  {
86  return !pump.Item.InWater || pump.IsActive && pump.FlowPercentage <= -99.9f;
87  }
88  }
89 
90  protected override AIObjective ObjectiveConstructor(Pump pump)
91  => new AIObjectiveOperateItem(pump, character, objectiveManager, Option, false)
92  {
93  completionCondition = () => IsReady(pump)
94  };
95 
96  protected override void OnObjectiveCompleted(AIObjective objective, Pump target)
97  => HumanAIController.RemoveTargets<AIObjectivePumpWater, Pump>(character, target);
98  }
99 }
An objective that creates specific kinds of subobjectives for specific types of targets,...
override void OnObjectiveCompleted(AIObjective objective, Pump target)
override AIObjective ObjectiveConstructor(Pump pump)
override IEnumerable< Pump > GetList()
List of all possible items of the specified type. Used for filtering the removed objectives.
override float GetTargetPriority()
Returns a priority value based on the current targets (e.g. high prio when there's lots of severe fir...
AIObjectivePumpWater(Character character, AIObjectiveManager objectiveManager, Identifier option, float priorityModifier=1)
override bool IsValidTarget(Pump pump)
Submarine Submarine
Definition: Entity.cs:53
static bool IsActive(Character c)
static bool IsFriendly(Character me, Character other, bool onlySameTeam=false)
bool IsInteractable(Character character)
Returns interactibility based on whether the character is on a player team
bool IgnoreByAI(Character character)
List< Item > GetItems(bool alsoFromConnectedSubs)