move loco post notice about WP set to host, so clients get them

This commit is contained in:
2025-09-02 09:42:14 -05:00
parent f1b68d2827
commit b562424f8b
2 changed files with 48 additions and 33 deletions

View File

@@ -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<AutoEngineerOrdersHelper_SetWaypoint_patch>();
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"));
}
}
}

View File

@@ -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<TweaksAndThingsPlugin>.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);