OpenTTD Source 20260531-master-g0e951f3528
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 static_cast<Track>(to_underlying(a));
70}
71
78{
79 assert(IsValidTrack(track));
80 return (TrackBits)(1 << track);
81}
82
92
100{
102 assert(IsValidCorner(corner));
103 return _corner_to_trackbits[corner];
104}
105
112{
113 assert(IsValidTrackdir(trackdir));
114 return static_cast<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 static_cast<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
359{
360 assert(IsValidTrackdir(trackdir));
362 return _next_trackdir[trackdir];
363}
364
376{
377 assert(IsValidTrack(track));
379 return _track_crosses_tracks[track];
380}
381
395{
396 assert(IsValidTrackdirForRoadVehicle(trackdir));
398 return _trackdir_to_exitdir[trackdir];
399}
400
417{
418 assert(IsValidTrack(track));
419 assert(IsValidDiagDirection(diagdir));
421 return _track_exitdir_to_trackdir[track][diagdir];
422}
423
442{
443 assert(IsValidTrack(track));
444 assert(IsValidDiagDirection(diagdir));
446 return _track_enterdir_to_trackdir[track][diagdir];
447}
448
463
471{
472 assert(IsValidDiagDirection(diagdir));
473 return static_cast<Track>(to_underlying(diagdir) & 1);
474}
475
483{
484 assert(IsValidDiagDirection(diagdir));
485 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
486}
487
496{
497 assert(IsValidDiagDirection(diagdir));
499 return _dir_to_diag_trackdir[diagdir];
500}
501
519
532
548/* Note that there is no direct table for this function (there used to be),
549 * but it uses two simpler tables to achieve the result */
550
570
577inline bool IsDiagonalTrack(Track track)
578{
579 assert(IsValidTrack(track));
580 return (track == TRACK_X) || (track == TRACK_Y);
581}
582
589inline bool IsDiagonalTrackdir(Trackdir trackdir)
590{
591 assert(IsValidTrackdir(trackdir));
592 return IsDiagonalTrack(TrackdirToTrack(trackdir));
593}
594
595
603inline bool TracksOverlap(TrackBits bits)
604{
605 /* With no, or only one track, there is no overlap */
606 if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
607 /* We know that there are at least two tracks present. When there are more
608 * than 2 tracks, they will surely overlap. When there are two, they will
609 * always overlap unless they are lower & upper or right & left. */
610 return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
611}
612
620inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
621{
622 if (HasBit(tracks, track)) return true;
623 return TracksOverlap(tracks | TrackToTrackBits(track));
624}
625
632{
634 return (dir & 0x07) >= 6;
635}
636
643{
645 return (dir & 0x06) == 0;
646}
647
658inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
659{
661 extern const TrackdirBits _uphill_trackdirs[];
662 return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
663}
664
673{
675
676 DiagDirection diagdir = DirToDiagDir(direction);
677
678 /* Determine the diagonal direction in which we will exit this tile */
679 if (!IsDiagonalDirection(direction) && track != state_dir_table[diagdir]) {
680 diagdir = ChangeDiagDir(diagdir, DiagDirDiff::Left90);
681 }
682
683 return diagdir;
684}
685
686#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.
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
EnumIndexArray< T, DiagDirection, DiagDirection::End > DiagDirectionIndexArray
Array with DiagDirection as index.
Direction
Defines the 8 directions on the map.
@ Left90
90 degrees left
Axis
Enumeration for the two axis X and Y.
DiagDirection
Enumeration for diagonal directions.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
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:53
EnumIndexArray< T, Corner, CORNER_END > CornerIndexArray
Array with Corner as index.
Definition slope_type.h:35
Iterable ensemble of each set bit in a value.
const TrackIndexArray< DiagDirectionIndexArray< Trackdir > > _track_enterdir_to_trackdir
Maps a track and an (4-way) dir to the trackdir that represents the track with the entry in the given...
const TrackIndexArray< TrackdirBits > _track_crosses_trackdirs
Maps a trackdir to all trackdirs that make 90 deg turns with it.
const DiagDirectionIndexArray< TrackdirBits > _exitdir_reaches_trackdirs
Maps a diagonal direction to the all trackdirs that are connected to any track entering in this direc...
const CornerIndexArray< TrackBits > _corner_to_trackbits
Maps a single horizontal/vertical trackbit that is in a specific tile corner.
const TrackdirIndexArray< DiagDirection > _trackdir_to_exitdir
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
const TrackdirIndexArray< Trackdir > _next_trackdir
Next trackdir to use when moving to a new tile for each current trackdir.
const DiagDirectionIndexArray< Trackdir > _dir_to_diag_trackdir
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
const TrackIndexArray< DirectionIndexArray< Trackdir > > _track_direction_to_trackdir
Maps a track and a full (8-way) direction to the trackdir that represents the track running in the gi...
const TrackIndexArray< TrackBits > _track_crosses_tracks
Maps a track to all tracks that make 90 deg turns with it.
const TrackIndexArray< DiagDirectionIndexArray< Trackdir > > _track_exitdir_to_trackdir
Maps a track and an (4-way) dir to the trackdir that represents the track with the exit in the given ...
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:620
bool IsUphillTrackdir(Slope slope, Trackdir dir)
Checks whether a trackdir on a specific slope is going uphill.
Definition track_func.h:658
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:577
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:672
bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition track_func.h:631
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:375
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:358
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition track_func.h:247
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:603
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:456
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:564
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:542
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:513
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:441
bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition track_func.h:589
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:531
bool IsStraightRoadTrackdir(Trackdir dir)
Checks whether the given trackdir is a straight road.
Definition track_func.h:642
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a DiagDirection to the associated diagonal TrackBits.
Definition track_func.h:482
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:416
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition track_func.h:495
TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition track_func.h:111
Track AxisToTrack(Axis a)
Convert an Axis to the corresponding Track Axis::X -> TRACK_X Axis::Y -> TRACK_Y Uses the fact that t...
Definition track_func.h:66
Track RemoveFirstTrack(TrackBits *tracks)
Removes first Track from TrackBits and returns it.
Definition track_func.h:131
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition track_func.h:394
TrackdirBits TrackToTrackdirBits(Track track)
Returns a TrackdirBit mask from a given Track.
Definition track_func.h:294
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a DiagDirection to the associated diagonal Track.
Definition track_func.h:470
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition track_func.h:308
All types related to tracks.
TrackBits
Bitfield corresponding to Track.
Definition track_type.h:42
@ TRACK_BIT_UPPER
Upper track.
Definition track_type.h:46
@ TRACK_BIT_LEFT
Left track.
Definition track_type.h:48
@ TRACK_BIT_HORZ
Upper and lower track.
Definition track_type.h:51
@ TRACK_BIT_NONE
No track.
Definition track_type.h:43
@ TRACK_BIT_MASK
Bitmask for the first 6 bits.
Definition track_type.h:58
@ TRACK_BIT_LOWER
Lower track.
Definition track_type.h:47
@ TRACK_BIT_RIGHT
Right track.
Definition track_type.h:49
@ TRACK_BIT_VERT
Left and right track.
Definition track_type.h:52
EnumIndexArray< T, Trackdir, TRACKDIR_END > TrackdirIndexArray
Allow incrementing of Trackdir variables.
Definition track_type.h:103
EnumIndexArray< T, Track, TRACK_END > TrackIndexArray
Allow incrementing of Track variables.
Definition track_type.h:39
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:73
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:92
@ TRACKDIR_END
Used for iterations.
Definition track_type.h:91
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition track_type.h:111
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:112
@ TRACKDIR_BIT_MASK
Bitmask for bit-operations.
Definition track_type.h:126
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition track_type.h:127
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