diff --git a/Assembly.version b/Assembly.version index 92ec459..45f4823 100644 --- a/Assembly.version +++ b/Assembly.version @@ -2,6 +2,6 @@ 2 0 - 0 + 1 \ No newline at end of file diff --git a/TweaksAndThings/Patches/BaseLocomotive_FindSourceLocomotive_Patch.cs b/TweaksAndThings/Patches/BaseLocomotive_FindSourceLocomotive_Patch.cs new file mode 100644 index 0000000..bc2db78 --- /dev/null +++ b/TweaksAndThings/Patches/BaseLocomotive_FindSourceLocomotive_Patch.cs @@ -0,0 +1,45 @@ +using HarmonyLib; +using Model; +using Model.Definition; +using Model.Physics; +using Railloader; +using System; +using static Model.Car; + +namespace RMROC451.TweaksAndThings.Patches; + +[HarmonyPatch(typeof(BaseLocomotive))] +[HarmonyPatch(nameof(BaseLocomotive.FindSourceLocomotive), typeof(LogicalEnd))] +[HarmonyPatchCategory("RMROC451TweaksAndThings")] +internal class BaseLocomotive_FindSourceLocomotive_Patch +{ + private static bool Prefix(BaseLocomotive __instance, LogicalEnd searchDirection, ref BaseLocomotive __result) + { + TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; + if (!tweaksAndThings.IsEnabled()) return true; + + __result = FindSourceLocomotive(__instance, searchDirection); + + return false; + } + public static BaseLocomotive FindSourceLocomotive(BaseLocomotive __instance, LogicalEnd searchDirection) + { + bool stop = false; + int? num = __instance.set.IndexOfCar(__instance); + if (!num.HasValue) + { + throw new Exception("Couldn't find car in set"); + } + int carIndex = num.Value; + LogicalEnd fromEnd = ((searchDirection == LogicalEnd.A) ? LogicalEnd.B : LogicalEnd.A); + Car car; + while (!stop && (car = __instance.set.NextCarConnected(ref carIndex, fromEnd, IntegrationSet.EnumerationCondition.AirAndCoupled, out stop)) != null) + { + if (!(car == __instance) && car is BaseLocomotive baseLocomotive && car.Archetype != CarArchetype.Tender && !baseLocomotive.locomotiveControl.air.IsCutOut) + { + return baseLocomotive; + } + } + return null; + } +}