From ec5f239a9a63726c8c07da057d642c1fca86fa03 Mon Sep 17 00:00:00 2001 From: J0anJosep Date: Sat, 15 May 2021 16:32:01 +0200 Subject: [PATCH] Add: Add road depot platforms. --- src/platform.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++-- src/platform_func.h | 38 +++++++++++++++++++++++-- src/platform_type.h | 1 + 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/src/platform.cpp b/src/platform.cpp index 6878ad4344..55e0871d62 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -183,6 +183,39 @@ uint GetRailDepotPlatformLength(TileIndex tile) return len - 1; } +/** + * Get the length of a road depot platform. + * @pre IsDepotTypeTile(tile, TRANSPORT_ROAD) + * @param tile Tile to check + * @param rtt Whether to check for road or tram type. + * @return The length of the platform in tile length. + */ +uint GetRoadDepotPlatformLength(TileIndex tile, RoadTramType rtt) +{ + assert(IsExtendedRoadDepotTile(tile)); + + DiagDirection dir = GetRoadDepotDirection(tile); + TileIndexDiff delta = TileOffsByDiagDir(dir); + + TileIndex t = tile; + uint len = 0; + do { + len++; + if ((GetRoadBits(t, rtt) & DiagDirToRoadBits(dir)) == ROAD_NONE) break; + t -= delta; + } while (IsCompatibleRoadDepotTile(t, tile, rtt)); + + t = tile; + dir = ReverseDiagDir(dir); + do { + len++; + if ((GetRoadBits(t, rtt) & DiagDirToRoadBits(dir)) == ROAD_NONE) break; + t += delta; + } while (IsCompatibleRoadDepotTile(t, tile, rtt)); + + return len - 1; +} + /** * Get the length of a rail depot platform in a given direction. @@ -206,12 +239,37 @@ uint GetRailDepotPlatformLength(TileIndex tile, DiagDirection dir) return length; } +/** + * Get the length of a road depot platform in a given direction. + * @pre IsRoadDepotTile(tile) + * @param tile Tile to check + * @param dir Direction to check + * @param rtt Whether to check for road or tram type. + * @return The length of the platform in tile length in the given direction. + */ +uint GetRoadDepotPlatformLength(TileIndex tile, DiagDirection dir, RoadTramType rtt) +{ + TileIndex start_tile = tile; + uint length = 0; + assert(IsExtendedRoadDepotTile(tile)); + assert(dir < DIAGDIR_END); + + do { + length++; + if ((GetRoadBits(tile, rtt) & DiagDirToRoadBits(dir)) == ROAD_NONE) break; + tile += TileOffsByDiagDir(dir); + } while (IsCompatibleRoadDepotTile(tile, start_tile, rtt)); + + return length; +} + /** * Get the length of a platform. * @param tile Tile to check + * @param rtt Whether to check for road or tram type (only for road transport). * @return The length of the platform in tile length. */ -uint GetPlatformLength(TileIndex tile) +uint GetPlatformLength(TileIndex tile, RoadTramType rtt) { switch (GetPlatformType(tile)) { case PT_RAIL_STATION: @@ -220,6 +278,8 @@ uint GetPlatformLength(TileIndex tile) return 1; case PT_RAIL_DEPOT: return GetRailDepotPlatformLength(tile); + case PT_ROAD_DEPOT: + return GetRoadDepotPlatformLength(tile, rtt); default: NOT_REACHED(); } } @@ -229,9 +289,10 @@ uint GetPlatformLength(TileIndex tile) * @pre IsRailDepotTile(tile) * @param tile Tile to check * @param dir Direction to check + * @param rtt Whether to check for road or tram type (only for road transport). * @return The length of the platform in tile length in the given direction. */ -uint GetPlatformLength(TileIndex tile, DiagDirection dir) +uint GetPlatformLength(TileIndex tile, DiagDirection dir, RoadTramType rtt) { switch (GetPlatformType(tile)) { case PT_RAIL_STATION: @@ -240,6 +301,8 @@ uint GetPlatformLength(TileIndex tile, DiagDirection dir) return 1; case PT_RAIL_DEPOT: return GetRailDepotPlatformLength(tile, dir); + case PT_ROAD_DEPOT: + return GetRoadDepotPlatformLength(tile, dir, rtt); default: NOT_REACHED(); } } diff --git a/src/platform_func.h b/src/platform_func.h index 30a2dae280..baa5912874 100644 --- a/src/platform_func.h +++ b/src/platform_func.h @@ -13,6 +13,7 @@ #include "station_map.h" #include "depot_map.h" #include "platform_type.h" +#include "road_map.h" /** * Check if a tile is a valid continuation to a railstation tile. @@ -57,6 +58,31 @@ static inline bool IsCompatibleTrainDepotTile(TileIndex test_tile, TileIndex dep GetDepotIndex(test_tile) == GetDepotIndex(depot_tile); } +/** + * Check if a tile is a valid continuation to an extended road depot tile. + * The tile \a test_tile is a valid continuation to \a depot_tile, if all of the following are true: + * \li \a test_tile is an extended depot tile + * \li \a test_tile and \a depot_tile have the same road type and appropriate road bits + * \li the tracks on \a test_tile and \a depot_tile are in the same direction + * \li both tiles belong to the same depot + * @param test_tile Tile to test + * @param depot_tile Depot tile to compare with + * @param rtt Whether road or tram type. + * @pre IsExtendedRoadDepotTile(depot_tile) + * @return true if the two tiles are compatible + */ +static inline bool IsCompatibleRoadDepotTile(TileIndex test_tile, TileIndex depot_tile, RoadTramType rtt) +{ + assert(IsExtendedRoadDepotTile(depot_tile)); + if (!IsExtendedRoadDepotTile(test_tile)) return false; + if (GetDepotIndex(test_tile) != GetDepotIndex(depot_tile)) return false; + if (GetRoadType(depot_tile, rtt) != GetRoadType(test_tile, rtt)) return false; + + DiagDirection dir = DiagdirBetweenTiles(test_tile, depot_tile); + assert(dir != INVALID_DIAGDIR); + return (GetRoadBits(test_tile, rtt) & DiagDirToRoadBits(dir)) != ROAD_NONE; +} + /** * Returns the type of platform of a given tile. * @param tile Tile to check @@ -72,6 +98,9 @@ static inline PlatformType GetPlatformType(TileIndex tile) case MP_RAILWAY: if (IsExtendedRailDepotTile(tile)) return PT_RAIL_DEPOT; break; + case MP_ROAD: + if (IsExtendedRoadDepotTile(tile)) return PT_ROAD_DEPOT; + break; default: break; } @@ -110,9 +139,10 @@ static inline bool HasPlatformReservation(TileIndex tile) * platform type and (depending on the platform type) its railtype or other specs. * @param test_tile the tile to check * @param orig_tile the tile with the platform type we are interested in + * @param rtt Whether to check road or tram types (only for road transport); * @return whether the two tiles are compatible tiles for defining a platform */ -static inline bool IsCompatiblePlatformTile(TileIndex test_tile, TileIndex orig_tile) +static inline bool IsCompatiblePlatformTile(TileIndex test_tile, TileIndex orig_tile, RoadTramType rtt = RTT_ROAD) { switch (GetPlatformType(orig_tile)) { case PT_RAIL_STATION: @@ -121,6 +151,8 @@ static inline bool IsCompatiblePlatformTile(TileIndex test_tile, TileIndex orig_ return test_tile == orig_tile; case PT_RAIL_DEPOT: return IsCompatibleTrainDepotTile(test_tile, orig_tile); + case PT_ROAD_DEPOT: + return IsCompatibleRoadDepotTile(test_tile, orig_tile, rtt); default: NOT_REACHED(); } } @@ -128,8 +160,8 @@ static inline bool IsCompatiblePlatformTile(TileIndex test_tile, TileIndex orig_ void SetPlatformReservation(TileIndex start, DiagDirection dir, bool b); void SetPlatformReservation(TileIndex start, bool b); -uint GetPlatformLength(TileIndex tile); -uint GetPlatformLength(TileIndex tile, DiagDirection dir); +uint GetPlatformLength(TileIndex tile, RoadTramType rtt = RTT_ROAD); +uint GetPlatformLength(TileIndex tile, DiagDirection dir, RoadTramType rtt = RTT_ROAD); TileIndex GetPlatformExtremeTile(TileIndex tile, DiagDirection dir); TileArea GetPlatformTileArea(TileIndex tile); diff --git a/src/platform_type.h b/src/platform_type.h index 27765d92c7..e6c543465a 100644 --- a/src/platform_type.h +++ b/src/platform_type.h @@ -16,6 +16,7 @@ enum PlatformType { PT_RAIL_STATION, PT_RAIL_WAYPOINT, PT_RAIL_DEPOT, + PT_ROAD_DEPOT, PT_END, INVALID_PLATFORM_TYPE = PT_END, };