using System; using RimWorld; using RimWorld.Planet; 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 colonist 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, and in 1.7 any colonist on Basic work, take it through WorkGiver_Warden_ReleasePrisoner, which calls /// GenGuest.PrisonerRelease: WasImprisoned, unclaim, free again, GenGuest.cs:8-21), and the fallback. /// An escape is told apart only by the note the escape hook leaves (LawCase.escapedTick), never by the /// pawn being off the map: a pawn sold, given away, kidnapped or lost with a map is off the map too. /// 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)) { // Escaped, and the world discarded the escapee (WorldPawnGC, every 15000 ticks: RimWorld.Planet/ // WorldPawnGC.cs:20, 28-30) before this check saw it off the map. A save in between nulls the // reference (LawCase.DropGoneReferences), so only the saved note counts. A pawn destroyed in place // (another mod, a dev tool) passes to the world too (Pawn.Destroy, Pawn.cs:2349-2397) and the GC may // discard it just the same, but it never escaped: offender gone. A discarded pawn is on no map, so an // escapee that died off the map before the GC took it closes as escaped too, as below. if (c.escapedTick >= 0) { CloseEscaped(c, comp); return; } comp.CloseCase(c, CaseOutcomes.OffenderGone); return; } if (p.Dead) { // An escapee stays a prisoner of the colony off the map (Pawn.ExitMap clears the guest status only for // a released prisoner, Pawn.cs:2556-2589) and world pawns keep ticking (WorldPawns.cs:73-101), so it can // bleed out there before this sweep. A world pawn dies with no corpse (Pawn.cs:2221-2224): the escape // ended the sentence, and the death came after it. A corpse on a map means it came back and died there. if (c.escapedTick >= 0 && !p.SpawnedOrAnyParentSpawned) { CloseEscaped(c, comp); return; } CaseMessages.Post(c, "BlaxLaws_MsgDiedInJail".Translate(c.OffenderLabel)); comp.CloseCase(c, EnforcementOutcomes.Died); return; } if (p.Spawned && c.escapedTick >= 0) c.escapedTick = -1; if (c.releaseTick < 0) c.releaseTick = (c.captureTick >= 0 ? c.captureTick : now) + JailFlow.SentenceTicks(c); bool served = now >= c.releaseTick; if (!p.Spawned && c.escapedTick >= 0) { // Escaped off the map, before or after the release tick: a prisoner on "Release" can still break out // (PrisonBreakUtility.cs:149-176 has no release check). CloseEscaped(c, comp); return; } if (!p.IsPrisonerOfColony) { if (LeftCustody(p)) { // Sold, given away, kidnapped, or lost with a map: not an escape, and not free in the colony. CaseMessages.Post(c, "BlaxLaws_MsgLeftCustody".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.OffenderGone); } else if (served) { // Released by a colonist after the sentence. CaseMessages.Post(c, "BlaxLaws_MsgBackAtWork".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.Served); } else { // Recruited, released by hand or by another mod, maybe off on a caravan since. 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)); } // A prisoner taken along in a caravan keeps its guest status (Pawn.cs:2520-2524) and nobody there can // carry out "Release"; GenGuest.PrisonerRelease needs no map for a colonist (GenGuest.cs:33-80). A pawn // in a transporter or shuttle in flight waits until it lands. if (now >= c.releaseTick + FallbackTicks && (p.Spawned || p.IsCaravanMember())) { // Nobody released it (nobody free, unreachable, a break, away in a caravan): Laws releases it where it is. GenGuest.PrisonerRelease(p); CaseMessages.Post(c, "BlaxLaws_MsgReleasedInPlace".Translate(c.OffenderLabel)); comp.CloseCase(c, CaseOutcomes.Served); } } private static void CloseEscaped(LawCase c, GameComponent_BlaxLaws comp) { CaseMessages.Post(c, "BlaxLaws_MsgEscaped".Translate(c.OffenderLabel)); comp.CloseCase(c, EnforcementOutcomes.Escaped); } /// /// A jailed colonist that is no prisoner any more and not in the colony's hands: another faction or none /// (sold, Pawn.cs:2640-2643; given away; lost with a map, Pawn.cs:1859-1888), or off the map in no caravan, /// transporter or container of the colony (kidnapped, KidnappedPawnsTracker.Kidnap). A colonist released /// in the colony keeps the player faction (GenGuest.cs:33-43). /// private static bool LeftCustody(Pawn p) { if (p.Faction != Faction.OfPlayer) return true; if (p.SpawnedOrAnyParentSpawned || p.IsCaravanMember()) return false; return !ThingOwnerUtility.AnyParentIs(p) && !ThingOwnerUtility.AnyParentIs(p); } /// 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 colonist 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); } } }