Client LuaCsForBarotrauma
AIObjectiveGetItems.cs
1 #nullable enable
3 using System.Linq;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 using System;
7 
8 namespace Barotrauma
9 {
11  {
12  public override Identifier Identifier { get; set; } = "get items".ToIdentifier();
13  public override string DebugTag => $"{Identifier}";
14  public override bool KeepDivingGearOn => true;
15  public override bool AllowMultipleInstances => true;
16  protected override bool AllowWhileHandcuffed => false;
17 
18  public bool AllowStealing { get; set; }
19  public bool TakeWholeStack { get; set; }
20  public bool AllowVariants { get; set; }
21  public bool Equip { get; set; }
22  public bool Wear { get; set; }
23  public bool CheckInventory { get; set; }
24  public bool EvaluateCombatPriority { get; set; }
25  public bool CheckPathForEachItem { get; set; }
26  public bool RequireNonEmpty { get; set; }
27  public bool RequireAllItems { get; set; }
28  public bool RequireDivingSuitAdequate { get; set; }
29 
33  public Func<Item, Identifier, bool>? ItemFilter;
34 
35  private readonly ImmutableArray<Identifier> gearTags;
36  private readonly ImmutableHashSet<Identifier> ignoredTags;
37  private bool subObjectivesCreated;
38 
39  public readonly HashSet<Item> achievedItems = new HashSet<Item>();
40 
41  public AIObjectiveGetItems(Character character, AIObjectiveManager objectiveManager, IEnumerable<Identifier> identifiersOrTags, float priorityModifier = 1) : base(character, objectiveManager, priorityModifier)
42  {
43  gearTags = AIObjectiveGetItem.ParseGearTags(identifiersOrTags).ToImmutableArray();
44  ignoredTags = AIObjectiveGetItem.ParseIgnoredTags(identifiersOrTags).ToImmutableHashSet();
45  }
46 
47  protected override bool CheckObjectiveSpecific() => subObjectivesCreated && subObjectives.None();
48 
49  protected override void Act(float deltaTime)
50  {
51  if (subObjectivesCreated) { return; }
52  foreach (Identifier tag in gearTags)
53  {
54  if (subObjectives.Any(so => so is AIObjectiveGetItem getItem && getItem.IdentifiersOrTags.Contains(tag))) { continue; }
55  int count = gearTags.Count(t => t == tag);
56  AIObjectiveGetItem? getItem = null;
57  TryAddSubObjective(ref getItem, () =>
58  {
59  var getItem = new AIObjectiveGetItem(character, tag, objectiveManager, Equip, CheckInventory && count <= 1)
60  {
62  Wear = Wear,
65  ignoredIdentifiersOrTags = ignoredTags,
68  ItemCount = count,
69  SpeakIfFails = RequireAllItems,
70 
71  };
72  if (ItemFilter != null)
73  {
74  getItem.ItemFilter = (Item it) => ItemFilter(it, tag);
75  }
76  return getItem;
77  },
78  onCompleted: () =>
79  {
80  var item = getItem?.TargetItem;
81  if (item?.IsOwnedBy(character) != null)
82  {
83  achievedItems.Add(item);
84  }
85  },
86  onAbandon: () =>
87  {
88  var item = getItem?.TargetItem;
89  if (item != null)
90  {
91  achievedItems.Remove(item);
92  }
93  RemoveSubObjective(ref getItem);
94  if (RequireAllItems)
95  {
96  Abandon = true;
97  }
98  });
99  }
100  subObjectivesCreated = true;
101  }
102 
103  public override void Reset()
104  {
105  base.Reset();
106  subObjectivesCreated = false;
107  achievedItems.Clear();
108  }
109  }
110 }
static IEnumerable< Identifier > ParseIgnoredTags(IEnumerable< Identifier > identifiersOrTags)
readonly ImmutableHashSet< Identifier > IdentifiersOrTags
static IEnumerable< Identifier > ParseGearTags(IEnumerable< Identifier > identifiersOrTags)
AIObjectiveGetItems(Character character, AIObjectiveManager objectiveManager, IEnumerable< Identifier > identifiersOrTags, float priorityModifier=1)
Func< Item, Identifier, bool >? ItemFilter
T1 = item to check, T2 = tag we're trying to find a suitable item for
override void Act(float deltaTime)
readonly HashSet< Item > achievedItems
override bool CheckObjectiveSpecific()
Should return whether the objective is completed or not.