Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RouteManager.UMM/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Id": "RouteManager",
"Version": "2.1.0.0",
"Version": "2.1.0.1",
"DisplayName": "Dispatcher",
"Author": "Erabior",
"AssemblyName": "RouteManager.UMM.dll",
Expand Down
2 changes: 1 addition & 1 deletion RouteManager/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@

internal class AppVersion
{
public const string Version = "2.1.0.0";
public const string Version = "2.1.0.1";
}

2 changes: 2 additions & 0 deletions RouteManager/RouteManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@
<Compile Include="v2\core\StationManager.cs" />
<Compile Include="v2\core\TrainManager.cs" />
<Compile Include="v2\dataStructures\LocoTelem.cs" />
<Compile Include="v2\dataStructures\RouteSwitchData.cs" />
<Compile Include="v2\dataStructures\SettingsData.cs" />
<Compile Include="v2\dataStructures\StationMapData.cs" />
<Compile Include="v2\dataStructures\StationInformation.cs" />
<Compile Include="v2\Dispatcher.cs" />
<Compile Include="v2\harmonyPatches\GraphPatch.cs" />
<Compile Include="v2\harmonyPatches\ProgrammaticWindowCreatorPatch.cs" />
<Compile Include="v2\harmonyPatches\RouteManagerUI.cs" />
<Compile Include="v2\helpers\DebugFunctions.cs" />
Expand Down
209 changes: 191 additions & 18 deletions RouteManager/v2/core/AutoEngineer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
using System.Linq;
using UnityEngine;
using RouteManager.v2.Logging;
using Network;
using RollingStock;
using Track;
using static Game.Reputation.PassengerReputationCalculator;

namespace RouteManager.v2.core
{
Expand Down Expand Up @@ -582,8 +580,6 @@ private static void onArrival(Car locomotive)
RouteManager.logger.LogToDebug("EXITING FUNCTION: onArrival", LogLevel.Trace);
}



//Check to see if passengers are unloaded
private static bool wasCurrentStopServed(Car locomotive)
{
Expand Down Expand Up @@ -701,7 +697,6 @@ private static bool passStillWaiting(Car locomotive, PassengerMarker? marker)
}



//Initial checks to determine if we can continue with the coroutine
private bool needToExitCoroutine(Car locomotive)
{
Expand Down Expand Up @@ -807,8 +802,6 @@ private static bool GetDirection(Car locomotive, PassengerStop stop)
************************************************************************************************************/




public IEnumerator AutoEngineerControlRoutine_dev(Car locomotive)
{
//Trace Function
Expand All @@ -824,34 +817,76 @@ public IEnumerator AutoEngineerControlRoutine_dev(Car locomotive)
RouteManager.logger.LogToDebug(String.Format("Loco: {0} \t has ID: {1}", locomotive.DisplayName, locomotive.id), LogLevel.Debug);

//Set some initial values
LocoTelem.closestStation[locomotive] = StationManager.GetClosestStation_dev(locomotive); //closest station will only return a station marked as a stop
LocoTelem.currentDestination[locomotive] = StationManager.getNextStation_dev(locomotive);//LocoTelem.closestStation[locomotive].Item1;
//StationManager.getInitialDestination(locomotive);
//closest station will only return a station marked as a stop
LocoTelem.closestStation[locomotive] = StationManager.GetClosestStation_dev(locomotive);
LocoTelem.currentDestination[locomotive] = StationManager.getNextStation_dev(locomotive);

//set locomotive drive direction
LocoTelem.locoTravelingForward[locomotive] = GetDirection(locomotive, LocoTelem.currentDestination[locomotive]);

//get route info - what are the switches on our route and what state do they need to be in?
List<RouteSwitchData> switchRequirements;



//Dev/testing only to be replaced once logic is working
PassengerStop alarka = PassengerStop.FindAll().Where(stop => stop.identifier == "alarka").First();
//End dev/testing code


//get all switches on our route from current location to end of line/end of branch line
if (DestinationManager.GetRouteSwitches(locomotive.LocationF, (Track.Location)alarka.TrackSpans.First().lower, out switchRequirements))
{
//we have the total route to end of line/branch
// Check if the next station has multiple platforms and find the last common switch - this is used later if we find ourselves against a switch
DestinationManager.PlanNextRoute(locomotive.LocationF, LocoTelem.currentDestination[locomotive], ref switchRequirements);
}
else
{
//oh-oh we're flying blind!
}

LocoTelem.routeSwitchRequirements[locomotive] = switchRequirements;

LocoTelem.closestStationNeedsUpdated[locomotive] = false;
LocoTelem.CenterCar[locomotive] = TrainManager.GetCenterCoach(locomotive);

//set initial passenger loading
TrainManager.CopyStationsFromLocoToCoaches_dev(locomotive);

RouteManager.logger.LogToDebug($"Copy complete", LogLevel.Debug);

RouteManager.logger.LogToDebug($"Closest station: {LocoTelem.closestStation.ContainsKey(locomotive)} Center Car: {LocoTelem.CenterCar.ContainsKey(locomotive)}", LogLevel.Debug);

//Give time for passenger loading/unloading if already at the station
if (Vector3.Distance(LocoTelem.closestStation[locomotive].Item1.CenterPoint, LocoTelem.CenterCar[locomotive].GetCenterPosition(Graph.Shared)) <= 15f) //StationManager.isTrainInStation(LocoTelem.CenterCar[locomotive]))
{
RouteManager.logger.LogToDebug($"We're close", LogLevel.Debug);

while (!wasCurrentStopServed_dev(locomotive))
{
yield return new WaitForSeconds(1);
}

if (RouteManager.Settings.showDepartureMessage)
RouteManager.logger.LogToConsole(String.Format("{0} has departed for {1}", Hyperlink.To(locomotive), LocoTelem.currentDestination[locomotive].DisplayName.ToUpper()));

LocoTelem.clearedForDeparture[locomotive] = true;
RouteManager.logger.LogToDebug($"Stop Served", LogLevel.Debug);
}


if (RouteManager.Settings.showDepartureMessage)
RouteManager.logger.LogToConsole(String.Format("{0} has departed for {1}", Hyperlink.To(locomotive), LocoTelem.currentDestination[locomotive].DisplayName.ToUpper()));

RouteManager.logger.LogToDebug($"Clearing", LogLevel.Debug);
LocoTelem.clearedForDeparture[locomotive] = true;
RouteManager.logger.LogToDebug($"Cleared", LogLevel.Debug);



/**************************************************
*
* Initialisation complete, start main routines
*
***************************************************/

//Feature Ehancement #30
LocoTelem.initialSpeedSliderSet[locomotive] = false;

Expand Down Expand Up @@ -887,6 +922,7 @@ public IEnumerator AutoEngineerControlRoutine_dev(Car locomotive)
yield break;
}


//Locomotive Enroute to Destination
public IEnumerator locomotiveTransitControl_dev(Car locomotive)
{
Expand All @@ -906,10 +942,16 @@ public IEnumerator locomotiveTransitControl_dev(Car locomotive)
//TEMP LOGIC
float distanceToStation = float.MaxValue;
bool delayExecution = false;
//float olddist = float.MaxValue;
float trainVelocity = 0;
float trainVelocity;
int stationPadding = 10;

//get initial data
RouteSwitchData nextSwitch;
float distanceToSwitch = float.MinValue;
bool stopForSwitch = false;
bool checkNextSwitch = true;
int nextSwitchIndex;


//Loop through transit logic
while (LocoTelem.TransitMode[locomotive])
Expand All @@ -925,6 +967,137 @@ public IEnumerator locomotiveTransitControl_dev(Car locomotive)
yield return null;
}

/*
* Check all switches that are up to 400 metres away
*/

//get the first switch in the list
nextSwitch = LocoTelem.routeSwitchRequirements[locomotive].FirstOrDefault();
nextSwitchIndex = 0;

if (nextSwitch != null)
{
distanceToSwitch = DestinationManager.GetDistanceToSwitch(locomotive, nextSwitch);
checkNextSwitch = true;
}

while (checkNextSwitch)
{
//remove switche if it's beneath/behind us
if(distanceToSwitch <= 0 && nextSwitch != null)
{
//remove the switch
RouteManager.logger.LogToDebug($"Removing switch: {nextSwitch.trackSwitch.id}, Distance: {distanceToSwitch}", LogLevel.Debug);
LocoTelem.routeSwitchRequirements[locomotive].Remove(nextSwitch);

//find the next switch and calculate the distance
nextSwitch = LocoTelem.routeSwitchRequirements[locomotive].FirstOrDefault();
nextSwitchIndex = 0;

//no more switches
if (nextSwitch == null)
break;
}
else
{
break;
}

//check our next switch distance
distanceToSwitch = DestinationManager.GetDistanceToSwitch(locomotive, nextSwitch);
RouteManager.logger.LogToDebug($"Approaching: {nextSwitch.trackSwitch.id}, Distance: {distanceToSwitch}", LogLevel.Debug);

if (distanceToSwitch <= 400)
{
//Check switch state vs requirements: Need normal and is reverse || need reverse and is normal
if (nextSwitch.requiredStateNormal && nextSwitch.trackSwitch.isThrown ||
!nextSwitch.requiredStateNormal && !nextSwitch.trackSwitch.isThrown)
{
RouteManager.logger.LogToDebug($"Switch {nextSwitch.trackSwitch.id} state incorrect req normal: {nextSwitch.requiredStateNormal}, is reversed: {nextSwitch.trackSwitch.isThrown}", LogLevel.Debug);
//switch is not in the required position, is it marked as able to be routed around?
//Currently we are ony looking at passenger platforms but in the future, we might want to look at track segments for complex switch yards
if (nextSwitch.isRoutable)
{
RouteManager.logger.LogToDebug($"Switch is routable", LogLevel.Debug);
//check if we can leave on an alternate platform
TrackSpan[] ts = LocoTelem.currentDestination[locomotive].TrackSpans.ToArray();

/*
**** work in progress - check all platforms ***
if (LocoTelem.nextPassengerPlatform[locomotive] == null)
{
LocoTelem.nextPassengerPlatform[locomotive] = 0;
}

while (LocoTelem.nextPassengerPlatform[locomotive] < ts.Length -1 )
{
//Try the next plaform
LocoTelem.nextPassengerPlatform[locomotive]++;
Location pNext = (Location)ts[(int)LocoTelem.nextPassengerPlatform[locomotive]].lower;

//can a route be found?
List<RouteSwitchData> mainRoute = LocoTelem.routeSwitchRequirements[locomotive];
}
*/

Location pNext = (Location)ts[1].lower;
List<RouteSwitchData> mainRoute = LocoTelem.routeSwitchRequirements[locomotive];

if (pNext == null || !DestinationManager.PlanRouteDeviation(ref mainRoute, nextSwitch, locomotive.LocationF, pNext))
{
//we can't enter this platform come to a stop
stopForSwitch = true;
RouteManager.logger.LogToDebug($"Deviation unsuccessful", LogLevel.Debug);
}
else
{
RouteManager.logger.LogToDebug($"Locomotive {locomotive.DisplayName}: route updated for switch");
}
}
else
{
stopForSwitch = true;
RouteManager.logger.LogToDebug($"Switch is unroutable", LogLevel.Debug);
}
}

if (stopForSwitch)
{
RouteManager.logger.LogToConsole(String.Format("{0} is holding at a switch; required state: {1}", Hyperlink.To(locomotive), nextSwitch.requiredStateNormal ? "NORMAL" : "REVERSED"));

//wait for the switch to clear
while (nextSwitch.requiredStateNormal == nextSwitch.trackSwitch.isThrown)
{
StateManager.ApplyLocal(new AutoEngineerCommand(locomotive.id, AutoEngineerMode.Road, LocoTelem.locoTravelingForward[locomotive], (int)0, null));
yield return new WaitForSeconds(1);
}

RouteManager.logger.LogToDebug($"Switch {nextSwitch.trackSwitch.id} cleared", LogLevel.Debug);
stopForSwitch = false;
}
}
else
{
RouteManager.logger.LogToDebug($"No switches within 400m", LogLevel.Debug);
checkNextSwitch = false;
break;
}

//Get the switch after the current one
RouteManager.logger.LogToDebug($"Getting subsequent switch, index: {nextSwitchIndex}, count: {LocoTelem.routeSwitchRequirements[locomotive].Count() - 1}", LogLevel.Debug);
if (nextSwitchIndex < LocoTelem.routeSwitchRequirements[locomotive].Count() - 1)
{
nextSwitchIndex++;

nextSwitch = LocoTelem.routeSwitchRequirements[locomotive][nextSwitchIndex + 1];
distanceToSwitch = DestinationManager.GetDistanceToSwitch(locomotive, nextSwitch);
}
else
{
checkNextSwitch = false;
}
}

//Getting close to a station update some values...
//Cheeky optimization to reduce excessive logging...
if (distanceToStation != float.MaxValue)
Expand Down Expand Up @@ -979,7 +1152,7 @@ public IEnumerator locomotiveTransitControl_dev(Car locomotive)
//Try again in 5 seconds
if (delayExecution)
{
yield return new WaitForSeconds(5);
yield return new WaitForSeconds(1);//5);
}

/*****************************************************************
Expand Down Expand Up @@ -1008,7 +1181,7 @@ public IEnumerator locomotiveTransitControl_dev(Car locomotive)
RouteManager.logger.LogToDebug($"{locomotive.DisplayName} distance to station: {distanceToStation} Speed: {trainVelocity} Max speed: {(int)LocoTelem.RMMaxSpeed[locomotive]}");
generalTransit(locomotive);

yield return new WaitForSeconds(5);
yield return new WaitForSeconds(1);// 5);
}
//Entering Destination Boundary
else if (distanceToStation <= 400 && distanceToStation > 300)
Expand Down
Loading