Client LuaCsForBarotrauma
CheckSelectedAction.cs
2 using System.Collections.Generic;
3 
4 namespace Barotrauma
5 {
10  {
11  public enum SelectedItemType { Primary, Secondary, Any };
12 
13  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the character to check.")]
14  public Identifier CharacterTag { get; set; }
15 
16  [Serialize("", IsPropertySaveable.Yes, description: "If specified, only items that have been given this tag using TagAction are considered valid.")]
17  public Identifier TargetTag { get; set; }
18 
19  [Serialize(SelectedItemType.Any, IsPropertySaveable.Yes, description: "How does the item need to be selected? Primary item (i.e. any device you're interacting with), secondary item (such as ladders or chairs which allow interacting with a primary item at the same time), or either?")]
20  public SelectedItemType ItemType { get; set; }
21 
22  public CheckSelectedAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
23 
24  protected override bool? DetermineSuccess()
25  {
26  Character character = null;
27  if (!CharacterTag.IsEmpty)
28  {
29  foreach (var t in ParentEvent.GetTargets(CharacterTag))
30  {
31  if (t is Character c)
32  {
33  character = c;
34  break;
35  }
36  }
37  }
38  if (character == null)
39  {
40  Error($"{nameof(CheckSelectedAction)} error: {GetEventName()} uses a {nameof(CheckSelectedAction)} but no valid character was found for tag \"{CharacterTag}\"! This will cause the check to automatically fail.");
41  return false;
42  }
43  if (!TargetTag.IsEmpty)
44  {
45  IEnumerable<Entity> targets = ParentEvent.GetTargets(TargetTag);
46  if (targets.None())
47  {
48  Error($"{nameof(CheckSelectedAction)} error: {GetEventName()} uses a {nameof(CheckSelectedAction)} but no valid targets were found for tag \"{TargetTag}\"! This will cause the check to automatically fail.");
49  return false;
50  }
51  foreach (var target in targets)
52  {
53  if (target is Character targetCharacter)
54  {
55  if (ItemType == SelectedItemType.Any && character.SelectedCharacter == targetCharacter) { return true; }
56  continue;
57  }
58  if (target is not Item targetItem)
59  {
60  continue;
61  }
62  if (IsSelected(targetItem))
63  {
64  return true;
65  }
66  }
67  return false;
68 
69  bool IsSelected(Item item)
70  {
71  return ItemType switch
72  {
73  SelectedItemType.Any => character.IsAnySelectedItem(item),
74  SelectedItemType.Primary => character.SelectedItem == item,
75  SelectedItemType.Secondary => character.SelectedSecondaryItem == item,
76  _ => false
77  };
78  }
79  }
80  else
81  {
82  return ItemType switch
83  {
84  SelectedItemType.Any => !character.HasSelectedAnyItem,
85  SelectedItemType.Primary => character.SelectedItem == null,
86  SelectedItemType.Secondary => character.SelectedSecondaryItem == null,
87  _ => false
88  };
89  }
90 #if DEBUG
91  void Error(string errorMsg)
92  {
93  DebugConsole.ThrowError(errorMsg, contentPackage: ParentEvent.Prefab.ContentPackage);
94  }
95 #else
96 
97  void Error(string errorMsg)
98  {
99  DebugConsole.LogError(errorMsg, contentPackage: ParentEvent.Prefab.ContentPackage);
100  }
101 #endif
102  }
103 
104  private string GetEventName()
105  {
106  return ParentEvent?.Prefab?.Identifier is { IsEmpty: false } identifier ? $"the event \"{identifier}\"" : "an unknown event";
107  }
108  }
109 }
bool HasSelectedAnyItem
Has the characters selected a primary or a secondary item?
Item????????? SelectedItem
The primary selected item. It can be any device that character interacts with. This excludes items li...
Item SelectedSecondaryItem
The secondary selected item. It's an item other than a device (see SelectedItem), e....
bool IsAnySelectedItem(Item item)
Is the item either the primary or the secondary selected item?
Check whether a specific character has selected a specific kind of item.
CheckSelectedAction(ScriptedEvent parentEvent, ContentXElement element)
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
EventPrefab Prefab
Definition: Event.cs:16
ContentPackage? ContentPackage
Definition: Prefab.cs:37
readonly Identifier Identifier
Definition: Prefab.cs:34
IEnumerable< Entity > GetTargets(Identifier tag)