Compare commits

...

7 Commits

9 changed files with 389 additions and 118 deletions

View File

@@ -2,6 +2,6 @@
<PropertyGroup>
<MajorVersion>2</MajorVersion>
<MinorVersion>1</MinorVersion>
<PatchVersion>1</PatchVersion>
<PatchVersion>4</PatchVersion>
</PropertyGroup>
</Project>

View File

@@ -2,6 +2,7 @@
using Game.State;
using Helpers;
using Model.Ops;
using Model.Ops.Timetable;
using Network;
using RMROC451.TweaksAndThings.Extensions;
using RMROC451.TweaksAndThings.Patches;
@@ -46,9 +47,16 @@ public class EchoCommand : IConsoleCommand
EntityReference loco = new EntityReference(EntityType.Car, car.id);
if (comps[2] == "+") message = new Hyperlink(entityReference.URI(), string.Format(message, OpsController.Shared.ClosestArea(car)?.name ?? "???"));
if (StateManager.IsHost) car.PostNotice(nameof(EchoCommand), $"{message} :{StateManager.Shared._playersManager.LocalPlayer}");
ExpandedConsole_Add_Patch.SendMs(null, $"{Hyperlink.To(car)} {message}");
if (!StateManager.IsHost) Multiplayer.Broadcast($"{StateManager.Shared._playersManager.LocalPlayer} {Hyperlink.To(car)}: \"{message}\"");
string hlt = Hyperlink.To(car);
hlt = car.TryGetTimetableTrain(out Timetable.Train t) ? hlt.Replace(car.DisplayName, t.DisplayStringLong) : hlt;
if (StateManager.IsHost)
{
car.PostNotice(nameof(EchoCommand), $"{message} :{StateManager.Shared._playersManager.LocalPlayer}");
ExpandedConsole_Add_Patch.SendMs(null, $"{StateManager.Shared._playersManager.LocalPlayer} {hlt} {message}");
}
if (!StateManager.IsHost) Multiplayer.Broadcast($"{StateManager.Shared._playersManager.LocalPlayer} {hlt}: \"{message}\"");
return string.Empty;
}

View File

@@ -103,12 +103,12 @@ public static class Car_Extensions
FindMyCaboose(car, 0f, decrement: false, requireLoad: false);
public static Car? FindMyCabooseWithLoadRequirement(this Car car, float timeNeeded, bool decrement) =>
FindMyCaboose(car, timeNeeded, decrement, requireLoad: false);
FindMyCaboose(car, timeNeeded, decrement, requireLoad: true);
private static Car? FindMyCaboose(this Car car, float timeNeeded, bool decrement = false, bool requireLoad = true) =>
(
car.CarCaboose() ?? car.CarsNearCurrentCar(timeNeeded, decrement).FindNearestCabooseFromNearbyCars()
)?.CabooseWithSufficientCrewHours(timeNeeded, decrement);
)?.CabooseWithSufficientCrewHours(timeNeeded: timeNeeded, requireLoad:requireLoad, decrement: decrement);
public static Car? CabooseWithSufficientCrewHours(this Car car, float timeNeeded, bool requireLoad, bool decrement = false)
{

View File

@@ -72,7 +72,7 @@ internal class AutoEngineerOrdersHelper_SendAutoEngineerCommand_Patch
Func<bool> noCaboose = () =>
{
bool output = (bool)TrainController.Shared.SelectedLocomotive.FindMyCabooseSansLoadRequirement();
bool output = TrainController.Shared.SelectedLocomotive.FindMyCabooseSansLoadRequirement() == null;
logMessage += $"\ncaboose? {!output}";
return output;
};
@@ -86,6 +86,8 @@ internal class AutoEngineerOrdersHelper_SendAutoEngineerCommand_Patch
noCaboose();
logMessage += $"\nGovern AE? {output}";
if (_log.IsEnabled(Serilog.Events.LogEventLevel.Verbose))
logMessage += $"\n{Environment.StackTrace}";
_log.Debug(logMessage);

View File

@@ -1,9 +1,14 @@
using Game.State;
using GalaSoft.MvvmLight.Messaging;
using Game.Events;
using Game.Messages;
using Game.State;
using HarmonyLib;
using KeyValue.Runtime;
using Model;
using Model.AI;
using Model.Ops;
using Model.Ops.Timetable;
using Model.Physics;
using Network;
using Network.Messages;
using Railloader;
@@ -15,7 +20,6 @@ using Track;
using Track.Search;
using UI;
using UI.EngineControls;
using UnityEngine;
using static Track.Search.RouteSearch;
using Location = Track.Location;
@@ -27,85 +31,94 @@ namespace RMROC451.TweaksAndThings.Patches;
internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
{
private static Serilog.ILogger _log => Log.ForContext<AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch>();
private static readonly HashSet<IDisposable> _keyChangeObservers = new HashSet<IDisposable>();
private static readonly HashSet<string> destinations = new HashSet<string>();
private static readonly HashSet<Car> consist = new HashSet<Car>();
private static string _lastLocoCarId = string.Empty;
private static bool recalcing = false;
private static readonly HashSet<IDisposable> _keyChangeObservers = [];
private static HashSet<OpsCarPosition?> locoConsistDestinations = [];
private static Game.GameDateTime? timetableSaveTime = null;
static string getDictKey(Car car) => car.DisplayName;
static Car placeholder = new();
static void Postfix(AutoEngineerWaypointControls __instance, ref OptionsDropdownConfiguration __result)
{
_log.Information($"HI BOB");
foreach(var o in _keyChangeObservers) o.Dispose();
_keyChangeObservers.Clear();
destinations.Clear();
recalcing = false;
TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase<TweaksAndThingsPlugin>.Shared;
if (!tweaksAndThings.IsEnabled()) return;
//_log.Information($"HI2");
List<DropdownMenu.RowData> rowDatas = __result.Rows.ToList();
var func = __result.OnRowSelected;
int origCount = rowDatas.Count;
int maxRowOrig = origCount - 1;
var selectedLoco = TrainController.Shared.SelectedLocomotive;
if (selectedLoco.id != _lastLocoCarId || !(consist?.Any() ?? false))
try
{
_lastLocoCarId = selectedLoco.id;
consist.Clear();
consist.UnionWith(selectedLoco.EnumerateCoupled()?.ToList() ?? Enumerable.Empty<Car>());
}
var aeoh = new AutoEngineerOrdersHelper(persistence: new AutoEngineerPersistence(selectedLoco.KeyValueObject), locomotive: selectedLoco);
List<(string destinationId, string destination, float? distance, Location? location)> jumpTos = new();
PrepLocoUsage(__instance, out BaseLocomotive selectedLoco, out int numberOfCars);
TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase<TweaksAndThingsPlugin>.Shared;
if (!tweaksAndThings.IsEnabled() || !ShouldRecalc(selectedLoco)) return;
foreach(var c in consist)
{
AddObserversToCar(__instance, c);
OpsCarPosition? destination = c.Waybill.HasValue && !c.Waybill.Value.Completed ? c.Waybill.Value.Destination : null;
bool completed = c.Waybill?.Completed ?? false;
if (!destination.HasValue && c.TryGetOverrideDestination(OverrideDestination.Repair, OpsController.Shared, out (OpsCarPosition, string)? result)) destination = result.Value.Item1;
_log.Information($"{c.DisplayName} -> {destination.HasValue}");
timetableSaveTime = TimetableController.Shared.CurrentDocument.Modified;
Messenger.Default.Unregister(placeholder);
string destId = destination?.Identifier ?? string.Empty;
if (destinations.Contains(destId)) continue;
if (destination.HasValue && !completed)
Messenger.Default.Register(placeholder, delegate (TimetableDidChange evt)
{
string destName = destination.Value.DisplayName;
float? distance = null;
_log.Debug("Received {evt}, rebuilding.", evt);
Rebuild(__instance, selectedLoco, evt.GetType().Name);
});
Messenger.Default.Register(placeholder, delegate (CarTrainCrewChanged evt)
{
_log.Debug("Received {evt}, rebuilding {1} - {2}.", evt, selectedLoco.id, evt.CarId);
Rebuild(__instance, selectedLoco, evt.GetType().Name);
});
Messenger.Default.Register(placeholder, delegate (TrainCrewsDidChange evt)
{
_log.Debug("Received {evt}, rebuilding.", evt);
Rebuild(__instance, selectedLoco, evt.GetType().Name);
});
Messenger.Default.Register(placeholder, delegate (UpdateTrainCrews evt)
{
_log.Debug("Received {evt}, rebuilding.", evt);
Rebuild(__instance, selectedLoco, evt.GetType().Name);
});
if (Graph.Shared.TryGetLocationFromPoint(destination.Value.Spans?.FirstOrDefault().GetSegments().FirstOrDefault(), destination.Value.Spans?.FirstOrDefault()?.GetCenterPoint() ?? default, 200f, out Location destLoc))
{
IterateCarsDetectDestinations(
__instance,
__result,
selectedLoco,
numberOfCars,
out List<DropdownMenu.RowData> rowDatas,
out Action<int> func,
out int origCount,
out int maxRowOrig,
out AutoEngineerOrdersHelper aeoh
);
float trainMomentum = 0f;
Location start = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.RouteStartLocation(out trainMomentum) : RouteStartLocation(__instance, selectedLoco);
HeuristicCosts autoEngineer = HeuristicCosts.AutoEngineer;
List<RouteSearch.Step> list = new List<RouteSearch.Step>();
var totLen = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.CalculateTotalLength() : CalculateTotalLength(selectedLoco);
distance = Graph.Shared.FindRoute(start, destLoc, autoEngineer, list, out var metrics, checkForCars: false, totLen, trainMomentum)
? metrics.Distance
: null;
};
_log.Information($"{c.DisplayName} -> {destName} {destId} {distance?.ToString()}");
if (distance.HasValue)
{
destinations.Add(destId);
jumpTos.Add((
destinationId: destId,
destination: $"WP> {destName}"
, distance: distance
, location: (Location?)destLoc
));
}
}
}
jumpTos = jumpTos?.OrderBy(c => c.distance)?.ToList() ?? [];
List<(string destinationId, string destination, float? distance, float sortDistance, Location? location)> jumpTos = BuildJumpToOptions(__instance, selectedLoco);
__result = WireUpJumpTosToSettingMenu(
__instance,
selectedLoco,
rowDatas,
func,
origCount,
maxRowOrig,
aeoh,
ref jumpTos
);
} catch(Exception ex)
{
_log.Error(ex, "I have a very unique set of skills; I will find you and I will squash you.");
}
}
private static bool ShouldRecalc(BaseLocomotive selectedLoco)
{
bool output = false;
string locoKey = getDictKey(selectedLoco);
List<Car> consist = new List<Car>();
consist = selectedLoco.EnumerateCoupled().ToList();
HashSet<OpsCarPosition?> destinations = consist.Where(c => GetCarDestinationIdentifier(c).HasValue).Select(GetCarDestinationIdentifier).ToHashSet();
output |= !locoConsistDestinations.Equals(destinations);
output |= !(_keyChangeObservers?.Any() ?? false);
output |= selectedLoco.TryGetTimetableTrain(out _) && TimetableController.Shared.CurrentDocument.Modified != timetableSaveTime;
return output;
}
private static OptionsDropdownConfiguration WireUpJumpTosToSettingMenu(AutoEngineerWaypointControls __instance, BaseLocomotive selectedLoco, List<DropdownMenu.RowData> rowDatas, Action<int> func, int origCount, int maxRowOrig, AutoEngineerOrdersHelper aeoh, ref List<(string destinationId, string destination, float? distance, float sortDistance, Location? location)> jumpTos)
{
OptionsDropdownConfiguration __result;
jumpTos = jumpTos?.OrderBy(c => c.sortDistance)?.ToList() ?? default;
var localJumpTos = jumpTos.ToList();
var safetyFirst = AutoEngineerOrdersHelper_SendAutoEngineerCommand_Patch.SafetyFirstGoverningApplies() && jumpTos.Any();
rowDatas.AddRange(jumpTos.Select(j =>
@@ -119,12 +132,12 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
rowDatas
, delegate (int row)
{
_log.Information($"{TrainController.Shared.SelectedLocomotive.DisplayName} row {row}/{jumpTos.Count}/{rowDatas.Count}");
_log.Debug($"{__instance.Locomotive.DisplayName} row {row}/{localJumpTos.Count}/{rowDatas.Count}");
if (row <= maxRowOrig)
{
func(row);
}
if (row > maxRowOrig && jumpTos[row - origCount].location.HasValue)
if (row > maxRowOrig && localJumpTos[row - origCount].location.HasValue)
{
if (safetyFirst)
{
@@ -132,7 +145,7 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
return;
}
float trainMomentum = 0f;
Location end = jumpTos[row - origCount].location.Value;
Location end = localJumpTos[row - origCount].location.Value;
Location start = RouteStartLocation(__instance, selectedLoco);
HeuristicCosts autoEngineer = HeuristicCosts.AutoEngineer;
List<RouteSearch.Step> list = new List<RouteSearch.Step>();
@@ -142,8 +155,9 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
{
RouteSearch.Metrics metrics2;
bool flag = Graph.Shared.FindRoute(start, end, autoEngineer, null, out metrics2);
Multiplayer.SendError(StateManager.Shared.PlayersManager.LocalPlayer, flag ? (selectedLoco.DisplayName + " Train too long to navigate to waypoint.") : (selectedLoco.DisplayName + " Unable to find a path to waypoint."), AlertLevel.Error);
} else
Multiplayer.SendError(StateManager.Shared.PlayersManager.LocalPlayer, flag ? (getDictKey(selectedLoco) + " Train too long to navigate to waypoint.") : (getDictKey(selectedLoco) + " Unable to find a path to waypoint."), AlertLevel.Error);
}
else
{
var mw = (location: end, carId: string.Empty);
aeoh.SetWaypoint(mw.location, mw.carId);
@@ -152,7 +166,161 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
}
}
);
return __result;
}
private static List<(string destinationId, string destination, float? distance, float sortDistance, Location? location)> BuildJumpToOptions(AutoEngineerWaypointControls __instance, BaseLocomotive selectedLoco)
{
List<(string destinationId, string destination, float? distance, float sortDistance, Location? location)> jumpTos = new();
foreach (OpsCarPosition ocp in locoConsistDestinations)
{
string destName = ocp.DisplayName;
string destId = ocp.Identifier;
float? distance = null;
float sortdistance = 0f;
if (
Graph.Shared.TryGetLocationFromPoint(
ocp.Spans?.FirstOrDefault().GetSegments().FirstOrDefault(),
ocp.Spans?.FirstOrDefault()?.GetCenterPoint() ?? default,
200f,
out Location destLoc
)
)
{
float trainMomentum = 0f;
Location start = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.RouteStartLocation(out trainMomentum) : RouteStartLocation(__instance, selectedLoco);
HeuristicCosts autoEngineer = HeuristicCosts.AutoEngineer;
List<RouteSearch.Step> list = new List<RouteSearch.Step>();
var totLen = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.CalculateTotalLength() : CalculateTotalLength(selectedLoco);
distance = Graph.Shared.FindRoute(start, destLoc, autoEngineer, list, out var metrics, checkForCars: false, totLen, trainMomentum)
? metrics.Distance
: null;
sortdistance = Graph.Shared.FindRoute(start, destLoc, autoEngineer, list, out metrics, checkForCars: false, 0f, trainMomentum)
? metrics.Distance
: float.MaxValue;
};
_log.Debug($"{getDictKey(selectedLoco)} -> {destName} {destId} {distance?.ToString()}");
jumpTos.Add((
destinationId: destId,
destination: $"WP> {destName}"
, distance: distance
, sortdistance: sortdistance
, location: (Location?)destLoc
));
}
if (selectedLoco.TryGetTimetableTrain(out Timetable.Train t))
{
_log.Information($"{getDictKey(selectedLoco)} -> {t.DisplayStringLong}");
foreach (var e in t.Entries)
{
var stp = TimetableController.Shared.GetAllStations().FirstOrDefault(ps => ps.code == e.Station);
_log.Information($"{getDictKey(selectedLoco)} -> {t.DisplayStringLong} -> {e.Station} {stp}");
if (stp != null)
{
try
{
string destName = t.TrainType == Timetable.TrainType.Passenger ? stp.passengerStop.DisplayName : stp.DisplayName;
string destId = t.TrainType == Timetable.TrainType.Passenger ? stp.passengerStop.identifier : stp.code;
float? distance = null;
float sortdistance = 0f;
if (
Graph.Shared.TryGetLocationFromPoint(
stp.passengerStop.TrackSpans?.FirstOrDefault().GetSegments().FirstOrDefault(),
stp.passengerStop.TrackSpans?.FirstOrDefault()?.GetCenterPoint() ?? default,
200f,
out Location destLoc
)
)
{
float trainMomentum = 0f;
Location start = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.RouteStartLocation(out trainMomentum) : RouteStartLocation(__instance, selectedLoco);
HeuristicCosts autoEngineer = HeuristicCosts.AutoEngineer;
List<RouteSearch.Step> list = new List<RouteSearch.Step>();
var totLen = StateManager.IsHost ? selectedLoco.AutoEngineerPlanner.CalculateTotalLength() : CalculateTotalLength(selectedLoco);
distance = Graph.Shared.FindRoute(start, destLoc, autoEngineer, list, out var metrics, checkForCars: false, totLen, trainMomentum)
? metrics.Distance
: null;
sortdistance = Graph.Shared.FindRoute(start, destLoc, autoEngineer, list, out metrics, checkForCars: false, 0f, trainMomentum)
? metrics.Distance
: float.MaxValue;
};
_log.Debug($"{getDictKey(selectedLoco)} -> {destName} {destId} {distance?.ToString()}");
jumpTos.Add((
destinationId: destId,
destination: $"{t.DisplayStringLong} > {destName}"
, distance: distance
, sortdistance: sortdistance
, location: (Location?)destLoc
));
} catch (Exception ex)
{
_log.Warning(ex, $"Timetable entry not added to AE gear cog options {stp}");
}
}
}
}
return jumpTos;
}
private static void IterateCarsDetectDestinations(AutoEngineerWaypointControls __instance, OptionsDropdownConfiguration __result, BaseLocomotive selectedLoco, int numberOfCars, out List<DropdownMenu.RowData> rowDatas, out Action<int> func, out int origCount, out int maxRowOrig, out AutoEngineerOrdersHelper aeoh)
{
rowDatas = __result.Rows.ToList();
func = __result.OnRowSelected;
origCount = rowDatas.Count;
maxRowOrig = origCount - 1;
locoConsistDestinations = [];
HashSet<OpsCarPosition?> seen = [];
aeoh = new AutoEngineerOrdersHelper(persistence: new AutoEngineerPersistence(selectedLoco.KeyValueObject), locomotive: selectedLoco);
Car.LogicalEnd logicalEnd = ((selectedLoco.set.IndexOfCar(selectedLoco).GetValueOrDefault(0) >= numberOfCars / 2) ? Car.LogicalEnd.B : Car.LogicalEnd.A);
Car.LogicalEnd end = ((logicalEnd == Car.LogicalEnd.A) ? Car.LogicalEnd.B : Car.LogicalEnd.A);
bool stop = false;
int carIndex = selectedLoco.set.StartIndexForConnected(selectedLoco, logicalEnd, IntegrationSet.EnumerationCondition.Coupled);
Car car;
string locoKey = getDictKey(selectedLoco);
while (!stop && (car = selectedLoco.set.NextCarConnected(ref carIndex, logicalEnd, IntegrationSet.EnumerationCondition.Coupled, out stop)) != null)
{
AddObserversToCar(__instance, car);
var ocp = GetCarDestinationIdentifier(car);
_log.Debug($"{locoKey} --> {getDictKey(car)} -> {ocp.HasValue} {ocp?.DisplayName}");
if (ocp.HasValue)
{
seen.Add(ocp.Value);
locoConsistDestinations.Add(ocp.Value);
}
}
var dropped =
locoConsistDestinations.Except(seen).ToHashSet(); //are no longer here
_log.Debug($"{locoKey} --> [{seen.Count}] -> Seen -> {string.Join(Environment.NewLine, seen.Select(k => k.Value.DisplayName))}");
_log.Debug($"{locoKey} --> [{locoConsistDestinations.Count}] -> Cache -> {string.Join(Environment.NewLine, locoConsistDestinations.Select(k => $"{locoKey}:{k.Value.DisplayName}"))}");
_log.Debug($"{locoKey} --> [{dropped.Count}] -> removed -> {string.Join(Environment.NewLine, dropped.Select(k => k.Value.DisplayName))}");
locoConsistDestinations = locoConsistDestinations.Intersect(seen).ToHashSet(); //remove ones that are no longer here
seen.Clear();
}
private static void PrepLocoUsage(AutoEngineerWaypointControls __instance, out BaseLocomotive selectedLoco, out int numberOfCars)
{
//wire up that loco
selectedLoco = __instance.Locomotive;
numberOfCars = selectedLoco.set.NumberOfCars;
_log.Debug($"{getDictKey(selectedLoco)} --> HI BOB[{numberOfCars}]");
foreach (var o in _keyChangeObservers) o.Dispose();
_keyChangeObservers.Clear();
}
private static OpsCarPosition? GetCarDestinationIdentifier(Car c)
{
OpsCarPosition? destination = null;
if (c.TryGetOverrideDestination(OverrideDestination.Repair, OpsController.Shared, out (OpsCarPosition, string)? result))
destination = result.Value.Item1;
if (!destination.HasValue && c.Waybill.HasValue && !c.Waybill.Value.Completed)
destination = c.Waybill.Value.Destination;
return destination;
}
private static Location RouteStartLocation(AutoEngineerWaypointControls __instance, BaseLocomotive _locomotive) {
@@ -184,6 +352,7 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
{
AddObserver(__instance, c, Car.KeyOpsWaybill);
AddObserver(__instance, c, Car.KeyOpsRepairDestination);
AddObserver(__instance, c, nameof(Car.trainCrewId));
foreach (Car.LogicalEnd logicalEnd in CarInspector_PopulateCarPanel_Patch.ends)
{
AddObserver(__instance, c, Car.KeyValueKeyFor(Car.EndGearStateKey.IsCoupled, c.LogicalToEnd(logicalEnd)), true);
@@ -197,28 +366,56 @@ internal class AutoEngineerWaypointControls_ConfigureOptionsDropdown_Patch
key,
delegate (Value value)
{
_log.Information($"{car.DisplayName} OBSV {key}: {value}; recalcing {recalcing}");
if (recalcing) return;
try
_log.Debug($"{getDictKey(car)} OBSV {key}: {value}");
if (!(locoConsistDestinations?.Any() ?? false)) return;
bool waybillChng = (new[] { Car.KeyOpsWaybill, Car.KeyOpsRepairDestination }).Contains(key);
string? destId =
waybillChng ?
GetCarDestinationIdentifier(car)?.Identifier ?? null :
null;
if (waybillChng)
{
foreach(var o in _keyChangeObservers)
if (locoConsistDestinations.Contains(GetCarDestinationIdentifier(car)))
{
o.Dispose();
return;
}
else
{
_log.Debug($"{getDictKey(car)} OBSV {key}: destNew; {destId}; reload");
}
var loco = TrainController.Shared.SelectedLocomotive;
if (!TrainController.Shared.SelectRecall()) TrainController.Shared.SelectedCar = null;
_keyChangeObservers.Clear();
recalcing = true;
new WaitForSeconds(0.25f);
TrainController.Shared.SelectedCar = loco;
}
catch (Exception ex)
else
{
_log.ForContext("car", car).Warning(ex, $"{nameof(AddObserver)} {car} Exception logged for {key}");
_log.Debug($"{getDictKey(car)} OBSV {key}: {value}");
}
Rebuild(__instance, car, key);
},
false
)
);
}
private static void Rebuild(AutoEngineerWaypointControls __instance, Car car, string key)
{
try
{
foreach (var o in _keyChangeObservers)
{
o.Dispose();
}
var loco = __instance.Locomotive;
if (!TrainController.Shared.SelectRecall()) TrainController.Shared.SelectedCar = null;
_keyChangeObservers.Clear();
locoConsistDestinations = [];
TrainController.Shared.SelectedCar = loco;
}
catch (Exception ex)
{
_log.ForContext("car", car).Warning(ex, $"{nameof(AddObserver)} {car} Exception logged for {key}");
}
}
}

View File

@@ -1,7 +1,8 @@
using Game;
using Game.State;
using HarmonyLib;
using Helpers;
using Model;
using Model.Ops.Timetable;
using Newtonsoft.Json;
using Railloader;
using Serilog;
@@ -21,6 +22,7 @@ namespace RMROC451.TweaksAndThings.Patches;
[HarmonyPatchCategory("RMROC451TweaksAndThings")]
internal class ExpandedConsole_Add_Patch
{
private static Serilog.ILogger _log => Log.ForContext<ExpandedConsole_Add_Patch>();
private static void Prefix(ref UI.Console.Console.Entry entry)
{
entry.Text = $"{entry.Timestamp} : {entry.Text}";
@@ -28,12 +30,26 @@ internal class ExpandedConsole_Add_Patch
SendMs((UI.Console.Console.Entry?)entry);
}
private static string hold => @"```ansi
■{loco}■ {msg}
```";
private static string west => @"```ansi
◀{loco}  {msg}
```";
private static string east => @"```ansi
 {loco}▶ {msg}
```";
internal static void SendMs(UI.Console.Console.Entry? entry, string? text = null)
{
try
{
if (entry is null && !String.IsNullOrEmpty(text)) entry = new() { Text = text };
var msgText = entry?.Text ?? string.Empty;
if (msgText.StartsWith("Usage:")) return;
TweaksAndThingsPlugin tweaksAndThings = SingletonPluginBase<TweaksAndThingsPlugin>.Shared;
StateManager shared = StateManager.Shared;
GameStorage gameStorage = shared.Storage;
@@ -50,31 +66,45 @@ internal class ExpandedConsole_Add_Patch
var carId = t.IsMatch(msgText) ? Regex.Match(msgText, "car:(.*?)\"").Groups[1].Captures[0].ToString() : string.Empty;
Model.Car? car = TrainController.Shared.CarForString(carId);
var data = UpdateCarText(car);
bool engineInMessage = car?.IsLocomotive ?? false;
var image = engineInMessage ?
new
{
url = string.Empty
} :
null;
string msgToSend = string.Empty;
string desc = Regex.Replace(msgText, "<.*?>", "");
desc = !!car ? desc.Replace(car?.DisplayName ?? string.Empty, string.Empty) : desc;
desc = desc.Trim();//.Replace(": ", "\n");
if (engineInMessage)
if (!!car && engineInMessage)
{
CTCPanelMarkerManager cTCPanelMarkerManager = UnityEngine.Object.FindObjectOfType<CTCPanelMarkerManager>();
CTCPanelMarker marker = cTCPanelMarkerManager?._markers?.Values?.FirstOrDefault(v => v.TooltipInfo.Text.Contains(car.Ident.RoadNumber));
string color = CTCPanelMarker.InferColorFromText(car?.DisplayName).HexString().Replace("#", string.Empty);
if (marker != null)
string markerText = marker?.TooltipInfo.Text ?? string.Empty;
if (markerText.StartsWith(">") || markerText.EndsWith(">") || data.Item2 == Timetable.Direction.East || msgText.Contains("*-"))
{
color = CTCPanelMarker.InferColorFromText(marker.TooltipInfo.Text).HexString().Replace("#", string.Empty);
msgToSend = east.Replace("{loco}", car?.DisplayName.Replace(" ", string.Empty)).Replace("{msg}", desc);
}
image = new
else if (markerText.StartsWith("<") || markerText.EndsWith("<") || data.Item2 == Timetable.Direction.West || msgText.Contains("-*"))
{
url = $"https://img.shields.io/badge/{car.DisplayName.Replace(" ", "%20")}-%20-{color}.png"
};
msgToSend = west.Replace("{loco}", car?.DisplayName.Replace(" ", string.Empty)).Replace("{msg}", desc);
}
else
{
msgToSend = hold.Replace("{loco}", car?.DisplayName.Replace(" ", string.Empty)).Replace("{msg}", desc);
}
}
else
{
msgToSend = desc;
}
msgToSend = msgToSend
.Replace(" , ", ", ")
.Replace(", :", " :")
.Replace(" ; ", "; ")
.Replace("*-", string.Empty)
.Replace("#", string.Empty)
.Replace("-*", string.Empty)
.Trim();
var SuccessWebHook = new
{
@@ -83,9 +113,7 @@ internal class ExpandedConsole_Add_Patch
{
new
{
description= Regex.Replace(msgText, "<.*?>", "").Replace(": ", "\n"),
timestamp=DateTime.UtcNow,
image
description= msgToSend
},
}
};
@@ -103,6 +131,26 @@ internal class ExpandedConsole_Add_Patch
}
}
public static (string , Timetable.Direction?) UpdateCarText(Car car)
{
string output = string.Empty;
Timetable.Direction? dir = null;
if (car?.IsLocomotive ?? false)
{
if (StateManager.Shared.PlayersManager.TrainCrewForId(car.trainCrewId, out var trainCrew))
{
output = trainCrew.Name;
if (TimetableController.Shared.TryGetTrainForTrainCrew(trainCrew, out Timetable.Train timetableTrain))
{
dir = timetableTrain.Direction;
output += " (Train " + timetableTrain.DisplayStringShort + ")";
}
}
}
return (output, dir);
}
public static GameDateTime RealNow()
{
var now = DateTime.Now;

View File

@@ -48,8 +48,7 @@ internal class InterchangedIndustryLoader_ServiceInterchange_Patch
if (num4 > 0)
{
var canAfford = shared.CanAfford(num4);
penalty = Mathf.CeilToInt(!canAfford ? num4 * 0.2f : 0);
__instance.Industry.ApplyToBalance(-num4, __instance.ledgerCategory, null, 0, quiet: true);
penalty += Mathf.CeilToInt(!canAfford ? num4 * 0.2f : 0);
num2 += num4;
num3++;

View File

@@ -98,6 +98,7 @@ internal class OpsController_AnnounceCoalescedPayments_Patch
var data = car.QuantityCapacityOfLoad(CrewHoursLoad());
if ((data.quantity + quantityToLoad > data.capacity) && data.quantity < data.capacity)
{
quantityToLoad = data.capacity; //ensure topping off
Multiplayer.Broadcast($"{Hyperlink.To(car)}: \"Caboose crew topped off.\"");
CrewCarDict[car.id] = (CrewCarDict[car.id].spotted, false);
}

View File

@@ -1,13 +1,17 @@
using Game.State;
using HarmonyLib;
using KeyValue.Runtime;
using Model;
using Model.Ops;
using Railloader;
using RMROC451.TweaksAndThings.Extensions;
using RollingStock;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UI.Tags;
using static Unity.IO.LowLevel.Unsafe.AsyncReadManagerMetrics;
namespace RMROC451.TweaksAndThings.Patches;
@@ -54,6 +58,18 @@ internal class TagController_UpdateTag_Patch
if (car.EndAirSystemIssue()) tags.Add(TextSprites.CycleWaybills);
if (car.HandbrakeApplied()) tags.Add(TextSprites.HandbrakeWheel);
if (car.IsPassengerCar())
{
PassengerMarker? passengerMarker = car.GetPassengerMarker();
if (passengerMarker.HasValue)
{
IEnumerable<string> loadInfo = car.PassengerCountString(passengerMarker).Split('/');
//string item4 = CarPickable.PassengerString(car, passengerMarker.Value);
string val = TextSprites.PiePercent(float.Parse(loadInfo.First()), float.Parse(loadInfo.Last())) + $" {car.PassengerCountString(passengerMarker)} Passengers";
tagCallout.callout.Text = tagCallout.callout.Text.Contains("Empty") ? tagCallout.callout.Text.Replace("Empty", val) : tagCallout.callout.Text + $"\n{val}";
}
}
tagCallout.callout.Title =
tags.Any() switch
{