diff --git a/TweaksAndThings/Patches/AutoEngineerOrdersHelper_SetWaypoint_patch.cs b/TweaksAndThings/Patches/AutoEngineerOrdersHelper_SetWaypoint_patch.cs deleted file mode 100644 index e270df8..0000000 --- a/TweaksAndThings/Patches/AutoEngineerOrdersHelper_SetWaypoint_patch.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Game.Notices; -using Game.State; -using HarmonyLib; -using Model; -using Network; -using Serilog; -using UI.EngineControls; -using UnityEngine; - -namespace RMROC451.TweaksAndThings.Patches; - - -[HarmonyPatch(typeof(AutoEngineerOrdersHelper))] -[HarmonyPatch(nameof(AutoEngineerOrdersHelper.SetWaypoint), typeof(Track.Location), typeof(string))] -[HarmonyPatchCategory("RMROC451TweaksAndThings")] -internal class AutoEngineerOrdersHelper_SetWaypoint_patch -{ - private static Serilog.ILogger _log => Log.ForContext(); - static void Postfix(AutoEngineerOrdersHelper __instance, Track.Location location, string coupleToCarId) - { - if (StateManager.IsHost) - { - _log.Debug($"start setWP"); - Car selectedLoco = __instance._locomotive; - _log.Debug($"{selectedLoco?.DisplayName ?? ""} set WP"); - Vector3 gamePoint = location.GetPosition(); - EntityReference entityReference = new EntityReference(EntityType.Position, new Vector4(gamePoint.x, gamePoint.y, gamePoint.z, 0)); - selectedLoco.PostNotice("ai-wpt-rmroc451", new Hyperlink(entityReference.URI(), $"WP SET")); - } - } -} diff --git a/TweaksAndThings/Patches/AutoEngineerPlanner_HandleCommand_Patch.cs b/TweaksAndThings/Patches/AutoEngineerPlanner_HandleCommand_Patch.cs index 8d2f982..fa3edbc 100644 --- a/TweaksAndThings/Patches/AutoEngineerPlanner_HandleCommand_Patch.cs +++ b/TweaksAndThings/Patches/AutoEngineerPlanner_HandleCommand_Patch.cs @@ -1,5 +1,6 @@ using Game; using Game.Messages; +using Game.Notices; using Game.State; using HarmonyLib; using Model; @@ -15,8 +16,10 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Track; using UI.EngineControls; using UI.EngineRoster; +using UnityEngine; using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics; using static UnityEngine.InputSystem.InputRemoting; @@ -33,6 +36,7 @@ internal class AutoEngineerPlanner_HandleCommand_Patch static bool Prefix(AutoEngineerPlanner __instance, ref AutoEngineerCommand command, ref IPlayer sender) { TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase.Shared; + LocoNoticeWPSet(__instance, command, sender); if (!tweaksAndThings.IsEnabled() || !tweaksAndThings.SafetyFirst() || (sender.IsRemote && !tweaksAndThings.SafetyFirstClientEnforce()) || command.MaxSpeedMph <= governedSpeed) return true; BaseLocomotive loco = __instance._locomotive; @@ -49,9 +53,10 @@ internal class AutoEngineerPlanner_HandleCommand_Patch string message = $"{Enum.GetName(typeof(AutoEngineerMode), command.Mode)}[{loco.DisplayName}] governed{{0}}due to Safety First rules."; if (orig != command.MaxSpeedMph) - { + { message = string.Format(message, $" from {orig} to {command.MaxSpeedMph} MPH "); - }else + } + else { message = string.Format(message, " "); } @@ -62,6 +67,47 @@ internal class AutoEngineerPlanner_HandleCommand_Patch return true; } + private static void LocoNoticeWPSet(AutoEngineerPlanner __instance, AutoEngineerCommand command, IPlayer sender) + { + OrderWaypoint? wp = + string.IsNullOrEmpty(command.WaypointLocationString) ? + null : + new OrderWaypoint?(new OrderWaypoint(command.WaypointLocationString, command.WaypointCoupleToCarId)); + + if ( + wp.HasValue && + !string.IsNullOrEmpty(wp.Value.LocationString) && + !__instance._orders.Waypoint.Equals(wp) + ) + { + _log.Debug($"start setWP"); + Car selectedLoco = __instance._locomotive; + _log.Debug($"{selectedLoco?.DisplayName ?? ""} set WP"); + if (LocationPositionFromString(wp.Value, out Vector3 gamePoint)) + { + EntityReference entityReference = new EntityReference(EntityType.Position, new Vector4(gamePoint.x, gamePoint.y, gamePoint.z, 0)); + selectedLoco.PostNotice("ai-wpt-rmroc451", new Hyperlink(entityReference.URI(), $"WP SET [{sender.Name}]")); + } + } + } + + internal static bool LocationPositionFromString(OrderWaypoint waypoint, out Vector3 position) + { + Location location; + position = default; + try + { + location = Graph.Shared.ResolveLocationString(waypoint.LocationString); + position = location.GetPosition(); + return true; + } + catch (Exception exception) + { + Log.Error(exception, "Couldn't get location from waypoint: {locStr}", waypoint.LocationString); + return false; + } + } + internal static bool SafetyFirstGoverningApplies(BaseLocomotive loco) { var _persistence = new AutoEngineerPersistence(loco.KeyValueObject);