From 6ff0858b38cac1fa734081adef1ae6d6b6297a13 Mon Sep 17 00:00:00 2001 From: Loïc Guilloux Date: Sat, 6 Nov 2021 17:32:37 +0100 Subject: Fix #9669, cbe00ec: Only try valid reverse directions (#9672) --- src/pathfinder/npf/npf.cpp | 7 +++++-- src/pathfinder/yapf/yapf_ship.cpp | 8 +++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp index f7cb1a5cd..25681507c 100644 --- a/src/pathfinder/npf/npf.cpp +++ b/src/pathfinder/npf/npf.cpp @@ -1225,9 +1225,12 @@ bool NPFShipCheckReverse(const Ship *v, Trackdir *best_td) AyStarUserData user = { v->owner, TRANSPORT_WATER, RAILTYPES_NONE, ROADTYPES_NONE, 0 }; if (best_td != nullptr) { - TrackdirBits rtds = DiagdirReachesTrackdirs(ReverseDiagDir(VehicleExitDir(v->direction, v->state))); + DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state)); + TrackdirBits rtds = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(v->tile, TRANSPORT_WATER, 0, entry)); Trackdir best = (Trackdir)FindFirstBit2x64(rtds); - for (rtds = KillFirstBit(rtds); rtds != TRACKDIR_BIT_NONE; rtds = KillFirstBit(rtds)) { + rtds = KillFirstBit(rtds); + if (rtds == TRACKDIR_BIT_NONE) return false; /* At most one choice. */ + for (; rtds != TRACKDIR_BIT_NONE; rtds = KillFirstBit(rtds)) { Trackdir td = (Trackdir)FindFirstBit2x64(rtds); ftd = NPFRouteToStationOrTileTwoWay(v->tile, best, false, v->tile, td, false, &fstd, &user); if (ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) best = td; diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp index 25a63ec66..947de5a76 100644 --- a/src/pathfinder/yapf/yapf_ship.cpp +++ b/src/pathfinder/yapf/yapf_ship.cpp @@ -212,7 +212,13 @@ public: /* create pathfinder instance */ Tpf pf; /* set origin and destination nodes */ - pf.SetOrigin(tile, trackdir == nullptr ? TrackdirToTrackdirBits(td1) | TrackdirToTrackdirBits(td2) : DiagdirReachesTrackdirs(ReverseDiagDir(VehicleExitDir(v->direction, v->state)))); + if (trackdir == nullptr) { + pf.SetOrigin(tile, TrackdirToTrackdirBits(td1) | TrackdirToTrackdirBits(td2)); + } else { + DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state)); + TrackdirBits rtds = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0, entry)); + pf.SetOrigin(tile, rtds); + } pf.SetDestination(v); /* find best path */ if (!pf.FindPath(v)) return false; -- cgit v1.2.3-70-g09d2