(svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
authorrubidium
Sat, 02 Aug 2008 22:48:01 +0000
changeset 9787 cedb26977f52
parent 9786 0169b327f3d2
child 9788 8d1bdf1ba220
(svn r13929) -Codechange [YAPP]: Reserving and unreserving of single tracks is now possible. (michi_cc)
src/pbs.cpp
src/pbs.h
src/rail_map.h
--- a/src/pbs.cpp	Sat Aug 02 22:47:48 2008 +0000
+++ b/src/pbs.cpp	Sat Aug 02 22:48:01 2008 +0000
@@ -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 @@
 	}
 	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;
+	}
+}
--- a/src/pbs.h	Sat Aug 02 22:47:48 2008 +0000
+++ b/src/pbs.h	Sat Aug 02 22:48:01 2008 +0000
@@ -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 */
--- a/src/rail_map.h	Sat Aug 02 22:47:48 2008 +0000
+++ b/src/rail_map.h	Sat Aug 02 22:48:01 2008 +0000
@@ -257,6 +257,39 @@
 }
 
 /**
+ * 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)