From 3804469b3afc9452761587cd60d4a209a6cad4fa Mon Sep 17 00:00:00 2001 From: amevarashi Date: Mon, 1 Aug 2022 19:21:13 +0500 Subject: [PATCH] Optimized Thought_Opinionbased --- .../ThoughtDefExtension_StageFromOpinion.cs | 21 +++++++ .../Thoughts/Thought_Opinionbased.cs | 58 ++++++++++++++----- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/Source/IdeologyAddon/Thoughts/ThoughtDefExtension_StageFromOpinion.cs b/Source/IdeologyAddon/Thoughts/ThoughtDefExtension_StageFromOpinion.cs index 30252c9..3b6bfc4 100644 --- a/Source/IdeologyAddon/Thoughts/ThoughtDefExtension_StageFromOpinion.cs +++ b/Source/IdeologyAddon/Thoughts/ThoughtDefExtension_StageFromOpinion.cs @@ -8,5 +8,26 @@ namespace RJWSexperience { [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] public List minimumValueforStage = new List(); + + public override IEnumerable ConfigErrors() + { + foreach (string error in base.ConfigErrors()) + { + yield return error; + } + + if (minimumValueforStage.NullOrEmpty()) + { + yield return " should have an entry for every stage"; + } + + for (int i = 0; i < minimumValueforStage.Count - 1; i++) + { + if (minimumValueforStage[i] > minimumValueforStage[i + 1]) + { + yield return "Values in should be ordered from the lowest to the highest"; + } + } + } } } diff --git a/Source/IdeologyAddon/Thoughts/Thought_Opinionbased.cs b/Source/IdeologyAddon/Thoughts/Thought_Opinionbased.cs index f1a7d01..fded6ad 100644 --- a/Source/IdeologyAddon/Thoughts/Thought_Opinionbased.cs +++ b/Source/IdeologyAddon/Thoughts/Thought_Opinionbased.cs @@ -1,36 +1,62 @@ using RimWorld; using System.Collections.Generic; +using Verse; namespace RJWSexperience { public class Thought_Opinionbased : Thought_Memory { - private ThoughtDefExtension_StageFromOpinion extension; + private List minimumValueforStage; - protected ThoughtDefExtension_StageFromOpinion Extension + protected List MinimumValueforStage { get { - if (extension == null) - extension = def.GetModExtension(); - return extension; + if (minimumValueforStage == null) + { + minimumValueforStage = def.GetModExtension().minimumValueforStage; + } + return minimumValueforStage; } } - protected List MinimumValueforStage => Extension.minimumValueforStage; - - public override int CurStageIndex + /// + /// This method is called for every thought right after the pawn is assigned + /// + public override bool TryMergeWithExistingMemory(out bool showBubble) { - get + UpdateCurStage(); + return base.TryMergeWithExistingMemory(out showBubble); + } + + /// + /// Called every 150 ticks + /// + public override void ThoughtInterval() + { + UpdateCurStage(); + base.ThoughtInterval(); + } + + protected void UpdateCurStage() + { + if (otherPawn == null) { - float value = 0f; - if (otherPawn != null) value = pawn.relations?.OpinionOf(otherPawn) ?? 0f; - for (int i = MinimumValueforStage.Count - 1; i > 0; i--) - { - if (MinimumValueforStage[i] < value) return i; - } - return 0; + Log.Warning($"[RSI] Thought_Opinionbased {def.defName} for pawn {pawn.NameShortColored} lacks otherPawn"); + SetForcedStage(0); } + + float value = pawn.relations?.OpinionOf(otherPawn) ?? 0f; + + for (int i = MinimumValueforStage.Count - 1; i > 0; i--) + { + if (MinimumValueforStage[i] < value) + { + SetForcedStage(i); + return; + } + } + SetForcedStage(0); } } }