Fix #11840: Ship pathfinder always returns a valid trackdir if one is available (#12031)

This commit is contained in:
Kuhnovic 2024-02-09 18:31:38 +01:00 committed by GitHub
parent b68d263d5f
commit e38c3c59f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -172,20 +172,28 @@ public:
return 'w'; return 'w';
} }
/** Returns a random trackdir that can be reached from the current tile/trackdir, or INVALID_TRACK if none is available. */
static Trackdir GetRandomFollowUpTrackdir(const Ship *v, TileIndex tile, Trackdir dir, bool include_90_degree_turns)
{
TrackFollower follower(v);
if (follower.Follow(tile, dir)) {
tile = follower.m_new_tile;
TrackdirBits dirs = follower.m_new_td_bits;
if (!include_90_degree_turns) dirs &= ~TrackdirCrossesTrackdirs(dir);
const int strip_amount = _random.Next(CountBits(dirs));
for (int s = 0; s < strip_amount; ++s) RemoveFirstTrackdir(&dirs);
return FindFirstTrackdir(dirs);
}
return INVALID_TRACKDIR;
}
/** Creates a random path, avoids 90 degree turns. */ /** Creates a random path, avoids 90 degree turns. */
static Trackdir CreateRandomPath(const Ship *v, TileIndex tile, Trackdir dir, ShipPathCache &path_cache, int path_length) static Trackdir CreateRandomPath(const Ship *v, TileIndex tile, Trackdir dir, ShipPathCache &path_cache, int path_length)
{ {
for (int i = 0; i < path_length; ++i) { for (int i = 0; i < path_length; ++i) {
TrackFollower F(v); const Trackdir random_dir = GetRandomFollowUpTrackdir(v, tile, dir, false);
if (F.Follow(tile, dir)) { if (random_dir == INVALID_TRACKDIR) break;
tile = F.m_new_tile; path_cache.push_back(random_dir);
TrackdirBits dirs = F.m_new_td_bits & ~TrackdirCrossesTrackdirs(dir);
const int strip_amount = _random.Next(CountBits(dirs));
for (int s = 0; s < strip_amount; ++s) RemoveFirstTrackdir(&dirs);
dir = FindFirstTrackdir(dirs);
if (dir == INVALID_TRACKDIR) break;
path_cache.push_back(dir);
}
} }
if (path_cache.empty()) return INVALID_TRACKDIR; if (path_cache.empty()) return INVALID_TRACKDIR;
@ -210,12 +218,12 @@ public:
} }
/* Move back to the old tile/trackdir (where ship is coming from). */ /* Move back to the old tile/trackdir (where ship is coming from). */
TileIndex src_tile = TileAddByDiagDir(tile, ReverseDiagDir(enterdir)); const TileIndex src_tile = TileAddByDiagDir(tile, ReverseDiagDir(enterdir));
Trackdir trackdir = v->GetVehicleTrackdir(); const Trackdir trackdir = v->GetVehicleTrackdir();
assert(IsValidTrackdir(trackdir)); assert(IsValidTrackdir(trackdir));
/* Convert origin trackdir to TrackdirBits. */ /* Convert origin trackdir to TrackdirBits. */
TrackdirBits trackdirs = TrackdirToTrackdirBits(trackdir); const TrackdirBits trackdirs = TrackdirToTrackdirBits(trackdir);
const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1); const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1);
if (high_level_path.empty()) { if (high_level_path.empty()) {
@ -244,7 +252,7 @@ public:
path_found = pf.FindPath(v); path_found = pf.FindPath(v);
Node *node = pf.GetBestNode(); Node *node = pf.GetBestNode();
if (attempt == 0 && !path_found) continue; // Try again with restricted search area. if (attempt == 0 && !path_found) continue; // Try again with restricted search area.
if (!path_found || !node) return INVALID_TRACKDIR; if (!path_found || node == nullptr) GetRandomFollowUpTrackdir(v, src_tile, trackdir, true);
/* Return only the path within the current water region if an intermediate destination was returned. If not, cache the entire path /* Return only the path within the current water region if an intermediate destination was returned. If not, cache the entire path
* to the final destination tile. The low-level pathfinder might actually prefer a different docking tile in a nearby region. Without * to the final destination tile. The low-level pathfinder might actually prefer a different docking tile in a nearby region. Without