using System; using RimWorld; using UnityEngine; using Verse; namespace BlaxLaws { /// /// Serving the sentence (design: "Jail for N days"). Hook K (Patch_Pawn_GuestTracker_CapturedBy) moves a /// case to Jailed at the capture and starts the sentence there, not at filing; "Maintain only" lands two /// ticks later, after any next-tick default from Blax Castes or a prison mod. Every 2500 ticks Check sets /// "Release" once the sentence is served, releases in place a day later if no warden did, and closes the /// case when the pawn is out (served, released early, escaped, dead). Laws writes the prisoner /// interaction mode only on pawns it jailed itself. /// public static class Sentences { /// A day after the release tick without a release, Laws releases the pawn in place. public const int FallbackTicks = GenDate.TicksPerDay; private static int Now => Find.TickManager?.TicksGame ?? 0; /// /// Writes "Maintain only" on a pawn Laws jailed, two ticks after the capture /// (Pawn_GuestTracker.cs:304), and sets maintainOnlyDone. Two ticks win over Castes' next-tick default /// and Prison Labor's default in any component order (GameComponentUtility.cs:24-31; Game.cs:472-489). /// public static void WriteMaintainOnly(LawCase c) { if (c == null || c.IsClosed || c.status != CaseStatus.Jailed || c.maintainOnlyDone) return; Pawn p = c.offender; if (p?.guest == null || !p.IsPrisonerOfColony) return; p.guest.SetExclusiveInteraction(PrisonerInteractionModeDefOf.MaintainOnly); c.maintainOnlyDone = true; } /// /// Every 2500 ticks for a jailed case: the early end, the "Maintain only" backstop, "Release" at the /// release tick (wardens take it through WorkGiver_Warden_ReleasePrisoner, which calls /// GenGuest.PrisonerRelease: WasImprisoned, unclaim, free again, GenGuest.cs:8-21), and the fallback. /// public static void Check(LawCase c) { if (c == null || c.IsClosed || c.status != CaseStatus.Jailed) return; GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance; if (comp == null) return; Pawn p = c.offender; int now = Now; if (p == null || LawCase.IsGone(p)) { comp.CloseCase(c, CaseOutcomes.OffenderGone); return; } if (p.Dead) { CaseMessages.Post(c, "BlaxLaws_MsgDiedInJail".Translate(c.OffenderLabel)); comp.CloseCase(c, EnforcementOutcomes.Died); return; } if (c.releaseTick < 0) c.releaseTick = (c.captureTick >= 0 ? c.captureTick : now) + JailFlow.SentenceTicks(c); bool served = now >= c.releaseTick; if (!p.IsPrisonerOfColony) { // Out of Laws' custody: released by a warden after the sentence, or early (recruited, // released by hand or by another mod), or escaped off the map. if (!p.Spawned && !served) { CaseMessages.Post(c, "BlaxLaws_MsgEscaped".Translate(c.OffenderLabel)); comp.CloseCase(c, EnforcementOutcomes.Escaped); } else if (served) { CaseMessages.Post(c, "BlaxLaws_MsgBackAtWork".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.Served); } else { CaseMessages.Post(c, "BlaxLaws_MsgReleasedEarly".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.ReleasedEarly); } return; } if (!c.maintainOnlyDone && !Enforcement.IsQueued(c, CaseTask.MaintainOnly)) WriteMaintainOnly(c); if (!served) return; if (p.guest.ExclusiveInteractionMode != PrisonerInteractionModeDefOf.Release) { p.guest.SetExclusiveInteraction(PrisonerInteractionModeDefOf.Release); CaseMessages.Post(c, "BlaxLaws_MsgSentenceServed".Translate(c.OffenderLabel)); } if (now >= c.releaseTick + FallbackTicks && p.Spawned) { // Nobody released it (no warden, unreachable, a break): Laws releases it where it is. GenGuest.PrisonerRelease(p); CaseMessages.Post(c, "BlaxLaws_MsgReleasedInPlace".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.Served); } } /// The Cases tab's "Arrest now" applies: a jail case held for a role holder or mechanitor. public static bool CanArrestNow(LawCase c) => c?.offender != null && !c.IsClosed && c.status == CaseStatus.Open && c.waitReason == WaitReasons.Hold; /// "Arrest now" on a held case: lifts the hold, renews guilt and starts the arrest (or leaves it waiting for a bed or an enforcer). public static void ArrestNow(LawCase c) { if (!CanArrestNow(c)) return; JailFlow.LiftHold(c); JailFlow.Become(c); JailFlow.TryArrest(c, ignoreHold: true); } /// Ticks of the sentence left for a jailed case, or -1. The Cases tab counts down with it. public static int TicksLeft(LawCase c) => c != null && c.status == CaseStatus.Jailed && c.releaseTick >= 0 ? Math.Max(0, c.releaseTick - Now) : -1; /// This capture is the one Laws sent: the case's own enforcer took the offender for a jail case. public static bool IsOurCapture(LawCase c, Pawn byPawn) { if (c == null || c.IsClosed || c.status != CaseStatus.Enforcing || byPawn == null || c.enforcer != byPawn) return false; string p = c.activePunishment; return p != null && (p == BlaxLawsDefOf.Jail?.defName || p == BlaxLawsDefOf.SubdueThenJail?.defName); } /// /// Hook K's postfix: the sentence starts at the capture. Records the capture and release ticks, queues /// "Maintain only" two ticks on, and sends the jail letter. /// internal static void OnCaptured(LawCase c) { int now = Now; c.activePunishment = BlaxLawsDefOf.Jail?.defName ?? "Jail"; c.status = CaseStatus.Jailed; c.waitReason = null; c.captureTick = now; c.releaseTick = now + JailFlow.SentenceTicks(c); c.maintainOnlyDone = false; Enforcement.Schedule(c, CaseTask.MaintainOnly, c.activeStage, Enforcement.MaintainOnlyDelay); CaseLetters.Jailed(c); } /// /// The law of a jailed case was repealed (Lawbook.Delete stops the case): Laws sets "Release" on the /// pawn it jailed, so a warden lets it go. /// internal static void ReleaseOnRepeal(LawCase c) { Pawn p = c?.offender; if (p?.guest == null || !p.IsPrisonerOfColony) return; p.guest.SetExclusiveInteraction(PrisonerInteractionModeDefOf.Release); CaseMessages.Post(c, "BlaxLaws_MsgRepealedRelease".Translate(c.OffenderLabel)); } /// The release day as a date at the offender's map. public static string ReleaseDate(LawCase c) { if (c == null || c.releaseTick < 0) return ""; Map map = c.offender?.MapHeld; Vector2 at = map != null ? Find.WorldGrid.LongLatOf(map.Tile) : Vector2.zero; return GenDate.DateFullStringAt(GenDate.TickGameToAbs(c.releaseTick), at); } } }