summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-08-02 22:47:20 +0000
committerrubidium <rubidium@openttd.org>2008-08-02 22:47:20 +0000
commitec7cc498254cccf3e1d150e6d978b7cf2b89eb89 (patch)
tree8771ffd53d04267f920a1a780c439b1f8f8c63de /src
parentea570c81600edf40dfa6876d20e5e3a6ededb3d3 (diff)
downloadopenttd-ec7cc498254cccf3e1d150e6d978b7cf2b89eb89.tar.xz
(svn r13926) -Add [YAPP]: Add map accessors for path reservations. (michi_cc)
Diffstat (limited to 'src')
-rw-r--r--src/rail_map.h81
-rw-r--r--src/road_map.h37
-rw-r--r--src/station_map.h36
-rw-r--r--src/track_func.h15
-rw-r--r--src/tunnelbridge_map.h39
5 files changed, 208 insertions, 0 deletions
diff --git a/src/rail_map.h b/src/rail_map.h
index 8c436d055..56eecc473 100644
--- a/src/rail_map.h
+++ b/src/rail_map.h
@@ -224,6 +224,87 @@ static inline WaypointID GetWaypointIndex(TileIndex t)
return (WaypointID)_m[t].m2;
}
+
+/**
+ * Returns the reserved track bits of the tile
+ * @pre IsPlainRailTile(t)
+ * @param t the tile to query
+ * @return the track bits
+ */
+static inline TrackBits GetTrackReservation(TileIndex t)
+{
+ assert(IsPlainRailTile(t));
+ byte track_b = GB(_m[t].m2, 8, 3);
+ Track track = (Track)(track_b - 1); // map array saves Track+1
+ if (track_b == 0) return TRACK_BIT_NONE;
+ return (TrackBits)(TrackToTrackBits(track) | (HasBit(_m[t].m2, 11) ? TrackToTrackBits(TrackToOppositeTrack(track)) : 0));
+}
+
+/**
+ * Sets the reserved track bits of the tile
+ * @pre IsPlainRailTile(t) && !TracksOverlap(b)
+ * @param t the tile to change
+ * @param b the track bits
+ */
+static inline void SetTrackReservation(TileIndex t, TrackBits b)
+{
+ assert(IsPlainRailTile(t));
+ assert(b != INVALID_TRACK_BIT);
+ assert(!TracksOverlap(b));
+ Track track = RemoveFirstTrack(&b);
+ SB(_m[t].m2, 8, 3, track == INVALID_TRACK ? 0 : track+1);
+ SB(_m[t].m2, 11, 1, (byte)(b != TRACK_BIT_NONE));
+}
+
+/**
+ * Get the reservation state of the waypoint or depot
+ * @note Works for both waypoints and rail depots
+ * @pre IsRailWaypoint(t) || IsRailDepot(t)
+ * @param t the waypoint/depot tile
+ * @return reservation state
+ */
+static inline bool GetDepotWaypointReservation(TileIndex t)
+{
+ assert(IsRailWaypoint(t) || IsRailDepot(t));
+ return HasBit(_m[t].m5, 4);
+}
+
+/**
+ * Set the reservation state of the waypoint or depot
+ * @note Works for both waypoints and rail depots
+ * @pre IsRailWaypoint(t) || IsRailDepot(t)
+ * @param t the waypoint/depot tile
+ * @param b the reservation state
+ */
+static inline void SetDepotWaypointReservation(TileIndex t, bool b)
+{
+ assert(IsRailWaypoint(t) || IsRailDepot(t));
+ SB(_m[t].m5, 4, 1, (byte)b);
+}
+
+/**
+ * Get the reserved track bits for a waypoint
+ * @pre IsRailWaypoint(t)
+ * @param t the tile
+ * @return reserved track bits
+ */
+static inline TrackBits GetRailWaypointReservation(TileIndex t)
+{
+ return GetDepotWaypointReservation(t) ? GetRailWaypointBits(t) : TRACK_BIT_NONE;
+}
+
+/**
+ * Get the reserved track bits for a depot
+ * @pre IsRailDepot(t)
+ * @param t the tile
+ * @return reserved track bits
+ */
+static inline TrackBits GetRailDepotReservation(TileIndex t)
+{
+ return GetDepotWaypointReservation(t) ? TrackToTrackBits(GetRailDepotTrack(t)) : TRACK_BIT_NONE;
+}
+
+
static inline SignalType GetSignalType(TileIndex t, Track track)
{
assert(GetRailTileType(t) == RAIL_TILE_SIGNALS);
diff --git a/src/road_map.h b/src/road_map.h
index 756e3fcb7..dfd88ae3f 100644
--- a/src/road_map.h
+++ b/src/road_map.h
@@ -253,6 +253,43 @@ static inline TrackBits GetCrossingRailBits(TileIndex tile)
return AxisToTrackBits(GetCrossingRailAxis(tile));
}
+
+/**
+ * Get the reservation state of the rail crossing
+ * @pre IsLevelCrossingTile(t)
+ * @param t the crossing tile
+ * @return reservation state
+ */
+static inline bool GetCrossingReservation(TileIndex t)
+{
+ assert(IsLevelCrossingTile(t));
+ return HasBit(_m[t].m5, 4);
+}
+
+/**
+ * Set the reservation state of the rail crossing
+ * @note Works for both waypoints and rail depots
+ * @pre IsLevelCrossingTile(t)
+ * @param t the crossing tile
+ * @param b the reservation state
+ */
+static inline void SetCrossingReservation(TileIndex t, bool b)
+{
+ assert(IsLevelCrossingTile(t));
+ SB(_m[t].m5, 4, 1, b ? 1 : 0);
+}
+
+/**
+ * Get the reserved track bits for a rail crossing
+ * @pre IsLevelCrossingTile(t)
+ * @param t the tile
+ * @return reserved track bits
+ */
+static inline TrackBits GetRailCrossingReservation(TileIndex t)
+{
+ return GetCrossingReservation(t) ? GetCrossingRailBits(t) : TRACK_BIT_NONE;
+}
+
static inline bool IsCrossingBarred(TileIndex t)
{
assert(IsLevelCrossing(t));
diff --git a/src/station_map.h b/src/station_map.h
index 7422a32e5..77d427958 100644
--- a/src/station_map.h
+++ b/src/station_map.h
@@ -204,6 +204,41 @@ static inline bool IsCompatibleTrainStationTile(TileIndex t1, TileIndex t2)
!IsStationTileBlocked(t1);
}
+/**
+ * Get the reservation state of the rail station
+ * @pre IsRailwayStationTile(t)
+ * @param t the station tile
+ * @return reservation state
+ */
+static inline bool GetRailwayStationReservation(TileIndex t)
+{
+ assert(IsRailwayStationTile(t));
+ return HasBit(_m[t].m6, 2);
+}
+
+/**
+ * Set the reservation state of the rail station
+ * @pre IsRailwayStationTile(t)
+ * @param t the station tile
+ * @param b the reservation state
+ */
+static inline void SetRailwayStationReservation(TileIndex t, bool b)
+{
+ assert(IsRailwayStationTile(t));
+ SB(_m[t].m6, 2, 1, b ? 1 : 0);
+}
+
+/**
+ * Get the reserved track bits for a waypoint
+ * @pre IsRailwayStationTile(t)
+ * @param t the tile
+ * @return reserved track bits
+ */
+static inline TrackBits GetRailStationReservation(TileIndex t)
+{
+ return GetRailwayStationReservation(t) ? AxisToTrackBits(GetRailStationAxis(t)) : TRACK_BIT_NONE;
+}
+
static inline DiagDirection GetDockDirection(TileIndex t)
{
@@ -277,6 +312,7 @@ static inline void MakeRailStation(TileIndex t, Owner o, StationID sid, Axis a,
{
MakeStation(t, o, sid, STATION_RAIL, section + a);
SetRailType(t, rt);
+ SetRailwayStationReservation(t, false);
}
static inline void MakeRoadStop(TileIndex t, Owner o, StationID sid, RoadStopType rst, RoadTypes rt, DiagDirection d)
diff --git a/src/track_func.h b/src/track_func.h
index e6591212a..144ea03e8 100644
--- a/src/track_func.h
+++ b/src/track_func.h
@@ -198,6 +198,21 @@ static inline bool IsValidTrackdir(Trackdir trackdir)
*/
/**
+ * Find the opposite track to a given track.
+ *
+ * TRACK_LOWER -> TRACK_UPPER and vice versa, likewise for left/right.
+ * TRACK_X is mapped to TRACK_Y and reversed.
+ *
+ * @param t the track to convert
+ * @return the opposite track
+ */
+static inline Track TrackToOppositeTrack(Track t)
+{
+ assert(t != INVALID_TRACK);
+ return (Track)(t ^ 1);
+}
+
+/**
* Maps a trackdir to the reverse trackdir.
*
* Returns the reverse trackdir of a Trackdir value. The reverse trackdir
diff --git a/src/tunnelbridge_map.h b/src/tunnelbridge_map.h
index bdc709944..5b5c6e447 100644
--- a/src/tunnelbridge_map.h
+++ b/src/tunnelbridge_map.h
@@ -11,6 +11,7 @@
#include "bridge_map.h"
#include "tunnel_map.h"
#include "transport_type.h"
+#include "track_func.h"
/**
@@ -80,4 +81,42 @@ static inline TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
return IsTunnel(t) ? GetOtherTunnelEnd(t) : GetOtherBridgeEnd(t);
}
+
+/**
+ * Get the reservation state of the rail tunnel/bridge
+ * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
+ * @param t the tile
+ * @return reservation state
+ */
+static inline bool GetTunnelBridgeReservation(TileIndex t)
+{
+ assert(IsTileType(t, MP_TUNNELBRIDGE));
+ assert(GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL);
+ return HasBit(_m[t].m5, 4);
+}
+
+/**
+ * Set the reservation state of the rail tunnel/bridge
+ * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
+ * @param t the tile
+ * @param b the reservation state
+ */
+static inline void SetTunnelBridgeReservation(TileIndex t, bool b)
+{
+ assert(IsTileType(t, MP_TUNNELBRIDGE));
+ assert(GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL);
+ SB(_m[t].m5, 4, 1, b ? 1 : 0);
+}
+
+/**
+ * Get the reserved track bits for a rail tunnel/bridge
+ * @pre IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL
+ * @param t the tile
+ * @return reserved track bits
+ */
+static inline TrackBits GetRailTunnelBridgeReservation(TileIndex t)
+{
+ return GetTunnelBridgeReservation(t) ? DiagDirToDiagTrackBits(GetTunnelBridgeDirection(t)) : TRACK_BIT_NONE;
+}
+
#endif /* TUNNELBRIDGE_MAP_H */