2024-07-04 14:04:51 +00:00
|
|
|
|
using HarmonyLib;
|
2024-07-05 13:04:45 +00:00
|
|
|
|
using RimWorld;
|
|
|
|
|
using RimWorld.Planet;
|
2024-07-04 14:04:51 +00:00
|
|
|
|
using rjw;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Verse;
|
|
|
|
|
|
|
|
|
|
namespace RJW_Genes
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
[HarmonyPatch(typeof(SexUtility), "Aftersex")]
|
|
|
|
|
public class Patch_GeneticSexThief
|
|
|
|
|
{
|
2024-07-05 13:04:45 +00:00
|
|
|
|
|
|
|
|
|
public const int FACTION_GOODWILL_CHANGE = -10;
|
|
|
|
|
|
2024-07-04 14:04:51 +00:00
|
|
|
|
public static void Postfix(SexProps props)
|
|
|
|
|
{
|
|
|
|
|
if (props == null || props.pawn == null || props.partner == null || props.partner.IsAnimal())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pawn pawn = props.pawn;
|
|
|
|
|
Pawn partner = props.partner;
|
|
|
|
|
|
|
|
|
|
if (pawn.genes == null || partner.genes == null) return;
|
|
|
|
|
|
|
|
|
|
// If both have the swap gene, nothing happens
|
|
|
|
|
if (GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_sexual_genetic_thief)
|
|
|
|
|
&& GeneUtility.HasGeneNullCheck(partner, GeneDefOf.rjw_genes_sexual_genetic_thief))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (GeneUtility.HasGeneNullCheck(pawn,GeneDefOf.rjw_genes_sexual_genetic_thief) &&
|
|
|
|
|
!GeneUtility.HasGeneNullCheck(partner, GeneDefOf.rjw_genes_genetic_disease_immunity))
|
|
|
|
|
{
|
|
|
|
|
ChanceExtension chanceExt = GeneDefOf.rjw_genes_sexual_genetic_thief.GetModExtension<ChanceExtension>();
|
|
|
|
|
if (chanceExt != null && (new Random()).NextDouble() < chanceExt.chance)
|
|
|
|
|
StealRandomGene(pawn, partner);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GeneUtility.HasGeneNullCheck(partner, GeneDefOf.rjw_genes_sexual_genetic_thief) &&
|
|
|
|
|
!GeneUtility.HasGeneNullCheck(pawn, GeneDefOf.rjw_genes_genetic_disease_immunity))
|
|
|
|
|
{
|
|
|
|
|
ChanceExtension chanceExt = GeneDefOf.rjw_genes_sexual_genetic_thief.GetModExtension<ChanceExtension>();
|
|
|
|
|
if (chanceExt != null && (new Random()).NextDouble() < chanceExt.chance)
|
|
|
|
|
StealRandomGene(partner, pawn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a random gene from one pawn and adds it too the other as xenogene.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static void StealRandomGene(Pawn stealer, Pawn victim, bool AddAsXenogene = true)
|
|
|
|
|
{
|
|
|
|
|
var stolenGene = victim.genes.GenesListForReading
|
|
|
|
|
.Where(gene => victim.genes.HasActiveGene(gene.def))
|
|
|
|
|
.RandomElement();
|
|
|
|
|
|
|
|
|
|
if (stolenGene == null) return;
|
|
|
|
|
|
|
|
|
|
ModLog.Debug($"Sexual Gene Thief: {stealer} steals {stolenGene.def} from {victim}");
|
|
|
|
|
|
|
|
|
|
stealer.genes.AddGene(stolenGene.def, AddAsXenogene);
|
|
|
|
|
victim.genes.RemoveGene(stolenGene);
|
2024-07-05 13:04:45 +00:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
victim.Faction != null && stealer.Faction != null
|
|
|
|
|
&& victim.Faction != stealer.Faction
|
|
|
|
|
&& victim.Faction != Faction.OfPlayer) {
|
|
|
|
|
|
|
|
|
|
HistoryEventDef reason = DefDatabase<HistoryEventDef>.GetNamedSilentFail("rjw_genes_GoodwillChangedReason_StoleGene");
|
|
|
|
|
victim.Faction.TryAffectGoodwillWith(stealer.Faction, FACTION_GOODWILL_CHANGE, true, true, reason, victim);
|
|
|
|
|
}
|
2024-07-04 14:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|