Exemplo 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 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);
                }
            }
        }