Draft for birth-inheritance of hive genes

This commit is contained in:
Vegapnk 2023-04-23 19:09:22 +02:00
parent 085f572780
commit a15895947a
11 changed files with 411 additions and 3 deletions

View File

@ -15,6 +15,22 @@ Fixes:
- Cockeater now leaves a bite wound!
- Pythokin-Patch checks for Licentialabs (#30)
**Queen & Caste Logic**
There are 3 genes revolting around a new, hopefully flexible insect-caste system. Queens, Drones and Workers. These reproduce either through normal sex, or can utilize the insect birth once [this PR](https://gitgud.io/Ed86/rjw/-/merge_requests/266) has been merged in.
In general, the logic is the following:
- A queen can have sex with anyone. If the partner was a drone, there is chance for the baby to become a queen, drone or worker.
- If the partner of the queen was not a drone, the baby will be a worker.
- If the drone didn't mate with a queen but someone else, normal inheritance happens
- The assignemnt is done by xenotypes for queen and drones. The baby will get all xenogenes of their parents xenotypes.
- For workers, every queen can have a set of genes for their workers defined in [a special def](./Common/Defs/QueenWorkerMappingDefs/QueenWorkerMappingDefs_base.xml). These will be added as endogenes, so that pawns can still become xenotypes.
- There is a default set for worker genes, making mentally dumb, sterile and servile pawns.
I am not sure if I want to have a specific mapping defining that Queen can only mate with certain Drones, let me know how you feel about it.
*Queens can be male*. I just used the female-term, but implementation is gender-neutral.
ToDo:
- Icons: Cocoon, Spelopede Spawn

View File

@ -34,6 +34,7 @@
<exclusionTags>
<li>rjw_genes_hive_caste</li>
<li>rjw_genes_swearing_loyalty</li>
</exclusionTags>
<biostatCpx>7</biostatCpx>
@ -121,6 +122,10 @@
<displayOrderInCategory>10</displayOrderInCategory>
<displayCategory>rjw_genes_hive</displayCategory>
<exclusionTags>
<li>rjw_genes_swearing_loyalty</li>
</exclusionTags>
<biostatCpx>1</biostatCpx>
<biostatMet>-1</biostatMet>
</GeneDef>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>
<Defs>
<XenotypeDef>
<defName>rjw_genes_test_queen_xenotype</defName>
<label>Test Queen</label>
<iconPath>UI/Icons/Xenotypes/Sanguophage</iconPath>
<soundDefOnImplant>PawnBecameSanguophage</soundDefOnImplant>
<genes>
<li>rjw_genes_queen</li>
<li>rjw_genes_hypersexual</li>
<li>rjw_genes_female_only</li>
<li>Fertile</li>
<li>Skin_PaleYellow</li>
</genes>
</XenotypeDef>
<XenotypeDef>
<defName>rjw_genes_test_drone_xenotype</defName>
<label>Test Drone</label>
<iconPath>UI/Icons/Xenotypes/Sanguophage</iconPath>
<genes>
<li>rjw_genes_drone</li>
<li>rjw_genes_male_only</li>
<li>rjw_genes_zealous_loyalty</li>
<li>Fertile</li>
<li>Skin_DeepRed</li>
</genes>
</XenotypeDef>
</Defs>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<Defs>
<RJW_Genes.QueenWorkerMappingDef>
<!-- Important: this default defName cannot be renamed! It is a protected keyword.-->
<!-- Changing the queenXenotype-Name will also throw an error.-->
<!-- But you can change the workerGenes list.-->
<defName>rjw_genes_default_worker_genes</defName>
<queenXenotype>default</queenXenotype>
<workerGenes>
<li>rjw_genes_worker</li>
<li>Sterile</li>
<li>rjw_genes_zealous_loyalty</li>
<li>Skin_Green</li>
</workerGenes>
</RJW_Genes.QueenWorkerMappingDef>
<RJW_Genes.QueenWorkerMappingDef>
<defName>rjw_genes_test_queen_worker_mapping</defName>
<queenXenotype>rjw_genes_test_queen_xenotype</queenXenotype>
<workerGenes>
<li>rjw_genes_worker</li>
<li>rjw_genes_zealous_loyalty</li>
<li>Skin_InkBlack</li>
</workerGenes>
</RJW_Genes.QueenWorkerMappingDef>
</Defs>

View 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;
}
}

View File

@ -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))
{

View File

@ -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));
}
}
}

View 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;
}
}
}

View File

@ -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)
{

View File

@ -1,10 +1,20 @@
using Verse;
using System.Linq;
using Verse;
namespace BTE_MMLA
namespace RJW_Genes
{
[StaticConstructorOnStartup]
public static class RJW_Genes
{
static RJW_Genes() => Log.Message("RJW-Genes loaded");
static RJW_Genes()
{
ModLog.Message("RJW-Genes loaded");
if (RJW_Genes_Settings.rjw_genes_detailed_debug)
{
ModLog.Message($"{HiveUtility.getQueenXenotypes().EnumerableCount()} Queen-Xenotypes ({string.Join(",", HiveUtility.getQueenXenotypes().Select(t => t.defName))})");
ModLog.Message($"{HiveUtility.getDroneXenotypes().EnumerableCount()} Drone-Xenotypes ({string.Join(",", HiveUtility.getDroneXenotypes().Select(t => t.defName))})");
ModLog.Message($"Found {HiveUtility.GetQueenWorkerMappings().Count} Queen-Worker Mappings ({string.Join(",", HiveUtility.GetQueenWorkerMappings().Keys.Select(t => t.defName))} + Default) ");
}
}
}
}

View File

@ -95,6 +95,8 @@
<Compile Include="Genes\Hive\Genes\ConditionalStatAffecters\ConditionalStatAffecter_QueenCloseBy.cs" />
<Compile Include="Genes\Hive\Genes\ConditionalStatAffecters\ConditionalStatAffecter_QueenAbsent.cs" />
<Compile Include="Genes\Hive\Helpers\HiveUtility.cs" />
<Compile Include="Genes\Hive\Defs\QueenWorkerMappingDef.cs" />
<Compile Include="Genes\Hive\Patches\Patch_BirthOutcome_SetHiveGenes.cs" />
<Compile Include="Genes\Hive\Thoughts\Thoughtworker_QueenAbsent_Mood.cs" />
<Compile Include="Genes\Hive\Thoughts\Thoughtworker_MultipleQueens_Mood.cs" />
<Compile Include="Genes\Hive\Thoughts\Thoughtworker_QueenPresent_Mood.cs" />