OpenTTD
track_func.h
Go to the documentation of this file.
1 /* $Id$ */
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 trackdir != INVALID_TRACKDIR && ((1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE);
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 
339 static inline bool HasTrack(TrackBits tracks, Track track)
340 {
341  assert(IsValidTrack(track));
342  return HasBit(tracks, track);
343 }
344 
350 static inline bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
351 {
352  assert(IsValidTrackdir(trackdir));
353  return HasBit(trackdirs, trackdir);
354 }
355 
362 static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
363 {
364  return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
365 }
366 
373 static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
374 {
376 }
377 
386 static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
387 {
388  return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
389 }
390 
398 static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
399 {
400  return (TrackStatus)(trackdirbits | (red_signals << 16));
401 }
402 
413 static inline Trackdir NextTrackdir(Trackdir trackdir)
414 {
415  assert(IsValidTrackdir(trackdir));
416  extern const Trackdir _next_trackdir[TRACKDIR_END];
417  return _next_trackdir[trackdir];
418 }
419 
430 static inline TrackBits TrackCrossesTracks(Track track)
431 {
432  assert(IsValidTrack(track));
433  extern const TrackBits _track_crosses_tracks[TRACK_END];
434  return _track_crosses_tracks[track];
435 }
436 
449 static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
450 {
451  assert(IsValidTrackdirForRoadVehicle(trackdir));
452  extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
453  return _trackdir_to_exitdir[trackdir];
454 }
455 
471 static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
472 {
473  assert(IsValidTrack(track));
474  assert(IsValidDiagDirection(diagdir));
475  extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
476  return _track_exitdir_to_trackdir[track][diagdir];
477 }
478 
497 {
498  assert(IsValidTrack(track));
499  assert(IsValidDiagDirection(diagdir));
500  extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
501  return _track_enterdir_to_trackdir[track][diagdir];
502 }
503 
509 {
510  assert(IsValidTrack(track));
511  assert(IsValidDirection(dir));
512  extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
513  return _track_direction_to_trackdir[track][dir];
514 }
515 
522 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
523 {
524  assert(IsValidDiagDirection(diagdir));
525  return (Track)(diagdir & 1);
526 }
527 
535 {
536  assert(IsValidDiagDirection(diagdir));
537  return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
538 }
539 
548 {
549  assert(IsValidDiagDirection(diagdir));
550  extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
551  return _dir_to_diag_trackdir[diagdir];
552 }
553 
566 {
567  assert(IsValidDiagDirection(diagdir));
568  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
569  return _exitdir_reaches_trackdirs[diagdir];
570 }
571 
584 
595 {
596  assert(IsValidTrackdir(trackdir));
597  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
598  return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
599 }
600 /* Note that there is no direct table for this function (there used to be),
601  * but it uses two simpler tables to achieve the result */
602 
617 {
618  assert(IsValidTrackdirForRoadVehicle(trackdir));
619  extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
620  return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
621 }
622 
629 static inline bool IsDiagonalTrack(Track track)
630 {
631  assert(IsValidTrack(track));
632  return (track == TRACK_X) || (track == TRACK_Y);
633 }
634 
641 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
642 {
643  assert(IsValidTrackdir(trackdir));
644  return IsDiagonalTrack(TrackdirToTrack(trackdir));
645 }
646 
647 
655 static inline bool TracksOverlap(TrackBits bits)
656 {
657  /* With no, or only one track, there is no overlap */
658  if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
659  /* We know that there are at least two tracks present. When there are more
660  * than 2 tracks, they will surely overlap. When there are two, they will
661  * always overlap unless they are lower & upper or right & left. */
662  return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
663 }
664 
672 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
673 {
674  if (HasBit(tracks, track)) return true;
675  return TracksOverlap(tracks | TrackToTrackBits(track));
676 }
677 
683 static inline bool IsReversingRoadTrackdir(Trackdir dir)
684 {
685  assert(IsValidTrackdirForRoadVehicle(dir));
686  return (dir & 0x07) >= 6;
687 }
688 
694 static inline bool IsStraightRoadTrackdir(Trackdir dir)
695 {
696  assert(IsValidTrackdirForRoadVehicle(dir));
697  return (dir & 0x06) == 0;
698 }
699 
710 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
711 {
712  assert(IsValidTrackdirForRoadVehicle(dir));
713  extern const TrackdirBits _uphill_trackdirs[];
714  return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
715 }
716 
724 static inline DiagDirection VehicleExitDir(Direction direction, TrackBits track)
725 {
727 
728  DiagDirection diagdir = DirToDiagDir(direction);
729 
730  /* Determine the diagonal direction in which we will exit this tile */
731  if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
732  diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
733  }
734 
735  return diagdir;
736 }
737 
738 #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.
static DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
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:683
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:534
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:496
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:413
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:522
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:565
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:449
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:694
Used for iterations.
Definition: track_type.h:29
static bool HasTrack(TrackBits tracks, Track track)
Checks whether a TrackBits has a given Track.
Definition: track_func.h:339
Right track.
Definition: track_type.h:48
static bool IsUphillTrackdir(Slope slope, Trackdir dir)
Checks whether a trackdir on a specific slope is going uphill.
Definition: track_func.h:710
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:672
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
static bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition: track_func.h:350
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:386
static bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
static DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a 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:508
static Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:547
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:594
static TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:398
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:583
static bool IsDiagonalTrack(Track track)
Checks if a given Track is diagonal.
Definition: track_func.h:629
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
Lower track.
Definition: track_type.h:46
Upper track.
Definition: track_type.h:45
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:641
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:373
static DiagDirection VehicleExitDir(Direction direction, TrackBits track)
Determine the side in which the vehicle will leave the tile.
Definition: track_func.h:724
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:471
static TrackBits TrackCrossesTracks(Track track)
Maps a track to all tracks that make 90 deg turns with it.
Definition: track_func.h:430
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
Left track.
Definition: track_type.h:47
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:616
90 degrees left
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:362
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:655
static Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition: track_func.h:221