using RimWorld; using Verse; using Verse.AI; namespace BlaxLaws { /// /// The arrest of a jail case (design: "Jail for N days"): the hold for role holders and mechanitors, the /// waits (hostile break, danger, no enforcer, no prisoner bed, a reservation someone else holds), guilt /// renewed right before the job, the old bed noted before the job's first toil takes it and given back /// when the arrest fails, and the Arrest or Capture job itself. PunishmentWorker_Jail, "Subdue then /// jail" and "Arrest now" call in here; the sentence itself is Sentences. /// public static class JailFlow { /// The sentence: the days of the row the case runs, in ticks. public static int SentenceTicks(LawCase c) { Law law = c?.LawFor(c.activeStage) ?? c?.CurrentLaw; if (law != null) return Lawbook.ClampDays(law.days) * GenDate.TicksPerDay; return Lawbook.DefaultDays(c?.severity ?? LawSeverity.Moderate) * GenDate.TicksPerDay; } /// /// Guilt right before an Arrest or Capture: the sentence plus one day for the release fallback, so the /// arrest of a free colonist is accepted (Pawn.cs:4087) and a downed one can be captured /// (GenAI.cs:39-67) however late the arrest comes. /// public static int RenewalTicks(LawCase c) => SentenceTicks(c) + Sentences.FallbackTicks; /// /// The case now runs as Jail (from "Subdue then jail", or a fallback). Clears the wait reason the subdue /// left. /// public static void Become(LawCase c) { if (c == null || c.IsClosed) return; c.activePunishment = BlaxLawsDefOf.Jail?.defName ?? "Jail"; if (c.status != CaseStatus.Jailed) c.status = CaseStatus.Open; c.waitReason = null; } /// The case's enforcer runs or has queued an Arrest or Capture of the offender. public static bool ArrestRuns(LawCase c) => c?.enforcer != null && EnforcerJobs.TakesToJail(c.enforcer, c.offender); /// Ends the case's Arrest or Capture job, current or queued, and gives the old bed back. public static void EndArrest(LawCase c) { if (c?.enforcer == null || c.offender == null) return; bool ended = EnforcerJobs.End(c.enforcer, JobDefOf.Arrest, c.offender); ended |= EnforcerJobs.End(c.enforcer, JobDefOf.Capture, c.offender); if (ended) GiveOldBedBack(c); } /// /// A running or queued Arrest or Capture meets danger: the job ends, the old bed goes back and the case /// waits for Danger; the sweep arrests after the fight. An enforcer already carrying the offender /// finishes, since the carry makes it a prisoner (JobDriver_TakeToBed.cs:121-129). Returns whether it /// was called off. /// public static bool CallOffInDanger(LawCase c) { Pawn o = c?.offender; if (o == null || c.enforcer == null || c.enforcer.IsCarryingPawn(o) || !Enforcers.DangerBlocks(o)) return false; EndArrest(c); WaitReasons.Set(c, WaitReasons.Danger); return true; } /// Whether "Arrest now" lifted the hold for this case. Saved on the case. public static bool HoldLifted(LawCase c) => c != null && c.holdLifted; /// "Arrest now": from here on the case arrests a role holder or mechanitor like anyone. public static void LiftHold(LawCase c) { if (c == null) return; c.holdLifted = true; } /// /// Starts the Arrest (or the Capture of a downed offender) now, or leaves the case waiting with its /// reason; a letter goes out once when the player has to act (hold, no bed, nobody to enforce). /// Returns whether the job runs. /// public static bool TryArrest(LawCase c, bool ignoreHold) { Pawn o = c?.offender; if (o == null || c.IsClosed || o.Dead || c.status == CaseStatus.Jailed) return false; if (o.IsPrisonerOfColony) { AlreadyPrisoner(c); return false; } // First: a role holder or mechanitor waits for "Arrest now" whatever else its case waited for before. if (!ignoreHold && DlcSupport.HeldForArrestNow(o) && !HoldLifted(c)) { if (WaitReasons.Set(c, WaitReasons.Hold) && CaseLetters.Once(c, CaseLetters.KindHold)) HoldLetter(c); return false; } if (!o.Spawned) { WaitReasons.Set(c, WaitReasons.Away); return false; } // Vanilla refuses the arrest of a pawn whose break is hostile to the arrester (GenAI.cs:83): wait // until it ends or the pawn is down, then arrest or capture it. if (!o.Downed && Enforcers.FightsBack(o)) { WaitReasons.Set(c, WaitReasons.HostileBreak); return false; } if (Enforcers.DangerBlocks(o)) { WaitReasons.Set(c, WaitReasons.Danger); return false; } Pawn e = Enforcers.Pick(c, EnforcerNeed.Arrest, out string reason); if (e == null) { if (WaitReasons.Set(c, reason) && reason == WaitReasons.NoEnforcer && CaseLetters.Once(c, CaseLetters.KindNoEnforcer)) CaseLetters.NoEnforcer(c); return false; } Building_Bed bed = FindPrisonerBed(o, e); if (bed == null) { if (WaitReasons.Set(c, WaitReasons.NoBed) && CaseLetters.Once(c, CaseLetters.KindNoBed)) NoBedLetter(c); return false; } if (!Enforcers.CanTakeToBed(e, o, bed)) { WaitReasons.Set(c, WaitReasons.Reserved); return false; } Guilt.AtLeast(o, RenewalTicks(c)); NoteOldBed(c, o); // The float menu offers Arrest only to drafted pawns (FloatMenuOptionProvider_Arrest.cs:27-30), so // Laws builds the job as it does (:52-54). Job job = JobMaker.MakeJob(o.Downed ? JobDefOf.Capture : JobDefOf.Arrest, o, bed); job.count = 1; c.enforcer = e; c.status = CaseStatus.Enforcing; c.waitReason = null; if (EnforcerJobs.Start(e, job)) return true; WaitReasons.Set(c, WaitReasons.Reserved); return false; } /// /// "Subdue then jail", in the tick the hit downed the offender: renews guilt and puts a vanilla Capture /// to a prisoner bed at the front of the enforcer's queue (JobQueue.cs:40), so it is the enforcer's /// next job, ahead of a doctor's rescue. A queued job fails its reservations silently /// (Pawn_JobTracker.cs:336, 373-375), and a queued vanilla Capture is safe in a save. The case runs as /// Jail from here; no bed or a hold leaves it waiting. /// public static void CaptureAfterSubdue(LawCase c, Pawn enforcer) { Become(c); Pawn o = c?.offender; if (o == null || enforcer?.jobs == null || c.IsClosed) return; if (DlcSupport.HeldForArrestNow(o) && !HoldLifted(c)) { if (WaitReasons.Set(c, WaitReasons.Hold) && CaseLetters.Once(c, CaseLetters.KindHold)) HoldLetter(c); return; } Building_Bed bed = FindPrisonerBed(o, enforcer); if (bed == null) { if (WaitReasons.Set(c, WaitReasons.NoBed) && CaseLetters.Once(c, CaseLetters.KindNoBed)) NoBedLetter(c); return; } Guilt.AtLeast(o, RenewalTicks(c)); NoteOldBed(c, o); Job job = JobMaker.MakeJob(JobDefOf.Capture, o, bed); job.count = 1; job.playerForced = true; enforcer.jobs.jobQueue.EnqueueFirst(job, JobTag.Misc); c.enforcer = enforcer; c.status = CaseStatus.Enforcing; c.waitReason = null; } /// /// A prisoner bed the enforcer can take the offender to, as the arrest float menu searches /// (FloatMenuOptionProvider_Arrest.cs:41-49): first respecting other reservations, then ignoring /// them. The caller checks the reservations before it starts a job. /// public static Building_Bed FindPrisonerBed(Pawn offender, Pawn enforcer) { if (offender == null || enforcer == null) return null; return RestUtility.FindBedFor(offender, enforcer, checkSocialProperness: false, ignoreOtherReservations: false, GuestStatus.Prisoner) ?? RestUtility.FindBedFor(offender, enforcer, checkSocialProperness: false, ignoreOtherReservations: true, GuestStatus.Prisoner); } /// /// The bed the offender owns, right before the job: the arrest's first toil claims the prisoner bed, /// which unclaims the colonist's own (JobDriver_TakeToBed.cs:73; Pawn_Ownership.cs:125-138), and a /// failed job gives up only the prisoner bed (JobDriver_TakeToBed.cs:74-80). /// public static void NoteOldBed(LawCase c, Pawn o) { Building_Bed owned = o?.ownership?.OwnedBed; if (owned != null && !owned.ForPrisoners && !owned.Medical) c.oldBed = owned; } /// /// After an Arrest or Capture that ended without a prisoner: reclaims the old bed if it is still free. /// A bed someone else claimed meanwhile stays theirs (ClaimBedIfNonMedical would evict them). /// public static void GiveOldBedBack(LawCase c) { Pawn o = c?.offender; Building_Bed bed = c?.oldBed; if (o?.ownership == null || bed == null || o.Dead || o.IsPrisonerOfColony) return; if (!bed.Spawned || bed.Destroyed || bed.ForPrisoners || bed.Medical) return; Building_Bed owned = o.ownership.OwnedBed; if (owned == bed || (owned != null && !owned.ForPrisoners)) return; if (!bed.AnyUnownedSleepingSlot) return; o.ownership.ClaimBedIfNonMedical(bed); } /// /// A slave's jail row in this release (no "Jail for slaves" yet: releasing an imprisoned slave sends it /// back to its old faction, GenGuest.cs:14-19): subdue while it is in a break, else warn. /// public static void SlaveFallback(LawCase c) { Pawn o = c?.offender; if (o == null || c.IsClosed) return; if (!o.Downed && SubdueFlow.InBreak(c) && !Enforcers.FightsBack(o) && !DlcSupport.IsChild(o)) { c.activePunishment = BlaxLawsDefOf.Subdue?.defName ?? "Subdue"; c.waitReason = null; SubdueFlow.TryIssue(c, EnforcerNeed.Subdue); return; } SubdueFlow.WarnInstead(c, "BlaxLaws_MsgWarnedSlave"); } /// The offender became a prisoner without Laws (by hand, another mod): Laws never touches it and closes the case. public static void AlreadyPrisoner(LawCase c) { if (c == null || c.IsClosed || c.status == CaseStatus.Jailed) return; CaseMessages.Post(c, "BlaxLaws_MsgAlreadyPrisoner".Translate(c.OffenderLabel)); GameComponent_BlaxLaws.Instance?.CloseCase(c, EnforcementOutcomes.AlreadyPrisoner); } /// Why Laws holds the arrest of this pawn: its ideoligion role or the mechs it controls. For the hold letter and the "Arrest now" tooltip. public static string HoldReason(Pawn p) { string name = p?.LabelShort ?? "?"; return DlcSupport.HoldsRole(p) ? "BlaxLaws_HoldRole".Translate(name).Resolve() : "BlaxLaws_HoldMechs".Translate(name).Resolve(); } private static void HoldLetter(LawCase c) => CaseLetters.Send(c, "BlaxLaws_LetterHoldLabel", "BlaxLaws_LetterHoldText", LetterDefOf.NegativeEvent, c.OffenderLabel, c.Label, HoldReason(c.offender)); private static void NoBedLetter(LawCase c) => CaseLetters.Send(c, "BlaxLaws_LetterNoBedLabel", "BlaxLaws_LetterNoBedText", LetterDefOf.NegativeEvent, c.OffenderLabel, c.Label); } }