声音
This commit is contained in:
240
Source/WulaFallenEmpire/EventSystem/Condition/ConditionBase.cs
Normal file
240
Source/WulaFallenEmpire/EventSystem/Condition/ConditionBase.cs
Normal file
@@ -0,0 +1,240 @@
|
||||
using Verse;
|
||||
using RimWorld;
|
||||
|
||||
namespace WulaFallenEmpire
|
||||
{
|
||||
public abstract class ConditionBase
|
||||
{
|
||||
public abstract bool IsMet(out string reason);
|
||||
}
|
||||
|
||||
public class Condition_VariableEquals : ConditionBase
|
||||
{
|
||||
public string name;
|
||||
public string value;
|
||||
public string valueVariableName;
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
if (!eventVarManager.HasVariable(name))
|
||||
{
|
||||
reason = $"Variable '{name}' not found.";
|
||||
return false;
|
||||
}
|
||||
|
||||
object variable = eventVarManager.GetVariable<object>(name);
|
||||
string compareValueStr = value;
|
||||
|
||||
if (!string.IsNullOrEmpty(valueVariableName))
|
||||
{
|
||||
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
||||
if (compareValueStr == null)
|
||||
{
|
||||
reason = $"Comparison variable '{valueVariableName}' not set.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool met = false;
|
||||
try
|
||||
{
|
||||
if (variable is int)
|
||||
{
|
||||
met = (int)variable == int.Parse(compareValueStr);
|
||||
}
|
||||
else if (variable is float)
|
||||
{
|
||||
met = (float)variable == float.Parse(compareValueStr);
|
||||
}
|
||||
else if (variable is bool)
|
||||
{
|
||||
met = (bool)variable == bool.Parse(compareValueStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
met = variable?.ToString() == compareValueStr;
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Warning($"[EventSystem] Condition_VariableEquals: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
|
||||
reason = "Type mismatch or parsing error during comparison.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!met)
|
||||
{
|
||||
reason = $"Requires {name} = {compareValueStr} (Current: {variable})";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "";
|
||||
}
|
||||
return met;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Condition_CompareVariable : ConditionBase
|
||||
{
|
||||
public string name;
|
||||
public float value;
|
||||
public string valueVariableName;
|
||||
|
||||
protected abstract bool Compare(float var1, float var2);
|
||||
protected abstract string GetOperatorString();
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
if (!eventVarManager.HasVariable(name))
|
||||
{
|
||||
Log.Message($"[EventSystem] {GetType().Name}: Variable '{name}' not found, defaulting to 0f.");
|
||||
eventVarManager.SetVariable(name, 0f);
|
||||
}
|
||||
|
||||
float variable = eventVarManager.GetVariable<float>(name);
|
||||
|
||||
float compareValue = value;
|
||||
if (!string.IsNullOrEmpty(valueVariableName))
|
||||
{
|
||||
compareValue = eventVarManager.GetVariable<float>(valueVariableName, float.NaN);
|
||||
if (float.IsNaN(compareValue))
|
||||
{
|
||||
reason = $"Comparison variable '{valueVariableName}' not set or not a number.";
|
||||
Log.Warning($"[EventSystem] {GetType().Name} check for '{name}' failed: {reason}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool met = Compare(variable, compareValue);
|
||||
Log.Message($"[EventSystem] {GetType().Name} check: Name='{name}', CurrentValue='{variable}', CompareValue='{compareValue}', Met={met}");
|
||||
if (!met)
|
||||
{
|
||||
reason = $"Requires {name} {GetOperatorString()} {compareValue} (Current: {variable})";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "";
|
||||
}
|
||||
return met;
|
||||
}
|
||||
}
|
||||
|
||||
public class Condition_VariableGreaterThan : Condition_CompareVariable
|
||||
{
|
||||
protected override bool Compare(float var1, float var2) => var1 > var2;
|
||||
protected override string GetOperatorString() => ">";
|
||||
}
|
||||
|
||||
public class Condition_VariableLessThan : Condition_CompareVariable
|
||||
{
|
||||
protected override bool Compare(float var1, float var2) => var1 < var2;
|
||||
protected override string GetOperatorString() => "<";
|
||||
}
|
||||
|
||||
public class Condition_VariableGreaterThanOrEqual : Condition_CompareVariable
|
||||
{
|
||||
protected override bool Compare(float var1, float var2) => var1 >= var2;
|
||||
protected override string GetOperatorString() => ">=";
|
||||
}
|
||||
|
||||
public class Condition_VariableLessThanOrEqual : Condition_CompareVariable
|
||||
{
|
||||
protected override bool Compare(float var1, float var2) => var1 <= var2;
|
||||
protected override string GetOperatorString() => "<=";
|
||||
}
|
||||
|
||||
public class Condition_VariableNotEqual : ConditionBase
|
||||
{
|
||||
public string name;
|
||||
public string value;
|
||||
public string valueVariableName;
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
if (!eventVarManager.HasVariable(name))
|
||||
{
|
||||
reason = $"Variable '{name}' not found.";
|
||||
return false;
|
||||
}
|
||||
|
||||
object variable = eventVarManager.GetVariable<object>(name);
|
||||
string compareValueStr = value;
|
||||
|
||||
if (!string.IsNullOrEmpty(valueVariableName))
|
||||
{
|
||||
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
|
||||
if (compareValueStr == null)
|
||||
{
|
||||
reason = $"Comparison variable '{valueVariableName}' not set.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool met = false;
|
||||
try
|
||||
{
|
||||
if (variable is int)
|
||||
{
|
||||
met = (int)variable != int.Parse(compareValueStr);
|
||||
}
|
||||
else if (variable is float)
|
||||
{
|
||||
met = (float)variable != float.Parse(compareValueStr);
|
||||
}
|
||||
else if (variable is bool)
|
||||
{
|
||||
met = (bool)variable != bool.Parse(compareValueStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
met = variable?.ToString() != compareValueStr;
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Log.Warning($"[EventSystem] Condition_VariableNotEqual: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
|
||||
reason = "Type mismatch or parsing error during comparison.";
|
||||
return false;
|
||||
}
|
||||
|
||||
Log.Message($"[EventSystem] Condition_VariableNotEqual check: Name='{name}', Type='{variable?.GetType().Name ?? "null"}', CurrentValue='{variable}', CompareValue='{compareValueStr}', Met={met}");
|
||||
if (!met)
|
||||
{
|
||||
reason = $"Requires {name} != {compareValueStr} (Current: {variable})";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "";
|
||||
}
|
||||
return met;
|
||||
}
|
||||
}
|
||||
|
||||
public class Condition_FactionExists : ConditionBase
|
||||
{
|
||||
public FactionDef factionDef;
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
if (factionDef == null)
|
||||
{
|
||||
reason = "FactionDef not specified in Condition_FactionExists.";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool exists = Find.FactionManager.FirstFactionOfDef(factionDef) != null;
|
||||
if (!exists)
|
||||
{
|
||||
reason = $"Faction '{factionDef.label}' does not exist in the world.";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = "";
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using Verse;
|
||||
using RimWorld;
|
||||
|
||||
namespace WulaFallenEmpire
|
||||
{
|
||||
public abstract class Condition
|
||||
{
|
||||
public abstract bool IsMet(out string reason);
|
||||
}
|
||||
|
||||
public class Condition_FlagExists : ConditionBase
|
||||
{
|
||||
public string flagName;
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
if (string.IsNullOrEmpty(flagName))
|
||||
{
|
||||
reason = "Flag name is not specified.";
|
||||
return false;
|
||||
}
|
||||
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
bool flagExists = eventVarManager.HasFlag(flagName);
|
||||
|
||||
if (!flagExists)
|
||||
{
|
||||
reason = $"Flag '{flagName}' does not exist or has expired.";
|
||||
}
|
||||
else
|
||||
{
|
||||
int remainingTicks = eventVarManager.GetFlagRemainingTicks(flagName);
|
||||
if (remainingTicks < 0)
|
||||
{
|
||||
reason = $"Flag '{flagName}' exists (permanent).";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = $"Flag '{flagName}' exists (expires in {remainingTicks} ticks).";
|
||||
}
|
||||
}
|
||||
|
||||
Log.Message($"[EventSystem] Condition_FlagExists check: Flag='{flagName}', Exists={flagExists}, Reason='{reason}'");
|
||||
return flagExists;
|
||||
}
|
||||
}
|
||||
|
||||
public class Condition_FlagNotExists : ConditionBase
|
||||
{
|
||||
public string flagName;
|
||||
|
||||
public override bool IsMet(out string reason)
|
||||
{
|
||||
if (string.IsNullOrEmpty(flagName))
|
||||
{
|
||||
reason = "Flag name is not specified.";
|
||||
return false;
|
||||
}
|
||||
|
||||
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
|
||||
bool flagExists = eventVarManager.HasFlag(flagName);
|
||||
|
||||
if (flagExists)
|
||||
{
|
||||
int remainingTicks = eventVarManager.GetFlagRemainingTicks(flagName);
|
||||
if (remainingTicks < 0)
|
||||
{
|
||||
reason = $"Flag '{flagName}' exists (permanent).";
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = $"Flag '{flagName}' exists (expires in {remainingTicks} ticks).";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
reason = $"Flag '{flagName}' does not exist.";
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user