using System; using HarmonyLib; using RimWorld; using Verse; namespace BlaxZones { /// /// Package Camp. Sets CampScope while a trader caravan (or tribute collector, which inherits the /// method) arrives; the finalizer restores the previous value, even on an exception. /// IncidentWorker_TraderCaravanArrival.cs:78-129: the one RCellFinder call is at :112, the lord with /// the chill spot is made at :126-127. IncidentWorker_CaravanArrivalTributeCollector.cs:7 does not /// override TryExecuteWorker, so the prefix on the base class covers it with no Royalty code. /// [HarmonyPatch(typeof(IncidentWorker_TraderCaravanArrival), "TryExecuteWorker")] public static class Patch_TraderCaravanArrival_TryExecuteWorker { public static void Prefix(out bool __state) { __state = CampScope.Active; CampScope.Active = true; } public static Exception Finalizer(bool __state, Exception __exception) { CampScope.Active = __state; return __exception; } } /// /// Package Camp. The overload every TryFindRandomSpotJustOutsideColony call ends in /// (RCellFinder.cs:714). While CampScope is active and a camp zone exists, returns a spot next to the /// zone; otherwise vanilla runs. Visitor groups reach it too but never set CampScope. /// [HarmonyPatch(typeof(RCellFinder), nameof(RCellFinder.TryFindRandomSpotJustOutsideColony), new[] { typeof(IntVec3), typeof(Map), typeof(Pawn), typeof(IntVec3), typeof(Predicate) }, new[] { ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Out, ArgumentType.Normal })] public static class Patch_RCellFinder_TryFindRandomSpotJustOutsideColony { /// Return false with __result true and result set to skip vanilla; true lets vanilla run. public static bool Prefix(IntVec3 root, Map map, Pawn searcher, ref IntVec3 result, Predicate extraValidator, ref bool __result) { if (!CampScope.Active) return true; try { if (CampScope.TryFindCampSpot(map, extraValidator, out IntVec3 cell)) { result = cell; __result = true; return false; } } catch (Exception e) { ZonesLog.Once("Caravan camp spot", e); } return true; } } }