using RimWorld; using rjw; using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using Verse; using Verse.AI; namespace RJW_Menstruation { public static class MenstruationUtility { [Obsolete("This method is obsolete. Use GetMenstruationComps or a related function instead", false)] public static HediffComp_Menstruation GetMenstruationComp(this Pawn pawn) { return pawn.GetFirstMenstruationComp(); } public static IEnumerable GetMenstruationComps(this Pawn pawn) { List hedifflist = Genital_Helper.get_PartsHediffList(pawn, Genital_Helper.get_genitalsBPR(pawn))?.FindAll(h => VariousDefOf.AllVaginas.Contains(h.def)); if (hedifflist == null) yield break; foreach (Hediff hediff in hedifflist) { HediffComp_Menstruation result = hediff.TryGetComp(); if (result != null) yield return result; } } public static HediffComp_Menstruation GetFirstMenstruationComp(this Pawn pawn) { return pawn.GetMenstruationComps().FirstOrDefault(); } public static HediffComp_Menstruation GetRandomMenstruationComp(this Pawn pawn) { return pawn.GetMenstruationComps().RandomElementWithFallback(); } public static HediffComp_Menstruation GetFertileMenstruationComp(this Pawn pawn) { List comps = pawn.GetMenstruationComps().ToList(); return comps.Where(c => c.IsDangerDay).RandomElementWithFallback() ?? comps.RandomElementWithFallback(); } public static HediffComp_Menstruation GetMenstruationComp(this Hediff vagina) { if (VariousDefOf.AllVaginas.Contains(vagina?.def)) { return vagina.TryGetComp(); } return null; } public static HediffComp_Menstruation GetMenstruationComp(this Hediff_BasePregnancy pregnancy) { return pregnancy?.pawn.GetMenstruationComps().FirstOrDefault(comp => comp.Pregnancy == pregnancy); } public static HediffComp_Anus GetAnusComp(this Pawn pawn) { List hedifflist = Genital_Helper.get_PartsHediffList(pawn, Genital_Helper.get_genitalsBPR(pawn))?.FindAll((Hediff h) => h.def.defName.ToLower().Contains("anus")); HediffComp_Anus result; if (!hedifflist.NullOrEmpty()) { foreach (Hediff h in hedifflist) { result = h.TryGetComp(); if (result != null) return result; } } return null; } public static HediffComp_Anus GetAnusComp(this Hediff hediff) { if (hediff is Hediff_PartBaseNatural || hediff is Hediff_PartBaseArtifical) { return hediff.TryGetComp(); } return null; } public static float GetFertilityChance(this HediffComp_Menstruation comp) { return 1.0f - Mathf.Pow(1.0f - Configurations.FertilizeChance, comp.TotalFertCum * comp.Props.basefertilizationChanceFactor); } public static Texture2D GetPregnancyIcon(this HediffComp_Menstruation comp, Hediff hediff) { string icon = ""; int babycount = 1; if (hediff is Hediff_MechanoidPregnancy) { return ContentFinder.Get(("Womb/Mechanoid_Fluid"), true); } else if (hediff is Hediff_BasePregnancy h) { babycount = h.babies.Count; string fetustex = h.babies?.FirstOrDefault()?.def.GetModExtension()?.fetusTexPath ?? "Fetus/Fetus_Default"; if (h.GestationProgress < 0.2f) icon = comp.WombTex + "_Implanted"; else if (h.GestationProgress < 0.3f) { if (h.babies?.First()?.def?.race?.FleshType == FleshTypeDefOf.Insectoid) icon += "Fetus/Insects/Insect_Early00"; else icon += "Fetus/Fetus_Early00"; } else if (h.GestationProgress < 0.4f) icon += fetustex + "00"; else if (h.GestationProgress < 0.5f) icon += fetustex + "01"; else if (h.GestationProgress < 0.6f) icon += fetustex + "02"; else if (h.GestationProgress < 0.7f) icon += fetustex + "03"; else if (h.GestationProgress < 0.8f) icon += fetustex + "04"; else icon += fetustex + "05"; } else icon = "Fetus/Slime_Abomi02"; Texture2D result = TryGetTwinsIcon(icon, babycount); if (result == null) result = ContentFinder.Get((icon), true); return result; } public static Texture2D TryGetTwinsIcon(string path, int babycount) { for (int i = babycount; i > 1; i--) { Texture2D result = ContentFinder.Get(path + "_Multiplet_" + i, false); if (result != null) return result; } return null; } public static Texture2D GetCumIcon(this HediffComp_Menstruation comp) { string icon = comp.WombTex; float cumpercent = comp.TotalCumPercent; if (cumpercent < 0.001f) return ContentFinder.Get("Womb/Empty", true); else if (cumpercent < 0.01f) icon += "_Cum_00"; else if (cumpercent < 0.05f) icon += "_Cum_01"; else if (cumpercent < 0.11f) icon += "_Cum_02"; else if (cumpercent < 0.17f) icon += "_Cum_03"; else if (cumpercent < 0.23f) icon += "_Cum_04"; else if (cumpercent < 0.29f) icon += "_Cum_05"; else if (cumpercent < 0.35f) icon += "_Cum_06"; else if (cumpercent < 0.41f) icon += "_Cum_07"; else if (cumpercent < 0.47f) icon += "_Cum_08"; else if (cumpercent < 0.53f) icon += "_Cum_09"; else if (cumpercent < 0.59f) icon += "_Cum_10"; else if (cumpercent < 0.65f) icon += "_Cum_11"; else if (cumpercent < 0.71f) icon += "_Cum_12"; else if (cumpercent < 0.77f) icon += "_Cum_13"; else if (cumpercent < 0.83f) icon += "_Cum_14"; else if (cumpercent < 0.89f) icon += "_Cum_15"; else if (cumpercent < 0.95f) icon += "_Cum_16"; else icon += "_Cum_17"; Texture2D cumtex = ContentFinder.Get((icon), true); return cumtex; } public static Texture2D GetInsectEggedIcon(this HediffComp_Menstruation comp) { List hediffs = comp.Pawn.health.hediffSet.GetHediffs().ToList(); if (hediffs.NullOrEmpty()) return null; string path = "Womb/Insect_Egged/"; if (hediffs.Max(hediff => hediff.eggssize) > 0.3) // The threshold for "large egg" in the debug { if (hediffs.Count == 1) return ContentFinder.Get(path + "Womb_Egged_Large", true); else return ContentFinder.Get(path + "Womb_Egged_ManyMixed", true); } Texture2D result = ContentFinder.Get(path + "Womb_Egged_" + hediffs.Count(), false); if (result == null) result = ContentFinder.Get(path + "Womb_Egged_Many", true); return result; } public static Texture2D GetWombIcon(this HediffComp_Menstruation comp) { Texture2D wombtex = comp.GetInsectEggedIcon(); if (wombtex != null) return wombtex; string icon = comp.WombTex; HediffComp_Menstruation.Stage stage = comp.curStage; if (stage == HediffComp_Menstruation.Stage.Bleeding || stage == HediffComp_Menstruation.Stage.ClimactericBleeding) icon += "_Bleeding"; wombtex = ContentFinder.Get(icon, true); return wombtex; } public static Texture2D GetEggIcon(this HediffComp_Menstruation comp, bool includeOvary) { switch (comp.CurrentVisibleStage) { case HediffComp_Menstruation.Stage.Follicular: case HediffComp_Menstruation.Stage.ClimactericFollicular: if (!includeOvary) break; if (comp is HediffComp_InducedOvulator) { if (comp.Pawn.jobs.curDriver is JobDriver_Sex job && job.Sexprops != null && !job.Sexprops.usedCondom && (job.Sexprops.sexType == xxx.rjwSextype.Vaginal || job.Sexprops.sexType == xxx.rjwSextype.DoublePenetration)) return ContentFinder.Get("Ovaries/Ovary_01", true); else break; } if (comp.curStageHrs > comp.CurStageIntervalHours - 30) // Approximate time for ovulation to occur return ContentFinder.Get("Ovaries/Ovary_01", true); else break; case HediffComp_Menstruation.Stage.Ovulatory: if (!includeOvary) break; return ContentFinder.Get("Ovaries/Ovary_02", true); case HediffComp_Menstruation.Stage.Luteal: case HediffComp_Menstruation.Stage.ClimactericLuteal: if (!comp.IsEggExist) break; int fertstage = comp.IsFertilized; if (fertstage >= 0) { if (fertstage <= Configurations.CycleAcceleration) return ContentFinder.Get("Eggs/Egg_Fertilizing02", true); else if (fertstage <= 18) return ContentFinder.Get("Eggs/Egg_Fertilized00", true); else if (fertstage <= 54) return ContentFinder.Get("Eggs/Egg_Fertilized01", true); else return ContentFinder.Get("Eggs/Egg_Fertilized02", true); } else if (comp.IsEggFertilizing) { if (comp.GetFertilityChance() < 0.5f) return ContentFinder.Get("Eggs/Egg_Fertilizing00", true); else return ContentFinder.Get("Eggs/Egg_Fertilizing01", true); } else return ContentFinder.Get("Eggs/Egg", true); case HediffComp_Menstruation.Stage.Pregnant: if (comp.Pregnancy is Hediff_MechanoidPregnancy) break; else if (comp.GetPregnancyProgress() < 0.2f) return ContentFinder.Get("Eggs/Egg_Implanted00", true); else break; } return ContentFinder.Get("Womb/Empty", true); } public static void DrawEggOverlay(this HediffComp_Menstruation comp, Rect wombRect, bool includeOvary) { Rect rect = new Rect(wombRect.xMax - wombRect.width / 3, wombRect.y, wombRect.width / 3, wombRect.width / 3); GUI.color = Color.white; GUI.DrawTexture(rect, comp.GetEggIcon(includeOvary), ScaleMode.ScaleToFit); } public static Texture2D GetGenitalIcon(this Pawn pawn, HediffComp_Menstruation comp, bool drawOrigin = false) { Hediff hediff = comp?.parent; if (hediff == null) return ContentFinder.Get("Genitals/Vagina00", true); //HediffComp_Menstruation comp = hediff.GetMenstruationComp(); string icon; float severity; if (drawOrigin) severity = comp.OriginVagSize; else severity = hediff.Severity; if (comp != null) icon = comp.VagTex; else icon = "Genitals/Vagina"; if (severity < 0.20f) icon += "00"; //micro else if (severity < 0.30f) icon += "01"; //tight else if (severity < 0.40f) icon += "02"; //tight else if (severity < 0.47f) icon += "03"; //average else if (severity < 0.53f) icon += "04"; //average else if (severity < 0.60f) icon += "05"; //average else if (severity < 0.70f) icon += "06"; //accomodating else if (severity < 0.80f) icon += "07"; //accomodating else if (severity < 0.87f) icon += "08"; //cavernous else if (severity < 0.94f) icon += "09"; //cavernous else if (severity < 1.01f) icon += "10"; //cavernous else icon += "11"; //abyssal return ContentFinder.Get((icon), true); } public static Texture2D GetAnalIcon(this Pawn pawn, bool drawOrigin = false) { Hediff hediff = Genital_Helper.get_PartsHediffList(pawn, Genital_Helper.get_anusBPR(pawn)).FirstOrDefault(h => VariousDefOf.AllAnuses.Contains(h.def)); if (hediff == null) hediff = Genital_Helper.get_PartsHediffList(pawn, Genital_Helper.get_anusBPR(pawn)).FirstOrDefault(h => h.def.defName.ToLower().Contains("anus")); if (hediff == null) return ContentFinder.Get(("Genitals/Anal00"), true); string icon; float severity; HediffComp_Anus comp = hediff.GetAnusComp(); if (comp != null) { CompProperties_Anus Props = (CompProperties_Anus)comp.props; icon = Props.analTex ?? "Genitals/Anal"; if (drawOrigin) severity = comp.OriginAnusSize; else severity = hediff.Severity; } else { icon = "Genitals/Anal"; severity = hediff.Severity; } if (severity < 0.20f) icon += "00"; //micro else if (severity < 0.40f) icon += "01"; //tight else if (severity < 0.60f) icon += "02"; //average else if (severity < 0.80f) icon += "03"; //accomodating else if (severity < 1.01f) icon += "04"; //cavernous else icon += "05"; //abyssal return ContentFinder.Get((icon), true); } public static float GestationHours(this Hediff_BasePregnancy hediff) { if (hediff == null) { Log.Error("Tried to get gestation length without a pregnancy."); return 1f; } else return (hediff.p_end_tick - hediff.p_start_tick) / GenDate.TicksPerHour; } public static float RandomVariabilityPercent(int recursion = 0) { // Humans, in days const float mean = 1.635f; const float stddev = 0.9138f; const float lambda = 0.234f; if (recursion >= 10) return mean / (28 * 2); float variability = Rand.Gaussian(mean, stddev) - Mathf.Log(Rand.Value) / lambda; variability /= 28 * 2; // Convert to percentage if (variability < 0 || variability > 0.35f) return RandomVariabilityPercent(recursion + 1); // ~2% chance, about the limit on how far variability can go before things start to break else return variability; } public static bool IsInEstrus(this Pawn pawn, bool visible = true) { if (pawn.Dead) return false; return pawn.health?.hediffSet?.HasHediff(visible ? VariousDefOf.Hediff_Estrus : VariousDefOf.Hediff_Estrus_Concealed) ?? false; } public static HediffComp_Menstruation.EstrusLevel HighestEstrus(this Pawn pawn) { HediffComp_Menstruation.EstrusLevel res = HediffComp_Menstruation.EstrusLevel.None; foreach(HediffComp_Menstruation comp in pawn.GetMenstruationComps()) { switch (comp.GetEstrusLevel()) { case HediffComp_Menstruation.EstrusLevel.None: break; case HediffComp_Menstruation.EstrusLevel.Concealed: res = HediffComp_Menstruation.EstrusLevel.Concealed; break; case HediffComp_Menstruation.EstrusLevel.Visible: return HediffComp_Menstruation.EstrusLevel.Visible; } } return res; } } }