summaryrefslogtreecommitdiff
path: root/src/pathfinder/opf
diff options
context:
space:
mode:
authorJ0anJosep <juanjo.ng.83@gmail.com>2018-04-29 12:23:01 +0200
committerfrosch <github@elsenhans.name>2018-06-27 23:14:30 +0200
commit31ac11bddb71945bba57d0cefac620f455963455 (patch)
tree6cd92f96ed2dd0ab601c961723aade3a6f18c4a5 /src/pathfinder/opf
parent85ebe20a761127bebe873389d81f31502685a98a (diff)
downloadopenttd-31ac11bddb71945bba57d0cefac620f455963455.tar.xz
Codechange: Increase readability of track functions and pathfinders.
Diffstat (limited to 'src/pathfinder/opf')
-rw-r--r--src/pathfinder/opf/opf_ship.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/pathfinder/opf/opf_ship.cpp b/src/pathfinder/opf/opf_ship.cpp
index 023c6a4a0..e3b7db5a6 100644
--- a/src/pathfinder/opf/opf_ship.cpp
+++ b/src/pathfinder/opf/opf_ship.cpp
@@ -183,9 +183,14 @@ bad:;
}
/**
- * returns the track to choose on the next tile, or -1 when it's better to
- * reverse. The tile given is the tile we are about to enter, enterdir is the
- * direction in which we are entering the tile
+ * Finds the best track to choose on the next tile and
+ * returns INVALID_TRACK when it is better to reverse.
+ * @param v The ship.
+ * @param tile The tile we are about to enter.
+ * @param enterdir The direction entering the tile.
+ * @param tracks The tracks available on new tile.
+ * @param[out] path_found Whether a path has been found.
+ * @return Best track on next tile or INVALID_TRACK when better to reverse.
*/
Track OPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found)
{
@@ -195,13 +200,15 @@ Track OPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir,
Track track;
/* Let's find out how far it would be if we would reverse first */
- Trackdir trackdir = v->GetVehicleTrackdir();
- TrackBits b = TrackStatusToTrackBits(GetTileTrackStatus(tile2, TRANSPORT_WATER, 0)) & DiagdirReachesTracks(ReverseDiagDir(enterdir)) & TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));
-
- uint distr = UINT_MAX; // distance if we reversed
- if (b != 0) {
- distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
- if (distr != UINT_MAX) distr++; // penalty for reversing
+ uint rev_dist = UINT_MAX; // distance if we reverse
+ Track cur_track = TrackdirToTrack(v->GetVehicleTrackdir()); // track on the current tile
+ DiagDirection rev_enterdir = ReverseDiagDir(enterdir);
+ TrackBits rev_tracks = TrackStatusToTrackBits(GetTileTrackStatus(tile2, TRANSPORT_WATER, 0)) &
+ DiagdirReachesTracks(rev_enterdir);
+
+ if ((rev_tracks & TrackToTrackBits(cur_track) != TRACK_BIT_NONE) {
+ rev_dist = FindShipTrack(v, tile2, rev_enterdir, TrackToTrackBits(cur_track), tile, &track);
+ if (rev_dist != UINT_MAX) rev_dist++; // penalty for reversing
}
/* And if we would not reverse? */
@@ -209,6 +216,6 @@ Track OPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir,
/* Due to the way this pathfinder works we cannot determine whether we're lost or not. */
path_found = true;
- if (dist <= distr) return track;
+ if (dist <= rev_dist) return track;
return INVALID_TRACK; // We could better reverse
}