Server LuaCsForBarotrauma
DamageModifier.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Xml.Linq;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 using System.Linq;
7 
8 namespace Barotrauma
9 {
11  {
12  public string Name => "Damage Modifier";
13 
14  public Dictionary<Identifier, SerializableProperty> SerializableProperties { get; private set; }
15 
16  [Serialize(1.0f, IsPropertySaveable.No), Editable(DecimalCount = 2)]
17  public float DamageMultiplier
18  {
19  get;
20  private set;
21  }
22 
23  [Serialize(1.0f, IsPropertySaveable.No), Editable(DecimalCount = 2, MinValueFloat = 0, MaxValueFloat = 1)]
24  public float ProbabilityMultiplier
25  {
26  get;
27  private set;
28  }
29 
30  [Serialize("0.0,360", IsPropertySaveable.No), Editable]
31  public Vector2 ArmorSector
32  {
33  get;
34  private set;
35  }
36 
37  public Vector2 ArmorSectorInRadians => new Vector2(MathHelper.ToRadians(ArmorSector.X), MathHelper.ToRadians(ArmorSector.Y));
38 
40  public bool DeflectProjectiles
41  {
42  get;
43  private set;
44  }
45 
47  public string AfflictionIdentifiers
48  {
49  get
50  {
51  return rawAfflictionIdentifierString;
52  }
53  private set
54  {
55  rawAfflictionIdentifierString = value;
56  parsedAfflictionIdentifiers = rawAfflictionIdentifierString.ToIdentifiers().ToImmutableArray();
57  }
58  }
59 
61  public string AfflictionTypes
62  {
63  get
64  {
65  return rawAfflictionTypeString;
66  }
67  private set
68  {
69  rawAfflictionTypeString = value;
70  parsedAfflictionTypes = rawAfflictionTypeString.ToIdentifiers().ToImmutableArray();
71  }
72  }
73 
74  private string rawAfflictionIdentifierString;
75  private string rawAfflictionTypeString;
76  private ImmutableArray<Identifier> parsedAfflictionIdentifiers;
77  private ImmutableArray<Identifier> parsedAfflictionTypes;
78  public ref readonly ImmutableArray<Identifier> ParsedAfflictionIdentifiers => ref parsedAfflictionIdentifiers;
79 
80  public ref readonly ImmutableArray<Identifier> ParsedAfflictionTypes => ref parsedAfflictionTypes;
81 
82  public DamageModifier(ContentXElement element, string parentDebugName, bool checkErrors = true)
83  {
84  Deserialize(element);
85  if (element.GetAttribute("afflictionnames") != null)
86  {
87  DebugConsole.ThrowError("Error in DamageModifier config (" + parentDebugName + ") - define afflictions using identifiers or types instead of names.",
88  contentPackage: element.ContentPackage);
89  }
90  if (checkErrors)
91  {
92  foreach (var afflictionType in parsedAfflictionTypes)
93  {
94  if (!AfflictionPrefab.Prefabs.Any(p => p.AfflictionType == afflictionType))
95  {
96  createWarningOrError($"Potentially invalid damage modifier in \"{parentDebugName}\". Could not find any afflictions of the type \"{afflictionType}\". Did you mean to use an affliction identifier instead?");
97  }
98  }
99  foreach (var afflictionIdentifier in parsedAfflictionIdentifiers)
100  {
101  if (!AfflictionPrefab.Prefabs.ContainsKey(afflictionIdentifier))
102  {
103  createWarningOrError($"Potentially invalid damage modifier in \"{parentDebugName}\". Could not find any afflictions with the identifier \"{afflictionIdentifier}\". Did you mean to use an affliction type instead?");
104  }
105  }
106  if (!parsedAfflictionTypes.Any() && !parsedAfflictionIdentifiers.Any())
107  {
108  createWarningOrError($"Potentially invalid damage modifier in \"{parentDebugName}\". Neither affliction types of identifiers defined.");
109  }
110  }
111 
112  void createWarningOrError(string msg)
113  {
114 #if DEBUG
115  DebugConsole.ThrowError(msg, contentPackage: element.ContentPackage);
116 #else
117  DebugConsole.AddWarning(msg, contentPackage: element.ContentPackage);
118 #endif
119  }
120  }
121 
122  public bool MatchesAfflictionIdentifier(string identifier) =>
123  MatchesAfflictionIdentifier(identifier.ToIdentifier());
124 
125  public bool MatchesAfflictionIdentifier(Identifier identifier)
126  {
127  //if no identifiers have been defined, the damage modifier affects all afflictions
128  if (AfflictionIdentifiers.Length == 0) { return true; }
129  return parsedAfflictionIdentifiers.Any(id => id == identifier);
130  }
131 
132  public bool MatchesAfflictionType(string type) =>
133  MatchesAfflictionType(type.ToIdentifier());
134 
135  public bool MatchesAfflictionType(Identifier type)
136  {
137  //if no types have been defined, the damage modifier affects all afflictions
138  if (AfflictionTypes.Length == 0) { return true; }
139  return parsedAfflictionTypes.Any(t => t == type);
140  }
141 
145  public bool MatchesAffliction(string identifier, string type) =>
146  MatchesAffliction(identifier.ToIdentifier(), type.ToIdentifier());
147 
148  public bool MatchesAffliction(Identifier identifier, Identifier type)
149  {
150  //if no identifiers or types have been defined, the damage modifier affects all afflictions
151  if (AfflictionIdentifiers.Length == 0 && AfflictionTypes.Length == 0) { return true; }
152  return parsedAfflictionIdentifiers.Any(id => id == identifier)
153  || parsedAfflictionTypes.Any(t => t == type);
154  }
155 
156  public bool MatchesAffliction(Affliction affliction) => MatchesAffliction(affliction.Identifier, affliction.Prefab.AfflictionType);
157 
158  public void Serialize(XElement element)
159  {
160  if (element == null) { return; }
162  }
163 
164  public void Deserialize(XElement element)
165  {
166  if (element == null) { return; }
168  }
169  }
170 }
AfflictionPrefab is a prefab that defines a type of affliction that can be applied to a character....
static readonly PrefabCollection< AfflictionPrefab > Prefabs
ContentPackage? ContentPackage
XAttribute? GetAttribute(string name)
bool MatchesAfflictionType(string type)
void Serialize(XElement element)
bool MatchesAfflictionIdentifier(string identifier)
void Deserialize(XElement element)
Dictionary< Identifier, SerializableProperty > SerializableProperties
DamageModifier(ContentXElement element, string parentDebugName, bool checkErrors=true)
bool MatchesAfflictionIdentifier(Identifier identifier)
bool MatchesAffliction(Affliction affliction)
bool MatchesAffliction(string identifier, string type)
Returns true if the type or the identifier matches the defined types/identifiers.
bool MatchesAfflictionType(Identifier type)
ref readonly ImmutableArray< Identifier > ParsedAfflictionIdentifiers
ref readonly ImmutableArray< Identifier > ParsedAfflictionTypes
bool MatchesAffliction(Identifier identifier, Identifier type)
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)