(svn r27953) -Cleanup: Adjust other languages for r27952
[openttd.git] / src / track_func.h
blob68de827ef1ec7920ec7419be2028e6071052641c
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 "direction_func.h"
18 #include "slope_func.h"
20 /**
21 * Iterate through each set Track in a TrackBits value.
22 * For more informations see FOR_EACH_SET_BIT_EX.
24 * @param var Loop index variable that stores fallowing set track. Must be of type Track.
25 * @param track_bits The value to iterate through (any expression).
27 * @see FOR_EACH_SET_BIT_EX
29 #define FOR_EACH_SET_TRACK(var, track_bits) FOR_EACH_SET_BIT_EX(Track, var, TrackBits, track_bits)
31 /**
32 * Checks if a Track is valid.
34 * @param track The value to check
35 * @return true if the given value is a valid track.
36 * @note Use this in an assert()
38 static inline bool IsValidTrack(Track track)
40 return track < TRACK_END;
43 /**
44 * Checks if a Trackdir is valid for road vehicles.
46 * @param trackdir The value to check
47 * @return true if the given value is a valid Trackdir
48 * @note Use this in an assert()
50 static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
52 return trackdir < TRACKDIR_END;
55 /**
56 * Checks if a Trackdir is valid for non-road vehicles.
58 * @param trackdir The value to check
59 * @return true if the given value is a valid Trackdir
60 * @note Use this in an assert()
62 static inline bool IsValidTrackdir(Trackdir trackdir)
64 return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
67 /**
68 * Convert an Axis to the corresponding Track
69 * AXIS_X -> TRACK_X
70 * AXIS_Y -> TRACK_Y
71 * Uses the fact that they share the same internal encoding
73 * @param a the axis to convert
74 * @return the track corresponding to the axis
76 static inline Track AxisToTrack(Axis a)
78 assert(IsValidAxis(a));
79 return (Track)a;
82 /**
83 * Maps a Track to the corresponding TrackBits value
84 * @param track the track to convert
85 * @return the converted TrackBits value of the track
87 static inline TrackBits TrackToTrackBits(Track track)
89 assert(IsValidTrack(track));
90 return (TrackBits)(1 << track);
93 /**
94 * Maps an Axis to the corresponding TrackBits value
95 * @param a the axis to convert
96 * @return the converted TrackBits value of the axis
98 static inline TrackBits AxisToTrackBits(Axis a)
100 return TrackToTrackBits(AxisToTrack(a));
104 * Returns a single horizontal/vertical trackbit that is in a specific tile corner.
106 * @param corner The corner of a tile.
107 * @return The TrackBits of the track in the corner.
109 static inline TrackBits CornerToTrackBits(Corner corner)
111 extern const TrackBits _corner_to_trackbits[];
112 assert(IsValidCorner(corner));
113 return _corner_to_trackbits[corner];
117 * Maps a Trackdir to the corresponding TrackdirBits value
118 * @param trackdir the track direction to convert
119 * @return the converted TrackdirBits value
121 static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
123 assert(IsValidTrackdir(trackdir));
124 return (TrackdirBits)(1 << trackdir);
128 * Removes first Track from TrackBits and returns it
130 * This function searchs for the first bit in the TrackBits,
131 * remove this bit from the parameter and returns the found
132 * bit as Track value. It returns INVALID_TRACK if the
133 * parameter was TRACK_BIT_NONE or INVALID_TRACK_BIT. This
134 * is basically used in while-loops to get up to 6 possible
135 * tracks on a tile until the parameter becomes TRACK_BIT_NONE.
137 * @param tracks The value with the TrackBits
138 * @return The first Track from the TrackBits value
139 * @see FindFirstTrack
141 static inline Track RemoveFirstTrack(TrackBits *tracks)
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;
149 return INVALID_TRACK;
153 * Removes first Trackdir from TrackdirBits and returns it
155 * This function searches for the first bit in the TrackdirBits parameter,
156 * remove this bit from the parameter and returns the fnound bit as
157 * Trackdir value. It returns INVALID_TRACKDIR if the trackdirs is
158 * TRACKDIR_BIT_NONE or INVALID_TRACKDIR_BIT. This is basically used in a
159 * while-loop to get all track-directions step by step until the value
160 * reaches TRACKDIR_BIT_NONE.
162 * @param trackdirs The value with the TrackdirBits
163 * @return The first Trackdir from the TrackdirBits value
164 * @see FindFirstTrackdir
166 static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
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;
174 return INVALID_TRACKDIR;
178 * Returns first Track from TrackBits or INVALID_TRACK
180 * This function returns the first Track found in the TrackBits value as Track-value.
181 * It returns INVALID_TRACK if the parameter is TRACK_BIT_NONE or INVALID_TRACK_BIT.
183 * @param tracks The TrackBits value
184 * @return The first Track found or INVALID_TRACK
185 * @see RemoveFirstTrack
187 static inline Track FindFirstTrack(TrackBits tracks)
189 return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK;
193 * Converts TrackBits to Track.
195 * This function converts a TrackBits value to a Track value. As it
196 * is not possible to convert two or more tracks to one track the
197 * parameter must contain only one track or be the INVALID_TRACK_BIT value.
199 * @param tracks The TrackBits value to convert
200 * @return The Track from the value or INVALID_TRACK
201 * @pre tracks must contains only one Track or be INVALID_TRACK_BIT
203 static inline Track TrackBitsToTrack(TrackBits tracks)
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;
210 * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR
212 * This function returns the first Trackdir in the given TrackdirBits value or
213 * INVALID_TRACKDIR if the value is TRACKDIR_BIT_NONE. The TrackdirBits must
214 * not be INVALID_TRACKDIR_BIT.
216 * @param trackdirs The TrackdirBits value
217 * @return The first Trackdir from the TrackdirBits or INVALID_TRACKDIR on TRACKDIR_BIT_NONE.
218 * @pre trackdirs must not be INVALID_TRACKDIR_BIT
219 * @see RemoveFirstTrackdir
221 static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
223 assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
224 return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
228 * Functions describing logical relations between Tracks, TrackBits, Trackdirs
229 * TrackdirBits, Direction and DiagDirections.
233 * Find the opposite track to a given track.
235 * TRACK_LOWER -> TRACK_UPPER and vice versa, likewise for left/right.
236 * TRACK_X is mapped to TRACK_Y and reversed.
238 * @param t the track to convert
239 * @return the opposite track
241 static inline Track TrackToOppositeTrack(Track t)
243 assert(IsValidTrack(t));
244 return (Track)(t ^ 1);
248 * Maps a trackdir to the reverse trackdir.
250 * Returns the reverse trackdir of a Trackdir value. The reverse trackdir
251 * is the same track with the other direction on it.
253 * @param trackdir The Trackdir value
254 * @return The reverse trackdir
255 * @pre trackdir must not be INVALID_TRACKDIR
257 static inline Trackdir ReverseTrackdir(Trackdir trackdir)
259 assert(IsValidTrackdirForRoadVehicle(trackdir));
260 return (Trackdir)(trackdir ^ 8);
264 * Returns the Track that a given Trackdir represents
266 * This function filters the Track which is used in the Trackdir value and
267 * returns it as a Track value.
269 * @param trackdir The trackdir value
270 * @return The Track which is used in the value
272 static inline Track TrackdirToTrack(Trackdir trackdir)
274 assert(IsValidTrackdir(trackdir));
275 return (Track)(trackdir & 0x7);
279 * Returns a Trackdir for the given Track
281 * Since every Track corresponds to two Trackdirs, we choose the
282 * one which points between NE and S. Note that the actual
283 * implementation is quite futile, but this might change
284 * in the future.
286 * @param track The given Track
287 * @return The Trackdir from the given Track
289 static inline Trackdir TrackToTrackdir(Track track)
291 assert(IsValidTrack(track));
292 return (Trackdir)track;
296 * Returns a TrackdirBit mask from a given Track
298 * The TrackdirBit mask contains the two TrackdirBits that
299 * correspond with the given Track (one for each direction).
301 * @param track The track to get the TrackdirBits from
302 * @return The TrackdirBits which the selected tracks
304 static inline TrackdirBits TrackToTrackdirBits(Track track)
306 Trackdir td = TrackToTrackdir(track);
307 return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td)));
311 * Discards all directional information from a TrackdirBits value
313 * Any Track which is present in either direction will be present in the result.
315 * @param bits The TrackdirBits to get the TrackBits from
316 * @return The TrackBits
318 static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
320 return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
324 * Converts TrackBits to TrackdirBits while allowing both directions.
326 * @param bits The TrackBits
327 * @return The TrackdirBits containing of bits in both directions.
329 static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
331 return (TrackdirBits)(bits * 0x101);
335 * Returns the present-trackdir-information of a TrackStatus.
337 * @param ts The TrackStatus returned by GetTileTrackStatus()
338 * @return the present trackdirs
340 static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
342 return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
346 * Returns the present-track-information of a TrackStatus.
348 * @param ts The TrackStatus returned by GetTileTrackStatus()
349 * @return the present tracks
351 static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
353 return TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(ts));
357 * Returns the red-signal-information of a TrackStatus.
359 * Note: The result may contain red signals for non-present tracks.
361 * @param ts The TrackStatus returned by GetTileTrackStatus()
362 * @return the The trackdirs that are blocked by red-signals
364 static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
366 return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
370 * Builds a TrackStatus
372 * @param trackdirbits present trackdirs
373 * @param red_signals red signals
374 * @return the TrackStatus representing the given information
376 static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
378 return (TrackStatus)(trackdirbits | (red_signals << 16));
382 * Maps a trackdir to the trackdir that you will end up on if you go straight
383 * ahead.
385 * This will be the same trackdir for diagonal trackdirs, but a
386 * different (alternating) one for straight trackdirs
388 * @param trackdir The given trackdir
389 * @return The next Trackdir value of the next tile.
391 static inline Trackdir NextTrackdir(Trackdir trackdir)
393 assert(IsValidTrackdir(trackdir));
394 extern const Trackdir _next_trackdir[TRACKDIR_END];
395 return _next_trackdir[trackdir];
399 * Maps a track to all tracks that make 90 deg turns with it.
401 * For the diagonal directions these are the complement of the
402 * direction, for the straight directions these are the
403 * two vertical or horizontal tracks, depend on the given direction
405 * @param track The given track
406 * @return The TrackBits with the tracks marked which cross the given track by 90 deg.
408 static inline TrackBits TrackCrossesTracks(Track track)
410 assert(IsValidTrack(track));
411 extern const TrackBits _track_crosses_tracks[TRACK_END];
412 return _track_crosses_tracks[track];
416 * Maps a trackdir to the (4-way) direction the tile is exited when following
417 * that trackdir.
419 * For the diagonal directions these are the same directions. For
420 * the straight directions these are the directions from the imagined
421 * base-tile to the bordering tile which will be joined if the given
422 * straight direction is leaved from the base-tile.
424 * @param trackdir The given track direction
425 * @return The direction which points to the resulting tile if following the Trackdir
427 static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
429 assert(IsValidTrackdirForRoadVehicle(trackdir));
430 extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
431 return _trackdir_to_exitdir[trackdir];
435 * Maps a track and an (4-way) dir to the trackdir that represents the track
436 * with the exit in the given direction.
438 * For the diagonal tracks the resulting track direction are clear for a given
439 * DiagDirection. It either matches the direction or it returns INVALID_TRACKDIR,
440 * as a TRACK_X cannot be applied with DIAG_SE.
441 * For the straight tracks the resulting track direction will be the
442 * direction which the DiagDirection is pointing. But this will be INVALID_TRACKDIR
443 * if the DiagDirection is pointing 'away' the track.
445 * @param track The track to apply an direction on
446 * @param diagdir The DiagDirection to apply on
447 * @return The resulting track direction or INVALID_TRACKDIR if not possible.
449 static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
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];
458 * Maps a track and an (4-way) dir to the trackdir that represents the track
459 * with the entry in the given direction.
461 * For the diagonal tracks the return value is clear, its either the matching
462 * track direction or INVALID_TRACKDIR.
463 * For the straight tracks this returns the track direction which results if
464 * you follow the DiagDirection and then turn by 45 deg left or right on the
465 * next tile. The new direction on the new track will be the returning Trackdir
466 * value. If the parameters makes no sense like the track TRACK_UPPER and the
467 * direction DIAGDIR_NE (target track cannot be reached) this function returns
468 * INVALID_TRACKDIR.
470 * @param track The target track
471 * @param diagdir The direction to "come from"
472 * @return the resulting Trackdir or INVALID_TRACKDIR if not possible.
474 static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
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];
483 * Maps a track and a full (8-way) direction to the trackdir that represents
484 * the track running in the given direction.
486 static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
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];
495 * Maps a (4-way) direction to the diagonal track incidating with that diagdir
497 * @param diagdir The direction
498 * @return The resulting Track
500 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
502 assert(IsValidDiagDirection(diagdir));
503 return (Track)(diagdir & 1);
507 * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir
509 * @param diagdir The direction
510 * @return The resulting TrackBits
512 static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
514 assert(IsValidDiagDirection(diagdir));
515 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
519 * Maps a (4-way) direction to the diagonal trackdir that runs in that
520 * direction.
522 * @param diagdir The direction
523 * @return The resulting Trackdir direction
525 static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
527 assert(IsValidDiagDirection(diagdir));
528 extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
529 return _dir_to_diag_trackdir[diagdir];
533 * Returns all trackdirs that can be reached when entering a tile from a given
534 * (diagonal) direction.
536 * This will obviously include 90 degree turns, since no information is available
537 * about the exact angle of entering
539 * @param diagdir The joining direction
540 * @return The TrackdirBits which can be used from the given direction
541 * @see DiagdirReachesTracks
543 static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
545 assert(IsValidDiagDirection(diagdir));
546 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
547 return _exitdir_reaches_trackdirs[diagdir];
551 * Returns all tracks that can be reached when entering a tile from a given
552 * (diagonal) direction.
554 * This will obviously include 90 degree turns, since no
555 * information is available about the exact angle of entering
557 * @param diagdir The joining direction
558 * @return The tracks which can be used
559 * @see DiagdirReachesTrackdirs
561 static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
564 * Maps a trackdir to the trackdirs that can be reached from it (ie, when
565 * entering the next tile.
567 * This will include 90 degree turns!
569 * @param trackdir The track direction which will be leaved
570 * @return The track directions which can be used from this direction (in the next tile)
572 static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
574 assert(IsValidTrackdir(trackdir));
575 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
576 return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
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 */
582 * Maps a trackdir to all trackdirs that make 90 deg turns with it.
584 * For the diagonal tracks this returns the track direction bits
585 * of the other axis in both directions, which cannot be joined by
586 * the given track direction.
587 * For the straight tracks this returns all possible 90 deg turns
588 * either on the current tile (which no train can joined) or on the
589 * bordering tiles.
591 * @param trackdir The track direction
592 * @return The TrackdirBits which are (more or less) 90 deg turns.
594 static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
596 assert(IsValidTrackdirForRoadVehicle(trackdir));
597 extern const TrackdirBits _track_crosses_trackdirs[TRACK_END];
598 return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
602 * Checks if a given Track is diagonal
604 * @param track The given track to check
605 * @return true if diagonal, else false
607 static inline bool IsDiagonalTrack(Track track)
609 assert(IsValidTrack(track));
610 return (track == TRACK_X) || (track == TRACK_Y);
614 * Checks if a given Trackdir is diagonal.
616 * @param trackdir The given trackdir
617 * @return true if the trackdir use a diagonal track
619 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
621 assert(IsValidTrackdir(trackdir));
622 return IsDiagonalTrack(TrackdirToTrack(trackdir));
627 * Checks if the given tracks overlap, ie form a crossing. Basically this
628 * means when there is more than one track on the tile, exept when there are
629 * two parallel tracks.
630 * @param bits The tracks present.
631 * @return Whether the tracks present overlap in any way.
633 static inline bool TracksOverlap(TrackBits bits)
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;
644 * Check if a given track is contained within or overlaps some other tracks.
646 * @param tracks Tracks to be tested against
647 * @param track The track to test
648 * @return true if the track is already in the tracks or overlaps the tracks.
650 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
652 if (HasBit(tracks, track)) return true;
653 return TracksOverlap(tracks | TrackToTrackBits(track));
657 * Checks whether the trackdir means that we are reversing.
658 * @param dir the trackdir to check
659 * @return true if it is a reversing road trackdir
661 static inline bool IsReversingRoadTrackdir(Trackdir dir)
663 assert(IsValidTrackdirForRoadVehicle(dir));
664 return (dir & 0x07) >= 6;
668 * Checks whether the given trackdir is a straight road
669 * @param dir the trackdir to check
670 * @return true if it is a straight road trackdir
672 static inline bool IsStraightRoadTrackdir(Trackdir dir)
674 assert(IsValidTrackdirForRoadVehicle(dir));
675 return (dir & 0x06) == 0;
679 * Checks whether a trackdir on a specific slope is going uphill.
681 * Valid for rail and road tracks.
682 * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
684 * @param slope The slope of the tile.
685 * @param dir The trackdir of interest.
686 * @return true iff the track goes upwards.
688 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
690 assert(IsValidTrackdirForRoadVehicle(dir));
691 extern const TrackdirBits _uphill_trackdirs[];
692 return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
695 #endif /* TRACK_FUNC_H */