tron@2186: /* $Id$ */ tron@2186: celestar@2232: /** @file rail.h */ celestar@2232: matthijs@1942: #ifndef RAIL_H matthijs@1942: #define RAIL_H matthijs@1942: KUDr@5636: #include "gfx.h" tron@3147: #include "direction.h" matthijs@1942: #include "tile.h" matthijs@1942: rubidium@7396: /** rubidium@7396: * Enumeration for all possible railtypes. rubidium@7396: * rubidium@7396: * This enumeration defines all 4 possible railtypes. rubidium@7396: */ rubidium@6248: enum RailType { rubidium@7396: RAILTYPE_BEGIN = 0, ///< Used for iterations rubidium@7396: RAILTYPE_RAIL = 0, ///< Standard non-electric rails rubidium@7396: RAILTYPE_ELECTRIC = 1, ///< Electric rails rubidium@7396: RAILTYPE_MONO = 2, ///< Monorail rubidium@7396: RAILTYPE_MAGLEV = 3, ///< Maglev rubidium@7396: RAILTYPE_END, ///< Used for iterations rubidium@7396: INVALID_RAILTYPE = 0xFF ///< Flag for invalid railtype rubidium@6248: }; tron@4041: tron@4041: typedef byte RailTypeMask; tron@4041: rubidium@5587: /** Allow incrementing of Track variables */ rubidium@5587: DECLARE_POSTFIX_INCREMENT(RailType); rubidium@5587: /** Define basic enum properties */ rubidium@5587: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@5587: typedef TinyEnumT RailTypeByte; rubidium@5587: tron@4041: rubidium@7396: /** rubidium@7396: * These are used to specify a single track. rubidium@7396: * Can be translated to a trackbit with TrackToTrackbit rubidium@7396: */ rubidium@6248: enum Track { rubidium@7396: TRACK_BEGIN = 0, ///< Used for iterations rubidium@7396: TRACK_X = 0, ///< Track along the x-axis (north-east to south-west) rubidium@7396: TRACK_Y = 1, ///< Track along the y-axis (north-west to south-east) rubidium@7396: TRACK_UPPER = 2, ///< Track in the upper corner of the tile (north) rubidium@7396: TRACK_LOWER = 3, ///< Track in the lower corner of the tile (south) rubidium@7396: TRACK_LEFT = 4, ///< Track in the left corner of the tile (west) rubidium@7396: TRACK_RIGHT = 5, ///< Track in the right corner of the tile (east) rubidium@7396: TRACK_END, ///< Used for iterations rubidium@7396: INVALID_TRACK = 0xFF ///< Flag for an invalid track rubidium@6248: }; tron@4041: rubidium@5587: /** Allow incrementing of Track variables */ rubidium@5587: DECLARE_POSTFIX_INCREMENT(Track); rubidium@5587: /** Define basic enum properties */ rubidium@5587: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@5587: typedef TinyEnumT TrackByte; rubidium@5587: tron@4041: rubidium@7396: /** rubidium@7396: * Convert an Axis to the corresponding Track tron@4158: * AXIS_X -> TRACK_X tron@4158: * AXIS_Y -> TRACK_Y tron@4158: * Uses the fact that they share the same internal encoding rubidium@7396: * rubidium@7396: * @param a the axis to convert rubidium@7396: * @return the track corresponding to the axis tron@4158: */ tron@4158: static inline Track AxisToTrack(Axis a) tron@4158: { tron@4158: return (Track)a; tron@4158: } tron@4158: tron@4158: tron@4041: /** Bitfield corresponding to Track */ rubidium@6248: enum TrackBits { rubidium@7396: TRACK_BIT_NONE = 0U, ///< No track rubidium@7396: TRACK_BIT_X = 1U << TRACK_X, ///< X-axis track rubidium@7396: TRACK_BIT_Y = 1U << TRACK_Y, ///< Y-axis track rubidium@7396: TRACK_BIT_UPPER = 1U << TRACK_UPPER, ///< Upper track rubidium@7396: TRACK_BIT_LOWER = 1U << TRACK_LOWER, ///< Lower track rubidium@7396: TRACK_BIT_LEFT = 1U << TRACK_LEFT, ///< Left track rubidium@7396: TRACK_BIT_RIGHT = 1U << TRACK_RIGHT, ///< Right track rubidium@7396: TRACK_BIT_CROSS = TRACK_BIT_X | TRACK_BIT_Y, ///< X-Y-axis cross rubidium@7396: TRACK_BIT_HORZ = TRACK_BIT_UPPER | TRACK_BIT_LOWER, ///< Upper and lower track rubidium@7396: TRACK_BIT_VERT = TRACK_BIT_LEFT | TRACK_BIT_RIGHT, ///< Left and right track rubidium@7396: TRACK_BIT_3WAY_NE = TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT,///< "Arrow" to the north-east rubidium@7396: TRACK_BIT_3WAY_SE = TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT,///< "Arrow" to the south-east rubidium@7396: TRACK_BIT_3WAY_SW = TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT, ///< "Arrow" to the south-west rubidium@7396: TRACK_BIT_3WAY_NW = TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT, ///< "Arrow" to the north-west rubidium@7396: TRACK_BIT_ALL = TRACK_BIT_CROSS | TRACK_BIT_HORZ | TRACK_BIT_VERT, ///< All possible tracks rubidium@7396: TRACK_BIT_MASK = 0x3FU, ///< Bitmask for the first 6 bits rubidium@7396: TRACK_BIT_WORMHOLE = 0x40U, ///< Bitflag for a wormhole (used for tunnels) rubidium@7396: TRACK_BIT_DEPOT = 0x80U, ///< Bitflag for a depot rubidium@7396: INVALID_TRACK_BIT = 0xFF ///< Flag for an invalid trackbits value rubidium@6248: }; tron@4041: rubidium@5587: /** Define basic enum properties */ rubidium@5587: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@5587: typedef TinyEnumT TrackBitsByte; rubidium@5587: rubidium@5587: DECLARE_ENUM_AS_BIT_SET(TrackBits); tron@4041: tron@4158: /** tron@4158: * Maps a Track to the corresponding TrackBits value rubidium@7396: * @param track the track to convert rubidium@7396: * @return the converted TrackBits value of the track tron@4158: */ tron@4158: static inline TrackBits TrackToTrackBits(Track track) tron@4158: { tron@4158: return (TrackBits)(1 << track); tron@4158: } tron@4158: rubidium@7396: /** rubidium@7396: * Maps an Axis to the corresponding TrackBits value rubidium@7396: * @param a the axis to convert rubidium@7396: * @return the converted TrackBits value of the axis rubidium@7396: */ tron@4158: static inline TrackBits AxisToTrackBits(Axis a) tron@4158: { tron@4158: return TrackToTrackBits(AxisToTrack(a)); tron@4158: } tron@4158: tron@4158: rubidium@7396: /** rubidium@7396: * Enumeration for tracks and directions. rubidium@7396: * rubidium@7396: * These are a combination of tracks and directions. Values are 0-5 in one rubidium@5999: * direction (corresponding to the Track enum) and 8-13 in the other direction. rubidium@5999: * 6, 7, 14 and 15 are used to encode the reversing of road vehicles. Those rubidium@5999: * reversing track dirs are not considered to be 'valid' except in a small rubidium@5999: * corner in the road vehicle controller. rubidium@5999: */ rubidium@6248: enum Trackdir { rubidium@7396: TRACKDIR_BEGIN = 0, ///< Used for iterations rubidium@7396: TRACKDIR_X_NE = 0, ///< X-axis and direction to north-east rubidium@7396: TRACKDIR_Y_SE = 1, ///< Y-axis and direction to south-east rubidium@7396: TRACKDIR_UPPER_E = 2, ///< Upper track and direction to east rubidium@7396: TRACKDIR_LOWER_E = 3, ///< Lower track and direction to east rubidium@7396: TRACKDIR_LEFT_S = 4, ///< Left track and direction to south rubidium@7396: TRACKDIR_RIGHT_S = 5, ///< Right track and direction to south rubidium@7396: TRACKDIR_RVREV_NE = 6, ///< (Road vehicle) reverse direction north-east rubidium@7396: TRACKDIR_RVREV_SE = 7, ///< (Road vehicle) reverse direction south-east rubidium@7396: TRACKDIR_X_SW = 8, ///< X-axis and direction to south-west rubidium@7396: TRACKDIR_Y_NW = 9, ///< Y-axis and direction to north-west rubidium@7396: TRACKDIR_UPPER_W = 10, ///< Upper track and direction to west rubidium@7396: TRACKDIR_LOWER_W = 11, ///< Lower track and direction to west rubidium@7396: TRACKDIR_LEFT_N = 12, ///< Left track and direction to north rubidium@7396: TRACKDIR_RIGHT_N = 13, ///< Right track and direction to north rubidium@7396: TRACKDIR_RVREV_SW = 14, ///< (Road vehicle) reverse direction south-west rubidium@7396: TRACKDIR_RVREV_NW = 15, ///< (Road vehicle) reverse direction north-west rubidium@7396: TRACKDIR_END, ///< Used for iterations rubidium@7396: INVALID_TRACKDIR = 0xFF, ///< Flag for an invalid trackdir rubidium@6248: }; matthijs@1942: rubidium@5587: /** Define basic enum properties */ rubidium@5587: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@5587: typedef TinyEnumT TrackdirByte; rubidium@5587: rubidium@7396: /** rubidium@7396: * Enumeration of bitmasks for the TrackDirs rubidium@7396: * rubidium@7396: * These are a combination of tracks and directions. Values are 0-5 in one rubidium@7396: * direction (corresponding to the Track enum) and 8-13 in the other direction. rubidium@7396: */ rubidium@6248: enum TrackdirBits { rubidium@7396: TRACKDIR_BIT_NONE = 0x0000, ///< No track build rubidium@7396: TRACKDIR_BIT_X_NE = 0x0001, ///< Track x-axis, direction north-east rubidium@7396: TRACKDIR_BIT_Y_SE = 0x0002, ///< Track y-axis, direction south-east rubidium@7396: TRACKDIR_BIT_UPPER_E = 0x0004, ///< Track upper, direction east rubidium@7396: TRACKDIR_BIT_LOWER_E = 0x0008, ///< Track lower, direction east rubidium@7396: TRACKDIR_BIT_LEFT_S = 0x0010, ///< Track left, direction south rubidium@7396: TRACKDIR_BIT_RIGHT_S = 0x0020, ///< Track right, direction south matthijs@1942: /* Again, note the two missing values here. This enables trackdir -> track conversion by doing (trackdir & 0xFF) */ rubidium@7396: TRACKDIR_BIT_X_SW = 0x0100, ///< Track x-axis, direction south-west rubidium@7396: TRACKDIR_BIT_Y_NW = 0x0200, ///< Track y-axis, direction north-west rubidium@7396: TRACKDIR_BIT_UPPER_W = 0x0400, ///< Track upper, direction west rubidium@7396: TRACKDIR_BIT_LOWER_W = 0x0800, ///< Track lower, direction west rubidium@7396: TRACKDIR_BIT_LEFT_N = 0x1000, ///< Track left, direction north rubidium@7396: TRACKDIR_BIT_RIGHT_N = 0x2000, ///< Track right, direction north rubidium@7396: TRACKDIR_BIT_MASK = 0x3F3F, ///< Bitmask for bit-operations rubidium@7396: INVALID_TRACKDIR_BIT = 0xFFFF, ///< Flag for an invalid trackdirbit value rubidium@6248: }; matthijs@1942: rubidium@5587: /** Define basic enum properties */ rubidium@5587: template <> struct EnumPropsT : MakeEnumPropsT {}; rubidium@5587: typedef TinyEnumT TrackdirBitsShort; rubidium@5587: DECLARE_ENUM_AS_BIT_SET(TrackdirBits); rubidium@5587: celestar@2233: /** This struct contains all the info that is needed to draw and construct tracks. celestar@2233: */ rubidium@6248: struct RailtypeInfo { celestar@2274: /** Struct containing the main sprites. @note not all sprites are listed, but only celestar@2274: * the ones used directly in the code */ celestar@2233: struct { celestar@2233: SpriteID track_y; ///< single piece of rail in Y direction, with ground celestar@2233: SpriteID track_ns; ///< two pieces of rail in North and South corner (East-West direction) celestar@2233: SpriteID ground; ///< ground sprite for a 3-way switch celestar@2233: SpriteID single_y; ///< single piece of rail in Y direction, without ground celestar@2233: SpriteID single_x; ///< single piece of rail in X direction celestar@2233: SpriteID single_n; ///< single piece of rail in the northern corner celestar@2233: SpriteID single_s; ///< single piece of rail in the southern corner celestar@2233: SpriteID single_e; ///< single piece of rail in the eastern corner celestar@2233: SpriteID single_w; ///< single piece of rail in the western corner tron@2511: SpriteID crossing; ///< level crossing, rail in X direction tron@2511: SpriteID tunnel; ///< tunnel sprites base celestar@2233: } base_sprites; celestar@2233: celestar@2274: /** struct containing the sprites for the rail GUI. @note only sprites referred to celestar@2274: * directly in the code are listed */ celestar@2274: struct { celestar@2274: SpriteID build_ns_rail; ///< button for building single rail in N-S direction celestar@2274: SpriteID build_x_rail; ///< button for building single rail in X direction celestar@2274: SpriteID build_ew_rail; ///< button for building single rail in E-W direction celestar@2274: SpriteID build_y_rail; ///< button for building single rail in Y direction celestar@2274: SpriteID auto_rail; ///< button for the autorail construction celestar@2274: SpriteID build_depot; ///< button for building depots celestar@2274: SpriteID build_tunnel; ///< button for building a tunnel celestar@2274: SpriteID convert_rail; ///< button for converting rail celestar@2274: } gui_sprites; celestar@2274: celestar@2274: struct { rubidium@7396: CursorID rail_ns; ///< Cursor for building rail in N-S direction rubidium@7396: CursorID rail_swne; ///< Cursor for building rail in X direction rubidium@7396: CursorID rail_ew; ///< Cursor for building rail in E-W direction rubidium@7396: CursorID rail_nwse; ///< Cursor for building rail in Y direction rubidium@7396: CursorID autorail; ///< Cursor for autorail tool rubidium@7396: CursorID depot; ///< Cursor for building a depot rubidium@7396: CursorID tunnel; ///< Cursor for building a tunnel rubidium@7396: CursorID convert; ///< Cursor for converting track tron@2514: } cursor; tron@2514: tron@2514: struct { celestar@2274: StringID toolbar_caption; celestar@2274: } strings; celestar@2274: celestar@2233: /** sprite number difference between a piece of track on a snowy ground and the corresponding one on normal ground */ celestar@2233: SpriteID snow_offset; celestar@2233: celestar@3355: /** bitmask to the OTHER railtypes on which an engine of THIS railtype generates power */ celestar@3355: RailTypeMask powered_railtypes; celestar@3355: celestar@3355: /** bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel */ celestar@3355: RailTypeMask compatible_railtypes; celestar@2254: celestar@2254: /** celestar@2254: * Offset between the current railtype and normal rail. This means that:

celestar@2254: * 1) All the sprites in a railset MUST be in the same order. This order celestar@2254: * is determined by normal rail. Check sprites 1005 and following for this order

celestar@2254: * 2) The position where the railtype is loaded must always be the same, otherwise belugas@6420: * the offset will fail. celestar@2254: * @note: Something more flexible might be desirable in the future. celestar@2254: */ celestar@2254: SpriteID total_offset; celestar@2536: celestar@2536: /** tron@4077: * Bridge offset tron@4077: */ celestar@2536: SpriteID bridge_offset; peter1138@3503: peter1138@3503: /** peter1138@3503: * Offset to add to ground sprite when drawing custom waypoints / stations peter1138@3503: */ peter1138@3503: byte custom_ground_offset; rubidium@6248: }; celestar@2233: celestar@2233: belugas@6420: /** these are the maximums used for updating signal blocks, and checking if a depot is in a pbs block */ hackykid@2008: enum { belugas@6420: NUM_SSD_ENTRY = 256, ///< max amount of blocks belugas@6420: NUM_SSD_STACK = 32, ///< max amount of blocks to check recursively hackykid@2008: }; matthijs@1942: matthijs@1948: /** matthijs@1948: * Maps a Trackdir to the corresponding TrackdirBits value rubidium@7396: * @param trackdir the track direction to convert rubidium@7396: * @return the converted TrackdirBits value matthijs@1948: */ rubidium@7396: static inline TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir) rubidium@7396: { rubidium@7396: return (TrackdirBits)(1 << trackdir); rubidium@7396: } matthijs@1948: celestar@2232: /** rubidium@7396: * Removes first Track from TrackBits and returns it rubidium@7396: * rubidium@7396: * This function searchs for the first bit in the TrackBits, rubidium@7396: * remove this bit from the parameter and returns the found rubidium@7396: * bit as Track value. It returns INVALID_TRACK if the rubidium@7396: * parameter was TRACK_BIT_NONE or INVALID_TRACK_BIT. This rubidium@7396: * is basically used in while-loops to get up to 6 possible rubidium@7396: * tracks on a tile until the parameter becomes TRACK_BIT_NONE. rubidium@7396: * rubidium@7396: * @param tracks The value with the TrackBits rubidium@7396: * @return The first Track from the TrackBits value rubidium@7396: * @see FindFirstTrack rubidium@7396: */ KUDr@5598: static inline Track RemoveFirstTrack(TrackBits *tracks) rubidium@5587: { KUDr@5598: if (*tracks != TRACK_BIT_NONE && *tracks != INVALID_TRACK_BIT) { KUDr@5598: Track first = (Track)FIND_FIRST_BIT(*tracks); KUDr@6127: ClrBitT(*tracks, first); rubidium@5587: return first; rubidium@5587: } rubidium@5587: return INVALID_TRACK; rubidium@5587: } rubidium@5587: rubidium@5587: /** rubidium@7396: * Removes first Trackdir from TrackdirBits and returns it rubidium@7396: * rubidium@7396: * This function searchs for the first bit in the TrackdirBits parameter, rubidium@7396: * remove this bit from the parameter and returns the fnound bit as rubidium@7396: * Trackdir value. It returns INVALID_TRACKDIR if the trackdirs is rubidium@7396: * TRACKDIR_BIT_NONE or INVALID_TRACKDIR_BIT. This is basically used in a rubidium@7396: * while-loop to get all track-directions step by step until the value rubidium@7396: * reaches TRACKDIR_BIT_NONE. rubidium@7396: * rubidium@7396: * @param trackdirs The value with the TrackdirBits rubidium@7396: * @return The first Trackdir from the TrackdirBits value rubidium@7396: * @see FindFirstTrackdir rubidium@7396: */ KUDr@5598: static inline Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs) rubidium@5587: { KUDr@5598: if (*trackdirs != TRACKDIR_BIT_NONE && *trackdirs != INVALID_TRACKDIR_BIT) { KUDr@5598: Trackdir first = (Trackdir)FindFirstBit2x64(*trackdirs); KUDr@6127: ClrBitT(*trackdirs, first); rubidium@5587: return first; rubidium@5587: } rubidium@5587: return INVALID_TRACKDIR; rubidium@5587: } rubidium@5587: rubidium@5587: /** rubidium@7396: * Returns first Track from TrackBits or INVALID_TRACK rubidium@7396: * rubidium@7396: * This function returns the first Track found in the TrackBits value as Track-value. rubidium@7396: * It returns INVALID_TRACK if the parameter is TRACK_BIT_NONE or INVALID_TRACK_BIT. rubidium@7396: * rubidium@7396: * @param tracks The TrackBits value rubidium@7396: * @return The first Track found or INVALID_TRACK rubidium@7396: * @see RemoveFirstTrack rubidium@7396: */ rubidium@5587: static inline Track FindFirstTrack(TrackBits tracks) rubidium@5587: { rubidium@5587: return (tracks != TRACK_BIT_NONE && tracks != INVALID_TRACK_BIT) ? (Track)FIND_FIRST_BIT(tracks) : INVALID_TRACK; rubidium@5587: } rubidium@5587: rubidium@5587: /** rubidium@7396: * Converts TrackBits to Track. rubidium@7396: * rubidium@7396: * This function converts a TrackBits value to a Track value. As it rubidium@7396: * is not possible to convert two or more tracks to one track the rubidium@7396: * parameter must contain only one track or be the INVALID_TRACK_BIT value. rubidium@7396: * rubidium@7396: * @param tracks The TrackBits value to convert rubidium@7396: * @return The Track from the value or INVALID_TRACK rubidium@7396: * @pre tracks must contains only one Track or be INVALID_TRACK_BIT rubidium@7396: */ rubidium@5587: static inline Track TrackBitsToTrack(TrackBits tracks) rubidium@5587: { KUDr@7155: assert(tracks == INVALID_TRACK_BIT || (tracks != TRACK_BIT_NONE && KILL_FIRST_BIT(tracks & TRACK_BIT_MASK) == 0)); KUDr@7155: return tracks != INVALID_TRACK_BIT ? (Track)FIND_FIRST_BIT(tracks & TRACK_BIT_MASK) : INVALID_TRACK; rubidium@5587: } rubidium@5587: rubidium@5587: /** rubidium@7396: * Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR rubidium@7396: * rubidium@7396: * This function returns the first Trackdir in the given TrackdirBits value or rubidium@7396: * INVALID_TRACKDIR if the value is TRACKDIR_BIT_NONE. The TrackdirBits must rubidium@7396: * not be INVALID_TRACKDIR_BIT. rubidium@7396: * rubidium@7396: * @param trackdirs The TrackdirBits value rubidium@7396: * @return The first Trackdir from the TrackdirBits or INVALID_TRACKDIR on TRACKDIR_BIT_NONE. rubidium@7396: * @pre trackdirs must not be INVALID_TRACKDIR_BIT rubidium@7396: * @see RemoveFirstTrackdir rubidium@7396: */ rubidium@5587: static inline Trackdir FindFirstTrackdir(TrackdirBits trackdirs) rubidium@5587: { rubidium@5587: assert((trackdirs & ~TRACKDIR_BIT_MASK) == TRACKDIR_BIT_NONE); rubidium@5587: return (trackdirs != TRACKDIR_BIT_NONE) ? (Trackdir)FindFirstBit2x64(trackdirs) : INVALID_TRACKDIR; rubidium@5587: } rubidium@5587: rubidium@5587: /** rubidium@7396: * Checks if a Track is valid. rubidium@7396: * rubidium@7396: * @param track The value to check rubidium@7396: * @return true if the given value is a valid track. rubidium@7396: * @note Use this in an assert() matthijs@1942: */ rubidium@7396: static inline bool IsValidTrack(Track track) rubidium@7396: { rubidium@7396: return track < TRACK_END; rubidium@7396: } matthijs@1942: celestar@2232: /** rubidium@7396: * Checks if a Trackdir is valid. rubidium@7396: * rubidium@7396: * @param trackdir The value to check rubidium@7396: * @return true if the given valie is a valid Trackdir rubidium@7396: * @note Use this in an assert() rubidium@7396: */ rubidium@7396: static inline bool IsValidTrackdir(Trackdir trackdir) rubidium@7396: { rubidium@7396: return (TrackdirToTrackdirBits(trackdir) & TRACKDIR_BIT_MASK) != 0; rubidium@7396: } rubidium@7396: rubidium@7396: /* matthijs@1942: * Functions to map tracks to the corresponding bits in the signal matthijs@1942: * presence/status bytes in the map. You should not use these directly, but matthijs@1942: * wrapper functions below instead. XXX: Which are these? matthijs@1942: */ matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a trackdir to the bit that stores its status in the map arrays, in the matthijs@1942: * direction along with the trackdir. matthijs@1942: */ rubidium@6162: static inline byte SignalAlongTrackdir(Trackdir trackdir) rubidium@6162: { rubidium@6162: extern const byte _signal_along_trackdir[TRACKDIR_END]; rubidium@6162: return _signal_along_trackdir[trackdir]; rubidium@6162: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a trackdir to the bit that stores its status in the map arrays, in the matthijs@1942: * direction against the trackdir. matthijs@1942: */ rubidium@6162: static inline byte SignalAgainstTrackdir(Trackdir trackdir) rubidium@6162: { matthijs@1948: extern const byte _signal_against_trackdir[TRACKDIR_END]; matthijs@1948: return _signal_against_trackdir[trackdir]; matthijs@1948: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a Track to the bits that store the status of the two signals that can matthijs@1942: * be present on the given track. matthijs@1942: */ rubidium@6162: static inline byte SignalOnTrack(Track track) rubidium@6162: { matthijs@1948: extern const byte _signal_on_track[TRACK_END]; matthijs@1948: return _signal_on_track[track]; matthijs@1948: } matthijs@1942: matthijs@1942: matthijs@1942: /* matthijs@1942: * Functions describing logical relations between Tracks, TrackBits, Trackdirs matthijs@1942: * TrackdirBits, Direction and DiagDirections. matthijs@1942: */ matthijs@1942: matthijs@1942: /** matthijs@1944: * Maps a trackdir to the reverse trackdir. rubidium@7396: * rubidium@7396: * Returns the reverse trackdir of a Trackdir value. The reverse trackdir rubidium@7396: * is the same track with the other direction on it. rubidium@7396: * rubidium@7396: * @param trackdir The Trackdir value rubidium@7396: * @return The reverse trackdir rubidium@7396: * @pre trackdir must not be INVALID_TRACKDIR matthijs@1944: */ rubidium@5587: static inline Trackdir ReverseTrackdir(Trackdir trackdir) rubidium@5587: { rubidium@5587: assert(trackdir != INVALID_TRACKDIR); tron@3500: return (Trackdir)(trackdir ^ 8); matthijs@1948: } matthijs@1944: matthijs@2782: /** matthijs@2782: * Returns the Track that a given Trackdir represents rubidium@7396: * rubidium@7396: * This function filters the Track which is used in the Trackdir value and rubidium@7396: * returns it as a Track value. rubidium@7396: * rubidium@7396: * @param trackdir The trackdir value rubidium@7396: * @return The Track which is used in the value matthijs@2782: */ rubidium@7396: static inline Track TrackdirToTrack(Trackdir trackdir) rubidium@7396: { rubidium@7396: return (Track)(trackdir & 0x7); rubidium@7396: } matthijs@1944: matthijs@2782: /** rubidium@7396: * Returns a Trackdir for the given Track rubidium@7396: * rubidium@7396: * Since every Track corresponds to two Trackdirs, we choose the rubidium@7396: * one which points between NE and S. Note that the actual rubidium@7396: * implementation is quite futile, but this might change matthijs@1944: * in the future. rubidium@7396: * rubidium@7396: * @param track The given Track rubidium@7396: * @return The Trackdir from the given Track matthijs@1944: */ rubidium@7396: static inline Trackdir TrackToTrackdir(Track track) rubidium@7396: { rubidium@7396: return (Trackdir)track; rubidium@7396: } matthijs@1944: matthijs@2782: /** rubidium@7396: * Returns a TrackdirBit mask from a given Track rubidium@7396: * rubidium@7396: * The TrackdirBit mask contains the two TrackdirBits that matthijs@1944: * correspond with the given Track (one for each direction). rubidium@7396: * rubidium@7396: * @param track The track to get the TrackdirBits from rubidium@7396: * @return The TrackdirBits which the selected tracks matthijs@1944: */ tron@4077: static inline TrackdirBits TrackToTrackdirBits(Track track) tron@4077: { tron@4077: Trackdir td = TrackToTrackdir(track); tron@4077: return (TrackdirBits)(TrackdirToTrackdirBits(td) | TrackdirToTrackdirBits(ReverseTrackdir(td))); tron@4077: } matthijs@1944: matthijs@1944: /** rubidium@7396: * Discards all directional information from a TrackdirBits value rubidium@7396: * rubidium@7396: * Any Track which is present in either direction will be present in the result. rubidium@7396: * rubidium@7396: * @param bits The TrackdirBits to get the TrackBits from rubidium@7396: * @return The TrackBits matthijs@2782: */ tron@4077: static inline TrackBits TrackdirBitsToTrackBits(TrackdirBits bits) tron@4077: { matthijs@4281: return (TrackBits)((bits | (bits >> 8)) & TRACK_BIT_MASK); tron@4077: } matthijs@2782: matthijs@2782: /** matthijs@1942: * Maps a trackdir to the trackdir that you will end up on if you go straight rubidium@7396: * ahead. rubidium@7396: * rubidium@7396: * This will be the same trackdir for diagonal trackdirs, but a matthijs@1942: * different (alternating) one for straight trackdirs rubidium@7396: * rubidium@7396: * @param trackdir The given trackdir rubidium@7396: * @return The next Trackdir value of the next tile. matthijs@1942: */ tron@4077: static inline Trackdir NextTrackdir(Trackdir trackdir) tron@4077: { matthijs@1948: extern const Trackdir _next_trackdir[TRACKDIR_END]; matthijs@1948: return _next_trackdir[trackdir]; matthijs@1948: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a track to all tracks that make 90 deg turns with it. rubidium@7396: * rubidium@7396: * For the diagonal directions these are the complement of the rubidium@7396: * direction, for the straight directions these are the rubidium@7396: * two vertical or horizontal tracks, depend on the given direction rubidium@7396: * rubidium@7396: * @param track The given track rubidium@7396: * @return The TrackBits with the tracks marked which cross the given track by 90 deg. matthijs@1942: */ tron@4077: static inline TrackBits TrackCrossesTracks(Track track) tron@4077: { matthijs@1948: extern const TrackBits _track_crosses_tracks[TRACK_END]; matthijs@1948: return _track_crosses_tracks[track]; matthijs@1948: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a trackdir to the (4-way) direction the tile is exited when following matthijs@1942: * that trackdir. rubidium@7396: * rubidium@7396: * For the diagonal directions these are the same directions. For rubidium@7396: * the straight directions these are the directions from the imagined rubidium@7396: * base-tile to the bordering tile which will be joined if the given rubidium@7396: * straight direction is leaved from the base-tile. rubidium@7396: * rubidium@7396: * @param trackdir The given track direction rubidium@7396: * @return The direction which points to the resulting tile if following the Trackdir matthijs@1942: */ tron@4077: static inline DiagDirection TrackdirToExitdir(Trackdir trackdir) tron@4077: { matthijs@1948: extern const DiagDirection _trackdir_to_exitdir[TRACKDIR_END]; matthijs@1948: return _trackdir_to_exitdir[trackdir]; matthijs@1948: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a track and an (4-way) dir to the trackdir that represents the track matthijs@1942: * with the exit in the given direction. rubidium@7396: * rubidium@7396: * For the diagonal tracks the resulting track direction are clear for a given rubidium@7396: * DiagDirection. It either matches the direction or it returns INVALID_TRACKDIR, rubidium@7396: * as a TRACK_X cannot be applied with DIAG_SE. rubidium@7396: * For the straight tracks the resulting track direction will be the rubidium@7396: * direction which the DiagDirection is pointing. But this will be INVALID_TRACKDIR rubidium@7396: * if the DiagDirection is pointing 'away' the track. rubidium@7396: * rubidium@7396: * @param track The track to applie an direction on rubidium@7396: * @param diagdir The DiagDirection to applie on rubidium@7396: * @return The resulting track direction or INVALID_TRACKDIR if not possible. matthijs@1942: */ tron@4077: static inline Trackdir TrackExitdirToTrackdir(Track track, DiagDirection diagdir) tron@4077: { matthijs@1948: extern const Trackdir _track_exitdir_to_trackdir[TRACK_END][DIAGDIR_END]; matthijs@1948: return _track_exitdir_to_trackdir[track][diagdir]; matthijs@1948: } matthijs@1942: matthijs@1942: /** hackykid@2008: * Maps a track and an (4-way) dir to the trackdir that represents the track rubidium@7396: * with the entry in the given direction. rubidium@7396: * rubidium@7396: * For the diagonal tracks the return value is clear, its either the matching rubidium@7396: * track direction or INVALID_TRACKDIR. rubidium@7396: * For the straight tracks this returns the track direction which results if rubidium@7396: * you follow the DiagDirection and then turn by 45 deg left or right on the rubidium@7396: * next tile. The new direction on the new track will be the returning Trackdir rubidium@7396: * value. If the parameters makes no sense like the track TRACK_UPPER and the rubidium@7396: * diraction DIAGDIR_NE (target track cannot be reached) this function returns rubidium@7396: * INVALID_TRACKDIR. rubidium@7396: * rubidium@7396: * @param track The target track rubidium@7396: * @param diagdir The direction to "come from" rubidium@7396: * @return the resulting Trackdir or INVALID_TRACKDIR if not possible. hackykid@2008: */ tron@4077: static inline Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir) tron@4077: { hackykid@2008: extern const Trackdir _track_enterdir_to_trackdir[TRACK_END][DIAGDIR_END]; hackykid@2008: return _track_enterdir_to_trackdir[track][diagdir]; hackykid@2008: } hackykid@2008: hackykid@2008: /** matthijs@1942: * Maps a track and a full (8-way) direction to the trackdir that represents matthijs@1942: * the track running in the given direction. matthijs@1942: */ tron@4077: static inline Trackdir TrackDirectionToTrackdir(Track track, Direction dir) tron@4077: { matthijs@1948: extern const Trackdir _track_direction_to_trackdir[TRACK_END][DIR_END]; matthijs@1948: return _track_direction_to_trackdir[track][dir]; matthijs@1948: } matthijs@1942: matthijs@1942: /** matthijs@1942: * Maps a (4-way) direction to the diagonal trackdir that runs in that matthijs@1942: * direction. rubidium@7396: * rubidium@7396: * @param diagdir The direction rubidium@7396: * @return The resulting Trackdir direction matthijs@1942: */ tron@4077: static inline Trackdir DiagdirToDiagTrackdir(DiagDirection diagdir) tron@4077: { matthijs@1948: extern const Trackdir _dir_to_diag_trackdir[DIAGDIR_END]; matthijs@1948: return _dir_to_diag_trackdir[diagdir]; matthijs@1948: } matthijs@1942: matthijs@2782: /** matthijs@2782: * Returns all trackdirs that can be reached when entering a tile from a given rubidium@7396: * (diagonal) direction. rubidium@7396: * rubidium@7396: * This will obviously include 90 degree turns, since no information is available rubidium@7396: * about the exact angle of entering rubidium@7396: * rubidium@7396: * @param diagdir The joining direction rubidium@7396: * @return The TrackdirBits which can be used from the given direction rubidium@7396: * @see DiagdirReachesTracks rubidium@7396: */ rubidium@6162: static inline TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir) rubidium@6162: { rubidium@6162: extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END]; rubidium@6162: return _exitdir_reaches_trackdirs[diagdir]; rubidium@6162: } matthijs@2782: matthijs@2782: /** matthijs@2782: * Returns all tracks that can be reached when entering a tile from a given rubidium@7396: * (diagonal) direction. rubidium@7396: * rubidium@7396: * This will obviously include 90 degree turns, since no rubidium@7396: * information is available about the exact angle of entering rubidium@7396: * rubidium@7396: * @param diagdir The joining irection rubidium@7396: * @return The tracks which can be used rubidium@7396: * @see DiagdirReachesTrackdirs rubidium@7396: */ matthijs@2782: static inline TrackBits DiagdirReachesTracks(DiagDirection diagdir) { return TrackdirBitsToTrackBits(DiagdirReachesTrackdirs(diagdir)); } matthijs@2782: matthijs@1942: /** matthijs@1942: * Maps a trackdir to the trackdirs that can be reached from it (ie, when rubidium@7396: * entering the next tile. rubidium@7396: * rubidium@7396: * This will include 90 degree turns! rubidium@7396: * rubidium@7396: * @param trackdir The track direction which will be leaved rubidium@7396: * @return The track directions which can be used from this direction (in the next tile) matthijs@1942: */ rubidium@6162: static inline TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir) rubidium@6162: { rubidium@6162: extern const TrackdirBits _exitdir_reaches_trackdirs[DIAGDIR_END]; rubidium@6162: return _exitdir_reaches_trackdirs[TrackdirToExitdir(trackdir)]; rubidium@6162: } matthijs@1942: /* Note that there is no direct table for this function (there used to be), matthijs@1942: * but it uses two simpeler tables to achieve the result */ matthijs@2782: matthijs@1942: /** matthijs@1942: * Maps a trackdir to all trackdirs that make 90 deg turns with it. rubidium@7396: * rubidium@7396: * For the diagonal tracks this returns the track direction bits rubidium@7396: * of the other axis in both directions, which cannot be joined by rubidium@7396: * the given track direction. rubidium@7396: * For the straight tracks this returns all possible 90 deg turns rubidium@7396: * either on the current tile (which no train can joined) or on the rubidium@7396: * bordering tiles. rubidium@7396: * rubidium@7396: * @param trackdir The track direction rubidium@7396: * @return The TrackdirBits which are (more or less) 90 deg turns. matthijs@1942: */ rubidium@6162: static inline TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir) rubidium@6162: { matthijs@1948: extern const TrackdirBits _track_crosses_trackdirs[TRACKDIR_END]; matthijs@1948: return _track_crosses_trackdirs[TrackdirToTrack(trackdir)]; matthijs@1948: } matthijs@1942: rubidium@7396: /** rubidium@7396: * Checks if a given Track is diagonal rubidium@7396: * rubidium@7396: * @param track The given track to check rubidium@7396: * @return true if diagonal, else false rubidium@7396: */ rubidium@7396: static inline bool IsDiagonalTrack(Track track) rubidium@7396: { rubidium@7396: return (track == TRACK_X) || (track == TRACK_Y); rubidium@7396: } hackykid@2008: rubidium@7396: /** rubidium@7396: * Checks if a given Trackdir is diagonal. rubidium@7396: * rubidium@7396: * @param trackdir The given trackdir rubidium@7396: * @return true if the trackdir use a diagonal track rubidium@7396: */ rubidium@7396: static inline bool IsDiagonalTrackdir(Trackdir trackdir) rubidium@7396: { rubidium@7396: return IsDiagonalTrack(TrackdirToTrack(trackdir)); rubidium@7396: } matthijs@1967: matthijs@1967: /** celestar@2233: * Returns a pointer to the Railtype information for a given railtype celestar@2233: * @param railtype the rail type which the information is requested for celestar@2233: * @return The pointer to the RailtypeInfo celestar@2233: */ ludde@2236: static inline const RailtypeInfo *GetRailTypeInfo(RailType railtype) celestar@2233: { rubidium@6162: extern RailtypeInfo _railtypes[RAILTYPE_END]; celestar@2233: assert(railtype < RAILTYPE_END); ludde@2236: return &_railtypes[railtype]; celestar@2233: } celestar@2233: celestar@2233: /** matthijs@2006: * Checks if an engine of the given RailType can drive on a tile with a given matthijs@2006: * RailType. This would normally just be an equality check, but for electric matthijs@2006: * rails (which also support non-electric engines). matthijs@2006: * @return Whether the engine can drive on this tile. matthijs@2006: * @param enginetype The RailType of the engine we are considering. matthijs@2006: * @param tiletype The RailType of the tile we are considering. matthijs@2006: */ matthijs@2006: static inline bool IsCompatibleRail(RailType enginetype, RailType tiletype) matthijs@2006: { celestar@2233: return HASBIT(GetRailTypeInfo(enginetype)->compatible_railtypes, tiletype); matthijs@2006: } matthijs@2006: rubidium@7396: /** rubidium@7396: * Checks if an engine of the given RailType got power on a tile with a given rubidium@7396: * RailType. This would normally just be an equality check, but for electric rubidium@7396: * rails (which also support non-electric engines). rubidium@7396: * @return Whether the engine got power on this tile. rubidium@7396: * @param enginetype The RailType of the engine we are considering. rubidium@7396: * @param tiletype The RailType of the tile we are considering. rubidium@7396: */ celestar@3355: static inline bool HasPowerOnRail(RailType enginetype, RailType tiletype) celestar@3355: { celestar@3355: return HASBIT(GetRailTypeInfo(enginetype)->powered_railtypes, tiletype); celestar@3355: } celestar@3355: matthijs@2782: /** matthijs@2782: * Checks if the given tracks overlap, ie form a crossing. Basically this matthijs@2782: * means when there is more than one track on the tile, exept when there are matthijs@2782: * two parallel tracks. matthijs@2782: * @param bits The tracks present. matthijs@2782: * @return Whether the tracks present overlap in any way. matthijs@2782: */ matthijs@2782: static inline bool TracksOverlap(TrackBits bits) matthijs@2782: { tron@4077: /* With no, or only one track, there is no overlap */ tron@4077: if (bits == 0 || KILL_FIRST_BIT(bits) == 0) return false; tron@4077: /* We know that there are at least two tracks present. When there are more tron@4077: * than 2 tracks, they will surely overlap. When there are two, they will tron@4077: * always overlap unless they are lower & upper or right & left. */ tron@3258: return bits != TRACK_BIT_HORZ && bits != TRACK_BIT_VERT; matthijs@2782: } matthijs@2782: rubidium@7539: void *UpdateTrainPowerProc(Vehicle *v, void *data); tron@2520: void DrawTrainDepotSprite(int x, int y, int image, RailType railtype); tron@2520: void DrawDefaultWaypointSprite(int x, int y, RailType railtype); celestar@3355: celestar@3355: /** celestar@3355: * Draws overhead wires and pylons for electric railways. celestar@3355: * @param ti The TileInfo struct of the tile being drawn celestar@3355: * @see DrawCatenaryRailway celestar@3355: */ celestar@3355: void DrawCatenary(const TileInfo *ti); rubidium@7601: void DrawCatenaryOnTunnel(const TileInfo *ti); celestar@3355: rubidium@7335: Foundation GetRailFoundation(Slope tileh, TrackBits bits); KUDr@5116: KUDr@5116: int32 SettingsDisableElrail(int32 p1); ///< _patches.disable_elrail callback KUDr@5116: celestar@2536: #endif /* RAIL_H */