using System.Collections.Generic; using RimWorld; using Verse; using Verse.AI; using Verse.AI.Group; namespace BlaxLaws { /// What the enforcer must be able to do. public enum EnforcerNeed { /// Violence (subdue). Subdue, /// Manipulation (arrest, capture). Arrest, /// Both: "Subdue then jail" beats the pawn down and carries it to the prisoner bed. SubdueAndArrest, } /// /// Who enforces and when nobody may be sent (design: "Enforcement runs a tick later", enforcer pick and /// danger). Runs only when a case is dispatched or swept, never per tick, and walks only the free /// colonists of the offender's map. /// /// The pick: free non-slave adult colonists on the offender's map, awake, not downed, not in a mental /// state, not drafted, no player-forced job, no lord (parties, rituals, caravans forming), not the /// offender and not the victim a murderous rage names. Never pulled off a job that must not be dropped: /// a job that isn't casually interruptible (in Core: Rescue, Capture, Arrest, TendPatient, BeatFire, /// ExtinguishSelf, ManTurret, DeliverToBed, LayDown), carrying a pawn (PawnUtility.cs:115), fleeing /// (Flee, FleeAndCower) or surgery (a DoBill whose recipe IsSurgery, RecipeDef.cs:362). Wardens first /// (Warden work active), then anyone able unless the setting is "Wardens only"; nearest by straight /// distance among those that can reach the offender. The case's own enforcer goes first when it is still /// free, so a later stage hands over to the same pawn. /// public static class Enforcers { private struct Candidate { public Pawn pawn; public bool warden; public float distSq; } private static readonly List tmp = new List(); /// The enforcer for the case, or null when nobody can go now (the case waits for the sweep). public static Pawn Pick(LawCase c, EnforcerNeed need) => Pick(c, need, out _); /// The enforcer, or null with WaitReasons.NoEnforcer (nobody able at all) or Busy (nobody free now). public static Pawn Pick(LawCase c, EnforcerNeed need, out string waitReason) { Pawn offender = c?.offender; if (offender == null || !offender.Spawned || offender.Map == null) { waitReason = WaitReasons.Away; return null; } return PickFrom(c, need, offender.Map.mapPawns.FreeColonistsSpawned, out waitReason); } /// The pick over the given candidates (the map's free colonists in the game; a fixed list in tests). public static Pawn PickFrom(LawCase c, EnforcerNeed need, IEnumerable candidates, out string waitReason) { waitReason = null; Pawn offender = c?.offender; if (offender == null || !offender.Spawned) { waitReason = WaitReasons.Away; return null; } bool wardensOnly = BlaxLawsMod.S.enforcers == EnforcerMode.WardensOnly; // Hand-over: a later stage keeps the pawn that already acts for the case. Pawn own = c.enforcer; if (own != null && own.Spawned && own.Map == offender.Map && IsAble(own, offender, need, wardensOnly) && IsFree(own, c) && own.CanReach(offender, PathEndMode.Touch, Danger.Deadly)) return own; int able = 0; tmp.Clear(); try { foreach (Pawn p in candidates) { if (p == null || p.Map != offender.Map || !IsAble(p, offender, need, wardensOnly)) continue; able++; if (!IsFree(p, c)) continue; tmp.Add(new Candidate { pawn = p, warden = IsWarden(p), distSq = (p.Position - offender.Position).LengthHorizontalSquared }); } tmp.Sort((a, b) => { if (a.warden != b.warden) return a.warden ? -1 : 1; return a.distSq.CompareTo(b.distSq); }); foreach (Candidate cand in tmp) { if (cand.pawn.CanReach(offender, PathEndMode.Touch, Danger.Deadly)) return cand.pawn; } } finally { tmp.Clear(); } waitReason = able == 0 ? WaitReasons.NoEnforcer : WaitReasons.Busy; return null; } /// /// Could ever enforce this case, whatever it does right now: a free non-slave adult colonist other /// than the offender, able to move and to do what the punishment needs; a warden in "Wardens only". /// public static bool IsAble(Pawn p, Pawn offender, EnforcerNeed need, bool wardensOnly) { if (p == null || p == offender || p.Dead || !p.RaceProps.Humanlike) return false; if (!p.IsFreeNonSlaveColonist || DlcSupport.IsSlave(p) || p.IsQuestLodger()) return false; // Children (Biotech) and babies never enforce; DevelopmentalStage reads Adult without Biotech. if (!p.DevelopmentalStage.Adult()) return false; if (p.health?.capacities == null || !p.health.capacities.CapableOf(PawnCapacityDefOf.Moving)) return false; if ((need == EnforcerNeed.Subdue || need == EnforcerNeed.SubdueAndArrest) && p.WorkTagIsDisabled(WorkTags.Violent)) return false; if ((need == EnforcerNeed.Arrest || need == EnforcerNeed.SubdueAndArrest) && !p.health.capacities.CapableOf(PawnCapacityDefOf.Manipulation)) return false; if (wardensOnly && !IsWarden(p)) return false; return true; } /// Free to be sent now (see the class summary). A job of this case's own enforcement counts as free. public static bool IsFree(Pawn p, LawCase c) { if (p == null || !p.Spawned || p.Downed || p.Dead || !p.Awake() || p.InMentalState || p.Drafted) return false; if (p.GetLord() != null) return false; if (p.IsCarryingPawn()) return false; Pawn offender = c?.offender; if (offender?.MentalState is MentalState_MurderousRage rage && rage.target == p) return false; Job job = p.CurJob; if (job == null) return true; if (offender != null && EnforcerJobs.IsEnforcementJob(job, offender)) return c.enforcer == p; return !MustNotDrop(job); } /// /// A job Laws never pulls a pawn off: player-forced, not casually interruptible, fleeing, or surgery. /// public static bool MustNotDrop(Job job) { if (job?.def == null) return false; if (job.playerForced || !job.def.casualInterruptible) return true; if (job.def == JobDefOf.Flee || job.def == JobDefOf.FleeAndCower || job.def == JobDefOf.FleeAndCowerShort) return true; if (job.def == JobDefOf.DoBill && job.RecipeDef != null && job.RecipeDef.IsSurgery) return true; return false; } /// Warden work is active for the pawn (and not disabled). public static bool IsWarden(Pawn p) => p?.workSettings != null && p.workSettings.EverWork && !p.WorkTypeIsDisabled(WorkTypeDefOf.Warden) && p.workSettings.WorkIsActive(WorkTypeDefOf.Warden); /// /// True while hostiles threaten the offender's map (GenHostility.cs:227) or are near the offender /// (PawnUtility.cs:161): nobody is sent, the case waits. A berserk colonist counts: its state is /// hostile to the player faction (MentalState_Berserk.cs:12-15, GenHostility.cs:128-133). /// public static bool DangerBlocks(Pawn offender) { if (offender == null || !offender.Spawned || offender.Map == null) return false; return GenHostility.AnyHostileActiveThreatToPlayer(offender.Map) || PawnUtility.EnemiesAreNearby(offender); } /// /// The offender's live break is hostile to the colony (berserk in vanilla). Wins over the catalogue's /// hostileBreak (design: "Lawbook tab"): such a pawn fights back and vanilla refuses its arrest /// (GenAI.cs:83). /// public static bool FightsBack(Pawn p) => p?.MentalState != null && p.MentalState.ForceHostileTo(Faction.OfPlayer); /// /// The enforcer can take the offender to the bed now: neither is reserved by someone else. Checked /// right before an Arrest or Capture starts, so StartJob never logs a reservation error /// (ReservationUtility.cs:63; Pawn_JobTracker.cs:336, 373-380). /// public static bool CanTakeToBed(Pawn enforcer, Pawn offender, Building_Bed bed) { if (enforcer == null || offender == null || bed == null) return false; return enforcer.CanReserve(offender) && enforcer.CanReserve(bed, bed.SleepingSlotsCount, 0); } } }