using System.Collections.Generic;
using RimWorld;
using Verse;
using Verse.AI;
namespace BlaxLaws
{
///
/// The first release's detection hooks (design: "Detection: one postfix per act, quiet while the book is
/// empty"), one [HarmonyPatch] class per hook in this folder; BlaxLawsStartup applies them. Every hook reads
/// its Watch flag first, so an empty book costs one static field read per call; then Watch.MayMatch (one
/// set lookup); then it builds an ActEvent and calls CaseFiler.Report, all inside its own try/catch with
/// HookGuard.Fail. Hooks only record: CaseFiler queues, Enforcement punishes a tick later.
///
/// | Hook | Target (decompiled 1.7) | Files |
/// |---|---|---|
/// | A | MentalStateHandler.TryStartMentalState, prefix and postfix (Verse.AI/MentalStateHandler.cs:91; undraft :141-144, PostStart :162, StopAll :165-172, return true :202) | Break, planning; skips states another pawn's ability caused |
/// | C | Thing.Ingested, prefix and postfix (Verse/Thing.cs:2005) | Ate, and a binge's dose |
/// | E | Pawn_InteractionsTracker.TryInteractWith, postfix (RimWorld/Pawn_InteractionsTracker.cs:176, true at :268) | Interaction |
/// | F | Pawn_InteractionsTracker.StartSocialFight, postfix (:373) | Special/StartedSocialFight |
/// | I | Verb_Ignite.TryCastShot, postfix (RimWorld/Verb_Ignite.cs:12) | Special/SetFire |
/// | J | MentalState_CorpseObsession.MentalStateTick, postfix (Verse.AI/MentalState_CorpseObsession.cs:18) | Special/DisplayedCorpse |
/// | U | CompUsable.UsedBy and every override, prefix, postfix and finalizer (RimWorld/CompUsable.cs:161) | Used |
/// | H | HistoryEventsManager.RecordEvent, postfix (RimWorld/HistoryEventsManager.cs:55) | RecordedAct |
/// | R | Pawn_RecordsTracker.Increment and AddTo, postfixes (RimWorld/Pawn_RecordsTracker.cs:72, 84) | Record |
/// | B | RecordsUtility.Notify_BillDone, postfix (RimWorld/RecordsUtility.cs:52) | Bill |
///
/// None of the targets is a method of a few lines: A, C, E, H, U and B have loops, try blocks or dozens of
/// calls; F builds a message and starts two states; I and J are virtual overrides reached through a
/// virtual call; Increment and AddTo each build an error string on their failure path (about 80 bytes of
/// IL with five calls), above what the JIT inlines into a caller.
///
public static class DetectionHooks
{
///
/// The pawn acts on its own: one of the targets A, B or C of its current job is the thing. Vanilla
/// moves a job's target to the stack it carries (RimWorld/Toils_Ingest.cs:34; Verse.AI/Toils_Haul.cs:104)
/// and eats or uses the target (RimWorld/Toils_Ingest.cs:402, 428; RimWorld/JobDriver_UseItem.cs:95-96),
/// and a drink at a gathering spot is target C of the social relax job (RimWorld/JobDriver_SocialRelax.cs:75).
/// A doctor's dose, a warden or parent feeding someone and a doctor's serum run on the feeder's job
/// (RimWorld/Recipe_AdministerIngestible.cs:11, RimWorld/JobDriver_FoodFeedPatient.cs:78,
/// RimWorld/JobDriver_Feed.cs:47, RimWorld/Recipe_AdministerUsableItem.cs:15), so the eater's own job never
/// targets the thing.
///
public static bool OnOwnJob(Pawn pawn, Thing thing)
{
if (thing == null)
return false;
Job job = pawn?.CurJob;
if (job == null)
return false;
return job.targetA.Thing == thing || job.targetB.Thing == thing || job.targetC.Thing == thing;
}
/// A building whose def is a table (Verse/ThingDef.cs:1008) stands on the thing's cell.
public static bool OnTable(Thing thing)
{
if (thing == null || !thing.Spawned || thing.Map == null)
return false;
List here = thing.Position.GetThingList(thing.Map);
for (int i = 0; i < here.Count; i++)
{
if (here[i] != thing && here[i].def != null && here[i].def.IsTable)
return true;
}
return false;
}
/// The corpse lies in a grave or sarcophagus (Verse.AI/CorpseObsessionMentalStateUtility.cs:46).
public static bool InGrave(Corpse corpse) => corpse?.ParentHolder is Building_Grave;
}
}