Client LuaCsForBarotrauma
CheckConnectionAction.cs
3 using System.Linq;
4 
5 namespace Barotrauma;
6 
10 class CheckConnectionAction : BinaryOptionAction
11 {
12  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the item to check.")]
13  public Identifier ItemTag { get; set; }
14 
15  [Serialize("", IsPropertySaveable.Yes, description: "The name of the connection to check on the target item.")]
16  public Identifier ConnectionName { get; set; }
17 
18  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the item the connection must be wired to. If omitted, it doesn't matter what the connection is wired to.")]
19  public Identifier ConnectedItemTag { get; set; }
20 
21  [Serialize("", IsPropertySaveable.Yes, description: "The name of the other connection the connection must be wired to. If omitted, it doesn't matter what the connection is wired to.")]
22  public Identifier OtherConnectionName { get; set; }
23 
24  [Serialize(1, IsPropertySaveable.Yes, description: "Minimum number of matching connections for the check to succeed.")]
25  public int MinAmount { get; set; }
26 
27  public CheckConnectionAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
28 
29  protected override bool? DetermineSuccess()
30  {
31  int amount = 0;
32  var connectTargets = !ConnectedItemTag.IsEmpty ? ParentEvent.GetTargets(ConnectedItemTag) : Enumerable.Empty<Entity>();
33  foreach (var target in ParentEvent.GetTargets(ItemTag))
34  {
35  if (target is not Item targetItem) { continue; }
36  if (targetItem.GetComponent<ConnectionPanel>() is not ConnectionPanel panel) { continue; }
37  if (panel.Connections == null || panel.Connections.None()) { continue; }
38  foreach (var connection in panel.Connections)
39  {
40  if (!IsCorrectConnection(connection, ConnectionName)) { continue; }
41  if (ConnectedItemTag.IsEmpty && OtherConnectionName.IsEmpty)
42  {
43  amount += connection.Wires.Count;
44  if (amount >= MinAmount) { return true; }
45  continue;
46  }
47  foreach (var wire in connection.Wires)
48  {
49  if (wire.OtherConnection(connection) is not Connection otherConnection) { continue; }
50  if (!ConnectedItemTag.IsEmpty && !IsCorrectConnection(otherConnection, OtherConnectionName)) { continue; }
51  if (!ConnectedItemTag.IsEmpty && !IsCorrectItem()) { continue; }
52  amount++;
53  if (amount >= MinAmount) { return true; }
54  bool IsCorrectItem() => connectTargets.Contains(otherConnection.Item);
55  }
56 
57  bool IsCorrectConnection(Connection connection, Identifier id) => connection.Name.ToIdentifier() == id;
58  }
59  }
60  return false;
61  }
62 }
Check whether a specific connection of an item is wired to a specific kind of connection.
CheckConnectionAction(ScriptedEvent parentEvent, ContentXElement element)
override? bool DetermineSuccess()