diff --git a/1.6/1.6/Assemblies/WulaFallenEmpire.dll b/1.6/1.6/Assemblies/WulaFallenEmpire.dll index 2de3a52b..0bb87f64 100644 Binary files a/1.6/1.6/Assemblies/WulaFallenEmpire.dll and b/1.6/1.6/Assemblies/WulaFallenEmpire.dll differ diff --git a/1.6/1.6/Defs/EventDefs/EventDef_Wula.xml b/1.6/1.6/Defs/EventDefs/EventDef_Wula.xml index 9974c07a..aa4b79c4 100644 --- a/1.6/1.6/Defs/EventDefs/EventDef_Wula.xml +++ b/1.6/1.6/Defs/EventDefs/EventDef_Wula.xml @@ -20,10 +20,12 @@
  • Wula_UI_Check_Odyssey_Is_Active 0 + Int
  • Wula_UI_Check_Anomaly_Is_Active 0 + Int
  • Wula_UI_Check_Odyssey_Is_Active @@ -40,7 +42,7 @@
  • Wula_FE_Spiritualist_Goodwill
  • -
  • +
  • Wula_FE_Spiritualist_Faction Wula_FE_Spiritualist_Goodwill
  • @@ -546,10 +548,12 @@
  • Wula_Has_Receive_Silver_From_Anisia 0 + Int
  • Wula_Has_Receive_MechWula_From_Anisia 0 + Int
  • diff --git a/1.6/Anomaly/Defs/EventDefs/EventDef_WULA_FE_Spiritualist.xml b/1.6/Anomaly/Defs/EventDefs/EventDef_WULA_FE_Spiritualist.xml index 5b5b3b50..b377459f 100644 --- a/1.6/Anomaly/Defs/EventDefs/EventDef_WULA_FE_Spiritualist.xml +++ b/1.6/Anomaly/Defs/EventDefs/EventDef_WULA_FE_Spiritualist.xml @@ -27,6 +27,7 @@
  • WULA_FE_Spiritualist_Influence 0 + Int
  • @@ -105,6 +106,7 @@
  • WULA_FE_Spiritualist_Influence 0 + Int
  • @@ -170,6 +172,7 @@
  • WULA_FE_Spiritualist_Influence 0 + Int
  • @@ -248,6 +251,7 @@
  • WULA_FE_Spiritualist_Influence 0 + Int
  • diff --git a/Source/WulaFallenEmpire/EventSystem/Condition.cs b/Source/WulaFallenEmpire/EventSystem/Condition.cs index 68127d30..4b8325da 100644 --- a/Source/WulaFallenEmpire/EventSystem/Condition.cs +++ b/Source/WulaFallenEmpire/EventSystem/Condition.cs @@ -19,26 +19,53 @@ namespace WulaFallenEmpire var eventVarManager = Find.World.GetComponent(); if (!eventVarManager.HasVariable(name)) { - eventVarManager.SetVariable(name, "0"); + reason = $"Variable '{name}' not found."; + return false; } - - object variable = eventVarManager.GetVariable(name); - string compareValue = value; + object variable = eventVarManager.GetVariable(name); + string compareValueStr = value; + if (!string.IsNullOrEmpty(valueVariableName)) { - compareValue = eventVarManager.GetVariable(valueVariableName)?.ToString(); - if (compareValue == null) + compareValueStr = eventVarManager.GetVariable(valueVariableName)?.ToString(); + if (compareValueStr == null) { reason = $"Comparison variable '{valueVariableName}' not set."; return false; } } - bool met = variable.ToString() == compareValue; + 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} = {compareValue} (Current: {variable})"; + reason = $"Requires {name} = {compareValueStr} (Current: {variable})"; } else { @@ -62,6 +89,7 @@ namespace WulaFallenEmpire var eventVarManager = Find.World.GetComponent(); if (!eventVarManager.HasVariable(name)) { + Log.Message($"[EventSystem] {GetType().Name}: Variable '{name}' not found, defaulting to 0f."); eventVarManager.SetVariable(name, 0f); } @@ -74,11 +102,13 @@ namespace WulaFallenEmpire 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})"; @@ -126,26 +156,54 @@ namespace WulaFallenEmpire var eventVarManager = Find.World.GetComponent(); if (!eventVarManager.HasVariable(name)) { - eventVarManager.SetVariable(name, "0"); + reason = $"Variable '{name}' not found."; + return false; } object variable = eventVarManager.GetVariable(name); + string compareValueStr = value; - string compareValue = value; if (!string.IsNullOrEmpty(valueVariableName)) { - compareValue = eventVarManager.GetVariable(valueVariableName)?.ToString(); - if (compareValue == null) + compareValueStr = eventVarManager.GetVariable(valueVariableName)?.ToString(); + if (compareValueStr == null) { reason = $"Comparison variable '{valueVariableName}' not set."; return false; } } - bool met = variable.ToString() != compareValue; + 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} != {compareValue} (Current: {variable})"; + reason = $"Requires {name} != {compareValueStr} (Current: {variable})"; } else { diff --git a/Source/WulaFallenEmpire/EventSystem/DebugActions.cs b/Source/WulaFallenEmpire/EventSystem/DebugActions.cs index 5f83cc69..ed5e6e15 100644 --- a/Source/WulaFallenEmpire/EventSystem/DebugActions.cs +++ b/Source/WulaFallenEmpire/EventSystem/DebugActions.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; +using LudeonTK; using Verse; using RimWorld; -using LudeonTK; namespace WulaFallenEmpire { @@ -13,7 +13,6 @@ namespace WulaFallenEmpire List list = new List(); foreach (EventDef localDef in DefDatabase.AllDefs) { - // Capture the local variable for the lambda EventDef currentDef = localDef; list.Add(new DebugMenuOption(currentDef.defName, DebugMenuOptionMode.Action, delegate { @@ -23,4 +22,13 @@ namespace WulaFallenEmpire Find.WindowStack.Add(new Dialog_DebugOptionListLister(list)); } } -} + + public static class WulaDebugActionsVariables + { + [DebugAction("Wula Fallen Empire", "Manage Event Variables", actionType = DebugActionType.Action, allowedGameStates = AllowedGameStates.PlayingOnMap)] + private static void ManageEventVariables() + { + Find.WindowStack.Add(new Dialog_ManageEventVariables()); + } + } +} \ No newline at end of file diff --git a/Source/WulaFallenEmpire/EventSystem/Dialog_ManageEventVariables.cs b/Source/WulaFallenEmpire/EventSystem/Dialog_ManageEventVariables.cs new file mode 100644 index 00000000..cc546d44 --- /dev/null +++ b/Source/WulaFallenEmpire/EventSystem/Dialog_ManageEventVariables.cs @@ -0,0 +1,97 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using Verse; + +namespace WulaFallenEmpire +{ + public class Dialog_ManageEventVariables : Window + { + private Vector2 scrollPosition; + private Dictionary editBuffers = new Dictionary(); + private EventVariableManager manager; + + public override Vector2 InitialSize => new Vector2(800f, 600f); + + public Dialog_ManageEventVariables() + { + forcePause = true; + doCloseX = true; + doCloseButton = true; + closeOnClickedOutside = true; + absorbInputAroundWindow = true; + manager = Find.World.GetComponent(); + RefreshBuffers(); + } + + private void RefreshBuffers() + { + editBuffers.Clear(); + foreach (var kvp in manager.GetAllVariables()) + { + editBuffers[kvp.Key] = kvp.Value?.ToString() ?? ""; + } + } + + public override void DoWindowContents(Rect inRect) + { + Listing_Standard listing = new Listing_Standard(); + listing.Begin(inRect); + + if (listing.ButtonText("Refresh")) + { + RefreshBuffers(); + } + if (listing.ButtonText("Clear All Variables")) + { + manager.ClearAll(); + RefreshBuffers(); + } + + listing.GapLine(); + + Rect viewRect = new Rect(0f, 0f, inRect.width - 16f, manager.GetAllVariables().Count * 32f); + Widgets.BeginScrollView(listing.GetRect(inRect.height - 100f), ref scrollPosition, viewRect); + + Listing_Standard varListing = new Listing_Standard(); + varListing.Begin(viewRect); + + var allVars = manager.GetAllVariables().OrderBy(kvp => kvp.Key).ToList(); + + foreach (var kvp in allVars) + { + Rect rowRect = varListing.GetRect(30f); + string key = kvp.Key; + object value = kvp.Value; + string typeName = value?.GetType().Name ?? "null"; + + Widgets.Label(rowRect.LeftPart(0.4f).Rounded(), $"{key} ({typeName})"); + + string buffer = editBuffers[key]; + string newValue = Widgets.TextField(rowRect.RightPart(0.6f).LeftPart(0.8f).Rounded(), buffer); + editBuffers[key] = newValue; + + if (Widgets.ButtonText(rowRect.RightPart(0.1f).Rounded(), "Set")) + { + // Attempt to parse and set the variable + if (value is int) + { + if (int.TryParse(newValue, out int intVal)) manager.SetVariable(key, intVal); + } + else if (value is float) + { + if (float.TryParse(newValue, out float floatVal)) manager.SetVariable(key, floatVal); + } + else + { + manager.SetVariable(key, newValue); + } + } + } + + varListing.End(); + Widgets.EndScrollView(); + listing.End(); + } + } +} \ No newline at end of file diff --git a/Source/WulaFallenEmpire/EventSystem/Effect.cs b/Source/WulaFallenEmpire/EventSystem/Effect.cs index 119627d9..a1bdb56e 100644 --- a/Source/WulaFallenEmpire/EventSystem/Effect.cs +++ b/Source/WulaFallenEmpire/EventSystem/Effect.cs @@ -163,25 +163,34 @@ namespace WulaFallenEmpire { public string name; public string value; + public string type; // Int, Float, String, Bool + public bool forceSet = false; public override void Execute(Dialog_CustomDisplay dialog = null) { var eventVarManager = Find.World.GetComponent(); - if (!eventVarManager.HasVariable(name)) + if (!forceSet && eventVarManager.HasVariable(name)) { - if (int.TryParse(value, out int intValue)) + return; + } + + object realValue = value; + if (!string.IsNullOrEmpty(type)) + { + if (type.Equals("int", System.StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out int intVal)) { - eventVarManager.SetVariable(name, intValue); + realValue = intVal; } - else if (float.TryParse(value, out float floatValue)) + else if (type.Equals("float", System.StringComparison.OrdinalIgnoreCase) && float.TryParse(value, out float floatVal)) { - eventVarManager.SetVariable(name, floatValue); + realValue = floatVal; } - else + else if (type.Equals("bool", System.StringComparison.OrdinalIgnoreCase) && bool.TryParse(value, out bool boolVal)) { - eventVarManager.SetVariable(name, value); + realValue = boolVal; } } + eventVarManager.SetVariable(name, realValue); } } @@ -337,14 +346,14 @@ namespace WulaFallenEmpire Add, Subtract, Multiply, - Divide, - Set + Divide } public class Effect_ModifyVariable : Effect { public string name; - public float value; + public string value; + public string valueVariableName; public VariableOperation operation; public override void Execute(Dialog_CustomDisplay dialog = null) @@ -354,43 +363,70 @@ namespace WulaFallenEmpire Log.Error("[WulaFallenEmpire] Effect_ModifyVariable has a null or empty name."); return; } - + var eventVarManager = Find.World.GetComponent(); - if (!eventVarManager.HasVariable(name)) + // Determine the value to modify by + string valueStr = value; + if (!string.IsNullOrEmpty(valueVariableName)) { - eventVarManager.SetVariable(name, 0f); + valueStr = eventVarManager.GetVariable(valueVariableName)?.ToString(); + if (valueStr == null) + { + Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: valueVariableName '{valueVariableName}' not found."); + return; + } } - - float currentValue = eventVarManager.GetVariable(name); - switch (operation) + // Get the target variable, or initialize it + object variable = eventVarManager.GetVariable(name); + if (variable == null) { - case VariableOperation.Add: - currentValue += value; - break; - case VariableOperation.Subtract: - currentValue -= value; - break; - case VariableOperation.Multiply: - currentValue *= value; - break; - case VariableOperation.Divide: - if (value != 0) - { - currentValue /= value; - } - else - { - Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero for variable '{name}'."); - } - break; - case VariableOperation.Set: - currentValue = value; - break; - } + Log.Message($"[EventSystem] Effect_ModifyVariable: Variable '{name}' not found, initializing to 0."); + variable = 0; + } - eventVarManager.SetVariable(name, currentValue); + object originalValue = variable; + object newValue = null; + + // Perform operation based on type + try + { + if (variable is int || (variable is float && !valueStr.Contains("."))) // Allow int ops + { + int currentVal = System.Convert.ToInt32(variable); + int modVal = int.Parse(valueStr); + newValue = (int)Modify((float)currentVal, (float)modVal, operation); + } + else // Default to float operation + { + float currentVal = System.Convert.ToSingle(variable); + float modVal = float.Parse(valueStr); + newValue = Modify(currentVal, modVal, operation); + } + + Log.Message($"[EventSystem] Modifying variable '{name}'. Operation: {operation}. Value: {valueStr}. From: {originalValue} To: {newValue}"); + eventVarManager.SetVariable(name, newValue); + } + catch (System.Exception e) + { + Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: Could not parse or operate on value '{valueStr}' for variable '{name}'. Error: {e.Message}"); + } + } + + private float Modify(float current, float modifier, VariableOperation op) + { + switch (op) + { + case VariableOperation.Add: return current + modifier; + case VariableOperation.Subtract: return current - modifier; + case VariableOperation.Multiply: return current * modifier; + case VariableOperation.Divide: + if (modifier != 0) return current / modifier; + Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero."); + return current; + default: return current; + } } } @@ -556,4 +592,33 @@ namespace WulaFallenEmpire } } } +public class Effect_StoreFactionGoodwill : Effect + { + public FactionDef factionDef; + public string variableName; + + public override void Execute(Dialog_CustomDisplay dialog = null) + { + if (factionDef == null || string.IsNullOrEmpty(variableName)) + { + Log.Error("[WulaFallenEmpire] Effect_StoreFactionGoodwill is not configured correctly."); + return; + } + + var eventVarManager = Find.World.GetComponent(); + Faction faction = Find.FactionManager.FirstFactionOfDef(factionDef); + + if (faction != null) + { + int goodwill = faction.GoodwillWith(Faction.OfPlayer); + Log.Message($"[EventSystem] Storing goodwill for faction '{faction.Name}' ({goodwill}) into variable '{variableName}'."); + eventVarManager.SetVariable(variableName, goodwill); + } + else + { + Log.Warning($"[EventSystem] Effect_StoreFactionGoodwill: Faction '{factionDef.defName}' not found. Storing 0 in variable '{variableName}'."); + eventVarManager.SetVariable(variableName, 0); + } + } + } } diff --git a/Source/WulaFallenEmpire/EventSystem/EventVariableManager.cs b/Source/WulaFallenEmpire/EventSystem/EventVariableManager.cs index 868535cb..de84903a 100644 --- a/Source/WulaFallenEmpire/EventSystem/EventVariableManager.cs +++ b/Source/WulaFallenEmpire/EventSystem/EventVariableManager.cs @@ -47,7 +47,10 @@ namespace WulaFallenEmpire public void SetVariable(string name, object value) { if (string.IsNullOrEmpty(name)) return; - + + // Log the variable change + Log.Message($"[EventSystem] Setting variable '{name}' to value '{value}' of type {value?.GetType().Name ?? "null"}."); + // Clear any existing variable with the same name to prevent type confusion ClearVariable(name); @@ -83,48 +86,45 @@ namespace WulaFallenEmpire if (string.IsNullOrEmpty(name)) return defaultValue; object value = null; - bool found = false; - - if (typeof(T) == typeof(List) && pawnListVars.TryGetValue(name, out var pawnListVal)) + if (pawnListVars.TryGetValue(name, out var pawnListVal)) { value = pawnListVal; - found = true; } - else if (typeof(T) == typeof(Pawn) && pawnVars.TryGetValue(name, out var pawnVal)) + else if (pawnVars.TryGetValue(name, out var pawnVal)) { value = pawnVal; - found = true; } - else if (typeof(T) == typeof(float) && floatVars.TryGetValue(name, out var floatVal)) + else if (floatVars.TryGetValue(name, out var floatVal)) { value = floatVal; - found = true; } - else if (typeof(T) == typeof(int) && intVars.TryGetValue(name, out var intVal)) + else if (intVars.TryGetValue(name, out var intVal)) { value = intVal; - found = true; } else if (stringVars.TryGetValue(name, out var stringVal)) { value = stringVal; - found = true; } - if (found) + if (value != null) { if (value is T typedValue) { return typedValue; } - try { + // Handle cases where T is object but the stored value is, e.g., an int + if (typeof(T) == typeof(object)) + { + return (T)value; + } return (T)System.Convert.ChangeType(value, typeof(T)); } - catch (System.Exception) + catch (System.Exception e) { - Log.Warning($"[WulaFallenEmpire] EventVariableManager: Variable '{name}' of type {value.GetType()} could not be converted to {typeof(T)}."); + Log.Warning($"[WulaFallenEmpire] EventVariableManager: Variable '{name}' of type {value.GetType()} could not be converted to {typeof(T)}. Error: {e.Message}"); return defaultValue; } } @@ -143,6 +143,10 @@ namespace WulaFallenEmpire public void ClearVariable(string name) { + if (HasVariable(name)) + { + Log.Message($"[EventSystem] Clearing variable '{name}'."); + } intVars.Remove(name); floatVars.Remove(name); stringVars.Remove(name); @@ -158,5 +162,16 @@ namespace WulaFallenEmpire pawnVars.Clear(); pawnListVars.Clear(); } + + public Dictionary GetAllVariables() + { + var allVars = new Dictionary(); + foreach (var kvp in intVars) allVars[kvp.Key] = kvp.Value; + foreach (var kvp in floatVars) allVars[kvp.Key] = kvp.Value; + foreach (var kvp in stringVars) allVars[kvp.Key] = kvp.Value; + foreach (var kvp in pawnVars) allVars[kvp.Key] = kvp.Value; + foreach (var kvp in pawnListVars) allVars[kvp.Key] = kvp.Value; + return allVars; + } } } \ No newline at end of file diff --git a/Source/WulaFallenEmpire/WulaFallenEmpire.csproj b/Source/WulaFallenEmpire/WulaFallenEmpire.csproj index ba1d8ec9..489a0453 100644 --- a/Source/WulaFallenEmpire/WulaFallenEmpire.csproj +++ b/Source/WulaFallenEmpire/WulaFallenEmpire.csproj @@ -83,6 +83,7 @@ +