From ebf7efb730bddc9aed825c28d1df8210e6ccf25a Mon Sep 17 00:00:00 2001 From: Vegapnk Date: Fri, 5 Jul 2024 16:03:42 +0200 Subject: [PATCH] Draft for Pregnancy Overwrite --- Common/Defs/GeneDefs/GeneDefs_Breeding.xml | 12 +++ Source/GeneDefOf.cs | 1 + .../Patches/Patch_PregnancyOverwrite.cs | 80 +++++++++++++++++++ Source/Rjw-Genes.csproj | 2 + 4 files changed, 95 insertions(+) create mode 100644 Source/Genes/Special/Patches/Patch_PregnancyOverwrite.cs diff --git a/Common/Defs/GeneDefs/GeneDefs_Breeding.xml b/Common/Defs/GeneDefs/GeneDefs_Breeding.xml index b3a9844..832621a 100644 --- a/Common/Defs/GeneDefs/GeneDefs_Breeding.xml +++ b/Common/Defs/GeneDefs/GeneDefs_Breeding.xml @@ -152,4 +152,16 @@ 1 0 + + + rjw_genes_pregnancy_overwrite + + Carriers of this gene can 'overwrite' an existing pregnancy, keeping the progress but effectively replacing the father. + Genes/Icons/RJW_Genes_PheromoneSpit + 75 + + 4 + -2 + + \ No newline at end of file diff --git a/Source/GeneDefOf.cs b/Source/GeneDefOf.cs index 873df6f..f4a2d08 100644 --- a/Source/GeneDefOf.cs +++ b/Source/GeneDefOf.cs @@ -107,6 +107,7 @@ namespace RJW_Genes public static readonly GeneDef rjw_genes_sex_tamer; public static readonly GeneDef rjw_genes_sexual_genetic_swap; public static readonly GeneDef rjw_genes_sexual_genetic_thief; + public static readonly GeneDef rjw_genes_pregnancy_overwrite; // Cosmetic public static readonly GeneDef rjw_genes_succubus_tail; diff --git a/Source/Genes/Special/Patches/Patch_PregnancyOverwrite.cs b/Source/Genes/Special/Patches/Patch_PregnancyOverwrite.cs new file mode 100644 index 0000000..b7b35a6 --- /dev/null +++ b/Source/Genes/Special/Patches/Patch_PregnancyOverwrite.cs @@ -0,0 +1,80 @@ +using HarmonyLib; +using RimWorld; +using rjw; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; +using static HarmonyLib.Code; + +namespace RJW_Genes +{ + [HarmonyPatch(typeof(SexUtility), "Aftersex")] + public class Patch_PregnancyOverwrite + { + public const int FACTION_GOODWILL_CHANGE = -5; + + public static void Postfix(SexProps props) + { + if (props == null || props.pawn == null || props.partner == null || props.partner.IsAnimal()) + { + return; + } + + Pawn pawn = props.pawn; + Pawn partner = props.partner; + + if (pawn.genes == null || partner.genes == null) return; + + // If both have the swap gene, nothing happens + if (GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_pregnancy_overwrite) + && GeneUtility.HasGeneNullCheck(partner, GeneDefOf.rjw_genes_pregnancy_overwrite)) + return; + + // If both are pregnant, we have some weird interaction. Exit Early + if (pawn.IsPregnant() && partner.IsPregnant()) + return; + // If neither are pregnant, nothing can happen. + if (!pawn.IsPregnant() && !partner.IsPregnant()) + return; + + ModLog.Debug("Firing Pregnancy Overwrite Patch - Passed Simple NullChecks"); + + if (pawn.IsPregnant() + && GeneUtility.HasGeneNullCheck(partner, GeneDefOf.rjw_genes_pregnancy_overwrite)) + TryReplacePregnancy(partner, pawn); + + if (partner.IsPregnant() + && GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_pregnancy_overwrite)) + TryReplacePregnancy(pawn, partner); + } + + public static void TryReplacePregnancy(Pawn replacer, Pawn pregnant) + { + // TODO: This mostly works, but needs some more checks. + // - Check if there is a pregnancy occurring + // - Check for Disease Immunity + // - Add Faction Penalties + + ModLog.Debug($"Firing Pregnancy Overwrite for {replacer} and {pregnant}"); + + // The "CanImpregnate" does not work as I want, as the pawn is already pregnant, so it wont allow to be pregnated. + //PregnancyHelper.CanImpregnate(pawn, partner, props.sexType) + + Hediff pregnancyHediff = PregnancyUtility.GetPregnancyHediff(pregnant); + if (pregnancyHediff == null) + return; + + float gestationProgress = pregnancyHediff.Severity; + + PregnancyUtility.ForceEndPregnancy(pregnant); + + PregnancyHelper.StartVanillaPregnancy(pregnant, replacer); + Hediff replacementPregnancyHediff = PregnancyUtility.GetPregnancyHediff(pregnant); + replacementPregnancyHediff.Severity = gestationProgress; + } + + } +} diff --git a/Source/Rjw-Genes.csproj b/Source/Rjw-Genes.csproj index 306bcb7..8354632 100644 --- a/Source/Rjw-Genes.csproj +++ b/Source/Rjw-Genes.csproj @@ -61,6 +61,7 @@ + @@ -193,6 +194,7 @@ +