summaryrefslogtreecommitdiff
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
parent85ebe20a761127bebe873389d81f31502685a98a (diff)
downloadopenttd-31ac11bddb71945bba57d0cefac620f455963455.tar.xz
Codechange: Increase readability of track functions and pathfinders.
-rw-r--r--src/pathfinder/follow_track.hpp2
-rw-r--r--src/pathfinder/npf/npf.cpp6
-rw-r--r--src/pathfinder/opf/opf_ship.cpp29
-rw-r--r--src/pathfinder/yapf/yapf_common.hpp3
-rw-r--r--src/pathfinder/yapf/yapf_costrail.hpp2
-rw-r--r--src/pathfinder/yapf/yapf_destrail.hpp9
-rw-r--r--src/pathfinder/yapf/yapf_road.cpp3
-rw-r--r--src/track_func.h2
8 files changed, 29 insertions, 27 deletions
diff --git a/src/pathfinder/follow_track.hpp b/src/pathfinder/follow_track.hpp
index ad81d974b..70f148de3 100644
--- a/src/pathfinder/follow_track.hpp
+++ b/src/pathfinder/follow_track.hpp
@@ -239,7 +239,7 @@ protected:
} else {
m_new_td_bits = TrackStatusToTrackdirBits(GetTileTrackStatus(m_new_tile, TT(), IsRoadTT() ? RoadVehicle::From(m_veh)->compatible_roadtypes : 0));
- if (IsTram() && m_new_td_bits == 0) {
+ if (IsTram() && m_new_td_bits == TRACKDIR_BIT_NONE) {
/* GetTileTrackStatus() returns 0 for single tram bits.
* As we cannot change it there (easily) without breaking something, change it here */
switch (GetSingleTramBit(m_new_tile)) {
diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp
index f989ff2c3..3218d8314 100644
--- a/src/pathfinder/npf/npf.cpp
+++ b/src/pathfinder/npf/npf.cpp
@@ -807,7 +807,7 @@ static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_tr
{
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
- if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
+ if (trackdirbits == TRACKDIR_BIT_NONE && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
/* GetTileTrackStatus() returns 0 for single tram bits.
* As we cannot change it there (easily) without breaking something, change it here */
switch (GetSingleTramBit(dst_tile)) {
@@ -900,7 +900,7 @@ static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
- if (trackdirbits == 0) {
+ if (trackdirbits == TRACKDIR_BIT_NONE) {
/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
@@ -924,7 +924,7 @@ static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
/* Enumerate possible track */
uint i = 0;
- while (trackdirbits != 0) {
+ while (trackdirbits != TRACKDIR_BIT_NONE) {
Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
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
}
diff --git a/src/pathfinder/yapf/yapf_common.hpp b/src/pathfinder/yapf/yapf_common.hpp
index 660c23116..ac2e5b5a2 100644
--- a/src/pathfinder/yapf/yapf_common.hpp
+++ b/src/pathfinder/yapf/yapf_common.hpp
@@ -142,8 +142,7 @@ public:
/** Called by YAPF to detect if node ends in the desired destination */
inline bool PfDetectDestination(Node &n)
{
- bool bDest = (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
- return bDest;
+ return (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
}
/**
diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp
index ca317f09a..5c5a7fbaf 100644
--- a/src/pathfinder/yapf/yapf_costrail.hpp
+++ b/src/pathfinder/yapf/yapf_costrail.hpp
@@ -104,7 +104,7 @@ public:
assert(IsValidTrackdir(td2));
int cost = 0;
if (TrackFollower::Allow90degTurns()
- && ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
+ && ((TrackdirToTrackdirBits(td2) & TrackdirCrossesTrackdirs(td1)) != TRACKDIR_BIT_NONE)) {
/* 90-deg curve penalty */
cost += Yapf().PfGetSettings().rail_curve90_penalty;
} else if (td2 != NextTrackdir(td1)) {
diff --git a/src/pathfinder/yapf/yapf_destrail.hpp b/src/pathfinder/yapf/yapf_destrail.hpp
index 03519b059..7b3f1c5a1 100644
--- a/src/pathfinder/yapf/yapf_destrail.hpp
+++ b/src/pathfinder/yapf/yapf_destrail.hpp
@@ -166,16 +166,13 @@ public:
/** Called by YAPF to detect if node ends in the desired destination */
inline bool PfDetectDestination(TileIndex tile, Trackdir td)
{
- bool bDest;
if (m_dest_station_id != INVALID_STATION) {
- bDest = HasStationTileRail(tile)
+ return HasStationTileRail(tile)
&& (GetStationIndex(tile) == m_dest_station_id)
&& (GetRailStationTrack(tile) == TrackdirToTrack(td));
- } else {
- bDest = (tile == m_destTile)
- && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
}
- return bDest;
+
+ return (tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
}
/**
diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp
index 380b641da..b2ec6ae23 100644
--- a/src/pathfinder/yapf/yapf_road.cpp
+++ b/src/pathfinder/yapf/yapf_road.cpp
@@ -185,8 +185,7 @@ public:
/** Called by YAPF to detect if node ends in the desired destination */
inline bool PfDetectDestination(Node &n)
{
- bool bDest = IsRoadDepotTile(n.m_segment_last_tile);
- return bDest;
+ return IsRoadDepotTile(n.m_segment_last_tile);
}
inline bool PfDetectDestinationTile(TileIndex tile, Trackdir trackdir)
diff --git a/src/track_func.h b/src/track_func.h
index 5e1585c08..c1a23ba03 100644
--- a/src/track_func.h
+++ b/src/track_func.h
@@ -61,7 +61,7 @@ static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
*/
static inline bool IsValidTrackdir(Trackdir trackdir)
{
- return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
+ return (1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE;
}
/**