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
@@ -57,7 +57,6 @@ namespace Barotrauma
public bool Stackable; //Can the same status effect be applied several times to the same targets?
private readonly int useItemCount;
private readonly int cancelStatusEffect;
public readonly ActionType type;
@@ -212,12 +211,6 @@ namespace Barotrauma
case "useitem":
useItemCount++;
break;
case "cancel":
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;
case "requireditem":
case "requireditems":
RelatedItem newRequiredItem = RelatedItem.Load(subElement);
@@ -270,69 +263,10 @@ namespace Barotrauma
if (!propertyConditionals.Any()) return true;
foreach (ISerializableEntity target in targets)
{
if (target == null || target.SerializableProperties == null) continue;
foreach (PropertyConditional pc in propertyConditionals)
{
if (target == null || target.SerializableProperties == null) continue;
string valStr = pc.Value.ToString();
switch (pc.Type)
{
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 == PropertyConditional.OperatorType.Equals) == (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 operator is == then it needs to match everything, otherwise if its != there must be zero matches.
return pc.Operator == PropertyConditional.OperatorType.Equals ? matches >= readTags.Length : matches <= 0;
}
case PropertyConditional.ConditionType.HasStatusTag:
List<DurationListElement> durations = DurationList.FindAll(d => d.Targets.Contains(target));
List<DelayedListElement> 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 == PropertyConditional.OperatorType.Equals ? 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 == PropertyConditional.OperatorType.Equals ? 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 == PropertyConditional.OperatorType.Equals) == (((Character)target).SpeciesName == valStr);
}
{
if (!pc.Matches(target)) return false;
}
}
return true;