OpenTTD Source  20241108-master-g80f628063a
track_func.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef TRACK_FUNC_H
11 #define TRACK_FUNC_H
12 
13 #include "core/bitmath_func.hpp"
14 #include "track_type.h"
15 #include "direction_func.h"
16 #include "slope_func.h"
17 
20 
28 inline bool IsValidTrack(Track track)
29 {
30  return track < TRACK_END;
31 }
32 
41 {
42  return trackdir < TRACKDIR_END;
43 }
44 
52 inline bool IsValidTrackdir(Trackdir trackdir)
53 {
54  return trackdir != INVALID_TRACKDIR && ((1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE);
55 }
56 
67 {
68  assert(IsValidAxis(a));
69  return (Track)a;
70 }
71 
78 {
79  assert(IsValidTrack(track));
80  return (TrackBits)(1 << track);
81 }
82 
89 {
90  return TrackToTrackBits(AxisToTrack(a));
91 }
92 
100 {
101  extern const TrackBits _corner_to_trackbits[];
102  assert(IsValidCorner(corner));
103  return _corner_to_trackbits[corner];
104 }
105 
112 {
113  assert(IsValidTrackdir(trackdir));
114  return (TrackdirBits)(1 << trackdir);
115 }
116 
132 {
133  if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
134  assert((*tracks & ~TRACK_BIT_MASK) == TRACK_BIT_NONE);
135  Track first = (Track)FindFirstBit(*tracks);
136  ClrBit(*tracks, first);
137  return first;
138  }
139  return INVALID_TRACK;
140 }
141 
157 {
158  if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
159  assert((*trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
160  Trackdir first = (Trackdir)FindFirstBit(*trackdirs);
161  ClrBit(*trackdirs, first);
162  return first;
163  }
164  return INVALID_TRACKDIR;
165 }
166 
178 {
179  return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FindFirstBit(tracks) : INVALID_TRACK;
180 }
181 
194 {
195  assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE));
196  return tracks != INVALID_TRACK_BIT ? (Track)FindFirstBit(tracks & TRACK_BIT_MASK) : INVALID_TRACK;
197 }
198 
212 {
213  assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
214  return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit(trackdirs) : INVALID_TRACKDIR;
215 }
216 
217 /*
218  * Functions describing logical relations between Tracks, TrackBits, Trackdirs
219  * TrackdirBits, Direction and DiagDirections.
220  */
221 
232 {
233  assert(IsValidTrack(t));
234  return (Track)(t ^ 1);
235 }
236 
248 {
249  assert(IsValidTrackdirForRoadVehicle(trackdir));
250  return (Trackdir)(trackdir ^ 8);
251 }
252 
263 {
264  assert(IsValidTrackdir(trackdir));
265  return (Track)(trackdir & 0x7);
266 }
267 
280 {
281  assert(IsValidTrack(track));
282  return (Trackdir)track;
283 }
284 
295 {
296  Trackdir td = TrackToTrackdir(track);
298 }
299 
309 {
310  return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
311 }
312 
320 {
321  return (TrackdirBits)(bits * 0x101);
322 }
323 
329 inline bool HasTrack(TrackBits tracks, Track track)
330 {
331  assert(IsValidTrack(track));
332  return HasBit(tracks, track);
333 }
334 
340 inline bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
341 {
342  assert(IsValidTrackdir(trackdir));
343  return HasBit(trackdirs, trackdir);
344 }
345 
353 {
354  return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
355 }
356 
363 inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
364 {
366 }
367 
376 inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
377 {
378  return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
379 }
380 
388 inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
389 {
390  return (TrackStatus)(trackdirbits | (red_signals << 16));
391 }
392 
404 {
405  assert(IsValidTrackdir(trackdir));
406  extern const Trackdir _next_trackdir[TRACKDIR_END];
407  return _next_trackdir[trackdir];
408 }
409 
421 {
422  assert(IsValidTrack(track));
423  extern const TrackBits _track_crosses_tracks[TRACK_END];
424  return _track_crosses_tracks[track];
425 }
426 
440 {
441  assert(IsValidTrackdirForRoadVehicle(trackdir));
442  extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
443  return _trackdir_to_exitdir[trackdir];
444 }
445 
462 {
463  assert(IsValidTrack(track));
464  assert(IsValidDiagDirection(diagdir));
465  extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
466  return _track_exitdir_to_trackdir[track][diagdir];
467 }
468 
487 {
488  assert(IsValidTrack(track));
489  assert(IsValidDiagDirection(diagdir));
490  extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
491  return _track_enterdir_to_trackdir[track][diagdir];
492 }
493 
499 {
500  assert(IsValidTrack(track));
501  assert(IsValidDirection(dir));
502  extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
503  return _track_direction_to_trackdir[track][dir];
504 }
505 
513 {
514  assert(IsValidDiagDirection(diagdir));
515  return (Track)(diagdir & 1);
516 }
517 
525 {
526  assert(IsValidDiagDirection(diagdir));
527  return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
528 }
529 
538 {
539  assert(IsValidDiagDirection(diagdir));
540  extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
541  return _dir_to_diag_trackdir[diagdir];
542 }
543 
556 {
557  assert(IsValidDiagDirection(diagdir));
558  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
559  return _exitdir_reaches_trackdirs[diagdir];
560 }
561 
574 
585 {
586  assert(IsValidTrackdir(trackdir));
587  extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
588  return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
589 }
590 /* Note that there is no direct table for this function (there used to be),
591  * but it uses two simpler tables to achieve the result */
592 
607 {
608  assert(IsValidTrackdirForRoadVehicle(trackdir));
609  extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
610  return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
611 }
612 
619 inline bool IsDiagonalTrack(Track track)
620 {
621  assert(IsValidTrack(track));
622  return (track == TRACK_X) || (track == TRACK_Y);
623 }
624 
631 inline bool IsDiagonalTrackdir(Trackdir trackdir)
632 {
633  assert(IsValidTrackdir(trackdir));
634  return IsDiagonalTrack(TrackdirToTrack(trackdir));
635 }
636 
637 
645 inline bool TracksOverlap(TrackBits bits)
646 {
647  /* With no, or only one track, there is no overlap */
648  if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
649  /* We know that there are at least two tracks present. When there are more
650  * than 2 tracks, they will surely overlap. When there are two, they will
651  * always overlap unless they are lower & upper or right & left. */
652  return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
653 }
654 
662 inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
663 {
664  if (HasBit(tracks, track)) return true;
665  return TracksOverlap(tracks | TrackToTrackBits(track));
666 }
667 
674 {
675  assert(IsValidTrackdirForRoadVehicle(dir));
676  return (dir & 0x07) >= 6;
677 }
678 
685 {
686  assert(IsValidTrackdirForRoadVehicle(dir));
687  return (dir & 0x06) == 0;
688 }
689 
700 inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
701 {
702  assert(IsValidTrackdirForRoadVehicle(dir));
703  extern const TrackdirBits _uphill_trackdirs[];
704  return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
705 }
706 
715 {
717 
718  DiagDirection diagdir = DirToDiagDir(direction);
719 
720  /* Determine the diagonal direction in which we will exit this tile */
721  if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
722  diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
723  }
724 
725  return diagdir;
726 }
727 
728 #endif /* TRACK_FUNC_H */
Functions related to bit mathematics.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Different functions related to conversions between directions.
bool IsValidAxis(Axis d)
Checks if an integer value is a valid Axis.
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
Applies a difference on a DiagDirection.
bool IsValidDirection(Direction d)
Checks if an integer value is a valid Direction.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
Direction
Defines the 8 directions on the map.
@ DIR_END
Used to iterate.
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_END
Used for iterations.
@ DIAGDIRDIFF_90LEFT
90 degrees left
Functions related to slopes.
static constexpr Slope RemoveHalftileSlope(Slope s)
Removes a halftile slope from a slope.
Definition: slope_func.h:60
static constexpr bool IsValidCorner(Corner corner)
Rangecheck for Corner enumeration.
Definition: slope_func.h:24
Slope
Enumeration for the slope-type.
Definition: slope_type.h:48
Corner
Enumeration of tile corners.
Definition: slope_type.h:22
Iterable ensemble of each set bit in a value.
Trackdir TrackToTrackdir(Track track)
Returns a Trackdir for the given Track.
Definition: track_func.h:279
bool HasTrack(TrackBits tracks, Track track)
Checks whether a TrackBits has a given Track.
Definition: track_func.h:329
Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition: track_func.h:156
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:262
Track TrackToOppositeTrack(Track t)
Find the opposite track to a given track.
Definition: track_func.h:231
bool TrackOverlapsTracks(TrackBits tracks, Track track)
Check if a given track is contained within or overlaps some other tracks.
Definition: track_func.h:662
bool IsUphillTrackdir(Slope slope, Trackdir dir)
Checks whether a trackdir on a specific slope is going uphill.
Definition: track_func.h:700
TrackBits CornerToTrackBits(Corner corner)
Returns a single horizontal/vertical trackbit that is in a specific tile corner.
Definition: track_func.h:99
bool IsDiagonalTrack(Track track)
Checks if a given Track is diagonal.
Definition: track_func.h:619
TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:77
TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:319
DiagDirection VehicleExitDir(Direction direction, TrackBits track)
Determine the side in which the vehicle will leave the tile.
Definition: track_func.h:714
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:352
bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition: track_func.h:673
Track TrackBitsToTrack(TrackBits tracks)
Converts TrackBits to Track.
Definition: track_func.h:193
TrackBits TrackCrossesTracks(Track track)
Maps a track to all tracks that make 90 deg turns with it.
Definition: track_func.h:420
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:403
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:247
TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
Builds a TrackStatus.
Definition: track_func.h:388
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:645
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:498
TrackBits AxisToTrackBits(Axis a)
Maps an Axis to the corresponding TrackBits value.
Definition: track_func.h:88
bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition: track_func.h:52
Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition: track_func.h:211
bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
Checks if a Trackdir is valid for road vehicles.
Definition: track_func.h:40
TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
Maps a trackdir to all trackdirs that make 90 deg turns with it.
Definition: track_func.h:606
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:584
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition: track_func.h:555
bool IsValidTrack(Track track)
Checks if a Track is valid.
Definition: track_func.h:28
Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition: track_func.h:177
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:486
bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition: track_func.h:631
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition: track_func.h:340
TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition: track_func.h:573
bool IsStraightRoadTrackdir(Trackdir dir)
Checks whether the given trackdir is a straight road.
Definition: track_func.h:684
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir.
Definition: track_func.h:524
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:461
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:537
TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:111
TrackBits TrackStatusToTrackBits(TrackStatus ts)
Returns the present-track-information of a TrackStatus.
Definition: track_func.h:363
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:66
Track RemoveFirstTrack(TrackBits *tracks)
Removes first Track from TrackBits and returns it.
Definition: track_func.h:131
TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
Returns the red-signal-information of a TrackStatus.
Definition: track_func.h:376
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:439
TrackdirBits TrackToTrackdirBits(Track track)
Returns a TrackdirBit mask from a given Track.
Definition: track_func.h:294
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition: track_func.h:512
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:308
All types related to tracks.
TrackBits
Allow incrementing of Track variables.
Definition: track_type.h:35
@ TRACK_BIT_UPPER
Upper track.
Definition: track_type.h:39
@ TRACK_BIT_LEFT
Left track.
Definition: track_type.h:41
@ INVALID_TRACK_BIT
Flag for an invalid trackbits value.
Definition: track_type.h:54
@ TRACK_BIT_HORZ
Upper and lower track.
Definition: track_type.h:44
@ TRACK_BIT_NONE
No track.
Definition: track_type.h:36
@ TRACK_BIT_MASK
Bitmask for the first 6 bits.
Definition: track_type.h:51
@ TRACK_BIT_LOWER
Lower track.
Definition: track_type.h:40
@ TRACK_BIT_RIGHT
Right track.
Definition: track_type.h:42
@ TRACK_BIT_VERT
Left and right track.
Definition: track_type.h:45
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:86
@ TRACKDIR_END
Used for iterations.
Definition: track_type.h:85
TrackdirBits
Allow incrementing of Trackdir variables.
Definition: track_type.h:98
@ TRACKDIR_BIT_NONE
No track build.
Definition: track_type.h:99
@ TRACKDIR_BIT_MASK
Bitmask for bit-operations.
Definition: track_type.h:113
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition: track_type.h:114
Track
These are used to specify a single track.
Definition: track_type.h:19
@ INVALID_TRACK
Flag for an invalid track.
Definition: track_type.h:28
@ TRACK_Y
Track along the y-axis (north-west to south-east)
Definition: track_type.h:22
@ TRACK_END
Used for iterations.
Definition: track_type.h:27
@ TRACK_X
Track along the x-axis (north-east to south-west)
Definition: track_type.h:21