From d7c5e9e8ab7fc877c619bb819a584c653a73d4b7 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Fri, 1 Mar 2024 13:32:13 +0000 Subject: [PATCH] Codechange: Where the ship comes from is already known This simplifies the handling of variables. `ChooseShipTrack` is called upon entering `tile`, and looking further back to the caller, it can be deduced that `v->tile` matches `src_tile`. With that said, `enterdir` can also be removed, as it's not used anywhere else. `CreateRandomPath` and `GetRandomFollowUpTrackdir` is being fed `src_tile` as it's 2nd parameter. This could be eliminated, as `v` is also being passed to it. Just use `v->tile` in those functions. --- src/pathfinder/yapf/yapf.h | 3 +-- src/pathfinder/yapf/yapf_ship.cpp | 20 +++++++++----------- src/ship_cmd.cpp | 9 +++------ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/pathfinder/yapf/yapf.h b/src/pathfinder/yapf/yapf.h index ca7b4f02c9..186986ce57 100644 --- a/src/pathfinder/yapf/yapf.h +++ b/src/pathfinder/yapf/yapf.h @@ -21,11 +21,10 @@ * Finds the best path for given ship using YAPF. * @param v the ship that needs to find a path * @param tile the tile to find the path from (should be next tile the ship is about to enter) - * @param enterdir diagonal direction which the ship will enter this new tile from * @param path_found [out] Whether a path has been found (true) or has been guessed (false) * @return the best trackdir for next turn or INVALID_TRACK if the path could not be found */ -Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, bool &path_found, ShipPathCache &path_cache); +Track YapfShipChooseTrack(const Ship *v, TileIndex tile, bool &path_found, ShipPathCache &path_cache); /** * Returns true if it is better to reverse the ship before leaving depot using YAPF. diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp index 709d4f2919..a4638879c5 100644 --- a/src/pathfinder/yapf/yapf_ship.cpp +++ b/src/pathfinder/yapf/yapf_ship.cpp @@ -188,9 +188,9 @@ public: } /** 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, Trackdir dir, ShipPathCache &path_cache, int path_length) { - std::pair tile_dir = { tile, dir }; + std::pair tile_dir = { v->tile, dir }; for (int i = 0; i < path_length; ++i) { tile_dir = GetRandomFollowUpTileTrackdir(v, tile_dir.first, tile_dir.second); if (tile_dir.second == INVALID_TRACKDIR) break; @@ -204,10 +204,8 @@ public: return result; } - static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, bool &path_found, ShipPathCache &path_cache) + static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, bool &path_found, ShipPathCache &path_cache) { - /* Move back to the old tile/trackdir (where ship is coming from). */ - const TileIndex src_tile = TileAddByDiagDir(tile, ReverseDiagDir(enterdir)); const Trackdir trackdir = v->GetVehicleTrackdir(); assert(IsValidTrackdir(trackdir)); @@ -218,7 +216,7 @@ public: if (high_level_path.empty()) { path_found = false; /* Make the ship move around aimlessly. This prevents repeated pathfinder calls and clearly indicates that the ship is lost. */ - return CreateRandomPath(v, src_tile, trackdir, path_cache, SHIP_LOST_PATH_LENGTH); + return CreateRandomPath(v, trackdir, path_cache, SHIP_LOST_PATH_LENGTH); } /* Try one time without restricting the search area, which generally results in better and more natural looking paths. @@ -228,7 +226,7 @@ public: Tpf pf(MAX_SHIP_PF_NODES); /* Set origin and destination nodes */ - pf.SetOrigin(src_tile, trackdirs); + pf.SetOrigin(v->tile, trackdirs); pf.SetDestination(v); const bool is_intermediate_destination = static_cast(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1; if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back()); @@ -241,7 +239,7 @@ public: path_found = pf.FindPath(v); Node *node = pf.GetBestNode(); if (attempt == 0 && !path_found) continue; // Try again with restricted search area. - if (!path_found || node == nullptr) return GetRandomFollowUpTileTrackdir(v, src_tile, trackdir).second; + if (!path_found || node == nullptr) return GetRandomFollowUpTileTrackdir(v, v->tile, trackdir).second; /* 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 @@ -267,7 +265,7 @@ public: /* A empty path means we are already at the destination. The pathfinder shouldn't have been called at all. * Return a random reachable trackdir to hopefully nudge the ship out of this strange situation. */ - if (path_cache.empty()) return GetRandomFollowUpTileTrackdir(v, src_tile, trackdir).second; + if (path_cache.empty()) return GetRandomFollowUpTileTrackdir(v, v->tile, trackdir).second; /* Take out the last trackdir as the result. */ const Trackdir result = path_cache.front(); @@ -437,9 +435,9 @@ struct CYapfShip : CYapfTpath); break; + case VPF_YAPF: track = YapfShipChooseTrack(v, tile, path_found, v->path); break; default: NOT_REACHED(); } } @@ -821,7 +818,7 @@ static void ShipController(Ship *v) } /* Choose a direction, and continue if we find one */ - const Track track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks); + const Track track = ChooseShipTrack(v, gp.new_tile, tracks); if (track == INVALID_TRACK) return ReverseShip(v); const ShipSubcoordData &b = _ship_subcoord[diagdir][track];