242 lines
10 KiB
C#
242 lines
10 KiB
C#
using RimWorld;
|
|
using RimWorld.Planet;
|
|
using Verse;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// One incident: a break from its start to its end, or one act outside a break (plus the repeats the
|
|
/// cooldown folds into it). Saved deep in GameComponent_BlaxLaws as names, numbers and pawn references;
|
|
/// a reference to a pawn that is gone becomes null on save and after load, and the case keeps the name
|
|
/// (design: "Lawbook and cases in one GameComponent").
|
|
///
|
|
/// CaseFiler opens cases; Enforcement and the punishment workers move them on. Write the fields from
|
|
/// those places only; close a case with GameComponent_BlaxLaws.CloseCase.
|
|
/// </summary>
|
|
public class LawCase : IExposable
|
|
{
|
|
public int id;
|
|
|
|
// ---- who ------------------------------------------------------------------------------
|
|
|
|
public Pawn offender;
|
|
/// <summary>The offender's short name when the case was filed, kept after the pawn is gone.</summary>
|
|
public string offenderName;
|
|
public string offenderId;
|
|
|
|
public Pawn victim;
|
|
public string victimName;
|
|
public string victimId;
|
|
|
|
// ---- what -----------------------------------------------------------------------------
|
|
|
|
/// <summary>The act key of the row that opened the case.</summary>
|
|
public string actKey;
|
|
|
|
/// <summary>The defName that matched (which drug, which recipe, which break). Updated by a later act of the case.</summary>
|
|
public string matchedDef;
|
|
|
|
/// <summary>Break cases: the MentalStateDef's defName. Null for acts outside a break.</summary>
|
|
public string breakState;
|
|
|
|
/// <summary>Break cases: the tick the break started (TicksGame minus MentalState.Age, to within 30 ticks).</summary>
|
|
public int breakStartTick = -1;
|
|
|
|
/// <summary>The row whose punishment the planning stage runs, or -1.</summary>
|
|
public int planningLawId = -1;
|
|
|
|
/// <summary>The row whose punishment the did-it stage runs, or -1. Set once per case.</summary>
|
|
public int didItLawId = -1;
|
|
|
|
/// <summary>The highest severity of the case's rows, kept for the log after a row is deleted.</summary>
|
|
public LawSeverity severity;
|
|
|
|
/// <summary>The stage reached.</summary>
|
|
public LawStage stage;
|
|
|
|
/// <summary>Did-it acts in this case: doses during a binge, repeats inside the cooldown. 0 at planning.</summary>
|
|
public int count;
|
|
|
|
public CaseNotes notes;
|
|
|
|
// ---- when and how far -----------------------------------------------------------------
|
|
|
|
public int openTick;
|
|
public int lastActTick;
|
|
public int closeTick = -1;
|
|
public CaseStatus status = CaseStatus.Open;
|
|
|
|
/// <summary>CaseOutcomes key once closed, else null.</summary>
|
|
public string outcome;
|
|
|
|
/// <summary>Which stages' punishments were dispatched (each row fires once per case).</summary>
|
|
public StageMask punishedStages;
|
|
|
|
/// <summary>The PunishmentDef defName running for this case (Enforcement calls its Sweep), or null.</summary>
|
|
public string activePunishment;
|
|
|
|
/// <summary>The stage activePunishment runs for.</summary>
|
|
public LawStage activeStage;
|
|
|
|
/// <summary>The pawn carrying it out, one enforcement at a time.</summary>
|
|
public Pawn enforcer;
|
|
|
|
/// <summary>Why enforcement waits, as a Keyed key the Cases tab shows, or null. Set by the punishment workers.</summary>
|
|
public string waitReason;
|
|
|
|
// ---- jail -----------------------------------------------------------------------------
|
|
|
|
/// <summary>The tick the offender became Laws' prisoner (hook K), or -1.</summary>
|
|
public int captureTick = -1;
|
|
|
|
/// <summary>"Maintain only" was written two ticks after the capture.</summary>
|
|
public bool maintainOnlyDone;
|
|
|
|
/// <summary>Capture tick plus the sentence, or -1.</summary>
|
|
public int releaseTick = -1;
|
|
|
|
/// <summary>The tick a jailed offender escaped off the map (the escape hook), or -1; reset when it is back on a map.</summary>
|
|
public int escapedTick = -1;
|
|
|
|
/// <summary>The bed the offender owned right before the arrest job took it, given back when the arrest fails.</summary>
|
|
public Building_Bed oldBed;
|
|
|
|
/// <summary>"Arrest now" lifted the hold for this case: from here on it arrests a role holder or mechanitor like anyone.</summary>
|
|
public bool holdLifted;
|
|
|
|
// ---- reads ----------------------------------------------------------------------------
|
|
|
|
public bool IsClosed => status == CaseStatus.Closed;
|
|
|
|
public bool IsBreakCase => !breakState.NullOrEmpty();
|
|
|
|
public MentalStateDef BreakStateDef => breakState.NullOrEmpty() ? null : DefDatabase<MentalStateDef>.GetNamedSilentFail(breakState);
|
|
|
|
public int LawIdFor(LawStage s) => s == LawStage.Planning ? planningLawId : didItLawId;
|
|
|
|
/// <summary>The row the stage runs, or null (none, or deleted).</summary>
|
|
public Law LawFor(LawStage s) => GameComponent_BlaxLaws.Instance?.LawById(LawIdFor(s));
|
|
|
|
/// <summary>The row the case shows: did it, else planning, else null.</summary>
|
|
public Law CurrentLaw => LawFor(LawStage.DidIt) ?? LawFor(LawStage.Planning);
|
|
|
|
public bool HasPunished(LawStage s) => (punishedStages & LawLabels.Mask(s)) != 0;
|
|
|
|
public void MarkPunished(LawStage s) => punishedStages |= LawLabels.Mask(s);
|
|
|
|
/// <summary>The offender's name, live while the pawn exists.</summary>
|
|
public string OffenderLabel => offender != null ? offender.LabelShort : offenderName ?? "?";
|
|
|
|
public string VictimLabel => victim != null ? victim.LabelShort : victimName;
|
|
|
|
/// <summary>What the case is about: the current row's label, else the act key.</summary>
|
|
public string Label => CurrentLaw?.Label ?? actKey;
|
|
|
|
// ---- writes for CaseFiler, Enforcement and the punishment workers -----------------------
|
|
|
|
public void SetOffender(Pawn p)
|
|
{
|
|
offender = p;
|
|
offenderName = p?.LabelShort;
|
|
offenderId = p?.ThingID;
|
|
}
|
|
|
|
public void SetVictim(Pawn p)
|
|
{
|
|
victim = p;
|
|
victimName = p?.LabelShort;
|
|
victimId = p?.ThingID;
|
|
}
|
|
|
|
/// <summary>Sets the row a stage runs and raises the case's severity to it.</summary>
|
|
public void SetLaw(LawStage s, Law law)
|
|
{
|
|
if (law == null)
|
|
return;
|
|
if (s == LawStage.Planning)
|
|
planningLawId = law.id;
|
|
else
|
|
didItLawId = law.id;
|
|
if (law.severity > severity)
|
|
severity = law.severity;
|
|
}
|
|
|
|
/// <summary>Nulls references to pawns and things that are gone, so the save holds no reference it can't load. Names stay.</summary>
|
|
public void DropGoneReferences()
|
|
{
|
|
if (IsGone(offender))
|
|
offender = null;
|
|
if (IsGone(victim))
|
|
victim = null;
|
|
if (IsGone(enforcer))
|
|
enforcer = null;
|
|
if (oldBed != null && (oldBed.Destroyed || oldBed.Discarded))
|
|
oldBed = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Null, discarded, or destroyed without being dead. A dead pawn stays while its corpse or the world
|
|
/// still holds it, so its reference resolves on load (the Castes rule).
|
|
/// </summary>
|
|
public static bool IsGone(Pawn pawn)
|
|
{
|
|
if (pawn == null || pawn.Discarded)
|
|
return true;
|
|
if (!pawn.Destroyed)
|
|
return false;
|
|
if (!pawn.Dead)
|
|
return true;
|
|
return pawn.Corpse == null && !pawn.IsWorldPawn();
|
|
}
|
|
|
|
public void ExposeData()
|
|
{
|
|
if (Scribe.mode == LoadSaveMode.Saving)
|
|
DropGoneReferences();
|
|
|
|
Scribe_Values.Look(ref id, "id");
|
|
Scribe_References.Look(ref offender, "offender", saveDestroyedThings: true);
|
|
Scribe_Values.Look(ref offenderName, "offenderName");
|
|
Scribe_Values.Look(ref offenderId, "offenderId");
|
|
Scribe_References.Look(ref victim, "victim", saveDestroyedThings: true);
|
|
Scribe_Values.Look(ref victimName, "victimName");
|
|
Scribe_Values.Look(ref victimId, "victimId");
|
|
|
|
Scribe_Values.Look(ref actKey, "actKey");
|
|
Scribe_Values.Look(ref matchedDef, "matchedDef");
|
|
Scribe_Values.Look(ref breakState, "breakState");
|
|
Scribe_Values.Look(ref breakStartTick, "breakStartTick", -1);
|
|
Scribe_Values.Look(ref planningLawId, "planningLawId", -1);
|
|
Scribe_Values.Look(ref didItLawId, "didItLawId", -1);
|
|
Scribe_Values.Look(ref severity, "severity", LawSeverity.Minor);
|
|
Scribe_Values.Look(ref stage, "stage", LawStage.Planning);
|
|
Scribe_Values.Look(ref count, "count");
|
|
Scribe_Values.Look(ref notes, "notes", CaseNotes.None);
|
|
|
|
Scribe_Values.Look(ref openTick, "openTick");
|
|
Scribe_Values.Look(ref lastActTick, "lastActTick");
|
|
Scribe_Values.Look(ref closeTick, "closeTick", -1);
|
|
Scribe_Values.Look(ref status, "status", CaseStatus.Open);
|
|
Scribe_Values.Look(ref outcome, "outcome");
|
|
Scribe_Values.Look(ref punishedStages, "punishedStages", StageMask.None);
|
|
Scribe_Values.Look(ref activePunishment, "activePunishment");
|
|
Scribe_Values.Look(ref activeStage, "activeStage", LawStage.Planning);
|
|
Scribe_References.Look(ref enforcer, "enforcer", saveDestroyedThings: true);
|
|
Scribe_Values.Look(ref waitReason, "waitReason");
|
|
|
|
Scribe_Values.Look(ref captureTick, "captureTick", -1);
|
|
Scribe_Values.Look(ref maintainOnlyDone, "maintainOnlyDone");
|
|
Scribe_Values.Look(ref releaseTick, "releaseTick", -1);
|
|
Scribe_Values.Look(ref escapedTick, "escapedTick", -1);
|
|
Scribe_References.Look(ref oldBed, "oldBed");
|
|
Scribe_Values.Look(ref holdLifted, "holdLifted");
|
|
|
|
if (Scribe.mode == LoadSaveMode.PostLoadInit)
|
|
DropGoneReferences();
|
|
}
|
|
|
|
public override string ToString() =>
|
|
"Case " + id + " " + OffenderLabel + " " + actKey + " " + stage + " x" + count + " " + status + (outcome != null ? " (" + outcome + ")" : "");
|
|
}
|
|
}
|