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.
pull/688/head
SamuXarick 3 months ago committed by Kuhnovic
parent 7f49b6f25a
commit d7c5e9e8ab

@ -21,11 +21,10 @@
* Finds the best path for given ship using YAPF. * Finds the best path for given ship using YAPF.
* @param v the ship that needs to find a path * @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 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) * @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 * @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. * Returns true if it is better to reverse the ship before leaving depot using YAPF.

@ -188,9 +188,9 @@ public:
} }
/** 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, Trackdir dir, ShipPathCache &path_cache, int path_length)
{ {
std::pair<TileIndex, Trackdir> tile_dir = { tile, dir }; std::pair<TileIndex, Trackdir> tile_dir = { v->tile, dir };
for (int i = 0; i < path_length; ++i) { for (int i = 0; i < path_length; ++i) {
tile_dir = GetRandomFollowUpTileTrackdir(v, tile_dir.first, tile_dir.second); tile_dir = GetRandomFollowUpTileTrackdir(v, tile_dir.first, tile_dir.second);
if (tile_dir.second == INVALID_TRACKDIR) break; if (tile_dir.second == INVALID_TRACKDIR) break;
@ -204,10 +204,8 @@ public:
return result; 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(); const Trackdir trackdir = v->GetVehicleTrackdir();
assert(IsValidTrackdir(trackdir)); assert(IsValidTrackdir(trackdir));
@ -218,7 +216,7 @@ public:
if (high_level_path.empty()) { if (high_level_path.empty()) {
path_found = false; path_found = false;
/* Make the ship move around aimlessly. This prevents repeated pathfinder calls and clearly indicates that the ship is lost. */ /* 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. /* 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); Tpf pf(MAX_SHIP_PF_NODES);
/* Set origin and destination nodes */ /* Set origin and destination nodes */
pf.SetOrigin(src_tile, trackdirs); pf.SetOrigin(v->tile, trackdirs);
pf.SetDestination(v); pf.SetDestination(v);
const bool is_intermediate_destination = static_cast<int>(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1; const bool is_intermediate_destination = static_cast<int>(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1;
if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back()); if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back());
@ -241,7 +239,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 == 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 /* 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
@ -267,7 +265,7 @@ public:
/* A empty path means we are already at the destination. The pathfinder shouldn't have been called at all. /* 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. */ * 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. */ /* Take out the last trackdir as the result. */
const Trackdir result = path_cache.front(); const Trackdir result = path_cache.front();
@ -437,9 +435,9 @@ struct CYapfShip : CYapfT<CYapfShip_TypesT<CYapfShip, CFollowTrackWater, CShipNo
}; };
/** Ship controller helper - path finder invoker. */ /** Ship controller helper - path finder invoker. */
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)
{ {
Trackdir td_ret = CYapfShip::ChooseShipTrack(v, tile, enterdir, path_found, path_cache); Trackdir td_ret = CYapfShip::ChooseShipTrack(v, tile, path_found, path_cache);
return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK; return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
} }

@ -501,14 +501,11 @@ static void ShipArrivesAt(const Vehicle *v, Station *st)
* *
* @param v Ship to navigate * @param v Ship to navigate
* @param tile Tile, the ship is about to enter * @param tile Tile, the ship is about to enter
* @param enterdir Direction of entering
* @param tracks Available track choices on \a tile * @param tracks Available track choices on \a tile
* @return Track to choose, or INVALID_TRACK when to reverse. * @return Track to choose, or INVALID_TRACK when to reverse.
*/ */
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks) static Track ChooseShipTrack(Ship *v, TileIndex tile, TrackBits tracks)
{ {
assert(IsValidDiagDirection(enterdir));
bool path_found = true; bool path_found = true;
Track track; Track track;
@ -535,7 +532,7 @@ static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, Tr
switch (_settings_game.pf.pathfinder_for_ships) { switch (_settings_game.pf.pathfinder_for_ships) {
case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break; case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break;
case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, path_found, v->path); break; case VPF_YAPF: track = YapfShipChooseTrack(v, tile, path_found, v->path); break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
} }
@ -821,7 +818,7 @@ static void ShipController(Ship *v)
} }
/* Choose a direction, and continue if we find one */ /* 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); if (track == INVALID_TRACK) return ReverseShip(v);
const ShipSubcoordData &b = _ship_subcoord[diagdir][track]; const ShipSubcoordData &b = _ship_subcoord[diagdir][track];

Loading…
Cancel
Save