tron@3101: /* $Id$ */ tron@3101: belugas@6393: /** @file rail_map.h Hides the direct accesses to the map array with map accessors */ belugas@6393: tron@3101: #ifndef RAIL_MAP_H tron@3101: #define RAIL_MAP_H tron@3101: tron@3239: #include "direction.h" tron@4041: #include "rail.h" tron@3101: #include "tile.h" tron@3239: tron@3239: rubidium@6540: /** Different types of Rail-related tiles */ rubidium@6248: enum RailTileType { rubidium@6540: RAIL_TILE_NORMAL = 0, ///< Normal rail tile without signals rubidium@6540: RAIL_TILE_SIGNALS = 1, ///< Normal rail tile with signals rubidium@6540: RAIL_TILE_WAYPOINT = 2, ///< Waypoint (X or Y direction) rubidium@6540: RAIL_TILE_DEPOT = 3, ///< Depot (one entrance) rubidium@6248: }; tron@3239: rubidium@6540: /** rubidium@6540: * Returns the RailTileType (normal with or without signals, rubidium@6540: * waypoint or depot). rubidium@6540: * @param t the tile to get the information from rubidium@6540: * @pre IsTileType(t, MP_RAILWAY) rubidium@6540: * @return the RailTileType rubidium@6540: */ tron@3239: static inline RailTileType GetRailTileType(TileIndex t) tron@3239: { tron@3239: assert(IsTileType(t, MP_RAILWAY)); rubidium@6172: 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@6540: * @param t the tile to get the information from rubidium@6540: * @pre IsTileType(t, MP_RAILWAY) rubidium@6540: * @return true if and only if the tile is normal rail (with or without signals) tron@3772: */ rubidium@6540: static inline bool IsPlainRailTile(TileIndex t) tron@3772: { rubidium@6540: 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@6540: * @param t the tile to get the information from rubidium@6540: * @pre IsTileType(t, MP_RAILWAY) rubidium@6540: * @return true if and only if the tile has signals tron@3772: */ rubidium@6540: static inline bool HasSignals(TileIndex t) tron@3772: { rubidium@6540: return GetRailTileType(t) == RAIL_TILE_SIGNALS; tron@3772: } tron@3772: tron@3772: /** rubidium@6172: * Add/remove the 'has signal' bit from the RailTileType rubidium@6540: * @param tile the tile to add/remove the signals to/from rubidium@6540: * @param signals whether the rail tile should have signals or not rubidium@6540: * @pre IsPlainRailTile(tile) tron@3772: */ rubidium@6172: static inline void SetHasSignals(TileIndex tile, bool signals) tron@3772: { rubidium@6172: assert(IsPlainRailTile(tile)); rubidium@6172: SB(_m[tile].m5, 6, 1, signals); tron@3772: } tron@3772: rubidium@6540: /** rubidium@6540: * Is this tile a rail depot? rubidium@6540: * @param t the tile to get the information from rubidium@6540: * @pre IsTileType(t, MP_RAILWAY) rubidium@6540: * @return true if and only if the tile is a rail depot rubidium@6540: */ tron@4182: static inline bool IsRailDepot(TileIndex t) tron@4182: { rubidium@6172: return GetRailTileType(t) == RAIL_TILE_DEPOT; tron@4182: } tron@4182: rubidium@6540: /** rubidium@6540: * Is this tile a rail waypoint? rubidium@6540: * @param t the tile to get the information from rubidium@6540: * @pre IsTileType(t, MP_RAILWAY) rubidium@6540: * @return true if and only if the tile is a rail waypoint rubidium@6540: */ tron@4044: static inline bool IsRailWaypoint(TileIndex t) tron@4044: { rubidium@6172: return GetRailTileType(t) == RAIL_TILE_WAYPOINT; tron@4044: } tron@4044: tron@4044: rubidium@6540: /** rubidium@6540: * Gets the rail type of the given tile rubidium@6540: * @param t the tile to get the rail type from rubidium@6540: * @return the rail type of the tile rubidium@6540: */ 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@6540: /** rubidium@6540: * Sets the track bits of the given tile rubidium@6540: * @param t the tile to set the track bits of rubidium@6540: * @param r the new track bits for the tile rubidium@6540: */ 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@6540: /** rubidium@6540: * Gets the rail type of the given tile rubidium@6540: * @param t the tile to get the rail type from rubidium@6540: * @return the rail type of the tile rubidium@6540: */ 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@6540: /** rubidium@6540: * Sets the track bits of the given tile rubidium@6540: * @param t the tile to set the track bits of rubidium@6540: * @param b the new track bits for the tile rubidium@6540: */ 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@6540: * Returns whether the given track is present on the given tile. rubidium@6540: * @param tile the tile to check the track presence of rubidium@6540: * @param track the track to search for on the tile rubidium@6540: * @pre IsPlainRailTile(tile) rubidium@6540: * @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: { tron@3772: return HASBIT(GetTrackBits(tile), track); tron@3772: } tron@3772: rubidium@6540: /** rubidium@6540: * Returns the direction the depot is facing to rubidium@6540: * @param t the tile to get the depot facing from rubidium@6540: * @pre IsRailDepotTile(t) rubidium@6540: * @return the direction the depot is facing rubidium@6540: */ 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@6540: /** rubidium@6540: * Returns the axis of the waypoint rubidium@6540: * @param t the tile to get the waypoint axis from rubidium@6540: * @pre IsRailWaypointTile(t) rubidium@6540: * @return the axis of the waypoint rubidium@6540: */ 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@6540: /** rubidium@6540: * Returns the track of the waypoint rubidium@6540: * @param t the tile to get the waypoint track from rubidium@6540: * @pre IsRailWaypointTile(t) rubidium@6540: * @return the track of the waypoint rubidium@6540: */ celestar@3453: static inline Track GetRailWaypointTrack(TileIndex t) celestar@3448: { tron@4158: return AxisToTrack(GetWaypointAxis(t)); celestar@3448: } tron@3185: rubidium@6540: /** rubidium@6540: * Returns the track bits of the waypoint rubidium@6540: * @param t the tile to get the waypoint track bits from rubidium@6540: * @pre IsRailWaypointTile(t) rubidium@6540: * @return the track bits of the waypoint rubidium@6540: */ tron@3101: static inline TrackBits GetRailWaypointBits(TileIndex t) tron@3101: { tron@4158: return TrackToTrackBits(GetRailWaypointTrack(t)); celestar@3528: } celestar@3528: rubidium@6540: /** rubidium@6540: * Returns waypoint index (for the waypoint pool) rubidium@6540: * @param t the tile to get the waypoint index from rubidium@6540: * @pre IsRailWaypointTile(t) rubidium@6540: * @return the waypoint index rubidium@6540: */ rubidium@6182: static inline WaypointID GetWaypointIndex(TileIndex t) rubidium@6182: { rubidium@6182: return (WaypointID)_m[t].m2; rubidium@6182: } tron@3101: rubidium@6540: /** Type of signal, i.e. how does the signal behave? */ rubidium@6248: enum SignalType { rubidium@6540: SIGTYPE_NORMAL = 0, ///< normal signal rubidium@6540: SIGTYPE_ENTRY = 1, ///< presignal block entry rubidium@6540: SIGTYPE_EXIT = 2, ///< presignal block exit rubidium@6540: SIGTYPE_COMBO = 3 ///< presignal inter-block rubidium@6248: }; tron@3238: tron@3238: static inline SignalType GetSignalType(TileIndex t) tron@3238: { rubidium@3792: assert(GetRailTileType(t) == RAIL_TILE_SIGNALS); rubidium@6172: return (SignalType)GB(_m[t].m2, 0, 2); tron@3238: } tron@3238: tron@3238: static inline void SetSignalType(TileIndex t, SignalType s) tron@3238: { rubidium@3792: assert(GetRailTileType(t) == RAIL_TILE_SIGNALS); rubidium@6172: SB(_m[t].m2, 0, 2, s); tron@3238: } tron@3238: celestar@3521: static inline bool IsPresignalEntry(TileIndex t) celestar@3521: { celestar@3521: return GetSignalType(t) == SIGTYPE_ENTRY || GetSignalType(t) == SIGTYPE_COMBO; celestar@3521: } celestar@3521: celestar@3521: static inline bool IsPresignalExit(TileIndex t) celestar@3521: { celestar@3521: return GetSignalType(t) == SIGTYPE_EXIT || GetSignalType(t) == SIGTYPE_COMBO; celestar@3521: } celestar@3521: celestar@3522: static inline void CycleSignalSide(TileIndex t, Track track) celestar@3522: { celestar@3522: byte sig; celestar@3522: byte pos = 6; celestar@3522: if (track == TRACK_LOWER || track == TRACK_RIGHT) pos = 4; 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: rubidium@6540: /** Variant of the signal, i.e. how does the signal look? */ rubidium@6248: enum SignalVariant { rubidium@6540: SIG_ELECTRIC = 0, ///< Light signal rubidium@6540: SIG_SEMAPHORE = 1 ///< Old-fashioned semaphore signal rubidium@6248: }; tron@3237: tron@3237: static inline SignalVariant GetSignalVariant(TileIndex t) tron@3237: { rubidium@6172: return (SignalVariant)GB(_m[t].m2, 2, 1); tron@3237: } tron@3237: tron@3237: static inline void SetSignalVariant(TileIndex t, SignalVariant v) tron@3237: { rubidium@6172: SB(_m[t].m2, 2, 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@6248: enum SignalState { rubidium@6540: SIGNAL_STATE_RED = 0, ///< The signal is red rubidium@6540: SIGNAL_STATE_GREEN = 1, ///< The signal is green rubidium@6248: }; celestar@3575: rubidium@6753: /** rubidium@6753: * Set the states of the signals (Along/AgainstTrackDir) rubidium@6753: * @param tile the tile to set the states for rubidium@6753: * @param state the new state rubidium@6753: */ rubidium@6753: static inline void SetSignalStates(TileIndex tile, uint state) rubidium@6753: { rubidium@6753: SB(_m[tile].m2, 4, 4, state); rubidium@6753: } rubidium@6753: rubidium@6753: /** rubidium@6753: * Set the states of the signals (Along/AgainstTrackDir) rubidium@6753: * @param tile the tile to set the states for rubidium@6753: * @param state the new state rubidium@6753: */ rubidium@6753: static inline uint GetSignalStates(TileIndex tile) rubidium@6753: { rubidium@6753: return GB(_m[tile].m2, 4, 4); rubidium@6753: } rubidium@6753: rubidium@6753: /** rubidium@6753: * Get the state of a single signal rubidium@6753: * @param t the tile to get the signal state for rubidium@6753: * @param signalbit the signal rubidium@6753: * @return the state of the signal rubidium@6753: */ celestar@3575: static inline SignalState GetSingleSignalState(TileIndex t, byte signalbit) celestar@3575: { rubidium@6753: return (SignalState)HASBIT(GetSignalStates(t), signalbit); celestar@3575: } celestar@3575: rubidium@6753: /** rubidium@6753: * Set whether the given signals are present (Along/AgainstTrackDir) rubidium@6753: * @param tile the tile to set the present signals for rubidium@6753: * @param signals the signals that have to be present rubidium@6753: */ rubidium@6753: static inline void SetPresentSignals(TileIndex tile, uint signals) rubidium@6753: { rubidium@6753: SB(_m[tile].m3, 4, 4, signals); rubidium@6753: } rubidium@6753: rubidium@6753: /** rubidium@6753: * Get whether the given signals are present (Along/AgainstTrackDir) rubidium@6753: * @param tile the tile to get the present signals for rubidium@6753: * @return the signals that are present rubidium@6753: */ rubidium@6753: static inline uint GetPresentSignals(TileIndex tile) rubidium@6753: { rubidium@6753: return GB(_m[tile].m3, 4, 4); rubidium@6753: } rubidium@6753: rubidium@6753: /** rubidium@6753: * Checks whether the given signals is present rubidium@6753: * @param t the tile to check on rubidium@6753: * @param signalbit the signal rubidium@6753: * @return true if and only if the signal is present rubidium@6753: */ rubidium@6753: static inline bool IsSignalPresent(TileIndex t, byte signalbit) rubidium@6753: { rubidium@6753: return HASBIT(GetPresentSignals(t), signalbit); rubidium@6753: } 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@6753: (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@6753: 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@6753: return GetSignalStates(tile) & SignalAlongTrackdir(trackdir) ? tron@4041: SIGNAL_STATE_GREEN : SIGNAL_STATE_RED; tron@4041: } tron@4041: tron@4041: tron@4041: /** tron@4041: * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile. tron@4041: */ tron@6154: RailType GetTileRailType(TileIndex tile); tron@4041: rubidium@6540: /** The ground 'under' the rail */ rubidium@6248: enum RailGroundType { rubidium@6540: RAIL_GROUND_BARREN = 0, ///< Nothing (dirt) rubidium@6540: RAIL_GROUND_GRASS = 1, ///< Grassy rubidium@6540: RAIL_GROUND_FENCE_NW = 2, ///< Grass with a fence at the NW edge rubidium@6540: RAIL_GROUND_FENCE_SE = 3, ///< Grass with a fence at the SE edge rubidium@6540: RAIL_GROUND_FENCE_SENW = 4, ///< Grass with a fence at the NW and SE edges rubidium@6540: RAIL_GROUND_FENCE_NE = 5, ///< Grass with a fence at the NE edge rubidium@6540: RAIL_GROUND_FENCE_SW = 6, ///< Grass with a fence at the SW edge rubidium@6540: RAIL_GROUND_FENCE_NESW = 7, ///< Grass with a fence at the NE and SW edges rubidium@6540: RAIL_GROUND_FENCE_VERT1 = 8, ///< Grass with a fence at the western side rubidium@6540: RAIL_GROUND_FENCE_VERT2 = 9, ///< Grass with a fence at the eastern side rubidium@6540: RAIL_GROUND_FENCE_HORIZ1 = 10, ///< Grass with a fence at the southern side rubidium@6540: RAIL_GROUND_FENCE_HORIZ2 = 11, ///< Grass with a fence at the northern side rubidium@6540: RAIL_GROUND_ICE_DESERT = 12, ///< Icy or sandy rubidium@6248: }; celestar@3523: celestar@3523: static inline void SetRailGroundType(TileIndex t, RailGroundType rgt) celestar@3523: { rubidium@6172: SB(_m[t].m4, 0, 4, rgt); celestar@3523: } celestar@3523: celestar@3523: static inline RailGroundType GetRailGroundType(TileIndex t) celestar@3523: { rubidium@6172: 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@6172: _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@6172: _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@6172: _m[t].m5 = RAIL_TILE_WAYPOINT << 6 | a; tron@3101: } tron@3101: peter1138@4666: #endif /* RAIL_MAP_H */