Client LuaCsForBarotrauma
AbilityConditionAttackData.cs
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma.Abilities
7 {
9  {
10  [Flags]
11  private enum WeaponType
12  {
13  Any = 0,
14  Melee = 1,
15  Ranged = 2,
16  HandheldRanged = 4,
17  Turret = 8,
18  NoWeapon = 16
19  };
20 
21  private static readonly List<WeaponType> WeaponTypeValues = Enum.GetValues(typeof(WeaponType)).Cast<WeaponType>().ToList();
22 
23  private readonly string itemIdentifier;
24  private readonly Identifier[] tags;
25  private readonly WeaponType weapontype;
26  private readonly bool ignoreNonHarmfulAttacks;
27 
28  private readonly bool ignoreOwnAttacks;
29 
31  {
32  itemIdentifier = conditionElement.GetAttributeString(nameof(itemIdentifier), string.Empty);
33  tags = conditionElement.GetAttributeIdentifierArray(nameof(tags), Array.Empty<Identifier>());
34  ignoreNonHarmfulAttacks = conditionElement.GetAttributeBool(nameof(ignoreNonHarmfulAttacks), false);
35  ignoreOwnAttacks = conditionElement.GetAttributeBool(nameof(ignoreOwnAttacks), false);
36 
37  string weaponTypeStr = conditionElement.GetAttributeString("weapontype", "Any");
38  if (!Enum.TryParse(weaponTypeStr, ignoreCase: true, out weapontype))
39  {
40  DebugConsole.ThrowError($"Error in talent \"{characterTalent.DebugIdentifier}\": \"{weaponTypeStr}\" is not a valid weapon type.",
41  contentPackage: conditionElement.ContentPackage);
42  }
43  }
44 
45  protected override bool MatchesConditionSpecific(AbilityObject abilityObject)
46  {
47  if (abilityObject is AbilityAttackData attackData)
48  {
49  if (ignoreOwnAttacks && attackData.Attacker == character) { return false; }
50 
51  if (ignoreNonHarmfulAttacks && attackData.SourceAttack != null)
52  {
53  if (attackData.SourceAttack.Stun <= 0.0f && (attackData.SourceAttack.Afflictions?.All(a => a.Key.Prefab.IsBuff) ?? true))
54  {
55  return false;
56  }
57  }
58 
59  Item item = attackData?.SourceAttack?.SourceItem;
60 
61  if (!string.IsNullOrEmpty(itemIdentifier))
62  {
63  if (item?.Prefab.Identifier != itemIdentifier)
64  {
65  return false;
66  }
67  }
68 
69  if (tags.Any())
70  {
71  if (!tags.All(t => item?.HasTag(t) ?? false))
72  {
73  return false;
74  }
75  }
76 
77  if (weapontype != WeaponType.Any)
78  {
79  foreach (WeaponType wt in WeaponTypeValues)
80  {
81  if (wt == WeaponType.Any || !weapontype.HasFlag(wt)) { continue; }
82  switch (wt)
83  {
84  case WeaponType.Melee:
85  //if the item has an active projectile component (has been fired), don't consider it a melee weapon
86  if (item?.GetComponent<Projectile>() is { IsActive: true }) { continue; }
87  if (item?.GetComponent<MeleeWeapon>() != null) { return true; }
88  break;
89  case WeaponType.Ranged:
90  //if the item has a melee weapon component that's being used now, don't consider it a projectile
91  if (item?.GetComponent<MeleeWeapon>() is { Hitting: true }) { continue; }
92  if (item?.GetComponent<Projectile>() != null) { return true; }
93  break;
94  case WeaponType.HandheldRanged:
95  {
96  var projectile = item?.GetComponent<Projectile>();
97  if (projectile?.Launcher?.GetComponent<Holdable>() != null) { return true; }
98  }
99  break;
100  case WeaponType.Turret:
101  {
102  var projectile = item?.GetComponent<Projectile>();
103  if (projectile?.Launcher?.GetComponent<Turret>() != null) { return true; }
104  }
105  break;
106  case WeaponType.NoWeapon:
107  if (item == null) { return true; }
108  break;
109  }
110  }
111  return false;
112  }
113 
114  return true;
115  }
116  else
117  {
118  LogAbilityConditionError(abilityObject, typeof(AbilityAttackData));
119  return false;
120  }
121  }
122  }
123 }
AbilityConditionAttackData(CharacterTalent characterTalent, ContentXElement conditionElement)
override bool MatchesConditionSpecific(AbilityObject abilityObject)
void LogAbilityConditionError(AbilityObject abilityObject, Type expectedData)
string? GetAttributeString(string key, string? def)
Identifier[] GetAttributeIdentifierArray(Identifier[] def, params string[] keys)
ContentPackage? ContentPackage
bool GetAttributeBool(string key, bool def)
readonly Identifier Identifier
Definition: Prefab.cs:34