using System.Collections.Generic; using System.Linq; using Verse; using Verse.AI; using Verse.AI.Group; using RimWorld; using rjw; using RJWSexperience.Ideology; using UnityEngine; namespace Rimworld_Animations_Patch { public static class SexInteractionUtility { public static bool PawnCaughtLovinByWitness(Pawn pawn, Pawn witness) { if (witness == null || pawn == witness || witness.AnimalOrWildMan() || witness.RaceProps.IsMechanoid || witness.Awake() == false || witness.CanSee(pawn) == false) { return false; } if (pawn.IsHavingSex() == false && pawn.IsMasturbating() == false) { return false; } List sexParticipants = pawn.GetAllSexParticipants(); bool witnessIsCourtingSexParticipant = witness.jobs.curDriver is JobDriver_SexBaseInitiator && sexParticipants.Contains((witness.jobs.curDriver as JobDriver_SexBaseInitiator).Partner); if (sexParticipants.Contains(witness) || witnessIsCourtingSexParticipant) { return false; } return true; } public static bool PawnIsCheatingOnPartner(Pawn pawn, Pawn partner) { if (BasicSettings.worryAboutInfidelity == false || pawn.IsMasturbating() || pawn.IsHavingSex() == false || pawn.GetAllSexParticipants().Contains(partner)) { return false; } if (pawn.GetAllSexParticipants().Any(x => pawn.GetSpouseCount(false) > 0 && pawn.GetSpouses(false).Contains(x))) { return false; } if (pawn.GetSexPartner().Dead || pawn.GetSexPartner().IsAnimal()) { return false; } return partner.IsLoverOfOther(pawn) && pawn.HasTrait("Polygamist") == false && partner.HasTrait("Polygamist") == false; } public static bool InvitePasserbyForSex(Pawn passerby, List participants) { if (passerby == null || participants.NullOrEmpty() || participants.Contains(passerby) || passerby.AnimalOrWildMan() || passerby.RaceProps.IsMechanoid || passerby.Awake() == false || participants.All(x => x.CanSee(passerby) == false)) { return false; } if (participants.Any(x => x.IsForbidden(passerby) || x.HostileTo(passerby) || PawnIsCheatingOnPartner(x, passerby)) || CasualSex_Helper.CanHaveSex(passerby) == false || xxx.IsTargetPawnOkay(passerby) == false || participants.Count > 2) { return false; } if (passerby.MentalState != null || passerby.jobs.curDriver is JobDriver_Flee || passerby.jobs.curDriver is JobDriver_AttackMelee || passerby.jobs.curDriver is JobDriver_Vomit) { return false; } if (SexUtility.ReadyForHookup(passerby) && (passerby?.jobs?.curJob == null || (passerby.jobs.curJob.playerForced == false && CasualSex_Helper.quickieAllowedJobs.Contains(passerby.jobs.curJob.def))) && participants.Any(x => SexAppraiser.would_fuck(x, passerby) > 0.1f && SexAppraiser.would_fuck(passerby, x) > 0.1f) && participants.All(x => SexAppraiser.would_fuck(x, passerby, false, false, true) > 0.1f && SexAppraiser.would_fuck(passerby, x, false, false, true) > 0.1f)) { return true; } return false; } public static ThoughtDef GetThoughtsAboutSexAct(Pawn pawn, JobDriver_Sex jobDriver, out Precept precept) { ThoughtDef thoughtDef = null; precept = null; if (pawn == null || jobDriver == null) { return null; } bool sexIsNecro = jobDriver.Partner != null && jobDriver.Partner.Dead; bool sexIsBeastial = jobDriver.Partner != null && jobDriver.Partner.RaceProps.Animal; bool sexIsRape = sexIsBeastial == false && sexIsNecro == false && (jobDriver is JobDriver_Rape || jobDriver is JobDriver_RapeEnemy || jobDriver is JobDriver_SexBaseRecieverRaped); bool sexIsSlaveRape = sexIsRape && (jobDriver.Partner.IsPrisoner || jobDriver.Partner.IsSlave); bool sexIsXeno = jobDriver.Partner != null && jobDriver.Partner.def.defName != jobDriver.pawn.def.defName; bool isXenophobe = pawn.HasTrait("Xenophobia") && pawn.story.traits.DegreeOfTrait(DefDatabase.GetNamedSilentFail("Xenophobia")) > 0; bool isXenophile = pawn.HasTrait("Xenophobia") && pawn.story.traits.DegreeOfTrait(DefDatabase.GetNamedSilentFail("Xenophobia")) < 0; if (BasicSettings.worryAboutNecro && sexIsNecro && xxx.is_necrophiliac(pawn) == false) { thoughtDef = xxx.is_necrophiliac(pawn) ? DefDatabase.GetNamedSilentFail("SawNecrophilia_Honorable") : pawn.HasPreceptForIssue("Necrophilia", out precept) ? DefDatabase.GetNamedSilentFail("Saw" + precept.def.defName) : DefDatabase.GetNamedSilentFail("SawNecrophilia_Abhorrent"); } else if (BasicSettings.worryAboutBeastiality && sexIsBeastial) { thoughtDef = xxx.is_zoophile(pawn) ? DefDatabase.GetNamedSilentFail("SawBeastility_Honorable") : pawn.HasPreceptForIssue("Beastility", out precept) ? DefDatabase.GetNamedSilentFail("Saw" + precept.def.defName) : DefDatabase.GetNamedSilentFail("SawBeastility_Abhorrent"); } else if (BasicSettings.worryAboutRape && BasicSettings.ignoreSlaveRape == false && sexIsSlaveRape) { thoughtDef = xxx.is_rapist(pawn) ? DefDatabase.GetNamedSilentFail("SawRape_Honorable") : pawn.HasPreceptForIssue("Rape", out precept) ? DefDatabase.GetNamedSilentFail("Saw" + precept.def.defName) : DefDatabase.GetNamedSilentFail("SawRape_Abhorrent"); } else if (BasicSettings.worryAboutRape && sexIsRape) { thoughtDef = xxx.is_rapist(pawn) ? DefDatabase.GetNamedSilentFail("SawRape_Honorable") : pawn.HasPreceptForIssue("Rape", out precept) ? DefDatabase.GetNamedSilentFail("Saw" + precept.def.defName) : DefDatabase.GetNamedSilentFail("SawRape_Abhorrent"); } else if (BasicSettings.worryAboutXeno && sexIsXeno) { thoughtDef = isXenophobe ? DefDatabase.GetNamedSilentFail("SawHAR_AlienDating_Prohibited") : isXenophile ? DefDatabase.GetNamedSilentFail("SawHAR_AlienDating_Honorable") : pawn.HasPreceptForIssue("HAR_AlienDating", out precept) ? DefDatabase.GetNamedSilentFail("Saw" + precept.def.defName) : DefDatabase.GetNamedSilentFail("SawHAR_AlienDating_Acceptable"); } //DebugMode.Message("Sex job is: " + jobDriver + " Issue is: " + (precept?.def?.issue?.defName).ToStringSafe() + " Opinion is: " + (precept?.def?.defName).ToStringSafe() + " Thought is: " + (thoughtDef?.defName).ToStringSafe()); return thoughtDef; } public static void TriggerReactionInWitness(Pawn witness, Pawn otherPawn, string reaction) { if (BasicSettings.majorTabooCanStartFights == false || reaction.NullOrEmpty()) { return; } if (witness.MentalState != null || witness.jobs.curDriver is JobDriver_Flee || witness.jobs.curDriver is JobDriver_AttackMelee || witness.jobs.curDriver is JobDriver_Vomit) { return; } // Panicked if (reaction == "Panicked" || (reaction == "Indignant" && Random.value <= 0.5f)) { // Fight if (otherPawn.RaceProps.Humanlike && witness.RaceProps.Humanlike && witness.DislikesViolence() == false && (Random.value <= 0.2f || witness.EnjoysViolence()) && witness.HostileTo(otherPawn) == false && InteractionUtility.TryGetRandomVerbForSocialFight(witness, out Verb verbToUse)) { witness.interactions.StartSocialFight(otherPawn, "MessageSocialFight"); } // Flight else { Job job = JobMaker.MakeJob(JobDefOf.FleeAndCower, CellFinderLoose.GetFleeDest(witness, new List() { otherPawn }, 24f), otherPawn); witness.jobs.EndCurrentJob(JobCondition.InterruptForced, false, false); witness.jobs.StartJob(job); } } // Vomit else if (reaction == "Nauseated") { Job jobVomit = JobMaker.MakeJob(JobDefOf.Vomit); Job jobFlee = JobMaker.MakeJob(JobDefOf.FleeAndCower, CellFinderLoose.GetFleeDest(witness, new List() { otherPawn }, 24f), otherPawn); witness.jobs.EndCurrentJob(JobCondition.InterruptForced, false, false); if (Random.value <= 0.2f) { witness.jobs.StartJob(jobVomit); witness.jobs.jobQueue.EnqueueFirst(jobFlee); } else { witness.jobs.StartJob(jobFlee); } } // Indignant else if (reaction == "Indignant") { witness.mindState.mentalStateHandler.TryStartMentalState(DefDatabase.GetNamedSilentFail("TargetedInsultingSpree"), null, true, false, null, true, false, false); (witness.mindState.mentalStateHandler.CurState as MentalState_TargetedInsultingSpree).target = otherPawn; } } public static bool ResolveThoughtsForWhenSexIsWitnessed(Pawn pawn, Pawn witness, out bool witnessJoiningSex) { witnessJoiningSex = Random.value < BasicSettings.chanceForOtherToJoinInSex && InvitePasserbyForSex(witness, pawn.GetAllSexParticipants()); // Exit clauses if (witness.AnimalOrWildMan() || witness.RaceProps.IsMechanoid || witness.Dead || witness.Faction == null || witness.Faction.HostileTo(Faction.OfPlayer)) { return false; } // Get basic thoughts string pawnThoughtDefName = pawn.IsMasturbating() ? "SeenMasturbating" : "SeenHavingSex"; string witnessThoughtDefName = pawn.IsMasturbating() ? "SawMasturbation" : "SawSex"; ThoughtDef pawnThoughtDef = BasicSettings.needPrivacy ? DefDatabase.GetNamedSilentFail(pawnThoughtDefName) : null; ThoughtDef witnessThoughtDef = BasicSettings.needPrivacy ? DefDatabase.GetNamedSilentFail(witnessThoughtDefName) : null; // Exhibitionist pawn if (xxx.has_quirk(pawn, "Exhibitionist")) { pawnThoughtDef = DefDatabase.GetNamedSilentFail(pawnThoughtDefName + "Exhibitionist"); } // Voyeuristic witness if (xxx.has_quirk(witness, "Voyeur")) { witnessThoughtDef = DefDatabase.GetNamedSilentFail(witnessThoughtDefName + "Voyeur"); } // Mediating cirumstances bool sexIsRitual = pawn.GetLord() != null && pawn.GetLord().LordJob is LordJob_Ritual && witness?.Ideo == pawn?.Ideo; bool sexIsParty = pawn.GetLord() != null && pawn.GetLord().LordJob is LordJob_Joinable_Party; bool pawnIsVictim = pawn.CurJob.def == xxx.gettin_raped || pawn.Dead; bool pawnIsCheating = sexIsRitual == false && sexIsParty == false && pawnIsVictim == false && PawnIsCheatingOnPartner(pawn, witness); // Wipe thoughts if pawn is a victim if (pawnIsVictim) { pawnThoughtDef = null; witnessThoughtDef = null; } // Add thought if pawn is cheating if (pawnIsCheating) { if (pawn.needs.mood.thoughts.memories.GetFirstMemoryOfDef(DefDatabase.GetNamedSilentFail("CaughtCheating")) == null) { pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase.GetNamedSilentFail("CaughtCheating"), witness); } if (witness.needs.mood.thoughts.memories.GetFirstMemoryOfDef(ThoughtDefOf.CheatedOnMe) == null) { witness.needs.mood.thoughts.memories.TryGainMemory(ThoughtDefOf.CheatedOnMe, pawn); } witnessJoiningSex = false; } // Determine if there are any issues with the sex event and the witness' morals Precept precept = null; if (sexIsRitual == false && sexIsParty == false && pawnIsVictim == false) { witnessThoughtDef = GetThoughtsAboutSexAct(witness, pawn.jobs.curDriver as JobDriver_Sex, out precept); } // Apply thoughts to witness if (witnessThoughtDef != null) { witness.needs.mood.thoughts.memories.TryGainMemory(witnessThoughtDef, pawn, precept); if (witnessThoughtDef.stages[0].baseMoodEffect < 0) { witness?.TryGetComp()?.TryToExclaim(); } witnessJoiningSex = witnessThoughtDef.hediff != null ? false : witnessJoiningSex; } // Extreme reaction if (witnessThoughtDef?.hediff != null) { TriggerReactionInWitness(witness, pawn, witnessThoughtDef.hediff.defName); } else if (pawnIsCheating) { TriggerReactionInWitness(witness, pawn, "Indignant"); } // Apply thoughts to pawn if (pawnThoughtDef != null) { pawn.needs.mood.thoughts.memories.TryGainMemory(pawnThoughtDef, witness, null); if (pawnThoughtDef.stages[0].baseMoodEffect < 0) { pawn?.TryGetComp()?.TryToExclaim(); } } return witnessJoiningSex; } } }