Moved PropertyConditional match checking & status effect cancelling from the StatusEffect class to PropertyConditional.
This commit is contained in:
@@ -380,8 +380,9 @@
|
|||||||
<Throwable canBeCombined="true" slots="Any,RightHand,LeftHand" throwforce="4.0" aimpos="35,-10">
|
<Throwable canBeCombined="true" slots="Any,RightHand,LeftHand" throwforce="4.0" aimpos="35,-10">
|
||||||
<StatusEffect type="OnUse" target="Character">
|
<StatusEffect type="OnUse" target="Character">
|
||||||
<RequiredItem name="Medical Syringe" type="Container"/>
|
<RequiredItem name="Medical Syringe" type="Container"/>
|
||||||
<Conditional hasstatustag="morbusine"/>
|
<Conditional hasstatustag="morbusine">
|
||||||
<Cancel/>
|
<CancelStatusEffect/>
|
||||||
|
</Conditional>
|
||||||
</StatusEffect>
|
</StatusEffect>
|
||||||
</Throwable>
|
</Throwable>
|
||||||
</Item>
|
</Item>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
@@ -30,6 +31,8 @@ namespace Barotrauma
|
|||||||
public readonly OperatorType Operator;
|
public readonly OperatorType Operator;
|
||||||
public readonly string Value;
|
public readonly string Value;
|
||||||
|
|
||||||
|
private readonly int cancelStatusEffect;
|
||||||
|
|
||||||
public PropertyConditional(XAttribute attribute)
|
public PropertyConditional(XAttribute attribute)
|
||||||
{
|
{
|
||||||
string attributeString = attribute.Value.ToString();
|
string attributeString = attribute.Value.ToString();
|
||||||
@@ -81,12 +84,25 @@ namespace Barotrauma
|
|||||||
default:
|
default:
|
||||||
if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything
|
if (op != "==" && op != "!=" && op != ">" && op != "<" && op != ">=" && op != "<=") //Didn't use escape strings or anything
|
||||||
{
|
{
|
||||||
atStr = attributeString; //We probably don't even have an operator
|
atStr = attributeString; //We probably don't even have an operator
|
||||||
DebugConsole.ThrowError("Error in property conditional - \"" + op + "\" is not a valid operator.");
|
|
||||||
}
|
}
|
||||||
break;
|
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))
|
if (!Enum.TryParse(attribute.Name.ToString(), true, out Type))
|
||||||
{
|
{
|
||||||
PropertyName = attribute.Name.ToString();
|
PropertyName = attribute.Name.ToString();
|
||||||
@@ -95,8 +111,75 @@ namespace Barotrauma
|
|||||||
|
|
||||||
Value = atStr;
|
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();
|
object propertyValue = property.GetValue();
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ namespace Barotrauma
|
|||||||
public bool Stackable; //Can the same status effect be applied several times to the same targets?
|
public bool Stackable; //Can the same status effect be applied several times to the same targets?
|
||||||
|
|
||||||
private readonly int useItemCount;
|
private readonly int useItemCount;
|
||||||
private readonly int cancelStatusEffect;
|
|
||||||
|
|
||||||
public readonly ActionType type;
|
public readonly ActionType type;
|
||||||
|
|
||||||
@@ -212,12 +211,6 @@ namespace Barotrauma
|
|||||||
case "useitem":
|
case "useitem":
|
||||||
useItemCount++;
|
useItemCount++;
|
||||||
break;
|
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 "requireditem":
|
||||||
case "requireditems":
|
case "requireditems":
|
||||||
RelatedItem newRequiredItem = RelatedItem.Load(subElement);
|
RelatedItem newRequiredItem = RelatedItem.Load(subElement);
|
||||||
@@ -270,69 +263,10 @@ namespace Barotrauma
|
|||||||
if (!propertyConditionals.Any()) return true;
|
if (!propertyConditionals.Any()) return true;
|
||||||
foreach (ISerializableEntity target in targets)
|
foreach (ISerializableEntity target in targets)
|
||||||
{
|
{
|
||||||
|
if (target == null || target.SerializableProperties == null) continue;
|
||||||
foreach (PropertyConditional pc in propertyConditionals)
|
foreach (PropertyConditional pc in propertyConditionals)
|
||||||
{
|
{
|
||||||
if (target == null || target.SerializableProperties == null) continue;
|
if (!pc.Matches(target)) return false;
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user