summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2013-11-25 13:16:06 +0000
committerrubidium <rubidium@openttd.org>2013-11-25 13:16:06 +0000
commit4b8d9aad5b4359345c79f8f5567db4f88c2d41e1 (patch)
tree38093606b7d87024119289e4390e831c43e9117a /src
parentc143ca729a823f58cfa45aa2ea464d436b81d66b (diff)
downloadopenttd-4b8d9aad5b4359345c79f8f5567db4f88c2d41e1.tar.xz
(svn r26105) -Codechange: add a lot of assertions to track/road functions that might eventually lead to become indices into tables
Diffstat (limited to 'src')
-rw-r--r--src/direction_func.h80
-rw-r--r--src/road_func.h19
-rw-r--r--src/track_func.h95
3 files changed, 131 insertions, 63 deletions
diff --git a/src/direction_func.h b/src/direction_func.h
index 657645e28..888948584 100644
--- a/src/direction_func.h
+++ b/src/direction_func.h
@@ -15,6 +15,39 @@
#include "direction_type.h"
/**
+ * Checks if an integer value is a valid DiagDirection
+ *
+ * @param d The value to check
+ * @return True if the value belongs to a DiagDirection, else false
+ */
+static inline bool IsValidDiagDirection(DiagDirection d)
+{
+ return d < DIAGDIR_END;
+}
+
+/**
+ * Checks if an integer value is a valid Direction
+ *
+ * @param d The value to check
+ * @return True if the value belongs to a Direction, else false
+ */
+static inline bool IsValidDirection(Direction d)
+{
+ return d < DIR_END;
+}
+
+/**
+ * Checks if an integer value is a valid Axis
+ *
+ * @param d The value to check
+ * @return True if the value belongs to an Axis, else false
+ */
+static inline bool IsValidAxis(Axis d)
+{
+ return d < AXIS_END;
+}
+
+/**
* Return the reverse of a direction
*
* @param d The direction to get the reverse from
@@ -22,6 +55,7 @@
*/
static inline Direction ReverseDir(Direction d)
{
+ assert(IsValidDirection(d));
return (Direction)(4 ^ d);
}
@@ -35,6 +69,8 @@ static inline Direction ReverseDir(Direction d)
*/
static inline DirDiff DirDifference(Direction d0, Direction d1)
{
+ assert(IsValidDirection(d0));
+ assert(IsValidDirection(d1));
/* Cast to uint so compiler can use bitmask. If the difference is negative
* and we used int instead of uint, further "+ 8" would have to be added. */
return (DirDiff)((uint)(d0 - d1) % 8);
@@ -69,6 +105,7 @@ static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
*/
static inline Direction ChangeDir(Direction d, DirDiff delta)
{
+ assert(IsValidDirection(d));
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
return (Direction)((uint)(d + delta) % 8);
}
@@ -82,6 +119,7 @@ static inline Direction ChangeDir(Direction d, DirDiff delta)
*/
static inline DiagDirection ReverseDiagDir(DiagDirection d)
{
+ assert(IsValidDiagDirection(d));
return (DiagDirection)(2 ^ d);
}
@@ -98,6 +136,7 @@ static inline DiagDirection ReverseDiagDir(DiagDirection d)
*/
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
{
+ assert(IsValidDiagDirection(d));
/* Cast to uint so compiler can use bitmask. Result can never be negative. */
return (DiagDirection)((uint)(d + delta) % 4);
}
@@ -114,6 +153,7 @@ static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
*/
static inline DiagDirection DirToDiagDir(Direction dir)
{
+ assert(IsValidDirection(dir));
return (DiagDirection)(dir >> 1);
}
@@ -129,6 +169,7 @@ static inline DiagDirection DirToDiagDir(Direction dir)
*/
static inline Direction DiagDirToDir(DiagDirection dir)
{
+ assert(IsValidDiagDirection(dir));
return (Direction)(dir * 2 + 1);
}
@@ -143,6 +184,7 @@ static inline Direction DiagDirToDir(DiagDirection dir)
*/
static inline Axis OtherAxis(Axis a)
{
+ assert(IsValidAxis(a));
return (Axis)(a ^ 1);
}
@@ -159,6 +201,7 @@ static inline Axis OtherAxis(Axis a)
*/
static inline Axis DiagDirToAxis(DiagDirection d)
{
+ assert(IsValidDiagDirection(d));
return (Axis)(d & 1);
}
@@ -176,6 +219,7 @@ static inline Axis DiagDirToAxis(DiagDirection d)
*/
static inline DiagDirection AxisToDiagDir(Axis a)
{
+ assert(IsValidAxis(a));
return (DiagDirection)(2 - a);
}
@@ -192,6 +236,7 @@ static inline DiagDirection AxisToDiagDir(Axis a)
*/
static inline Direction AxisToDirection(Axis a)
{
+ assert(IsValidAxis(a));
return (Direction)(5 - 2 * a);
}
@@ -203,43 +248,11 @@ static inline Direction AxisToDirection(Axis a)
*/
static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
{
+ assert(IsValidAxis(xy));
return (DiagDirection)(xy * 3 ^ ns * 2);
}
/**
- * Checks if an integer value is a valid DiagDirection
- *
- * @param d The value to check
- * @return True if the value belongs to a DiagDirection, else false
- */
-static inline bool IsValidDiagDirection(DiagDirection d)
-{
- return d < DIAGDIR_END;
-}
-
-/**
- * Checks if an integer value is a valid Direction
- *
- * @param d The value to check
- * @return True if the value belongs to a Direction, else false
- */
-static inline bool IsValidDirection(Direction d)
-{
- return d < DIR_END;
-}
-
-/**
- * Checks if an integer value is a valid Axis
- *
- * @param d The value to check
- * @return True if the value belongs to an Axis, else false
- */
-static inline bool IsValidAxis(Axis d)
-{
- return d < AXIS_END;
-}
-
-/**
* Checks if a given Direction is diagonal.
*
* @param dir The given direction.
@@ -247,6 +260,7 @@ static inline bool IsValidAxis(Axis d)
*/
static inline bool IsDiagonalDirection(Direction dir)
{
+ assert(IsValidDirection(dir));
return (dir & 1) != 0;
}
diff --git a/src/road_func.h b/src/road_func.h
index bfe261980..c4af229d5 100644
--- a/src/road_func.h
+++ b/src/road_func.h
@@ -38,6 +38,16 @@ static inline bool IsValidRoadType(RoadType rt)
}
/**
+ * Whether the given roadtype is valid.
+ * @param rt the roadtype to check for validness
+ * @return true if and only if valid
+ */
+static inline bool IsValidRoadBits(RoadBits r)
+{
+ return r < ROAD_END;
+}
+
+/**
* Maps a RoadType to the corresponding RoadTypes value
*
* @param rt the roadtype to get the roadtypes from
@@ -45,6 +55,7 @@ static inline bool IsValidRoadType(RoadType rt)
*/
static inline RoadTypes RoadTypeToRoadTypes(RoadType rt)
{
+ assert(IsValidRoadType(rt));
return (RoadTypes)(1 << rt);
}
@@ -73,6 +84,7 @@ static inline RoadTypes ComplementRoadTypes(RoadTypes r)
*/
static inline RoadBits ComplementRoadBits(RoadBits r)
{
+ assert(IsValidRoadBits(r));
return (RoadBits)(ROAD_ALL ^ r);
}
@@ -86,6 +98,7 @@ static inline RoadBits ComplementRoadBits(RoadBits r)
*/
static inline RoadBits MirrorRoadBits(RoadBits r)
{
+ assert(IsValidRoadBits(r));
return (RoadBits)(GB(r, 0, 2) << 2 | GB(r, 2, 2));
}
@@ -100,6 +113,7 @@ static inline RoadBits MirrorRoadBits(RoadBits r)
*/
static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
{
+ assert(IsValidRoadBits(r));
for (; rot > (DiagDirDiff)0; rot--) {
r = (RoadBits)(GB(r, 0, 1) << 3 | GB(r, 1, 3));
}
@@ -114,6 +128,7 @@ static inline RoadBits RotateRoadBits(RoadBits r, DiagDirDiff rot)
*/
static inline bool IsStraightRoad(RoadBits r)
{
+ assert(IsValidRoadBits(r));
return (r == ROAD_X || r == ROAD_Y);
}
@@ -128,6 +143,7 @@ static inline bool IsStraightRoad(RoadBits r)
*/
static inline RoadBits DiagDirToRoadBits(DiagDirection d)
{
+ assert(IsValidDiagDirection(d));
return (RoadBits)(ROAD_NW << (3 ^ d));
}
@@ -142,6 +158,7 @@ static inline RoadBits DiagDirToRoadBits(DiagDirection d)
*/
static inline RoadBits AxisToRoadBits(Axis a)
{
+ assert(IsValidAxis(a));
return a == AXIS_X ? ROAD_X : ROAD_Y;
}
@@ -154,7 +171,7 @@ static inline RoadBits AxisToRoadBits(Axis a)
*/
static inline Money RoadMaintenanceCost(RoadType roadtype, uint32 num)
{
- assert(roadtype < ROADTYPE_END);
+ assert(IsValidRoadType(roadtype));
return (_price[PR_INFRASTRUCTURE_ROAD] * (roadtype == ROADTYPE_TRAM ? 3 : 2) * num * (1 + IntSqrt(num))) >> 9; // 2 bits fraction for the multiplier and 7 bits scaling.
}
diff --git a/src/track_func.h b/src/track_func.h
index 0cd3dcf31..689679237 100644
--- a/src/track_func.h
+++ b/src/track_func.h
@@ -14,6 +14,7 @@
#include "core/bitmath_func.hpp"
#include "track_type.h"
+#include "direction_func.h"
#include "slope_func.h"
/**
@@ -28,6 +29,42 @@
#define FOR_EACH_SET_TRACK(var, track_bits) FOR_EACH_SET_BIT_EX(Track, var, TrackBits, track_bits)
/**
+ * Checks if a Track is valid.
+ *
+ * @param track The value to check
+ * @return true if the given value is a valid track.
+ * @note Use this in an assert()
+ */
+static inline bool IsValidTrack(Track track)
+{
+ return track < TRACK_END;
+}
+
+/**
+ * Checks if a Trackdir is valid for road vehicles.
+ *
+ * @param trackdir The value to check
+ * @return true if the given value is a valid Trackdir
+ * @note Use this in an assert()
+ */
+static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
+{
+ return trackdir < TRACKDIR_END;
+}
+
+/**
+ * Checks if a Trackdir is valid for non-road vehicles.
+ *
+ * @param trackdir The value to check
+ * @return true if the given value is a valid Trackdir
+ * @note Use this in an assert()
+ */
+static inline bool IsValidTrackdir(Trackdir trackdir)
+{
+ return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
+}
+
+/**
* Convert an Axis to the corresponding Track
* AXIS_X -> TRACK_X
* AXIS_Y -> TRACK_Y
@@ -38,6 +75,7 @@
*/
static inline Track AxisToTrack(Axis a)
{
+ assert(IsValidAxis(a));
return (Track)a;
}
@@ -48,6 +86,7 @@ static inline Track AxisToTrack(Axis a)
*/
static inline TrackBits TrackToTrackBits(Track track)
{
+ assert(IsValidTrack(track));
return (TrackBits)(1 << track);
}
@@ -74,8 +113,6 @@ static inline TrackBits CornerToTrackBits(Corner corner)
return _corner_to_trackbits[corner];
}
-
-
/**
* Maps a Trackdir to the corresponding TrackdirBits value
* @param trackdir the track direction to convert
@@ -83,6 +120,7 @@ static inline TrackBits CornerToTrackBits(Corner corner)
*/
static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
{
+ assert(IsValidTrackdir(trackdir));
return (TrackdirBits)(1 << trackdir);
}
@@ -103,6 +141,7 @@ static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
static inline Track RemoveFirstTrack(TrackBits *tracks)
{
if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
+ assert((*tracks & ~TRACK_BIT_MASK) == TRACK_BIT_NONE);
Track first = (Track)FIND_FIRST_BIT(*tracks);
ClrBit(*tracks, first);
return first;
@@ -127,6 +166,7 @@ static inline Track RemoveFirstTrack(TrackBits *tracks)
static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
{
if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
+ assert((*trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs);
ClrBit(*trackdirs, first);
return first;
@@ -184,31 +224,6 @@ static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
}
-/**
- * Checks if a Track is valid.
- *
- * @param track The value to check
- * @return true if the given value is a valid track.
- * @note Use this in an assert()
- */
-static inline bool IsValidTrack(Track track)
-{
- return track < TRACK_END;
-}
-
-/**
- * Checks if a Trackdir is valid.
- *
- * @param trackdir The value to check
- * @return true if the given value is a valid Trackdir
- * @note Use this in an assert()
- */
-static inline bool IsValidTrackdir(Trackdir trackdir)
-{
- return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0;
-}
-
-
/*
* Functions describing logical relations between Tracks, TrackBits, Trackdirs
* TrackdirBits, Direction and DiagDirections.
@@ -225,7 +240,7 @@ static inline bool IsValidTrackdir(Trackdir trackdir)
*/
static inline Track TrackToOppositeTrack(Track t)
{
- assert(t != INVALID_TRACK);
+ assert(IsValidTrack(t));
return (Track)(t ^ 1);
}
@@ -241,7 +256,7 @@ static inline Track TrackToOppositeTrack(Track t)
*/
static inline Trackdir ReverseTrackdir(Trackdir trackdir)
{
- assert(trackdir != INVALID_TRACKDIR);
+ assert(IsValidTrackdirForRoadVehicle(trackdir));
return (Trackdir)(trackdir ^ 8);
}
@@ -256,6 +271,7 @@ static inline Trackdir ReverseTrackdir(Trackdir trackdir)
*/
static inline Track TrackdirToTrack(Trackdir trackdir)
{
+ assert(IsValidTrackdir(trackdir));
return (Track)(trackdir & 0x7);
}
@@ -272,6 +288,7 @@ static inline Track TrackdirToTrack(Trackdir trackdir)
*/
static inline Trackdir TrackToTrackdir(Track track)
{
+ assert(IsValidTrack(track));
return (Trackdir)track;
}
@@ -373,6 +390,7 @@ static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, Trackdir
*/
static inline Trackdir NextTrackdir(Trackdir trackdir)
{
+ assert(IsValidTrackdir(trackdir));
extern const Trackdir _next_trackdir[TRACKDIR_END];
return _next_trackdir[trackdir];
}
@@ -389,6 +407,7 @@ static inline Trackdir NextTrackdir(Trackdir trackdir)
*/
static inline TrackBits TrackCrossesTracks(Track track)
{
+ assert(IsValidTrack(track));
extern const TrackBits _track_crosses_tracks[TRACK_END];
return _track_crosses_tracks[track];
}
@@ -407,6 +426,7 @@ static inline TrackBits TrackCrossesTracks(Track track)
*/
static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
{
+ assert(IsValidTrackdirForRoadVehicle(trackdir));
extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
return _trackdir_to_exitdir[trackdir];
}
@@ -428,6 +448,8 @@ static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
*/
static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
{
+ assert(IsValidTrack(track));
+ assert(IsValidDiagDirection(diagdir));
extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
return _track_exitdir_to_trackdir[track][diagdir];
}
@@ -451,6 +473,8 @@ static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir
*/
static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
{
+ assert(IsValidTrack(track));
+ assert(IsValidDiagDirection(diagdir));
extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
return _track_enterdir_to_trackdir[track][diagdir];
}
@@ -461,6 +485,8 @@ static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdi
*/
static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
{
+ assert(IsValidTrack(track));
+ assert(IsValidDirection(dir));
extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
return _track_direction_to_trackdir[track][dir];
}
@@ -473,6 +499,7 @@ static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
*/
static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
{
+ assert(IsValidDiagDirection(diagdir));
return (Track)(diagdir & 1);
}
@@ -484,6 +511,7 @@ static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
*/
static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
{
+ assert(IsValidDiagDirection(diagdir));
return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
}
@@ -496,6 +524,7 @@ static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
*/
static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
{
+ assert(IsValidDiagDirection(diagdir));
extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
return _dir_to_diag_trackdir[diagdir];
}
@@ -513,6 +542,7 @@ static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
*/
static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
{
+ assert(IsValidDiagDirection(diagdir));
extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
return _exitdir_reaches_trackdirs[diagdir];
}
@@ -541,6 +571,7 @@ static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return Tra
*/
static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
{
+ assert(IsValidTrackdir(trackdir));
extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
}
@@ -562,6 +593,7 @@ static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
*/
static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
{
+ assert(IsValidTrackdirForRoadVehicle(trackdir));
extern const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END];
return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
}
@@ -574,6 +606,7 @@ static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
*/
static inline bool IsDiagonalTrack(Track track)
{
+ assert(IsValidTrack(track));
return (track == TRACK_X) || (track == TRACK_Y);
}
@@ -585,6 +618,7 @@ static inline bool IsDiagonalTrack(Track track)
*/
static inline bool IsDiagonalTrackdir(Trackdir trackdir)
{
+ assert(IsValidTrackdir(trackdir));
return IsDiagonalTrack(TrackdirToTrack(trackdir));
}
@@ -626,6 +660,7 @@ static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
*/
static inline bool IsReversingRoadTrackdir(Trackdir dir)
{
+ assert(IsValidTrackdirForRoadVehicle(dir));
return (dir & 0x07) >= 6;
}
@@ -636,6 +671,7 @@ static inline bool IsReversingRoadTrackdir(Trackdir dir)
*/
static inline bool IsStraightRoadTrackdir(Trackdir dir)
{
+ assert(IsValidTrackdirForRoadVehicle(dir));
return (dir & 0x06) == 0;
}
@@ -651,6 +687,7 @@ static inline bool IsStraightRoadTrackdir(Trackdir dir)
*/
static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
{
+ assert(IsValidTrackdirForRoadVehicle(dir));
extern const TrackdirBits _uphill_trackdirs[];
return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
}