Blax Laws 1.0.0: lawbook, act catalogue, enforcement and jail
- Laws tab with the lawbook and the case log; the book starts empty and
the mod watches nothing until a law exists.
- Act catalogue built from the game's defs (breaks, food, drugs, usables,
interactions, recorded acts, records, bills) plus special acts, with
groups, break links and a starter lawbook.
- Enforcement: warn, guilty, subdue, jail for N days and subdue then jail,
one case per incident, danger waits, role holders and mechanitors held
for Arrest now.
- Headless test suite green in every DLC combination, core only and with
the compat mods.
2026-12-02 20:07:17 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Verse;
|
|
|
|
|
|
|
|
|
|
namespace BlaxLaws
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class Watch
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Any category or special act is watched. False for an empty book.</summary>
|
|
|
|
|
public static bool Any;
|
|
|
|
|
|
2026-12-03 18:28:31 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool AnyLaw;
|
|
|
|
|
|
Blax Laws 1.0.0: lawbook, act catalogue, enforcement and jail
- Laws tab with the lawbook and the case log; the book starts empty and
the mod watches nothing until a law exists.
- Act catalogue built from the game's defs (breaks, food, drugs, usables,
interactions, recorded acts, records, bills) plus special acts, with
groups, break links and a starter lawbook.
- Enforcement: warn, guilty, subdue, jail for N days and subdue then jail,
one case per incident, danger waits, role holders and mechanitors held
for Arrest now.
- Headless test suite green in every DLC combination, core only and with
the compat mods.
2026-12-02 20:07:17 +00:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
/// <summary>Bumped on every rebuild.</summary>
|
|
|
|
|
public static int Version { get; private set; }
|
|
|
|
|
|
|
|
|
|
private sealed class CategoryWatch
|
|
|
|
|
{
|
|
|
|
|
public readonly HashSet<Def> defs = new HashSet<Def>();
|
|
|
|
|
/// <summary>A row or link on the category's "any".</summary>
|
|
|
|
|
public bool all;
|
|
|
|
|
/// <summary>A watched group reads the thing, so every act of the category must be checked.</summary>
|
|
|
|
|
public bool thingGroups;
|
|
|
|
|
/// <summary>Enabled rows of this category whose entry resolves, in lawbook order.</summary>
|
|
|
|
|
public readonly List<Law> laws = new List<Law>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<string, CategoryWatch> byCategory = new Dictionary<string, CategoryWatch>();
|
|
|
|
|
private static readonly HashSet<string> specials = new HashSet<string>();
|
|
|
|
|
private static readonly List<Law> NoLaws = new List<Law>();
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether any enabled row (or break link) uses the category.</summary>
|
|
|
|
|
public static bool Category(string categoryId) => categoryId != null && byCategory.ContainsKey(categoryId);
|
|
|
|
|
|
|
|
|
|
/// <summary>Whether a row (or break link) watches this special act ("SetFire").</summary>
|
|
|
|
|
public static bool SpecialAct(string actId) => actId != null && specials.Contains(actId);
|
|
|
|
|
|
|
|
|
|
/// <summary>The def is in the category's watched set, or the category watches everything.</summary>
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>A watched group of the category reads the thing, so a hook must report every act of it.</summary>
|
|
|
|
|
public static bool ThingGroups(string categoryId) =>
|
|
|
|
|
categoryId != null && byCategory.TryGetValue(categoryId, out CategoryWatch w) && w.thingGroups;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Enabled rows of the category whose entry resolves. Empty when none. Do not change the list.</summary>
|
|
|
|
|
public static IReadOnlyList<Law> LawsOf(string categoryId)
|
|
|
|
|
{
|
|
|
|
|
if (categoryId != null && byCategory.TryGetValue(categoryId, out CategoryWatch w))
|
|
|
|
|
return w.laws;
|
|
|
|
|
return NoLaws;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Everything off: an empty book.</summary>
|
|
|
|
|
public static void Clear()
|
|
|
|
|
{
|
2026-12-03 18:28:31 +00:00
|
|
|
AnyLaw = false;
|
Blax Laws 1.0.0: lawbook, act catalogue, enforcement and jail
- Laws tab with the lawbook and the case log; the book starts empty and
the mod watches nothing until a law exists.
- Act catalogue built from the game's defs (breaks, food, drugs, usables,
interactions, recorded acts, records, bills) plus special acts, with
groups, break links and a starter lawbook.
- Enforcement: warn, guilty, subdue, jail for N days and subdue then jail,
one case per incident, danger waits, role holders and mechanitors held
for Arrest now.
- Headless test suite green in every DLC combination, core only and with
the compat mods.
2026-12-02 20:07:17 +00:00
|
|
|
byCategory.Clear();
|
|
|
|
|
specials.Clear();
|
|
|
|
|
SetFlags();
|
|
|
|
|
Version++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Rebuilds from the running game's lawbook. Disabled rows and rows whose entry is missing watch
|
|
|
|
|
/// nothing. Never throws.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Rebuild()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
byCategory.Clear();
|
|
|
|
|
specials.Clear();
|
|
|
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
2026-12-03 18:28:31 +00:00
|
|
|
AnyLaw = comp != null && comp.Laws.Count > 0;
|
Blax Laws 1.0.0: lawbook, act catalogue, enforcement and jail
- Laws tab with the lawbook and the case log; the book starts empty and
the mod watches nothing until a law exists.
- Act catalogue built from the game's defs (breaks, food, drugs, usables,
interactions, recorded acts, records, bills) plus special acts, with
groups, break links and a starter lawbook.
- Enforcement: warn, guilty, subdue, jail for N days and subdue then jail,
one case per incident, danger waits, role holders and mechanitors held
for Arrest now.
- Headless test suite green in every DLC combination, core only and with
the compat mods.
2026-12-02 20:07:17 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|