using System; using System.Collections.Generic; using Verse; using Verse.AI; namespace BlaxLaws { /// /// A category of the act catalogue (Defs/BlaxLaws_Categories.xml). The defName is the category id, the /// first part of every act key, so it never changes. The worker reads the loaded defs once and returns /// the category's entries. /// public class ActCategoryDef : Def { public Type workerClass = typeof(ActCategoryWorker); /// Picker order; lower first. public int order; /// Def names the worker adds beyond its rule (Berserk for the breaks). public List include = new List(); /// Def names the worker leaves out (animal and baby states, faction-level events). public List exclude = new List(); /// /// Break category only: mental state classes hostile to the colony, subclasses included (berserk). Their /// entries never offer Subdue. Enforcement re-checks ForceHostileTo(Faction.OfPlayer) on the live /// state, which wins over this list. /// public List hostileStateClasses = new List(); [Unsaved] private ActCategoryWorker workerInt; public ActCategoryWorker Worker { get { if (workerInt == null) { workerInt = (ActCategoryWorker)Activator.CreateInstance(workerClass ?? typeof(ActCategoryWorker)); workerInt.def = this; } return workerInt; } } public bool Includes(string defName) => include != null && include.Contains(defName); public bool Excludes(string defName) => exclude != null && exclude.Contains(defName); public override IEnumerable ConfigErrors() { foreach (string e in base.ConfigErrors()) yield return e; if (workerClass == null || !typeof(ActCategoryWorker).IsAssignableFrom(workerClass)) yield return "workerClass must be an ActCategoryWorker"; if (defName.IndexOf(ActKey.Separator) >= 0) yield return "defName must not contain '" + ActKey.Separator + "': it is the first part of an act key"; } } /// /// A special act with its own hook (Defs/BlaxLaws_Acts.xml): SetFire, DisplayedCorpse, StartedSocialFight in /// the first release. The defName is the act's id in "Special/<defName>", so it never changes. Its /// hook files it with an ActEvent whose def is this ActDef. /// public class ActDef : Def { public Type workerClass = typeof(ActWorker); /// Picker order inside "Special acts"; lower first, then A to Z. public int order; [Unsaved] private ActWorker workerInt; public ActWorker Worker { get { if (workerInt == null) { workerInt = (ActWorker)Activator.CreateInstance(workerClass ?? typeof(ActWorker)); workerInt.def = this; } return workerInt; } } public string Key => ActKey.Of(ActCategories.Special, defName); } /// /// Behaviour of one special act beyond its hook. The first release needs none; the hook files the act. /// Later acts (the violent ones, the uprisings) override what they need. /// public class ActWorker { public ActDef def; /// The tooltip the lawbook row shows, saying who counts as the offender. Default: the def's description. public virtual string RowTooltip => def?.description; } /// /// A break's "Did it" (Defs/BlaxLaws_BreakLinks.xml): the act that counts, for a mental state class and its /// subclasses. Another mod links its own break class with one of these and no code. /// public class BreakLinkDef : Def { /// The mental state class, subclasses included. public Type stateClass; /// The linked act's key in another category: a single def ("Special/SetFire") or a group ("Ate/@anyDrug"). public string act; /// The row's drug option filters the act (drug binges). public bool usesDrugOption; public override IEnumerable ConfigErrors() { foreach (string e in base.ConfigErrors()) yield return e; if (stateClass == null || !typeof(MentalState).IsAssignableFrom(stateClass)) yield return "stateClass must be a MentalState class"; if (ActKey.CategoryOf(act) == null || ActKey.NameOf(act) == null) yield return "act must be an act key such as Special/SetFire"; if (ActKey.CategoryOf(act) == ActCategories.Break) yield return "act must name an act outside the breaks"; } } /// Resolves the link of a mental state: the most derived linked class wins. public static class BreakLinks { private static readonly Dictionary byClass = new Dictionary(); private static readonly Dictionary hostileByClass = new Dictionary(); /// The link for a break, or null when its class has none (Planning only). public static BreakLinkDef For(MentalStateDef state) => For(state?.stateClass); public static BreakLinkDef For(Type stateClass) { if (stateClass == null) return null; lock (byClass) { if (byClass.TryGetValue(stateClass, out BreakLinkDef cachedLink)) return cachedLink; } BreakLinkDef best = null; int bestDepth = -1; foreach (BreakLinkDef link in DefDatabase.AllDefsListForReading) { if (link.stateClass == null || !link.stateClass.IsAssignableFrom(stateClass)) continue; int depth = Depth(link.stateClass); if (depth > bestDepth) { best = link; bestDepth = depth; } } lock (byClass) byClass[stateClass] = best; return best; } /// /// Whether the act is the link's act: same category and the link's def, or a member of the link's /// group. The drug option is not checked here (Lawbook.BestRow checks it on the break row). /// public static bool Matches(BreakLinkDef link, ActEvent act) { if (link == null || act == null || act.def == null) return false; if (ActKey.CategoryOf(link.act) != act.categoryId) return false; string name = ActKey.NameOf(link.act); if (!ActKey.IsGroup(link.act)) return name == act.def.defName; ActGroup group = ActCatalogue.GroupOf(link.act); return group != null && group.Matches(act); } /// A state hostile to the colony by its class (berserk), from the Break category's hostileStateClasses. public static bool IsHostile(MentalStateDef state) { Type cls = state?.stateClass; if (cls == null) return false; lock (hostileByClass) { if (hostileByClass.TryGetValue(cls, out bool h)) return h; } bool hostile = false; ActCategoryDef breaks = DefDatabase.GetNamedSilentFail(ActCategories.Break); if (breaks?.hostileStateClasses != null) { foreach (Type t in breaks.hostileStateClasses) { if (t != null && t.IsAssignableFrom(cls)) { hostile = true; break; } } } lock (hostileByClass) hostileByClass[cls] = hostile; return hostile; } /// Drops the caches (tests that add a link at runtime). public static void ClearCache() { lock (byClass) byClass.Clear(); lock (hostileByClass) hostileByClass.Clear(); } private static int Depth(Type t) { int d = 0; for (Type x = t; x != null; x = x.BaseType) d++; return d; } } }