OpenTTD: src/track_func.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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
121 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
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
241 static inline Track TrackToOppositeTrack(Track t)
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
304 static inline TrackdirBits TrackToTrackdirBits(Track track)
305 {
306 Trackdir td = TrackToTrackdir(track);
307 return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td)));
308 }
309
318 static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
319 {
320 return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
321 }
322
329 static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
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 {
353 return TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(ts));
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
474 static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
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
486 static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
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
512 static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
513 {
514 assert(IsValidDiagDirection(diagdir));
515 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
516 }
517
525 static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
526 {
527 assert(IsValidDiagDirection(diagdir));
528 extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
529 return _dir_to_diag_trackdir[diagdir];
530 }
531
543 static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
544 {
545 assert(IsValidDiagDirection(diagdir));
546 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
547 return _exitdir_reaches_trackdirs[diagdir];
548 }
549
561 static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
562
572 static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
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
594 static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
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 */