private void RecalculateMorphCount(MorphTracker tracker) { MorphDef myMorph = parent.def.GetMorphOfRace(); AspectTracker aspectTracker = Pawn.GetAspectTracker(); if (aspectTracker == null) { return; } MorphGroupDef group = myMorph?.group; AspectDef aspectDef = group?.aspectDef; if (aspectDef == null) { return; } Aspect aspect = aspectTracker.GetAspect(aspectDef); if (aspect == null) //if the hediff is missing for some reason add it again { aspect = aspectDef.CreateInstance(); aspectTracker.Add(aspect); } aspect.StageIndex = tracker.GetGroupCount(group) - 1; }
/// <summary> /// Applies the aspect to the given pawn /// </summary> /// <param name="pawn">The pawn.</param> /// <param name="aspect">The aspect.</param> /// <param name="stageIndex">Index of the stage.</param> /// <param name="outLst">The out LST.</param> /// <returns> /// if the aspect was successfully added or not /// </returns> protected virtual bool ApplyAspect([NotNull] Pawn pawn, [NotNull] AspectDef aspect, int stageIndex, [CanBeNull] List <Aspect> outLst) { var aspectTracker = pawn.GetAspectTracker(); if (aspectTracker == null) { return(false); } if (HasConflictingAspect(aspectTracker, aspect)) { return(false); } if (aspectTracker.Contains(aspect)) { return(false); //do not add the same aspect multiple times } if (!aspect.IsValidFor(pawn)) { return(false); //check for any other def restrictions } if (pawn.story?.traits != null && !CheckPawnTraits(pawn.story.traits, aspect)) { return(false); //check pawn traits } var aInst = aspect.CreateInstance(); outLst?.Add(aInst); aspectTracker.Add(aInst, stageIndex); return(true); }
/// <summary> Removes the aspect with the given def from the pawn. </summary> public void Remove(AspectDef def) { Aspect af = _aspects.FirstOrDefault(a => a.def == def); if (af != null) { _rmCache.Add(af); } }
/// <summary> /// Determines whether the specified tracker has a conflicting aspect. /// </summary> /// <param name="tracker">The tracker.</param> /// <param name="testAspect">The test aspect.</param> /// <returns> /// <c>true</c> if the specified tracker has conflicting aspect ; otherwise, <c>false</c>. /// </returns> protected bool HasConflictingAspect([NotNull] AspectTracker tracker, [NotNull] AspectDef testAspect) { foreach (AspectDef conflict in testAspect.conflictingAspects) { if (tracker.Contains(conflict)) { return(true); } } return(false); }
/// <summary>Gets the available stages that can be added by the given aspect.</summary> /// <param name="def">The definition.</param> /// <returns></returns> public IEnumerable <int> GetAvailableStagesFor(AspectDef def) { if (_addDict == null) { _addDict = new Dictionary <AspectDef, List <int> >(); IEnumerable <IGrouping <AspectDef, int> > grouping = aspects.GroupBy(a => a.aspect, a => a.stage); foreach (IGrouping <AspectDef, int> group in grouping) { _addDict[group.Key] = group.Distinct().ToList(); } } return(_addDict.TryGetValue(def) ?? Enumerable.Empty <int>()); }
/// <summary> Add the given aspect to this pawn at the specified stage index. </summary> public void Add([NotNull] AspectDef def, int startStage = 0) { if (def == null) { throw new ArgumentNullException(nameof(def)); } if (_aspects.Count(a => a.def == def) > 0) { Log.Warning($"trying to add an aspect with def {def.defName} to pawn {Pawn.Name} which already has an aspect of that def"); return; } Add(def.CreateInstance(), startStage); }
/// <summary> /// Checks if the pawn has any traits that block the given aspect /// </summary> /// <param name="traitSet">The trait set.</param> /// <param name="testAspect">The test aspect.</param> /// <returns>if given trait set is valid for the given aspect</returns> protected bool CheckPawnTraits([NotNull] TraitSet traitSet, [NotNull] AspectDef testAspect) { foreach (TraitDef traitDef in testAspect.requiredTraits.MakeSafe()) { if (!traitSet.HasTrait(traitDef)) { return(false); } } foreach (var traitDef in testAspect.conflictingTraits.MakeSafe()) { if (traitSet.HasTrait(traitDef)) { return(false); } } return(true); }
private void MorphCountChanged(MorphTracker sender, MorphDef morph) { MorphDef myMorph = parent.def.GetMorphOfRace(); if (myMorph?.group == null) { return; } if (myMorph.group != morph?.group) { return; } var pawn = (Pawn)parent; AspectTracker aspectTracker = pawn.GetAspectTracker(); if (aspectTracker == null) { return; } AspectDef aspectDef = morph?.group?.aspectDef; if (aspectDef == null) { return; } Aspect aspect = aspectTracker.GetAspect(aspectDef); if (aspect == null) //if the aspect is missing for some reason add it again { aspect = aspectDef.CreateInstance(); aspectTracker.Add(aspect); } var comp = pawn.Map?.GetComponent <MorphTracker>(); aspect.StageIndex = (comp?.GetGroupCount(morph.group) ?? 0) - 1; //stage should always be equal to the number of morphs in the group active in the same map }
public Aspect GetAspect(AspectDef aspectDef) { return(_aspects.FirstOrDefault(d => d.def == aspectDef)); }
/// <summary> /// Determines whether this instance contains the given aspect at the given stage. /// </summary> /// <param name="aspectDef">The aspect definition.</param> /// <param name="stage">The stage.</param> /// <returns> /// <c>true</c> if this instance contains the specified aspect at the given stage; otherwise, <c>false</c>. /// </returns> public bool Contains(AspectDef aspectDef, int stage) { var aspect = _aspects.FirstOrDefault(a => a.def == aspectDef); return(aspect?.StageIndex == stage); }
/// <summary> /// if this tracker contains an aspect with the given def /// </summary> /// <param name="aspect"></param> /// <returns></returns> public bool Contains(AspectDef aspect) { return(_aspects.Any(a => a.def == aspect)); }