(svn r23005) -Fix (r23004): Of course there's still the 16-sprite version for shore...
[openttd/fttd.git] / src / track_func.h
bloba497dd4993b14dbe8b5f81921e3d2772a28eddfa
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 searchs 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 valie is a valid Trackdir
204 * @note Use this in an assert()
206 static inline bool IsValidTrackdir(Trackdir trackdir)
208 return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0;
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 != INVALID_TRACKDIR);
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 & 0x7);
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 an (4-way) dir to the trackdir that represents the track
416 * with the exit in the given direction.
418 * For the diagonal tracks the resulting track direction are clear for a given
419 * DiagDirection. It either matches the direction or it returns INVALID_TRACKDIR,
420 * as a TRACK_X cannot be applied with DIAG_SE.
421 * For the straight tracks the resulting track direction will be the
422 * direction which the DiagDirection is pointing. But this will be INVALID_TRACKDIR
423 * if the DiagDirection is pointing 'away' the track.
425 * @param track The track to applie an direction on
426 * @param diagdir The DiagDirection to applie on
427 * @return The resulting track direction or INVALID_TRACKDIR if not possible.
429 static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
431 extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
432 return _track_exitdir_to_trackdir[track][diagdir];
436 * Maps a track and an (4-way) dir to the trackdir that represents the track
437 * with the entry in the given direction.
439 * For the diagonal tracks the return value is clear, its either the matching
440 * track direction or INVALID_TRACKDIR.
441 * For the straight tracks this returns the track direction which results if
442 * you follow the DiagDirection and then turn by 45 deg left or right on the
443 * next tile. The new direction on the new track will be the returning Trackdir
444 * value. If the parameters makes no sense like the track TRACK_UPPER and the
445 * diraction DIAGDIR_NE (target track cannot be reached) this function returns
446 * INVALID_TRACKDIR.
448 * @param track The target track
449 * @param diagdir The direction to "come from"
450 * @return the resulting Trackdir or INVALID_TRACKDIR if not possible.
452 static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
454 extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
455 return _track_enterdir_to_trackdir[track][diagdir];
459 * Maps a track and a full (8-way) direction to the trackdir that represents
460 * the track running in the given direction.
462 static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
464 extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
465 return _track_direction_to_trackdir[track][dir];
469 * Maps a (4-way) direction to the diagonal track incidating with that diagdir
471 * @param diagdir The direction
472 * @return The resulting Track
474 static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
476 return (Track)(diagdir & 1);
480 * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir
482 * @param diagdir The direction
483 * @return The resulting TrackBits
485 static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
487 return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
491 * Maps a (4-way) direction to the diagonal trackdir that runs in that
492 * direction.
494 * @param diagdir The direction
495 * @return The resulting Trackdir direction
497 static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
499 extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
500 return _dir_to_diag_trackdir[diagdir];
504 * Returns all trackdirs that can be reached when entering a tile from a given
505 * (diagonal) direction.
507 * This will obviously include 90 degree turns, since no information is available
508 * about the exact angle of entering
510 * @param diagdir The joining direction
511 * @return The TrackdirBits which can be used from the given direction
512 * @see DiagdirReachesTracks
514 static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
516 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
517 return _exitdir_reaches_trackdirs[diagdir];
521 * Returns all tracks that can be reached when entering a tile from a given
522 * (diagonal) direction.
524 * This will obviously include 90 degree turns, since no
525 * information is available about the exact angle of entering
527 * @param diagdir The joining irection
528 * @return The tracks which can be used
529 * @see DiagdirReachesTrackdirs
531 static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
534 * Maps a trackdir to the trackdirs that can be reached from it (ie, when
535 * entering the next tile.
537 * This will include 90 degree turns!
539 * @param trackdir The track direction which will be leaved
540 * @return The track directions which can be used from this direction (in the next tile)
542 static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
544 extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
545 return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
547 /* Note that there is no direct table for this function (there used to be),
548 * but it uses two simpeler tables to achieve the result */
551 * Maps a trackdir to all trackdirs that make 90 deg turns with it.
553 * For the diagonal tracks this returns the track direction bits
554 * of the other axis in both directions, which cannot be joined by
555 * the given track direction.
556 * For the straight tracks this returns all possible 90 deg turns
557 * either on the current tile (which no train can joined) or on the
558 * bordering tiles.
560 * @param trackdir The track direction
561 * @return The TrackdirBits which are (more or less) 90 deg turns.
563 static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
565 extern const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END];
566 return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
570 * Checks if a given Track is diagonal
572 * @param track The given track to check
573 * @return true if diagonal, else false
575 static inline bool IsDiagonalTrack(Track track)
577 return (track == TRACK_X) || (track == TRACK_Y);
581 * Checks if a given Trackdir is diagonal.
583 * @param trackdir The given trackdir
584 * @return true if the trackdir use a diagonal track
586 static inline bool IsDiagonalTrackdir(Trackdir trackdir)
588 return IsDiagonalTrack(TrackdirToTrack(trackdir));
593 * Checks if the given tracks overlap, ie form a crossing. Basically this
594 * means when there is more than one track on the tile, exept when there are
595 * two parallel tracks.
596 * @param bits The tracks present.
597 * @return Whether the tracks present overlap in any way.
599 static inline bool TracksOverlap(TrackBits bits)
601 /* With no, or only one track, there is no overlap */
602 if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
603 /* We know that there are at least two tracks present. When there are more
604 * than 2 tracks, they will surely overlap. When there are two, they will
605 * always overlap unless they are lower & upper or right & left. */
606 return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
610 * Check if a given track is contained within or overlaps some other tracks.
612 * @param tracks Tracks to be testet against
613 * @param track The track to test
614 * @return true if the track is already in the tracks or overlaps the tracks.
616 static inline bool TrackOverlapsTracks(TrackBits tracks, Track track)
618 if (HasBit(tracks, track)) return true;
619 return TracksOverlap(tracks | TrackToTrackBits(track));
623 * Checks whether the trackdir means that we are reversing.
624 * @param dir the trackdir to check
625 * @return true if it is a reversing road trackdir
627 static inline bool IsReversingRoadTrackdir(Trackdir dir)
629 return (dir & 0x07) >= 6;
633 * Checks whether the given trackdir is a straight road
634 * @param dir the trackdir to check
635 * @return true if it is a straight road trackdir
637 static inline bool IsStraightRoadTrackdir(Trackdir dir)
639 return (dir & 0x06) == 0;
643 * Checks whether a trackdir on a specific slope is going uphill.
645 * Valid for rail and road tracks.
646 * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
648 * @param slope The slope of the tile.
649 * @param dir The trackdir of interest.
650 * @return true iff the track goes upwards.
652 static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
654 extern const TrackdirBits _uphill_trackdirs[];
655 return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
658 #endif /* TRACK_FUNC_H */