Ejemplo n.º 1
0
        private bool IsTraitAddable([NotNull] TraitItem trait, out string error)
        {
            if (trait.TraitDef.IsDisallowedByBackstory(_pawn, trait.Degree, out Backstory backstory))
            {
                error = "TKUtils.Trait.RestrictedByBackstory".LocalizeKeyed(backstory.identifier, trait.Name);

                return(false);
            }

            if (_pawn.kindDef.disallowedTraits?.Any(t => t.defName.Equals(trait.DefName)) == true)
            {
                error = "TKUtils.Trait.RestrictedByKind".LocalizeKeyed(_pawn.kindDef.race.LabelCap, trait.Name);

                return(false);
            }

            if (trait.TraitDef.IsDisallowedByKind(_pawn, trait.Degree))
            {
                error = "TKUtils.Trait.RestrictedByKind".LocalizeKeyed(_pawn.kindDef.race.LabelCap, trait.Name);

                return(false);
            }

            error = null;

            return(true);
        }
Ejemplo n.º 2
0
        private bool IsTraitRemovable(TraitItem trait, out string error)
        {
            if (CompatRegistry.Alien != null && CompatRegistry.Alien.IsTraitForced(_pawn, trait.DefName, trait.Degree))
            {
                error = "TKUtils.RemoveTrait.Kind".LocalizeKeyed(_pawn.kindDef.race.LabelCap, trait.Name);

                return(false);
            }

            if (RationalRomance.Active && RationalRomance.IsTraitDisabled(trait.TraitDef !))
            {
                error = "TKUtils.RemoveTrait.RationalRomance".LocalizeKeyed(trait.Name.CapitalizeFirst());

                return(false);
            }

            if (CompatRegistry.Magic?.IsClassTrait(trait.TraitDef !) == true && !TkSettings.ClassChanges)
            {
                error = "TKUtils.RemoveTrait.Class".LocalizeKeyed(trait.Name);

                return(false);
            }

            error = null;

            return(true);
        }
Ejemplo n.º 3
0
        private static bool IsUsable([CanBeNull] TraitItem t1, [CanBeNull] TraitItem t2)
        {
            if (t1?.TraitDef == null || t2?.TraitDef == null)
            {
                return(false);
            }

            return(t1.CanRemove && t2.CanAdd);
        }
Ejemplo n.º 4
0
        public TraitItem GetNextAsTrait(Action <string> errorCallback)
        {
            TraitItem trait = GetNextAsTrait();

            if (trait == null)
            {
                errorCallback.Invoke(_lastArgument);
            }

            return(trait);
        }
Ejemplo n.º 5
0
        public IEnumerable <TraitItem> GetAllAsTrait()
        {
            while (HasNext())
            {
                TraitItem trait = GetNextAsTrait();

                if (trait == null)
                {
                    break;
                }

                yield return(trait);
            }
        }
Ejemplo n.º 6
0
        public static bool TryGetTrait(string input, out TraitItem trait)
        {
            if (input.StartsWith("$"))
            {
                input = input.Substring(1);

                trait = Traits.Find(t => string.Equals(t.DefName, input));
            }
            else
            {
                trait = Traits.Find(t => string.Equals(t.Name.ToToolkit().StripTags(), input.ToToolkit().StripTags(), StringComparison.InvariantCultureIgnoreCase));
            }

            return(trait != null);
        }
Ejemplo n.º 7
0
        public IEnumerable <TraitItem> GetAllAsTrait(Action <string> errorCallback)
        {
            while (HasNext())
            {
                TraitItem trait = GetNextAsTrait();

                if (trait == null)
                {
                    errorCallback.Invoke(_lastArgument);

                    break;
                }

                yield return(trait);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Processes the given trait partial data, and loads it into the
        ///     mod's store.
        /// </summary>
        /// <param name="partialData">
        ///     A collection of partial data to load into
        ///     the mod's store
        /// </param>
        public static void ProcessTraitPartial([NotNull] IEnumerable <TraitItem> partialData)
        {
            var builder = new StringBuilder();

            foreach (TraitItem partial in partialData)
            {
                TraitItem existing = Traits.Find(i => i.DefName.Equals(partial.DefName) && i.Degree == partial.Degree);

                if (existing == null)
                {
                    if (partial.TraitDef == null)
                    {
                        builder.Append($"  - {partial.Data?.Mod ?? "UNKNOWN"}:{partial.DefName}:{partial.Degree}\n");

                        continue;
                    }

                    Traits.Add(partial);

                    continue;
                }

                existing.Name         = partial.Name;
                existing.CanAdd       = partial.CanAdd;
                existing.CostToAdd    = partial.CostToAdd;
                existing.CanRemove    = partial.CanRemove;
                existing.CostToRemove = partial.CostToRemove;
                existing.Data         = partial.Data;
            }

            if (builder.Length <= 0)
            {
                return;
            }

            builder.Insert(0, "The following traits could not be loaded from the partial data provided:\n");
            TkUtils.Logger.Warn(builder.ToString());
        }
Ejemplo n.º 9
0
        private bool TryProcessTraits([NotNull] Pawn subject, [NotNull] IEnumerable <TraitItem> traits, out List <TraitEvent> traitEvents)
        {
            var container = new List <TraitEvent>();
            List <TraitItem> traitItems = traits.ToList();
            List <string>    defNames   = traitItems.Select(t => t.DefName).ToList();

            container.AddRange(
                subject.story.traits.allTraits.Where(t => !defNames.Contains(t.def.defName))
                .Select(
                    t =>
            {
                TraitItem trait = Data.Traits.Find(i => i.DefName.Equals(t.def.defName) && i.Degree == t.Degree);

                if (!trait.CanRemove)
                {
                    return(new TraitEvent {
                        Type = EventType.Noop, Error = "TKUtils.RemoveTrait.Disabled".LocalizeKeyed(trait.Name)
                    });
                }

                return(!IsTraitRemovable(trait, out string error)
                                ? new TraitEvent {
                    Type = EventType.Noop, Error = error, Item = trait
                }
                                : new TraitEvent {
                    Type = EventType.Remove, Trait = t, Item = trait
                });
            }
                    )
                );

            container.AddRange(
                traitItems.Where(t => !subject.story.traits.HasTrait(t.TraitDef))
                .Select(
                    t =>
            {
                if (!t.CanAdd)
                {
                    return(new TraitEvent {
                        Type = EventType.Noop, Error = "TKUtils.Trait.Disabled".LocalizeKeyed(t.Name)
                    });
                }

                return(!IsTraitAddable(t, out string error)
                                ? new TraitEvent {
                    Type = EventType.Noop, Error = error, Item = t
                }
                                : new TraitEvent {
                    Type = EventType.Add, Item = t
                });
            }
                    )
                );

            container.AddRange(
                traitItems.Where(t => subject.story.traits.allTraits.Find(i => i.def.defName.Equals(t.DefName) && i.Degree == t.Degree) != null)
                .Select(t => new TraitEvent {
                Type = EventType.Noop, Item = t
            })
                );

            var final = new List <TraitEvent>(container.Where(e => e.Type == EventType.Remove));

            final.AddRange(container.Where(t => t.Type != EventType.Remove).GroupBy(t => t.Item.DefName).Select(e => e.First()));


            traitEvents = final;

            return(true);
        }
Ejemplo n.º 10
0
        public bool TryGetNextAsTrait(out TraitItem trait)
        {
            trait = GetNextAsTrait();

            return(!(trait is null));
        }
Ejemplo n.º 11
0
 public static bool CompareToInput([NotNull] TraitItem trait, [NotNull] string input) => RichTextHelper.StripTags(trait.Name)
 .ToToolkit()
 .EqualsIgnoreCase(RichTextHelper.StripTags(input).ToToolkit());