Client LuaCsForBarotrauma
AIObjectiveChargeBatteries.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; } = "charge batteries".ToIdentifier();
13  public override bool AllowAutomaticItemUnequipping => true;
14  private IEnumerable<PowerContainer> batteryList;
15 
16  public AIObjectiveChargeBatteries(Character character, AIObjectiveManager objectiveManager, Identifier option, float priorityModifier)
17  : base(character, objectiveManager, priorityModifier, option) { }
18 
19  protected override bool IsValidTarget(PowerContainer battery)
20  {
21  if (battery == null) { return false; }
22  if (battery.OutputDisabled) { return false; }
23  var item = battery.Item;
24  if (item.IgnoreByAI(character)) { return false; }
25  if (!item.IsInteractable(character)) { return false; }
26  if (item.Submarine == null) { return false; }
27  if (item.CurrentHull == null) { return false; }
28  if (item.Submarine.TeamID != character.TeamID) { return false; }
29  if (character.Submarine != null)
30  {
31  if (!character.Submarine.IsConnectedTo(item.Submarine)) { return false; }
32  }
33  if (item.ConditionPercentage <= 0) { return false; }
34  if (item.IsClaimedByBallastFlora) { return false; }
35  if (Character.CharacterList.Any(c => c.CurrentHull == item.CurrentHull && !HumanAIController.IsFriendly(c) && HumanAIController.IsActive(c))) { return false; }
36  if (IsReady(battery)) { return false; }
37  return true;
38  }
39 
40  protected override float GetTargetPriority()
41  {
42  if (Targets.None()) { return 0; }
43  if (Option == "charge")
44  {
45  return Targets.Max(t => MathHelper.Lerp(100, 0, Math.Abs(PowerContainer.aiRechargeTargetRatio - t.RechargeRatio)));
46  }
47  else
48  {
49 
50  return Targets.Max(t => MathHelper.Lerp(0, 100, t.RechargeRatio));
51  }
52  }
53 
54  protected override IEnumerable<PowerContainer> GetList()
55  {
56  if (batteryList == null)
57  {
58  if (character == null || character.Submarine == null)
59  {
60  return Array.Empty<PowerContainer>();
61  }
62  batteryList = character.Submarine.GetItems(true).Select(i => i.GetComponent<PowerContainer>()).Where(b => b != null);
63  }
64  return batteryList;
65  }
66 
67  private bool IsReady(PowerContainer battery)
68  {
69  if (battery.HasBeenTuned && character.IsDismissed) { return true; }
70  if (Option == "charge")
71  {
73  }
74  else
75  {
76  return battery.RechargeRatio <= 0;
77  }
78  }
79 
80  protected override AIObjective ObjectiveConstructor(PowerContainer battery) =>
81  new AIObjectiveOperateItem(battery, character, objectiveManager, Option, false, priorityModifier: PriorityModifier)
82  {
83  Override = !character.IsDismissed,
84  completionCondition = () => IsReady(battery)
85  };
86 
87  protected override void OnObjectiveCompleted(AIObjective objective, PowerContainer target)
88  => HumanAIController.RemoveTargets<AIObjectiveChargeBatteries, PowerContainer>(character, target);
89  }
90 }
override float GetTargetPriority()
Returns a priority value based on the current targets (e.g. high prio when there's lots of severe fir...
override void OnObjectiveCompleted(AIObjective objective, PowerContainer target)
override IEnumerable< PowerContainer > GetList()
List of all possible items of the specified type. Used for filtering the removed objectives.
AIObjectiveChargeBatteries(Character character, AIObjectiveManager objectiveManager, Identifier option, float priorityModifier)
override AIObjective ObjectiveConstructor(PowerContainer battery)
override bool IsValidTarget(PowerContainer battery)
An objective that creates specific kinds of subobjectives for specific types of targets,...
static bool IsActive(Character c)
static bool IsFriendly(Character me, Character other, bool onlySameTeam=false)