Client LuaCsForBarotrauma
AIObjectiveDeconstructItems.cs
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
8  {
9  public override Identifier Identifier { get; set; } = "deconstruct items".ToIdentifier();
10 
11  //Clear periodically, because we may ending up ignoring items when all deconstructors are full
12  protected override float IgnoreListClearInterval => 30;
13 
14  protected override bool AllowInFriendlySubs => true;
15 
16  protected override int MaxTargets => 10;
17 
18  private bool checkedDeconstructorExists;
19 
20  public AIObjectiveDeconstructItems(Character character, AIObjectiveManager objectiveManager, float priorityModifier = 1)
21  : base(character, objectiveManager, priorityModifier)
22  {
23  }
24 
25  public override void OnSelected()
26  {
27  base.OnSelected();
28  if (!checkedDeconstructorExists)
29  {
30  if (character.Submarine == null ||
31  Item.ItemList.None(it =>
32  it.GetComponent<Deconstructor>() != null &&
33  it.IsInteractable(character) &&
34  character.Submarine.IsEntityFoundOnThisSub(it, includingConnectedSubs: true, allowDifferentTeam: true, allowDifferentType: true)))
35  {
36  character.Speak(TextManager.Get("orderdialogself.deconstructitem.nodeconstructor").Value, delay: 5.0f,
37  identifier: "nodeconstructor".ToIdentifier(), minDurationBetweenSimilar: 30.0f);
38  Abandon = true;
39  }
40  checkedDeconstructorExists = true;
41  }
42  }
43 
44  public override void Reset()
45  {
46  base.Reset();
47  checkedDeconstructorExists = false;
48  }
49 
50  protected override float GetTargetPriority()
51  {
52  if (Targets.None()) { return 0; }
53  if (objectiveManager.IsOrder(this))
54  {
55  return objectiveManager.GetOrderPriority(this);
56  }
57  return AIObjectiveManager.RunPriority - 0.5f;
58  }
59 
60  protected override bool IsValidTarget(Item target)
61  {
62  if (target == null || target.Removed) { return false; }
63  // If the target was selected as a valid target, we'll have to accept it so that the objective can be completed.
64  // The validity changes when a character picks the item up.
65  if (!IsValidTarget(target, character, checkInventory: true))
66  {
67  return Objectives.ContainsKey(target) && AIObjectiveCleanupItems.IsItemInsideValidSubmarine(target, character);
68  }
69  //note that the item can be outside hulls and still be a valid target - it can be in the character's inventory
70  if (target.CurrentHull != null && target.CurrentHull.FireSources.Count > 0) { return false; }
71 
72  foreach (Character c in Character.CharacterList)
73  {
74  if (c == character || !HumanAIController.IsActive(c)) { continue; }
75  if (c.CurrentHull == target.CurrentHull && !HumanAIController.IsFriendly(c))
76  {
77  // Don't deconstruct items in rooms that have enemies inside.
78  return false;
79  }
80  else if (c.TeamID == character.TeamID && c.AIController is HumanAIController humanAi)
81  {
82  if (humanAi.ObjectiveManager.CurrentObjective is AIObjectiveDeconstructItem deconstruct && deconstruct.Item == target)
83  {
84  return false;
85  }
86  }
87  }
88  return true;
89  }
90 
91  protected override IEnumerable<Item> GetList() => Item.DeconstructItems;
92 
93  protected override AIObjective ObjectiveConstructor(Item item)
94  => new AIObjectiveDeconstructItem(item, character, objectiveManager, priorityModifier: PriorityModifier);
95 
96  protected override void OnObjectiveCompleted(AIObjective objective, Item target)
97  => HumanAIController.RemoveTargets<AIObjectiveDeconstructItems, Item>(character, target);
98 
99  private static bool IsValidTarget(Item item, Character character, bool checkInventory)
100  {
101  if (item == null || item.Removed) { return false; }
102  if (item.GetRootInventoryOwner() == character) { return true; }
104  item,
105  character,
106  checkInventory,
107  allowUnloading: true,
108  requireValidContainer: false,
109  ignoreItemsMarkedForDeconstruction: false);
110  }
111 
112  public override void OnDeselected()
113  {
114  base.OnDeselected();
115  foreach (var subObjective in SubObjectives)
116  {
117  if (subObjective is AIObjectiveDeconstructItem deconstructObjective)
118  {
119  deconstructObjective.DropTarget();
120  }
121  }
122  }
123  }
124 }
static bool IsItemInsideValidSubmarine(Item item, Character character)
override bool IsValidTarget(Item target)
override AIObjective ObjectiveConstructor(Item item)
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, Item target)
override IEnumerable< Item > GetList()
List of all possible items of the specified type. Used for filtering the removed objectives.
AIObjectiveDeconstructItems(Character character, AIObjectiveManager objectiveManager, float priorityModifier=1)
An objective that creates specific kinds of subobjectives for specific types of targets,...
Dictionary< T, AIObjective > Objectives
const float RunPriority
Objectives with a priority equal to or higher than this make the character run.
static bool IsActive(Character c)
static bool IsFriendly(Character me, Character other, bool onlySameTeam=false)
static readonly List< Item > ItemList
static HashSet< Item > DeconstructItems
Items that have been marked for deconstruction