/// <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> /// <param name="force">if set to <c>true</c> the mutation is added regardless if the mutation is valid for the given pawn.</param> /// <returns></returns> /// <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, bool force = false) { 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); } if (!force && !mutation.IsValidFor(pawn)) { 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 (!force && 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)); }
/// <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> /// <param name="force">if set to <c>true</c> the mutation will be added regardless if it is valid for the given pawn.</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, bool force = false) { 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, force)); } if (!force && !mutation.IsValidFor(pawn)) { return(MutationResult.Empty); } 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)); }