Client LuaCsForBarotrauma
AIObjectivePrepare.cs
2 using System;
3 using System.Linq;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 
7 namespace Barotrauma
8 {
10  {
11  public override Identifier Identifier { get; set; } = "prepare".ToIdentifier();
12  public override string DebugTag => $"{Identifier}";
13  public override bool KeepDivingGearOn => true;
14  public override bool KeepDivingGearOnAlsoWhenInactive => true;
15  public override bool PrioritizeIfSubObjectivesActive => true;
16  protected override bool AllowWhileHandcuffed => false;
17 
18  private AIObjectiveGetItem getSingleItemObjective;
19  private AIObjectiveGetItems getAllItemsObjective;
20  private AIObjectiveGetItems getMultipleItemsObjective;
21  private bool subObjectivesCreated;
22  private readonly Item targetItem;
23  private readonly ImmutableArray<Identifier> requiredItems;
24  private readonly ImmutableArray<Identifier> optionalItems;
25  public bool KeepActiveWhenReady { get; set; }
26  public bool CheckInventory { get; set; }
27  public bool FindAllItems { get; set; }
28  public bool Equip { get; set; }
29  public bool EvaluateCombatPriority { get; set; }
30  public bool RequireNonEmpty { get; set; }
31 
32  private AIObjective GetSubObjective()
33  {
34  if (getSingleItemObjective != null) { return getSingleItemObjective; }
35  if (getAllItemsObjective == null || getAllItemsObjective.IsCompleted)
36  {
37  return getMultipleItemsObjective;
38  }
39  return getAllItemsObjective;
40  }
41  public AIObjectivePrepare(Character character, AIObjectiveManager objectiveManager, Item targetItem, float priorityModifier = 1)
42  : base(character, objectiveManager, priorityModifier)
43  {
44  this.targetItem = targetItem;
45  }
46 
47  public AIObjectivePrepare(Character character, AIObjectiveManager objectiveManager, IEnumerable<Identifier> optionalItems, IEnumerable<Identifier> requiredItems = null, float priorityModifier = 1)
48  : base(character, objectiveManager, priorityModifier)
49  {
50  this.optionalItems = optionalItems.ToImmutableArray();
51  if (requiredItems != null)
52  {
53  this.requiredItems = requiredItems.ToImmutableArray();
54  }
55  }
56 
57  protected override bool CheckObjectiveState() => IsCompleted;
58 
59  protected override float GetPriority()
60  {
61  if (!IsAllowed)
62  {
64  return Priority;
65  }
67  var subObjective = GetSubObjective();
68  if (subObjective is { IsCompleted: true })
69  {
70  Priority = 0;
71  }
72  return Priority;
73  }
74 
75  protected override void Act(float deltaTime)
76  {
77  if (!subObjectivesCreated)
78  {
79  if (FindAllItems && targetItem == null)
80  {
81  getMultipleItemsObjective = CreateObjectives(optionalItems, requireAll: false);
82  if (requiredItems != null && requiredItems.Any())
83  {
84  getAllItemsObjective = CreateObjectives(requiredItems, requireAll: true);
85  }
86  AIObjectiveGetItems CreateObjectives(IEnumerable<Identifier> itemTags, bool requireAll)
87  {
88  AIObjectiveGetItems objectiveReference = null;
89  if (!TryAddSubObjective(ref objectiveReference, () =>
90  {
91  var getItems = new AIObjectiveGetItems(character, objectiveManager, itemTags)
92  {
94  Equip = Equip,
97  RequireAllItems = requireAll
98  };
99 
100  if (itemTags.Contains(Tags.HeavyDivingGear))
101  {
102  getItems.ItemFilter = (Item it, Identifier tag) =>
103  {
104  if (tag == Tags.HeavyDivingGear)
105  {
107  }
108  return true;
109  };
110  }
111  return getItems;
112  },
113  onCompleted: () =>
114  {
115  if (!KeepActiveWhenReady)
116  {
117  IsCompleted = true;
118  }
119  },
120  onAbandon: () => Abandon = true))
121  {
122  Abandon = true;
123  }
124  return objectiveReference;
125  }
126  }
127  else
128  {
129  Func<AIObjectiveGetItem> getItemConstructor;
130  if (targetItem != null)
131  {
132  getItemConstructor = () => new AIObjectiveGetItem(character, targetItem, objectiveManager, equip: Equip)
133  {
134  SpeakIfFails = true
135  };
136  }
137  else
138  {
139  IEnumerable<Identifier> allItems = optionalItems;
140  if (requiredItems != null && requiredItems.Any())
141  {
142  allItems = requiredItems;
143  }
144  getItemConstructor = () => new AIObjectiveGetItem(character, allItems, objectiveManager, equip: Equip, checkInventory: CheckInventory)
145  {
147  SpeakIfFails = true,
149  };
150  }
151  if (!TryAddSubObjective(ref getSingleItemObjective, getItemConstructor,
152  onCompleted: () =>
153  {
154  if (!KeepActiveWhenReady)
155  {
156  IsCompleted = true;
157  }
158  },
159  onAbandon: () => Abandon = true))
160  {
161  Abandon = true;
162  }
163  }
164  subObjectivesCreated = true;
165  }
166  }
167 
168  public override void Reset()
169  {
170  base.Reset();
171  subObjectivesCreated = false;
172  getMultipleItemsObjective = null;
173  getSingleItemObjective = null;
174  getAllItemsObjective = null;
175  }
176  }
177 }
static bool IsSuitablePressureProtection(Item item, Identifier tag, Character character)
float Priority
Final priority value after all calculations.
float GetOrderPriority(AIObjective objective)
override bool PrioritizeIfSubObjectivesActive
AIObjectivePrepare(Character character, AIObjectiveManager objectiveManager, IEnumerable< Identifier > optionalItems, IEnumerable< Identifier > requiredItems=null, float priorityModifier=1)
override void Act(float deltaTime)
override bool KeepDivingGearOnAlsoWhenInactive
AIObjectivePrepare(Character character, AIObjectiveManager objectiveManager, Item targetItem, float priorityModifier=1)
override bool CheckObjectiveState()
Should return whether the objective is completed or not.