src/track_func.h
author Tero Marttila <terom@fixme.fi>
Tue, 22 Jul 2008 21:51:14 +0300
changeset 11180 982e9f814f97
parent 10546 07ab34af246e
permissions -rw-r--r--
scan for tarfiles in CACHE_DIR, remember what Subdirectory a tar was found in, set the GCF_FLAG on GRFs loaded from there, and hide those in the NewGRF GUI
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2049
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2049
diff changeset
     2
8597
87f03bc9c8e4 (svn r11662) -Codechange: move some rail types/related functions around.
rubidium
parents: 8596
diff changeset
     3
/** @file track_func.h Different conversion functions from one kind of track to another. */
4041
caf4fcb4e05b (svn r5309) Partially fix the rail header dependency fiasco: rail_map.h now depends on rail.h and not the other way round anymore
tron
parents: 3900
diff changeset
     4
8597
87f03bc9c8e4 (svn r11662) -Codechange: move some rail types/related functions around.
rubidium
parents: 8596
diff changeset
     5
#ifndef TRACK_FUNC_H
87f03bc9c8e4 (svn r11662) -Codechange: move some rail types/related functions around.
rubidium
parents: 8596
diff changeset
     6
#define TRACK_FUNC_H
4041
caf4fcb4e05b (svn r5309) Partially fix the rail header dependency fiasco: rail_map.h now depends on rail.h and not the other way round anymore
tron
parents: 3900
diff changeset
     7
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8604
diff changeset
     8
#include "core/bitmath_func.hpp"
8597
87f03bc9c8e4 (svn r11662) -Codechange: move some rail types/related functions around.
rubidium
parents: 8596
diff changeset
     9
#include "track_type.h"
87f03bc9c8e4 (svn r11662) -Codechange: move some rail types/related functions around.
rubidium
parents: 8596
diff changeset
    10
#include "direction_type.h"
8604
8afdd9877afd (svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
rubidium
parents: 8599
diff changeset
    11
#include "slope_func.h"
4041
caf4fcb4e05b (svn r5309) Partially fix the rail header dependency fiasco: rail_map.h now depends on rail.h and not the other way round anymore
tron
parents: 3900
diff changeset
    12
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    13
/**
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    14
 * Convert an Axis to the corresponding Track
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    15
 * AXIS_X -> TRACK_X
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    16
 * AXIS_Y -> TRACK_Y
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    17
 * Uses the fact that they share the same internal encoding
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    18
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    19
 * @param a the axis to convert
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    20
 * @return the track corresponding to the axis
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    21
 */
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    22
static inline Track AxisToTrack(Axis a)
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    23
{
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    24
	return (Track)a;
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    25
}
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    26
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    27
/**
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    28
 * Maps a Track to the corresponding TrackBits value
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    29
 * @param track the track to convert
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    30
 * @return the converted TrackBits value of the track
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    31
 */
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    32
static inline TrackBits TrackToTrackBits(Track track)
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    33
{
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    34
	return (TrackBits)(1 << track);
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    35
}
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    36
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    37
/**
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    38
 * Maps an Axis to the corresponding TrackBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    39
 * @param a the axis to convert
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    40
 * @return the converted TrackBits value of the axis
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    41
 */
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    42
static inline TrackBits AxisToTrackBits(Axis a)
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    43
{
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    44
	return TrackToTrackBits(AxisToTrack(a));
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    45
}
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    46
8199
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    47
/**
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    48
 * Returns a single horizontal/vertical trackbit, that is in a specific tile corner.
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    49
 *
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    50
 * @param corner The corner of a tile.
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    51
 * @return The TrackBits of the track in the corner.
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    52
 */
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    53
static inline TrackBits CornerToTrackBits(Corner corner)
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    54
{
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    55
	extern const TrackBits _corner_to_trackbits[];
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    56
	assert(IsValidCorner(corner));
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    57
	return _corner_to_trackbits[corner];
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    58
}
00ee9637c0a0 (svn r11237) -Codechange: reduce code duplication between GetRailFoundation() and CheckRailSlope(). Patch by frosch.
rubidium
parents: 8097
diff changeset
    59
4158
a8f7265a6fd0 (svn r5582) Add and use AxisToTrack{Bits,}()
tron
parents: 4077
diff changeset
    60
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    61
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
    62
/**
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
    63
 * Maps a Trackdir to the corresponding TrackdirBits value
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    64
 * @param trackdir the track direction to convert
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    65
 * @return the converted TrackdirBits value
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
    66
 */
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    67
static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    68
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    69
	return (TrackdirBits)(1 << trackdir);
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    70
}
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
    71
2232
0c63dce21e30 (svn r2752) -Doc: Added some doxygen stuff to rail.h. I'm gonna mess around with this file and wanted to get rid of this first
celestar
parents: 2186
diff changeset
    72
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    73
 * Removes first Track from TrackBits and returns it
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    74
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    75
 * This function searchs for the first bit in the TrackBits,
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    76
 * remove this bit from the parameter and returns the found
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    77
 * bit as Track value. It returns INVALID_TRACK if the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    78
 * parameter was TRACK_BIT_NONE or INVALID_TRACK_BIT. This
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    79
 * is basically used in while-loops to get up to 6 possible
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    80
 * tracks on a tile until the parameter becomes TRACK_BIT_NONE.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    81
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    82
 * @param tracks The value with the TrackBits
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    83
 * @return The first Track from the TrackBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    84
 * @see FindFirstTrack
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    85
 */
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
    86
static inline Track RemoveFirstTrack(TrackBits *tracks)
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    87
{
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
    88
	if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) {
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
    89
		Track first = (Track)FIND_FIRST_BIT(*tracks);
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
    90
		ClrBit(*tracks, first);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    91
		return first;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    92
	}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    93
	return INVALID_TRACK;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    94
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    95
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
    96
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    97
 * Removes first Trackdir from TrackdirBits and returns it
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    98
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
    99
 * This function searchs for the first bit in the TrackdirBits parameter,
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   100
 * remove this bit from the parameter and returns the fnound bit as
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   101
 * Trackdir value. It returns INVALID_TRACKDIR if the trackdirs is
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   102
 * TRACKDIR_BIT_NONE or INVALID_TRACKDIR_BIT. This is basically used in a
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   103
 * while-loop to get all track-directions step by step until the value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   104
 * reaches TRACKDIR_BIT_NONE.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   105
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   106
 * @param trackdirs The value with the TrackdirBits
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   107
 * @return The first Trackdir from the TrackdirBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   108
 * @see FindFirstTrackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   109
 */
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   110
static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   111
{
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   112
	if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) {
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   113
		Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs);
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   114
		ClrBit(*trackdirs, first);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   115
		return first;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   116
	}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   117
	return INVALID_TRACKDIR;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   118
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   119
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   120
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   121
 * Returns first Track from TrackBits or INVALID_TRACK
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   122
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   123
 * This function returns the first Track found in the TrackBits value as Track-value.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   124
 * It returns INVALID_TRACK if the parameter is TRACK_BIT_NONE or INVALID_TRACK_BIT.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   125
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   126
 * @param tracks The TrackBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   127
 * @return The first Track found or INVALID_TRACK
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   128
 * @see RemoveFirstTrack
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   129
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   130
static inline Track FindFirstTrack(TrackBits tracks)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   131
{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   132
	return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   133
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   134
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   135
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   136
 * Converts TrackBits to Track.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   137
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   138
 * This function converts a TrackBits value to a Track value. As it
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   139
 * is not possible to convert two or more tracks to one track the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   140
 * parameter must contain only one track or be the INVALID_TRACK_BIT value.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   141
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   142
 * @param tracks The TrackBits value to convert
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   143
 * @return The Track from the value or INVALID_TRACK
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   144
 * @pre tracks must contains only one Track or be INVALID_TRACK_BIT
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   145
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   146
static inline Track TrackBitsToTrack(TrackBits tracks)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   147
{
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8267
diff changeset
   148
	assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KillFirstBit(tracks & TRACK_BIT_MASK) == TRACK_BIT_NONE));
7651
71d525fb971e (svn r10429) -Fix: VC8 Code Analyzer warning: buffer overrun
KUDr
parents: 6916
diff changeset
   149
	return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks & TRACK_BIT_MASK) : INVALID_TRACK;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   150
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   151
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   152
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   153
 * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   154
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   155
 * This function returns the first Trackdir in the given TrackdirBits value or
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   156
 * INVALID_TRACKDIR if the value is TRACKDIR_BIT_NONE. The TrackdirBits must
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   157
 * not be INVALID_TRACKDIR_BIT.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   158
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   159
 * @param trackdirs The TrackdirBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   160
 * @return The first Trackdir from the TrackdirBits or INVALID_TRACKDIR on TRACKDIR_BIT_NONE.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   161
 * @pre trackdirs must not be INVALID_TRACKDIR_BIT
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   162
 * @see RemoveFirstTrackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   163
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   164
static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   165
{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   166
	assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE);
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   167
	return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   168
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   169
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   170
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   171
 * Checks if a Track is valid.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   172
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   173
 * @param track The value to check
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   174
 * @return true if the given value is a valid track.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   175
 * @note Use this in an assert()
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   176
 */
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   177
static inline bool IsValidTrack(Track track)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   178
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   179
	return track < TRACK_END;
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   180
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   181
2232
0c63dce21e30 (svn r2752) -Doc: Added some doxygen stuff to rail.h. I'm gonna mess around with this file and wanted to get rid of this first
celestar
parents: 2186
diff changeset
   182
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   183
 * Checks if a Trackdir is valid.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   184
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   185
 * @param trackdir The value to check
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   186
 * @return true if the given valie is a valid Trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   187
 * @note Use this in an assert()
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   188
 */
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   189
static inline bool IsValidTrackdir(Trackdir trackdir)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   190
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   191
	return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0;
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   192
}
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   193
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   194
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   195
/*
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   196
 * Functions describing logical relations between Tracks, TrackBits, Trackdirs
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   197
 * TrackdirBits, Direction and DiagDirections.
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   198
 */
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   199
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   200
/**
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   201
 * Maps a trackdir to the reverse trackdir.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   202
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   203
 * Returns the reverse trackdir of a Trackdir value. The reverse trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   204
 * is the same track with the other direction on it.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   205
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   206
 * @param trackdir The Trackdir value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   207
 * @return The reverse trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   208
 * @pre trackdir must not be INVALID_TRACKDIR
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   209
 */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   210
static inline Trackdir ReverseTrackdir(Trackdir trackdir)
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   211
{
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5726
diff changeset
   212
	assert(trackdir != INVALID_TRACKDIR);
3500
70b8e368e43c (svn r4351) Simplify ReverseTrackdir() to use a simple arithmetic operation instead of a table lookup
tron
parents: 3498
diff changeset
   213
	return (Trackdir)(trackdir ^ 8);
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   214
}
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   215
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   216
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   217
 * Returns the Track that a given Trackdir represents
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   218
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   219
 * This function filters the Track which is used in the Trackdir value and
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   220
 * returns it as a Track value.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   221
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   222
 * @param trackdir The trackdir value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   223
 * @return The Track which is used in the value
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   224
 */
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   225
static inline Track TrackdirToTrack(Trackdir trackdir)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   226
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   227
	return (Track)(trackdir & 0x7);
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   228
}
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   229
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   230
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   231
 * Returns a Trackdir for the given Track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   232
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   233
 * Since every Track corresponds to two Trackdirs, we choose the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   234
 * one which points between NE and S. Note that the actual
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   235
 * implementation is quite futile, but this might change
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   236
 * in the future.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   237
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   238
 * @param track The given Track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   239
 * @return The Trackdir from the given Track
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   240
 */
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   241
static inline Trackdir TrackToTrackdir(Track track)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   242
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   243
	return (Trackdir)track;
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   244
}
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   245
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   246
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   247
 * Returns a TrackdirBit mask from a given Track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   248
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   249
 * The TrackdirBit mask contains the two TrackdirBits that
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   250
 * correspond with the given Track (one for each direction).
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   251
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   252
 * @param track The track to get the TrackdirBits from
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   253
 * @return The TrackdirBits which the selected tracks
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   254
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   255
static inline TrackdirBits TrackToTrackdirBits(Track track)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   256
{
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   257
	Trackdir td = TrackToTrackdir(track);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   258
	return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td)));
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   259
}
1944
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   260
012fa5e69118 (svn r2450) * Codechange: Replaced all uses of the arrays in tile.h with calls to the associated wrapper functions.
matthijs
parents: 1942
diff changeset
   261
/**
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   262
 * Discards all directional information from a TrackdirBits value
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   263
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   264
 * Any Track which is present in either direction will be present in the result.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   265
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   266
 * @param bits The TrackdirBits to get the TrackBits from
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   267
 * @return The TrackBits
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   268
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   269
static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   270
{
4281
55f66f81239d (svn r5911) -Fix [FS#215]: Discard upper bits of TrackdirBitsToTrackBits result.
matthijs
parents: 4158
diff changeset
   271
	return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   272
}
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   273
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   274
/**
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   275
 * Converts TrackBits to TrackdirBits while allowing both directions.
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   276
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   277
 * @param bits The TrackBits
10546
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   278
 * @return The TrackdirBits containing of bits in both directions.
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   279
 */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   280
static inline TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   281
{
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   282
	return (TrackdirBits)(bits * 0x101);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   283
}
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   284
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   285
/**
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   286
 * Returns the present-trackdir-information of a TrackStatus.
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   287
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   288
 * @param ts The TrackStatus returned by GetTileTrackStatus()
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   289
 * @return the present trackdirs
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   290
 */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   291
static inline TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   292
{
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   293
	return (TrackdirBits)(ts & TRACKDIR_BIT_MASK);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   294
}
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   295
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   296
/**
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   297
 * Returns the present-track-information of a TrackStatus.
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   298
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   299
 * @param ts The TrackStatus returned by GetTileTrackStatus()
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   300
 * @return the present tracks
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   301
 */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   302
static inline TrackBits TrackStatusToTrackBits(TrackStatus ts)
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   303
{
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   304
	return TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(ts));
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   305
}
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   306
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   307
/**
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   308
 * Returns the red-signal-information of a TrackStatus.
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   309
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   310
 * Note: The result may contain red signals for non-present tracks.
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   311
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   312
 * @param ts The TrackStatus returned by GetTileTrackStatus()
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   313
 * @return the The trackdirs that are blocked by red-signals
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   314
 */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   315
static inline TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   316
{
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   317
	return (TrackdirBits)((ts >> 16) & TRACKDIR_BIT_MASK);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   318
}
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   319
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   320
/**
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   321
 * Builds a TrackStatus
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   322
 *
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   323
 * @param trackdirbits present trackdirs
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   324
 * @param red_signals red signals
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   325
 * @return the TrackStatus representing the given information
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   326
 */
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   327
static inline TrackStatus CombineTrackStatus(TrackdirBits trackdirbits, TrackdirBits red_signals)
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   328
{
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   329
	return (TrackStatus)(trackdirbits | (red_signals << 16));
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   330
}
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   331
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8609
diff changeset
   332
/**
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   333
 * Maps a trackdir to the trackdir that you will end up on if you go straight
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   334
 * ahead.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   335
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   336
 * This will be the same trackdir for diagonal trackdirs, but a
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   337
 * different (alternating) one for straight trackdirs
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   338
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   339
 * @param trackdir The given trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   340
 * @return The next Trackdir value of the next tile.
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   341
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   342
static inline Trackdir NextTrackdir(Trackdir trackdir)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   343
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   344
	extern const Trackdir _next_trackdir[TRACKDIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   345
	return _next_trackdir[trackdir];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   346
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   347
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   348
/**
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   349
 * Maps a track to all tracks that make 90 deg turns with it.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   350
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   351
 * For the diagonal directions these are the complement of the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   352
 * direction, for the straight directions these are the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   353
 * two vertical or horizontal tracks, depend on the given direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   354
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   355
 * @param track The given track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   356
 * @return The TrackBits with the tracks marked which cross the given track by 90 deg.
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   357
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   358
static inline TrackBits TrackCrossesTracks(Track track)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   359
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   360
	extern const TrackBits _track_crosses_tracks[TRACK_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   361
	return _track_crosses_tracks[track];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   362
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   363
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   364
/**
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   365
 * Maps a trackdir to the (4-way) direction the tile is exited when following
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   366
 * that trackdir.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   367
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   368
 * For the diagonal directions these are the same directions. For
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   369
 * the straight directions these are the directions from the imagined
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   370
 * base-tile to the bordering tile which will be joined if the given
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   371
 * straight direction is leaved from the base-tile.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   372
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   373
 * @param trackdir The given track direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   374
 * @return The direction which points to the resulting tile if following the Trackdir
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   375
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   376
static inline DiagDirection TrackdirToExitdir(Trackdir trackdir)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   377
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   378
	extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   379
	return _trackdir_to_exitdir[trackdir];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   380
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   381
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   382
/**
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   383
 * Maps a track and an (4-way) dir to the trackdir that represents the track
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   384
 * with the exit in the given direction.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   385
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   386
 * For the diagonal tracks the resulting track direction are clear for a given
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   387
 * DiagDirection. It either matches the direction or it returns INVALID_TRACKDIR,
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   388
 * as a TRACK_X cannot be applied with DIAG_SE.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   389
 * For the straight tracks the resulting track direction will be the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   390
 * direction which the DiagDirection is pointing. But this will be INVALID_TRACKDIR
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   391
 * if the DiagDirection is pointing 'away' the track.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   392
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   393
 * @param track The track to applie an direction on
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   394
 * @param diagdir The DiagDirection to applie on
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   395
 * @return The resulting track direction or INVALID_TRACKDIR if not possible.
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   396
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   397
static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   398
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   399
	extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   400
	return _track_exitdir_to_trackdir[track][diagdir];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   401
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   402
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   403
/**
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   404
 * Maps a track and an (4-way) dir to the trackdir that represents the track
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   405
 * with the entry in the given direction.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   406
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   407
 * For the diagonal tracks the return value is clear, its either the matching
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   408
 * track direction or INVALID_TRACKDIR.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   409
 * For the straight tracks this returns the track direction which results if
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   410
 * you follow the DiagDirection and then turn by 45 deg left or right on the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   411
 * next tile. The new direction on the new track will be the returning Trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   412
 * value. If the parameters makes no sense like the track TRACK_UPPER and the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   413
 * diraction DIAGDIR_NE (target track cannot be reached) this function returns
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   414
 * INVALID_TRACKDIR.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   415
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   416
 * @param track The target track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   417
 * @param diagdir The direction to "come from"
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   418
 * @return the resulting Trackdir or INVALID_TRACKDIR if not possible.
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   419
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   420
static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   421
{
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   422
	extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END];
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   423
	return _track_enterdir_to_trackdir[track][diagdir];
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   424
}
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   425
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   426
/**
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   427
 * Maps a track and a full (8-way) direction to the trackdir that represents
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   428
 * the track running in the given direction.
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   429
 */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   430
static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   431
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   432
	extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   433
	return _track_direction_to_trackdir[track][dir];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   434
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   435
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   436
/**
10546
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   437
 * Maps a (4-way) direction to the diagonal track incidating with that diagdir
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   438
 *
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   439
 * @param diagdir The direction
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   440
 * @return The resulting Track
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   441
 */
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   442
static inline Track DiagDirToDiagTrack(DiagDirection diagdir)
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   443
{
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   444
	return (Track)(diagdir & 1);
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   445
}
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   446
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   447
/**
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   448
 * Maps a (4-way) direction to the diagonal track bits incidating with that diagdir
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   449
 *
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   450
 * @param diagdir The direction
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   451
 * @return The resulting TrackBits
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   452
 */
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   453
static inline TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   454
{
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   455
	return TrackToTrackBits(DiagDirToDiagTrack(diagdir));
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   456
}
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   457
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   458
/**
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   459
 * Maps a (4-way) direction to the diagonal trackdir that runs in that
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   460
 * direction.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   461
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   462
 * @param diagdir The direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   463
 * @return The resulting Trackdir direction
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   464
 */
10546
07ab34af246e (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9149
diff changeset
   465
static inline Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   466
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   467
	extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   468
	return _dir_to_diag_trackdir[diagdir];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   469
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   470
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   471
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   472
 * Returns all trackdirs that can be reached when entering a tile from a given
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   473
 * (diagonal) direction.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   474
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   475
 * This will obviously include 90 degree turns, since no information is available
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   476
 * about the exact angle of entering
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   477
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   478
 * @param diagdir The joining direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   479
 * @return The TrackdirBits which can be used from the given direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   480
 * @see DiagdirReachesTracks
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   481
 */
6488
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   482
static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   483
{
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   484
	extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   485
	return _exitdir_reaches_trackdirs[diagdir];
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   486
}
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   487
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   488
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   489
 * Returns all tracks that can be reached when entering a tile from a given
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   490
 * (diagonal) direction.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   491
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   492
 * This will obviously include 90 degree turns, since no
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   493
 * information is available about the exact angle of entering
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   494
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   495
 * @param diagdir The joining irection
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   496
 * @return The tracks which can be used
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   497
 * @see DiagdirReachesTrackdirs
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   498
 */
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   499
static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); }
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   500
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   501
/**
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   502
 * Maps a trackdir to the trackdirs that can be reached from it (ie, when
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   503
 * entering the next tile.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   504
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   505
 * This will include 90 degree turns!
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   506
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   507
 * @param trackdir The track direction which will be leaved
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   508
 * @return The track directions which can be used from this direction (in the next tile)
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   509
 */
6488
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   510
static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   511
{
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   512
	extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END];
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   513
	return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)];
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   514
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   515
/* Note that there is no direct table for this function (there used to be),
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   516
 * but it uses two simpeler tables to achieve the result */
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   517
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   518
/**
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   519
 * Maps a trackdir to all trackdirs that make 90 deg turns with it.
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   520
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   521
 * For the diagonal tracks this returns the track direction bits
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   522
 * of the other axis in both directions, which cannot be joined by
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   523
 * the given track direction.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   524
 * For the straight tracks this returns all possible 90 deg turns
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   525
 * either on the current tile (which no train can joined) or on the
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   526
 * bordering tiles.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   527
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   528
 * @param trackdir The track direction
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   529
 * @return The TrackdirBits which are (more or less) 90 deg turns.
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   530
 */
6488
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   531
static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
07bbbafd4f70 (svn r8912) -Codechange: coding style cleanup and moving of two global externs into the functions that use them.
rubidium
parents: 6482
diff changeset
   532
{
1948
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   533
	extern const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   534
	return _track_crosses_trackdirs[TrackdirToTrack(trackdir)];
e5f34b543a3c (svn r2454) * Codechange: Move #include "stdafx.h" and "openttd.h" from rail.h to rail.c
matthijs
parents: 1947
diff changeset
   535
}
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
   536
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   537
/**
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   538
 * Checks if a given Track is diagonal
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   539
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   540
 * @param track The given track to check
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   541
 * @return true if diagonal, else false
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   542
 */
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   543
static inline bool IsDiagonalTrack(Track track)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   544
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   545
	return (track == TRACK_X) || (track == TRACK_Y);
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   546
}
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
   547
7892
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   548
/**
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   549
 * Checks if a given Trackdir is diagonal.
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   550
 *
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   551
 * @param trackdir The given trackdir
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   552
 * @return true if the trackdir use a diagonal track
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   553
 */
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   554
static inline bool IsDiagonalTrackdir(Trackdir trackdir)
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   555
{
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   556
	return IsDiagonalTrack(TrackdirToTrack(trackdir));
af0701f35fdf (svn r10765) -Documentation [FS#1100]: of rail.h. Based on a patch by Progman.
rubidium
parents: 7831
diff changeset
   557
}
1967
c57192f4c121 (svn r2473) - Add: VehicleMayEnterTile(), which checks if the tile owner of a tile is correct for a vehicle to enter it. Based upon glx's code.
matthijs
parents: 1950
diff changeset
   558
3355
a653b8e47f27 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3267
diff changeset
   559
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   560
/**
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   561
 * Checks if the given tracks overlap, ie form a crossing. Basically this
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   562
 * means when there is more than one track on the tile, exept when there are
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   563
 * two parallel tracks.
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   564
 * @param  bits The tracks present.
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   565
 * @return Whether the tracks present overlap in any way.
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   566
 */
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   567
static inline bool TracksOverlap(TrackBits bits)
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   568
{
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   569
	/* With no, or only one track, there is no overlap */
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8267
diff changeset
   570
	if (bits == TRACK_BIT_NONE || KillFirstBit(bits) == TRACK_BIT_NONE) return false;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   571
	/* We know that there are at least two tracks present. When there are more
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   572
	 * than 2 tracks, they will surely overlap. When there are two, they will
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4041
diff changeset
   573
	 * always overlap unless they are lower & upper or right & left. */
3258
94a915e7485f (svn r3946) Add short hand names for common track combinations
tron
parents: 3253
diff changeset
   574
	return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT;
2782
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   575
}
b8453c2cb0ed (svn r3329) - Doc: Some documentation cleanups.
matthijs
parents: 2548
diff changeset
   576
8598
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   577
/**
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   578
 * Checks whether the trackdir means that we are reversing.
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   579
 * @param dir the trackdir to check
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   580
 * @return true if it is a reversing road trackdir
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   581
 */
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   582
static inline bool IsReversingRoadTrackdir(Trackdir dir)
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   583
{
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   584
	return (dir & 0x07) >= 6;
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   585
}
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   586
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   587
/**
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   588
 * Checks whether the given trackdir is a straight road
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   589
 * @param dir the trackdir to check
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   590
 * @return true if it is a straight road trackdir
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   591
 */
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   592
static inline bool IsStraightRoadTrackdir(Trackdir dir)
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   593
{
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   594
	return (dir & 0x06) == 0;
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   595
}
14ae80fe4c8f (svn r11663) -Codechange: moving of the road related types and functions.
rubidium
parents: 8597
diff changeset
   596
9149
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   597
/**
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   598
 * Checks whether a trackdir on a specific slope is going uphill.
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   599
 *
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   600
 * Valid for rail and road tracks.
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   601
 * Valid for tile-slopes (under foundation) and foundation-slopes (on foundation).
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   602
 *
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   603
 * @param slope The slope of the tile.
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   604
 * @param dir The trackdir of interest.
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   605
 * @return true iff the track goes upwards.
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   606
 */
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   607
static inline bool IsUphillTrackdir(Slope slope, Trackdir dir)
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   608
{
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   609
	extern const TrackdirBits _uphill_trackdirs[];
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   610
	return HasBit(_uphill_trackdirs[RemoveHalftileSlope(slope)], dir);
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   611
}
cd335504407b (svn r12313) -Fix: YAPF and NTP did not apply penalty for uphill tracks on steep slopes.
frosch
parents: 9112
diff changeset
   612
8599
b609cdeeff3f (svn r11664) -Codechange: use more specific ("rail_type.h" instead of "rail.h" that includes way more than only "rail_type.h") includes at some places.
rubidium
parents: 8598
diff changeset
   613
#endif /* TRACK_FUNC_H */