rimworld-blax-laws/Source/BlaxLaws/Enforcement/EnforcerJobs.cs

97 lines
4.4 KiB
C#
Raw Permalink Normal View History

using RimWorld;
using Verse;
using Verse.AI;
namespace BlaxLaws
{
/// <summary>
/// Starting, finding and ending the enforcer's jobs. Laws starts its jobs at once, interrupting what the
/// enforcer does, with playerForced set (design: "Enforcement runs a tick later"), and never queues a
/// job of its own; the one job it queues is the vanilla Capture after a subdue.
/// </summary>
public static class EnforcerJobs
{
/// <summary>
/// Starts the job now: StartJob with InterruptForced and preToilReservationsCanFail, so a reservation
/// that fails ends the job quietly instead of logging (Pawn_JobTracker.cs:252, 373-375). Returns
/// whether the job runs.
/// </summary>
public static bool Start(Pawn enforcer, Job job)
{
if (enforcer?.jobs == null || job == null)
return false;
job.playerForced = true;
enforcer.jobs.StartJob(job, JobCondition.InterruptForced, null, resumeCurJobAfterwards: false, cancelBusyStances: true,
null, JobTag.Misc, fromQueue: false, canReturnCurJobToPool: false, keepCarryingThingOverride: null, continueSleeping: false,
addToJobsThisTick: true, preToilReservationsCanFail: true);
return enforcer.CurJob == job;
}
/// <summary>A subdue, arrest or capture job on this offender.</summary>
public static bool IsEnforcementJob(Job job, Pawn offender)
{
if (job?.def == null || offender == null || job.targetA.Thing != offender)
return false;
return job.def == BlaxLawsDefOf.BlaxLaws_Subdue || job.def == JobDefOf.Arrest || job.def == JobDefOf.Capture;
}
/// <summary>The pawn's current job is this def on this target.</summary>
public static bool Runs(Pawn pawn, JobDef def, Pawn target)
{
Job job = pawn?.CurJob;
return job != null && job.def == def && target != null && job.targetA.Thing == target;
}
/// <summary>A job of this def on this target waits in the pawn's queue.</summary>
public static bool Queued(Pawn pawn, JobDef def, Pawn target)
{
JobQueue queue = pawn?.jobs?.jobQueue;
if (queue == null || target == null)
return false;
for (int i = 0; i < queue.Count; i++)
{
Job job = queue[i]?.job;
if (job != null && job.def == def && job.targetA.Thing == target)
return true;
}
return false;
}
/// <summary>The pawn runs or has queued an Arrest or Capture of the target.</summary>
public static bool TakesToJail(Pawn pawn, Pawn target) =>
pawn != null && target != null && !pawn.Dead &&
(Runs(pawn, JobDefOf.Arrest, target) || Runs(pawn, JobDefOf.Capture, target) ||
Queued(pawn, JobDefOf.Capture, target) || Queued(pawn, JobDefOf.Arrest, target));
/// <summary>
/// Ends the pawn's job of this def on this target, current or queued. Returns whether one ended. The pawn
/// starts no new job here: a hand-over (Enforcement.Dispatch stops the earlier punishment, then starts the
/// next) must find its enforcer free, and the think tree would hand it any job at once, Ingest, Reading
/// or ClearSnow among them, which aren't casually interruptible, so the pick would pass it over. A pawn
/// left without a job gets one from the think tree on its next job tick (Pawn_JobTracker.cs:195-202).
/// </summary>
public static bool End(Pawn pawn, JobDef def, Pawn target)
{
if (pawn?.jobs == null || def == null || target == null)
return false;
bool ended = false;
JobQueue queue = pawn.jobs.jobQueue;
for (int i = queue.Count - 1; i >= 0; i--)
{
Job job = queue[i]?.job;
if (job != null && job.def == def && job.targetA.Thing == target)
{
pawn.jobs.EndCurrentOrQueuedJob(job, JobCondition.InterruptForced);
ended = true;
}
}
if (Runs(pawn, def, target))
{
pawn.jobs.EndCurrentJob(JobCondition.InterruptForced, startNewJob: false);
ended = true;
}
return ended;
}
}
}