using System; using System.Runtime.CompilerServices; using HarmonyLib; using RimWorld; using Verse; using Verse.AI; namespace BlaxLaws { /// /// Hook A: a break starts (category Break, planning). Postfix on MentalStateHandler.TryStartMentalState /// (Verse.AI/MentalStateHandler.cs:91), about 110 lines. The new state's PostStart has run at :162 before /// `return true` at :202, so a drug binge has chosen its chemical (Verse.AI/MentalState_BingingDrug.cs:24-27) /// and a corpse obsession its corpse (Verse.AI/MentalState_CorpseObsession.cs:41-45). The method undrafts /// the pawn (:141-144) and, for most states, stops its job (:165-172) before the postfix runs. A state /// another pawn's ability forced on the pawn files nothing, and a self-cast state is checked for the player /// order in the prefix, before vanilla undrafts the pawn and stops its job. /// [HarmonyPatch(typeof(MentalStateHandler), nameof(MentalStateHandler.TryStartMentalState))] public static class Patch_MentalStateHandler_TryStartMentalState { public const string Where = "hook A, MentalStateHandler.TryStartMentalState"; /// /// A state the pawn starts on itself (a self-cast ability such as the Berserk trance: otherPawn is the pawn, /// RimWorld/CompAbilityEffect_GiveMentalState.cs:59) is a player order when the pawn is drafted or on a /// player-forced job. That has to be read here, before vanilla undrafts the pawn (Verse.AI/MentalStateHandler.cs:141-144) /// and StopAll ends the cast job (:165-172). /// public static void Prefix(Pawn ___pawn, Pawn otherPawn, out bool __state) { __state = false; if (!Watch.Break || otherPawn == null || otherPawn != ___pawn) return; __state = OrderedAtStart(___pawn); } [MethodImpl(MethodImplOptions.NoInlining)] private static bool OrderedAtStart(Pawn pawn) { try { return Offenders.OnPlayerOrder(pawn); } catch (Exception e) { HookGuard.Fail(Where, e); return false; } } public static void Postfix(MentalStateHandler __instance, Pawn ___pawn, MentalStateDef stateDef, Pawn otherPawn, bool __result, bool __state) { if (!Watch.Break || !__result) return; File(__instance, ___pawn, stateDef, otherPawn, __state); } // The work, off the hot path: a method with a try block is never inlined, so the flag check above // stays a static field read the patched method can inline. [MethodImpl(MethodImplOptions.NoInlining)] private static void File(MentalStateHandler __instance, Pawn ___pawn, MentalStateDef stateDef, Pawn otherPawn, bool orderedAtStart) { try { MentalState state = __instance.CurState; // A state that ended inside its own start (no corpse to obsess over) files nothing. if (state?.def == null || state.def != stateDef || !Watch.MayMatch(ActCategories.Break, state.def)) return; // Another pawn's ability forced the state on this one (a psycast, Neuroquake, a modded caster): the // pawn did nothing. Only abilities and the social fight pass otherPawn, and the social fight is its own // special act (hook F). if (otherPawn != null && otherPawn != ___pawn) return; // A self-cast state on a player order (Berserk trance cast by the player) is exempt like any other order. if (orderedAtStart && BlaxLawsMod.S.playerOrdersExempt) return; var act = new ActEvent(ActCategories.Break, state.def, ___pawn) { mentalState = state }; if (state is MentalState_BingingDrug binge) { act.chemical = binge.chemical; act.drugCategory = DrugInfo.BingeCategory(binge); } else if (state is MentalState_CorpseObsession obsession && DetectionHooks.InGrave(obsession.corpse)) { // Vanilla obsesses over buried corpses (Verse.AI/CorpseObsessionMentalStateUtility.cs:17-21), so // the corpse is nearly always dug up; hook J keeps the note for the did-it. act.notes |= CaseNotes.DugUp; act.thing = obsession.corpse; } CaseFiler.Report(act); } catch (Exception e) { HookGuard.Fail(Where, e); } } } }