From 2137cea3bb255bdf797c9240b858fe375136793d Mon Sep 17 00:00:00 2001 From: Hielke Morsink Date: Fri, 28 Jan 2022 10:04:16 +0100 Subject: [PATCH] Reduce use of UnderlyingType --- src/openrct2-ui/windows/Ride.cpp | 12 ++-- src/openrct2/ride/Ride.cpp | 56 +++++++++---------- src/openrct2/ride/RideConstruction.cpp | 26 ++++----- src/openrct2/ride/Station.cpp | 21 +++---- .../scripting/bindings/ride/ScRide.cpp | 4 +- src/openrct2/world/Footpath.cpp | 9 ++- 6 files changed, 59 insertions(+), 69 deletions(-) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index 9aad45531d..b48867fdbf 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -1456,16 +1456,12 @@ static std::optional GetStationIndexFromViewSelection(const rct_wi return std::nullopt; } - for (StationIndex::UnderlyingType index = 0; index < std::size(ride->GetStations()); ++index) + for (const auto& station : ride->GetStations()) { - StationIndex stationIndex = StationIndex::FromUnderlying(index); - const auto& station = ride->GetStation(stationIndex); - if (!station.Start.IsNull()) + if (!station.Start.IsNull() && viewSelectionIndex-- == 0) { - if (viewSelectionIndex-- == 0) - { - return std::make_optional(stationIndex); - } + const auto stationIndex = ride->GetStationIndex(&station); + return std::make_optional(stationIndex); } } return std::nullopt; diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 952c5e1198..93d50562cf 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -262,18 +262,18 @@ int32_t Ride::GetAge() const int32_t Ride::GetTotalQueueLength() const { int32_t queueLength = 0; - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) - if (!ride_get_entrance_location(this, StationIndex::FromUnderlying(i)).IsNull()) - queueLength += stations[i].QueueLength; + for (const auto& station : stations) + if (!station.Entrance.IsNull()) + queueLength += station.QueueLength; return queueLength; } int32_t Ride::GetMaxQueueTime() const { uint8_t queueTime = 0; - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) - if (!ride_get_entrance_location(this, StationIndex::FromUnderlying(i)).IsNull()) - queueTime = std::max(queueTime, stations[i].QueueTime); + for (const auto& station : stations) + if (!station.Entrance.IsNull()) + queueTime = std::max(queueTime, station.QueueTime); return static_cast(queueTime); } @@ -2637,17 +2637,16 @@ static int32_t ride_check_for_entrance_exit(RideId rideIndex) */ void Ride::ChainQueues() const { - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : stations) { - auto location = ride_get_entrance_location(this, StationIndex::FromUnderlying(i)); - if (location.IsNull()) + if (station.Entrance.IsNull()) continue; - auto mapLocation = location.ToCoordsXYZ(); + auto mapLocation = station.Entrance.ToCoordsXYZ(); // This will fire for every entrance on this x, y and z, regardless whether that actually belongs to // the ride or not. - TileElement* tileElement = map_get_first_element_at(location); + TileElement* tileElement = map_get_first_element_at(station.Entrance); if (tileElement != nullptr) { do @@ -2659,7 +2658,7 @@ void Ride::ChainQueues() const int32_t direction = tileElement->GetDirection(); footpath_chain_ride_queue( - id, StationIndex::FromUnderlying(i), mapLocation, tileElement, direction_reverse(direction)); + id, GetStationIndex(&station), mapLocation, tileElement, direction_reverse(direction)); } while (!(tileElement++)->IsLastForTile()); } } @@ -2972,18 +2971,15 @@ static void ride_set_maze_entrance_exit_points(Ride* ride) // Create a list of all the entrance and exit positions TileCoordsXYZD* position = positions; - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : ride->GetStations()) { - const auto entrance = ride_get_entrance_location(ride, StationIndex::FromUnderlying(i)); - const auto exit = ride_get_exit_location(ride, StationIndex::FromUnderlying(i)); - - if (!entrance.IsNull()) + if (!station.Entrance.IsNull()) { - *position++ = entrance; + *position++ = station.Entrance; } - if (!exit.IsNull()) + if (!station.Exit.IsNull()) { - *position++ = exit; + *position++ = station.Exit; } } (*position++).SetNull(); @@ -3788,7 +3784,7 @@ static bool ride_create_cable_lift(RideId rideIndex, bool isApplying) /** * Opens the construction window prompting to construct a missing entrance or exit. - * This will also the screen to the first station missing the entrance or exit. + * This will also move the screen to the first station missing the entrance or exit. * rct2: 0x006B51C0 */ void Ride::ConstructMissingEntranceOrExit() const @@ -3798,31 +3794,35 @@ void Ride::ConstructMissingEntranceOrExit() const return; int8_t entranceOrExit = -1; - StationIndex::UnderlyingType i; - for (i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + const RideStation* incompleteStation = nullptr; + for (const auto& station : stations) { - if (stations[i].Start.IsNull()) + if (station.Start.IsNull()) continue; - if (ride_get_entrance_location(this, StationIndex::FromUnderlying(i)).IsNull()) + if (station.Entrance.IsNull()) { entranceOrExit = WC_RIDE_CONSTRUCTION__WIDX_ENTRANCE; + incompleteStation = &station; break; } - if (ride_get_exit_location(this, StationIndex::FromUnderlying(i)).IsNull()) + if (station.Exit.IsNull()) { entranceOrExit = WC_RIDE_CONSTRUCTION__WIDX_EXIT; + incompleteStation = &station; break; } } - if (entranceOrExit == -1) + if (incompleteStation == nullptr) + { // No station with a missing entrance or exit was found return; + } if (type != RIDE_TYPE_MAZE) { - auto location = stations[i].GetStart(); + auto location = incompleteStation->GetStart(); window_scroll_to_location(w, location); CoordsXYE trackElement; diff --git a/src/openrct2/ride/RideConstruction.cpp b/src/openrct2/ride/RideConstruction.cpp index ab85d48509..bf99969bc3 100644 --- a/src/openrct2/ride/RideConstruction.cpp +++ b/src/openrct2/ride/RideConstruction.cpp @@ -1341,12 +1341,12 @@ void Ride::ValidateStations() if (type != RIDE_TYPE_MAZE) { // find the stations of the ride to begin stepping over track elements from - for (StationIndex::UnderlyingType stationId = 0; stationId < OpenRCT2::Limits::MaxStationsPerRide; ++stationId) + for (const auto& station : stations) { - if (stations[stationId].Start.IsNull()) + if (station.Start.IsNull()) continue; - CoordsXYZ location = stations[stationId].GetStart(); + CoordsXYZ location = station.GetStart(); uint8_t direction = INVALID_DIRECTION; bool specialTrack = false; @@ -1390,7 +1390,7 @@ void Ride::ValidateStations() break; } // update the StationIndex, get the TrackElement's rotation - tileElement->AsTrack()->SetStationIndex(StationIndex::FromUnderlying(stationId)); + tileElement->AsTrack()->SetStationIndex(GetStationIndex(&station)); direction = tileElement->GetDirection(); // In the future this could look at the TED and see if the station has a sequence longer than 1 @@ -1441,26 +1441,24 @@ void Ride::ValidateStations() break; } - tileElement->AsTrack()->SetStationIndex(StationIndex::FromUnderlying(stationId)); + tileElement->AsTrack()->SetStationIndex(GetStationIndex(&station)); } } } // determine what entrances and exits exist FixedVector locations; - for (StationIndex::UnderlyingType stationId = 0; stationId < OpenRCT2::Limits::MaxStationsPerRide; ++stationId) + for (auto& station : stations) { - auto entrance = ride_get_entrance_location(this, StationIndex::FromUnderlying(stationId)); - if (!entrance.IsNull()) + if (!station.Entrance.IsNull()) { - locations.push_back(entrance); - ride_clear_entrance_location(this, StationIndex::FromUnderlying(stationId)); + locations.push_back(station.Entrance); + station.Entrance.SetNull(); } - auto exit = ride_get_exit_location(this, StationIndex::FromUnderlying(stationId)); - if (!exit.IsNull()) + if (!station.Exit.IsNull()) { - locations.push_back(exit); - ride_clear_exit_location(this, StationIndex::FromUnderlying(stationId)); + locations.push_back(station.Exit); + station.Exit.SetNull(); } } diff --git a/src/openrct2/ride/Station.cpp b/src/openrct2/ride/Station.cpp index c367280db2..5524c32153 100644 --- a/src/openrct2/ride/Station.cpp +++ b/src/openrct2/ride/Station.cpp @@ -370,12 +370,11 @@ TileElement* ride_get_station_exit_element(const CoordsXYZ& elementPos) StationIndex ride_get_first_valid_station_exit(Ride* ride) { - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : ride->GetStations()) { - StationIndex stationIndex = StationIndex::FromUnderlying(i); - if (!ride->GetStation(stationIndex).Exit.IsNull()) + if (!station.Exit.IsNull()) { - return stationIndex; + return ride->GetStationIndex(&station); } } return StationIndex::GetNull(); @@ -383,12 +382,11 @@ StationIndex ride_get_first_valid_station_exit(Ride* ride) StationIndex ride_get_first_valid_station_start(const Ride* ride) { - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : ride->GetStations()) { - StationIndex stationIndex = StationIndex::FromUnderlying(i); - if (!ride->GetStation(stationIndex).Start.IsNull()) + if (!station.Start.IsNull()) { - return stationIndex; + return ride->GetStationIndex(&station); } } return StationIndex::GetNull(); @@ -396,12 +394,11 @@ StationIndex ride_get_first_valid_station_start(const Ride* ride) StationIndex ride_get_first_empty_station_start(const Ride* ride) { - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : ride->GetStations()) { - StationIndex stationIndex = StationIndex::FromUnderlying(i); - if (ride->GetStation(stationIndex).Start.IsNull()) + if (station.Start.IsNull()) { - return stationIndex; + return ride->GetStationIndex(&station); } } return StationIndex::GetNull(); diff --git a/src/openrct2/scripting/bindings/ride/ScRide.cpp b/src/openrct2/scripting/bindings/ride/ScRide.cpp index 462d01c3c8..be661bf858 100644 --- a/src/openrct2/scripting/bindings/ride/ScRide.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRide.cpp @@ -292,9 +292,9 @@ namespace OpenRCT2::Scripting auto ride = GetRide(); if (ride != nullptr) { - for (StationIndex::UnderlyingType i = 0; i < std::size(ride->GetStations()); i++) + for (const auto& station : ride->GetStations()) { - result.push_back(std::make_shared(ride->id, StationIndex::FromUnderlying(i))); + result.push_back(std::make_shared(ride->id, ride->GetStationIndex(&station))); } } return result; diff --git a/src/openrct2/world/Footpath.cpp b/src/openrct2/world/Footpath.cpp index e45cefbce7..bd3a8fe110 100644 --- a/src/openrct2/world/Footpath.cpp +++ b/src/openrct2/world/Footpath.cpp @@ -1236,13 +1236,12 @@ void footpath_update_queue_chains() if (ride == nullptr) continue; - for (StationIndex::UnderlyingType i = 0; i < OpenRCT2::Limits::MaxStationsPerRide; i++) + for (const auto& station : ride->GetStations()) { - TileCoordsXYZD location = ride_get_entrance_location(ride, StationIndex::FromUnderlying(i)); - if (location.IsNull()) + if (station.Entrance.IsNull()) continue; - TileElement* tileElement = map_get_first_element_at(location); + TileElement* tileElement = map_get_first_element_at(station.Entrance); if (tileElement != nullptr) { do @@ -1256,7 +1255,7 @@ void footpath_update_queue_chains() Direction direction = direction_reverse(tileElement->GetDirection()); footpath_chain_ride_queue( - rideIndex, StationIndex::FromUnderlying(i), location.ToCoordsXY(), tileElement, direction); + rideIndex, ride->GetStationIndex(&station), station.Entrance.ToCoordsXY(), tileElement, direction); } while (!(tileElement++)->IsLastForTile()); } }