mirror of
https://github.com/vegapnk/RJW-Genes.git
synced 2024-08-15 00:23:31 +00:00
Draft for birth-inheritance of hive genes
This commit is contained in:
parent
085f572780
commit
a15895947a
11 changed files with 411 additions and 3 deletions
22
Source/Genes/Hive/Defs/QueenWorkerMappingDef.cs
Normal file
22
Source/Genes/Hive/Defs/QueenWorkerMappingDef.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Verse;
|
||||
|
||||
namespace RJW_Genes
|
||||
{
|
||||
/// <summary>
|
||||
/// This def covers the birth of workers for each queen-xenotype.
|
||||
///
|
||||
/// It is used when a baby is born to a pawn with the queen-xenotype;
|
||||
/// There is a random check for the type of the baby, and if the baby is born to be a worker,
|
||||
/// additional genes are looked up here.
|
||||
/// </summary>
|
||||
public class QueenWorkerMappingDef : Def
|
||||
{
|
||||
public string queenXenotype;
|
||||
public List<string> workerGenes;
|
||||
}
|
||||
}
|
|
@ -21,6 +21,9 @@ namespace RJW_Genes
|
|||
{
|
||||
if (req.Pawn == null || !req.Pawn.Spawned)
|
||||
return false;
|
||||
// If the pawn is not on Map (e.g. caravan), no mali
|
||||
if (!HiveUtility.PawnIsOnHomeMap(req.Pawn))
|
||||
return false;
|
||||
|
||||
if (GeneUtility.HasGeneNullCheck(req.Pawn, GeneDefOf.rjw_genes_zealous_loyalty))
|
||||
{
|
||||
|
|
|
@ -67,5 +67,142 @@ namespace RJW_Genes
|
|||
&& pawn.Spawned
|
||||
&& pawn.Map == homeMap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Xenotype of a pawn if the pawn has a xenotype with the queen gene.
|
||||
/// Null otherwise.
|
||||
/// </summary>
|
||||
/// <param name="pawn"></param>
|
||||
/// <returns>A xenotype with a queen gene, or null.</returns>
|
||||
public static XenotypeDef TryGetQueenXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
if (GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_queen))
|
||||
{
|
||||
var potentialXenotype = pawn.genes.Xenotype;
|
||||
if (potentialXenotype != null && potentialXenotype.genes.Contains(GeneDefOf.rjw_genes_queen))
|
||||
{
|
||||
return potentialXenotype;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Xenotype of a pawn if the pawn has a xenotype with the drone gene.
|
||||
/// Null otherwise.
|
||||
/// </summary>
|
||||
/// <param name="pawn"></param>
|
||||
/// <returns>A xenotype with a drone gene, or null.</returns>
|
||||
public static XenotypeDef TryGetDroneXenotype(Pawn pawn)
|
||||
{
|
||||
if (pawn == null)
|
||||
return null;
|
||||
|
||||
if (GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_drone))
|
||||
{
|
||||
var potentialXenotype = pawn.genes.Xenotype;
|
||||
if (potentialXenotype != null && potentialXenotype.genes.Contains(GeneDefOf.rjw_genes_drone))
|
||||
{
|
||||
return potentialXenotype;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up the Queen-WorkerMappings and returns a cleaned / updated dictionary.
|
||||
///
|
||||
/// This method takes care of genes maybe not existing (from other mods) or misspellings etc.
|
||||
/// Prints a bigger piece of information when debug printing is enabled.
|
||||
/// </summary>
|
||||
/// <returns>A mapping which Queen-Xenotypes should produce which worker genes.</returns>
|
||||
|
||||
public static Dictionary<XenotypeDef,List<GeneDef>> GetQueenWorkerMappings()
|
||||
{
|
||||
Dictionary<XenotypeDef,List<GeneDef>> dict = new Dictionary<XenotypeDef, List<GeneDef>>();
|
||||
IEnumerable<QueenWorkerMappingDef> mappingDefs = DefDatabase<QueenWorkerMappingDef>.AllDefs;
|
||||
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"Found {mappingDefs.Count()} Queen-Worker mappings in defs");
|
||||
|
||||
// Dev-Note: I first a nice lambda here, but I used nesting in favour of logging.
|
||||
foreach (QueenWorkerMappingDef mappingDef in mappingDefs)
|
||||
{
|
||||
|
||||
if (mappingDef.defName == "rjw_genes_default_worker_genes")
|
||||
{
|
||||
// Do nothing, there is no lookup but this entry is fine and should not log a warning.
|
||||
continue;
|
||||
}
|
||||
XenotypeDef queenDef = DefDatabase<XenotypeDef>.GetNamed(mappingDef.queenXenotype);
|
||||
if (queenDef != null)
|
||||
{
|
||||
List<GeneDef> workerGenes = new List<GeneDef>();
|
||||
foreach (string geneName in mappingDef.workerGenes)
|
||||
{
|
||||
GeneDef workerGene = DefDatabase<GeneDef>.GetNamed(geneName);
|
||||
if (workerGene != null)
|
||||
workerGenes.Add(workerGene);
|
||||
else if(RJW_Genes_Settings.rjw_genes_detailed_debug)
|
||||
ModLog.Warning($"Could not look up Gene {geneName} for {mappingDef.queenXenotype}.");
|
||||
}
|
||||
dict.Add(queenDef, workerGenes);
|
||||
}
|
||||
else {
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug)
|
||||
ModLog.Warning($"Did not find a matching xenotype for {mappingDef.queenXenotype}! Defaulting to rjw_genes_default_worker_genes.");
|
||||
}
|
||||
}
|
||||
|
||||
return dict;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up the default genes for any queen offspring that has no other definition for it.
|
||||
/// This is done by looking for the mapping with *exactly* defName rjw_genes_default_worker_genes.
|
||||
///
|
||||
/// The idea is that players can edit the default types, but that this is a protected keyword.
|
||||
/// </summary>
|
||||
/// <returns>A list of genes for workers that do not have specific mappings defined.</returns>
|
||||
public static List<GeneDef> LookupDefaultWorkerGenes()
|
||||
{
|
||||
IEnumerable<QueenWorkerMappingDef> mappingDefs = DefDatabase<QueenWorkerMappingDef>.AllDefs;
|
||||
|
||||
List<GeneDef> workerGenes = new List<GeneDef>();
|
||||
|
||||
var defaultMapping = mappingDefs.First(m => m.defName == "rjw_genes_default_worker_genes");
|
||||
if (defaultMapping == null)
|
||||
{
|
||||
ModLog.Error("Did not find default worker genes for queen-offspring! Please make sure you did not rename the 'rjw_genes_default_worker_genes'.");
|
||||
return workerGenes;
|
||||
}
|
||||
|
||||
foreach (string geneName in defaultMapping.workerGenes)
|
||||
{
|
||||
GeneDef workerGene = DefDatabase<GeneDef>.GetNamed(geneName);
|
||||
if (workerGene != null)
|
||||
workerGenes.Add(workerGene);
|
||||
else if (RJW_Genes_Settings.rjw_genes_detailed_debug)
|
||||
ModLog.Warning($"Could not look up gene {geneName} for rjw_genes_default_worker_genes.");
|
||||
}
|
||||
|
||||
return workerGenes;
|
||||
}
|
||||
|
||||
public static IEnumerable<XenotypeDef> getQueenXenotypes()
|
||||
{
|
||||
return DefDatabase<XenotypeDef>.AllDefs.Where(type => type.genes.Contains(GeneDefOf.rjw_genes_queen));
|
||||
}
|
||||
|
||||
public static IEnumerable<XenotypeDef> getDroneXenotypes()
|
||||
{
|
||||
return DefDatabase<XenotypeDef>.AllDefs.Where(type => type.genes.Contains(GeneDefOf.rjw_genes_drone));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
150
Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs
Normal file
150
Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs
Normal file
|
@ -0,0 +1,150 @@
|
|||
using HarmonyLib;
|
||||
using RimWorld;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Verse;
|
||||
|
||||
namespace RJW_Genes
|
||||
{
|
||||
/// <summary>
|
||||
/// Patches the method `ApplyBirthOutcome` from `PregnancyUtility`.
|
||||
///
|
||||
/// The 'ApplyBirthOutcome' returns the finished baby, for which we alter the pawn according to our xenotypes.
|
||||
/// </summary>
|
||||
|
||||
[HarmonyPatch(typeof(PregnancyUtility), nameof(PregnancyUtility.ApplyBirthOutcome))]
|
||||
public class Patch_BirthOutcome_SetHiveGenes
|
||||
{
|
||||
const double QUEEN_CHANCE = 0.01f;
|
||||
const double DRONE_CHANCE = 0.49f;
|
||||
const double WORKER_CHANCE = 1 - QUEEN_CHANCE - DRONE_CHANCE;
|
||||
|
||||
|
||||
[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.");
|
||||
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;
|
||||
|
||||
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);
|
||||
// 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 < QUEEN_CHANCE)
|
||||
{
|
||||
pawn.genes.SetXenotype(queenDef);
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a new queen with xenotype {queenDef.defName} ({QUEEN_CHANCE*100}% chance,rolled {roll})");
|
||||
// TODO: Make a letter ?
|
||||
}
|
||||
// Case 2.b: New Drone born
|
||||
else if (roll < DRONE_CHANCE + QUEEN_CHANCE)
|
||||
{
|
||||
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} ({(DRONE_CHANCE + QUEEN_CHANCE) * 100}% chance,rolled {roll}))");
|
||||
}
|
||||
// Case 2.c: Worker
|
||||
else {
|
||||
if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a worker ({(WORKER_CHANCE) * 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>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,6 +20,9 @@ namespace RJW_Genes
|
|||
// Queens cannot have loyalty thoughts
|
||||
if (GeneUtility.HasGeneNullCheck(p, GeneDefOf.rjw_genes_queen))
|
||||
return (ThoughtState)false;
|
||||
// If the pawn is not on Map (e.g. caravan), no mali
|
||||
if (!HiveUtility.PawnIsOnHomeMap(p))
|
||||
return (ThoughtState)false;
|
||||
|
||||
if (GeneUtility.HasGeneNullCheck(p, GeneDefOf.rjw_genes_zealous_loyalty) && HiveUtility.QueensOnMap() >= 2)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue