From 5196caddde78faeeccf39529d3d612fbd3ea6ba2 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 4 Sep 2020 17:41:27 +0100 Subject: [PATCH] Fix assertion failure which could occur when using reverse behind signal --- src/pathfinder/yapf/yapf_costrail.hpp | 13 ++++--------- src/pathfinder/yapf/yapf_destrail.hpp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index 1549467189..8d176ec802 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -478,15 +478,6 @@ public: /* Skip the first transition cost calculation. */ goto no_entry_cost; } else if (n.flags_u.flags_s.m_teleport) { - int x1 = 2 * TileX(prev.tile); - int y1 = 2 * TileY(prev.tile); - int x2 = 2 * TileX(cur.tile); - int y2 = 2 * TileY(cur.tile); - int dx = abs(x1 - x2) + 4; // up to 2x track exit dir tile offsets in opposite directions - int dy = abs(y1 - y2) + 4; // " - int dmin = min(dx, dy); - int dxy = abs(dx - dy); - segment_entry_cost += dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2); goto no_entry_cost; } @@ -765,6 +756,10 @@ no_entry_cost: // jump here at the beginning if the node has no parent (it is th } } + if (has_parent && n.flags_u.flags_s.m_teleport) { + extra_cost += Yapf().TeleportCost(n.GetLastTile(), n.m_parent->GetLastTile()); + } + /* total node cost */ n.m_cost = parent_cost + segment_entry_cost + segment_cost + extra_cost; diff --git a/src/pathfinder/yapf/yapf_destrail.hpp b/src/pathfinder/yapf/yapf_destrail.hpp index 05a1235778..89a323ccfc 100644 --- a/src/pathfinder/yapf/yapf_destrail.hpp +++ b/src/pathfinder/yapf/yapf_destrail.hpp @@ -67,6 +67,11 @@ public: n.m_estimate = n.m_cost; return true; } + + inline int TeleportCost(TileIndex cur_tile, TileIndex prev_tile) + { + return 0; + } }; template @@ -105,6 +110,11 @@ public: n.m_estimate = n.m_cost; return true; } + + inline int TeleportCost(TileIndex cur_tile, TileIndex prev_tile) + { + return 0; + } }; template @@ -201,6 +211,22 @@ public: assert(n.m_estimate >= n.m_parent->m_estimate); return true; } + + inline int TeleportCost(TileIndex cur_tile, TileIndex prev_tile) + { + auto calculate_distance_cost = [&](TileIndex t, int d_adjust) -> int { + int x1 = 2 * TileX(t); + int y1 = 2 * TileY(t); + int x2 = 2 * TileX(m_destTile); + int y2 = 2 * TileY(m_destTile); + int dx = abs(x1 - x2) + d_adjust; + int dy = abs(y1 - y2); + int dmin = min(dx, dy) + d_adjust; // up to 2x track exit dir tile offsets in opposite directions + int dxy = abs(dx - dy) + d_adjust; // " + return dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2); + }; + return max(0, calculate_distance_cost(prev_tile, 8) - calculate_distance_cost(cur_tile, 0)); + } }; #endif /* YAPF_DESTRAIL_HPP */