Example #1
0
        /// <summary>
        ///     Adds the mutation to the given pawn
        /// </summary>
        /// <param name="pawn">The pawn.</param>
        /// <param name="mutation">The mutation.</param>
        /// <param name="parts">The parts.</param>
        /// <param name="countToAdd">The count to add.</param>
        /// <param name="ancillaryEffects">The ancillary effects.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        ///     pawn
        ///     or
        ///     mutation
        /// </exception>
        public static MutationResult AddMutation([NotNull] Pawn pawn, [NotNull] MutationDef mutation, [CanBeNull] List <BodyPartDef> parts,
                                                 int countToAdd = int.MaxValue,
                                                 AncillaryMutationEffects?ancillaryEffects = null)
        {
            if (pawn == null)
            {
                throw new ArgumentNullException(nameof(pawn));
            }
            if (mutation == null)
            {
                throw new ArgumentNullException(nameof(mutation));
            }

            var addLst = new List <BodyPartRecord>();

            if (parts != null)
            {
                foreach (BodyPartRecord notMissingPart in pawn.health.hediffSet.GetAllNonMissingWithoutProsthetics())
                {
                    if (parts.Contains(notMissingPart.def))
                    {
                        addLst.Add(notMissingPart);
                        if (parts.Count >= countToAdd)
                        {
                            break;
                        }
                    }
                }

                if (addLst.Count == 0)
                {
                    return(MutationResult.Empty);
                }
                return(AddMutation(pawn, mutation, addLst, ancillaryEffects));
            }


            var existingHediff = pawn.health.hediffSet.hediffs.FirstOrDefault(m => m.def == mutation && m.Part == null);

            if (existingHediff != null)
            {
                (existingHediff as Hediff_AddedMutation)?.ResumeAdaption();
                return(MutationResult.Empty);
            }

            if (!(HediffMaker.MakeHediff(mutation, pawn) is Hediff_AddedMutation hDef))
            {
                Log.Error($"{mutation.defName} is not a mutation but is being added like one!");
                return(MutationResult.Empty);
            }

            pawn.health.AddHediff(hDef);

            DoAncillaryMutationEffects(pawn, mutation, hDef, ancillaryEffects ?? AncillaryMutationEffects.Default);

            return(new MutationResult(hDef));
        }
Example #2
0
        /// <summary>
        ///     Adds the mutation to the given pawn
        /// </summary>
        /// <param name="pawn">The pawn.</param>
        /// <param name="mutation">The mutation.</param>
        /// <param name="records">The records to add mutations to</param>
        /// <param name="ancillaryEffects">The ancillary effects.</param>
        /// <exception cref="ArgumentNullException">
        ///     pawn
        ///     or
        ///     mutation
        ///     or
        ///     records
        /// </exception>
        public static MutationResult AddMutation([NotNull] Pawn pawn, [NotNull] MutationDef mutation,
                                                 [NotNull] IEnumerable <BodyPartRecord> records,
                                                 AncillaryMutationEffects?ancillaryEffects = null)
        {
            if (pawn == null)
            {
                throw new ArgumentNullException(nameof(pawn));
            }
            if (mutation == null)
            {
                throw new ArgumentNullException(nameof(mutation));
            }
            if (records == null)
            {
                throw new ArgumentNullException(nameof(records));
            }
            HediffSet hSet = pawn.health?.hediffSet;

            if (hSet == null)
            {
                return(MutationResult.Empty);
            }
            List <Hediff_AddedMutation> lst = new List <Hediff_AddedMutation>();

            foreach (BodyPartRecord bodyPartRecord in records)
            {
                if (bodyPartRecord.IsMissingAtAllIn(pawn))
                {
                    LogMsg(LogLevel.Pedantic, $"could not add {mutation.defName} to {pawn.Name} on {bodyPartRecord.Label} because it is missing or has a prosthetic");
                    continue;
                }

                if (HasAnyBlockingMutations(pawn, mutation, bodyPartRecord))
                {
                    continue;
                }

                var existingMutation = hSet.hediffs.FirstOrDefault(h => h.def == mutation && h.Part == bodyPartRecord);
                if (existingMutation != null) //resume adaption for mutations that are already added instead of re adding them
                {
                    LogMsg(LogLevel.Pedantic, $"could not add {mutation.defName} to {pawn.Name} on {bodyPartRecord.Label} because it already has that mutation");

                    existingMutation.ResumeAdjustment(); //don't do count restarted mutations as new ones
                    continue;
                }

                var hediff = HediffMaker.MakeHediff(mutation, pawn, bodyPartRecord) as Hediff_AddedMutation;
                if (hediff == null)
                {
                    Log.Error($"{mutation.defName} is not a mutation but is being added like one!");
                    continue;
                }

                lst.Add(hediff);
                hSet.AddDirect(hediff);
            }

            AncillaryMutationEffects aEffects = ancillaryEffects ?? AncillaryMutationEffects.Default;

            if (lst.Count > 0) //only do this if we actually added any mutations
            {
                DoAncillaryMutationEffects(pawn, mutation, lst, aEffects);
            }

            return(new MutationResult(lst));
        }
Example #3
0
 /// <summary>
 ///     Adds the mutation to the given pawn
 /// </summary>
 /// <param name="pawn">The pawn.</param>
 /// <param name="mutation">The mutation.</param>
 /// <param name="countToAdd">The count to add.</param>
 /// <param name="ancillaryEffects">The ancillary effects.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException">
 ///     pawn
 ///     or
 ///     mutation
 /// </exception>
 public static MutationResult AddMutation([NotNull] Pawn pawn, [NotNull] MutationDef mutation, int countToAdd = int.MaxValue, AncillaryMutationEffects?ancillaryEffects = null)
 {
     if (pawn == null)
     {
         throw new ArgumentNullException(nameof(pawn));
     }
     if (mutation == null)
     {
         throw new ArgumentNullException(nameof(mutation));
     }
     return(AddMutation(pawn, mutation, mutation.parts, countToAdd, ancillaryEffects));
 }
Example #4
0
        /// <summary>
        /// Adds all morph mutations.
        /// </summary>
        /// <param name="pawn">The pawn.</param>
        /// <param name="morph">The morph.</param>
        /// <param name="ancillaryEffects">The ancillary effects.</param>
        /// <exception cref="ArgumentNullException">pawn
        /// or
        /// morph</exception>
        public static void AddAllMorphMutations([NotNull] Pawn pawn, [NotNull] MorphDef morph, AncillaryMutationEffects?ancillaryEffects = null)
        {
            if (pawn == null)
            {
                throw new ArgumentNullException(nameof(pawn));
            }
            if (morph == null)
            {
                throw new ArgumentNullException(nameof(morph));
            }
            var hediffSet = pawn.health.hediffSet;

            _recordCache.Clear();
            _recordCache.AddRange(hediffSet.GetNotMissingParts());
            foreach (MutationDef mutation in morph.AllAssociatedMutations)
            {
                if (mutation.parts != null)
                {
                    foreach (BodyPartDef mutationPart in mutation.parts)
                    {
                        foreach (BodyPartRecord bodyPartRecord in _recordCache.Where(r => r.def == mutationPart))
                        {
                            if (hediffSet.HasHediff(mutation, bodyPartRecord))
                            {
                                continue;
                            }
                            AddMutation(pawn, mutation, bodyPartRecord, ancillaryEffects: ancillaryEffects);
                        }
                    }
                }
                else
                {
                    if (!hediffSet.HasHediff(mutation))
                    {
                        AddMutation(pawn, mutation, ancillaryEffects: ancillaryEffects);
                    }
                }
            }
        }