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 
8 /** @file track_func.h Different conversion functions from one kind of track to another. */
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 
18 using SetTrackBitIterator = SetBitIterator<Track, TrackBits>;
19 
20 /**
21  * Checks if a Track is valid.
22  *
23  * @param track The value to check
24  * @return true if the given value is a valid track.
25  * @note Use this in an assert()
26  */
IsValidTrack(Track track)27 static inline bool IsValidTrack(Track track)
28 {
29 	return track < TRACK_END;
30 }
31 
32 /**
33  * Checks if a Trackdir is valid for road vehicles.
34  *
35  * @param trackdir The value to check
36  * @return true if the given value is a valid Trackdir
37  * @note Use this in an assert()
38  */
IsValidTrackdirForRoadVehicle(Trackdir trackdir)39 static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
40 {
41 	return trackdir < TRACKDIR_END;
42 }
43 
44 /**
45  * Checks if a Trackdir is valid for non-road vehicles.
46  *
47  * @param trackdir The value to check
48  * @return true if the given value is a valid Trackdir
49  * @note Use this in an assert()
50  */
IsValidTrackdir(Trackdir trackdir)51 static inline bool IsValidTrackdir(Trackdir trackdir)
52 {
53 	return trackdir != INVALID_TRACKDIR && ((1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE);
54 }
55 
56 /**
57  * Convert an Axis to the corresponding Track
58  * AXIS_X -> TRACK_X
59  * AXIS_Y -> TRACK_Y
60  * Uses the fact that they share the same internal encoding
61  *
62  * @param a the axis to convert
63  * @return the track corresponding to the axis
64  */
AxisToTrack(Axis a)65 static inline Track AxisToTrack(Axis a)
66 {
67 	assert(IsValidAxis(a));
68 	return (Track)a;
69 }
70 
71 /**
72  * Maps a Track to the corresponding TrackBits value
73  * @param track the track to convert
74  * @return the converted TrackBits value of the track
75  */
TrackToTrackBits(Track track)76 static inline TrackBits TrackToTrackBits(Track track)
77 {
78 	assert(IsValidTrack(track));
79 	return (TrackBits)(1 << track);
80 }
81 
82 /**
83  * Maps an Axis to the corresponding TrackBits value
84  * @param a the axis to convert
85  * @return the converted TrackBits value of the axis
86  */
AxisToTrackBits(Axis a)87 static inline TrackBits AxisToTrackBits(Axis a)
88 {
89 	return TrackToTrackBits(AxisToTrack(a));
90 }
91 
92 /**
93  * Returns a single horizontal/vertical trackbit that is in a specific tile corner.
94  *
95  * @param corner The corner of a tile.
96  * @return The TrackBits of the track in the corner.
97  */
CornerToTrackBits(Corner corner)98 static inline TrackBits CornerToTrackBits(Corner corner)
99 {
100 	extern const TrackBits _corner_to_trackbits[];
101 	assert(IsValidCorner(corner));
102 	return _corner_to_trackbits[corner];
103 }
104 
105 /**
106  * Maps a Trackdir to the corresponding TrackdirBits value
107  * @param trackdir the track direction to convert
108  * @return the converted TrackdirBits value
109  */
TrackdirToTrackdirBits(Trackdir trackdir)110 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
111 {
112 	assert(IsValidTrackdir(trackdir));
113 	return (TrackdirBits)(1 << trackdir);
114 }
115 
116 /**
117  * Removes first Track from TrackBits and returns it
118  *
119  * This function searches for the first bit in the TrackBits,
120  * remove this bit from the parameter and returns the found
121  * bit as Track value. It returns INVALID_TRACK if the
122  * parameter was TRACK_BIT_NONE or INVALID_TRACK_BIT. This
123  * is basically used in while-loops to get up to 6 possible
124  * tracks on a tile until the parameter becomes TRACK_BIT_NONE.
125  *
126  * @param tracks The value with the TrackBits
127  * @return The first Track from the TrackBits value
128  * @see FindFirstTrack
129  */
RemoveFirstTrack(TrackBits * tracks)130 static inline Track RemoveFirstTrack(TrackBits *tracks)
131 {
132 	if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
133 		assert((*tracks & ~TRACK_BIT_MASK) == TRACK_BIT_NONE);
134 		Track first = (Track)FIND_FIRST_BIT(*tracks);
135 		ClrBit(*tracks, first);
136 		return first;
137 	}
138 	return INVALID_TRACK;
139 }
140 
141 /**
142  * Removes first Trackdir from TrackdirBits and returns it
143  *
144  * This function searches for the first bit in the TrackdirBits parameter,
145  * remove this bit from the parameter and returns the fnound bit as
146  * Trackdir value. It returns INVALID_TRACKDIR if the trackdirs is
147  * TRACKDIR_BIT_NONE or INVALID_TRACKDIR_BIT. This is basically used in a
148  * while-loop to get all track-directions step by step until the value
149  * reaches TRACKDIR_BIT_NONE.
150  *
151  * @param trackdirs The value with the TrackdirBits
152  * @return The first Trackdir from the TrackdirBits value
153  * @see FindFirstTrackdir
154  */
RemoveFirstTrackdir(TrackdirBits * trackdirs)155 static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
156 {
157 	if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
158 		assert((*trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
159 		Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs);
160 		ClrBit(*trackdirs, first);
161 		return first;
162 	}
163 	return INVALID_TRACKDIR;
164 }
165 
166 /**
167  * Returns first Track from TrackBits or INVALID_TRACK
168  *
169  * This function returns the first Track found in the TrackBits value as Track-value.
170  * It returns INVALID_TRACK if the parameter is TRACK_BIT_NONE or INVALID_TRACK_BIT.
171  *
172  * @param tracks The TrackBits value
173  * @return The first Track found or INVALID_TRACK
174  * @see RemoveFirstTrack
175  */
FindFirstTrack(TrackBits tracks)176 static inline Track FindFirstTrack(TrackBits tracks)
177 {
178 	return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK;
179 }
180 
181 /**
182  * Converts TrackBits to Track.
183  *
184  * This function converts a TrackBits value to a Track value. As it
185  * is not possible to convert two or more tracks to one track the
186  * parameter must contain only one track or be the INVALID_TRACK_BIT value.
187  *
188  * @param tracks The TrackBits value to convert
189  * @return The Track from the value or INVALID_TRACK
190  * @pre tracks must contains only one Track or be INVALID_TRACK_BIT
191  */
TrackBitsToTrack(TrackBits tracks)192 static inline Track TrackBitsToTrack(TrackBits tracks)
193 {
194 	assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE));
195 	return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks & TRACK_BIT_MASK) : INVALID_TRACK;
196 }
197 
198 /**
199  * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR
200  *
201  * This function returns the first Trackdir in the given TrackdirBits value or
202  * INVALID_TRACKDIR if the value is TRACKDIR_BIT_NONE. The TrackdirBits must
203  * not be INVALID_TRACKDIR_BIT.
204  *
205  * @param trackdirs The TrackdirBits value
206  * @return The first Trackdir from the TrackdirBits or INVALID_TRACKDIR on TRACKDIR_BIT_NONE.
207  * @pre trackdirs must not be INVALID_TRACKDIR_BIT
208  * @see RemoveFirstTrackdir
209  */
FindFirstTrackdir(TrackdirBits trackdirs)210 static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
211 {
212 	assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
213 	return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
214 }
215 
216 /*
217  * Functions describing logical relations between Tracks, TrackBits, Trackdirs
218  * TrackdirBits, Direction and DiagDirections.
219  */
220 
221 /**
222  * Find the opposite track to a given track.
223  *
224  * TRACK_LOWER -> TRACK_UPPER and vice versa, likewise for left/right.
225  * TRACK_X is mapped to TRACK_Y and reversed.
226  *
227  * @param t the track to convert
228  * @return the opposite track
229  */
TrackToOppositeTrack(Track t)230 static inline Track TrackToOppositeTrack(Track t)
231 {
232 	assert(IsValidTrack(t));
233 	return (Track)(t ^ 1);
234 }
235 
236 /**
237  * Maps a trackdir to the reverse trackdir.
238  *
239  * Returns the reverse trackdir of a Trackdir value. The reverse trackdir
240  * is the same track with the other direction on it.
241  *
242  * @param trackdir The Trackdir value
243  * @return The reverse trackdir
244  * @pre trackdir must not be INVALID_TRACKDIR
245  */
ReverseTrackdir(Trackdir trackdir)246 static inline Trackdir ReverseTrackdir(Trackdir trackdir)
247 {
248 	assert(IsValidTrackdirForRoadVehicle(trackdir));
249 	return (Trackdir)(trackdir ^ 8);
250 }
251 
252 /**
253  * Returns the Track that a given Trackdir represents
254  *
255  * This function filters the Track which is used in the Trackdir value and
256  * returns it as a Track value.
257  *
258  * @param trackdir The trackdir value
259  * @return The Track which is used in the value
260  */
TrackdirToTrack(Trackdir trackdir)261 static inline Track TrackdirToTrack(Trackdir trackdir)
262 {
263 	assert(IsValidTrackdir(trackdir));
264 	return (Track)(trackdir & 0x7);
265 }
266 
267 /**
268  * Returns a Trackdir for the given Track
269  *
270  * Since every Track corresponds to two Trackdirs, we choose the
271  * one which points between NE and S. Note that the actual
272  * implementation is quite futile, but this might change
273  * in the future.
274  *
275  * @param track The given Track
276  * @return The Trackdir from the given Track
277  */
TrackToTrackdir(Track track)278 static inline Trackdir TrackToTrackdir(Track track)
279 {
280 	assert(IsValidTrack(track));
281 	return (Trackdir)track;
282 }
283 
284 /**
285  * Returns a TrackdirBit mask from a given Track
286  *
287  * The TrackdirBit mask contains the two TrackdirBits that
288  * correspond with the given Track (one for each direction).
289  *
290  * @param track The track to get the TrackdirBits from
291  * @return The TrackdirBits which the selected tracks
292  */
TrackToTrackdirBits(Track track)293 static inline TrackdirBits TrackToTrackdirBits(Track track)
294 {
295 	Trackdir td = TrackToTrackdir(track);
296 	return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td)));
297 }
298 
299 /**
300  * Discards all directional information from a TrackdirBits value
301  *
302  * Any Track which is present in either direction will be present in the result.
303  *
304  * @param bits The TrackdirBits to get the TrackBits from
305  * @return The TrackBits
306  */
TrackdirBitsToTrackBits(TrackdirBits bits)307 static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
308 {
309 	return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
310 }
311 
312 /**
313  * Converts TrackBits to TrackdirBits while allowing both directions.
314  *
315  * @param bits The TrackBits
316  * @return The TrackdirBits containing of bits in both directions.
317  */
TrackBitsToTrackdirBits(TrackBits bits)318 static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
319 {
320 	return (TrackdirBits)(bits * 0x101);
321 }
322 
323 /**
324  * Checks whether a TrackBits has a given Track.
325  * @param tracks The track bits.
326  * @param track The track to check.
327  */
HasTrack(TrackBits tracks,Track track)328 static inline bool HasTrack(TrackBits tracks, Track track)
329 {
330 	assert(IsValidTrack(track));
331 	return HasBit(tracks, track);
332 }
333 
334 /**
335  * Checks whether a TrackdirBits has a given Trackdir.
336  * @param trackdirs The trackdir bits.
337  * @param trackdir The trackdir to check.
338  */
HasTrackdir(TrackdirBits trackdirs,Trackdir trackdir)339 static inline bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
340 {
341 	assert(IsValidTrackdir(trackdir));
342 	return HasBit(trackdirs, trackdir);
343 }
344 
345 /**
346  * Returns the present-trackdir-information of a TrackStatus.
347  *
348  * @param ts The TrackStatus returned by GetTileTrackStatus()
349  * @return the present trackdirs
350  */
TrackStatusToTrackdirBits(TrackStatus ts)351 static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
352 {
353 	return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
354 }
355 
356 /**
357  * Returns the present-track-information of a TrackStatus.
358  *
359  * @param ts The TrackStatus returned by GetTileTrackStatus()
360  * @return the present tracks
361  */
TrackStatusToTrackBits(TrackStatus ts)362 static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
363 {
364 	return TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(ts));
365 }
366 
367 /**
368  * Returns the red-signal-information of a TrackStatus.
369  *
370  * Note: The result may contain red signals for non-present tracks.
371  *
372  * @param ts The TrackStatus returned by GetTileTrackStatus()
373  * @return the The trackdirs that are blocked by red-signals
374  */
TrackStatusToRedSignals(TrackStatus ts)375 static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
376 {
377 	return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
378 }
379 
380 /**
381  * Builds a TrackStatus
382  *
383  * @param trackdirbits present trackdirs
384  * @param red_signals red signals
385  * @return the TrackStatus representing the given information
386  */
CombineTrackStatus(TrackdirBits trackdirbits,TrackdirBits red_signals)387 static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
388 {
389 	return (TrackStatus)(trackdirbits | (red_signals << 16));
390 }
391 
392 /**
393  * Maps a trackdir to the trackdir that you will end up on if you go straight
394  * ahead.
395  *
396  * This will be the same trackdir for diagonal trackdirs, but a
397  * different (alternating) one for straight trackdirs
398  *
399  * @param trackdir The given trackdir
400  * @return The next Trackdir value of the next tile.
401  */
NextTrackdir(Trackdir trackdir)402 static inline Trackdir NextTrackdir(Trackdir trackdir)
403 {
404 	assert(IsValidTrackdir(trackdir));
405 	extern const Trackdir _next_trackdir[TRACKDIR_END];
406 	return _next_trackdir[trackdir];
407 }
408 
409 /**
410  * Maps a track to all tracks that make 90 deg turns with it.
411  *
412  * For the diagonal directions these are the complement of the
413  * direction, for the straight directions these are the
414  * two vertical or horizontal tracks, depend on the given direction
415  *
416  * @param track The given track
417  * @return The TrackBits with the tracks marked which cross the given track by 90 deg.
418  */
TrackCrossesTracks(Track track)419 static inline TrackBits TrackCrossesTracks(Track track)
420 {
421 	assert(IsValidTrack(track));
422 	extern const TrackBits _track_crosses_tracks[TRACK_END];
423 	return _track_crosses_tracks[track];
424 }
425 
426 /**
427  * Maps a trackdir to the (4-way) direction the tile is exited when following
428  * that trackdir.
429  *
430  * For the diagonal directions these are the same directions. For
431  * the straight directions these are the directions from the imagined
432  * base-tile to the bordering tile which will be joined if the given
433  * straight direction is leaved from the base-tile.
434  *
435  * @param trackdir The given track direction
436  * @return The direction which points to the resulting tile if following the Trackdir
437  */
TrackdirToExitdir(Trackdir trackdir)438 static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
439 {
440 	assert(IsValidTrackdirForRoadVehicle(trackdir));
441 	extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
442 	return _trackdir_to_exitdir[trackdir];
443 }
444 
445 /**
446  * Maps a track and an (4-way) dir to the trackdir that represents the track
447  * with the exit in the given direction.
448  *
449  * For the diagonal tracks the resulting track direction are clear for a given
450  * DiagDirection. It either matches the direction or it returns INVALID_TRACKDIR,
451  * as a TRACK_X cannot be applied with DIAG_SE.
452  * For the straight tracks the resulting track direction will be the
453  * direction which the DiagDirection is pointing. But this will be INVALID_TRACKDIR
454  * if the DiagDirection is pointing 'away' the track.
455  *
456  * @param track The track to apply an direction on
457  * @param diagdir The DiagDirection to apply on
458  * @return The resulting track direction or INVALID_TRACKDIR if not possible.
459  */
TrackExitdirToTrackdir(Track track,DiagDirection diagdir)460 static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
461 {
462 	assert(IsValidTrack(track));
463 	assert(IsValidDiagDirection(diagdir));
464 	extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
465 	return _track_exitdir_to_trackdir[track][diagdir];
466 }
467 
468 /**
469  * Maps a track and an (4-way) dir to the trackdir that represents the track
470  * with the entry in the given direction.
471  *
472  * For the diagonal tracks the return value is clear, its either the matching
473  * track direction or INVALID_TRACKDIR.
474  * For the straight tracks this returns the track direction which results if
475  * you follow the DiagDirection and then turn by 45 deg left or right on the
476  * next tile. The new direction on the new track will be the returning Trackdir
477  * value. If the parameters makes no sense like the track TRACK_UPPER and the
478  * direction DIAGDIR_NE (target track cannot be reached) this function returns
479  * INVALID_TRACKDIR.
480  *
481  * @param track The target track
482  * @param diagdir The direction to "come from"
483  * @return the resulting Trackdir or INVALID_TRACKDIR if not possible.
484  */
TrackEnterdirToTrackdir(Track track,DiagDirection diagdir)485 static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
486 {
487 	assert(IsValidTrack(track));
488 	assert(IsValidDiagDirection(diagdir));
489 	extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
490 	return _track_enterdir_to_trackdir[track][diagdir];
491 }
492 
493 /**
494  * Maps a track and a full (8-way) direction to the trackdir that represents
495  * the track running in the given direction.
496  */
TrackDirectionToTrackdir(Track track,Direction dir)497 static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
498 {
499 	assert(IsValidTrack(track));
500 	assert(IsValidDirection(dir));
501 	extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
502 	return _track_direction_to_trackdir[track][dir];
503 }
504 
505 /**
506  * Maps a (4-way) direction to the diagonal track incidating with that diagdir
507  *
508  * @param diagdir The direction
509  * @return The resulting Track
510  */
DiagDirToDiagTrack(DiagDirection diagdir)511 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
512 {
513 	assert(IsValidDiagDirection(diagdir));
514 	return (Track)(diagdir & 1);
515 }
516 
517 /**
518  * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir
519  *
520  * @param diagdir The direction
521  * @return The resulting TrackBits
522  */
DiagDirToDiagTrackBits(DiagDirection diagdir)523 static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
524 {
525 	assert(IsValidDiagDirection(diagdir));
526 	return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
527 }
528 
529 /**
530  * Maps a (4-way) direction to the diagonal trackdir that runs in that
531  * direction.
532  *
533  * @param diagdir The direction
534  * @return The resulting Trackdir direction
535  */
DiagDirToDiagTrackdir(DiagDirection diagdir)536 static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
537 {
538 	assert(IsValidDiagDirection(diagdir));
539 	extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
540 	return _dir_to_diag_trackdir[diagdir];
541 }
542 
543 /**
544  * Returns all trackdirs that can be reached when entering a tile from a given
545  * (diagonal) direction.
546  *
547  * This will obviously include 90 degree turns, since no information is available
548  * about the exact angle of entering
549  *
550  * @param diagdir The joining direction
551  * @return The TrackdirBits which can be used from the given direction
552  * @see DiagdirReachesTracks
553  */
DiagdirReachesTrackdirs(DiagDirection diagdir)554 static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
555 {
556 	assert(IsValidDiagDirection(diagdir));
557 	extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
558 	return _exitdir_reaches_trackdirs[diagdir];
559 }
560 
561 /**
562  * Returns all tracks that can be reached when entering a tile from a given
563  * (diagonal) direction.
564  *
565  * This will obviously include 90 degree turns, since no
566  * information is available about the exact angle of entering
567  *
568  * @param diagdir The joining direction
569  * @return The tracks which can be used
570  * @see DiagdirReachesTrackdirs
571  */
DiagdirReachesTracks(DiagDirection diagdir)572 static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
573 
574 /**
575  * Maps a trackdir to the trackdirs that can be reached from it (ie, when
576  * entering the next tile.
577  *
578  * This will include 90 degree turns!
579  *
580  * @param trackdir The track direction which will be leaved
581  * @return The track directions which can be used from this direction (in the next tile)
582  */
TrackdirReachesTrackdirs(Trackdir trackdir)583 static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
584 {
585 	assert(IsValidTrackdir(trackdir));
586 	extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
587 	return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
588 }
589 /* Note that there is no direct table for this function (there used to be),
590  * but it uses two simpler tables to achieve the result */
591 
592 /**
593  * Maps a trackdir to all trackdirs that make 90 deg turns with it.
594  *
595  * For the diagonal tracks this returns the track direction bits
596  * of the other axis in both directions, which cannot be joined by
597  * the given track direction.
598  * For the straight tracks this returns all possible 90 deg turns
599  * either on the current tile (which no train can joined) or on the
600  * bordering tiles.
601  *
602  * @param trackdir The track direction
603  * @return The TrackdirBits which are (more or less) 90 deg turns.
604  */
TrackdirCrossesTrackdirs(Trackdir trackdir)605 static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
606 {
607 	assert(IsValidTrackdirForRoadVehicle(trackdir));
608 	extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
609 	return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
610 }
611 
612 /**
613  * Checks if a given Track is diagonal
614  *
615  * @param track The given track to check
616  * @return true if diagonal, else false
617  */
IsDiagonalTrack(Track track)618 static inline bool IsDiagonalTrack(Track track)
619 {
620 	assert(IsValidTrack(track));
621 	return (track == TRACK_X) || (track == TRACK_Y);
622 }
623 
624 /**
625  * Checks if a given Trackdir is diagonal.
626  *
627  * @param trackdir The given trackdir
628  * @return true if the trackdir use a diagonal track
629  */
IsDiagonalTrackdir(Trackdir trackdir)630 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
631 {
632 	assert(IsValidTrackdir(trackdir));
633 	return IsDiagonalTrack(TrackdirToTrack(trackdir));
634 }
635 
636 
637 /**
638  * Checks if the given tracks overlap, ie form a crossing. Basically this
639  * means when there is more than one track on the tile, except when there are
640  * two parallel tracks.
641  * @param  bits The tracks present.
642  * @return Whether the tracks present overlap in any way.
643  */
TracksOverlap(TrackBits bits)644 static inline bool TracksOverlap(TrackBits bits)
645 {
646 	/* With no, or only one track, there is no overlap */
647 	if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
648 	/* We know that there are at least two tracks present. When there are more
649 	 * than 2 tracks, they will surely overlap. When there are two, they will
650 	 * always overlap unless they are lower & upper or right & left. */
651 	return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
652 }
653 
654 /**
655  * Check if a given track is contained within or overlaps some other tracks.
656  *
657  * @param tracks Tracks to be tested against
658  * @param track The track to test
659  * @return true if the track is already in the tracks or overlaps the tracks.
660  */
TrackOverlapsTracks(TrackBits tracks,Track track)661 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
662 {
663 	if (HasBit(tracks, track)) return true;
664 	return TracksOverlap(tracks | TrackToTrackBits(track));
665 }
666 
667 /**
668  * Checks whether the trackdir means that we are reversing.
669  * @param dir the trackdir to check
670  * @return true if it is a reversing road trackdir
671  */
IsReversingRoadTrackdir(Trackdir dir)672 static inline bool IsReversingRoadTrackdir(Trackdir dir)
673 {
674 	assert(IsValidTrackdirForRoadVehicle(dir));
675 	return (dir & 0x07) >= 6;
676 }
677 
678 /**
679  * Checks whether the given trackdir is a straight road
680  * @param dir the trackdir to check
681  * @return true if it is a straight road trackdir
682  */
IsStraightRoadTrackdir(Trackdir dir)683 static inline bool IsStraightRoadTrackdir(Trackdir dir)
684 {
685 	assert(IsValidTrackdirForRoadVehicle(dir));
686 	return (dir & 0x06) == 0;
687 }
688 
689 /**
690  * Checks whether a trackdir on a specific slope is going uphill.
691  *
692  * Valid for rail and road tracks.
693  * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
694  *
695  * @param slope The slope of the tile.
696  * @param dir The trackdir of interest.
697  * @return true iff the track goes upwards.
698  */
IsUphillTrackdir(Slope slope,Trackdir dir)699 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
700 {
701 	assert(IsValidTrackdirForRoadVehicle(dir));
702 	extern const TrackdirBits _uphill_trackdirs[];
703 	return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
704 }
705 
706 /**
707  * Determine the side in which the vehicle will leave the tile
708  *
709  * @param direction vehicle direction
710  * @param track vehicle track bits
711  * @return side of tile the vehicle will leave
712  */
VehicleExitDir(Direction direction,TrackBits track)713 static inline DiagDirection VehicleExitDir(Direction direction, TrackBits track)
714 {
715 	static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
716 
717 	DiagDirection diagdir = DirToDiagDir(direction);
718 
719 	/* Determine the diagonal direction in which we will exit this tile */
720 	if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
721 		diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
722 	}
723 
724 	return diagdir;
725 }
726 
727 #endif /* TRACK_FUNC_H */
728