200 lines
8.4 KiB
C#
200 lines
8.4 KiB
C#
using System;
|
|
using Verse;
|
|
using Verse.AI;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// Turns an act into a case (design: "Act catalogue and detection", How, and "Defaults I picked"). The one
|
|
/// entry point every detection hook calls; it never punishes, it queues: Enforcement starts the
|
|
/// punishment a tick later.
|
|
///
|
|
/// - A break's start (category Break) opens a planning case when a planning row matches.
|
|
/// - An act that is the linked did-it of the offender's current break belongs to that break's case and
|
|
/// opens no other case. When the act also matches a row of its own category, the row with the higher
|
|
/// severity decides, the break's row on a tie. Each stage's punishment fires once per case; later acts
|
|
/// raise the count.
|
|
/// - Any other act: the most specific enabled row of its category decides. Within the cooldown
|
|
/// ("Days between cases") a further act of the same offender under the same row raises the count on
|
|
/// that case, open or closed, and files nothing new.
|
|
/// </summary>
|
|
public static class CaseFiler
|
|
{
|
|
/// <summary>
|
|
/// Files the act. Returns the case that took it (new or existing), or null when no row covers it, the
|
|
/// offender isn't covered, or the act is a player order. Safe to call from any hook; the hook still
|
|
/// wraps the call in its own try/catch.
|
|
/// </summary>
|
|
public static LawCase Report(ActEvent act)
|
|
{
|
|
if (LawsTestHooks.ReportThrows)
|
|
throw new InvalidOperationException("test: CaseFiler.Report throws");
|
|
if (act?.offender == null || act.categoryId == null || !Watch.Any)
|
|
return null;
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
|
if (comp == null || !Offenders.CountsAsOffender(act.offender))
|
|
return null;
|
|
|
|
if (act.categoryId == ActCategories.Break)
|
|
return ReportBreakStart(comp, act);
|
|
|
|
Law own = Lawbook.BestRow(act, LawStage.DidIt);
|
|
LawCase linked = ReportLinkedAct(comp, act, own);
|
|
if (linked != null)
|
|
return linked;
|
|
return own == null ? null : ReportAct(comp, act, own);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Opens a case for a row, at a stage, without matching: for hooks that already know the row (a later
|
|
/// stage of a special flow) and for tests. Sets the stage's row, count, notes and ticks; queues nothing.
|
|
/// </summary>
|
|
public static LawCase Open(Law law, ActEvent act, LawStage stage)
|
|
{
|
|
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
|
|
if (comp == null || law == null || act?.offender == null)
|
|
return null;
|
|
int now = Now;
|
|
var c = new LawCase
|
|
{
|
|
id = comp.TakeCaseId(),
|
|
actKey = law.actKey,
|
|
matchedDef = act.def?.defName,
|
|
stage = stage,
|
|
count = stage == LawStage.DidIt ? 1 : 0,
|
|
notes = act.notes,
|
|
openTick = now,
|
|
lastActTick = now,
|
|
status = CaseStatus.Open,
|
|
};
|
|
c.SetOffender(act.offender);
|
|
c.SetVictim(act.victim);
|
|
c.SetLaw(stage, law);
|
|
comp.AddCase(c);
|
|
return c;
|
|
}
|
|
|
|
/// <summary>The tick the pawn's break started, to within 30 ticks: now minus MentalState.Age.</summary>
|
|
public static int BreakStartTick(MentalState state) => state == null ? Now : Now - state.Age;
|
|
|
|
/// <summary>Whether the case's break is still running on its offender.</summary>
|
|
public static bool StillInBreak(LawCase c)
|
|
{
|
|
if (c == null || !c.IsBreakCase || c.offender == null || c.offender.Dead)
|
|
return false;
|
|
MentalState state = c.offender.MentalState;
|
|
return state?.def != null && state.def.defName == c.breakState &&
|
|
Math.Abs(BreakStartTick(state) - c.breakStartTick) <= GameComponent_BlaxLaws.BreakStartTolerance;
|
|
}
|
|
|
|
private static int Now => Find.TickManager?.TicksGame ?? 0;
|
|
|
|
// ---- the three paths -----------------------------------------------------------------
|
|
|
|
private static LawCase ReportBreakStart(GameComponent_BlaxLaws comp, ActEvent act)
|
|
{
|
|
if (!(act.def is MentalStateDef state))
|
|
return null;
|
|
Law row = Lawbook.BestRow(act, LawStage.Planning);
|
|
if (row == null)
|
|
return null;
|
|
MentalState live = act.mentalState ?? act.offender.MentalState;
|
|
int start = BreakStartTick(live);
|
|
LawCase c = comp.BreakCaseOf(act.offender, state.defName, start);
|
|
if (c != null)
|
|
return c;
|
|
c = Open(row, act, LawStage.Planning);
|
|
c.breakState = state.defName;
|
|
c.breakStartTick = start;
|
|
Enforcement.Enqueue(c, LawStage.Planning);
|
|
return c;
|
|
}
|
|
|
|
private static LawCase ReportLinkedAct(GameComponent_BlaxLaws comp, ActEvent act, Law own)
|
|
{
|
|
MentalState state = act.offender.MentalState;
|
|
if (state?.def == null)
|
|
return null;
|
|
BreakLinkDef link = BreakLinks.For(state.def);
|
|
if (link == null || !BreakLinks.Matches(link, act))
|
|
return null;
|
|
|
|
var breakAct = new ActEvent(ActCategories.Break, state.def, act.offender)
|
|
{
|
|
victim = act.victim,
|
|
thing = act.thing,
|
|
chemical = act.chemical,
|
|
drugCategory = act.drugCategory,
|
|
facts = act.facts,
|
|
notes = act.notes,
|
|
mentalState = state,
|
|
};
|
|
Law breakRow = Lawbook.BestRow(breakAct, LawStage.DidIt);
|
|
int start = BreakStartTick(state);
|
|
LawCase c = comp.BreakCaseOf(act.offender, state.def.defName, start);
|
|
if (c == null && breakRow == null)
|
|
return null; // No law on this break: the act files as any other act.
|
|
|
|
int now = Now;
|
|
if (c == null)
|
|
{
|
|
c = Open(breakRow, breakAct, LawStage.DidIt);
|
|
c.breakState = state.def.defName;
|
|
c.breakStartTick = start;
|
|
c.count = 0;
|
|
}
|
|
c.count++;
|
|
c.lastActTick = now;
|
|
c.notes |= act.notes;
|
|
c.matchedDef = act.def?.defName;
|
|
if (c.stage == LawStage.Planning)
|
|
c.stage = LawStage.DidIt;
|
|
if (c.victim == null && act.victim != null)
|
|
c.SetVictim(act.victim);
|
|
|
|
if (c.didItLawId < 0 && !c.HasPunished(LawStage.DidIt))
|
|
{
|
|
Law decider = breakRow;
|
|
if (own != null && (decider == null || own.severity > decider.severity))
|
|
decider = own;
|
|
if (decider != null)
|
|
{
|
|
c.SetLaw(LawStage.DidIt, decider);
|
|
if (c.IsClosed)
|
|
{
|
|
// A planning case already closed (warned) while the break goes on: the did-it reopens it.
|
|
c.status = CaseStatus.Open;
|
|
c.outcome = null;
|
|
c.closeTick = -1;
|
|
}
|
|
Enforcement.Enqueue(c, LawStage.DidIt);
|
|
}
|
|
}
|
|
return c;
|
|
}
|
|
|
|
private static LawCase ReportAct(GameComponent_BlaxLaws comp, ActEvent act, Law own)
|
|
{
|
|
int now = Now;
|
|
int window = BlaxLawsMod.S.CooldownTicks;
|
|
if (window > 0)
|
|
{
|
|
LawCase recent = comp.LatestActCaseOf(act.offender, own.id);
|
|
// An unresolved case takes every repeat, whatever its age (a held or waiting jail case gets no
|
|
// second case and no second letter). After it closes the window counts from its open tick, so
|
|
// frequent repeats can't push it out forever.
|
|
if (recent != null && (!recent.IsClosed || now - recent.openTick < window))
|
|
{
|
|
recent.count++;
|
|
recent.lastActTick = now;
|
|
recent.notes |= act.notes;
|
|
return recent;
|
|
}
|
|
}
|
|
LawCase c = Open(own, act, LawStage.DidIt);
|
|
Enforcement.Enqueue(c, LawStage.DidIt);
|
|
return c;
|
|
}
|
|
}
|
|
}
|