using System.Collections.Generic; using System.Linq; using Verse; namespace RJW_Menstruation { public class Hediff_Estrus : HediffWithComps { const int checkInterval = GenTicks.TickRareInterval; private bool shouldRemove = false; public override bool ShouldRemove { get { return shouldRemove || base.ShouldRemove; } } protected bool IsVisible { get { return def == VariousDefOf.Hediff_Estrus; } } public override void PostAdd(DamageInfo? dinfo) { base.PostAdd(dinfo); if (IsVisible) { List removals = pawn.health.hediffSet.hediffs.Where(hediff => hediff.def == VariousDefOf.Hediff_Estrus_Concealed).ToList(); foreach (Hediff concealedEstrus in removals) { pawn.health.RemoveHediff(concealedEstrus); } } } public override void PostTick() { base.PostTick(); if (!pawn.IsHashIntervalTick(checkInterval)) return; if (IsVisible) { switch (pawn.HighestEstrus()) { case HediffComp_Menstruation.EstrusLevel.None: shouldRemove = true; break; case HediffComp_Menstruation.EstrusLevel.Concealed: shouldRemove = true; pawn.health.AddHediff(VariousDefOf.Hediff_Estrus_Concealed); break; case HediffComp_Menstruation.EstrusLevel.Visible: break; } } else { // Adding a visible will remove this one, so we don't have to check it here if (pawn.HighestEstrus() == HediffComp_Menstruation.EstrusLevel.None) shouldRemove = true; } } public override bool TryMergeWith(Hediff other) { if (!(other is Hediff_Estrus otherEstrus)) return false; if (this.def == otherEstrus.def) return true; else if (!this.IsVisible && otherEstrus.IsVisible && !otherEstrus.shouldRemove) return true; // A concealed estrus won't be added if there's a visible // This is a visible estrus overwriting a concealed. else return false; // Since this is being added while hediffs are being looped through, it's not safe to remove the concealed yet. Do it in PostAdd. } } }