From 40457fc0ddfcf586747a3069c79aa3ca28e91aad Mon Sep 17 00:00:00 2001 From: Kuhnovic <68320206+Kuhnovic@users.noreply.github.com> Date: Fri, 8 Aug 2025 09:52:01 +0200 Subject: [PATCH] Change: Separate ships travelling in opposite direction (#14493) --- src/pathfinder/yapf/yapf_ship.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp index a47df5abb3..9698cc603f 100644 --- a/src/pathfinder/yapf/yapf_ship.cpp +++ b/src/pathfinder/yapf/yapf_ship.cpp @@ -355,6 +355,23 @@ public: return 0; } + /** + * Whether the provided direction is a preferred direction for a given tile. This is used to separate ships travelling in opposite directions. + * @param tile Tile of current node. + * @param td Trackdir of current node. + * @returns true if a preferred direction, false otherwise. + */ + inline static bool IsPreferredShipDirection(TileIndex tile, Trackdir td) + { + const bool odd_x = TileX(tile) & 1; + const bool odd_y = TileY(tile) & 1; + if (td == TRACKDIR_X_NE) return odd_y; + if (td == TRACKDIR_X_SW) return !odd_y; + if (td == TRACKDIR_Y_NW) return odd_x; + if (td == TRACKDIR_Y_SE) return !odd_x; + return (odd_x ^ odd_y) ^ HasBit(TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LEFT_S | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_LOWER_E, td); + } + /** * Called by YAPF to calculate the cost from the origin to the given node. * Calculates only the cost of given node, adds it to the parent node cost @@ -376,6 +393,9 @@ public: c += count * 3 * YAPF_TILE_LENGTH; } + /* Encourage separation between ships traveling in different directions. */ + if (!IsPreferredShipDirection(n.GetTile(), n.GetTrackdir())) c += YAPF_TILE_LENGTH; + /* Skipped tile cost for aqueducts. */ c += YAPF_TILE_LENGTH * tf->tiles_skipped;