rimworld-blax-laws/Source/BlaxLaws/Sentences/Sentences.cs

221 lines
11 KiB
C#

using System;
using RimWorld;
using RimWorld.Planet;
using UnityEngine;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// 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.
/// </summary>
public static class Sentences
{
/// <summary>A day after the release tick without a release, Laws releases the pawn in place.</summary>
public const int FallbackTicks = GenDate.TicksPerDay;
private static int Now => Find.TickManager?.TicksGame ?? 0;
/// <summary>
/// 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).
/// </summary>
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;
}
/// <summary>
/// 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.
/// </summary>
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.
if (c.escapedTick >= 0 && (p == null || !p.Dead))
{
CloseEscaped(c, comp);
return;
}
comp.CloseCase(c, CaseOutcomes.OffenderGone);
return;
}
if (p.Dead)
{
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);
}
/// <summary>
/// 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).
/// </summary>
private static bool LeftCustody(Pawn p)
{
if (p.Faction != Faction.OfPlayer)
return true;
if (p.SpawnedOrAnyParentSpawned || p.IsCaravanMember())
return false;
return !ThingOwnerUtility.AnyParentIs<ActiveTransporterInfo>(p) && !ThingOwnerUtility.AnyParentIs<TravellingTransporters>(p);
}
/// <summary>The Cases tab's "Arrest now" applies: a jail case held for a role holder or mechanitor.</summary>
public static bool CanArrestNow(LawCase c) =>
c?.offender != null && !c.IsClosed && c.status == CaseStatus.Open && c.waitReason == WaitReasons.Hold;
/// <summary>"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).</summary>
public static void ArrestNow(LawCase c)
{
if (!CanArrestNow(c))
return;
JailFlow.LiftHold(c);
JailFlow.Become(c);
JailFlow.TryArrest(c, ignoreHold: true);
}
/// <summary>Ticks of the sentence left for a jailed case, or -1. The Cases tab counts down with it.</summary>
public static int TicksLeft(LawCase c) =>
c != null && c.status == CaseStatus.Jailed && c.releaseTick >= 0 ? Math.Max(0, c.releaseTick - Now) : -1;
/// <summary>This capture is the one Laws sent: the case's own enforcer took the offender for a jail case.</summary>
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);
}
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>
/// 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.
/// </summary>
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));
}
/// <summary>The release day as a date at the offender's map.</summary>
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);
}
}
}