From ad3c18517e808cff1d30d95bd290f1fee8639983 Mon Sep 17 00:00:00 2001 From: Joshua Thome Date: Sun, 16 Feb 2025 16:45:17 -0600 Subject: [PATCH] Helper methods to calculate and apply checks --- NewHorizons/Handlers/ConditionalsHandler.cs | 103 ++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 NewHorizons/Handlers/ConditionalsHandler.cs diff --git a/NewHorizons/Handlers/ConditionalsHandler.cs b/NewHorizons/Handlers/ConditionalsHandler.cs new file mode 100644 index 00000000..a53263d4 --- /dev/null +++ b/NewHorizons/Handlers/ConditionalsHandler.cs @@ -0,0 +1,103 @@ +using NewHorizons.External.Modules.Conditionals; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NewHorizons.Handlers +{ + public static class ConditionalsHandler + { + public static bool Check(ConditionalCheckConditionsInfo check) + { + var dcm = DialogueConditionManager.SharedInstance; + + var passed = true; + if (check.allConditionsSet != null && check.allConditionsSet.Length > 0) + { + passed = passed && check.allConditionsSet.All(c => dcm.GetConditionState(c)); + } + if (check.anyConditionsSet != null && check.anyConditionsSet.Length > 0) + { + passed = passed && check.anyConditionsSet.Any(c => dcm.GetConditionState(c)); + } + if (check.allPersistentConditionsSet != null && check.allPersistentConditionsSet.Length > 0) + { + passed = passed && check.allPersistentConditionsSet.All(c => PlayerData.GetPersistentCondition(c)); + } + if (check.anyPersistentConditionsSet != null && check.anyPersistentConditionsSet.Length > 0) + { + passed = passed && check.anyPersistentConditionsSet.Any(c => PlayerData.GetPersistentCondition(c)); + } + if (check.allFactsRevealed != null && check.allFactsRevealed.Length > 0) + { + passed = passed && check.allFactsRevealed.All(f => ShipLogHandler.KnowsFact(f)); + } + if (check.anyFactsRevealed != null && check.anyFactsRevealed.Length > 0) + { + passed = passed && check.anyFactsRevealed.Any(f => ShipLogHandler.KnowsFact(f)); + } + if (check.invert) + { + passed = !passed; + } + + return passed; + } + + public static void ApplyEffects(ConditionalCheckEffectsInfo effects, bool checkPassed) + { + if ((checkPassed || effects.reversible) && effects.setConditions != null) + { + foreach (var condition in effects.setConditions) + { + if (DialogueConditionManager.SharedInstance.GetConditionState(condition) != checkPassed) + { + DialogueConditionManager.SharedInstance.SetConditionState(condition, checkPassed); + } + } + } + else if ((!checkPassed || effects.reversible) && effects.unsetConditions != null) + { + foreach (var condition in effects.unsetConditions) + { + if (DialogueConditionManager.SharedInstance.GetConditionState(condition) != !checkPassed) + { + DialogueConditionManager.SharedInstance.SetConditionState(condition, !checkPassed); + } + } + } + if ((checkPassed || effects.reversible) && effects.setPersistentConditions != null) + { + foreach (var condition in effects.setPersistentConditions) + { + if (!PlayerData.GetPersistentCondition(condition) != checkPassed) + { + PlayerData.SetPersistentCondition(condition, checkPassed); + } + } + } + else if ((!checkPassed || effects.reversible) && effects.unsetPersistentConditions != null) + { + foreach (var condition in effects.unsetPersistentConditions) + { + if (PlayerData.GetPersistentCondition(condition) != !checkPassed) + { + PlayerData.SetPersistentCondition(condition, !checkPassed); + } + } + } + if (checkPassed && effects.revealFacts != null) + { + foreach (var fact in effects.revealFacts) + { + if (!ShipLogHandler.KnowsFact(fact)) + { + Locator.GetShipLogManager().RevealFact(fact); + } + } + } + } + } +}