summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-08-02 22:48:01 +0000
committerrubidium <rubidium@openttd.org>2008-08-02 22:48:01 +0000
commitf6555cf6f81776fa4a09fab2091bbbe43972cddc (patch)
tree7f9e26ee10037396c79f0f24eb97c51d72db17e9 /src
parent2bb88255388bedae0d94c9c06751ee8f140a9194 (diff)
downloadopenttd-f6555cf6f81776fa4a09fab2091bbbe43972cddc.tar.xz
(svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
Diffstat (limited to 'src')
-rw-r--r--src/pbs.cpp116
-rw-r--r--src/pbs.h18
-rw-r--r--src/rail_map.h33
3 files changed, 166 insertions, 1 deletions
diff --git a/src/pbs.cpp b/src/pbs.cpp
index c75eb8caf..631f05c70 100644
--- a/src/pbs.cpp
+++ b/src/pbs.cpp
@@ -1,7 +1,6 @@
/* $Id$ */
/** @file pbs.cpp */
-
#include "stdafx.h"
#include "openttd.h"
#include "pbs.h"
@@ -9,6 +8,10 @@
#include "road_map.h"
#include "station_map.h"
#include "tunnelbridge_map.h"
+#include "functions.h"
+#include "debug.h"
+#include "direction_func.h"
+#include "settings_type.h"
/**
* Get the reserved trackbits for any tile, regardless of type.
@@ -41,3 +44,114 @@ TrackBits GetReservedTrackbits(TileIndex t)
}
return TRACK_BIT_NONE;
}
+
+/**
+ * Set the reservation for a complete station platform.
+ * @pre IsRailwayStationTile(start)
+ * @param start starting tile of the platform
+ * @param dir the direction in which to follow the platform
+ * @param b the state the reservation should be set to
+ */
+void SetRailwayStationPlatformReservation(TileIndex start, DiagDirection dir, bool b)
+{
+ TileIndex tile = start;
+ TileIndexDiff diff = TileOffsByDiagDir(dir);
+
+ assert(IsRailwayStationTile(start));
+ assert(GetRailStationAxis(start) == DiagDirToAxis(dir));
+
+ do {
+ SetRailwayStationReservation(tile, b);
+ if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(tile);
+ tile = TILE_ADD(tile, diff);
+ } while (IsCompatibleTrainStationTile(tile, start));
+}
+
+/**
+ * Try to reserve a specific track on a tile
+ * @param tile the tile
+ * @param t the track
+ * @return true if reservation was successfull, i.e. the track was
+ * free and didn't cross any other reserved tracks.
+ */
+bool TryReserveRailTrack(TileIndex tile, Track t)
+{
+ assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
+
+ if (_settings_client.gui.show_track_reservation) {
+ MarkTileDirtyByTile(tile);
+ }
+
+ switch (GetTileType(tile)) {
+ case MP_RAILWAY:
+ if (IsPlainRailTile(tile)) return TryReserveTrack(tile, t);
+ if (IsRailWaypoint(tile) || IsRailDepot(tile)) {
+ if (!GetDepotWaypointReservation(tile)) {
+ SetDepotWaypointReservation(tile, true);
+ return true;
+ }
+ }
+ break;
+
+ case MP_ROAD:
+ if (IsLevelCrossing(tile) && !GetCrossingReservation(tile)) {
+ SetCrossingReservation(tile, true);
+ return true;
+ }
+ break;
+
+ case MP_STATION:
+ if (IsRailwayStation(tile) && !GetRailwayStationReservation(tile)) {
+ SetRailwayStationReservation(tile, true);
+ return true;
+ }
+ break;
+
+ case MP_TUNNELBRIDGE:
+ if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL && !GetRailTunnelBridgeReservation(tile)) {
+ SetTunnelBridgeReservation(tile, true);
+ return true;
+ }
+ break;
+
+ default:
+ break;
+ }
+ return false;
+}
+
+/**
+ * Lift the reservation of a specific track on a tile
+ * @param tile the tile
+ * @param t the track
+ */
+ void UnreserveRailTrack(TileIndex tile, Track t)
+{
+ assert((GetTileTrackStatus(tile, TRANSPORT_RAIL, 0) & TrackToTrackBits(t)) != 0);
+
+ if (_settings_client.gui.show_track_reservation) {
+ MarkTileDirtyByTile(tile);
+ }
+
+ switch (GetTileType(tile)) {
+ case MP_RAILWAY:
+ if (IsRailWaypoint(tile) || IsRailDepot(tile)) SetDepotWaypointReservation(tile, false);
+ if (IsPlainRailTile(tile)) UnreserveTrack(tile, t);
+ break;
+
+ case MP_ROAD:
+ if (IsLevelCrossing(tile)) SetCrossingReservation(tile, false);
+ break;
+
+ case MP_STATION:
+ if (IsRailwayStation(tile)) SetRailwayStationReservation(tile, false);
+ break;
+
+ case MP_TUNNELBRIDGE:
+ if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) SetTunnelBridgeReservation(tile, false);
+ break;
+
+ default:
+ break;
+ }
+}
diff --git a/src/pbs.h b/src/pbs.h
index 51a9ba201..4103f1ebf 100644
--- a/src/pbs.h
+++ b/src/pbs.h
@@ -6,8 +6,26 @@
#define PBS_H
#include "tile_type.h"
+#include "direction_type.h"
#include "track_type.h"
TrackBits GetReservedTrackbits(TileIndex t);
+void SetRailwayStationPlatformReservation(TileIndex start, DiagDirection dir, bool b);
+
+bool TryReserveRailTrack(TileIndex tile, Track t);
+void UnreserveRailTrack(TileIndex tile, Track t);
+
+/**
+ * Check whether some of tracks is reserved on a tile.
+ *
+ * @param tile the tile
+ * @param tracks the tracks to test
+ * @return true if at least on of tracks is reserved
+ */
+static inline bool HasReservedTracks(TileIndex tile, TrackBits tracks)
+{
+ return (GetReservedTrackbits(tile) & tracks) != TRACK_BIT_NONE;
+}
+
#endif /* PBS_H */
diff --git a/src/rail_map.h b/src/rail_map.h
index 56eecc473..649e3baed 100644
--- a/src/rail_map.h
+++ b/src/rail_map.h
@@ -257,6 +257,39 @@ static inline void SetTrackReservation(TileIndex t, TrackBits b)
}
/**
+ * Try to reserve a specific track on a tile
+ * @pre IsPlainRailTile(t) && HasTrack(tile, t)
+ * @param tile the tile
+ * @param t the rack to reserve
+ * @return true if successful
+ */
+static inline bool TryReserveTrack(TileIndex tile, Track t)
+{
+ assert(HasTrack(tile, t));
+ TrackBits bits = TrackToTrackBits(t);
+ TrackBits res = GetTrackReservation(tile);
+ if ((res & bits) != TRACK_BIT_NONE) return false; // already reserved
+ res |= bits;
+ if (TracksOverlap(res)) return false; // crossing reservation present
+ SetTrackReservation(tile, res);
+ return true;
+}
+
+/**
+ * Lift the reservation of a specific track on a tile
+ * @pre IsPlainRailTile(t) && HasTrack(tile, t)
+ * @param tile the tile
+ * @param t the track to free
+ */
+static inline void UnreserveTrack(TileIndex tile, Track t)
+{
+ assert(HasTrack(tile, t));
+ TrackBits res = GetTrackReservation(tile);
+ res &= ~TrackToTrackBits(t);
+ SetTrackReservation(tile, res);
+}
+
+/**
* Get the reservation state of the waypoint or depot
* @note Works for both waypoints and rail depots
* @pre IsRailWaypoint(t) || IsRailDepot(t)