mirror of
https://github.com/vegapnk/RJW-Genes.git
synced 2024-08-15 00:23:31 +00:00
Extracted HiveBirthLogic in own File #37 - still buggy
This commit is contained in:
parent
47a78073bf
commit
8c267073bf
5 changed files with 151 additions and 211 deletions
134
Source/Genes/Hive/Helpers/HiveBirthLogic.cs
Normal file
134
Source/Genes/Hive/Helpers/HiveBirthLogic.cs
Normal file
|
@ -0,0 +1,134 @@
|
|||
using RimWorld;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Verse;
|
||||
|
||||
namespace RJW_Genes
|
||||
{
|
||||
public class HiveBirthLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// Central function for the Hive-Birth logic used in Patches.
|
||||
/// *Only* run this, if the pawn has a queen parent (either as mother/father, or as implanter in case of egg-logic).
|
||||
/// Covers the following behavior:
|
||||
/// 1. look up the Defs for the mother and HiveOffspringChances (or defaults)
|
||||
/// 2. If there is no drone involved, default to worker
|
||||
/// 3. Roll a random dice
|
||||
/// 3.1 Make a queen
|
||||
/// 3.2 Make a drone
|
||||
/// 3.3 Make a worker
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn born, that maybe becomes a hive-xenotype.</param>
|
||||
/// <param name="hasDroneParent">whether there was a drone parent involved</param>
|
||||
public static void ManageHiveBirth(Pawn pawn, bool hasDroneParent = false)
|
||||
{
|
||||
XenotypeDef queenDef = TryFindParentQueenXenotype(pawn);
|
||||
HiveOffspringChanceDef hiveOffspringChanceDef = HiveUtility.LookupHiveInheritanceChances(queenDef);
|
||||
|
||||
// Case 1: Mother is Queen, Father is something else. Produce Worker.
|
||||
if (!hasDroneParent)
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} was born as a worker, as it did not have Drone Father ({100}% chance)");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
// Case 2: Mother is Queen, Father is drone. Apply xenotype as per chance.
|
||||
else
|
||||
{
|
||||
double roll = (new Random()).NextDouble();
|
||||
// Case 2.a: New Queen born
|
||||
if (roll < hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
pawn.genes.SetXenotype(queenDef);
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a new queen with xenotype {queenDef.defName} ({hiveOffspringChanceDef.queenChance * 100}% chance,rolled {roll})");
|
||||
// TODO: Make a letter ?
|
||||
}
|
||||
// Case 2.b: New Drone born
|
||||
else if (roll < hiveOffspringChanceDef.droneChance + hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
XenotypeDef droneDef = TryFindParentDroneXenotype(pawn);
|
||||
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}))");
|
||||
}
|
||||
// Case 2.c: Worker
|
||||
else
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a worker ({(hiveOffspringChanceDef.workerChance) * 100}% chance,rolled {roll}))");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Turns a given pawn into a worker, by looking up the relevant genes as per queen.
|
||||
///
|
||||
/// If the queen xenotype has no mapping, the "rjw_genes_default_worker_xenotype" are used instead.
|
||||
/// The genes are added as endogenes, so the worker can still become a xenotype.
|
||||
/// </summary>
|
||||
/// <param name="pawnTobeWorker">The pawn for which the genes are added.</param>
|
||||
/// <param name="queenDef">The xenotype of the queen, used for lookup.</param>
|
||||
private static void MakeWorker(Pawn pawnTobeWorker, XenotypeDef queenDef)
|
||||
{
|
||||
if (pawnTobeWorker == null)
|
||||
return;
|
||||
|
||||
var mappings = HiveUtility.GetQueenWorkerMappings();
|
||||
|
||||
var genes = mappings.TryGetValue(queenDef, HiveUtility.LookupDefaultWorkerGenes());
|
||||
|
||||
foreach (var gene in genes)
|
||||
pawnTobeWorker.genes.AddGene(gene, false);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Drone-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the drone (instead of hardcoding things for father).
|
||||
/// If both are drones, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Drone-Xenotype of a parent or null. If both are drones, mothers are preferred.</returns>
|
||||
public static XenotypeDef TryFindParentDroneXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Queen-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the queen (instead of hardcoding things for father).
|
||||
/// If both are queens, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Queen-Xenotype of a parent or null. If both are queens, mothers are preferred.</returns>
|
||||
public static XenotypeDef TryFindParentQueenXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -224,5 +224,6 @@ namespace RJW_Genes
|
|||
return offspringChanceDefs.FirstOrFallback(t => t.queenXenotype == queenDef.defName, LookupDefaultHiveInheritanceChances());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,116 +34,20 @@ 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 = TryFindParentQueenXenotype(pawn) != null;
|
||||
bool hasDroneParent = TryFindParentDroneXenotype(pawn) != null;
|
||||
bool hasQueenParent = HiveBirthLogic.TryFindParentQueenXenotype(pawn) != null;
|
||||
bool hasDroneParent = HiveBirthLogic.TryFindParentDroneXenotype(pawn) != null;
|
||||
|
||||
if (hasQueenParent)
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"PostFix PregnancyUtility::ApplyBirthOutcome - Checking Hive Inheritance because {pawn} has a queen parent.");
|
||||
|
||||
XenotypeDef queenDef = TryFindParentQueenXenotype(pawn);
|
||||
HiveOffspringChanceDef hiveOffspringChanceDef = HiveUtility.LookupHiveInheritanceChances(queenDef);
|
||||
|
||||
// Case 1: Mother is Queen, Father is something else. Produce Worker.
|
||||
if (!hasDroneParent)
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} was born as a worker, as it did not have Drone Father ({100}% chance)");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
// Case 2: Mother is Queen, Father is drone. Apply xenotype as per chance.
|
||||
else
|
||||
{
|
||||
double roll = (new Random()).NextDouble();
|
||||
// Case 2.a: New Queen born
|
||||
if (roll < hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
pawn.genes.SetXenotype(queenDef);
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a new queen with xenotype {queenDef.defName} ({hiveOffspringChanceDef.queenChance * 100}% chance,rolled {roll})");
|
||||
// TODO: Make a letter ?
|
||||
}
|
||||
// Case 2.b: New Drone born
|
||||
else if (roll < hiveOffspringChanceDef.droneChance + hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
XenotypeDef droneDef = TryFindParentDroneXenotype(pawn);
|
||||
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}))");
|
||||
}
|
||||
// Case 2.c: Worker
|
||||
else {
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a worker ({(hiveOffspringChanceDef.workerChance) * 100}% chance,rolled {roll}))");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
}
|
||||
HiveBirthLogic.ManageHiveBirth(pawn, hasDroneParent);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"Ignoring Postfix PregnancyUtility::ApplyBirthOutcome - No Quene Parent - Doing Nothing");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns a given pawn into a worker, by looking up the relevant genes as per queen.
|
||||
///
|
||||
/// If the queen xenotype has no mapping, the "rjw_genes_default_worker_xenotype" are used instead.
|
||||
/// The genes are added as endogenes, so the worker can still become a xenotype.
|
||||
/// </summary>
|
||||
/// <param name="pawnTobeWorker">The pawn for which the genes are added.</param>
|
||||
/// <param name="queenDef">The xenotype of the queen, used for lookup.</param>
|
||||
private static void MakeWorker(Pawn pawnTobeWorker, XenotypeDef queenDef)
|
||||
{
|
||||
if (pawnTobeWorker == null)
|
||||
return;
|
||||
|
||||
var mappings = HiveUtility.GetQueenWorkerMappings();
|
||||
|
||||
var genes = mappings.TryGetValue(queenDef, HiveUtility.LookupDefaultWorkerGenes());
|
||||
|
||||
foreach (var gene in genes)
|
||||
pawnTobeWorker.genes.AddGene(gene, false);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Drone-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the drone (instead of hardcoding things for father).
|
||||
/// If both are drones, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Drone-Xenotype of a parent or null. If both are drones, mothers are preferred.</returns>
|
||||
private static XenotypeDef TryFindParentDroneXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Queen-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the queen (instead of hardcoding things for father).
|
||||
/// If both are queens, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Queen-Xenotype of a parent or null. If both are queens, mothers are preferred.</returns>
|
||||
private static XenotypeDef TryFindParentQueenXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace RJW_Genes
|
|||
/// Patches the method `ProcessHumanLikeInsectEgg` from `Hediff_InsectEgg`.
|
||||
///
|
||||
/// The 'ProcessHumanLikeInsectEgg' returns the finished baby, for which we alter the pawn according to our xenotypes.
|
||||
/// Note: This covers Insect-Egg Pregnancies only, and there is a (very similar) class `Patch_BirthOutCome_SetHiveGenes.cs` that handles normal pregnancies
|
||||
/// </summary>
|
||||
|
||||
[HarmonyPatch(typeof(Hediff_InsectEgg), "ProcessHumanLikeInsectEgg")]
|
||||
|
@ -24,130 +25,29 @@ namespace RJW_Genes
|
|||
[HarmonyPostfix]
|
||||
static void HandleHiveBasedInheritance(ref Thing __result)
|
||||
{
|
||||
|
||||
|
||||
// Check: Was the born thing a pawn?
|
||||
if (__result == null || !(__result is Pawn))
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message("There was a birth of something non-human - not entering logic for queen-drone-xenotype inheritance.");
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message("There was a birth of something non-human - not entering logic for queen-drone-xenotype inheritance.");
|
||||
return;
|
||||
}
|
||||
|
||||
Pawn pawn = (Pawn)__result;
|
||||
|
||||
// Important: Not all pawns have mother/father. Some Pawns are born in Growth-Vats or born from mod.
|
||||
bool hasQueenParent = TryFindParentQueenXenotype(pawn) != null;
|
||||
bool hasDroneParent = TryFindParentDroneXenotype(pawn) != null;
|
||||
bool hasQueenParent = HiveBirthLogic.TryFindParentQueenXenotype(pawn) != null;
|
||||
bool hasDroneParent = HiveBirthLogic.TryFindParentDroneXenotype(pawn) != null;
|
||||
|
||||
if (hasQueenParent)
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"PostFix PregnancyUtility::ApplyBirthOutcome - Checking Hive Inheritance because {pawn} has a queen parent.");
|
||||
|
||||
XenotypeDef queenDef = TryFindParentQueenXenotype(pawn);
|
||||
HiveOffspringChanceDef hiveOffspringChanceDef = HiveUtility.LookupHiveInheritanceChances(queenDef);
|
||||
|
||||
// Case 1: Mother is Queen, Father is something else. Produce Worker.
|
||||
if (!hasDroneParent)
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} was born as a worker, as it did not have Drone Father ({100}% chance)");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
// Case 2: Mother is Queen, Father is drone. Apply xenotype as per chance.
|
||||
else
|
||||
{
|
||||
double roll = (new Random()).NextDouble();
|
||||
// Case 2.a: New Queen born
|
||||
if (roll < hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
pawn.genes.SetXenotype(queenDef);
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a new queen with xenotype {queenDef.defName} ({hiveOffspringChanceDef.queenChance * 100}% chance,rolled {roll})");
|
||||
// TODO: Make a letter ?
|
||||
}
|
||||
// Case 2.b: New Drone born
|
||||
else if (roll < hiveOffspringChanceDef.droneChance + hiveOffspringChanceDef.queenChance)
|
||||
{
|
||||
XenotypeDef droneDef = TryFindParentDroneXenotype(pawn);
|
||||
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}))");
|
||||
}
|
||||
// Case 2.c: Worker
|
||||
else {
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a worker ({(hiveOffspringChanceDef.workerChance) * 100}% chance,rolled {roll}))");
|
||||
MakeWorker(pawn, queenDef);
|
||||
}
|
||||
}
|
||||
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);
|
||||
} else
|
||||
{
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message("There was an egg-birth without a (detected) queen-parent");
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"Ignoring Postfix Hediff_InsectEgg::ProcessHumanLikeInsectEgg - No Queen Parent - No Action.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns a given pawn into a worker, by looking up the relevant genes as per queen.
|
||||
///
|
||||
/// If the queen xenotype has no mapping, the "rjw_genes_default_worker_xenotype" are used instead.
|
||||
/// The genes are added as endogenes, so the worker can still become a xenotype.
|
||||
/// </summary>
|
||||
/// <param name="pawnTobeWorker">The pawn for which the genes are added.</param>
|
||||
/// <param name="queenDef">The xenotype of the queen, used for lookup.</param>
|
||||
private static void MakeWorker(Pawn pawnTobeWorker, XenotypeDef queenDef)
|
||||
{
|
||||
if (pawnTobeWorker == null)
|
||||
return;
|
||||
|
||||
var mappings = HiveUtility.GetQueenWorkerMappings();
|
||||
|
||||
var genes = mappings.TryGetValue(queenDef, HiveUtility.LookupDefaultWorkerGenes());
|
||||
|
||||
foreach (var gene in genes)
|
||||
pawnTobeWorker.genes.AddGene(gene, false);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Drone-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the drone (instead of hardcoding things for father).
|
||||
/// If both are drones, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Drone-Xenotype of a parent or null. If both are drones, mothers are preferred.</returns>
|
||||
private static XenotypeDef TryFindParentDroneXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetDroneXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Looks up if there is a Xenotype with Queen-Gene for the pawns parents.
|
||||
/// This is to account that maybe father or mother are the queen (instead of hardcoding things for father).
|
||||
/// If both are queens, the mothers is returned.
|
||||
/// </summary>
|
||||
/// <param name="pawn">The pawn for whichs parent the xenotypes is looked up.</param>
|
||||
/// <returns>The Queen-Xenotype of a parent or null. If both are queens, mothers are preferred.</returns>
|
||||
private static XenotypeDef TryFindParentQueenXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
var motherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetMother());
|
||||
var fatherXenotype = HiveUtility.TryGetQueenXenotype(pawn.GetFather());
|
||||
|
||||
if (motherXenotype != null)
|
||||
return motherXenotype;
|
||||
if (fatherXenotype != null)
|
||||
return fatherXenotype;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
<Compile Include="Genes\Hive\Defs\HiveOffspringChanceDef.cs" />
|
||||
<Compile Include="Genes\Hive\Genes\Gene_FerventOvipositor.cs" />
|
||||
<Compile Include="Genes\Hive\Genes\Gene_InsectIncubator.cs" />
|
||||
<Compile Include="Genes\Hive\Helpers\HiveBirthLogic.cs" />
|
||||
<Compile Include="Genes\Hive\Patches\Patch_InsectEggs_BirthBaby_SetHiveGenes.cs" />
|
||||
<Compile Include="Genes\Hive\Patches\Patch_InsectBreeder_EggFertilization.cs" />
|
||||
<Compile Include="Genes\Hive\Patches\Patch_InsectIncubator_PregnancyHelper.cs" />
|
||||
|
|
Loading…
Reference in a new issue