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 the target was selected as a valid target, we'll have to accept it so that the objective can be completed.
63  // The validity changes when a character picks the item up.
64  if (!IsValidTarget(target, character, checkInventory: true))
65  {
66  return Objectives.ContainsKey(target) && AIObjectiveCleanupItems.IsItemInsideValidSubmarine(target, character);
67  }
68  if (target.CurrentHull.FireSources.Count > 0) { return false; }
69 
70  foreach (Character c in Character.CharacterList)
71  {
72  if (c == character || !HumanAIController.IsActive(c)) { continue; }
73  if (c.CurrentHull == target.CurrentHull && !HumanAIController.IsFriendly(c))
74  {
75  // Don't deconstruct items in rooms that have enemies inside.
76  return false;
77  }
78  else if (c.TeamID == character.TeamID && c.AIController is HumanAIController humanAi)
79  {
80  if (humanAi.ObjectiveManager.CurrentObjective is AIObjectiveDeconstructItem deconstruct && deconstruct.Item == target)
81  {
82  return false;
83  }
84  }
85  }
86  return true;
87  }
88 
89  protected override IEnumerable<Item> GetList() => Item.DeconstructItems;
90 
91  protected override AIObjective ObjectiveConstructor(Item item)
92  => new AIObjectiveDeconstructItem(item, character, objectiveManager, priorityModifier: PriorityModifier);
93 
94  protected override void OnObjectiveCompleted(AIObjective objective, Item target)
95  => HumanAIController.RemoveTargets<AIObjectiveDeconstructItems, Item>(character, target);
96 
97  private static bool IsValidTarget(Item item, Character character, bool checkInventory)
98  {
99  if (item == null) { return false; }
100  if (item.GetRootInventoryOwner() == character) { return true; }
102  item,
103  character,
104  checkInventory,
105  allowUnloading: true,
106  requireValidContainer: false,
107  ignoreItemsMarkedForDeconstruction: false);
108  }
109 
110  public override void OnDeselected()
111  {
112  base.OnDeselected();
113  foreach (var subObjective in SubObjectives)
114  {
115  if (subObjective is AIObjectiveDeconstructItem deconstructObjective)
116  {
117  deconstructObjective.DropTarget();
118  }
119  }
120  }
121  }
122 }
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