From a13701c3d2b3ae4ea48f856135420c0666a525a8 Mon Sep 17 00:00:00 2001 From: RMROC451 Date: Wed, 24 Jul 2024 00:07:48 -0500 Subject: [PATCH] AutoHotboxSpotter Patch for caboose consist presence, making checks happen between every 15-30 seconds vs 60-300 (default) --- .../Extensions/AutoEngineer_Extensions.cs | 27 +++++++++++++++++++ .../AutoHotboxSpotter_SpotterLoop_Patch.cs | 26 ++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 TweaksAndThings/Patches/AutoHotboxSpotter_SpotterLoop_Patch.cs diff --git a/TweaksAndThings/Extensions/AutoEngineer_Extensions.cs b/TweaksAndThings/Extensions/AutoEngineer_Extensions.cs index 44b6383..9de603e 100644 --- a/TweaksAndThings/Extensions/AutoEngineer_Extensions.cs +++ b/TweaksAndThings/Extensions/AutoEngineer_Extensions.cs @@ -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(); + } + } + } } } diff --git a/TweaksAndThings/Patches/AutoHotboxSpotter_SpotterLoop_Patch.cs b/TweaksAndThings/Patches/AutoHotboxSpotter_SpotterLoop_Patch.cs new file mode 100644 index 0000000..6b742ce --- /dev/null +++ b/TweaksAndThings/Patches/AutoHotboxSpotter_SpotterLoop_Patch.cs @@ -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(); + + public static bool Prefix(AutoHotboxSpotter __instance, ref IEnumerator __result) + { + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.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 + } +}