using System; using Verse; namespace BlaxLaws { /// /// A group entry of a category ("Any drug binge", "Hard drugs", "Human meat", "Any surgery"). Groups are /// predicates over defs, written once in code by the catalogue package; Watch expands a def-level group /// into the set of defs it covers when the lawbook changes. A group that depends on the thing rather /// than its def (a meal with human meat in it) sets DependsOnThing and overrides Matches(ActEvent); its /// check then runs on every act of the category while the category is watched. /// public abstract class ActGroup { /// The category id. public string categoryId; /// The group id with its "@" (ActGroups), unique within the category. public string id; /// The Keyed key of the label ("BlaxLaws_Group_hardDrugs"). public string labelKey; /// /// Def entries of the category this group covers, set by ActCatalogue.Build. "The smaller group wins" /// when two groups match one act. /// public int size; public string Key => ActKey.OfGroup(categoryId, id); /// The category's "any": every entry. The least specific row. public bool IsAny => id == ActGroups.Any; /// The check needs the thing itself (ActEvent.facts or thing), not only the def. public virtual bool DependsOnThing => false; public virtual string Label => labelKey.NullOrEmpty() ? id : labelKey.Translate().ToString(); /// /// Whether a def of the category belongs to the group. For a DependsOnThing group: whether a thing of /// this def could (a meal could hold meat); the act check decides. /// public abstract bool MatchesDef(Def def); /// Whether the act belongs to the group. Default: its def does. public virtual bool Matches(ActEvent act) => IsAny || (act?.def != null && MatchesDef(act.def)); } /// A group from two predicates, for the catalogue package's group lists. public sealed class PredicateGroup : ActGroup { private readonly Func defPredicate; private readonly Func actPredicate; /// Null for a def-level group; set for a group that reads the thing. public PredicateGroup(string categoryId, string id, string labelKey, Func defPredicate, Func actPredicate = null) { this.categoryId = categoryId; this.id = id.StartsWith(ActKey.GroupPrefix) ? id : ActKey.GroupPrefix + id; this.labelKey = labelKey; this.defPredicate = defPredicate ?? (_ => true); this.actPredicate = actPredicate; } public override bool DependsOnThing => actPredicate != null; public override bool MatchesDef(Def def) => def != null && defPredicate(def); public override bool Matches(ActEvent act) { if (IsAny) return true; if (act == null) return false; return actPredicate != null ? actPredicate(act) : act.def != null && defPredicate(act.def); } } }