Add support for deleted functions
[openttd/fttd.git] / src / track_func.h
blob646888120a82407d5d71a17d7bc3edd77e024082
1 /* $Id$ */
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 */
10 /** @file track_func.h Different conversion functions from one kind of track to another. */
12 #ifndef TRACK_FUNC_H
13 #define TRACK_FUNC_H
15 #include "core/bitmath_func.hpp"
16 #include "track_type.h"
17 #include "slope_func.h"
19 /**
20 * Iterate through each set Track in a TrackBits value.
21 * For more informations see FOR_EACH_SET_BIT_EX.
23 * @param var Loop index variable that stores fallowing set track. Must be of type Track.
24 * @param track_bits The value to iterate through (any expression).
26 * @see FOR_EACH_SET_BIT_EX
28 #define FOR_EACH_SET_TRACK(var, track_bits) FOR_EACH_SET_BIT_EX(Track, var, TrackBits, track_bits)
30 /**
31 * Convert an Axis to the corresponding Track
32 * AXIS_X -> TRACK_X
33 * AXIS_Y -> TRACK_Y
34 * Uses the fact that they share the same internal encoding
36 * @param a the axis to convert
37 * @return the track corresponding to the axis
39 static inline Track AxisToTrack(Axis a)
41 return (Track)a;
44 /**
45 * Maps a Track to the corresponding TrackBits value
46 * @param track the track to convert
47 * @return the converted TrackBits value of the track
49 static inline TrackBits TrackToTrackBits(Track track)
51 return (TrackBits)(1 << track);
54 /**
55 * Maps an Axis to the corresponding TrackBits value
56 * @param a the axis to convert
57 * @return the converted TrackBits value of the axis
59 static inline TrackBits AxisToTrackBits(Axis a)
61 return TrackToTrackBits(AxisToTrack(a));
64 /**
65 * Returns a single horizontal/vertical trackbit that is in a specific tile corner.
67 * @param corner The corner of a tile.
68 * @return The TrackBits of the track in the corner.
70 static inline TrackBits CornerToTrackBits(Corner corner)
72 extern const TrackBits _corner_to_trackbits[];
73 assert(IsValidCorner(corner));
74 return _corner_to_trackbits[corner];
79 /**
80 * Maps a Trackdir to the corresponding TrackdirBits value
81 * @param trackdir the track direction to convert
82 * @return the converted TrackdirBits value
84 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
86 return (TrackdirBits)(1 << trackdir);
89 /**
90 * Removes first Track from TrackBits and returns it
92 * This function searchs for the first bit in the TrackBits,
93 * remove this bit from the parameter and returns the found
94 * bit as Track value. It returns INVALID_TRACK if the
95 * parameter was TRACK_BIT_NONE or INVALID_TRACK_BIT. This
96 * is basically used in while-loops to get up to 6 possible
97 * tracks on a tile until the parameter becomes TRACK_BIT_NONE.
99 * @param tracks The value with the TrackBits
100 * @return The first Track from the TrackBits value
101 * @see FindFirstTrack
103 static inline Track RemoveFirstTrack(TrackBits *tracks)
105 if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
106 Track first = (Track)FIND_FIRST_BIT(*tracks);
107 ClrBit(*tracks, first);
108 return first;
110 return INVALID_TRACK;
114 * Removes first Trackdir from TrackdirBits and returns it
116 * This function searches for the first bit in the TrackdirBits parameter,
117 * remove this bit from the parameter and returns the fnound bit as
118 * Trackdir value. It returns INVALID_TRACKDIR if the trackdirs is
119 * TRACKDIR_BIT_NONE or INVALID_TRACKDIR_BIT. This is basically used in a
120 * while-loop to get all track-directions step by step until the value
121 * reaches TRACKDIR_BIT_NONE.
123 * @param trackdirs The value with the TrackdirBits
124 * @return The first Trackdir from the TrackdirBits value
125 * @see FindFirstTrackdir
127 static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
129 if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
130 Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs);
131 ClrBit(*trackdirs, first);
132 return first;
134 return INVALID_TRACKDIR;
138 * Returns first Track from TrackBits or INVALID_TRACK
140 * This function returns the first Track found in the TrackBits value as Track-value.
141 * It returns INVALID_TRACK if the parameter is TRACK_BIT_NONE or INVALID_TRACK_BIT.
143 * @param tracks The TrackBits value
144 * @return The first Track found or INVALID_TRACK
145 * @see RemoveFirstTrack
147 static inline Track FindFirstTrack(TrackBits tracks)
149 return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK;
153 * Converts TrackBits to Track.
155 * This function converts a TrackBits value to a Track value. As it
156 * is not possible to convert two or more tracks to one track the
157 * parameter must contain only one track or be the INVALID_TRACK_BIT value.
159 * @param tracks The TrackBits value to convert
160 * @return The Track from the value or INVALID_TRACK
161 * @pre tracks must contains only one Track or be INVALID_TRACK_BIT
163 static inline Track TrackBitsToTrack(TrackBits tracks)
165 assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE));
166 return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks & TRACK_BIT_MASK) : INVALID_TRACK;
170 * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR
172 * This function returns the first Trackdir in the given TrackdirBits value or
173 * INVALID_TRACKDIR if the value is TRACKDIR_BIT_NONE. The TrackdirBits must
174 * not be INVALID_TRACKDIR_BIT.
176 * @param trackdirs The TrackdirBits value
177 * @return The first Trackdir from the TrackdirBits or INVALID_TRACKDIR on TRACKDIR_BIT_NONE.
178 * @pre trackdirs must not be INVALID_TRACKDIR_BIT
179 * @see RemoveFirstTrackdir
181 static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
183 assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
184 return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
188 * Checks if a Track is valid.
190 * @param track The value to check
191 * @return true if the given value is a valid track.
192 * @note Use this in an assert()
194 static inline bool IsValidTrack(Track track)
196 return track < TRACK_END;
200 * Checks if a Trackdir is valid.
202 * @param trackdir The value to check
203 * @return true if the given value is a valid Trackdir
204 * @note Use this in an assert()
206 static inline bool IsValidTrackdir(Trackdir trackdir)
208 return (trackdir & ~0x09) < 6;
213 * Functions describing logical relations between Tracks, TrackBits, Trackdirs
214 * TrackdirBits, Direction and DiagDirections.
218 * Find the opposite track to a given track.
220 * TRACK_LOWER -> TRACK_UPPER and vice versa, likewise for left/right.
221 * TRACK_X is mapped to TRACK_Y and reversed.
223 * @param t the track to convert
224 * @return the opposite track
226 static inline Track TrackToOppositeTrack(Track t)
228 assert(t != INVALID_TRACK);
229 return (Track)(t ^ 1);
233 * Maps a trackdir to the reverse trackdir.
235 * Returns the reverse trackdir of a Trackdir value. The reverse trackdir
236 * is the same track with the other direction on it.
238 * @param trackdir The Trackdir value
239 * @return The reverse trackdir
240 * @pre trackdir must not be INVALID_TRACKDIR
242 static inline Trackdir ReverseTrackdir(Trackdir trackdir)
244 assert(trackdir < TRACKDIR_END);
245 return (Trackdir)(trackdir ^ 8);
249 * Returns the Track that a given Trackdir represents
251 * This function filters the Track which is used in the Trackdir value and
252 * returns it as a Track value.
254 * @param trackdir The trackdir value
255 * @return The Track which is used in the value
257 static inline Track TrackdirToTrack(Trackdir trackdir)
259 return (Track)(trackdir & ~0x8);
263 * Returns a Trackdir for the given Track
265 * Since every Track corresponds to two Trackdirs, we choose the
266 * one which points between NE and S. Note that the actual
267 * implementation is quite futile, but this might change
268 * in the future.
270 * @param track The given Track
271 * @return The Trackdir from the given Track
273 static inline Trackdir TrackToTrackdir(Track track)
275 return (Trackdir)track;
279 * Returns a TrackdirBit mask from a given Track
281 * The TrackdirBit mask contains the two TrackdirBits that
282 * correspond with the given Track (one for each direction).
284 * @param track The track to get the TrackdirBits from
285 * @return The TrackdirBits which the selected tracks
287 static inline TrackdirBits TrackToTrackdirBits(Track track)
289 Trackdir td = TrackToTrackdir(track);
290 return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td)));
294 * Discards all directional information from a TrackdirBits value
296 * Any Track which is present in either direction will be present in the result.
298 * @param bits The TrackdirBits to get the TrackBits from
299 * @return The TrackBits
301 static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
303 return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
307 * Converts TrackBits to TrackdirBits while allowing both directions.
309 * @param bits The TrackBits
310 * @return The TrackdirBits containing of bits in both directions.
312 static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
314 return (TrackdirBits)(bits * 0x101);
318 * Returns the present-trackdir-information of a TrackStatus.
320 * @param ts The TrackStatus returned by GetTileTrackStatus()
321 * @return the present trackdirs
323 static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
325 return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
329 * Returns the present-track-information of a TrackStatus.
331 * @param ts The TrackStatus returned by GetTileTrackStatus()
332 * @return the present tracks
334 static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
336 return TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(ts));
340 * Returns the red-signal-information of a TrackStatus.
342 * Note: The result may contain red signals for non-present tracks.
344 * @param ts The TrackStatus returned by GetTileTrackStatus()
345 * @return the The trackdirs that are blocked by red-signals
347 static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
349 return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
353 * Builds a TrackStatus
355 * @param trackdirbits present trackdirs
356 * @param red_signals red signals
357 * @return the TrackStatus representing the given information
359 static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
361 return (TrackStatus)(trackdirbits | (red_signals << 16));
365 * Maps a trackdir to the trackdir that you will end up on if you go straight
366 * ahead.
368 * This will be the same trackdir for diagonal trackdirs, but a
369 * different (alternating) one for straight trackdirs
371 * @param trackdir The given trackdir
372 * @return The next Trackdir value of the next tile.
374 static inline Trackdir NextTrackdir(Trackdir trackdir)
376 extern const Trackdir _next_trackdir[TRACKDIR_END];
377 return _next_trackdir[trackdir];
381 * Maps a track to all tracks that make 90 deg turns with it.
383 * For the diagonal directions these are the complement of the
384 * direction, for the straight directions these are the
385 * two vertical or horizontal tracks, depend on the given direction
387 * @param track The given track
388 * @return The TrackBits with the tracks marked which cross the given track by 90 deg.
390 static inline TrackBits TrackCrossesTracks(Track track)
392 extern const TrackBits _track_crosses_tracks[TRACK_END];
393 return _track_crosses_tracks[track];
397 * Maps a trackdir to the (4-way) direction the tile is exited when following
398 * that trackdir.
400 * For the diagonal directions these are the same directions. For
401 * the straight directions these are the directions from the imagined
402 * base-tile to the bordering tile which will be joined if the given
403 * straight direction is leaved from the base-tile.
405 * @param trackdir The given track direction
406 * @return The direction which points to the resulting tile if following the Trackdir
408 static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
410 extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
411 return _trackdir_to_exitdir[trackdir];
415 * Maps a track and a full (8-way) direction to the trackdir that represents
416 * the track running in the given direction.
418 static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
420 extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
421 return _track_direction_to_trackdir[track][dir];
425 * Maps a pair of (4-way) directions to the trackdir that must be followed
426 * on a tile when entering in the first direction to exit in the second one.
428 * @param enterdir The direction in which the tile is entered
429 * @param exitdir The direction in which the tile is to be exited
430 * @return the trackdir that must be followed
431 * @note some combinations lead to reversing trackdirs
433 static inline Trackdir EnterdirExitdirToTrackdir(DiagDirection enterdir, DiagDirection exitdir)
435 extern const Trackdir _enterdir_exitdir_to_trackdir[DIAGDIR_END][DIAGDIR_END];
436 return _enterdir_exitdir_to_trackdir[enterdir][exitdir];
440 * Maps a (4-way) direction to the diagonal track incidating with that diagdir
442 * @param diagdir The direction
443 * @return The resulting Track
445 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
447 return (Track)(diagdir & 1);
451 * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir
453 * @param diagdir The direction
454 * @return The resulting TrackBits
456 static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
458 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
462 * Maps a (4-way) direction to the diagonal trackdir that runs in that
463 * direction.
465 * @param diagdir The direction
466 * @return The resulting Trackdir direction
468 static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
470 extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
471 return _dir_to_diag_trackdir[diagdir];
475 * Returns all trackdirs that can be reached when entering a tile from a given
476 * (diagonal) direction.
478 * This will obviously include 90 degree turns, since no information is available
479 * about the exact angle of entering
481 * @param diagdir The joining direction
482 * @return The TrackdirBits which can be used from the given direction
483 * @see DiagdirReachesTracks
485 static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
487 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
488 return _exitdir_reaches_trackdirs[diagdir];
492 * Returns all tracks that can be reached when entering a tile from a given
493 * (diagonal) direction.
495 * This will obviously include 90 degree turns, since no
496 * information is available about the exact angle of entering
498 * @param diagdir The joining direction
499 * @return The tracks which can be used
500 * @see DiagdirReachesTrackdirs
502 static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
505 * Maps a trackdir to the trackdirs that can be reached from it (ie, when
506 * entering the next tile.
508 * This will include 90 degree turns!
510 * @param trackdir The track direction which will be leaved
511 * @return The track directions which can be used from this direction (in the next tile)
513 static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
515 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
516 return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
518 /* Note that there is no direct table for this function (there used to be),
519 * but it uses two simpler tables to achieve the result */
522 * Maps a track to all trackdirs that make 90 deg turns with it.
524 * For the diagonal tracks this returns the track direction bits
525 * of the other axis in both directions, which cannot be joined by
526 * the given track direction.
527 * For the straight tracks this returns all possible 90 deg turns
528 * either on the current tile (which no train can joined) or on the
529 * bordering tiles.
531 * @param track The track
532 * @return The TrackdirBits which are (more or less) 90 deg turns.
534 static inline TrackdirBits TrackCrossesTrackdirs(Track track)
536 extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
537 return _track_crosses_trackdirs[track];
541 * Maps a trackdir to all trackdirs that make 90 deg turns with it.
543 * See TrackCrossesTrackdirs.
545 * @param trackdir The track direction
546 * @return The TrackdirBits which are (more or less) 90 deg turns.
548 static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
550 return TrackCrossesTrackdirs(TrackdirToTrack(trackdir));
554 * Checks if a given Track is diagonal
556 * @param track The given track to check
557 * @return true if diagonal, else false
559 static inline bool IsDiagonalTrack(Track track)
561 return (track == TRACK_X) || (track == TRACK_Y);
565 * Checks if a given Trackdir is diagonal.
567 * @param trackdir The given trackdir
568 * @return true if the trackdir use a diagonal track
570 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
572 return (trackdir & ~0x09) == 0;
577 * Checks if the given tracks overlap, ie form a crossing. Basically this
578 * means when there is more than one track on the tile, exept when there are
579 * two parallel tracks.
580 * @param bits The tracks present.
581 * @return Whether the tracks present overlap in any way.
583 static inline bool TracksOverlap(TrackBits bits)
585 /* With no, or only one track, there is no overlap */
586 if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
587 /* We know that there are at least two tracks present. When there are more
588 * than 2 tracks, they will surely overlap. When there are two, they will
589 * always overlap unless they are lower & upper or right & left. */
590 return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
594 * Check if a given track is contained within or overlaps some other tracks.
596 * @param tracks Tracks to be tested against
597 * @param track The track to test
598 * @return true if the track is already in the tracks or overlaps the tracks.
600 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
602 if (HasBit(tracks, track)) return true;
603 return TracksOverlap(tracks | TrackToTrackBits(track));
607 * Checks whether the trackdir means that we are reversing.
608 * @param dir the trackdir to check
609 * @return true if it is a reversing road trackdir
611 static inline bool IsReversingRoadTrackdir(Trackdir dir)
613 return (dir & 0x07) >= 6;
617 * Checks whether the given trackdir is a straight road
618 * @param dir the trackdir to check
619 * @return true if it is a straight road trackdir
621 static inline bool IsStraightRoadTrackdir(Trackdir dir)
623 return (dir & 0x06) == 0;
627 * Checks whether a trackdir on a specific slope is going uphill.
629 * Valid for rail and road tracks.
630 * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
632 * @param slope The slope of the tile.
633 * @param dir The trackdir of interest.
634 * @return true iff the track goes upwards.
636 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
638 extern const TrackdirBits _uphill_trackdirs[];
639 return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
642 #endif /* TRACK_FUNC_H */