public static InterestDef GetInterest(InterestList interests, SkillRecord skill, Pawn pawn) { float totalWeight = -1; float random = -1; InterestDef selected = null; try { totalWeight = SumWeights(interests, skill, pawn); if (totalWeight == 0) { return(interests[0]); } random = Rand.Range(0.0f, totalWeight); // random number generates is inclusive, so we make sure we don't hit the ends or errors result // this is a bit odd, but done so that we can do a strict < comparison below which // allows us to avoid the possibility of a 0-weight interest being chosen when totalweight > 0 if (random == 0) { random = 0.0001f; Log.Message("Generated perfect 0 from random number generator, wow!"); } if (random == totalWeight) { random -= 0.0001f; Log.Message("Generated perfect 1 from random number generator, wow!"); } foreach (InterestDef passion in interests) { float weight = passion.GetWeight(skill, pawn); if (random < weight) { selected = passion; break; } random -= weight; } if (selected == null) { throw new NullReferenceException("selected still null at end of loop"); } return(selected); } catch (NullReferenceException e) { Log.Message(e.Message); Log.Message("level " + (float)skill.Level); Log.Message("Random " + random); Log.Message("TotalWeight " + totalWeight); foreach (InterestDef passion in interests) { Log.Message(passion.defName + " weight " + passion.GetWeight(skill, pawn)); } } return(null); }
public static void GenerateInterests(Pawn pawn, Pawn father = null, Pawn mother = null, bool inherit = false) { List <SkillDef> allDefsListForReading = DefDatabase <SkillDef> .AllDefsListForReading; for (int i = 0; i < allDefsListForReading.Count; i++) { SkillDef skillDef = allDefsListForReading[i]; SkillRecord skill = pawn.skills.GetSkill(skillDef); if (!skill.TotallyDisabled) { if (inherit) { float coin = Rand.Value; if (coin < 0.5) { InheritSkill(skill, mother, father); continue; } } bool forbidPositive = NoPositiveInterests(pawn, skillDef); bool requirePositive = ForcePositiveInterest(pawn, skillDef); InterestDef selected = null; if (!forbidPositive) // conflicts take precedence over requirements { if (!requirePositive) // all interests are possible { selected = GetInterest(interestList, skill, pawn); } else // only positives { if (positiveInterestList.Count == 0) { selected = interestList.GetDefault(); } else { selected = GetInterest(positiveInterestList, skill, pawn); } } } else // no positives { if (negativeInterestList.Count == 0) { selected = interestList.GetDefault(); } else { selected = GetInterest(negativeInterestList, skill, pawn); } } skill.passion = (Passion)interestList.IndexOf(selected); } } }
private static bool Prefix(float global_lf, Passion passion, bool learning_saturated, ref float __result) { bool fastLearning = DebugSettings.fastLearning; if (fastLearning) { __result = 200f; } else { float num = global_lf - 1f; InterestDef id = InterestBase.interestList[(int)passion]; float toAdd = id.learnFactor; num += toAdd; if (learning_saturated) { num *= 0.2f; } __result = num; } return(false); }
// runs every 100 ticks, postfix to stub StartInspirationMTBDays patch public static void HandleInspirationMTB(ref float result, InspirationHandler ih) { if (interestList.inspirers.Count == 0) { return; } if (ih.pawn.needs.mood == null) // result = -1, no mood value yet, so return { return; } // go through each skill this pawn has, see if they have an inspiring passion Pawn_SkillTracker pst = ih.pawn.skills; if (pst == null) { return; } if (pst.skills == null) { return; } // find any inspiring interests this pawn has and get their indices List <int> inspiringList = pst.skills.Select(s => (int)s.passion).Where(x => interestList[x].inspires).ToList(); if (inspiringList.Count < 1) // no pawn skill with inspiring passion { return; } InterestDef interest = getBestInspirer(inspiringList); result = interest.MTBDays(ih); }