Compare commits

...

5 Commits

4 changed files with 155 additions and 37 deletions

View File

@@ -0,0 +1,60 @@
using HarmonyLib;
using Helpers;
using Railloader;
using Serilog;
using System.Collections;
using Track;
using UI;
using UnityEngine;
using static UI.AutoEngineerDestinationPicker;
namespace RMROC451.TweaksAndThings.Patches;
[HarmonyPatch(typeof(AutoEngineerDestinationPicker))]
[HarmonyPatch(nameof(AutoEngineerDestinationPicker.Loop))]
[HarmonyPatchCategory("RMROC451TweaksAndThings")]
internal class AutoEngineerDestinationPicker_Loop_Patch
{
static bool Prefix(AutoEngineerDestinationPicker __instance, ref IEnumerator __result)
{
TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase<TweaksAndThingsPlugin>.Shared;
if (!tweaksAndThings.IsEnabled()) return true;
__result = Loop(__instance);
return false;
}
private static IEnumerator Loop(AutoEngineerDestinationPicker __instance)
{
Hit valueOrDefault;
Location location;
WaitForSecondsRealtime wait = new WaitForSecondsRealtime(1/60);
while (true)
{
Location? currentOrdersGotoLocation = __instance.GetCurrentOrdersGotoLocation();
Hit? hit = __instance.HitLocation();
if (hit.HasValue)
{
valueOrDefault = hit.GetValueOrDefault();
location = valueOrDefault.Location;
Graph.PositionRotation positionRotation = __instance._graph.GetPositionRotation(location);
__instance.destinationMarker.position = WorldTransformer.GameToWorld(positionRotation.Position);
__instance.destinationMarker.rotation = positionRotation.Rotation;
__instance.destinationMarker.gameObject.SetActive(value: true);
if (!currentOrdersGotoLocation.Equals(location) && __instance.MouseClicked)
{
break;
}
}
else
{
__instance.destinationMarker.gameObject.SetActive(value: false);
}
yield return wait;
}
Log.Debug("DestinationPicker Hit: {hit} {car} {end}", valueOrDefault.Location, valueOrDefault.CarInfo?.car, valueOrDefault.CarInfo?.end);
__instance._ordersHelper.SetWaypoint(location, valueOrDefault.CarInfo?.car.id);
__instance.StopLoop();
}
}

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,9 +36,9 @@ 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 = TrainController.Shared.SelectedLocomotive;
if (TrainController.Shared.TryGetCarForId(command.LocomotiveId, out Car c)) loco = (BaseLocomotive)c;
BaseLocomotive loco = __instance._locomotive;
if (SafetyFirstGoverningApplies(loco))
{
@@ -52,7 +55,8 @@ internal class AutoEngineerPlanner_HandleCommand_Patch
if (orig != command.MaxSpeedMph)
{
message = string.Format(message, $" from {orig} to {command.MaxSpeedMph} MPH ");
}else
}
else
{
message = string.Format(message, " ");
}
@@ -63,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);

View File

@@ -15,6 +15,7 @@ using UI.EngineRoster;
using UI.Tooltips;
using UnityEngine;
using Game.State;
using Game;
namespace RMROC451.TweaksAndThings.Patches;
@@ -34,6 +35,8 @@ internal class EngineRosterRow_Refresh_Patch
string fuelInfoText = string.Empty;
string fuelInfoTooltip = string.Empty;
TweakyTweakTweakers(__instance);
if (tweaksAndThings == null ||
rosterFuelColumnSettings == null ||
!tweaksAndThings.IsEnabled() ||
@@ -131,13 +134,54 @@ internal class EngineRosterRow_Refresh_Patch
default:
break;
}
} catch (Exception ex)
}
catch (Exception ex)
{
rosterFuelColumnSettings.EngineRosterFuelStatusColumn = EngineRosterFuelDisplayColumn.None;
Log.Error(ex, "Error Detecting fuel status for engine roster");
}
}
private static void TweakyTweakTweakers(EngineRosterRow __instance)
{
var helperData = EngineTextHelper(__instance._engine);
if (helperData.HasValue)
{
__instance.nameLabel.text = helperData.Value.nameLabel;
__instance.nameTooltip.tooltipText += helperData.Value.nameTooltip;
}
__instance.crewLabel.text = string.Empty;
for (int i = __instance._crewComponents.Count - 1; i >= 0; i--)
{
string str = __instance._crewComponents[i];
if ((new[] { "MU", "AE" }).Contains(str))
str = $"<sup>{str} </sup>";
__instance.crewLabel.text = $"{str}{__instance.crewLabel.text}";
}
}
internal static (string nameLabel, string nameTooltip, int selectedCount)? EngineTextHelper(Car loco, bool mapIcon = false)
{
(string nameLabel, string nameTooltip, int selectedCount)? output = null;
int selectedCount = 0;
Dictionary<PlayerId, IPlayer> dictionary = StateManager.Shared.PlayersManager.AllPlayers.ToDictionary((IPlayer p) => p.PlayerId, (IPlayer p) => p);
List<string> usersSelected = new();
foreach (var kvp in dictionary)
{
if (new PlayerProperties(PlayerPropertiesManager.Shared._object[kvp.Key.ToString()]).SelectedCarId == loco.id)
{
usersSelected.Add(kvp.Value.Name);
selectedCount++;
}
}
if (selectedCount > 0 && dictionary.Count > 1)
output = ($"{(mapIcon && loco is BaseLocomotive ? loco.Ident.RoadNumber : loco.DisplayName)}<sub>{selectedCount}</sub>", $"{Environment.NewLine}Selected by: {string.Join(", ", usersSelected)}", selectedCount);
return output;
}
private static void SetLabelAndTooltip(ref TMP_Text label, ref UITooltipProvider tooltip, string fuelInfoText, string fuelInfoTooltip)
{
label.text = $" {fuelInfoText} {label.text}";