From ab485c677fda37b2fcad1de8eebeeb5636ef6cd3 Mon Sep 17 00:00:00 2001 From: amevarashi Date: Fri, 21 Apr 2023 20:19:37 +0500 Subject: [PATCH] Simplified virginity checks Updated names in the SexHistoryComp --- .../ExtensionMethods/PawnExtensions.cs | 25 +++-- .../ExtensionMethods/SexPropsExtensions.cs | 13 ++- Source/RJWSexperience/Patches/RJW_Patch.cs | 16 ++- .../SexHistory/SexHistoryComp.cs | 98 ++++++++----------- .../SexHistory/SexPartnerHistoryRecord.cs | 4 +- .../SexHistory/UI/SexStatusViewModel.cs | 8 +- .../RJWSexperience/Virginity/TraitHandler.cs | 5 +- 7 files changed, 83 insertions(+), 86 deletions(-) diff --git a/Source/RJWSexperience/ExtensionMethods/PawnExtensions.cs b/Source/RJWSexperience/ExtensionMethods/PawnExtensions.cs index 2469c21..15f8175 100644 --- a/Source/RJWSexperience/ExtensionMethods/PawnExtensions.cs +++ b/Source/RJWSexperience/ExtensionMethods/PawnExtensions.cs @@ -13,7 +13,7 @@ namespace RJWSexperience return false; IEnumerable relations = pawn.GetRelations(otherpawn); - if (relations.EnumerableNullOrEmpty()) + if (relations == null) return false; foreach (PawnRelationDef relation in relations) @@ -66,32 +66,31 @@ namespace RJWSexperience } /// - /// If the pawn is virgin, return true. + /// Check if the pawn is virgin /// public static bool IsVirgin(this Pawn pawn) { - return pawn.records.GetValue(RsDefOf.Record.VaginalSexCount) == 0; + return pawn.records.GetValue(RsDefOf.Record.VaginalSexCount) == 0 || + pawn.relations?.ChildrenCount > 0; // Male is a virgins unless he stick into vagina? Not sure it should work this way } /// - /// If pawn is virgin, lose his/her virginity. + /// Remove virginity if pawn is virgin and announce it /// - public static void PoptheCherry(this Pawn pawn, Pawn partner, SexProps props) + public static void TryRemoveVirginity(this Pawn pawn, Pawn partner, SexProps props) { - if (props?.sexType != xxx.rjwSextype.Vaginal) - return; - int? removedDegree = Virginity.TraitHandler.RemoveVirginTrait(pawn); - if (pawn.IsVirgin()) + if (SexperienceMod.Settings.EnableSexHistory && pawn.IsVirgin()) { - pawn.TryGetComp()?.RecordFirst(partner, props); - if (removedDegree != null) - Messages.Message(Keyed.RS_LostVirgin(pawn.LabelShort, partner.LabelShort), MessageTypeDefOf.NeutralEvent, true); + pawn.TryGetComp()?.RecordFirst(partner); } - if (removedDegree != null) + if (removedDegree != null && removedDegree != Virginity.TraitDegree.FemaleAfterSurgery) + { + Messages.Message(Keyed.RS_LostVirgin(pawn.LabelShort, partner.LabelShort), MessageTypeDefOf.NeutralEvent, true); RJWUtility.ThrowVirginHistoryEvent(pawn, partner, props, (int)removedDegree); + } } } } diff --git a/Source/RJWSexperience/ExtensionMethods/SexPropsExtensions.cs b/Source/RJWSexperience/ExtensionMethods/SexPropsExtensions.cs index 6749c2b..185b1b3 100644 --- a/Source/RJWSexperience/ExtensionMethods/SexPropsExtensions.cs +++ b/Source/RJWSexperience/ExtensionMethods/SexPropsExtensions.cs @@ -19,11 +19,22 @@ namespace RJWSexperience.ExtensionMethods public static bool IsBestiality(this SexProps props) { - if (props.partner != null) + if (props.hasPartner()) { return props.pawn.IsAnimal() ^ props.partner.IsAnimal(); } return false; } + + /// + /// Get a not-so-unique ID. Same interaction between the same partners will return same ID + /// + public static int GetTempId(this SexProps props) + { + return props.pawn.GetHashCode() ^ + (props.partner?.GetHashCode() ?? 0) ^ + props.dictionaryKey.GetHashCode() ^ + (props.isReceiver ? 0 : 1); + } } } diff --git a/Source/RJWSexperience/Patches/RJW_Patch.cs b/Source/RJWSexperience/Patches/RJW_Patch.cs index d712827..cbb78f6 100644 --- a/Source/RJWSexperience/Patches/RJW_Patch.cs +++ b/Source/RJWSexperience/Patches/RJW_Patch.cs @@ -11,7 +11,7 @@ using Verse; namespace RJWSexperience { - [HarmonyPatch(typeof(JobDriver_Sex), "Orgasm")] + [HarmonyPatch(typeof(JobDriver_Sex), "Orgasm")] // Despite the name, called every tick public static class RJW_Patch_Orgasm { public static void Postfix(JobDriver_Sex __instance) @@ -30,7 +30,7 @@ namespace RJWSexperience } } - [HarmonyPatch(typeof(SexUtility), nameof(SexUtility.SatisfyPersonal))] + [HarmonyPatch(typeof(SexUtility), nameof(SexUtility.SatisfyPersonal))] // Actual on orgasm method public static class RJW_Patch_SatisfyPersonal { private const float base_sat_per_fuck = 0.4f; @@ -46,7 +46,7 @@ namespace RJWSexperience CumUtility.FillCumBuckets(props); props.pawn.records?.Increment(RsDefOf.Record.OrgasmCount); if (SexperienceMod.Settings.EnableSexHistory && props.hasPartner()) - props.pawn.TryGetComp()?.RecordSatisfaction(props.partner, props, satisfaction); + props.pawn.TryGetComp()?.RecordOrgasm(props.partner, props, satisfaction); } } @@ -94,10 +94,10 @@ namespace RJWSexperience { public static void Postfix(JobDriver_SexBaseInitiator __instance) { - if (__instance.Sexprops.hasPartner()) + if (__instance.Sexprops.hasPartner() && __instance.Sexprops.sexType == xxx.rjwSextype.Vaginal) { - __instance.pawn.PoptheCherry(__instance.Partner, __instance.Sexprops); - __instance.Partner.PoptheCherry(__instance.pawn, __instance.Sexprops); + __instance.pawn.TryRemoveVirginity(__instance.Partner, __instance.Sexprops); + __instance.Partner.TryRemoveVirginity(__instance.pawn, __instance.Sexprops); } } } @@ -108,9 +108,7 @@ namespace RJWSexperience /// /// If masturbation and current map has a bucket, return location near the bucket /// - /// - /// - /// + /// The place to stand near a bucket /// Run original method public static bool Prefix(Pawn pawn, Pawn partner, ref IntVec3 __result) { diff --git a/Source/RJWSexperience/SexHistory/SexHistoryComp.cs b/Source/RJWSexperience/SexHistory/SexHistoryComp.cs index 8c103a3..a78a904 100644 --- a/Source/RJWSexperience/SexHistory/SexHistoryComp.cs +++ b/Source/RJWSexperience/SexHistory/SexHistoryComp.cs @@ -6,11 +6,14 @@ using Verse; namespace RJWSexperience.SexHistory { + /// + /// Stores pawn's sex history and handles its updates + /// public class SexHistoryComp : ThingComp { public const int ARRLEN = 20; - protected Dictionary histories = new Dictionary(); + protected Dictionary partnerRecords = new Dictionary(); protected string first = ""; protected bool dirty = true; protected xxx.rjwSextype recentSex = xxx.rjwSextype.None; @@ -43,17 +46,14 @@ namespace RJWSexperience.SexHistory protected int bestSexTickAbsCache = 0; public Gizmo Gizmo { get; private set; } - public Pawn ParentPawn => parent as Pawn; - - public SexPartnerHistoryRecord GetFirstPartnerHistory => histories.TryGetValue(first); - - public SexPartnerHistoryRecord GetMostPartnerHistory + public SexPartnerHistoryRecord FirstPartnerRecord => partnerRecords.TryGetValue(first); + public SexPartnerHistoryRecord MostPartnerRecord { get { Update(); - return histories.TryGetValue(mostPartnerCache); + return partnerRecords.TryGetValue(mostPartnerCache); } } public xxx.rjwSextype MostSextype @@ -72,13 +72,13 @@ namespace RJWSexperience.SexHistory return mostSatSextypeCache; } } - public SexPartnerHistoryRecord GetRecentPartnersHistory => histories.TryGetValue(recentPartner); - public SexPartnerHistoryRecord GetBestSexPartnerHistory + public SexPartnerHistoryRecord RecentPartnerRecord => partnerRecords.TryGetValue(recentPartner); + public SexPartnerHistoryRecord BestSexPartnerRecord { get { Update(); - return histories.TryGetValue(bestPartnerCache); + return partnerRecords.TryGetValue(bestPartnerCache); } } public float TotalSexHad @@ -96,21 +96,14 @@ namespace RJWSexperience.SexHistory { IEnumerable res = Enumerable.Empty(); Update(); - if (!histories.NullOrEmpty()) + if (!partnerRecords.NullOrEmpty()) { - res = histories.Values; + res = partnerRecords.Values; } return res; } } - public int PartnerCount - { - get - { - if (histories == null) histories = new Dictionary(); - return histories.Count; - } - } + public int PartnerCount => partnerRecords?.Count ?? 0; public int IncestuousCount => incestuous; public int RapedCount { @@ -160,7 +153,6 @@ namespace RJWSexperience.SexHistory public int FirstSexTickAbs => firstSexTickAbs; public int MostSexTickAbs => mostSexTickAbsCache; public int BestSexTickAbs => bestSexTickAbsCache; - public Pawn PreferRacePawn { get @@ -186,9 +178,9 @@ namespace RJWSexperience.SexHistory public int GetSextypeRecentTickAbs(int sextype) => sextypeRecentTickAbs[sextype]; - public float GetAVGSat(int index) + public float GetAVGSat(int sextype) { - float res = sextypeSat[index] / sextypeCount[index]; + float res = sextypeSat[sextype] / sextypeCount[sextype]; return float.IsNaN(res) ? 0f : res; } @@ -213,7 +205,7 @@ namespace RJWSexperience.SexHistory sextyperecenttickabssave = new List(); } - Scribe_Collections.Look(ref histories, "histories", LookMode.Value, LookMode.Deep); + Scribe_Collections.Look(ref partnerRecords, "histories", LookMode.Value, LookMode.Deep); Scribe_Values.Look(ref first, "first", string.Empty); Scribe_Values.Look(ref recentSex, "recentsex", xxx.rjwSextype.None); Scribe_Values.Look(ref recentSat, "recentsat", 0); @@ -228,8 +220,8 @@ namespace RJWSexperience.SexHistory Scribe_Collections.Look(ref sextypesatsave, "sextypesatsave", LookMode.Value); Scribe_Collections.Look(ref sextyperecenttickabssave, "sextyperecenttickabssave", LookMode.Value); - if (histories == null) - histories = new Dictionary(); + if (partnerRecords == null) + partnerRecords = new Dictionary(); if (Scribe.mode == LoadSaveMode.LoadingVars) { @@ -237,7 +229,7 @@ namespace RJWSexperience.SexHistory sextypeSat = sextypesatsave?.ToArray() ?? new float[ARRLEN]; sextypeRecentTickAbs = sextyperecenttickabssave?.ToArray() ?? new int[ARRLEN]; - foreach (KeyValuePair element in histories) + foreach (KeyValuePair element in partnerRecords) { element.Value.PartnerID = element.Key; } @@ -247,50 +239,52 @@ namespace RJWSexperience.SexHistory public void RecordSex(Pawn partner, SexProps props) { - RecordFirst(partner, props); - GetPartnerRecord(partner)?.RecordSex(props); + SexPartnerHistoryRecord partnerRecord = GetPartnerRecord(partner); + partnerRecord.RecordSex(props); recentPartner = partner.ThingID; recentSex = props.sexType; sextypeCount[(int)props.sexType]++; sextypeRecentTickAbs[(int)props.sexType] = GenTicks.TicksAbs; - if (partner.IsIncest(ParentPawn)) incestuous++; + if (partnerRecord.Incest) incestuous++; if (partner.Dead) corpsefuck++; if (props.IsBestiality()) bestiality++; else if (ParentPawn.def != partner.def) interspecies++; dirty = true; } - public void RecordSatisfaction(Pawn partner, SexProps props, float satisfaction) + public void RecordOrgasm(Pawn partner, SexProps props, float satisfaction) { - RecordFirst(partner, props); - GetPartnerRecord(partner)?.RecordSatisfaction(props, satisfaction); + GetPartnerRecord(partner).RecordOrgasm(props.sexType, satisfaction); recentSat = satisfaction; - sextypeSat[(int)props.sexType] += satisfaction; + sextypeSat[(int)props.sexType] += satisfaction; // Several orgasmsms in one sex are messing with this dirty = true; } - public void RecordFirst(Pawn partner, SexProps props) + /// + /// Record first partner and time of sex. No checks are performed, the caller should make sure it is the first time. + /// + /// + public void RecordFirst(Pawn partner) { - if (VirginCheck() && props.sexType == xxx.rjwSextype.Vaginal) - { - first = partner.ThingID; - SexHistoryComp history = partner.TryGetComp(); - firstSexTickAbs = GenTicks.TicksAbs; - history?.TakeSomeonesVirgin(ParentPawn); - } + first = partner.ThingID; + firstSexTickAbs = GenTicks.TicksAbs; + partner.TryGetComp()?.TakeSomeonesVirgin(ParentPawn); } + /// + /// Retrive an existing partner record or add a new one. Increments SexPartnerCount when new record is added + /// protected SexPartnerHistoryRecord GetPartnerRecord(Pawn partner) { string partnerId = partner.ThingID; - if (histories.TryGetValue(partnerId, out SexPartnerHistoryRecord record)) + if (partnerRecords.TryGetValue(partnerId, out SexPartnerHistoryRecord record)) { return record; } SexPartnerHistoryRecord newRecord = new SexPartnerHistoryRecord(partner, partner.IsIncest(ParentPawn)); - histories.Add(partnerId, newRecord); + partnerRecords.Add(partnerId, newRecord); ParentPawn.records.Increment(RsDefOf.Record.SexPartnerCount); return newRecord; } @@ -328,7 +322,7 @@ namespace RJWSexperience.SexHistory Dictionary racetotalsat = new Dictionary(); List allpartners = new List(); - foreach (KeyValuePair element in histories) + foreach (KeyValuePair element in partnerRecords) { SexPartnerHistoryRecord h = element.Value; @@ -386,9 +380,9 @@ namespace RJWSexperience.SexHistory mostPartnerCache = mostID; bestPartnerCache = bestID; - recentSexTickAbsCache = histories.TryGetValue(recentPartner)?.RecentSexTickAbs ?? 0; - mostSexTickAbsCache = histories.TryGetValue(mostPartnerCache)?.RecentSexTickAbs ?? 0; - bestSexTickAbsCache = histories.TryGetValue(bestPartnerCache)?.BestSexTickAbs ?? 0; + recentSexTickAbsCache = partnerRecords.TryGetValue(recentPartner)?.RecentSexTickAbs ?? 0; + mostSexTickAbsCache = partnerRecords.TryGetValue(mostPartnerCache)?.RecentSexTickAbs ?? 0; + bestSexTickAbsCache = partnerRecords.TryGetValue(bestPartnerCache)?.BestSexTickAbs ?? 0; racetotalsat.Clear(); allpartners.Clear(); @@ -414,14 +408,6 @@ namespace RJWSexperience.SexHistory #endregion Cache update - protected bool VirginCheck() - { - if (histories.TryGetValue(first) != null) - return false; - - return ParentPawn.IsVirgin(); - } - public override IEnumerable CompGetGizmosExtra() { if (SexperienceMod.Settings.HideGizmoWhenDrafted && ParentPawn.Drafted) diff --git a/Source/RJWSexperience/SexHistory/SexPartnerHistoryRecord.cs b/Source/RJWSexperience/SexHistory/SexPartnerHistoryRecord.cs index 12c0f7d..9bcc832 100644 --- a/Source/RJWSexperience/SexHistory/SexPartnerHistoryRecord.cs +++ b/Source/RJWSexperience/SexHistory/SexPartnerHistoryRecord.cs @@ -104,13 +104,13 @@ namespace RJWSexperience.SexHistory recentSexTickAbs = GenTicks.TicksAbs; } - public void RecordSatisfaction(SexProps props, float satisfaction) + public void RecordOrgasm(xxx.rjwSextype sextype, float satisfaction) { orgasms++; if (satisfaction > bestSatisfaction) { - bestSextype = props.sexType; + bestSextype = sextype; bestSatisfaction = satisfaction; bestSexTickAbs = GenTicks.TicksAbs; } diff --git a/Source/RJWSexperience/SexHistory/UI/SexStatusViewModel.cs b/Source/RJWSexperience/SexHistory/UI/SexStatusViewModel.cs index 2a80835..e7b3538 100644 --- a/Source/RJWSexperience/SexHistory/UI/SexStatusViewModel.cs +++ b/Source/RJWSexperience/SexHistory/UI/SexStatusViewModel.cs @@ -106,28 +106,28 @@ namespace RJWSexperience.SexHistory.UI InfoCards.Add(new InfoCard( pawn: Pawn, - partnerRecord: _history.GetRecentPartnersHistory, + partnerRecord: _history.RecentPartnerRecord, label: Keyed.RS_Recent_Sex_Partner, tooltipLabel: Keyed.RS_Recent_Sex_Partner_ToolTip, lastSexTimeTicks: _history.RecentSexTickAbs)); InfoCards.Add(new InfoCard( pawn: Pawn, - partnerRecord: _history.GetFirstPartnerHistory, + partnerRecord: _history.FirstPartnerRecord, label: Keyed.RS_First_Sex_Partner, tooltipLabel: Keyed.RS_First_Sex_Partner_ToolTip, lastSexTimeTicks: _history.FirstSexTickAbs)); InfoCards.Add(new InfoCard( pawn: Pawn, - partnerRecord: _history.GetMostPartnerHistory, + partnerRecord: _history.MostPartnerRecord, label: Keyed.RS_Most_Sex_Partner, tooltipLabel: Keyed.RS_Most_Sex_Partner_ToolTip, lastSexTimeTicks: _history.MostSexTickAbs)); InfoCards.Add(new InfoCard( pawn: Pawn, - partnerRecord: _history.GetBestSexPartnerHistory, + partnerRecord: _history.BestSexPartnerRecord, label: Keyed.RS_Best_Sex_Partner, tooltipLabel: Keyed.RS_Best_Sex_Partner_ToolTip, lastSexTimeTicks: _history.BestSexTickAbs)); diff --git a/Source/RJWSexperience/Virginity/TraitHandler.cs b/Source/RJWSexperience/Virginity/TraitHandler.cs index 31e6a5e..21cc148 100644 --- a/Source/RJWSexperience/Virginity/TraitHandler.cs +++ b/Source/RJWSexperience/Virginity/TraitHandler.cs @@ -1,5 +1,4 @@ using RimWorld; -using rjw; using Verse; namespace RJWSexperience.Virginity @@ -46,6 +45,10 @@ namespace RJWSexperience.Virginity } } + /// + /// Remove virginity trait and spawn blood filth if applicable + /// + /// Degree of the removed trait public static int? RemoveVirginTrait(Pawn pawn) { Trait virgin = pawn.story?.traits?.GetTrait(RsDefOf.Trait.Virgin);