tron@3101: /* $Id$ */ tron@3101: belugas@6889: /** @file rail_map.h Hides the direct accesses to the map array with map accessors */ belugas@6889: tron@3101: #ifndef RAIL_MAP_H tron@3101: #define RAIL_MAP_H tron@3101: rubidium@8634: #include "rail_type.h" rubidium@8634: #include "signal_func.h" rubidium@8596: #include "direction_func.h" rubidium@8597: #include "track_func.h" rubidium@8604: #include "tile_map.h" belugas@9257: #include "signal_type.h" rubidium@9271: #include "waypoint_type.h" tron@3239: tron@3239: rubidium@7036: /** Different types of Rail-related tiles */ rubidium@6574: enum RailTileType { rubidium@7036: RAIL_TILE_NORMAL = 0, ///< Normal rail tile without signals rubidium@7036: RAIL_TILE_SIGNALS = 1, ///< Normal rail tile with signals rubidium@7036: RAIL_TILE_WAYPOINT = 2, ///< Waypoint (X or Y direction) rubidium@7036: RAIL_TILE_DEPOT = 3, ///< Depot (one entrance) rubidium@6574: }; tron@3239: rubidium@7036: /** rubidium@7036: * Returns the RailTileType (normal with or without signals, rubidium@7036: * waypoint or depot). rubidium@7036: * @param t the tile to get the information from rubidium@7036: * @pre IsTileType(t, MP_RAILWAY) rubidium@7036: * @return the RailTileType rubidium@7036: */ tron@3239: static inline RailTileType GetRailTileType(TileIndex t) tron@3239: { tron@3239: assert(IsTileType(t, MP_RAILWAY)); rubidium@6498: return (RailTileType)GB(_m[t].m5, 6, 2); tron@3239: } tron@3239: tron@3772: /** tron@3772: * Returns whether this is plain rails, with or without signals. Iow, if this rubidium@3792: * tiles RailTileType is RAIL_TILE_NORMAL or RAIL_TILE_SIGNALS. rubidium@7036: * @param t the tile to get the information from rubidium@7036: * @pre IsTileType(t, MP_RAILWAY) rubidium@7036: * @return true if and only if the tile is normal rail (with or without signals) tron@3772: */ rubidium@7036: static inline bool IsPlainRailTile(TileIndex t) tron@3772: { rubidium@7036: RailTileType rtt = GetRailTileType(t); rubidium@3792: return rtt == RAIL_TILE_NORMAL || rtt == RAIL_TILE_SIGNALS; tron@3772: } tron@3772: tron@3772: /** tron@3772: * Checks if a rail tile has signals. rubidium@7036: * @param t the tile to get the information from rubidium@7036: * @pre IsTileType(t, MP_RAILWAY) rubidium@7036: * @return true if and only if the tile has signals tron@3772: */ rubidium@7036: static inline bool HasSignals(TileIndex t) tron@3772: { rubidium@7036: return GetRailTileType(t) == RAIL_TILE_SIGNALS; tron@3772: } tron@3772: tron@3772: /** rubidium@6498: * Add/remove the 'has signal' bit from the RailTileType rubidium@7036: * @param tile the tile to add/remove the signals to/from rubidium@7036: * @param signals whether the rail tile should have signals or not rubidium@7036: * @pre IsPlainRailTile(tile) tron@3772: */ rubidium@6498: static inline void SetHasSignals(TileIndex tile, bool signals) tron@3772: { rubidium@6498: assert(IsPlainRailTile(tile)); rubidium@6498: SB(_m[tile].m5, 6, 1, signals); tron@3772: } tron@3772: rubidium@7036: /** rubidium@7036: * Is this tile a rail depot? rubidium@7036: * @param t the tile to get the information from rubidium@7036: * @pre IsTileType(t, MP_RAILWAY) rubidium@7036: * @return true if and only if the tile is a rail depot rubidium@7036: */ tron@4182: static inline bool IsRailDepot(TileIndex t) tron@4182: { rubidium@6498: return GetRailTileType(t) == RAIL_TILE_DEPOT; tron@4182: } tron@4182: rubidium@7036: /** rubidium@7036: * Is this tile a rail waypoint? rubidium@7036: * @param t the tile to get the information from rubidium@7036: * @pre IsTileType(t, MP_RAILWAY) rubidium@7036: * @return true if and only if the tile is a rail waypoint rubidium@7036: */ tron@4044: static inline bool IsRailWaypoint(TileIndex t) tron@4044: { rubidium@6498: return GetRailTileType(t) == RAIL_TILE_WAYPOINT; tron@4044: } tron@4044: tron@4044: rubidium@7036: /** rubidium@7036: * Gets the rail type of the given tile rubidium@7036: * @param t the tile to get the rail type from rubidium@7036: * @return the rail type of the tile rubidium@7036: */ tron@3239: static inline RailType GetRailType(TileIndex t) tron@3239: { tron@3239: return (RailType)GB(_m[t].m3, 0, 4); tron@3239: } tron@3239: rubidium@7036: /** glx@9128: * Sets the rail type of the given tile glx@9128: * @param t the tile to set the rail type of glx@9128: * @param r the new rail type for the tile rubidium@7036: */ tron@3242: static inline void SetRailType(TileIndex t, RailType r) tron@3242: { tron@3242: SB(_m[t].m3, 0, 4, r); tron@3242: } tron@3242: tron@3239: rubidium@7036: /** glx@9128: * Gets the track bits of the given tile glx@9128: * @param t the tile to get the track bits from glx@9128: * @return the track bits of the tile rubidium@7036: */ tron@3267: static inline TrackBits GetTrackBits(TileIndex tile) tron@3267: { tron@3267: return (TrackBits)GB(_m[tile].m5, 0, 6); tron@3267: } tron@3267: rubidium@7036: /** rubidium@7036: * Sets the track bits of the given tile rubidium@7036: * @param t the tile to set the track bits of rubidium@7036: * @param b the new track bits for the tile rubidium@7036: */ tron@3273: static inline void SetTrackBits(TileIndex t, TrackBits b) tron@3273: { tron@3273: SB(_m[t].m5, 0, 6, b); tron@3273: } tron@3273: tron@3772: /** rubidium@7036: * Returns whether the given track is present on the given tile. rubidium@7036: * @param tile the tile to check the track presence of rubidium@7036: * @param track the track to search for on the tile rubidium@7036: * @pre IsPlainRailTile(tile) rubidium@7036: * @return true if and only if the given track exists on the tile tron@3772: */ tron@3772: static inline bool HasTrack(TileIndex tile, Track track) tron@3772: { skidd13@8424: return HasBit(GetTrackBits(tile), track); tron@3772: } tron@3772: rubidium@7036: /** rubidium@7036: * Returns the direction the depot is facing to rubidium@7036: * @param t the tile to get the depot facing from rubidium@7036: * @pre IsRailDepotTile(t) rubidium@7036: * @return the direction the depot is facing rubidium@7036: */ tron@3185: static inline DiagDirection GetRailDepotDirection(TileIndex t) tron@3185: { tron@3185: return (DiagDirection)GB(_m[t].m5, 0, 2); tron@3185: } tron@3185: tron@4158: rubidium@7036: /** rubidium@7036: * Returns the axis of the waypoint rubidium@7036: * @param t the tile to get the waypoint axis from rubidium@7036: * @pre IsRailWaypointTile(t) rubidium@7036: * @return the axis of the waypoint rubidium@7036: */ tron@4158: static inline Axis GetWaypointAxis(TileIndex t) tron@4158: { tron@4474: return (Axis)GB(_m[t].m5, 0, 1); tron@4158: } tron@4158: rubidium@7036: /** rubidium@7036: * Returns the track of the waypoint rubidium@7036: * @param t the tile to get the waypoint track from rubidium@7036: * @pre IsRailWaypointTile(t) rubidium@7036: * @return the track of the waypoint rubidium@7036: */ celestar@3453: static inline Track GetRailWaypointTrack(TileIndex t) celestar@3448: { tron@4158: return AxisToTrack(GetWaypointAxis(t)); celestar@3448: } tron@3185: rubidium@7036: /** rubidium@7036: * Returns the track bits of the waypoint rubidium@7036: * @param t the tile to get the waypoint track bits from rubidium@7036: * @pre IsRailWaypointTile(t) rubidium@7036: * @return the track bits of the waypoint rubidium@7036: */ tron@3101: static inline TrackBits GetRailWaypointBits(TileIndex t) tron@3101: { tron@4158: return TrackToTrackBits(GetRailWaypointTrack(t)); celestar@3528: } celestar@3528: rubidium@7036: /** rubidium@7036: * Returns waypoint index (for the waypoint pool) rubidium@7036: * @param t the tile to get the waypoint index from rubidium@7036: * @pre IsRailWaypointTile(t) rubidium@7036: * @return the waypoint index rubidium@7036: */ rubidium@6508: static inline WaypointID GetWaypointIndex(TileIndex t) rubidium@6508: { rubidium@6508: return (WaypointID)_m[t].m2; rubidium@6508: } tron@3101: glx@7266: static inline SignalType GetSignalType(TileIndex t, Track track) tron@3238: { rubidium@3792: assert(GetRailTileType(t) == RAIL_TILE_SIGNALS); glx@7266: byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0; glx@7266: return (SignalType)GB(_m[t].m2, pos, 2); tron@3238: } tron@3238: glx@7266: static inline void SetSignalType(TileIndex t, Track track, SignalType s) celestar@3521: { glx@7266: assert(GetRailTileType(t) == RAIL_TILE_SIGNALS); glx@7266: byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 0; glx@7266: SB(_m[t].m2, pos, 2, s); glx@7266: if (track == INVALID_TRACK) SB(_m[t].m2, 4, 2, s); celestar@3521: } celestar@3521: glx@7266: static inline bool IsPresignalEntry(TileIndex t, Track track) celestar@3521: { glx@7266: return GetSignalType(t, track) == SIGTYPE_ENTRY || GetSignalType(t, track) == SIGTYPE_COMBO; glx@7266: } glx@7266: glx@7266: static inline bool IsPresignalExit(TileIndex t, Track track) glx@7266: { glx@7266: return GetSignalType(t, track) == SIGTYPE_EXIT || GetSignalType(t, track) == SIGTYPE_COMBO; celestar@3521: } celestar@3521: celestar@3522: static inline void CycleSignalSide(TileIndex t, Track track) celestar@3522: { celestar@3522: byte sig; glx@7266: byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 4 : 6; celestar@3522: celestar@3522: sig = GB(_m[t].m3, pos, 2); celestar@3522: if (--sig == 0) sig = 3; celestar@3522: SB(_m[t].m3, pos, 2, sig); celestar@3522: } celestar@3522: glx@7266: static inline SignalVariant GetSignalVariant(TileIndex t, Track track) tron@3237: { glx@7266: byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 6 : 2; glx@7266: return (SignalVariant)GB(_m[t].m2, pos, 1); tron@3237: } tron@3237: glx@7266: static inline void SetSignalVariant(TileIndex t, Track track, SignalVariant v) tron@3237: { glx@7266: byte pos = (track == TRACK_LOWER || track == TRACK_RIGHT) ? 6 : 2; glx@7266: SB(_m[t].m2, pos, 1, v); glx@7266: if (track == INVALID_TRACK) SB(_m[t].m2, 6, 1, v); tron@3237: } tron@3237: celestar@3575: /** These are states in which a signal can be. Currently these are only two, so celestar@3575: * simple boolean logic will do. But do try to compare to this enum instead of celestar@3575: * normal boolean evaluation, since that will make future additions easier. celestar@3575: */ rubidium@6574: enum SignalState { rubidium@7036: SIGNAL_STATE_RED = 0, ///< The signal is red rubidium@7036: SIGNAL_STATE_GREEN = 1, ///< The signal is green rubidium@6574: }; celestar@3575: rubidium@7249: /** rubidium@7249: * Set the states of the signals (Along/AgainstTrackDir) rubidium@7249: * @param tile the tile to set the states for rubidium@7249: * @param state the new state rubidium@7249: */ rubidium@7249: static inline void SetSignalStates(TileIndex tile, uint state) rubidium@7249: { glx@7266: SB(_m[tile].m4, 4, 4, state); rubidium@7249: } rubidium@7249: rubidium@7249: /** rubidium@7249: * Set the states of the signals (Along/AgainstTrackDir) rubidium@7249: * @param tile the tile to set the states for rubidium@7249: * @param state the new state rubidium@7249: */ rubidium@7249: static inline uint GetSignalStates(TileIndex tile) rubidium@7249: { glx@7266: return GB(_m[tile].m4, 4, 4); rubidium@7249: } rubidium@7249: rubidium@7249: /** rubidium@7249: * Get the state of a single signal rubidium@7249: * @param t the tile to get the signal state for rubidium@7249: * @param signalbit the signal rubidium@7249: * @return the state of the signal rubidium@7249: */ celestar@3575: static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit) celestar@3575: { skidd13@8424: return (SignalState)HasBit(GetSignalStates(t), signalbit); celestar@3575: } celestar@3575: rubidium@7249: /** rubidium@7249: * Set whether the given signals are present (Along/AgainstTrackDir) rubidium@7249: * @param tile the tile to set the present signals for rubidium@7249: * @param signals the signals that have to be present rubidium@7249: */ rubidium@7249: static inline void SetPresentSignals(TileIndex tile, uint signals) rubidium@7249: { rubidium@7249: SB(_m[tile].m3, 4, 4, signals); rubidium@7249: } rubidium@7249: rubidium@7249: /** rubidium@7249: * Get whether the given signals are present (Along/AgainstTrackDir) rubidium@7249: * @param tile the tile to get the present signals for rubidium@7249: * @return the signals that are present rubidium@7249: */ rubidium@7249: static inline uint GetPresentSignals(TileIndex tile) rubidium@7249: { rubidium@7249: return GB(_m[tile].m3, 4, 4); rubidium@7249: } rubidium@7249: rubidium@7249: /** rubidium@7249: * Checks whether the given signals is present rubidium@7249: * @param t the tile to check on rubidium@7249: * @param signalbit the signal rubidium@7249: * @return true if and only if the signal is present rubidium@7249: */ rubidium@7249: static inline bool IsSignalPresent(TileIndex t, byte signalbit) rubidium@7249: { skidd13@8424: return HasBit(GetPresentSignals(t), signalbit); rubidium@7249: } tron@3237: tron@4041: /** tron@4041: * Checks for the presence of signals (either way) on the given track on the tron@4041: * given rail tile. tron@4041: */ tron@4041: static inline bool HasSignalOnTrack(TileIndex tile, Track track) tron@4041: { tron@4041: assert(IsValidTrack(track)); tron@4041: return tron@4041: GetRailTileType(tile) == RAIL_TILE_SIGNALS && rubidium@7249: (GetPresentSignals(tile) & SignalOnTrack(track)) != 0; tron@4041: } tron@4041: tron@4041: /** tron@4041: * Checks for the presence of signals along the given trackdir on the given tron@4041: * rail tile. tron@4041: * tron@4041: * Along meaning if you are currently driving on the given trackdir, this is tron@4041: * the signal that is facing us (for which we stop when it's red). tron@4041: */ tron@4041: static inline bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir) tron@4041: { tron@4041: assert (IsValidTrackdir(trackdir)); tron@4041: return tron@4041: GetRailTileType(tile) == RAIL_TILE_SIGNALS && rubidium@7249: GetPresentSignals(tile) & SignalAlongTrackdir(trackdir); tron@4041: } tron@4041: tron@4041: /** tron@4041: * Gets the state of the signal along the given trackdir. tron@4041: * tron@4041: * Along meaning if you are currently driving on the given trackdir, this is tron@4041: * the signal that is facing us (for which we stop when it's red). tron@4041: */ tron@4041: static inline SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir) tron@4041: { tron@4041: assert(IsValidTrackdir(trackdir)); tron@4041: assert(HasSignalOnTrack(tile, TrackdirToTrack(trackdir))); rubidium@7249: return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ? tron@4041: SIGNAL_STATE_GREEN : SIGNAL_STATE_RED; tron@4041: } tron@4041: smatz@8734: /** smatz@8734: * Sets the state of the signal along the given trackdir. smatz@8734: */ smatz@8734: static inline void SetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir, SignalState state) smatz@8734: { smatz@8734: if (state == SIGNAL_STATE_GREEN) { // set 1 smatz@8734: SetSignalStates(tile, GetSignalStates(tile) | SignalAlongTrackdir(trackdir)); smatz@8734: } else { smatz@8734: SetSignalStates(tile, GetSignalStates(tile) & ~SignalAlongTrackdir(trackdir)); smatz@8734: } smatz@8734: } smatz@8734: tron@4041: tron@4041: /** tron@4041: * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile. tron@4041: */ tron@6480: RailType GetTileRailType(TileIndex tile); tron@4041: rubidium@7036: /** The ground 'under' the rail */ rubidium@6574: enum RailGroundType { rubidium@7036: RAIL_GROUND_BARREN = 0, ///< Nothing (dirt) rubidium@7036: RAIL_GROUND_GRASS = 1, ///< Grassy rubidium@7036: RAIL_GROUND_FENCE_NW = 2, ///< Grass with a fence at the NW edge rubidium@7036: RAIL_GROUND_FENCE_SE = 3, ///< Grass with a fence at the SE edge rubidium@7036: RAIL_GROUND_FENCE_SENW = 4, ///< Grass with a fence at the NW and SE edges rubidium@7036: RAIL_GROUND_FENCE_NE = 5, ///< Grass with a fence at the NE edge rubidium@7036: RAIL_GROUND_FENCE_SW = 6, ///< Grass with a fence at the SW edge rubidium@7036: RAIL_GROUND_FENCE_NESW = 7, ///< Grass with a fence at the NE and SW edges truelight@7803: RAIL_GROUND_FENCE_VERT1 = 8, ///< Grass with a fence at the eastern side truelight@7803: RAIL_GROUND_FENCE_VERT2 = 9, ///< Grass with a fence at the western side rubidium@7036: RAIL_GROUND_FENCE_HORIZ1 = 10, ///< Grass with a fence at the southern side rubidium@7036: RAIL_GROUND_FENCE_HORIZ2 = 11, ///< Grass with a fence at the northern side rubidium@7036: RAIL_GROUND_ICE_DESERT = 12, ///< Icy or sandy frosch@8910: RAIL_GROUND_WATER = 13, ///< Grass with a fence and shore or water on the free halftile smatz@9019: RAIL_GROUND_HALF_SNOW = 14, ///< Snow only on higher part of slope (steep or one corner raised) rubidium@6574: }; celestar@3523: celestar@3523: static inline void SetRailGroundType(TileIndex t, RailGroundType rgt) celestar@3523: { rubidium@6498: SB(_m[t].m4, 0, 4, rgt); celestar@3523: } celestar@3523: celestar@3523: static inline RailGroundType GetRailGroundType(TileIndex t) celestar@3523: { rubidium@6498: return (RailGroundType)GB(_m[t].m4, 0, 4); celestar@3523: } celestar@3523: celestar@3523: static inline bool IsSnowRailGround(TileIndex t) celestar@3523: { celestar@3523: return GetRailGroundType(t) == RAIL_GROUND_ICE_DESERT; celestar@3523: } celestar@3523: celestar@3523: tron@3101: static inline void MakeRailNormal(TileIndex t, Owner o, TrackBits b, RailType r) tron@3101: { tron@3101: SetTileType(t, MP_RAILWAY); tron@3101: SetTileOwner(t, o); tron@3101: _m[t].m2 = 0; tron@3101: _m[t].m3 = r; tron@3101: _m[t].m4 = 0; rubidium@6498: _m[t].m5 = RAIL_TILE_NORMAL << 6 | b; tron@3101: } tron@3101: tron@3101: tron@3101: static inline void MakeRailDepot(TileIndex t, Owner o, DiagDirection d, RailType r) tron@3101: { tron@3101: SetTileType(t, MP_RAILWAY); tron@3101: SetTileOwner(t, o); tron@3101: _m[t].m2 = 0; tron@3101: _m[t].m3 = r; tron@3101: _m[t].m4 = 0; rubidium@6498: _m[t].m5 = RAIL_TILE_DEPOT << 6 | d; tron@3101: } tron@3101: tron@3101: tron@3101: static inline void MakeRailWaypoint(TileIndex t, Owner o, Axis a, RailType r, uint index) tron@3101: { tron@3101: SetTileType(t, MP_RAILWAY); tron@3101: SetTileOwner(t, o); tron@3101: _m[t].m2 = index; tron@3101: _m[t].m3 = r; tron@3101: _m[t].m4 = 0; rubidium@6498: _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a; tron@3101: } tron@3101: peter1138@4666: #endif /* RAIL_MAP_H */