diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index d9456726ca..cc0c2ef34b 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -1226,16 +1226,9 @@ CommandCost CanExpandRailStation(const BaseStation *st, TileArea &new_ta) return CommandCost(); } -RailStationTileLayout::RailStationTileLayout(const StationSpec *spec, uint8_t platforms, uint8_t length) : platforms(platforms), length(length) -{ - if (spec == nullptr) return; - - /* Look for a predefined layout for the required size. */ - auto found = spec->layouts.find(GetStationLayoutKey(platforms, length)); - if (found != std::end(spec->layouts)) this->layout = found->second; -} - -StationGfx RailStationTileLayout::Iterator::operator*() const +/** @copydoc RailStationTileLayout::Iterator::operator* */ +template <> +StationGfx RailStationTileLayout::Iterator::operator*() const { /* Use predefined layout if it exists. Mask bit zero which will indicate axis. */ if (!stl.layout.empty()) return this->stl.layout[this->position] & ~1; @@ -1515,7 +1508,7 @@ CommandCost CmdBuildRailStation(DoCommandFlags flags, TileIndex tile_org, RailTy TileIndexDiff tile_delta = TileOffsByAxis(axis); // offset to go to the next platform tile TileIndexDiff track_delta = TileOffsByAxis(OtherAxis(axis)); // offset to go to the next track - RailStationTileLayout stl{statspec, numtracks, plat_len}; + RailStationTileLayout stl{statspec, numtracks, plat_len}; for (auto [i, it, tile_track] = std::make_tuple(0, stl.begin(), tile_org); i != numtracks; ++i, tile_track += track_delta) { for (auto [j, tile] = std::make_tuple(0, tile_track); j != plat_len; ++j, tile += tile_delta, ++it) { /* Don't check the layout if there's no bridge above anyway. */ diff --git a/src/station_layout_type.h b/src/station_layout_type.h index 17382b3408..410c4019f6 100644 --- a/src/station_layout_type.h +++ b/src/station_layout_type.h @@ -13,13 +13,21 @@ #include "newgrf_station.h" #include "station_map.h" +template class RailStationTileLayout { private: std::span layout{}; ///< Predefined tile layout. uint platforms; ///< Number of platforms. uint length; ///< Length of platforms. public: - RailStationTileLayout(const StationSpec *spec, uint8_t platforms, uint8_t length); + RailStationTileLayout(const StationSpec *spec, uint8_t platforms, uint8_t length) : platforms(platforms), length(length) + { + if (spec == nullptr) return; + + /* Look for a predefined layout for the required size. */ + auto found = spec->layouts.find(GetStationLayoutKey(platforms, length)); + if (found != std::end(spec->layouts)) this->layout = found->second; + } class Iterator { const RailStationTileLayout &stl; ///< Station tile layout being iterated. @@ -36,6 +44,10 @@ public: bool operator==(const Iterator &rhs) const { return this->position == rhs.position; } bool operator==(const std::default_sentinel_t &) const { return this->position == this->stl.platforms * this->stl.length; } + /** + * Dereference operator. + * @return The StationGfx value associated with the index this iterator points to. + */ StationGfx operator*() const; Iterator &operator++() diff --git a/src/waypoint_cmd.cpp b/src/waypoint_cmd.cpp index 6b162f9012..0c4369ce6a 100644 --- a/src/waypoint_cmd.cpp +++ b/src/waypoint_cmd.cpp @@ -192,6 +192,16 @@ extern CommandCost IsBuoyBridgeAboveOk(TileIndex tile); extern CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlags flags, int replacement_spec_index); +/** @copydoc RailStationTileLayout::Iterator::operator* */ +template <> +StationGfx RailStationTileLayout::Iterator::operator*() const +{ + /* Use predefined layout if it exists. Mask bit zero which will indicate axis. */ + if (!stl.layout.empty()) return this->stl.layout[this->position] & ~1; + + return 0; +} + /** * Convert existing rail to waypoint. Eg build a waypoint station over * piece of rail @@ -239,7 +249,7 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlags flags, TileIndex start_tile, Axi StationID est = StationID::Invalid(); const StationSpec *spec = StationClass::Get(spec_class)->GetSpec(spec_index); - RailStationTileLayout stl{spec, count, 1}; + RailStationTileLayout stl{spec, count, 1}; /* Check whether the tiles we're building on are valid rail or not. */ TileIndexDiff offset = TileOffsByAxis(OtherAxis(axis));