summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ0anJosep <juanjo.ng.83@gmail.com>2018-05-12 18:19:40 +0200
committerfrosch <github@elsenhans.name>2018-06-27 23:14:30 +0200
commitd01c09fb73aacd1b3c204829e7c574bbde09153f (patch)
tree5327845c01eeba8864925a38d97ab210b58f1850
parent31ac11bddb71945bba57d0cefac620f455963455 (diff)
downloadopenttd-d01c09fb73aacd1b3c204829e7c574bbde09153f.tar.xz
Codechange: Use HasTrack(dir) to improve code readability.
-rw-r--r--src/pathfinder/opf/opf_ship.cpp2
-rw-r--r--src/pathfinder/yapf/yapf_common.hpp2
-rw-r--r--src/pathfinder/yapf/yapf_costrail.hpp4
-rw-r--r--src/pathfinder/yapf/yapf_destrail.hpp2
-rw-r--r--src/pathfinder/yapf/yapf_road.cpp6
-rw-r--r--src/pathfinder/yapf/yapf_ship.cpp2
-rw-r--r--src/track_func.h22
7 files changed, 31 insertions, 9 deletions
diff --git a/src/pathfinder/opf/opf_ship.cpp b/src/pathfinder/opf/opf_ship.cpp
index e3b7db5a6..1a866337c 100644
--- a/src/pathfinder/opf/opf_ship.cpp
+++ b/src/pathfinder/opf/opf_ship.cpp
@@ -206,7 +206,7 @@ Track OPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir,
TrackBits rev_tracks = TrackStatusToTrackBits(GetTileTrackStatus(tile2, TRANSPORT_WATER, 0)) &
DiagdirReachesTracks(rev_enterdir);
- if ((rev_tracks & TrackToTrackBits(cur_track) != TRACK_BIT_NONE) {
+ if (HasTrack(rev_tracks, cur_track)) {
rev_dist = FindShipTrack(v, tile2, rev_enterdir, TrackToTrackBits(cur_track), tile, &track);
if (rev_dist != UINT_MAX) rev_dist++; // penalty for reversing
}
diff --git a/src/pathfinder/yapf/yapf_common.hpp b/src/pathfinder/yapf/yapf_common.hpp
index ac2e5b5a2..8ff69b3f6 100644
--- a/src/pathfinder/yapf/yapf_common.hpp
+++ b/src/pathfinder/yapf/yapf_common.hpp
@@ -142,7 +142,7 @@ public:
/** Called by YAPF to detect if node ends in the desired destination */
inline bool PfDetectDestination(Node &n)
{
- return (n.m_key.m_tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(n.GetTrackdir())) != TRACKDIR_BIT_NONE);
+ return (n.m_key.m_tile == m_destTile) && HasTrackdir(m_destTrackdirs, n.GetTrackdir());
}
/**
diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp
index 5c5a7fbaf..022b9a167 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) & TrackdirCrossesTrackdirs(td1)) != TRACKDIR_BIT_NONE)) {
+ && HasTrackdir(TrackdirCrossesTrackdirs(td1), td2)) {
/* 90-deg curve penalty */
cost += Yapf().PfGetSettings().rail_curve90_penalty;
} else if (td2 != NextTrackdir(td1)) {
@@ -280,7 +280,7 @@ public:
{
assert(!n.flags_u.flags_s.m_targed_seen);
assert(tf->m_new_tile == n.m_key.m_tile);
- assert((TrackdirToTrackdirBits(n.m_key.m_td) & tf->m_new_td_bits) != TRACKDIR_BIT_NONE);
+ assert((HasTrackdir(tf->m_new_td_bits, n.m_key.m_td)));
CPerfStart perf_cost(Yapf().m_perf_cost);
diff --git a/src/pathfinder/yapf/yapf_destrail.hpp b/src/pathfinder/yapf/yapf_destrail.hpp
index 7b3f1c5a1..1d1833fbf 100644
--- a/src/pathfinder/yapf/yapf_destrail.hpp
+++ b/src/pathfinder/yapf/yapf_destrail.hpp
@@ -172,7 +172,7 @@ public:
&& (GetRailStationTrack(tile) == TrackdirToTrack(td));
}
- return (tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
+ return (tile == m_destTile) && HasTrackdir(m_destTrackdirs, td);
}
/**
diff --git a/src/pathfinder/yapf/yapf_road.cpp b/src/pathfinder/yapf/yapf_road.cpp
index b2ec6ae23..edc8d2a3a 100644
--- a/src/pathfinder/yapf/yapf_road.cpp
+++ b/src/pathfinder/yapf/yapf_road.cpp
@@ -260,7 +260,7 @@ public:
(m_non_artic || IsDriveThroughStopTile(tile));
}
- return tile == m_destTile && ((m_destTrackdirs & TrackdirToTrackdirBits(trackdir)) != TRACKDIR_BIT_NONE);
+ return tile == m_destTile && HasTrackdir(m_destTrackdirs, trackdir);
}
/**
@@ -420,7 +420,7 @@ public:
/* set origin (tile, trackdir) */
TileIndex src_tile = v->tile;
Trackdir src_td = v->GetVehicleTrackdir();
- if ((TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, v->compatible_roadtypes)) & TrackdirToTrackdirBits(src_td)) == 0) {
+ if (!HasTrackdir(TrackStatusToTrackdirBits(GetTileTrackStatus(src_tile, TRANSPORT_ROAD, v->compatible_roadtypes)), src_td)) {
/* sometimes the roadveh is not on the road (it resides on non-existing track)
* how should we handle that situation? */
return false;
@@ -503,7 +503,7 @@ FindDepotData YapfRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_dist
{
TileIndex tile = v->tile;
Trackdir trackdir = v->GetVehicleTrackdir();
- if ((TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, v->compatible_roadtypes)) & TrackdirToTrackdirBits(trackdir)) == 0) {
+ if (!HasTrackdir(TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, v->compatible_roadtypes)), trackdir)) {
return FindDepotData();
}
diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp
index df6cd468c..44a5c0cfa 100644
--- a/src/pathfinder/yapf/yapf_ship.cpp
+++ b/src/pathfinder/yapf/yapf_ship.cpp
@@ -65,7 +65,7 @@ public:
/* use vehicle's current direction if that's possible, otherwise use first usable one. */
Trackdir veh_dir = v->GetVehicleTrackdir();
- return ((trackdirs & TrackdirToTrackdirBits(veh_dir)) != 0) ? veh_dir : (Trackdir)FindFirstBit2x64(trackdirs);
+ return (HasTrackdir(trackdirs, veh_dir)) ? veh_dir : (Trackdir)FindFirstBit2x64(trackdirs);
}
/* move back to the old tile/trackdir (where ship is coming from) */
diff --git a/src/track_func.h b/src/track_func.h
index c1a23ba03..8e2056265 100644
--- a/src/track_func.h
+++ b/src/track_func.h
@@ -332,6 +332,28 @@ static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
}
/**
+ * Checks whether a TrackBits has a given Track.
+ * @param tracks The track bits.
+ * @param track The track to check.
+ */
+static inline bool HasTrack(TrackBits tracks, Track track)
+{
+ assert(IsValidTrack(track));
+ return HasBit(tracks, track);
+}
+
+/**
+ * Checks whether a TrackdirBits has a given Trackdir.
+ * @param trackdirs The trackdir bits.
+ * @param trackdir The trackdir to check.
+ */
+static inline bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
+{
+ assert(IsValidTrackdir(trackdir));
+ return HasBit(trackdirs, trackdir);
+}
+
+/**
* Returns the present-trackdir-information of a TrackStatus.
*
* @param ts The TrackStatus returned by GetTileTrackStatus()