diff --git a/CHANGELOG.md b/CHANGELOG.md index 495d954..89390cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,18 @@ # 1.2 -Changes: +**Changes:** - Cocoon Weaver Gene -- Spawn Spelopede Gene (Can re changed to spawn Megascarabs or other insects) +- Spawn Spelopede Gene (Can be changed to spawn Megascarabs or other insects via xml) +- Queens & Caste Logic (see below) +- Addition to InsectIncubator: Now fertilizes eggs once placed inside a host, and breeds out eggs twice as fast. -Internal: +**Internal:** - Renamed Abilities to have _ability_ in their name, to not exactly match the gene-defnames. +- Moved Insect-Incubator & Insect-Breeder to Hive Category and Folders -Fixes: +**Fixes:** - Cockeater Ability has now Icon of Cockeater Gene - Cockeater now leaves a bite wound! @@ -27,11 +30,12 @@ In general, the logic is the following: - 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. +- Chances for Offsprings (Drone, Queen, Worker) is defined in an [XML-Def](./Common/Defs/HiveOffspringChanceDef/HiveOffspringChanceDefs.xml) 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: +**ToDo:** - Icons: Cocoon, Spelopede Spawn - Sound: Spelopede Spawn diff --git a/Common/Defs/GeneDefs/GeneDefs_Breeding.xml b/Common/Defs/GeneDefs/GeneDefs_Breeding.xml index a2d7d7e..7482459 100644 --- a/Common/Defs/GeneDefs/GeneDefs_Breeding.xml +++ b/Common/Defs/GeneDefs/GeneDefs_Breeding.xml @@ -13,27 +13,6 @@ -1 - - rjw_genes_insectincubator - - Pawns with this gene are able to hold more insect eggs. - Genes/Icons/More_Egg_Space - 52 - rjw_genes_breeding - 1 - -1 - - - - rjw_genes_insectbreeder - - Pawns with this gene are able to fertilize eggs with any fertile penis. - World/WorldObjects/Expanding/Insects - 53 - rjw_genes_breeding - 1 - -1 - rjw_genes_zoophile diff --git a/Common/Defs/GeneDefs/GeneDefs_Hive.xml b/Common/Defs/GeneDefs/GeneDefs_Hive.xml index 897851f..84b15ff 100644 --- a/Common/Defs/GeneDefs/GeneDefs_Hive.xml +++ b/Common/Defs/GeneDefs/GeneDefs_Hive.xml @@ -165,4 +165,29 @@ 1 -1 + + + + rjw_genes_insectincubator + + RJW_Genes.Gene_InsectIncubator + Pawns with this gene are able to host more insect eggs, hatch them faster and fertilize any inserted egg. + Genes/Icons/More_Egg_Space + 52 + rjw_genes_hive + 1 + -1 + + + + rjw_genes_insectbreeder + + Pawns with this gene are able to fertilize eggs with any fertile penis. + World/WorldObjects/Expanding/Insects + 53 + rjw_genes_hive + 1 + -1 + + \ No newline at end of file diff --git a/Common/Defs/HiveOffspringChanceDef/HiveOffspringChanceDefs.xml b/Common/Defs/HiveOffspringChanceDef/HiveOffspringChanceDefs.xml new file mode 100644 index 0000000..884bd6a --- /dev/null +++ b/Common/Defs/HiveOffspringChanceDef/HiveOffspringChanceDefs.xml @@ -0,0 +1,36 @@ + + + + + + + + + + rjw_genes_default_hive_offspring_chances + default + + 0.02 + 0.48 + 0.5 + + + + + rjw_genes_test_queen_offspring_chances + rjw_genes_test_queen_xenotype + + 0.02 + 0.28 + 0.7 + + + + \ No newline at end of file diff --git a/Source/Genes/Hive/Defs/HiveOffspringChanceDef.cs b/Source/Genes/Hive/Defs/HiveOffspringChanceDef.cs new file mode 100644 index 0000000..77a4c28 --- /dev/null +++ b/Source/Genes/Hive/Defs/HiveOffspringChanceDef.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace RJW_Genes +{ + public class HiveOffspringChanceDef : Def + { + + public string queenXenotype; + + // Chance of the below should be 1 when summed up! + // Otherwise the roll-logic fails. + + public double queenChance; + public double droneChance; + public double workerChance; + + } +} diff --git a/Source/Genes/Hive/Genes/Gene_InsectIncubator.cs b/Source/Genes/Hive/Genes/Gene_InsectIncubator.cs new file mode 100644 index 0000000..d102a8a --- /dev/null +++ b/Source/Genes/Hive/Genes/Gene_InsectIncubator.cs @@ -0,0 +1,53 @@ +using RimWorld; +using rjw; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Verse; + +namespace RJW_Genes +{ + + /// + /// This Gene checks for all parasitic Insect-Eggs in a Pawn: + /// 1. Is it fertilized ? => tick it down "extra". + /// 2. Is it not fertilized? => fertilize it with the Incubator as parent + /// + /// Important: The other half of the behavior for the gene (more egg-capacity) is in `Patch_InsectINcubator_PregnancyHelper`. + /// + public class Gene_InsectIncubator : Gene + { + + const int TICK_INTERVAL = 60000 / 48; // 60k = 1 day, we want 0.5h which is 1/48th of 1 day. + + public override void Tick() + { + base.Tick(); + + // Don't check too often, only in the HashTickInterval to safe some computing power + if (this.pawn.IsHashIntervalTick(TICK_INTERVAL) && this.pawn.Map != null) + { + List eggs = new List(); + pawn.health.hediffSet.GetHediffs(ref eggs); + + foreach (Hediff_InsectEgg egg in eggs) + { + // The implanter check checks if the egg is still in an ovipositor. + if (egg.implanter == null || egg.implanter == pawn) + continue; + + if (!egg.fertilized && egg.implanter != null) { + egg.Fertilize(pawn); + if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"Gene_InsectIncubator: fertilized egg {egg} in {pawn}"); + } + else if (egg.fertilized) + { + egg.lastTick += TICK_INTERVAL; + } + } + } + } + } +} diff --git a/Source/Genes/Hive/Helpers/HiveUtility.cs b/Source/Genes/Hive/Helpers/HiveUtility.cs index 945db92..db65fb6 100644 --- a/Source/Genes/Hive/Helpers/HiveUtility.cs +++ b/Source/Genes/Hive/Helpers/HiveUtility.cs @@ -128,8 +128,6 @@ namespace RJW_Genes Dictionary> dict = new Dictionary>(); IEnumerable mappingDefs = DefDatabase.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) { @@ -204,5 +202,27 @@ namespace RJW_Genes return DefDatabase.AllDefs.Where(type => type.genes.Contains(GeneDefOf.rjw_genes_drone)); } + + public static HiveOffspringChanceDef LookupDefaultHiveInheritanceChances() + { + IEnumerable offspringChanceDefs = DefDatabase.AllDefs; + + List workerGenes = new List(); + + var defaultChance = offspringChanceDefs.First(m => m.defName == "rjw_genes_default_hive_offspring_chances"); + + if (defaultChance == null) + ModLog.Warning("Did not find `rjw_genes_default_hive_offspring_chances`. Someone likely changed the defname!"); + + return defaultChance; + } + + public static HiveOffspringChanceDef LookupHiveInheritanceChances(XenotypeDef queenDef) + { + IEnumerable offspringChanceDefs = DefDatabase.AllDefs; + + return offspringChanceDefs.FirstOrFallback(t => t.queenXenotype == queenDef.defName, LookupDefaultHiveInheritanceChances()); + } + } } diff --git a/Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs b/Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs index c640118..c075cc8 100644 --- a/Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs +++ b/Source/Genes/Hive/Patches/Patch_BirthOutcome_SetHiveGenes.cs @@ -18,9 +18,6 @@ namespace RJW_Genes [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] @@ -45,6 +42,8 @@ namespace RJW_Genes 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) { @@ -56,22 +55,22 @@ namespace RJW_Genes { double roll = (new Random()).NextDouble(); // Case 2.a: New Queen born - if (roll < QUEEN_CHANCE) + 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} ({QUEEN_CHANCE*100}% chance,rolled {roll})"); + 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 < DRONE_CHANCE + QUEEN_CHANCE) + 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} ({(DRONE_CHANCE + QUEEN_CHANCE) * 100}% chance,rolled {roll}))"); + 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 ({(WORKER_CHANCE) * 100}% chance,rolled {roll}))"); + if (RJW_Genes_Settings.rjw_genes_detailed_debug) ModLog.Message($"{pawn} born as a worker ({(hiveOffspringChanceDef.workerChance) * 100}% chance,rolled {roll}))"); MakeWorker(pawn, queenDef); } } diff --git a/Source/Genes/Breeding/Patch_EggFertilization.cs b/Source/Genes/Hive/Patches/Patch_InsectBreeder_EggFertilization.cs similarity index 97% rename from Source/Genes/Breeding/Patch_EggFertilization.cs rename to Source/Genes/Hive/Patches/Patch_InsectBreeder_EggFertilization.cs index d2f2dcf..6184b4a 100644 --- a/Source/Genes/Breeding/Patch_EggFertilization.cs +++ b/Source/Genes/Hive/Patches/Patch_InsectBreeder_EggFertilization.cs @@ -12,9 +12,10 @@ namespace RJW_Genes /// Patched Class is https://gitgud.io/Ed86/rjw/-/blob/master/1.4/Source/Common/Helpers/SexUtility.cs /// /// Normal Egg-Pregnancy logic is in https://gitgud.io/Ed86/rjw/-/blob/master/1.4/Source/Modules/Pregnancy/Pregnancy_Helper.cs + /// Gene: rjw_genes_insectbreeder /// [HarmonyPatch(typeof(SexUtility), "Aftersex")] - static class Patch_EggFertilization + static class Patch_InsectBreeder_EggFertilization { public static void Postfix(SexProps props) { diff --git a/Source/Genes/Breeding/PatchPregnancyHelper.cs b/Source/Genes/Hive/Patches/Patch_InsectIncubator_PregnancyHelper.cs similarity index 95% rename from Source/Genes/Breeding/PatchPregnancyHelper.cs rename to Source/Genes/Hive/Patches/Patch_InsectIncubator_PregnancyHelper.cs index 81a0ad4..fc3e530 100644 --- a/Source/Genes/Breeding/PatchPregnancyHelper.cs +++ b/Source/Genes/Hive/Patches/Patch_InsectIncubator_PregnancyHelper.cs @@ -15,9 +15,11 @@ namespace RJW_Genes /// /// This Class patches the RJW-DoEgg to allow up to MaxEggSizeMul times the original amount of eggs. /// This harmony patch was kindly provided by 'shabalox' https://github.com/Shabalox/RJW_Genes_Addons/ + /// + /// For Gene: rjw_genes_insectincubator /// [HarmonyPatch(typeof(PregnancyHelper), "DoEgg")] - static class PatchPregnancyHelper + static class Patch_InsectIncubator_PregnancyHelper { [HarmonyTranspiler] public static IEnumerable Transpiler(IEnumerable instructions, ILGenerator il) diff --git a/Source/RJW_Genes.cs b/Source/RJW_Genes.cs index e25feef..db4ffbf 100644 --- a/Source/RJW_Genes.cs +++ b/Source/RJW_Genes.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using Verse; namespace RJW_Genes @@ -14,6 +15,11 @@ namespace RJW_Genes 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) "); + + + IEnumerable offspringChanceDefs = DefDatabase.AllDefs; + IEnumerable faultOffspringDefs = offspringChanceDefs.Where(t => t.queenChance + t.workerChance + t.workerChance > 1.02 || t.queenChance + t.workerChance + t.workerChance < 0.98 ); + ModLog.Message($"Found {offspringChanceDefs.Count()} OffspringChanceDefs, of which {faultOffspringDefs.Count()} had faulty chances ({string.Join(",", faultOffspringDefs.Select(t => t.defName))})"); } } } diff --git a/Source/Rjw-Genes.csproj b/Source/Rjw-Genes.csproj index 68a3e34..6c1da44 100644 --- a/Source/Rjw-Genes.csproj +++ b/Source/Rjw-Genes.csproj @@ -43,8 +43,10 @@ - - + + + +