private static int FinalLevelOfSkill(Pawn pawn, SkillDef sk)
        {
            float num = (!sk.usuallyDefinedInBackstories) ? Rand.ByCurve(PawnGenerator.LevelRandomCurve, 100) : ((float)Rand.RangeInclusive(0, 4));

            foreach (Backstory item in from bs in pawn.story.AllBackstories
                     where bs != null
                     select bs)
            {
                foreach (KeyValuePair <SkillDef, int> item2 in item.skillGainsResolved)
                {
                    if (item2.Key == sk)
                    {
                        num += (float)item2.Value * Rand.Range(1f, 1.4f);
                    }
                }
            }
            for (int i = 0; i < pawn.story.traits.allTraits.Count; i++)
            {
                int num2 = 0;
                if (pawn.story.traits.allTraits[i].CurrentData.skillGains.TryGetValue(sk, out num2))
                {
                    num += (float)num2;
                }
            }
            float num3 = Rand.Range(1f, PawnGenerator.AgeSkillMaxFactorCurve.Evaluate((float)pawn.ageTracker.AgeBiologicalYears));

            num *= num3;
            num  = PawnGenerator.LevelFinalAdjustmentCurve.Evaluate(num);
            return(Mathf.Clamp(Mathf.RoundToInt(num), 0, 20));
        }
Exemple #2
0
        public static void RandByCurveTests()
        {
            DebugHistogram debugHistogram = new DebugHistogram((from x in Enumerable.Range(0, 30)
                                                                select(float) x).ToArray <float>());
            SimpleCurve curve = new SimpleCurve
            {
                {
                    new CurvePoint(0f, 0f),
                    true
                },
                {
                    new CurvePoint(10f, 1f),
                    true
                },
                {
                    new CurvePoint(15f, 2f),
                    true
                },
                {
                    new CurvePoint(20f, 2f),
                    true
                },
                {
                    new CurvePoint(21f, 0.5f),
                    true
                },
                {
                    new CurvePoint(30f, 0f),
                    true
                }
            };
            float num = 0f;

            for (int i = 0; i < 1000000; i++)
            {
                float num2 = Rand.ByCurve(curve);
                num += num2;
                debugHistogram.Add(num2);
            }
            debugHistogram.Display();
            Log.Message(string.Format("Average {0}, calculated as {1}", num / 1000000f, Rand.ByCurveAverage(curve)), false);
        }
        private static int FinalLevelOfSkill(Pawn pawn, SkillDef sk)
        {
            float num = ((!sk.usuallyDefinedInBackstories) ? Rand.ByCurve(LevelRandomCurve) : ((float)Rand.RangeInclusive(0, 4)));

            foreach (Backstory item in pawn.story.AllBackstories.Where((Backstory bs) => bs != null))
            {
                foreach (KeyValuePair <SkillDef, int> item2 in item.skillGainsResolved)
                {
                    if (item2.Key == sk)
                    {
                        num += (float)item2.Value * Rand.Range(1f, 1.4f);
                    }
                }
            }
            for (int i = 0; i < pawn.story.traits.allTraits.Count; i++)
            {
                int value = 0;
                if (pawn.story.traits.allTraits[i].CurrentData.skillGains.TryGetValue(sk, out value))
                {
                    num += (float)value;
                }
            }
            float num2 = Rand.Range(1f, AgeSkillMaxFactorCurve.Evaluate(pawn.ageTracker.AgeBiologicalYears));

            num *= num2;
            num  = LevelFinalAdjustmentCurve.Evaluate(num);
            if (pawn.kindDef.skills != null)
            {
                foreach (SkillRange skill in pawn.kindDef.skills)
                {
                    if (skill.Skill == sk)
                    {
                        if (num < (float)skill.Range.min || num > (float)skill.Range.max)
                        {
                            num = skill.Range.RandomInRange;
                        }
                        break;
                    }
                }
            }
            return(Mathf.Clamp(Mathf.RoundToInt(num), 0, 20));
        }
        public static void RandByCurveTests()
        {
            DebugHistogram debugHistogram = new DebugHistogram(Enumerable.Range(0, 30).Select((Func <int, float>)((int x) => x)).ToArray());
            SimpleCurve    curve          = new SimpleCurve
            {
                new CurvePoint(0f, 0f),
                new CurvePoint(10f, 1f),
                new CurvePoint(15f, 2f),
                new CurvePoint(20f, 2f),
                new CurvePoint(21f, 0.5f),
                new CurvePoint(30f, 0f)
            };
            float num = 0f;

            for (int i = 0; i < 1000000; i++)
            {
                float num2 = Rand.ByCurve(curve);
                num += num2;
                debugHistogram.Add(num2);
            }
            debugHistogram.Display();
            Log.Message($"Average {num / 1000000f}, calculated as {Rand.ByCurveAverage(curve)}");
        }
Exemple #5
0
        public static void RandByCurveTests()
        {
            DebugHistogram debugHistogram = new DebugHistogram((from x in Enumerable.Range(0, 30)
                                                                select(float) x).ToArray());
            SimpleCurve simpleCurve = new SimpleCurve();

            simpleCurve.Add(new CurvePoint(0f, 0f));
            simpleCurve.Add(new CurvePoint(10f, 1f));
            simpleCurve.Add(new CurvePoint(15f, 2f));
            simpleCurve.Add(new CurvePoint(20f, 2f));
            simpleCurve.Add(new CurvePoint(21f, 0.5f));
            simpleCurve.Add(new CurvePoint(30f, 0f));
            SimpleCurve curve = simpleCurve;
            float       num   = 0f;

            for (int i = 0; i < 1000000; i++)
            {
                float num2 = Rand.ByCurve(curve);
                num += num2;
                debugHistogram.Add(num2);
            }
            debugHistogram.Display();
            Log.Message($"Average {num / 1000000f}, calculated as {Rand.ByCurveAverage(curve)}");
        }
Exemple #6
0
        public static void DoBirthSpawn(Pawn mother, Pawn father)
        {
            int num = (mother.RaceProps.litterSizeCurve == null) ? 1 : Mathf.RoundToInt(Rand.ByCurve(mother.RaceProps.litterSizeCurve, 300));

            if (num < 1)
            {
                num = 1;
            }
            PawnGenerationRequest request = new PawnGenerationRequest(mother.kindDef, mother.Faction, PawnGenerationContext.NonPlayer, -1, false, true, false, false, true, false, 1f, false, true, true, false, false, false, false, null, null, null, null, null, null, null);
            Pawn pawn = null;

            for (int i = 0; i < num; i++)
            {
                pawn = PawnGenerator.GeneratePawn(request);
                if (PawnUtility.TrySpawnHatchedOrBornPawn(pawn, mother))
                {
                    if (pawn.playerSettings != null && mother.playerSettings != null)
                    {
                        pawn.playerSettings.AreaRestriction = mother.playerSettings.AreaRestriction;
                    }
                    if (pawn.RaceProps.IsFlesh)
                    {
                        pawn.relations.AddDirectRelation(PawnRelationDefOf.Parent, mother);
                        if (father != null)
                        {
                            pawn.relations.AddDirectRelation(PawnRelationDefOf.Parent, father);
                        }
                    }
                }
                else
                {
                    Find.WorldPawns.PassToWorld(pawn, PawnDiscardDecideMode.Discard);
                }
                TaleRecorder.RecordTale(TaleDefOf.GaveBirth, mother, pawn);
            }
            if (mother.Spawned)
            {
                FilthMaker.MakeFilth(mother.Position, mother.Map, ThingDefOf.FilthAmnioticFluid, mother.LabelIndefinite(), 5);
                if (mother.caller != null)
                {
                    mother.caller.DoCall();
                }
                if (pawn.caller != null)
                {
                    pawn.caller.DoCall();
                }
            }
        }
        private static void GenerateRandomAge(Pawn pawn, PawnGenerationRequest request)
        {
            if (request.FixedBiologicalAge.HasValue && request.FixedChronologicalAge.HasValue)
            {
                float?fixedBiologicalAge    = request.FixedBiologicalAge;
                bool  arg_46_0              = fixedBiologicalAge.HasValue;
                float?fixedChronologicalAge = request.FixedChronologicalAge;
                if ((arg_46_0 & fixedChronologicalAge.HasValue) && fixedBiologicalAge.GetValueOrDefault() > fixedChronologicalAge.GetValueOrDefault())
                {
                    Log.Warning(string.Concat(new object[]
                    {
                        "Tried to generate age for pawn ",
                        pawn,
                        ", but pawn generation request demands biological age (",
                        request.FixedBiologicalAge,
                        ") to be greater than chronological age (",
                        request.FixedChronologicalAge,
                        ")."
                    }));
                }
            }
            if (request.Newborn)
            {
                pawn.ageTracker.AgeBiologicalTicks = 0L;
            }
            else if (request.FixedBiologicalAge.HasValue)
            {
                pawn.ageTracker.AgeBiologicalTicks = (long)(request.FixedBiologicalAge.Value * 3600000f);
            }
            else
            {
                int   num = 0;
                float num2;
                while (true)
                {
                    if (pawn.RaceProps.ageGenerationCurve != null)
                    {
                        num2 = (float)Mathf.RoundToInt(Rand.ByCurve(pawn.RaceProps.ageGenerationCurve, 200));
                    }
                    else if (pawn.RaceProps.IsMechanoid)
                    {
                        num2 = (float)Rand.Range(0, 2500);
                    }
                    else
                    {
                        num2 = Rand.ByCurve(PawnGenerator.DefaultAgeGenerationCurve, 200) * pawn.RaceProps.lifeExpectancy;
                    }
                    num++;
                    if (num > 300)
                    {
                        break;
                    }
                    if (num2 <= (float)pawn.kindDef.maxGenerationAge && num2 >= (float)pawn.kindDef.minGenerationAge)
                    {
                        goto IL_1D9;
                    }
                }
                Log.Error("Tried 300 times to generate age for " + pawn);
IL_1D9:
                pawn.ageTracker.AgeBiologicalTicks = (long)(num2 * 3600000f) + (long)Rand.Range(0, 3600000);
            }
            if (request.Newborn)
            {
                pawn.ageTracker.AgeChronologicalTicks = 0L;
            }
            else if (request.FixedChronologicalAge.HasValue)
            {
                pawn.ageTracker.AgeChronologicalTicks = (long)(request.FixedChronologicalAge.Value * 3600000f);
            }
            else
            {
                int num3;
                if (request.CertainlyBeenInCryptosleep || Rand.Value < pawn.kindDef.backstoryCryptosleepCommonality)
                {
                    float value = Rand.Value;
                    if (value < 0.7f)
                    {
                        num3 = Rand.Range(0, 100);
                    }
                    else if (value < 0.95f)
                    {
                        num3 = Rand.Range(100, 1000);
                    }
                    else
                    {
                        int max = GenDate.Year((long)GenTicks.TicksAbs, 0f) - 2026 - pawn.ageTracker.AgeBiologicalYears;
                        num3 = Rand.Range(1000, max);
                    }
                }
                else
                {
                    num3 = 0;
                }
                int  ticksAbs = GenTicks.TicksAbs;
                long num4     = (long)ticksAbs - pawn.ageTracker.AgeBiologicalTicks;
                num4 -= (long)num3 * 3600000L;
                pawn.ageTracker.BirthAbsTicks = num4;
            }
            if (pawn.ageTracker.AgeBiologicalTicks > pawn.ageTracker.AgeChronologicalTicks)
            {
                pawn.ageTracker.AgeChronologicalTicks = pawn.ageTracker.AgeBiologicalTicks;
            }
        }
 private static void GenerateRandomAge(Pawn pawn, PawnGenerationRequest request)
 {
     if (request.FixedBiologicalAge.HasValue && request.FixedChronologicalAge.HasValue)
     {
         float?fixedBiologicalAge    = request.FixedBiologicalAge;
         bool  hasValue              = fixedBiologicalAge.HasValue;
         float?fixedChronologicalAge = request.FixedChronologicalAge;
         if ((hasValue & fixedChronologicalAge.HasValue) && fixedBiologicalAge.GetValueOrDefault() > fixedChronologicalAge.GetValueOrDefault())
         {
             Log.Warning("Tried to generate age for pawn " + pawn + ", but pawn generation request demands biological age (" + request.FixedBiologicalAge + ") to be greater than chronological age (" + request.FixedChronologicalAge + ").");
         }
     }
     if (request.Newborn)
     {
         pawn.ageTracker.AgeBiologicalTicks = 0L;
     }
     else if (request.FixedBiologicalAge.HasValue)
     {
         pawn.ageTracker.AgeBiologicalTicks = (long)(request.FixedBiologicalAge.Value * 3600000.0);
     }
     else
     {
         float num  = 0f;
         int   num2 = 0;
         while (true)
         {
             num = ((pawn.RaceProps.ageGenerationCurve == null) ? ((!pawn.RaceProps.IsMechanoid) ? (Rand.ByCurve(PawnGenerator.DefaultAgeGenerationCurve, 200) * pawn.RaceProps.lifeExpectancy) : ((float)Rand.Range(0, 2500))) : ((float)Mathf.RoundToInt(Rand.ByCurve(pawn.RaceProps.ageGenerationCurve, 200))));
             num2++;
             if (num2 > 300)
             {
                 Log.Error("Tried 300 times to generate age for " + pawn);
                 break;
             }
             if (!(num > (float)pawn.kindDef.maxGenerationAge) && !(num < (float)pawn.kindDef.minGenerationAge))
             {
                 break;
             }
         }
         pawn.ageTracker.AgeBiologicalTicks = (long)(num * 3600000.0) + Rand.Range(0, 3600000);
     }
     if (request.Newborn)
     {
         pawn.ageTracker.AgeChronologicalTicks = 0L;
     }
     else if (request.FixedChronologicalAge.HasValue)
     {
         pawn.ageTracker.AgeChronologicalTicks = (long)(request.FixedChronologicalAge.Value * 3600000.0);
     }
     else
     {
         int num3;
         if (request.CertainlyBeenInCryptosleep || Rand.Value < pawn.kindDef.backstoryCryptosleepCommonality)
         {
             float value = Rand.Value;
             if (value < 0.699999988079071)
             {
                 num3 = Rand.Range(0, 100);
             }
             else if (value < 0.949999988079071)
             {
                 num3 = Rand.Range(100, 1000);
             }
             else
             {
                 int max = GenDate.Year(GenTicks.TicksAbs, 0f) - 2026 - pawn.ageTracker.AgeBiologicalYears;
                 num3 = Rand.Range(1000, max);
             }
         }
         else
         {
             num3 = 0;
         }
         int  ticksAbs = GenTicks.TicksAbs;
         long num4     = ticksAbs - pawn.ageTracker.AgeBiologicalTicks;
         num4 -= (long)num3 * 3600000L;
         pawn.ageTracker.BirthAbsTicks = num4;
     }
     if (pawn.ageTracker.AgeBiologicalTicks > pawn.ageTracker.AgeChronologicalTicks)
     {
         pawn.ageTracker.AgeChronologicalTicks = pawn.ageTracker.AgeBiologicalTicks;
     }
 }
 private static void GenerateRandomAge(Pawn pawn, PawnGenerationRequest request)
 {
     if (request.FixedBiologicalAge.HasValue && request.FixedChronologicalAge.HasValue && request.FixedBiologicalAge > request.FixedChronologicalAge)
     {
         Log.Warning(string.Concat("Tried to generate age for pawn ", pawn, ", but pawn generation request demands biological age (", request.FixedBiologicalAge, ") to be greater than chronological age (", request.FixedChronologicalAge, ")."));
     }
     if (request.Newborn)
     {
         pawn.ageTracker.AgeBiologicalTicks = 0L;
     }
     else if (request.FixedBiologicalAge.HasValue)
     {
         pawn.ageTracker.AgeBiologicalTicks = (long)(request.FixedBiologicalAge.Value * 3600000f);
     }
     else
     {
         float num  = 0f;
         int   num2 = 0;
         do
         {
             num = ((pawn.RaceProps.ageGenerationCurve != null) ? ((float)Mathf.RoundToInt(Rand.ByCurve(pawn.RaceProps.ageGenerationCurve))) : ((!pawn.RaceProps.IsMechanoid) ? (Rand.ByCurve(DefaultAgeGenerationCurve) * pawn.RaceProps.lifeExpectancy) : Rand.Range(0f, 2500f)));
             num2++;
             if (num2 > 300)
             {
                 Log.Error("Tried 300 times to generate age for " + pawn);
                 break;
             }
         }while (num > (float)pawn.kindDef.maxGenerationAge || num < (float)pawn.kindDef.minGenerationAge);
         pawn.ageTracker.AgeBiologicalTicks = (long)(num * 3600000f) + Rand.Range(0, 3600000);
     }
     if (request.Newborn)
     {
         pawn.ageTracker.AgeChronologicalTicks = 0L;
     }
     else if (request.FixedChronologicalAge.HasValue)
     {
         pawn.ageTracker.AgeChronologicalTicks = (long)(request.FixedChronologicalAge.Value * 3600000f);
     }
     else
     {
         int num3;
         if (request.CertainlyBeenInCryptosleep || Rand.Value < pawn.kindDef.backstoryCryptosleepCommonality)
         {
             float value = Rand.Value;
             if (value < 0.7f)
             {
                 num3 = Rand.Range(0, 100);
             }
             else if (value < 0.95f)
             {
                 num3 = Rand.Range(100, 1000);
             }
             else
             {
                 int max = GenDate.Year(GenTicks.TicksAbs, 0f) - 2026 - pawn.ageTracker.AgeBiologicalYears;
                 num3 = Rand.Range(1000, max);
             }
         }
         else
         {
             num3 = 0;
         }
         long num4 = GenTicks.TicksAbs - pawn.ageTracker.AgeBiologicalTicks;
         num4 -= (long)num3 * 3600000L;
         pawn.ageTracker.BirthAbsTicks = num4;
     }
     if (pawn.ageTracker.AgeBiologicalTicks > pawn.ageTracker.AgeChronologicalTicks)
     {
         pawn.ageTracker.AgeChronologicalTicks = pawn.ageTracker.AgeBiologicalTicks;
     }
 }