Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Pathfinder Debugging #370

Closed
wants to merge 1 commit into from
Closed
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
29 changes: 25 additions & 4 deletions TLM/TLM/Custom/PathFinding/CustomPathFind2.cs
Original file line number Diff line number Diff line change
@@ -163,7 +163,9 @@ private bool m_terminated {
public uint m_failedPathFinds = 0;
public uint m_succeededPathFinds = 0;
private bool m_debug = false;
private bool m_logFailureOnly = false;
private IDictionary<ushort, IList<ushort>> m_debugPositions = null;
private IList<string> m_logBuffer = new List<string>();
#endif
#if PARKINGAI || JUNCTIONRESTRICTIONS
private ushort m_startSegmentA;
@@ -316,8 +318,10 @@ private void PathFindImplementation(uint unit, ref PathUnit data) {
(m_conf.Debug.EndSegmentId == 0 || data.m_position01.m_segment == m_conf.Debug.EndSegmentId || data.m_position03.m_segment == m_conf.Debug.EndSegmentId) &&
(m_conf.Debug.VehicleId == 0 || m_queueItem.vehicleId == m_conf.Debug.VehicleId)
;
m_logFailureOnly = m_debug && m_conf.Debug.Switches[26];
if (m_debug) {
m_debugPositions = new Dictionary<ushort, IList<ushort>>();
m_logBuffer.Clear();
}
#endif

@@ -616,6 +620,7 @@ private void PathFindImplementation(uint unit, ref PathUnit data) {
}
}
Debug(unit, $"PathFindImplementation: Reachability graph:\n== REACHABLE ==\n" + reachableBuf + "\n== UNREACHABLE ==\n" + unreachableBuf);
FlushLog(false);
}
#endif
// NON-STOCK CODE END
@@ -673,6 +678,7 @@ private void PathFindImplementation(uint unit, ref PathUnit data) {

if (m_debug) {
Debug(unit, $"PathFindImplementation: Path-find succeeded");
FlushLog(true);
}
#endif
// NON-STOCK CODE END
@@ -692,6 +698,7 @@ private void PathFindImplementation(uint unit, ref PathUnit data) {

if (m_debug) {
Debug(unit, $"Path-finding failed: Could not create path unit");
FlushLog(false);
}
#endif
// NON-STOCK CODE END
@@ -728,23 +735,37 @@ private void PathFindImplementation(uint unit, ref PathUnit data) {

if (m_debug) {
Debug(unit, $"Path-finding failed: Internal loop break error");
FlushLog(false);
}
#endif
// NON-STOCK CODE END
}
}

#if DEBUG
private void FlushLog(bool success) {
if (m_logFailureOnly && success) {
return;
}

StringBuilder sb = new StringBuilder();
m_logBuffer.ForEach((entry) => {
sb.AppendLine(entry + "\n");
});
Log._Debug(sb.ToString());
m_logBuffer.Clear();
}

private void Debug(uint unit, string message) {
Log._Debug(
m_logBuffer.Add(
$"PF T#({Thread.CurrentThread.ManagedThreadId}) IDX#({m_pathFindIndex}):\n"
+ $"UNIT({unit})\n"
+ message
);
}

private void Debug(uint unit, BufferItem item, string message) {
Log._Debug(
m_logBuffer.Add(
$"PF T#({Thread.CurrentThread.ManagedThreadId}) IDX#({m_pathFindIndex}):\n"
+ $"UNIT({unit}): s#({item.m_position.m_segment}), l#({item.m_position.m_lane})\n"
+ $"ITEM({item})\n"
@@ -753,7 +774,7 @@ private void Debug(uint unit, BufferItem item, string message) {
}

private void Debug(uint unit, BufferItem item, ushort nextSegmentId, string message) {
Log._Debug(
m_logBuffer.Add(
$"PF T#({Thread.CurrentThread.ManagedThreadId}) IDX#({m_pathFindIndex}):\n"
+ $"UNIT({unit}): s#({item.m_position.m_segment}), l#({item.m_position.m_lane}) -> s#({nextSegmentId})\n"
+ $"ITEM({item})\n"
@@ -762,7 +783,7 @@ private void Debug(uint unit, BufferItem item, ushort nextSegmentId, string mess
}

private void Debug(uint unit, BufferItem item, ushort nextSegmentId, int nextLaneIndex, uint nextLaneId, string message) {
Log._Debug(
m_logBuffer.Add(
$"PF T#({Thread.CurrentThread.ManagedThreadId}) IDX#({m_pathFindIndex}):\n"
+ $"UNIT({unit}): s#({item.m_position.m_segment}), l#({item.m_position.m_lane}) -> s#({nextSegmentId}), l#({nextLaneIndex}), lid#({nextLaneId})\n"
+ $"ITEM({item})\n"
5 changes: 3 additions & 2 deletions TLM/TLM/State/ConfigData/Debug.cs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ public class Debug {
false, // 9: debug vehicle to segment end linking
false, // 10: prevent routing recalculation on global configuration reload
false, // 11: debug junction restrictions
false, // 12: - unused -
false, // 12: debug pedestrian path-finding
false, // 13: priority rules debug
false, // 14: disable GUI overlay of citizens having a valid path
false, // 15: disable checking of other vehicles for trams
@@ -34,7 +34,8 @@ public class Debug {
false, // 22: parking ai debug log (vehicles)
false, // 23: debug lane connections
false, // 24: debug resource loading
false // 25: debug turn-on-red
false, // 25: debug turn-on-red
false // 26: log failed path-finds only (requires Switch[0] to be true)
};

public int NodeId = 0;