Optimized Thought_Opinionbased

This commit is contained in:
amevarashi 2022-08-01 19:21:13 +05:00
parent 6767feb57c
commit 3804469b3a
2 changed files with 63 additions and 16 deletions

View File

@ -8,5 +8,26 @@ namespace RJWSexperience
{ {
[SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")] [SuppressMessage("Minor Code Smell", "S1104:Fields should not have public accessibility", Justification = "Field value loaded from XML")]
public List<float> minimumValueforStage = new List<float>(); public List<float> minimumValueforStage = new List<float>();
public override IEnumerable<string> ConfigErrors()
{
foreach (string error in base.ConfigErrors())
{
yield return error;
}
if (minimumValueforStage.NullOrEmpty())
{
yield return "<minimumValueforStage> 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 <minimumValueforStage> should be ordered from the lowest to the highest";
}
}
}
} }
} }

View File

@ -1,36 +1,62 @@
using RimWorld; using RimWorld;
using System.Collections.Generic; using System.Collections.Generic;
using Verse;
namespace RJWSexperience namespace RJWSexperience
{ {
public class Thought_Opinionbased : Thought_Memory public class Thought_Opinionbased : Thought_Memory
{ {
private ThoughtDefExtension_StageFromOpinion extension; private List<float> minimumValueforStage;
protected ThoughtDefExtension_StageFromOpinion Extension protected List<float> MinimumValueforStage
{ {
get get
{ {
if (extension == null) if (minimumValueforStage == null)
extension = def.GetModExtension<ThoughtDefExtension_StageFromOpinion>(); {
return extension; minimumValueforStage = def.GetModExtension<ThoughtDefExtension_StageFromOpinion>().minimumValueforStage;
}
return minimumValueforStage;
} }
} }
protected List<float> MinimumValueforStage => Extension.minimumValueforStage; /// <summary>
/// This method is called for every thought right after the pawn is assigned
public override int CurStageIndex /// </summary>
public override bool TryMergeWithExistingMemory(out bool showBubble)
{ {
get UpdateCurStage();
return base.TryMergeWithExistingMemory(out showBubble);
}
/// <summary>
/// Called every 150 ticks
/// </summary>
public override void ThoughtInterval()
{
UpdateCurStage();
base.ThoughtInterval();
}
protected void UpdateCurStage()
{
if (otherPawn == null)
{ {
float value = 0f; Log.Warning($"[RSI] Thought_Opinionbased {def.defName} for pawn {pawn.NameShortColored} lacks otherPawn");
if (otherPawn != null) value = pawn.relations?.OpinionOf(otherPawn) ?? 0f; SetForcedStage(0);
for (int i = MinimumValueforStage.Count - 1; i > 0; i--)
{
if (MinimumValueforStage[i] < value) return i;
}
return 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);
} }
} }
} }