400 lines
17 KiB
C#
400 lines
17 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxLaws
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Every write to the lawbook goes through here (design: "Lawbook tab", How), and so does the "most
|
||
|
|
/// specific row wins" match. Each write rebuilds Watch. Action, Option and Stage are a row's key: a write
|
||
|
|
/// that would make a second row with the same key is refused and names the existing row.
|
||
|
|
/// </summary>
|
||
|
|
public static class Lawbook
|
||
|
|
{
|
||
|
|
public const int MinDays = 1;
|
||
|
|
public const int MaxDays = 50;
|
||
|
|
|
||
|
|
private static readonly List<Law> NoLaws = new List<Law>();
|
||
|
|
|
||
|
|
/// <summary>The running game's rows, or an empty list without a game.</summary>
|
||
|
|
public static IReadOnlyList<Law> Laws => GameComponent_BlaxLaws.Instance?.Laws ?? NoLaws;
|
||
|
|
|
||
|
|
/// <summary>The default days of a severity: Minor 1, Moderate 3, Serious 5, Grave 10.</summary>
|
||
|
|
public static int DefaultDays(LawSeverity severity)
|
||
|
|
{
|
||
|
|
switch (severity)
|
||
|
|
{
|
||
|
|
case LawSeverity.Minor: return 1;
|
||
|
|
case LawSeverity.Moderate: return 3;
|
||
|
|
case LawSeverity.Serious: return 5;
|
||
|
|
default: return 10;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static int ClampDays(int days) => days < MinDays ? MinDays : days > MaxDays ? MaxDays : days;
|
||
|
|
|
||
|
|
/// <summary>Null, "" and "@any" read as the same option (no option for a non-binge row, any drug for a binge row).</summary>
|
||
|
|
public static string NormalizeOption(string option) => option.NullOrEmpty() || option == DrugOptions.Any ? null : option;
|
||
|
|
|
||
|
|
// ---- lookups -------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>The row with this key, or null.</summary>
|
||
|
|
public static Law Find(string actKey, string option, LawStage stage)
|
||
|
|
{
|
||
|
|
foreach (Law l in Laws)
|
||
|
|
{
|
||
|
|
if (l.HasKey(actKey, option, stage))
|
||
|
|
return l;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The stages an entry offers (planning only for breaks; did it for linked breaks and every other entry).</summary>
|
||
|
|
public static IEnumerable<LawStage> StagesOf(ActEntry entry) => entry?.Stages ?? Enumerable.Empty<LawStage>();
|
||
|
|
|
||
|
|
/// <summary>Whether the punishment fits the entry and stage (PunishmentDef.fits).</summary>
|
||
|
|
public static bool Fits(PunishmentDef punishment, ActEntry entry, LawStage stage) =>
|
||
|
|
punishment != null && entry != null && punishment.Fits(entry, stage);
|
||
|
|
|
||
|
|
/// <summary>The punishments a row of this entry and stage offers, in order.</summary>
|
||
|
|
public static IEnumerable<PunishmentDef> PunishmentsFor(ActEntry entry, LawStage stage) =>
|
||
|
|
DefDatabase<PunishmentDef>.AllDefsListForReading.Where(p => Fits(p, entry, stage)).OrderBy(p => p.order);
|
||
|
|
|
||
|
|
/// <summary>"Needs Ideology" or "Missing: Core" for a row whose entry is missing, else null.</summary>
|
||
|
|
public static string MissingText(Law law) => law?.MissingText;
|
||
|
|
|
||
|
|
// ---- writes --------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A new row for the entry, not added yet: the entry's first stage, Moderate, Warn, 3 days and On; a
|
||
|
|
/// drug binge row gets the Any option. A missing entry gives a did-it row.
|
||
|
|
/// </summary>
|
||
|
|
public static Law NewRow(string actKey)
|
||
|
|
{
|
||
|
|
ActEntry entry = ActCatalogue.Get(actKey);
|
||
|
|
var law = new Law
|
||
|
|
{
|
||
|
|
actKey = actKey,
|
||
|
|
stage = entry?.FirstStage ?? LawStage.DidIt,
|
||
|
|
severity = LawSeverity.Moderate,
|
||
|
|
punishment = BlaxLawsDefOf.Warn?.defName ?? "Warn",
|
||
|
|
days = DefaultDays(LawSeverity.Moderate),
|
||
|
|
enabled = true,
|
||
|
|
option = entry != null && entry.hasDrugOption ? DrugOptions.Any : null,
|
||
|
|
};
|
||
|
|
Remember(law, entry);
|
||
|
|
return law;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Adds a prepared row (from NewRow, the starter list or a test). Gives it an id and, when its entry
|
||
|
|
/// resolves, the entry's label and source. Refuses a row whose key exists: then existing is that row.
|
||
|
|
/// </summary>
|
||
|
|
public static bool TryAdd(Law law, out Law existing)
|
||
|
|
{
|
||
|
|
existing = null;
|
||
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
||
|
|
if (comp == null || law == null || law.actKey.NullOrEmpty())
|
||
|
|
return false;
|
||
|
|
existing = Find(law.actKey, law.option, law.stage);
|
||
|
|
if (existing != null)
|
||
|
|
return false;
|
||
|
|
if (law.id <= 0 || comp.LawById(law.id) != null)
|
||
|
|
law.id = comp.TakeLawId();
|
||
|
|
law.days = ClampDays(law.days);
|
||
|
|
Remember(law, ActCatalogue.Get(law.actKey));
|
||
|
|
comp.AddLaw(law);
|
||
|
|
Changed();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Adds NewRow(actKey). Returns the new row, or null when a row with that key exists (Find gives it).</summary>
|
||
|
|
public static Law Add(string actKey)
|
||
|
|
{
|
||
|
|
Law law = NewRow(actKey);
|
||
|
|
return TryAdd(law, out _) ? law : null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Changes the row's action. The stage moves to the entry's first stage if the entry lacks it, a
|
||
|
|
/// punishment that no longer fits resets to Warn, and the option follows the entry. Refused when the
|
||
|
|
/// new key exists: then existing is that row.
|
||
|
|
/// </summary>
|
||
|
|
public static bool SetAction(Law law, string actKey, out Law existing)
|
||
|
|
{
|
||
|
|
existing = null;
|
||
|
|
if (law == null || actKey.NullOrEmpty())
|
||
|
|
return false;
|
||
|
|
ActEntry entry = ActCatalogue.Get(actKey);
|
||
|
|
LawStage stage = entry == null || entry.HasStage(law.stage) ? law.stage : entry.FirstStage;
|
||
|
|
string option = entry != null && entry.hasDrugOption ? law.option ?? DrugOptions.Any : null;
|
||
|
|
existing = FindOther(law, actKey, option, stage);
|
||
|
|
if (existing != null)
|
||
|
|
return false;
|
||
|
|
law.actKey = actKey;
|
||
|
|
law.stage = stage;
|
||
|
|
law.option = option;
|
||
|
|
Remember(law, entry);
|
||
|
|
ResetPunishmentIfUnfit(law, entry);
|
||
|
|
Changed();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Changes a drug binge row's option. Refused when the key exists.</summary>
|
||
|
|
public static bool SetOption(Law law, string option, out Law existing)
|
||
|
|
{
|
||
|
|
existing = null;
|
||
|
|
if (law == null)
|
||
|
|
return false;
|
||
|
|
existing = FindOther(law, law.actKey, option, law.stage);
|
||
|
|
if (existing != null)
|
||
|
|
return false;
|
||
|
|
law.option = option;
|
||
|
|
Changed();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Changes the row's stage; a punishment that no longer fits resets to Warn. Refused when the key exists.</summary>
|
||
|
|
public static bool SetStage(Law law, LawStage stage, out Law existing)
|
||
|
|
{
|
||
|
|
existing = null;
|
||
|
|
if (law == null)
|
||
|
|
return false;
|
||
|
|
ActEntry entry = law.Entry;
|
||
|
|
if (entry != null && !entry.HasStage(stage))
|
||
|
|
return false;
|
||
|
|
existing = FindOther(law, law.actKey, law.option, stage);
|
||
|
|
if (existing != null)
|
||
|
|
return false;
|
||
|
|
law.stage = stage;
|
||
|
|
ResetPunishmentIfUnfit(law, entry);
|
||
|
|
Changed();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Changes the severity. Days move to the new default only while they still equal the old default.</summary>
|
||
|
|
public static void SetSeverity(Law law, LawSeverity severity)
|
||
|
|
{
|
||
|
|
if (law == null || law.severity == severity)
|
||
|
|
return;
|
||
|
|
if (law.days == DefaultDays(law.severity))
|
||
|
|
law.days = DefaultDays(severity);
|
||
|
|
law.severity = severity;
|
||
|
|
Changed();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Changes the punishment. Refused (false) when it doesn't fit the row's entry and stage.</summary>
|
||
|
|
public static bool SetPunishment(Law law, PunishmentDef punishment)
|
||
|
|
{
|
||
|
|
if (law == null || punishment == null)
|
||
|
|
return false;
|
||
|
|
ActEntry entry = law.Entry;
|
||
|
|
if (entry != null && !punishment.Fits(entry, law.stage))
|
||
|
|
return false;
|
||
|
|
law.punishment = punishment.defName;
|
||
|
|
Changed();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void SetDays(Law law, int days)
|
||
|
|
{
|
||
|
|
if (law == null)
|
||
|
|
return;
|
||
|
|
law.days = ClampDays(days);
|
||
|
|
Changed();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void SetEnabled(Law law, bool enabled)
|
||
|
|
{
|
||
|
|
if (law == null || law.enabled == enabled)
|
||
|
|
return;
|
||
|
|
law.enabled = enabled;
|
||
|
|
Changed();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Moves the row to a new place in the list.</summary>
|
||
|
|
public static void Move(Law law, int index)
|
||
|
|
{
|
||
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
||
|
|
if (comp == null || law == null)
|
||
|
|
return;
|
||
|
|
comp.MoveLaw(law, index);
|
||
|
|
Changed();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>The open cases a delete would close; the UI names the count and asks first.</summary>
|
||
|
|
public static int OpenCaseCount(Law law) => GameComponent_BlaxLaws.Instance?.OpenCaseCount(law) ?? 0;
|
||
|
|
|
||
|
|
/// <summary>Deletes the row. Its open cases close as "law repealed". Returns how many closed.</summary>
|
||
|
|
public static int Delete(Law law)
|
||
|
|
{
|
||
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
||
|
|
if (comp == null || law == null || !comp.RemoveLaw(law))
|
||
|
|
return 0;
|
||
|
|
int closed = 0;
|
||
|
|
foreach (LawCase c in comp.OpenCases())
|
||
|
|
{
|
||
|
|
if (c.planningLawId != law.id && c.didItLawId != law.id)
|
||
|
|
continue;
|
||
|
|
PunishmentDef active = c.activePunishment.NullOrEmpty() ? null : DefDatabase<PunishmentDef>.GetNamedSilentFail(c.activePunishment);
|
||
|
|
if (active != null)
|
||
|
|
HookGuard.Run("Stopping " + active.defName, () => active.Worker.Stop(c));
|
||
|
|
comp.CloseCase(c, CaseOutcomes.Repealed);
|
||
|
|
closed++;
|
||
|
|
}
|
||
|
|
Changed();
|
||
|
|
return closed;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- starter lawbook -----------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>One suggested row.</summary>
|
||
|
|
public readonly struct StarterRow
|
||
|
|
{
|
||
|
|
public readonly string actKey;
|
||
|
|
public readonly string option;
|
||
|
|
public readonly LawStage stage;
|
||
|
|
public readonly LawSeverity severity;
|
||
|
|
public readonly string punishment;
|
||
|
|
public readonly int days;
|
||
|
|
|
||
|
|
public StarterRow(string actKey, string option, LawStage stage, LawSeverity severity, string punishment, int days)
|
||
|
|
{
|
||
|
|
this.actKey = actKey;
|
||
|
|
this.option = option;
|
||
|
|
this.stage = stage;
|
||
|
|
this.severity = severity;
|
||
|
|
this.punishment = punishment;
|
||
|
|
this.days = days;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The seven suggested rows (design: "Defaults I picked"), Core entries only. "Ate: any meat" is left
|
||
|
|
/// out, since most colonies eat meat.
|
||
|
|
/// </summary>
|
||
|
|
public static readonly StarterRow[] StarterRows =
|
||
|
|
{
|
||
|
|
new StarterRow(ActKey.OfGroup(ActCategories.Break, ActGroups.AnyDrugBinge), DrugOptions.Any, LawStage.Planning, LawSeverity.Moderate, "Subdue", 3),
|
||
|
|
new StarterRow(ActKey.OfGroup(ActCategories.Break, ActGroups.AnyDrugBinge), DrugOptions.Any, LawStage.DidIt, LawSeverity.Serious, "Jail", 5),
|
||
|
|
new StarterRow(ActKey.Of(ActCategories.Break, "FireStartingSpree"), null, LawStage.Planning, LawSeverity.Serious, "Subdue", 5),
|
||
|
|
new StarterRow(ActKey.Of(ActCategories.Break, "FireStartingSpree"), null, LawStage.DidIt, LawSeverity.Grave, "Jail", 10),
|
||
|
|
new StarterRow(ActKey.Of(ActCategories.Break, "CorpseObsession"), null, LawStage.Planning, LawSeverity.Moderate, "Subdue", 3),
|
||
|
|
new StarterRow(ActKey.Of(ActCategories.Special, SpecialActs.StartedSocialFight), null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1),
|
||
|
|
new StarterRow(ActKey.Of(ActCategories.Interaction, "Insult"), null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1),
|
||
|
|
};
|
||
|
|
|
||
|
|
/// <summary>Adds the starter rows that are missing. Returns how many were added (0 when pressed again).</summary>
|
||
|
|
public static int AddStarter()
|
||
|
|
{
|
||
|
|
int added = 0;
|
||
|
|
foreach (StarterRow s in StarterRows)
|
||
|
|
{
|
||
|
|
if (Find(s.actKey, s.option, s.stage) != null)
|
||
|
|
continue;
|
||
|
|
var law = new Law
|
||
|
|
{
|
||
|
|
actKey = s.actKey,
|
||
|
|
option = s.option,
|
||
|
|
stage = s.stage,
|
||
|
|
severity = s.severity,
|
||
|
|
punishment = s.punishment,
|
||
|
|
days = s.days,
|
||
|
|
enabled = true,
|
||
|
|
// Every starter entry is Core's; this is what a missing entry shows until the catalogue knows it.
|
||
|
|
savedLabel = ActKey.NameOf(s.actKey),
|
||
|
|
savedMod = DlcSupport.CoreName,
|
||
|
|
savedPackageId = DlcSupport.CorePackageId,
|
||
|
|
};
|
||
|
|
if (TryAdd(law, out _))
|
||
|
|
added++;
|
||
|
|
}
|
||
|
|
return added;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- matching ------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// The most specific enabled row of the act's category for the stage whose entry matches the act, or
|
||
|
|
/// null. Specificity: a single def over a group (the smaller group first) over the category's "any";
|
||
|
|
/// then, for drug binge rows, one chemical over hard or social over any drug; then the older row.
|
||
|
|
/// Rows whose entry is missing never match.
|
||
|
|
/// </summary>
|
||
|
|
public static Law BestRow(ActEvent act, LawStage stage)
|
||
|
|
{
|
||
|
|
if (act == null || act.categoryId == null)
|
||
|
|
return null;
|
||
|
|
IReadOnlyList<Law> rows = Watch.LawsOf(act.categoryId);
|
||
|
|
Law best = null;
|
||
|
|
ActEntry bestEntry = null;
|
||
|
|
for (int i = 0; i < rows.Count; i++)
|
||
|
|
{
|
||
|
|
Law law = rows[i];
|
||
|
|
if (!law.enabled || law.stage != stage)
|
||
|
|
continue;
|
||
|
|
ActEntry entry = ActCatalogue.Get(law.actKey);
|
||
|
|
if (entry == null || !entry.Matches(act))
|
||
|
|
continue;
|
||
|
|
if (entry.hasDrugOption && !DrugOptions.Matches(law.option, act))
|
||
|
|
continue;
|
||
|
|
if (best == null || MoreSpecific(entry, law, bestEntry, best))
|
||
|
|
{
|
||
|
|
best = law;
|
||
|
|
bestEntry = entry;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return best;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Whether row a (entry ea) is more specific than row b (entry eb).</summary>
|
||
|
|
public static bool MoreSpecific(ActEntry ea, Law a, ActEntry eb, Law b)
|
||
|
|
{
|
||
|
|
if (ea.Rank != eb.Rank)
|
||
|
|
return ea.Rank > eb.Rank;
|
||
|
|
if (ea.Rank == 2)
|
||
|
|
{
|
||
|
|
int sa = ea.group?.size ?? int.MaxValue, sb = eb.group?.size ?? int.MaxValue;
|
||
|
|
if (sa != sb)
|
||
|
|
return sa < sb;
|
||
|
|
}
|
||
|
|
int oa = ea.hasDrugOption ? DrugOptions.Rank(a.option) : 0;
|
||
|
|
int ob = eb.hasDrugOption ? DrugOptions.Rank(b.option) : 0;
|
||
|
|
if (oa != ob)
|
||
|
|
return oa > ob;
|
||
|
|
return a.id < b.id;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- helpers -------------------------------------------------------------------------
|
||
|
|
|
||
|
|
private static Law FindOther(Law self, string actKey, string option, LawStage stage)
|
||
|
|
{
|
||
|
|
foreach (Law l in Laws)
|
||
|
|
{
|
||
|
|
if (l != self && l.HasKey(actKey, option, stage))
|
||
|
|
return l;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Stores the entry's label and source on the row, so a missing entry still shows them.</summary>
|
||
|
|
private static void Remember(Law law, ActEntry entry)
|
||
|
|
{
|
||
|
|
if (entry == null)
|
||
|
|
return;
|
||
|
|
law.savedLabel = entry.label;
|
||
|
|
law.savedMod = entry.sourceMod;
|
||
|
|
law.savedPackageId = entry.sourcePackageId;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void ResetPunishmentIfUnfit(Law law, ActEntry entry)
|
||
|
|
{
|
||
|
|
PunishmentDef p = law.PunishmentDef;
|
||
|
|
if (entry != null && (p == null || !p.Fits(entry, law.stage)))
|
||
|
|
law.punishment = BlaxLawsDefOf.Warn?.defName ?? "Warn";
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>After every write: the hot-path cache follows the book.</summary>
|
||
|
|
private static void Changed() => Watch.Rebuild();
|
||
|
|
}
|
||
|
|
}
|