RimWorld

RimWorld

Rational Romance 2
Space Moth 12 Feb, 2021 @ 4:58am
Minor Bug Fixes
A friend of mine has been using this mod for a while, and started getting a non-critical error stating "CurStage is null while ShouldDiscard is false on SleptInBedroom for X". This error was happening every tick even while paused so was causing them lag.

I have investigated and found that the issue was caused by a bug in the ThoughtWorker_NoPersonalBedroom_CurrentStateInternal class. I have reworked the class and rebuilt the dll, and it seems to have fixed the issue.

I also took a look at the other issue mentioned with the ThoughtWorker_WantToSleepWithSpouseOrLover_CurrentStateInternal class, and have a possible fix (but have been unable to test)

Here are the two updated functions, if you want to give them a try:

public static class ThoughtWorker_NoPersonalBedroom_CurrentStateInternal { // CHANGE: Allowed for polyamory. public static void Postfix(ref ThoughtState __result, Pawn p) { /Log.Message(p.Name + " Start of bedroom check"); // Check Pawn has a bed, and a bedroom. Also make sure they are not a prisoner. if ((p.ownership.OwnedBed != null) && (p.ownership.OwnedBed.GetRoom() != null) && (p.IsPrisoner == false)) { IEnumerable<Pawn> loveTree = SexualityUtilities.getAllLoverPawnsFirstRemoved(p); bool hasStranger = false; // Check each bed foreach (Building_Bed bed in p.ownership.OwnedBed.GetRoom().ContainedBeds) { // Check each bed owner. foreach (Pawn pawn in bed.OwnersForReading) { if (pawn != null) { // If not a relation or the pawn being queried if ((loveTree.Contains(pawn) == false) && (pawn != p)) { hasStranger = true; break; } } else { // Kinda stranger bed, but we'll let it pass. Not a barracks unless others use it. } } } // The Pawn needs to forget about being in a barracks, because they love everyone. if (hasStranger == false) { // Do they consider that they have slept in any barracks? Thought_Memory memB = p.needs.mood.thoughts.memories.GetFirstMemoryOfDef(ThoughtDefOf.SleptInBarracks); if (memB != null) { // If they dont remember the joys of a bedroom, let them. if (p.needs.mood.thoughts.memories.GetFirstMemoryOfDef(ThoughtDefOf.SleptInBedroom) == null) { p.needs.mood.thoughts.memories.TryGainMemory(ThoughtDefOf.SleptInBedroom); } /* // Not sure what this is for, try and forget the barracks? Thought_Memory mem = p.needs.mood.thoughts.memories.GetFirstMemoryOfDef(ThoughtDefOf.SleptInBedroom); if (mem != null) { if (mem.def.stages.Count >= memB.CurStageIndex) { mem.SetForcedStage(memB.CurStageIndex); } }*/ __result = ThoughtState.Inactive; p.needs.mood.thoughts.memories.RemoveMemoriesOfDef(ThoughtDefOf.SleptInBarracks); } } } /Log.Message(p.Name + " End of bedroom check"); } }

public static class ThoughtWorker_WantToSleepWithSpouseOrLover_CurrentStateInternal { public static void Prefix(ref ThoughtState __result, Pawn p) { if (__result.StageIndex != ThoughtState.Inactive.StageIndex) { if (p.ownership.OwnedBed != null) { //DirectPawnRelation directPawnRelation = LovePartnerRelationUtility.ExistingMostLikedLovePartnerRel(p, false); IEnumerable<Pawn> partners = (from r in p.relations.PotentiallyRelatedPawns where LovePartnerRelationUtility.LovePartnerRelationExists(p, r) select r); if (partners != null) { IEnumerable<Pawn> partnersInBed = (from r in partners where p.ownership.OwnedBed.OwnersForReading.Contains(r) select r); bool multiplePartners = partners.Count() > 1; // If you have at least one partner in bed and have more than one partner, forget about it. if ((partnersInBed != null) && (partnersInBed.Count() > 0) && multiplePartners) { __result = ThoughtState.Inactive; return; } // Or if you are Polyamorous, being in the same room is enough if (p.story.traits.HasTrait(RRRTraitDefOf.Polyamorous)) { if (p.ownership.OwnedBed.GetRoom() != null) { foreach (Building_Bed bed in p.ownership.OwnedBed.GetRoom().ContainedBeds) { foreach (Pawn pawn in bed.OwnersForReading) { if (partners.Contains(pawn)) { __result = ThoughtState.Inactive; break; } } if (!__result.Active) { break; } } } } } } } } }