rimworld-blax-zones/Source/BlaxZones/CampPatches.cs

66 lines
2.7 KiB
C#
Raw Permalink Normal View History

using System;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxZones
{
/// <summary>
/// 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.
/// </summary>
[HarmonyPatch(typeof(IncidentWorker_TraderCaravanArrival), "TryExecuteWorker")]
public static class Patch_TraderCaravanArrival_TryExecuteWorker
{
public static void Prefix(out bool __state)
{
__state = CampScope.Active;
CampScope.Active = true;
}
/// <summary>
/// Returns void on purpose: Harmony then rethrows an exception with its stack trace intact. One
/// finalizer that returns Exception makes it throw the exception anew, and the trace starts here.
/// </summary>
public static void Finalizer(bool __state)
{
CampScope.Active = __state;
}
}
/// <summary>
/// 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.
/// </summary>
[HarmonyPatch(typeof(RCellFinder), nameof(RCellFinder.TryFindRandomSpotJustOutsideColony),
new[] { typeof(IntVec3), typeof(Map), typeof(Pawn), typeof(IntVec3), typeof(Predicate<IntVec3>) },
new[] { ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Normal, ArgumentType.Out, ArgumentType.Normal })]
public static class Patch_RCellFinder_TryFindRandomSpotJustOutsideColony
{
/// <summary>Return false with __result true and result set to skip vanilla; true lets vanilla run.</summary>
public static bool Prefix(IntVec3 root, Map map, Pawn searcher, ref IntVec3 result, Predicate<IntVec3> 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;
}
}
}