Have terminate pregnancy operation work on multiple pregnancies. Only remove the one that is the least far along.

This commit is contained in:
lutepickle 2022-11-21 22:21:38 -08:00
parent c6fbab5974
commit fe20e9d78b
4 changed files with 59 additions and 0 deletions

Binary file not shown.

View file

@ -3,6 +3,7 @@ using System.Linq;
using RimWorld; using RimWorld;
using Verse; using Verse;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
namespace RJW_Menstruation namespace RJW_Menstruation
{ {
@ -108,4 +109,61 @@ namespace RJW_Menstruation
comp.TakeLoosePregnancy(); comp.TakeLoosePregnancy();
} }
} }
[HarmonyPatch(typeof(PregnancyUtility), nameof(PregnancyUtility.TryTerminatePregnancy))]
public class TryTerminatePregnancy_Patch
{
private static Hediff GetEarliestPregnancy(Pawn pawn)
{
Hediff Earliest_Pregnancy = PregnancyUtility.GetPregnancyHediff(pawn);
foreach (HediffComp_Menstruation comp in pawn.GetMenstruationComps())
{
Hediff pregnancy = comp.Pregnancy;
if (pregnancy == null) continue;
if (Earliest_Pregnancy == null || Earliest_Pregnancy.Severity > pregnancy.Severity) Earliest_Pregnancy = pregnancy;
}
return Earliest_Pregnancy;
}
private static readonly MethodInfo GetPregnancyHediff = AccessTools.Method(typeof(PregnancyUtility), nameof(PregnancyUtility.GetPregnancyHediff), new System.Type[] { typeof(Pawn) });
// Also called for Recipe_TerminatePregnancy.ApplyOnPawn
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
if (GetPregnancyHediff == null) throw new System.InvalidOperationException("GetPregnancyHediff not found");
foreach (CodeInstruction instruction in instructions)
{
if (instruction.Calls(GetPregnancyHediff))
yield return CodeInstruction.Call(typeof(TryTerminatePregnancy_Patch), nameof(TryTerminatePregnancy_Patch.GetEarliestPregnancy));
else yield return instruction;
}
}
public static void Postfix(bool __result, Pawn pawn)
{
if (__result)
foreach (HediffComp_Menstruation comp in pawn.GetMenstruationComps())
_ = comp.Pregnancy; // get_Pregnancy will remove the hediff attached to the comp that doesn't have it anymore
}
}
[HarmonyPatch(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.AvailableOnNow))]
public class TerminatePregnancy_AvailableOnNow_Patch
{
public static void Postfix(ref bool __result, Thing thing)
{
if (!ModsConfig.BiotechActive || !(thing is Pawn pawn)) return;
__result |= pawn.GetMenstruationComps().Any(comp => comp.Pregnancy != null);
}
}
[HarmonyPatch(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.ApplyOnPawn))]
public class TerminatePregnancy_ApplyOnPawn_Patch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return TryTerminatePregnancy_Patch.Transpiler(instructions);
}
}
} }

View file

@ -1,5 +1,6 @@
Version 1.0.8.4 Version 1.0.8.4
- Newborns should now be baseliners if there are no xenotypes to inherit. - Newborns should now be baseliners if there are no xenotypes to inherit.
- The Biotech terminate pregnancy recipe can now terminate a menstruation pregnancy, too.
Version 1.0.8.3 Version 1.0.8.3
- Compatibility update for RJW 5.3.0.9 - Compatibility update for RJW 5.3.0.9