Skip to content

Commit

Permalink
Hotfix: Manual traffic lights do not work properly (thanks to @dpitch40
Browse files Browse the repository at this point in the history
… for reporting that issue)
  • Loading branch information
VictorPhilipp committed Jan 8, 2017
1 parent 370f2a9 commit a3ef25a
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 107 deletions.
10 changes: 8 additions & 2 deletions TLM/TLM/Custom/AI/CustomCarAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ public bool CustomStartPathFind(ushort vehicleID, ref Vehicle vehicleData, Vecto
state.PathRecalculationRequested = false;
#endif
#if DEBUG
//Log._Debug($"CustomCarAI.CustomStartPathFind called for vehicle {vehicleID}");
bool debug = GlobalConfig.Instance.DebugSwitches[8] && vehicleData.m_sourceBuilding == 23712;
if (debug)
Log._Debug($"CustomCarAI.CustomStartPathFind called for vehicle {vehicleID} which is a {VehicleStateManager.Instance._GetVehicleState(vehicleID).VehicleType} (AI: {this.GetType().Name}). going from {vehicleData.m_sourceBuilding} to {vehicleData.m_targetBuilding}");
#endif

VehicleInfo info = this.m_info;
Expand Down Expand Up @@ -433,7 +435,7 @@ public bool CustomStartPathFind(ushort vehicleID, ref Vehicle vehicleData, Vecto
#endif

#if DEBUG
if (GlobalConfig.Instance.DebugSwitches[4])
if (debug || GlobalConfig.Instance.DebugSwitches[4])
Log._Debug($"Path-finding starts for car {vehicleID}, path={path}, startPosA.segment={startPosA.m_segment}, startPosA.lane={startPosA.m_lane}, vehicleType={vehicleType}, endPosA.segment={endPosA.m_segment}, endPosA.lane={endPosA.m_lane}");
#endif

Expand All @@ -446,6 +448,10 @@ public bool CustomStartPathFind(ushort vehicleID, ref Vehicle vehicleData, Vecto
return true;
}
}
#if DEBUG
if (debug)
Log._Debug($"Path-finding failed for car {vehicleID} (path could not be created)");
#endif
return false;
}
}
Expand Down
6 changes: 5 additions & 1 deletion TLM/TLM/Custom/AI/CustomRoadAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public class CustomRoadAI : RoadBaseAI {
private static TrafficLightManager tlm = TrafficLightManager.Instance;

public void CustomNodeSimulationStep(ushort nodeId, ref NetNode data) {
tlm.NodeSimulationStep(nodeId, ref data);
try {
tlm.NodeSimulationStep(nodeId, ref data);
} catch (Exception e) {
Log.Warning($"CustomNodeSimulationStep: An error occurred while calling TrafficLightManager.NodeSimulationStep: {e.ToString()}");
}

if (Options.timedLightsEnabled) {
try {
Expand Down
2 changes: 1 addition & 1 deletion TLM/TLM/Custom/PathFinding/CustomPathFind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,7 @@ private bool ProcessItemCosts(bool allowAdvancedAI, bool ignoreLaneArrows, bool
if (debug)
logBuf.Add($"ProcessItemCosts: applying strict lane avoidance on deactivated advaned AI");
#endif
prevCost *= 100f;
prevCost *= 10000f;
}

// add costs for u-turns
Expand Down
5 changes: 4 additions & 1 deletion TLM/TLM/Geometry/SegmentGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,11 @@ private void cleanup() {

// static methods

internal static void OnBeforeLoadData() {
static SegmentGeometry() {
segmentGeometries = new SegmentGeometry[NetManager.MAX_SEGMENT_COUNT];
}

internal static void OnBeforeLoadData() {
Log._Debug($"Building {segmentGeometries.Length} segment geometries...");
for (ushort i = 0; i < segmentGeometries.Length; ++i) {
segmentGeometries[i] = new SegmentGeometry(i);
Expand Down
32 changes: 32 additions & 0 deletions TLM/TLM/Manager/CustomSegmentLightsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ public CustomSegmentLights AddSegmentLights(ushort segmentId, bool startNode, Ro
}
}

/// <summary>
/// Add custom traffic lights at the given node
/// </summary>
/// <param name="nodeId"></param>
public void AddNodeLights(ushort nodeId) {
NodeGeometry nodeGeo = NodeGeometry.Get(nodeId);
if (!nodeGeo.IsValid())
return;
foreach (SegmentEndGeometry endGeo in nodeGeo.SegmentEndGeometries) {
if (endGeo == null)
continue;

AddSegmentLights(endGeo.SegmentId, endGeo.StartNode);
}
}

/// <summary>
/// Removes custom traffic lights at the given node
/// </summary>
/// <param name="nodeId"></param>
public void RemoveNodeLights(ushort nodeId) {
NodeGeometry nodeGeo = NodeGeometry.Get(nodeId);
if (!nodeGeo.IsValid())
return;
foreach (SegmentEndGeometry endGeo in nodeGeo.SegmentEndGeometries) {
if (endGeo == null)
continue;

RemoveSegmentLight(endGeo.SegmentId, endGeo.StartNode);
}
}

/// <summary>
/// Removes all custom traffic lights at both ends of the given segment.
/// </summary>
Expand Down
27 changes: 3 additions & 24 deletions TLM/TLM/Manager/LaneConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static LaneConnectionManager() {
/// <param name="sourceStartNode">(optional) check at start node of source lane?</param>
/// <returns></returns>
public bool AreLanesConnected(uint sourceLaneId, uint targetLaneId, bool sourceStartNode) {
if (targetLaneId == 0 || !Flags.IsInitDone() || Flags.laneConnections[sourceLaneId] == null) {
if (targetLaneId == 0 || Flags.laneConnections[sourceLaneId] == null) {
return false;
}

Expand All @@ -54,9 +54,6 @@ public bool AreLanesConnected(uint sourceLaneId, uint targetLaneId, bool sourceS
/// <param name="sourceLaneId"></param>
/// <returns></returns>
public bool HasConnections(uint sourceLaneId, bool startNode) {
if (!Flags.IsInitDone()) {
return false;
}
int nodeArrayIndex = startNode ? 0 : 1;

bool ret = Flags.laneConnections[sourceLaneId] != null && Flags.laneConnections[sourceLaneId][nodeArrayIndex] != null;
Expand Down Expand Up @@ -84,10 +81,6 @@ public bool HasUturnConnections(ushort segmentId, bool startNode) {
}

internal int CountConnections(uint sourceLaneId, bool startNode) {
if (!Flags.IsInitDone()) {
return 0;
}

if (Flags.laneConnections[sourceLaneId] == null)
return 0;
int nodeArrayIndex = startNode ? 0 : 1;
Expand All @@ -103,10 +96,6 @@ internal int CountConnections(uint sourceLaneId, bool startNode) {
/// <param name="laneId"></param>
/// <returns></returns>
internal uint[] GetLaneConnections(uint laneId, bool startNode) {
if (!Flags.IsInitDone()) {
return null;
}

if (Flags.laneConnections[laneId] == null)
return null;

Expand All @@ -125,11 +114,7 @@ internal bool RemoveLaneConnection(uint laneId1, uint laneId2, bool startNode1)
#if DEBUGCONN
Log._Debug($"LaneConnectionManager.RemoveLaneConnection({laneId1}, {laneId2}, {startNode1}) called.");
#endif
bool ret = false;

if (Flags.IsInitDone()) {
ret = Flags.RemoveLaneConnection(laneId1, laneId2, startNode1);
}
bool ret = Flags.RemoveLaneConnection(laneId1, laneId2, startNode1);

#if DEBUGCONN
Log._Debug($"LaneConnectionManager.RemoveLaneConnection({laneId1}, {laneId2}, {startNode1}): ret={ret}");
Expand Down Expand Up @@ -200,9 +185,6 @@ internal void RemoveLaneConnections(uint laneId, bool startNode) {
Log._Debug($"LaneConnectionManager.RemoveLaneConnections({laneId}, {startNode}) called.");
#endif

if (!Flags.IsInitDone())
return;

if (Flags.laneConnections[laneId] == null)
return;

Expand Down Expand Up @@ -241,10 +223,7 @@ internal void RemoveLaneConnections(uint laneId, bool startNode) {
/// <param name="startNode1"></param>
/// <returns></returns>
internal bool AddLaneConnection(uint laneId1, uint laneId2, bool startNode1) {
bool ret = false;
if (Flags.IsInitDone()) {
ret = Flags.AddLaneConnection(laneId1, laneId2, startNode1);
}
bool ret = Flags.AddLaneConnection(laneId1, laneId2, startNode1);

#if DEBUGCONN
Log._Debug($"LaneConnectionManager.AddLaneConnection({laneId1}, {laneId2}, {startNode1}): ret={ret}");
Expand Down
22 changes: 6 additions & 16 deletions TLM/TLM/Manager/SpeedLimitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,14 @@ public float GetGameSpeedLimit(uint laneId) {
}

internal float GetLockFreeGameSpeedLimit(ushort segmentId, uint laneIndex, uint laneId, NetInfo.Lane laneInfo) {
if (Flags.IsInitDone()) {
if (Flags.laneSpeedLimitArray.Length <= segmentId) {
Log.Error($"laneSpeedLimitArray.Length = {Flags.laneSpeedLimitArray.Length}, segmentId={segmentId}. Out of range!");
return laneInfo.m_speedLimit;
}

float speedLimit = 0;
ushort?[] fastArray = Flags.laneSpeedLimitArray[segmentId];
if (fastArray != null && fastArray.Length > laneIndex && fastArray[laneIndex] != null) {
speedLimit = ToGameSpeedLimit((ushort)fastArray[laneIndex]);
} else {
speedLimit = laneInfo.m_speedLimit;
}
return speedLimit;
float speedLimit = 0;
ushort?[] fastArray = Flags.laneSpeedLimitArray[segmentId];
if (fastArray != null && fastArray.Length > laneIndex && fastArray[laneIndex] != null) {
speedLimit = ToGameSpeedLimit((ushort)fastArray[laneIndex]);
} else {
float ret = GetGameSpeedLimit(laneId);
return ret;
speedLimit = laneInfo.m_speedLimit;
}
return speedLimit;
}

/// <summary>
Expand Down
16 changes: 5 additions & 11 deletions TLM/TLM/Manager/VehicleRestrictionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,17 @@ internal Dictionary<byte, ExtVehicleType> GetAllowedVehicleTypesAsDict(ushort se
/// <param name="laneInfo"></param>
/// <returns></returns>
internal ExtVehicleType GetAllowedVehicleTypes(ushort segmentId, NetInfo segmentInfo, uint laneIndex, NetInfo.Lane laneInfo) {
if (Flags.IsInitDone()) {
ExtVehicleType?[] fastArray = Flags.laneAllowedVehicleTypesArray[segmentId];
if (fastArray != null && fastArray.Length > laneIndex && fastArray[laneIndex] != null) {
return (ExtVehicleType)fastArray[laneIndex];
}
ExtVehicleType?[] fastArray = Flags.laneAllowedVehicleTypesArray[segmentId];
if (fastArray != null && fastArray.Length > laneIndex && fastArray[laneIndex] != null) {
return (ExtVehicleType)fastArray[laneIndex];
}

return GetDefaultAllowedVehicleTypes(segmentId, segmentInfo, laneIndex, laneInfo);
}

internal bool HasSegmentRestrictions(ushort segmentId) { // TODO clean up restrictions (currently we do not check if restrictions are equal with the base type)
if (Flags.IsInitDone()) {
ExtVehicleType?[] fastArray = Flags.laneAllowedVehicleTypesArray[segmentId];
return fastArray != null;
}

return false;
ExtVehicleType?[] fastArray = Flags.laneAllowedVehicleTypesArray[segmentId];
return fastArray != null;
}

/// <summary>
Expand Down
Loading

0 comments on commit a3ef25a

Please sign in to comment.