OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
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
18using SetTrackBitIterator = SetBitIterator<Track, TrackBits>;
19using SetTrackdirBitIterator = SetBitIterator<Trackdir, TrackdirBits>;
20
28inline bool IsValidTrack(Track track)
29{
30 return track < TRACK_END;
31}
32
41{
42 return trackdir < TRACKDIR_END;
43}
44
52inline 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
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) {
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) ? (Track)FindFirstBit(tracks) : INVALID_TRACK;
180}
181
194{
195 assert(tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE);
196 return static_cast<Track>(FindFirstBit(tracks & TRACK_BIT_MASK));
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
299
309{
310 return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
311}
312
320{
321 return (TrackdirBits)(bits * 0x101);
322}
323
330inline bool HasTrack(TrackBits tracks, Track track)
331{
332 assert(IsValidTrack(track));
333 return HasBit(tracks, track);
334}
335
342inline bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
343{
344 assert(IsValidTrackdir(trackdir));
345 return HasBit(trackdirs, trackdir);
346}
347
355{
356 return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
357}
358
369
379{
380 return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
381}
382
390inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
391{
392 return (TrackStatus)(trackdirbits | (red_signals << 16));
393}
394
406{
407 assert(IsValidTrackdir(trackdir));
408 extern const Trackdir _next_trackdir[TRACKDIR_END];
409 return _next_trackdir[trackdir];
410}
411
423{
424 assert(IsValidTrack(track));
426 return _track_crosses_tracks[track];
427}
428
442{
443 assert(IsValidTrackdirForRoadVehicle(trackdir));
445 return _trackdir_to_exitdir[trackdir];
446}
447
464{
465 assert(IsValidTrack(track));
466 assert(IsValidDiagDirection(diagdir));
467 extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
468 return _track_exitdir_to_trackdir[track][diagdir];
469}
470
489{
490 assert(IsValidTrack(track));
491 assert(IsValidDiagDirection(diagdir));
492 extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
493 return _track_enterdir_to_trackdir[track][diagdir];
494}
495
504{
505 assert(IsValidTrack(track));
506 assert(IsValidDirection(dir));
507 extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
508 return _track_direction_to_trackdir[track][dir];
509}
510
518{
519 assert(IsValidDiagDirection(diagdir));
520 return (Track)(diagdir & 1);
521}
522
530{
531 assert(IsValidDiagDirection(diagdir));
532 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
533}
534
543{
544 assert(IsValidDiagDirection(diagdir));
545 extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
546 return _dir_to_diag_trackdir[diagdir];
547}
548
566
579
595/* Note that there is no direct table for this function (there used to be),
596 * but it uses two simpler tables to achieve the result */
597
612{
613 assert(IsValidTrackdir(trackdir));
616}
617
624inline bool IsDiagonalTrack(Track track)
625{
626 assert(IsValidTrack(track));
627 return (track == TRACK_X) || (track == TRACK_Y);
628}
629
636inline bool IsDiagonalTrackdir(Trackdir trackdir)
637{
638 assert(IsValidTrackdir(trackdir));
639 return IsDiagonalTrack(TrackdirToTrack(trackdir));
640}
641
642
650inline bool TracksOverlap(TrackBits bits)
651{
652 /* With no, or only one track, there is no overlap */
653 if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
654 /* We know that there are at least two tracks present. When there are more
655 * than 2 tracks, they will surely overlap. When there are two, they will
656 * always overlap unless they are lower & upper or right & left. */
657 return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
658}
659
667inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
668{
669 if (HasBit(tracks, track)) return true;
670 return TracksOverlap(tracks | TrackToTrackBits(track));
671}
672
679{
681 return (dir & 0x07) >= 6;
682}
683
690{
692 return (dir & 0x06) == 0;
693}
694
705inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
706{
708 extern const TrackdirBits _uphill_trackdirs[];
709 return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
710}
711
720{
722
723 DiagDirection diagdir = DirToDiagDir(direction);
724
725 /* Determine the diagonal direction in which we will exit this tile */
726 if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
727 diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
728 }
729
730 return diagdir;
731}
732
733#endif /* TRACK_FUNC_H */
Functions related to bit mathematics.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
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.
@ DIAGDIRDIFF_90LEFT
90 degrees left
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_END
Used for iterations.
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
Corner
Enumeration of tile corners.
Definition slope_type.h:21
Slope
Enumeration for the slope-type.
Definition slope_type.h:47
Iterable ensemble of each set bit in a value.
const TrackBits _track_crosses_tracks[]
Maps a track to all tracks that make 90 deg turns with it.
const DiagDirection _trackdir_to_exitdir[TRACKDIR_END]
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
const TrackdirBits _track_crosses_trackdirs[TRACK_END]
Maps a trackdir to all trackdirs that make 90 deg turns with it.
const TrackdirBits _exitdir_reaches_trackdirs[]
Maps a diagonal direction to the all trackdirs that are connected to any track entering in this direc...
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:330
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:667
bool IsUphillTrackdir(Slope slope, Trackdir dir)
Checks whether a trackdir on a specific slope is going uphill.
Definition track_func.h:705
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:624
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:719
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition track_func.h:354
bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition track_func.h:678
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:422
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:405
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:390
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:650
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:503
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:611
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:589
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:560
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:488
bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition track_func.h:636
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition track_func.h:342
TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:578
bool IsStraightRoadTrackdir(Trackdir dir)
Checks whether the given trackdir is a straight road.
Definition track_func.h:689
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir.
Definition track_func.h:529
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:463
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition track_func.h:542
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:365
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:378
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition track_func.h:441
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:517
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
@ 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:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
@ TRACKDIR_END
Used for iterations.
Definition track_type.h:84
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
@ TRACKDIR_BIT_MASK
Bitmask for bit-operations.
Definition track_type.h:112
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:113
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