using RimWorld; using RimWorld.Planet; using Verse; namespace BlaxLaws { /// /// 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. /// public class LawCase : IExposable { public int id; // ---- who ------------------------------------------------------------------------------ public Pawn offender; /// The offender's short name when the case was filed, kept after the pawn is gone. public string offenderName; public string offenderId; public Pawn victim; public string victimName; public string victimId; // ---- what ----------------------------------------------------------------------------- /// The act key of the row that opened the case. public string actKey; /// The defName that matched (which drug, which recipe, which break). Updated by a later act of the case. public string matchedDef; /// Break cases: the MentalStateDef's defName. Null for acts outside a break. public string breakState; /// Break cases: the tick the break started (TicksGame minus MentalState.Age, to within 30 ticks). public int breakStartTick = -1; /// The row whose punishment the planning stage runs, or -1. public int planningLawId = -1; /// The row whose punishment the did-it stage runs, or -1. Set once per case. public int didItLawId = -1; /// The highest severity of the case's rows, kept for the log after a row is deleted. public LawSeverity severity; /// The stage reached. public LawStage stage; /// Did-it acts in this case: doses during a binge, repeats inside the cooldown. 0 at planning. 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; /// CaseOutcomes key once closed, else null. public string outcome; /// Which stages' punishments were dispatched (each row fires once per case). public StageMask punishedStages; /// The PunishmentDef defName running for this case (Enforcement calls its Sweep), or null. public string activePunishment; /// The stage activePunishment runs for. public LawStage activeStage; /// The pawn carrying it out, one enforcement at a time. public Pawn enforcer; /// Why enforcement waits, as a Keyed key the Cases tab shows, or null. Set by the punishment workers. public string waitReason; // ---- jail ----------------------------------------------------------------------------- /// The tick the offender became Laws' prisoner (hook K), or -1. public int captureTick = -1; /// "Maintain only" was written two ticks after the capture. public bool maintainOnlyDone; /// Capture tick plus the sentence, or -1. public int releaseTick = -1; /// The bed the offender owned right before the arrest job took it, given back when the arrest fails. public Building_Bed oldBed; /// "Arrest now" lifted the hold for this case: from here on it arrests a role holder or mechanitor like anyone. public bool holdLifted; // ---- reads ---------------------------------------------------------------------------- public bool IsClosed => status == CaseStatus.Closed; public bool IsBreakCase => !breakState.NullOrEmpty(); public MentalStateDef BreakStateDef => breakState.NullOrEmpty() ? null : DefDatabase.GetNamedSilentFail(breakState); public int LawIdFor(LawStage s) => s == LawStage.Planning ? planningLawId : didItLawId; /// The row the stage runs, or null (none, or deleted). public Law LawFor(LawStage s) => GameComponent_BlaxLaws.Instance?.LawById(LawIdFor(s)); /// The row the case shows: did it, else planning, else null. 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); /// The offender's name, live while the pawn exists. public string OffenderLabel => offender != null ? offender.LabelShort : offenderName ?? "?"; public string VictimLabel => victim != null ? victim.LabelShort : victimName; /// What the case is about: the current row's label, else the act key. 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; } /// Sets the row a stage runs and raises the case's severity to it. 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; } /// Nulls references to pawns and things that are gone, so the save holds no reference it can't load. Names stay. 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; } /// /// 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). /// 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_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 + ")" : ""); } }