Esempio n. 1
0
        public static List <HediffItem> GetCompatibleItems(this HediffComp_AnotherRandom comp)
        {
            //HediffCondition defaultCondition = hcp.defaultCondition;

            List <HediffItem> answer = new List <HediffItem>();

            foreach (HediffItem hi in comp.Props.hediffPool)
            {
                HediffCondition calculatedHC = ConditionBuilder.GetDefaultPlusSpecificHediffCondition(comp.Props?.defaultCondition ?? null, hi?.specificCondition ?? null, comp.HighVerbosity);

                if (
                    calculatedHC.HasBodypartCondition ? calculatedHC.bodyPart.GetBPRFromHediffCondition(comp.Pawn, out _) : true &&
                    calculatedHC.HasPawnCondition ? calculatedHC.pawn.ValidateCompatibilityOfHediffWithPawn(comp.Pawn) : true
                    )
                {
                    answer.Add(hi);
                }
            }

            if (!answer.NullOrEmpty())
            {
                return(answer);
            }

            return(null);
        }
        public static HediffCondition GetDefaultPlusSpecificHediffCondition(HediffCondition defaultHC, HediffCondition specificHC, bool debug = false)
        {
            string debugStr = debug ? "GetDefaultPlusSpecificHediffCondition - " : "";

            Tools.Warn(debugStr + "allocating answerHC", debug);
            HediffCondition answerHc = new HediffCondition
            {
                bodyPart = new BodyPartCondition
                {
                    bodyPartDef   = new List <BodyPartDef>(),
                    bodyPartLabel = new List <string>(),
                    bodyPartTag   = new List <BodyPartTagDef>()
                },
                pawn = new PawnCondition
                {
                    race   = new List <ThingDef>(),
                    gender = new List <Gender>()
                }
            };

            if (defaultHC != null)
            {
                Tools.Warn(debugStr + "found defaultHC, copying", debug);
                CopyHediffCondition(defaultHC, answerHc, debug);
            }


            if (specificHC != null)
            {
                Tools.Warn(debugStr + "found specificHC, copying", debug);
                CopyHediffCondition(specificHC, answerHc, debug);
            }


            return(answerHc);
        }
        public static void CopyHediffCondition(HediffCondition source, HediffCondition dest, bool debug = false)
        {
            string debugStr = debug ? "CopyHediffCondition - " : "";

            if (source.HasBodypartCondition)
            {
                Tools.Warn(debugStr + "found HasBodypartCondition, copying", debug);
                if (source.bodyPart.HasDef)
                {
                    dest.bodyPart.bodyPartDef = source.bodyPart.bodyPartDef.ListFullCopy();
                }
                if (source.bodyPart.HasLabel)
                {
                    dest.bodyPart.bodyPartLabel = source.bodyPart.bodyPartLabel.ListFullCopy();
                }
                if (source.bodyPart.HasTag)
                {
                    dest.bodyPart.bodyPartTag = source.bodyPart.bodyPartTag.ListFullCopy();
                }
            }

            if (source.HasPawnCondition)
            {
                Tools.Warn(debugStr + "found HasPawnCondition, copying", debug);
                if (source.pawn.HasRace)
                {
                    dest.pawn.race = source.pawn.race.ListFullCopy();
                }
                if (source.pawn.HasGender)
                {
                    dest.pawn.gender = source.pawn.gender.ListFullCopy();
                }

                dest.pawn.ageRange = source.pawn.ageRange;
            }
        }
        public void ApplyHediff(Pawn pawn)
        {
            string debugStr = MyDebug ? Pawn.LabelShort + " - " + parent.def.defName + " - ApplyHediff" : "";

            List <HediffItem> AlreadyPickedItems = new List <HediffItem>();
            int iterationNum = Props.hediffToApplyNumRange.RandomInRange;

            List <HediffItem> compatibleItems = this.GetCompatibleItems();

            if (compatibleItems.NullOrEmpty())
            {
                return;
            }

            Tools.Warn(debugStr + "Trying to apply " + iterationNum + " hediffs among " + compatibleItems.Count + " options pool", MyDebug);

            for (int i = 0; i < iterationNum; i++)
            {
                string debugIteration = MyDebug ? "[" + i + "/" + iterationNum + "]" : "";

                if (!AlreadyPickedItems.NullOrEmpty())
                {
                    compatibleItems = compatibleItems.GetRemainingItems(AlreadyPickedItems);
                    if (compatibleItems.NullOrEmpty())
                    {
                        return;
                    }
                }
                Tools.Warn(debugStr + debugIteration + " " + compatibleItems.Count + " options remaining ", MyDebug);

                HediffItem hediffItem = compatibleItems.PickRandomWeightedItem();
                if (hediffItem == null)
                {
                    Tools.Warn(debugStr + debugIteration + " null hediffItem, giving up ", MyDebug);
                    return;
                }

                Tools.Warn(debugStr + debugIteration + " found a hediffItem:" + hediffItem?.hediffDef + ", going on ", MyDebug);

                float randomChanceToApply = hediffItem.applyChance.RandomInRange;
                if (!Rand.Chance(randomChanceToApply))
                {
                    Tools.Warn(debugStr + debugIteration + " rand(" + randomChanceToApply + ") == false, nothing is applied", MyDebug);

                    if (Props.excludePickedItems && Props.excludeRandomlyNotApplied)
                    {
                        AlreadyPickedItems.Add(hediffItem);
                    }
                    continue;
                }
                Tools.Warn(debugStr + debugIteration + " rand(" + randomChanceToApply + ") == true, hediff:" + hediffItem?.hediffDef + " will be applied", MyDebug);

                HediffDef curHD = hediffItem.hediffDef;
                if (curHD == null)
                {
                    Tools.Warn(debugStr + debugIteration + "cant find hediff, giving up", MyDebug);
                    return;
                }

                HediffCondition calculatedHC =
                    ConditionBuilder.GetDefaultPlusSpecificHediffCondition(Props?.defaultCondition ?? null, hediffItem?.specificCondition ?? null, HighVerbosity);

                if (!calculatedHC.bodyPart.GetBPRFromHediffCondition(Pawn, out BodyPartRecord curBPR, MyDebug))
                {
                    Tools.Warn(debugStr + debugIteration + " could not find anything suitable, giving up", MyDebug);
                    return;
                }

                Hediff hediff2apply = HediffMaker.MakeHediff(curHD, pawn, curBPR);
                if (hediff2apply == null)
                {
                    Tools.Warn(debugStr + debugIteration + "cant create hediff", MyDebug);
                    return;
                }

                hediff2apply.Severity = hediffItem.severity.RandomInRange;

                Tools.Warn(
                    debugStr + debugIteration + " Applying " +
                    "hediff:" + curHD.defName +
                    "; bpr:" + (curBPR == null ? "body" : curBPR.def.defName) +
                    "; severity:" + hediff2apply.Severity
                    , MyDebug
                    );

                pawn.health.AddHediff(hediff2apply, curBPR, null);

                if (Props.excludePickedItems)
                {
                    AlreadyPickedItems.Add(hediffItem);
                }
            }
        }