AutoHotboxSpotter Patch for caboose consist presence, making checks happen between every 15-30 seconds vs 60-300 (default)

This commit is contained in:
2024-07-24 00:07:48 -05:00
parent b5be17703f
commit a13701c3d2
2 changed files with 53 additions and 0 deletions

View File

@@ -62,5 +62,32 @@ namespace RMROC451.TweaksAndThings.Extensions
oiler.PayWages();
}
}
public static IEnumerator MrocAutoHotboxSpotterLoop(this AutoHotboxSpotter spotter, Serilog.ILogger _log)
{
while (true)
{
bool hasCaboose = spotter._cars.CabooseInConsist();
if (!spotter.HasCars)
{
yield return new WaitForSeconds(1f);
continue;
}
_log.Information("AutoHotboxSpotter {name}: Hotbox Spotter Running, Has Caboose => {hasCaboose}; Has Cars {hasCars}", spotter.name, hasCaboose, spotter.HasCars);
spotter.CheckForHotbox();
while (spotter.HasCars)
{
int num = Random.Range(60, 300);
if (hasCaboose)
{
var numOrig = num;
num = Random.Range(15, 30);
_log.Information("AutoHotboxSpotter {name}: Next check went from num(60,300) => {numOrig}; to num(15,30) => {hasCaboose}", spotter.name, numOrig, num);
}
yield return new WaitForSeconds(num);
spotter.CheckForHotbox();
}
}
}
}
}

View File

@@ -0,0 +1,26 @@
using HarmonyLib;
using Model.AI;
using Railloader;
using RMROC451.TweaksAndThings.Extensions;
using Serilog;
using System.Collections;
namespace RMROC451.TweaksAndThings.Patches;
[HarmonyPatch(typeof(AutoHotboxSpotter))]
[HarmonyPatch(nameof(AutoHotboxSpotter.SpotterLoop))]
[HarmonyPatchCategory("RMROC451TweaksAndThings")]
internal class AutoHotboxSpotter_SpotterLoop_Patch
{
private static ILogger _log => Log.ForContext<AutoHotboxSpotter_SpotterLoop_Patch>();
public static bool Prefix(AutoHotboxSpotter __instance, ref IEnumerator __result)
{
TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase<TweaksAndThingsPlugin>.Shared;
if (!tweaksAndThings.IsEnabled) return true;
bool buttonsHaveCost = tweaksAndThings?.settings?.EndGearHelpersRequirePayment ?? false;
if (buttonsHaveCost) __result = __instance.MrocAutoHotboxSpotterLoop(_log);
return !buttonsHaveCost; //only hit this if !buttonsHaveCost, since Loop is a coroutine
}
}