rail.c
author matthijs
Thu, 16 Jun 2005 18:04:02 +0000
changeset 1942 c5d5cf5b0263
child 1944 dd9cba5fab2a
permissions -rw-r--r--
(svn r2448) General cleanup of rail related code, more to follow.
* Add: rail.[ch] for rail-related enums and wrapper functions.
* Codechange: Removed dozens of magic numbers with below enums.
* Codechange: Rewrote CheckTrackCombination().
* Add: TILE_SIZE, TILE_PIXELS and TILE_HEIGHT constants.
* Add: enums RailTileType, RailTileSubtype, SignalType to mask against the map arrays.
* Add: enums Track, TrackBits, Trackdir, TrackdirBits for railway track data. (Note that the old RAIL_BIT constants are replaced by TRACK_BIT ones).
* Add: enums Direction and DiagDirection
* Codechange: Moved a bunch of track(dir) related lookup arrays from npf.[ch] to rail.[ch].
* Codechange: move RailType enum from tile.h to rail.h.
* Add: Wrapper functions for masking signal status in the map arrays: SignalAlongTrackdir, SignalAgainstTrackdir and SignalOnTrack.
* Add: Wrapper functions to access rail tiles, using above enums
* Add: Wrapper functions to modify tracks, trackdirs, directions, etc.
* Add: Wrapper functions for all lookup arrays in rail.[ch] (Arrays are still used in parts of the code)
* Codechange: Renamed some variables and arguments to better represent what they contain (railbit -> track, bits -> trackdirbits, etc.).
* Codechange: Don't use FindLandscapeHeight() in CmdRemoveSingleRail(), since it returns way too much info. Use GetTileSlope() instead.
* Codechange: [NPF] Removed some unused globals and code from npf.c.
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     1
#include "rail.h"
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     2
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     3
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     4
 * direction along with the trackdir */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     5
const byte _signal_along_trackdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     6
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     7
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     8
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
     9
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    10
/* Maps a trackdir to the bit that stores its status in the map arrays, in the
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    11
 * direction against the trackdir */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    12
const byte _signal_against_trackdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    13
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    14
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    15
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    16
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    17
/* Maps a Track to the bits that store the status of the two signals that can
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    18
 * be present on the given track */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    19
const byte _signal_on_track[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    20
	0xC0, 0xC0, 0xC0, 0x30, 0xC0, 0x30
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    21
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    22
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    23
/* Maps a diagonal direction to the all trackdirs that are connected to any
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    24
 * track entering in this direction (including those making 90 degree turns)
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    25
 */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    26
const TrackdirBits _exitdir_reaches_trackdirs[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    27
	TRACKDIR_BIT_DIAG1_NE|TRACKDIR_BIT_LOWER_E|TRACKDIR_BIT_LEFT_N,  /* DIAGDIR_NE */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    28
	TRACKDIR_BIT_DIAG2_SE|TRACKDIR_BIT_LEFT_S |TRACKDIR_BIT_UPPER_E, /* DIAGDIR_SE */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    29
	TRACKDIR_BIT_DIAG1_SW|TRACKDIR_BIT_UPPER_W|TRACKDIR_BIT_RIGHT_S, /* DIAGDIR_SW */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    30
	TRACKDIR_BIT_DIAG2_NW|TRACKDIR_BIT_RIGHT_N|TRACKDIR_BIT_LOWER_W  /* DIAGDIR_NW */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    31
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    32
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    33
/* TODO: Remove magic numbers from tables below just like
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    34
 * _exitdir_reaches_trackdirs[] */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    35
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    36
const Trackdir _next_trackdir[14] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    37
	0,  1,  3,  2,  5,  4, 0, 0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    38
	8,  9,  11, 10, 13, 12
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    39
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    40
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    41
/* Maps a trackdir to all trackdirs that make 90 deg turns with it. */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    42
const TrackdirBits _trackdir_crosses_trackdirs[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    43
	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C, 0, 0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    44
	0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    45
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    46
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    47
/* Maps a track to all tracks that make 90 deg turns with it. */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    48
const TrackBits _track_crosses_tracks[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    49
	0x2, /* Track 1 -> Track 2 */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    50
	0x1, /* Track 2 -> Track 1 */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    51
	0x30, /* Upper -> Left | Right */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    52
	0x30, /* Lower -> Left | Right */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    53
	0x0C, /* Left -> Upper | Lower */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    54
	0x0C, /* Right -> Upper | Lower */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    55
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    56
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    57
/* Maps a trackdir to the (4-way) direction the tile is exited when following
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    58
 * that trackdir */
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    59
const DiagDirection _trackdir_to_exitdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    60
	0,1,0,1,2,1, 0,0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    61
	2,3,3,2,3,0,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    62
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    63
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    64
const Trackdir _track_exitdir_to_trackdir[][DIAGDIR_END] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    65
	{0,    0xff, 8,    0xff},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    66
	{0xff, 1,    0xff, 9},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    67
	{2,    0xff, 0xff, 10},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    68
	{0xff, 3,    11,   0xf},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    69
	{0xff, 0xff, 4,    12},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    70
	{13,   5,    0xff, 0xff}
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    71
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    72
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    73
const Trackdir _track_direction_to_trackdir[][DIR_END] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    74
	{0xff, 0,    0xff, 0xff, 0xff, 8,    0xff, 0xff},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    75
	{0xff, 0xff, 0xff, 1,    0xff, 0xff, 0xff, 9},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    76
	{0xff, 0xff, 2,    0xff, 0xff, 0xff, 10,   0xff},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    77
	{0xff, 0xff, 3,    0xff, 0xff, 0xff, 11,   0xff},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    78
	{12,   0xff, 0xff, 0xff, 4,    0xff, 0xff, 0xff},
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    79
	{13,   0xff, 0xff, 0xff, 5,    0xff, 0xff, 0xff}
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    80
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    81
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    82
const Trackdir _dir_to_diag_trackdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    83
	0, 1, 8, 9,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    84
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    85
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    86
const DiagDirection _reverse_diagdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    87
	2, 3, 0, 1
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    88
};
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    89
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    90
const Trackdir _reverse_trackdir[] = {
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    91
	8, 9, 10, 11, 12, 13, 0xFF, 0xFF,
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    92
	0, 1, 2,  3,  4,  5
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
diff changeset
    93
};