Moved PropertyConditional match checking & status effect cancelling from the StatusEffect class to PropertyConditional.

This commit is contained in:
Joonas Rikkonen
2018-01-09 16:42:11 +02:00
parent 0671dba1b4
commit 3956785032
3 changed files with 92 additions and 74 deletions
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.Linq;
@@ -30,6 +31,8 @@ namespace Barotrauma
public readonly OperatorType Operator;
public readonly string Value;
private readonly int cancelStatusEffect;
public PropertyConditional(XAttribute attribute)
{
string attributeString = attribute.Value.ToString();
@@ -81,12 +84,25 @@ namespace Barotrauma
default:
if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything
{
atStr = attributeString; //We probably don't even have an operator
DebugConsole.ThrowError("Error in property conditional - \"" + op + "\" is not a valid operator.");
atStr = attributeString; //We probably don't even have an operator
}
break;
}
foreach (XElement subElement in attribute.Parent.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "cancel":
case "canceleffect":
case "cancelstatuseffect":
//This only works if there's a conditional checking for status effect tags. There is no way to cancel *all* status effects atm.
cancelStatusEffect = 1;
if (subElement.GetAttributeBool("all", false)) cancelStatusEffect = 2;
break;
}
}
if (!Enum.TryParse(attribute.Name.ToString(), true, out Type))
{
PropertyName = attribute.Name.ToString();
@@ -95,8 +111,75 @@ namespace Barotrauma
Value = atStr;
}
public bool Matches(ISerializableEntity target)
{
string valStr = Value.ToString();
switch (Type)
{
case ConditionType.PropertyValue:
if (target.SerializableProperties.TryGetValue(PropertyName.ToLowerInvariant(), out SerializableProperty property))
{
return Matches(property);
}
return false;
case ConditionType.Name:
return (Operator == OperatorType.Equals) == (target.Name == valStr);
case ConditionType.HasTag:
{
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 Operator == OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
}
case ConditionType.HasStatusTag:
List<DurationListElement> durations = StatusEffect.DurationList.FindAll(d => d.Targets.Contains(target));
List<DelayedListElement> delays = DelayedEffect.DelayList.FindAll(d => d.Targets.Contains(target));
bool success = false;
if (durations.Count > 0 || delays.Count > 0)
{
string[] readTags = valStr.Split(',');
foreach (DurationListElement duration in durations)
{
int matches = 0;
foreach (string tag in readTags)
if (duration.Parent.HasTag(tag)) matches++;
success = Operator == OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
StatusEffect.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 = Operator == OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
if (cancelStatusEffect > 0 && success)
DelayedEffect.DelayList.Remove(delay);
if (cancelStatusEffect != 2) //ditto
return success;
}
}
return success;
case ConditionType.SpeciesName:
Character targetCharacter = target as Character;
if (targetCharacter == null) return false;
return (Operator == OperatorType.Equals) == (targetCharacter.SpeciesName == valStr);
default:
return false;
}
}
public bool Matches(SerializableProperty property)
private bool Matches(SerializableProperty property)
{
object propertyValue = property.GetValue();