Client LuaCsForBarotrauma
CheckAfflictionAction.cs
1 #nullable enable
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace Barotrauma
6 {
10  internal class CheckAfflictionAction : BinaryOptionAction
11  {
12  [Serialize("", IsPropertySaveable.Yes, description: "Identifier of the affliction.")]
13  public Identifier Identifier { get; set; } = Identifier.Empty;
14 
15  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the character to check.")]
16  public Identifier TargetTag { get; set; } = Identifier.Empty;
17 
18  [Serialize("", IsPropertySaveable.Yes, description: "Tag referring to the character who caused the affliction. Can be used to require the affliction to be caused by a specific character.")]
19  public Identifier SourceCharacter { get; set; } = Identifier.Empty;
20 
21  [Serialize(LimbType.None, IsPropertySaveable.Yes, "Only check afflictions on the specified limb type.")]
22  public LimbType TargetLimb { get; set; }
23 
24  [Serialize(true, IsPropertySaveable.Yes, "When set to false, limb-specific afflictions are ignored when not checking a specific limb.")]
25  public bool AllowLimbAfflictions { get; set; }
26 
27  [Serialize(0.0f, IsPropertySaveable.Yes, "Minimum strength of the affliction.")]
28  public float MinStrength { get; set; }
29 
30  public CheckAfflictionAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
31 
32  protected override bool? DetermineSuccess()
33  {
34  if (Identifier.IsEmpty || TargetTag.IsEmpty) { return false; }
35  List<Character> targets = ParentEvent.GetTargets(TargetTag).OfType<Character>().ToList();
36 
37  foreach (var target in targets)
38  {
39  if (target.CharacterHealth == null) { continue; }
40  if (TargetLimb == LimbType.None)
41  {
42  if (target.CharacterHealth.GetAfflictionStrengthByIdentifier(Identifier, AllowLimbAfflictions) >= MinStrength) { return true; }
43  }
44  IEnumerable<Affliction> afflictions = target.CharacterHealth.GetAllAfflictions().Where(affliction =>
45  {
46  if (affliction.Prefab.LimbSpecific)
47  {
48  LimbType? limbType = target.CharacterHealth.GetAfflictionLimb(affliction)?.type;
49  if (limbType == null || limbType != TargetLimb) { return false; }
50  }
51  if (!SourceCharacter.IsEmpty)
52  {
53  if (!ParentEvent.GetTargets(SourceCharacter).Contains(affliction.Source)) { return false; }
54  }
55 
56  return affliction.Strength >= MinStrength;
57  });
58  if (afflictions.Any(a => a.Identifier == Identifier)) { return true; }
59  }
60  return false;
61  }
62 
63  public override string ToDebugString()
64  {
65  return $"{ToolBox.GetDebugSymbol(HasBeenDetermined())} {nameof(CheckAfflictionAction)} -> (TargetTag: {TargetTag.ColorizeObject()}, " +
66  $"AfflictionIdentifier: {Identifier.ColorizeObject()}, " +
67  $"TargetLimb: {TargetLimb.ColorizeObject()}, " +
68  $"Succeeded: {succeeded.ColorizeObject()})";
69  }
70  }
71 }
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
IEnumerable< Entity > GetTargets(Identifier tag)