Replace hugslib scheduler with CompPostTick. Hopefully this will cut down on phantom menstruation bugs.

This commit is contained in:
lutepickle 2022-08-08 08:07:44 -07:00
parent 7dcab74af5
commit 85c479a45b
6 changed files with 88 additions and 161 deletions

Binary file not shown.

View file

@ -1,5 +1,4 @@
using HugsLib;
using RimWorld;
using RimWorld;
using rjw;
using System;
using System.Collections.Generic;
@ -63,8 +62,6 @@ namespace RJW_Menstruation
protected Color cachedColor;
protected bool loaded = false;
public Action action;
protected float BabyHalfAge
{
get
@ -160,7 +157,30 @@ namespace RJW_Menstruation
Scribe_Values.Look(ref baseNipple, "baseNipple", baseNipple, true);
}
public override void CompPostTick(ref float severityAdjustment) { }
public override void CompPostTick(ref float severityAdjustment)
{
base.CompPostTick(ref severityAdjustment);
// If an exception makes it out, RW will remove the hediff, so catch it here
try
{
if (
!parent.pawn.IsHashIntervalTick(tickInterval) ||
!parent.pawn.Spawned || // TODO: Add option to simulate off-map pawns
parent.pawn.health.Dead
)
{
return;
}
CalculateBreastSize();
CalculateNipples();
UpdateNipples();
}
catch (Exception ex)
{
Log.Error($"Error processing breasts of {parent.pawn}: {ex}");
}
}
public override void CompPostPostAdd(DamageInfo? dinfo)
{
@ -175,8 +195,6 @@ namespace RJW_Menstruation
Log.Warning($"Attempted to remove breast comp from wrong pawn ({parent.pawn}).");
return;
}
HugsLibController.Instance.TickDelayScheduler.TryUnscheduleCallback(action);
if (Configurations.Debug) Log.Message(parent.pawn.Label + " breast tick scheduler removed");
base.CompPostPostRemoved();
}
@ -208,7 +226,6 @@ namespace RJW_Menstruation
public void Initialize()
{
Props = (CompProperties_Breast)props;
action = Update;
if (maxBreastIncrement <= 0f)
{
@ -232,15 +249,6 @@ namespace RJW_Menstruation
}
UpdateNipples();
loaded = true;
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(action, GetNextUpdate(), parent.pawn);
}
public void Update()
{
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(action, GetNextUpdate(), parent.pawn);
CalculateBreastSize();
CalculateNipples();
UpdateNipples();
}
protected void CalculateBreastSize()
@ -344,15 +352,6 @@ namespace RJW_Menstruation
cachedColor = Colors.CMYKLerp(parent.pawn.story?.SkinColor ?? Color.white, Props.BlackNippleColor, Alpha);
}
protected int GetNextUpdate()
{
// Just like the menstruation code
int currentOffset = Find.TickManager.TicksGame % tickInterval;
int nextOffset = (parent.pawn.HashOffset() % tickInterval + tickInterval) % tickInterval;
return ((nextOffset - currentOffset + tickInterval - 1) % tickInterval) + 1;
}
public void CopyBreastProperties(HediffComp_Breast original)
{
maxBreastIncrement = original.maxBreastIncrement;

View file

@ -1,5 +1,4 @@
using HugsLib;
using RimWorld;
using RimWorld;
using rjw;
using System;
using System.Collections.Generic;
@ -70,7 +69,6 @@ namespace RJW_Menstruation
public CompProperties_Menstruation Props;
public Stage curStage = Stage.Follicular;
public int curStageHrs = 0;
public Action actionref;
public bool loaded = false;
public int ovarypower = -100000;
public int eggstack = 0;
@ -530,11 +528,68 @@ namespace RJW_Menstruation
public override void CompPostTick(ref float severityAdjustment)
{
//initializer moved to SpawnSetup
//if (!loaded)
//{
// Initialize();
//}
base.CompPostTick(ref severityAdjustment);
// If an exception makes it out, RW will remove the hediff, so catch it here
try
{
if (
!parent.pawn.IsHashIntervalTick(tickInterval) ||
!parent.pawn.Spawned || // TODO: Add option to simulate off-map pawns
parent.pawn.health.Dead ||
(parent.pawn.IsAnimal() && !Configurations.EnableAnimalCycle)
)
{
return;
}
CumOut();
switch (curStage)
{
case Stage.Follicular:
FollicularAction(false);
break;
case Stage.Ovulatory:
OvulatoryAction();
break;
case Stage.Luteal:
LutealAction(false);
break;
case Stage.Bleeding:
BleedingAction(false);
break;
case Stage.Pregnant:
PregnantAction();
break;
case Stage.Recover:
RecoverAction();
break;
case Stage.None:
break;
case Stage.Young:
YoungAction();
break;
case Stage.ClimactericFollicular:
FollicularAction(true);
break;
case Stage.ClimactericLuteal:
LutealAction(true);
break;
case Stage.ClimactericBleeding:
BleedingAction(true);
break;
case Stage.Anestrus:
AnestrusAction();
break;
default:
GoNextStage(Stage.Follicular);
break;
}
if (pregnancy == null && parent.pawn.health.capacities.GetLevel(xxx.reproduction) <= 0) curStage = Stage.Young;
}
catch (Exception ex)
{
Log.Error($"Error processing womb of {parent.pawn}: {ex}");
}
}
public override void CompPostPostRemoved()
@ -546,8 +601,6 @@ namespace RJW_Menstruation
Log.Warning($"Attempted to remove menstruation comp from wrong pawn ({parent.pawn}).");
return;
}
HugsLibController.Instance.TickDelayScheduler.TryUnscheduleCallback(actionref);
if (Configurations.Debug) Log.Message(parent.pawn.Label + " menstruation tick scheduler removed");
pregnancy?.Miscarry();
base.CompPostPostRemoved();
}
@ -871,7 +924,6 @@ namespace RJW_Menstruation
{
if (cums == null) cums = new List<Cum>();
curStage = Stage.None;
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(curStage), GetNextUpdate(), parent.pawn, false);
loaded = true;
return;
}
@ -912,18 +964,6 @@ namespace RJW_Menstruation
}
}
if (parent.pawn.IsAnimal())
{
if (Configurations.EnableAnimalCycle)
{
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(curStage), GetNextUpdate(), parent.pawn, false);
}
}
else
{
if (pregnancy == null && parent.pawn.health.capacities.GetLevel(xxx.reproduction) <= 0) HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(Stage.Young), GetNextUpdate(), parent.pawn, false);
else HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(curStage), GetNextUpdate(), parent.pawn, false);
}
//Log.Message(parent.pawn.Label + " - Initialized menstruation comp");
loaded = true;
}
@ -1576,108 +1616,12 @@ namespace RJW_Menstruation
TaleRecorder.RecordTale(VariousDefOf.TaleCameInside, new object[] { cummer, parent.pawn });
}
private Action PeriodSimulator(Stage targetstage)
{
Action action = null;
switch (targetstage)
{
case Stage.Follicular:
action = delegate
{
FollicularAction(false);
};
break;
case Stage.Ovulatory:
action = OvulatoryAction;
break;
case Stage.Luteal:
action = delegate
{
LutealAction(false);
};
break;
case Stage.Bleeding:
action = delegate
{
BleedingAction(false);
};
break;
case Stage.Pregnant:
action = PregnantAction;
break;
case Stage.Recover:
action = RecoverAction;
break;
case Stage.None:
action = delegate
{
StayCurrentStageConst(Stage.None);
};
break;
case Stage.Young:
action = YoungAction;
break;
case Stage.ClimactericFollicular:
action = delegate
{
FollicularAction(true);
};
break;
case Stage.ClimactericLuteal:
action = delegate
{
LutealAction(true);
};
break;
case Stage.ClimactericBleeding:
action = delegate
{
BleedingAction(true);
};
break;
case Stage.Anestrus:
action = AnestrusAction;
break;
default:
curStage = Stage.Follicular;
curStageHrs = 0;
if (currentIntervalHours < 0) currentIntervalHours = PeriodRandomizer(curStage);
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(Stage.Follicular), GetNextUpdate(), parent.pawn, false);
break;
}
action += delegate
{
if (parent.pawn.health.capacities.GetLevel(xxx.reproduction) <= 0) curStage = Stage.Young;
//CumOut();
AfterSimulator();
};
action = CumOut + action;
actionref = action;
return actionref;
}
protected int GetNextUpdate()
{
// Ticks past the hour. Will be equal except for game start or load
int currentOffset = Find.TickManager.TicksGame % tickInterval;
int nextOffset = (parent.pawn.HashOffset() % tickInterval + tickInterval) % tickInterval; // Messy, but HashOffset is negative a lot
// The -1/+1 to ensure that equality works out to 1 hour and not 0 ticks
return ((nextOffset - currentOffset + tickInterval - 1) % tickInterval) + 1;
}
protected void GoNextStage(Stage nextstage, bool calculateHours = true)
{
curStageHrs = 0;
float variabilityFactor = nextstage == Stage.ClimactericFollicular || nextstage == Stage.ClimactericLuteal || nextstage == Stage.ClimactericBleeding ? 6.0f : 1.0f;
if (calculateHours) currentIntervalHours = PeriodRandomizer(nextstage, variabilityFactor);
curStage = nextstage;
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(nextstage), GetNextUpdate(), parent.pawn, false);
}
protected virtual void GoOvulatoryStage(bool climacteric)
@ -1688,13 +1632,11 @@ namespace RJW_Menstruation
//stage can be interrupted in other reasons
protected void StayCurrentStage()
{
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(curStage), GetNextUpdate(), parent.pawn, false);
}
//stage never changes
protected void StayCurrentStageConst(Stage curstage)
{
HugsLibController.Instance.TickDelayScheduler.ScheduleCallback(PeriodSimulator(curstage), GetNextUpdate(), parent.pawn, false);
}
protected void GoFollicularOrBleeding()

View file

@ -1,5 +1,4 @@
using HarmonyLib;
using HugsLib;
using RimWorld;
using System.Collections.Generic;
using UnityEngine;
@ -16,14 +15,12 @@ namespace RJW_Menstruation
//Log.Message("Initialize on spawnsetup");
foreach (HediffComp_Menstruation comp in __instance.GetMenstruationComps())
{
HugsLibController.Instance.TickDelayScheduler.TryUnscheduleCallback(comp.actionref);
comp.Initialize();
}
HediffComp_Breast bcomp = __instance.GetBreastComp();
if (bcomp != null)
{
HugsLibController.Instance.TickDelayScheduler.TryUnscheduleCallback(bcomp.action);
bcomp.Initialize();
}
}

View file

@ -93,10 +93,6 @@
<HintPath>..\..\..\..\..\..\RimWorldWin64_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="HugsLib">
<HintPath>..\..\..\..\..\..\..\..\workshop\content\294100\818773962\v1.3\Assemblies\HugsLib.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="RJW">
<HintPath>..\..\..\..\..\rjw\1.3\Assemblies\RJW.dll</HintPath>
<Private>False</Private>

View file

@ -13,12 +13,6 @@
<steamWorkshopUrl>steam://url/CommunityFilePage/2009463077</steamWorkshopUrl>
<downloadUrl>https://github.com/pardeike/HarmonyRimWorld/releases/latest</downloadUrl>
</li>
<li>
<packageId>UnlimitedHugs.HugsLib</packageId>
<displayName>HugsLib</displayName>
<downloadUrl>https://github.com/UnlimitedHugs/RimworldHugsLib/releases/latest</downloadUrl>
<steamWorkshopUrl>steam://url/CommunityFilePage/818773962</steamWorkshopUrl>
</li>
<li>
<packageId>rim.job.world</packageId>
<displayName>RimJobWorld</displayName>
@ -26,7 +20,6 @@
</li>
</modDependencies>
<loadAfter>
<li>UnlimitedHugs.HugsLib</li>
<li>brrainz.harmony</li>
<li>rim.job.world</li>
<li>Abraxas.RJW.RaceSupport</li>