OpenTTD
track_func.h
Go to the documentation of this file.
1 /* $Id: track_func.h 27808 2017-03-19 22:30:47Z peter1138 $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifndef TRACK_FUNC_H
13 #define TRACK_FUNC_H
14 
15 #include "core/bitmath_func.hpp"
16 #include "track_type.h"
17 #include "direction_func.h"
18 #include "slope_func.h"
19 
29 #define FOR_EACH_SET_TRACK(var, track_bits) FOR_EACH_SET_BIT_EX(Track, var, TrackBits, track_bits)
30 
38 static inline bool IsValidTrack(Track track)
39 {
40  return track < TRACK_END;
41 }
42 
50 static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
51 {
52  return trackdir < TRACKDIR_END;
53 }
54 
62 static inline bool IsValidTrackdir(Trackdir trackdir)
63 {
64  return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
65 }
66 
76 static inline Track AxisToTrack(Axis a)
77 {
78  assert(IsValidAxis(a));
79  return (Track)a;
80 }
81 
87 static inline TrackBits TrackToTrackBits(Track track)
88 {
89  assert(IsValidTrack(track));
90  return (TrackBits)(1 << track);
91 }
92 
98 static inline TrackBits AxisToTrackBits(Axis a)
99 {
100  return TrackToTrackBits(AxisToTrack(a));
101 }
102 
109 static inline TrackBits CornerToTrackBits(Corner corner)
110 {
111  extern const TrackBits _corner_to_trackbits[];
112  assert(IsValidCorner(corner));
113  return _corner_to_trackbits[corner];
114 }
115 
122 {
123  assert(IsValidTrackdir(trackdir));
124  return (TrackdirBits)(1 << trackdir);
125 }
126 
141 static inline Track RemoveFirstTrack(TrackBits *tracks)
142 {
143  if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
144  assert((*tracks & ~TRACK_BIT_MASK) == TRACK_BIT_NONE);
145  Track first = (Track)FIND_FIRST_BIT(*tracks);
146  ClrBit(*tracks, first);
147  return first;
148  }
149  return INVALID_TRACK;
150 }
151 
166 static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
167 {
168  if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
169  assert((*trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
170  Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs);
171  ClrBit(*trackdirs, first);
172  return first;
173  }
174  return INVALID_TRACKDIR;
175 }
176 
187 static inline Track FindFirstTrack(TrackBits tracks)
188 {
189  return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK;
190 }
191 
203 static inline Track TrackBitsToTrack(TrackBits tracks)
204 {
205  assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE));
206  return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks & TRACK_BIT_MASK) : INVALID_TRACK;
207 }
208 
221 static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
222 {
223  assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
224  return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
225 }
226 
227 /*
228  * Functions describing logical relations between Tracks, TrackBits, Trackdirs
229  * TrackdirBits, Direction and DiagDirections.
230  */
231 
242 {
243  assert(IsValidTrack(t));
244  return (Track)(t ^ 1);
245 }
246 
257 static inline Trackdir ReverseTrackdir(Trackdir trackdir)
258 {
259  assert(IsValidTrackdirForRoadVehicle(trackdir));
260  return (Trackdir)(trackdir ^ 8);
261 }
262 
272 static inline Track TrackdirToTrack(Trackdir trackdir)
273 {
274  assert(IsValidTrackdir(trackdir));
275  return (Track)(trackdir & 0x7);
276 }
277 
289 static inline Trackdir TrackToTrackdir(Track track)
290 {
291  assert(IsValidTrack(track));
292  return (Trackdir)track;
293 }
294 
305 {
306  Trackdir td = TrackToTrackdir(track);
308 }
309 
319 {
320  return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
321 }
322 
330 {
331  return (TrackdirBits)(bits * 0x101);
332 }
333 
340 static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
341 {
342  return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
343 }
344 
351 static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
352 {
354 }
355 
364 static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
365 {
366  return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
367 }
368 
376 static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
377 {
378  return (TrackStatus)(trackdirbits | (red_signals << 16));
379 }
380 
391 static inline Trackdir NextTrackdir(Trackdir trackdir)
392 {
393  assert(IsValidTrackdir(trackdir));
394  extern const Trackdir _next_trackdir[TRACKDIR_END];
395  return _next_trackdir[trackdir];
396 }
397 
408 static inline TrackBits TrackCrossesTracks(Track track)
409 {
410  assert(IsValidTrack(track));
411  extern const TrackBits _track_crosses_tracks[TRACK_END];
412  return _track_crosses_tracks[track];
413 }
414 
427 static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
428 {
429  assert(IsValidTrackdirForRoadVehicle(trackdir));
430  extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
431  return _trackdir_to_exitdir[trackdir];
432 }
433 
449 static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
450 {
451  assert(IsValidTrack(track));
452  assert(IsValidDiagDirection(diagdir));
453  extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
454  return _track_exitdir_to_trackdir[track][diagdir];
455 }
456 
475 {
476  assert(IsValidTrack(track));
477  assert(IsValidDiagDirection(diagdir));
478  extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
479  return _track_enterdir_to_trackdir[track][diagdir];
480 }
481 
487 {
488  assert(IsValidTrack(track));
489  assert(IsValidDirection(dir));
490  extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
491  return _track_direction_to_trackdir[track][dir];
492 }
493 
500 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
501 {
502  assert(IsValidDiagDirection(diagdir));
503  return (Track)(diagdir & 1);
504 }
505 
513 {
514  assert(IsValidDiagDirection(diagdir));
515  return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
516 }
517 
526 {
527  assert(IsValidDiagDirection(diagdir));
528  extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
529  return _dir_to_diag_trackdir[diagdir];
530 }
531 
544 {
545  assert(IsValidDiagDirection(diagdir));
546  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
547  return _exitdir_reaches_trackdirs[diagdir];
548 }
549 
562 
573 {
574  assert(IsValidTrackdir(trackdir));
575  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
576  return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
577 }
578 /* Note that there is no direct table for this function (there used to be),
579  * but it uses two simpler tables to achieve the result */
580 
595 {
596  assert(IsValidTrackdirForRoadVehicle(trackdir));
597  extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
598  return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
599 }
600 
607 static inline bool IsDiagonalTrack(Track track)
608 {
609  assert(IsValidTrack(track));
610  return (track == TRACK_X) || (track == TRACK_Y);
611 }
612 
619 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
620 {
621  assert(IsValidTrackdir(trackdir));
622  return IsDiagonalTrack(TrackdirToTrack(trackdir));
623 }
624 
625 
633 static inline bool TracksOverlap(TrackBits bits)
634 {
635  /* With no, or only one track, there is no overlap */
636  if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
637  /* We know that there are at least two tracks present. When there are more
638  * than 2 tracks, they will surely overlap. When there are two, they will
639  * always overlap unless they are lower & upper or right & left. */
640  return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
641 }
642 
650 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
651 {
652  if (HasBit(tracks, track)) return true;
653  return TracksOverlap(tracks | TrackToTrackBits(track));
654 }
655 
661 static inline bool IsReversingRoadTrackdir(Trackdir dir)
662 {
663  assert(IsValidTrackdirForRoadVehicle(dir));
664  return (dir & 0x07) >= 6;
665 }
666 
672 static inline bool IsStraightRoadTrackdir(Trackdir dir)
673 {
674  assert(IsValidTrackdirForRoadVehicle(dir));
675  return (dir & 0x06) == 0;
676 }
677 
688 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
689 {
690  assert(IsValidTrackdirForRoadVehicle(dir));
691  extern const TrackdirBits _uphill_trackdirs[];
692  return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
693 }
694 
695 #endif /* TRACK_FUNC_H */
All types related to tracks.
Corner
Enumeration of tile corners.
Definition: slope_type.h:24
static bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
Bitmask for the first 6 bits.
Definition: track_type.h:57
static bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition: track_func.h:661
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:272
Used for iterations.
Definition: track_type.h:92
Flag for an invalid trackdirbit value.
Definition: track_type.h:122
Track
These are used to specify a single track.
Definition: track_type.h:21
static TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:98
static TrackdirBits TrackToTrackdirBits(Track track)
Returns a TrackdirBit mask from a given Track.
Definition: track_func.h:304
static bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:26
static TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir. ...
Definition: track_func.h:512
static Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
Maps a track and an (4-way) dir to the trackdir that represents the track with the entry in the given...
Definition: track_func.h:474
static Trackdir NextTrackdir(Trackdir trackdir)
Maps a trackdir to the trackdir that you will end up on if you go straight ahead. ...
Definition: track_func.h:391
Functions related to bit mathematics.
static Track TrackToOppositeTrack(Track t)
Find the opposite track to a given track.
Definition: track_func.h:241
static Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:500
static Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track AXIS_X -> TRACK_X AXIS_Y -> TRACK_Y Uses the fact that the...
Definition: track_func.h:76
static TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction...
Definition: track_func.h:543
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:427
Direction
Defines the 8 directions on the map.
static TrackBits CornerToTrackBits(Corner corner)
Returns a single horizontal/vertical trackbit that is in a specific tile corner.
Definition: track_func.h:109
static bool IsStraightRoadTrackdir(Trackdir dir)
Checks whether the given trackdir is a straight road.
Definition: track_func.h:672
Used for iterations.
Definition: track_type.h:29
static bool IsUphillTrackdir(Slope slope, Trackdir dir)
Checks whether a trackdir on a specific slope is going uphill.
Definition: track_func.h:688
static Track TrackBitsToTrack(TrackBits tracks)
Converts TrackBits to Track.
Definition: track_func.h:203
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
Left and right track.
Definition: track_type.h:51
static bool TrackOverlapsTracks(TrackBits tracks, Track track)
Check if a given track is contained within or overlaps some other tracks.
Definition: track_func.h:650
static bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition: track_func.h:62
Flag for an invalid trackbits value.
Definition: track_type.h:60
Track along the y-axis (north-west to south-east)
Definition: track_type.h:24
static bool IsValidTrack(Track track)
Checks if a Track is valid.
Definition: track_func.h:38
#define FIND_FIRST_BIT(x)
Returns the first non-zero bit in a 6-bit value (from right).
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:121
Functions related to slopes.
static bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
Checks if a Trackdir is valid for road vehicles.
Definition: track_func.h:50
static Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition: track_func.h:166
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
Used for iterations.
static TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
Returns the red-signal-information of a TrackStatus.
Definition: track_func.h:364
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
static Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
Maps a track and a full (8-way) direction to the trackdir that represents the track running in the gi...
Definition: track_func.h:486
static Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:525
DiagDirection
Enumeration for diagonal directions.
Bitmask for bit-operations.
Definition: track_type.h:121
static TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
Maps a trackdir to the trackdirs that can be reached from it (ie, when entering the next tile...
Definition: track_func.h:572
static TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:376
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:257
static Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:62
Used to iterate.
static TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction...
Definition: track_func.h:561
static bool IsDiagonalTrack(Track track)
Checks if a given Track is diagonal.
Definition: track_func.h:607
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:87
static T KillFirstBit(T value)
Clear the first bit in an integer.
Flag for an invalid track.
Definition: track_type.h:30
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
static Track RemoveFirstTrack(TrackBits *tracks)
Removes first Track from TrackBits and returns it.
Definition: track_func.h:141
Upper and lower track.
Definition: track_type.h:50
static Trackdir TrackToTrackdir(Track track)
Returns a Trackdir for the given Track.
Definition: track_func.h:289
static bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
static bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition: track_func.h:619
Slope
Enumeration for the slope-type.
Definition: slope_type.h:50
static uint8 FindFirstBit2x64(const int value)
Finds the position of the first non-zero bit in an integer.
static Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition: track_func.h:187
static TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition: track_func.h:351
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:106
static Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
Maps a track and an (4-way) dir to the trackdir that represents the track with the exit in the given ...
Definition: track_func.h:449
static TrackBits TrackCrossesTracks(Track track)
Maps a track to all tracks that make 90 deg turns with it.
Definition: track_func.h:408
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
static TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:329
No track build.
Definition: track_type.h:107
static TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:318
Different functions related to conversions between directions.
Flag for an invalid trackdir.
Definition: track_type.h:93
Track along the x-axis (north-east to south-west)
Definition: track_type.h:23
static TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
Maps a trackdir to all trackdirs that make 90 deg turns with it.
Definition: track_func.h:594
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:340
No track.
Definition: track_type.h:42
Axis
Allow incrementing of DiagDirDiff variables.
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:633
static Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition: track_func.h:221