public void ApplyHediffAndRegisterWithBodyPartList(HediffItem hi, HediffAssociation CurHA, AssociatedHediffHistory CurAHH, List <BodyPartRecord> BPRL, bool debug = false)
 {
     foreach (BodyPartRecord BPR in BPRL)
     {
         ApplyHediffAndRegisterSingleBodyPart(hi, CurHA, CurAHH, BPR, debug);
     }
 }
        public void ApplyHediffAndRegisterSingleBodyPart(HediffItem hi, HediffAssociation CurHA, AssociatedHediffHistory CurAHH, BodyPartRecord BPR = null, bool debug = false)
        {
            //Severity
            Hediff h = HediffMaker.MakeHediff(hi.hediff, Pawn, BPR);

            if (hi.HasSeverity)
            {
                h.Severity = hi.severity.RandomInRange;
            }

            //Applying hediff to full body if BPR is null
            Pawn.health.AddHediff(h, BPR, null);

            //Recording hediff applied in registry
            CurAHH.appliedHediffs.Add(h);

            if (CurHA.specifics.HasLimit)
            {
                CurAHH.appliedNum++;
            }

            // destroy parent hediff if discard upon remove setting and random is satisfied
            if (CurHA.specifics.HasDiscard && CurHA.specifics.discard.HasUponApplyDiscard && Rand.Chance(CurHA.specifics.discard.uponApply.chance.RandomInRange))
            {
                Pawn.health.RemoveHediff(parent);
            }
            // add grace destroy if grace setting upon remove and random is satisfied
            if (CurHA.specifics.HasGrace && CurHA.specifics.grace.HasUponApplyGrace && Rand.Chance(CurHA.specifics.grace.uponApply.chance.RandomInRange))
            {
                CurAHH.grace += CurHA.specifics.grace.uponApply.tickAmount.RandomInRange;
            }
        }
Exemple #3
0
        //public static readonly MethodInfo ParentHolder = AccessTools.Method(type: typeof(IThingHolder), name: "ParentHolder");
        //shortHashGiver.Invoke(obj: null, parameters: new object[] {yourDef, typeof(yourDefType)});

        public static void CheckTriggeredAssociations(IEnumerable <Hediff> YahaHediffs, TriggerEvent triggerEvent)
        {
            foreach (Hediff h in YahaHediffs)
            {
                HediffComp_YetAnotherHediffApplier YahaComp = h.TryGetComp <HediffComp_YetAnotherHediffApplier>();
                bool MyDebug = YahaComp.Props.debug;
                if (MyDebug)
                {
                    Log.Warning("CheckTriggeredAssociations - Found " + h.def.defName + " Yaha hediff");
                }

                IEnumerable <int> indexes = YahaComp.GetTriggeredHediffAssociationIndex(triggerEvent, MyDebug);
                if (indexes.EnumerableNullOrEmpty())
                {
                    if (MyDebug)
                    {
                        Log.Warning("No " + h.def.defName + " Yaha hediff found with " + triggerEvent.GetDesc());
                    }
                    return;
                }

                foreach (int i in indexes)
                {
                    HediffAssociation       CurHA  = YahaComp.Props.associations[i];
                    AssociatedHediffHistory CurAHH = YahaComp.Registry[i];

                    if (MyDebug)
                    {
                        Log.Warning("CheckTriggeredAssociations - Found " + triggerEvent.GetDesc() + " ; i=" + i);
                    }

                    YahaComp.UpdateHediffDependingOnConditionsItem(CurHA, CurAHH, false, MyDebug);
                }
            }
        }
        public void UpdateHediffDependingOnConditions(bool debug = false)
        {
            for (int i = 0; i < Props.associations.Count; i++)
            //foreach(HediffAssociation element in Props.associations)
            {
                HediffAssociation       CurHA  = Props.associations[i];
                AssociatedHediffHistory CurAHH = Registry[i];

                UpdateHediffDependingOnConditionsItem(CurHA, CurAHH, true, debug);
            }
        }
        public bool RemoveHediffAndDeregister(HediffAssociation CurHA, AssociatedHediffHistory CurAHH, bool debug = false)
        {
            if (debug)
            {
                Log.Warning("Entering RemoveHediffAndDeregister");
            }

            if (Pawn.TrunkNodeComputation(CurHA.condition.trunk, debug))
            {
                if (debug)
                {
                    Log.Warning("Exiting RemoveHediffAndDeregister : condition was true");
                }
                return(true);
            }

            if ((CurHA.specifics != null) && (!CurHA.specifics.removeIfFalse))
            {
                return(false);
            }

            if (CurAHH == null || CurAHH.appliedHediffs.NullOrEmpty())
            {
                return(false);
            }

            //foreach(Hediff h in CurAHH.appliedHediffs)
            for (int j = CurAHH.appliedHediffs.Count - 1; j >= 0; j--)
            {
                Hediff h = CurAHH.appliedHediffs[j];
                // enough to remove the applied hediff ?? or need to retrieve it from pawn with bpr
                //Pawn.health.RemoveHediff(h);
                h.Severity = 0; h.PostRemoved();

                // destroy parent hediff if discard upon remove setting and random is satisfied
                if (CurHA.specifics.HasDiscard && CurHA.specifics.discard.HasUponRemoveDiscard && Rand.Chance(CurHA.specifics.discard.uponRemove.chance.RandomInRange))
                {
                    // will it blend ?
                    Pawn.health.RemoveHediff(parent);
                }
                // add grace destroy if grace setting upon remove and random is satisfied
                if (CurHA.specifics.HasGrace && CurHA.specifics.grace.HasUponRemoveGrace && Rand.Chance(CurHA.specifics.grace.uponRemove.chance.RandomInRange))
                {
                    CurAHH.grace += CurHA.specifics.grace.uponRemove.tickAmount.RandomInRange;
                }

                // Deregister
                //h.Severity = 0; h.PostRemoved();
                CurAHH.appliedHediffs.RemoveAt(j);
            }

            return(false);
        }
        public static bool CheckAllConditions(this Pawn p, HediffAssociation ha)
        {
            if (!p.OkPawn())
            {
                return(false);
            }

            if (ha.condition.HasNatureCondition)
            {
                if (ha.condition.nature.HasGender && !p.CheckGenderCondition(ha.condition.nature.gender))
                {
                    return(false);
                }
                if (ha.condition.nature.HasLifeStage && !p.CheckLifeStageCondition(ha.condition.nature.lifeStage))
                {
                    return(false);
                }
                if (ha.condition.nature.HasPawnkind && !p.CheckPawnKindCondition(ha.condition.nature.pawnKind))
                {
                    return(false);
                }
                if (ha.condition.nature.HasTrait && !p.CheckTraitCondition(ha.condition.nature.trait))
                {
                    return(false);
                }
            }

            if (ha.condition.HasJobCondition)
            {
                if (ha.condition.job.HasJob && !p.CheckCurrentJob(ha.condition.job))
                {
                    return(false);
                }
            }

            if (ha.condition.HasEnvironmentCondition)
            {
                if (ha.condition.environment.HasSeason && !p.CheckSeason(ha.condition.environment.season))
                {
                    return(false);
                }
                if (ha.condition.environment.HasWeather && !p.CheckWeather(ha.condition.environment.weather))
                {
                    return(false);
                }
            }

            return(true);
        }
        public void UpdateHediffDependingOnConditionsItem(HediffAssociation CurHA, AssociatedHediffHistory CurAHH, bool ContinueIfTriggered = true, bool debug = false)
        {
            // association has already applied needed hediffs in the past, skipping
            if (CurAHH.done)
            {
                return;
            }

            // triggered by harmony patch; no need to check it
            if (ContinueIfTriggered && CurHA.specifics.IsTriggered)
            {
                return;
            }

            if (CurAHH.HasGraceTime)
            {
                CurAHH.grace--;
                return;
            }

            // pawn does not fulfil conditions : remove already applied hediffs and/or go for next hediff
            if (!RemoveHediffAndDeregister(CurHA, CurAHH, debug))
            {
                return;
            }


            List <BodyPartRecord> bodyPartRecords = null;

            // Hediff association has body parts specifications
            if (CurHA.HasBodyPartToApplyHediff)
            {
                bodyPartRecords = Pawn.GetBP(CurHA.bodyPart, debug);

                //but pawn does not have any legit body part
                if (bodyPartRecords.NullOrEmpty())
                {
                    return;
                }
            }

            // target is full body
            if (bodyPartRecords.NullOrEmpty())
            {
                if (CurHA.HasHediffPool)
                {
                    foreach (HediffItem hi in CurHA.hediffPool)
                    {
                        ApplyHediffAndRegisterSingleBodyPart(hi, CurHA, CurAHH, null, debug);
                    }
                }
                else if (CurHA.HasRandomHediffPool)
                {
                    //Multiple random hediffs ????
                    ApplyHediffAndRegisterSingleBodyPart(CurHA.randomHediffPool.PickRandomWeightedItem(), CurHA, CurAHH, null, debug);
                }
            }
            // target has multiple bodypart records as target
            else
            {
                if (CurHA.HasHediffPool)
                {
                    foreach (HediffItem hi in CurHA.hediffPool)
                    {
                        ApplyHediffAndRegisterWithBodyPartList(hi, CurHA, CurAHH, bodyPartRecords, debug);
                    }
                }
                else if (CurHA.HasRandomHediffPool)
                {
                    ApplyHediffAndRegisterWithBodyPartList(CurHA.randomHediffPool.PickRandomWeightedItem(debug), CurHA, CurAHH, bodyPartRecords, debug);
                }
            }
            if (CurHA.specifics.HasLimit && CurAHH.appliedNum > CurHA.specifics.applyNumLimit)
            {
                CurAHH.done = true;
            }
        }