Simplified virginity checks

Updated names in the SexHistoryComp
This commit is contained in:
amevarashi 2023-04-21 20:19:37 +05:00
parent 67c2328ad6
commit ab485c677f
7 changed files with 83 additions and 86 deletions

View File

@ -13,7 +13,7 @@ namespace RJWSexperience
return false; return false;
IEnumerable<PawnRelationDef> relations = pawn.GetRelations(otherpawn); IEnumerable<PawnRelationDef> relations = pawn.GetRelations(otherpawn);
if (relations.EnumerableNullOrEmpty()) if (relations == null)
return false; return false;
foreach (PawnRelationDef relation in relations) foreach (PawnRelationDef relation in relations)
@ -66,32 +66,31 @@ namespace RJWSexperience
} }
/// <summary> /// <summary>
/// If the pawn is virgin, return true. /// Check if the pawn is virgin
/// </summary> /// </summary>
public static bool IsVirgin(this Pawn pawn) 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
} }
/// <summary> /// <summary>
/// If pawn is virgin, lose his/her virginity. /// Remove virginity if pawn is virgin and announce it
/// </summary> /// </summary>
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); int? removedDegree = Virginity.TraitHandler.RemoveVirginTrait(pawn);
if (pawn.IsVirgin()) if (SexperienceMod.Settings.EnableSexHistory && pawn.IsVirgin())
{ {
pawn.TryGetComp<SexHistory.SexHistoryComp>()?.RecordFirst(partner, props); pawn.TryGetComp<SexHistory.SexHistoryComp>()?.RecordFirst(partner);
if (removedDegree != null)
Messages.Message(Keyed.RS_LostVirgin(pawn.LabelShort, partner.LabelShort), MessageTypeDefOf.NeutralEvent, true);
} }
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); RJWUtility.ThrowVirginHistoryEvent(pawn, partner, props, (int)removedDegree);
}
} }
} }
} }

View File

@ -19,11 +19,22 @@ namespace RJWSexperience.ExtensionMethods
public static bool IsBestiality(this SexProps props) public static bool IsBestiality(this SexProps props)
{ {
if (props.partner != null) if (props.hasPartner())
{ {
return props.pawn.IsAnimal() ^ props.partner.IsAnimal(); return props.pawn.IsAnimal() ^ props.partner.IsAnimal();
} }
return false; return false;
} }
/// <summary>
/// Get a not-so-unique ID. Same interaction between the same partners will return same ID
/// </summary>
public static int GetTempId(this SexProps props)
{
return props.pawn.GetHashCode() ^
(props.partner?.GetHashCode() ?? 0) ^
props.dictionaryKey.GetHashCode() ^
(props.isReceiver ? 0 : 1);
}
} }
} }

View File

@ -11,7 +11,7 @@ using Verse;
namespace RJWSexperience 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 class RJW_Patch_Orgasm
{ {
public static void Postfix(JobDriver_Sex __instance) 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 public static class RJW_Patch_SatisfyPersonal
{ {
private const float base_sat_per_fuck = 0.4f; private const float base_sat_per_fuck = 0.4f;
@ -46,7 +46,7 @@ namespace RJWSexperience
CumUtility.FillCumBuckets(props); CumUtility.FillCumBuckets(props);
props.pawn.records?.Increment(RsDefOf.Record.OrgasmCount); props.pawn.records?.Increment(RsDefOf.Record.OrgasmCount);
if (SexperienceMod.Settings.EnableSexHistory && props.hasPartner()) if (SexperienceMod.Settings.EnableSexHistory && props.hasPartner())
props.pawn.TryGetComp<SexHistoryComp>()?.RecordSatisfaction(props.partner, props, satisfaction); props.pawn.TryGetComp<SexHistoryComp>()?.RecordOrgasm(props.partner, props, satisfaction);
} }
} }
@ -94,10 +94,10 @@ namespace RJWSexperience
{ {
public static void Postfix(JobDriver_SexBaseInitiator __instance) 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.pawn.TryRemoveVirginity(__instance.Partner, __instance.Sexprops);
__instance.Partner.PoptheCherry(__instance.pawn, __instance.Sexprops); __instance.Partner.TryRemoveVirginity(__instance.pawn, __instance.Sexprops);
} }
} }
} }
@ -108,9 +108,7 @@ namespace RJWSexperience
/// <summary> /// <summary>
/// If masturbation and current map has a bucket, return location near the bucket /// If masturbation and current map has a bucket, return location near the bucket
/// </summary> /// </summary>
/// <param name="pawn"></param> /// <param name="__result">The place to stand near a bucket</param>
/// <param name="partner"></param>
/// <param name="__result"></param>
/// <returns>Run original method</returns> /// <returns>Run original method</returns>
public static bool Prefix(Pawn pawn, Pawn partner, ref IntVec3 __result) public static bool Prefix(Pawn pawn, Pawn partner, ref IntVec3 __result)
{ {

View File

@ -6,11 +6,14 @@ using Verse;
namespace RJWSexperience.SexHistory namespace RJWSexperience.SexHistory
{ {
/// <summary>
/// Stores pawn's sex history and handles its updates
/// </summary>
public class SexHistoryComp : ThingComp public class SexHistoryComp : ThingComp
{ {
public const int ARRLEN = 20; public const int ARRLEN = 20;
protected Dictionary<string, SexPartnerHistoryRecord> histories = new Dictionary<string, SexPartnerHistoryRecord>(); protected Dictionary<string, SexPartnerHistoryRecord> partnerRecords = new Dictionary<string, SexPartnerHistoryRecord>();
protected string first = ""; protected string first = "";
protected bool dirty = true; protected bool dirty = true;
protected xxx.rjwSextype recentSex = xxx.rjwSextype.None; protected xxx.rjwSextype recentSex = xxx.rjwSextype.None;
@ -43,17 +46,14 @@ namespace RJWSexperience.SexHistory
protected int bestSexTickAbsCache = 0; protected int bestSexTickAbsCache = 0;
public Gizmo Gizmo { get; private set; } public Gizmo Gizmo { get; private set; }
public Pawn ParentPawn => parent as Pawn; public Pawn ParentPawn => parent as Pawn;
public SexPartnerHistoryRecord FirstPartnerRecord => partnerRecords.TryGetValue(first);
public SexPartnerHistoryRecord GetFirstPartnerHistory => histories.TryGetValue(first); public SexPartnerHistoryRecord MostPartnerRecord
public SexPartnerHistoryRecord GetMostPartnerHistory
{ {
get get
{ {
Update(); Update();
return histories.TryGetValue(mostPartnerCache); return partnerRecords.TryGetValue(mostPartnerCache);
} }
} }
public xxx.rjwSextype MostSextype public xxx.rjwSextype MostSextype
@ -72,13 +72,13 @@ namespace RJWSexperience.SexHistory
return mostSatSextypeCache; return mostSatSextypeCache;
} }
} }
public SexPartnerHistoryRecord GetRecentPartnersHistory => histories.TryGetValue(recentPartner); public SexPartnerHistoryRecord RecentPartnerRecord => partnerRecords.TryGetValue(recentPartner);
public SexPartnerHistoryRecord GetBestSexPartnerHistory public SexPartnerHistoryRecord BestSexPartnerRecord
{ {
get get
{ {
Update(); Update();
return histories.TryGetValue(bestPartnerCache); return partnerRecords.TryGetValue(bestPartnerCache);
} }
} }
public float TotalSexHad public float TotalSexHad
@ -96,21 +96,14 @@ namespace RJWSexperience.SexHistory
{ {
IEnumerable<SexPartnerHistoryRecord> res = Enumerable.Empty<SexPartnerHistoryRecord>(); IEnumerable<SexPartnerHistoryRecord> res = Enumerable.Empty<SexPartnerHistoryRecord>();
Update(); Update();
if (!histories.NullOrEmpty()) if (!partnerRecords.NullOrEmpty())
{ {
res = histories.Values; res = partnerRecords.Values;
} }
return res; return res;
} }
} }
public int PartnerCount public int PartnerCount => partnerRecords?.Count ?? 0;
{
get
{
if (histories == null) histories = new Dictionary<string, SexPartnerHistoryRecord>();
return histories.Count;
}
}
public int IncestuousCount => incestuous; public int IncestuousCount => incestuous;
public int RapedCount public int RapedCount
{ {
@ -160,7 +153,6 @@ namespace RJWSexperience.SexHistory
public int FirstSexTickAbs => firstSexTickAbs; public int FirstSexTickAbs => firstSexTickAbs;
public int MostSexTickAbs => mostSexTickAbsCache; public int MostSexTickAbs => mostSexTickAbsCache;
public int BestSexTickAbs => bestSexTickAbsCache; public int BestSexTickAbs => bestSexTickAbsCache;
public Pawn PreferRacePawn public Pawn PreferRacePawn
{ {
get get
@ -186,9 +178,9 @@ namespace RJWSexperience.SexHistory
public int GetSextypeRecentTickAbs(int sextype) => sextypeRecentTickAbs[sextype]; 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; return float.IsNaN(res) ? 0f : res;
} }
@ -213,7 +205,7 @@ namespace RJWSexperience.SexHistory
sextyperecenttickabssave = new List<int>(); sextyperecenttickabssave = new List<int>();
} }
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 first, "first", string.Empty);
Scribe_Values.Look(ref recentSex, "recentsex", xxx.rjwSextype.None); Scribe_Values.Look(ref recentSex, "recentsex", xxx.rjwSextype.None);
Scribe_Values.Look(ref recentSat, "recentsat", 0); 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 sextypesatsave, "sextypesatsave", LookMode.Value);
Scribe_Collections.Look(ref sextyperecenttickabssave, "sextyperecenttickabssave", LookMode.Value); Scribe_Collections.Look(ref sextyperecenttickabssave, "sextyperecenttickabssave", LookMode.Value);
if (histories == null) if (partnerRecords == null)
histories = new Dictionary<string, SexPartnerHistoryRecord>(); partnerRecords = new Dictionary<string, SexPartnerHistoryRecord>();
if (Scribe.mode == LoadSaveMode.LoadingVars) if (Scribe.mode == LoadSaveMode.LoadingVars)
{ {
@ -237,7 +229,7 @@ namespace RJWSexperience.SexHistory
sextypeSat = sextypesatsave?.ToArray() ?? new float[ARRLEN]; sextypeSat = sextypesatsave?.ToArray() ?? new float[ARRLEN];
sextypeRecentTickAbs = sextyperecenttickabssave?.ToArray() ?? new int[ARRLEN]; sextypeRecentTickAbs = sextyperecenttickabssave?.ToArray() ?? new int[ARRLEN];
foreach (KeyValuePair<string, SexPartnerHistoryRecord> element in histories) foreach (KeyValuePair<string, SexPartnerHistoryRecord> element in partnerRecords)
{ {
element.Value.PartnerID = element.Key; element.Value.PartnerID = element.Key;
} }
@ -247,50 +239,52 @@ namespace RJWSexperience.SexHistory
public void RecordSex(Pawn partner, SexProps props) public void RecordSex(Pawn partner, SexProps props)
{ {
RecordFirst(partner, props); SexPartnerHistoryRecord partnerRecord = GetPartnerRecord(partner);
GetPartnerRecord(partner)?.RecordSex(props); partnerRecord.RecordSex(props);
recentPartner = partner.ThingID; recentPartner = partner.ThingID;
recentSex = props.sexType; recentSex = props.sexType;
sextypeCount[(int)props.sexType]++; sextypeCount[(int)props.sexType]++;
sextypeRecentTickAbs[(int)props.sexType] = GenTicks.TicksAbs; sextypeRecentTickAbs[(int)props.sexType] = GenTicks.TicksAbs;
if (partner.IsIncest(ParentPawn)) incestuous++; if (partnerRecord.Incest) incestuous++;
if (partner.Dead) corpsefuck++; if (partner.Dead) corpsefuck++;
if (props.IsBestiality()) bestiality++; if (props.IsBestiality()) bestiality++;
else if (ParentPawn.def != partner.def) interspecies++; else if (ParentPawn.def != partner.def) interspecies++;
dirty = true; 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).RecordOrgasm(props.sexType, satisfaction);
GetPartnerRecord(partner)?.RecordSatisfaction(props, satisfaction);
recentSat = satisfaction; recentSat = satisfaction;
sextypeSat[(int)props.sexType] += satisfaction; sextypeSat[(int)props.sexType] += satisfaction; // Several orgasmsms in one sex are messing with this
dirty = true; dirty = true;
} }
public void RecordFirst(Pawn partner, SexProps props) /// <summary>
/// Record first partner and time of sex. No checks are performed, the caller should make sure it is the first time.
/// </summary>
/// <param name="partner"></param>
public void RecordFirst(Pawn partner)
{ {
if (VirginCheck() && props.sexType == xxx.rjwSextype.Vaginal) first = partner.ThingID;
{ firstSexTickAbs = GenTicks.TicksAbs;
first = partner.ThingID; partner.TryGetComp<SexHistoryComp>()?.TakeSomeonesVirgin(ParentPawn);
SexHistoryComp history = partner.TryGetComp<SexHistoryComp>();
firstSexTickAbs = GenTicks.TicksAbs;
history?.TakeSomeonesVirgin(ParentPawn);
}
} }
/// <summary>
/// Retrive an existing partner record or add a new one. Increments SexPartnerCount when new record is added
/// </summary>
protected SexPartnerHistoryRecord GetPartnerRecord(Pawn partner) protected SexPartnerHistoryRecord GetPartnerRecord(Pawn partner)
{ {
string partnerId = partner.ThingID; string partnerId = partner.ThingID;
if (histories.TryGetValue(partnerId, out SexPartnerHistoryRecord record)) if (partnerRecords.TryGetValue(partnerId, out SexPartnerHistoryRecord record))
{ {
return record; return record;
} }
SexPartnerHistoryRecord newRecord = new SexPartnerHistoryRecord(partner, partner.IsIncest(ParentPawn)); SexPartnerHistoryRecord newRecord = new SexPartnerHistoryRecord(partner, partner.IsIncest(ParentPawn));
histories.Add(partnerId, newRecord); partnerRecords.Add(partnerId, newRecord);
ParentPawn.records.Increment(RsDefOf.Record.SexPartnerCount); ParentPawn.records.Increment(RsDefOf.Record.SexPartnerCount);
return newRecord; return newRecord;
} }
@ -328,7 +322,7 @@ namespace RJWSexperience.SexHistory
Dictionary<ThingDef, int> racetotalsat = new Dictionary<ThingDef, int>(); Dictionary<ThingDef, int> racetotalsat = new Dictionary<ThingDef, int>();
List<Pawn> allpartners = new List<Pawn>(); List<Pawn> allpartners = new List<Pawn>();
foreach (KeyValuePair<string, SexPartnerHistoryRecord> element in histories) foreach (KeyValuePair<string, SexPartnerHistoryRecord> element in partnerRecords)
{ {
SexPartnerHistoryRecord h = element.Value; SexPartnerHistoryRecord h = element.Value;
@ -386,9 +380,9 @@ namespace RJWSexperience.SexHistory
mostPartnerCache = mostID; mostPartnerCache = mostID;
bestPartnerCache = bestID; bestPartnerCache = bestID;
recentSexTickAbsCache = histories.TryGetValue(recentPartner)?.RecentSexTickAbs ?? 0; recentSexTickAbsCache = partnerRecords.TryGetValue(recentPartner)?.RecentSexTickAbs ?? 0;
mostSexTickAbsCache = histories.TryGetValue(mostPartnerCache)?.RecentSexTickAbs ?? 0; mostSexTickAbsCache = partnerRecords.TryGetValue(mostPartnerCache)?.RecentSexTickAbs ?? 0;
bestSexTickAbsCache = histories.TryGetValue(bestPartnerCache)?.BestSexTickAbs ?? 0; bestSexTickAbsCache = partnerRecords.TryGetValue(bestPartnerCache)?.BestSexTickAbs ?? 0;
racetotalsat.Clear(); racetotalsat.Clear();
allpartners.Clear(); allpartners.Clear();
@ -414,14 +408,6 @@ namespace RJWSexperience.SexHistory
#endregion Cache update #endregion Cache update
protected bool VirginCheck()
{
if (histories.TryGetValue(first) != null)
return false;
return ParentPawn.IsVirgin();
}
public override IEnumerable<Gizmo> CompGetGizmosExtra() public override IEnumerable<Gizmo> CompGetGizmosExtra()
{ {
if (SexperienceMod.Settings.HideGizmoWhenDrafted && ParentPawn.Drafted) if (SexperienceMod.Settings.HideGizmoWhenDrafted && ParentPawn.Drafted)

View File

@ -104,13 +104,13 @@ namespace RJWSexperience.SexHistory
recentSexTickAbs = GenTicks.TicksAbs; recentSexTickAbs = GenTicks.TicksAbs;
} }
public void RecordSatisfaction(SexProps props, float satisfaction) public void RecordOrgasm(xxx.rjwSextype sextype, float satisfaction)
{ {
orgasms++; orgasms++;
if (satisfaction > bestSatisfaction) if (satisfaction > bestSatisfaction)
{ {
bestSextype = props.sexType; bestSextype = sextype;
bestSatisfaction = satisfaction; bestSatisfaction = satisfaction;
bestSexTickAbs = GenTicks.TicksAbs; bestSexTickAbs = GenTicks.TicksAbs;
} }

View File

@ -106,28 +106,28 @@ namespace RJWSexperience.SexHistory.UI
InfoCards.Add(new InfoCard( InfoCards.Add(new InfoCard(
pawn: Pawn, pawn: Pawn,
partnerRecord: _history.GetRecentPartnersHistory, partnerRecord: _history.RecentPartnerRecord,
label: Keyed.RS_Recent_Sex_Partner, label: Keyed.RS_Recent_Sex_Partner,
tooltipLabel: Keyed.RS_Recent_Sex_Partner_ToolTip, tooltipLabel: Keyed.RS_Recent_Sex_Partner_ToolTip,
lastSexTimeTicks: _history.RecentSexTickAbs)); lastSexTimeTicks: _history.RecentSexTickAbs));
InfoCards.Add(new InfoCard( InfoCards.Add(new InfoCard(
pawn: Pawn, pawn: Pawn,
partnerRecord: _history.GetFirstPartnerHistory, partnerRecord: _history.FirstPartnerRecord,
label: Keyed.RS_First_Sex_Partner, label: Keyed.RS_First_Sex_Partner,
tooltipLabel: Keyed.RS_First_Sex_Partner_ToolTip, tooltipLabel: Keyed.RS_First_Sex_Partner_ToolTip,
lastSexTimeTicks: _history.FirstSexTickAbs)); lastSexTimeTicks: _history.FirstSexTickAbs));
InfoCards.Add(new InfoCard( InfoCards.Add(new InfoCard(
pawn: Pawn, pawn: Pawn,
partnerRecord: _history.GetMostPartnerHistory, partnerRecord: _history.MostPartnerRecord,
label: Keyed.RS_Most_Sex_Partner, label: Keyed.RS_Most_Sex_Partner,
tooltipLabel: Keyed.RS_Most_Sex_Partner_ToolTip, tooltipLabel: Keyed.RS_Most_Sex_Partner_ToolTip,
lastSexTimeTicks: _history.MostSexTickAbs)); lastSexTimeTicks: _history.MostSexTickAbs));
InfoCards.Add(new InfoCard( InfoCards.Add(new InfoCard(
pawn: Pawn, pawn: Pawn,
partnerRecord: _history.GetBestSexPartnerHistory, partnerRecord: _history.BestSexPartnerRecord,
label: Keyed.RS_Best_Sex_Partner, label: Keyed.RS_Best_Sex_Partner,
tooltipLabel: Keyed.RS_Best_Sex_Partner_ToolTip, tooltipLabel: Keyed.RS_Best_Sex_Partner_ToolTip,
lastSexTimeTicks: _history.BestSexTickAbs)); lastSexTimeTicks: _history.BestSexTickAbs));

View File

@ -1,5 +1,4 @@
using RimWorld; using RimWorld;
using rjw;
using Verse; using Verse;
namespace RJWSexperience.Virginity namespace RJWSexperience.Virginity
@ -46,6 +45,10 @@ namespace RJWSexperience.Virginity
} }
} }
/// <summary>
/// Remove virginity trait and spawn blood filth if applicable
/// </summary>
/// <returns>Degree of the removed trait</returns>
public static int? RemoveVirginTrait(Pawn pawn) public static int? RemoveVirginTrait(Pawn pawn)
{ {
Trait virgin = pawn.story?.traits?.GetTrait(RsDefOf.Trait.Virgin); Trait virgin = pawn.story?.traits?.GetTrait(RsDefOf.Trait.Virgin);