using System.Collections.Generic; using Verse; namespace BlaxLaws { /// /// The hot-path cache of the current game's lawbook (design: "Detection: one postfix per act, quiet while /// the book is empty"). One static flag per category and per special act, plus per category the set of /// watched defs (groups expanded) and the enabled rows whose entry resolves. Rebuilt on every lawbook /// write and in GameComponent_BlaxLaws.FinalizeInit, cleared when a game starts or loads. Not game state: /// nothing here is saved. /// /// Every hook reads its flag first, so while the book is empty a hook costs one static field read: /// /// if (!Watch.Ate) return; // empty book: done /// if (!Watch.MayMatch(ActCategories.Ate, def)) return; // one set lookup /// CaseFiler.Report(new ActEvent(ActCategories.Ate, def, pawn) { ... }); /// /// A break row with a linked did-it also watches the linked act (a drug binge row watches every drug, /// a fire-starting spree row the SetFire special act), so a dose during a binge reaches CaseFiler. /// public static class Watch { /// Any category or special act is watched. False for an empty book. public static bool Any; /// /// The lawbook has at least one row, disabled rows and rows whose entry is missing included. Hook P /// (the "Court place" switch) reads it first, so while the book is empty the hook costs one field read. /// public static bool AnyLaw; // One flag per category: some enabled row (or a break row's link) uses it. public static bool Break; public static bool Ate; public static bool Used; public static bool Interaction; public static bool RecordedAct; public static bool Record; public static bool Bill; public static bool Special; // One flag per special act of the first release. public static bool SetFire; public static bool DisplayedCorpse; public static bool StartedSocialFight; /// Bumped on every rebuild. public static int Version { get; private set; } private sealed class CategoryWatch { public readonly HashSet defs = new HashSet(); /// A row or link on the category's "any". public bool all; /// A watched group reads the thing, so every act of the category must be checked. public bool thingGroups; /// Enabled rows of this category whose entry resolves, in lawbook order. public readonly List laws = new List(); } private static readonly Dictionary byCategory = new Dictionary(); private static readonly HashSet specials = new HashSet(); private static readonly List NoLaws = new List(); /// Whether any enabled row (or break link) uses the category. public static bool Category(string categoryId) => categoryId != null && byCategory.ContainsKey(categoryId); /// Whether a row (or break link) watches this special act ("SetFire"). public static bool SpecialAct(string actId) => actId != null && specials.Contains(actId); /// The def is in the category's watched set, or the category watches everything. public static bool IsWatched(string categoryId, Def def) { if (categoryId == null || !byCategory.TryGetValue(categoryId, out CategoryWatch w)) return false; return w.all || (def != null && w.defs.Contains(def)); } /// A watched group of the category reads the thing, so a hook must report every act of it. public static bool ThingGroups(string categoryId) => categoryId != null && byCategory.TryGetValue(categoryId, out CategoryWatch w) && w.thingGroups; /// /// The act may match a row or a link: the def is watched, or a thing-level group needs the act. A /// hook builds its ActEvent and calls CaseFiler.Report only when this is true. /// public static bool MayMatch(string categoryId, Def def) { if (categoryId == null || !byCategory.TryGetValue(categoryId, out CategoryWatch w)) return false; return w.all || w.thingGroups || (def != null && w.defs.Contains(def)); } /// Enabled rows of the category whose entry resolves. Empty when none. Do not change the list. public static IReadOnlyList LawsOf(string categoryId) { if (categoryId != null && byCategory.TryGetValue(categoryId, out CategoryWatch w)) return w.laws; return NoLaws; } /// Everything off: an empty book. public static void Clear() { AnyLaw = false; byCategory.Clear(); specials.Clear(); SetFlags(); Version++; } /// /// Rebuilds from the running game's lawbook. Disabled rows and rows whose entry is missing watch /// nothing. Never throws. /// public static void Rebuild() { try { byCategory.Clear(); specials.Clear(); GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance; AnyLaw = comp != null && comp.Laws.Count > 0; if (comp != null) { foreach (Law law in comp.Laws) { if (law == null || !law.enabled) continue; ActEntry entry = ActCatalogue.Get(law.actKey); if (entry == null || !entry.HasStage(law.stage)) continue; Of(entry.categoryId).laws.Add(law); AddEntry(entry); if (entry.IsBreak) { foreach (BreakLinkDef link in entry.links) { ActEntry linked = ActCatalogue.Get(link.act); if (linked != null) AddEntry(linked); } } } } } catch (System.Exception e) { HookGuard.Fail("Rebuilding the watched acts", e); } SetFlags(); Version++; } private static CategoryWatch Of(string categoryId) { if (!byCategory.TryGetValue(categoryId, out CategoryWatch w)) byCategory[categoryId] = w = new CategoryWatch(); return w; } private static void AddEntry(ActEntry entry) { CategoryWatch w = Of(entry.categoryId); if (entry.categoryId == ActCategories.Special) specials.Add(entry.name); if (entry.def != null) { w.defs.Add(entry.def); return; } ActGroup g = entry.group; if (g == null) return; if (g.IsAny) { w.all = true; if (entry.categoryId == ActCategories.Special) { foreach (ActEntry e in ActCatalogue.EntriesOf(ActCategories.Special)) { if (e.def != null) specials.Add(e.name); } } return; } if (g.DependsOnThing) w.thingGroups = true; foreach (ActEntry e in ActCatalogue.EntriesOf(entry.categoryId)) { if (e.def != null && g.MatchesDef(e.def)) { w.defs.Add(e.def); if (entry.categoryId == ActCategories.Special) specials.Add(e.name); } } } private static void SetFlags() { Break = Category(ActCategories.Break); Ate = Category(ActCategories.Ate); Used = Category(ActCategories.Used); Interaction = Category(ActCategories.Interaction); RecordedAct = Category(ActCategories.RecordedAct); Record = Category(ActCategories.Record); Bill = Category(ActCategories.Bill); Special = Category(ActCategories.Special); SetFire = SpecialAct(SpecialActs.SetFire); DisplayedCorpse = SpecialAct(SpecialActs.DisplayedCorpse); StartedSocialFight = SpecialAct(SpecialActs.StartedSocialFight); Any = byCategory.Count > 0; } } }