From 8dc63c93081759c012588e3ed77156a8e196079a Mon Sep 17 00:00:00 2001 From: Vegapnk Date: Mon, 5 Jun 2023 16:52:55 +0200 Subject: [PATCH] Fallback Logic for non-parent eggs, closes #37 --- Source/Genes/Hive/Helpers/HiveBirthLogic.cs | 4 +- ...Patch_InsectEggs_BirthBaby_SetHiveGenes.cs | 62 +++++++++++++++++-- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Source/Genes/Hive/Helpers/HiveBirthLogic.cs b/Source/Genes/Hive/Helpers/HiveBirthLogic.cs index d5ca5f0..8bb1600 100644 --- a/Source/Genes/Hive/Helpers/HiveBirthLogic.cs +++ b/Source/Genes/Hive/Helpers/HiveBirthLogic.cs @@ -30,9 +30,10 @@ namespace RJW_Genes /// /// The pawn born, that maybe becomes a hive-xenotype. /// whether there was a drone parent involved - public static void ManageHiveBirth(Pawn pawn, bool hasDroneParent = false) + public static void ManageHiveBirth(Pawn pawn, bool hasDroneParent = false, XenotypeDef fallbackQueenDef = null, XenotypeDef fallbackDroneDef = null) { XenotypeDef queenDef = TryFindParentQueenXenotype(pawn); + if (queenDef == null) queenDef = fallbackQueenDef; HiveOffspringChanceDef hiveOffspringChanceDef = HiveUtility.LookupHiveInheritanceChances(queenDef); // Case 1: Mother is Queen, Father is something else. Produce Worker. @@ -56,6 +57,7 @@ namespace RJW_Genes else if (roll < hiveOffspringChanceDef.droneChance + hiveOffspringChanceDef.queenChance) { XenotypeDef droneDef = TryFindParentDroneXenotype(pawn); + if (droneDef == null) droneDef = fallbackDroneDef; pawn.genes.SetXenotype(droneDef); if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a new drone with xenotype {droneDef.defName} ({(hiveOffspringChanceDef.droneChance + hiveOffspringChanceDef.queenChance) * 100}% chance,rolled {roll}))"); } diff --git a/Source/Genes/Hive/Patches/Patch_InsectEggs_BirthBaby_SetHiveGenes.cs b/Source/Genes/Hive/Patches/Patch_InsectEggs_BirthBaby_SetHiveGenes.cs index c864732..fe9155e 100644 --- a/Source/Genes/Hive/Patches/Patch_InsectEggs_BirthBaby_SetHiveGenes.cs +++ b/Source/Genes/Hive/Patches/Patch_InsectEggs_BirthBaby_SetHiveGenes.cs @@ -23,9 +23,8 @@ namespace RJW_Genes [HarmonyPostfix] - static void HandleHiveBasedInheritance(ref Thing __result) + static void HandleHiveBasedInheritance(ref Thing __result, ref Hediff_InsectEgg __instance) { - // Check: Was the born thing a pawn? if (__result == null || !(__result is Pawn)) { @@ -35,19 +34,70 @@ namespace RJW_Genes Pawn pawn = (Pawn)__result; - // Important: Not all pawns have mother/father. Some Pawns are born in Growth-Vats or born from mod. - bool hasQueenParent = HiveBirthLogic.TryFindParentQueenXenotype(pawn) != null; - bool hasDroneParent = HiveBirthLogic.TryFindParentDroneXenotype(pawn) != null; + XenotypeDef queenDef = HiveBirthLogic.TryFindParentQueenXenotype(pawn) ?? TryFindParentQueenXenotypeFromEgg(__instance); + XenotypeDef droneDef = HiveBirthLogic.TryFindParentDroneXenotype(pawn) ?? TryFindParentDroneXenotypeFromEgg(__instance); + + bool hasQueenParent = queenDef != null; + bool hasDroneParent = droneDef != null; if (hasQueenParent) { if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"PostFix Hediff_InsectEgg::ProcessHumanLikeInsectEgg - Checking Hive Inheritance because {pawn} has a queen parent."); - HiveBirthLogic.ManageHiveBirth(pawn, hasDroneParent); + HiveBirthLogic.ManageHiveBirth(pawn, hasDroneParent, fallbackQueenDef: queenDef, fallbackDroneDef: droneDef); } else { if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"Ignoring Postfix Hediff_InsectEgg::ProcessHumanLikeInsectEgg - No Queen Parent - No Action."); } } + /// + /// Tries to retrieve a queen-xenotype-def from a given egg. + /// Checking priority goes: Implanter > Fertilizer > Null Otherwise. + /// + /// This is meant to be a fallback to the parent-relations which were not present in RJW 5.3.1. + /// Some comments and thoughts are captured in Issue #37. + /// + /// An Egg for which queens are looked up for + /// The relevant xenotypedef of a queen, or null. + public static XenotypeDef TryFindParentQueenXenotypeFromEgg(Hediff_InsectEgg egg) + { + XenotypeDef queenDef = null; + if (egg == null) + return null; + + if (egg.implanter != null) + queenDef = HiveUtility.TryGetQueenXenotype(egg.implanter); + + if (queenDef == null && egg.father != null) + queenDef = HiveUtility.TryGetQueenXenotype(egg.implanter); + + return queenDef; + } + + + + /// + /// Tries to retrieve a drone-xenotype-def from a given egg. + /// Checking priority goes: Implanter > Fertilizer > Null Otherwise. + /// + /// This is meant to be a fallback to the parent-relations which were not present in RJW 5.3.1. + /// Some comments and thoughts are captured in Issue #37. + /// + /// An Egg for which drones are looked up for + /// The relevant xenotypedef of a drone, or null. + public static XenotypeDef TryFindParentDroneXenotypeFromEgg(Hediff_InsectEgg egg) + { + XenotypeDef droneDef = null; + if (egg == null) + return null; + + if (egg.implanter != null) + droneDef = HiveUtility.TryGetQueenXenotype(egg.implanter); + + if (droneDef == null && egg.father != null) + droneDef = HiveUtility.TryGetQueenXenotype(egg.implanter); + + return droneDef; + } } }