diff --git a/Barotrauma/BarotraumaShared/Source/StatusEffects/PropertyConditional.cs b/Barotrauma/BarotraumaShared/Source/StatusEffects/PropertyConditional.cs index 423a1acf7..678dc030c 100644 --- a/Barotrauma/BarotraumaShared/Source/StatusEffects/PropertyConditional.cs +++ b/Barotrauma/BarotraumaShared/Source/StatusEffects/PropertyConditional.cs @@ -1,13 +1,24 @@ using System; +using System.Globalization; using System.Xml.Linq; namespace Barotrauma { class PropertyConditional { - public readonly string Attribute; + public enum ConditionType + { + PropertyValue, + Name, + SpeciesName, + HasTag, + HasStatusTag + } + + public readonly ConditionType Type; + public readonly string PropertyName; public readonly string Operator; - public readonly object Value; + public readonly string Value; public PropertyConditional(XAttribute attribute) { @@ -66,45 +77,58 @@ namespace Barotrauma break; } - Attribute = attribute.Name.ToString().ToLowerInvariant(); + if (!Enum.TryParse(attribute.Name.ToString(), true, out Type)) + { + PropertyName = attribute.Name.ToString(); + Type = ConditionType.PropertyValue; + } + Operator = op; Value = atStr; } - - public PropertyConditional(string Attribute, string Operator, object Value) - { - this.Attribute = Attribute; - this.Operator = Operator; - this.Value = Value; - } - + public bool Matches(SerializableProperty property) { - if (property.GetValue() == null) + object propertyValue = property.GetValue(); + + if (propertyValue == null) { DebugConsole.ThrowError("Couldn't compare " + Value.ToString() + " (" + Value.GetType() + ") to property \"" + property.Name + "- property.GetValue() returns null!!"); return false; } - Type type = property.GetValue().GetType(); + Type type = propertyValue.GetType(); float? floatValue = null; float? floatProperty = null; if (type == typeof(float) || type == typeof(int)) { - floatValue = Convert.ToSingle(Value); - floatProperty = Convert.ToSingle(property.GetValue()); + if (Single.TryParse(Value, NumberStyles.Float, CultureInfo.InvariantCulture, out float parsedFloat)) + { + floatValue = parsedFloat; + } + floatProperty = (float)propertyValue; } switch (Operator) { case "==": - if (property.GetValue().Equals(floatValue == null ? Value : floatValue)) - return true; - break; + if (floatValue == null) + { + return property.GetValue().Equals(floatValue); + } + else + { + return property.GetValue().Equals(Value); + } case "!=": - if (property.GetValue().Equals(floatValue == null ? Value : floatValue)) - return true; - break; + if (floatValue == null) + { + return !property.GetValue().Equals(floatValue); + } + else + { + return !property.GetValue().Equals(Value); + } case ">": if (floatValue == null) { diff --git a/Barotrauma/BarotraumaShared/Source/StatusEffects/StatusEffect.cs b/Barotrauma/BarotraumaShared/Source/StatusEffects/StatusEffect.cs index b0312b90a..28c523af8 100644 --- a/Barotrauma/BarotraumaShared/Source/StatusEffects/StatusEffect.cs +++ b/Barotrauma/BarotraumaShared/Source/StatusEffects/StatusEffect.cs @@ -274,64 +274,65 @@ namespace Barotrauma { if (target == null || target.SerializableProperties == null) continue; - if (!target.SerializableProperties.TryGetValue(pc.Attribute, out SerializableProperty property)) + string valStr = pc.Value.ToString(); + + switch (pc.Type) { - //Do special conditional checks - - string valStr = pc.Value.ToString(); - if (pc.Attribute == "name") + case PropertyConditional.ConditionType.PropertyValue: + if (target.SerializableProperties.TryGetValue(pc.PropertyName.ToLowerInvariant(), out SerializableProperty property)) + { + return pc.Matches(property); + } + return false; + case PropertyConditional.ConditionType.Name: return pc.Operator == "==" ? target.Name == valStr : target.Name != valStr; + case PropertyConditional.ConditionType.HasTag: + { + string[] readTags = valStr.Split(','); + int matches = 0; + foreach (string tag in readTags) + if (((Item)target).HasTag(tag)) matches++; - if (pc.Attribute == "speciesname" && target is Character) + //If operator is == then it needs to match everything, otherwise if its != there must be zero matches. + return pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; + } + case PropertyConditional.ConditionType.HasStatusTag: + List durations = DurationList.FindAll(d => d.Targets.Contains(target)); + List delays = DelayedEffect.DelayList.FindAll(d => d.Targets.Contains(target)); + + bool success = false; + if (durations.Any() || delays.Any()) + { + string[] readTags = valStr.Split(','); + foreach (DurationListElement duration in durations) + { + int matches = 0; + foreach (string tag in readTags) + if (duration.Parent.HasTag(tag)) matches++; + + success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; + if (cancelStatusEffect > 0 && success) + DurationList.Remove(duration); + if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags + return success; + } + foreach (DelayedListElement delay in delays) + { + int matches = 0; + foreach (string tag in readTags) + if (delay.Parent.HasTag(tag)) matches++; + + success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; + if (cancelStatusEffect > 0 && success) + DelayedEffect.DelayList.Remove(delay); + if (cancelStatusEffect != 2) //ditto + return success; + } + } + return success; + case PropertyConditional.ConditionType.SpeciesName: return pc.Operator == "==" ? ((Character)target).SpeciesName == valStr : ((Character)target).SpeciesName != valStr; - - if ((pc.Attribute == "hastag" || pc.Attribute == "hastags") && target is Item) - { - string[] readTags = valStr.Split(','); - int matches = 0; - foreach (string tag in readTags) - if (((Item)target).HasTag(tag)) matches++; - - //If operator is == then it needs to match everything, otherwise if its != there must be zero matches. - return pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; - } - - List durations = DurationList.FindAll(d => d.Targets.Contains(target)); - List delays = DelayedEffect.DelayList.FindAll(d => d.Targets.Contains(target)); - - bool success = false; - if (pc.Attribute == "hasstatustag" || pc.Attribute == "hasstatustags" && (durations.Any() || delays.Any())) - { - string[] readTags = valStr.Split(','); - foreach (DurationListElement duration in durations) - { - int matches = 0; - foreach (string tag in readTags) - if (duration.Parent.HasTag(tag)) matches++; - - success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; - if (cancelStatusEffect > 0 && success) - DurationList.Remove(duration); - if (cancelStatusEffect != 2) //cancelStatusEffect 1 = only cancel once, cancelStatusEffect 2 = cancel all of matching tags - return success; - } - foreach (DelayedListElement delay in delays) - { - int matches = 0; - foreach (string tag in readTags) - if (delay.Parent.HasTag(tag)) matches++; - - success = pc.Operator == "==" ? matches >= readTags.Length : matches <= 0; - if (cancelStatusEffect > 0 && success) - DelayedEffect.DelayList.Remove(delay); - if (cancelStatusEffect != 2) //ditto - return success; - } - } - return success; } - else if (!pc.Matches(property)) - return false; } } return true;