From 7b71fe8acd9d04989314978b07c552dbbb7674c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Wed, 8 May 2019 23:34:41 +0200 Subject: [PATCH] Fix error value checked from ride_get_smallest_station_length (#8719) ride_get_smallest_station_length returns the int32_t::max instead of -1 on error. --- src/openrct2/network/Network.cpp | 2 +- src/openrct2/ride/Ride.cpp | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index 9e525a359d..cbc767b85c 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -31,7 +31,7 @@ // This string specifies which version of network stream current build uses. // It is used for making sure only compatible builds get connected, even within // single OpenRCT2 version. -#define NETWORK_STREAM_VERSION "23" +#define NETWORK_STREAM_VERSION "24" #define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION static Peep* _pickup_peep = nullptr; diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 5cb6b78f63..7bfe2d534f 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -24,6 +24,7 @@ #include "../common.h" #include "../config/Config.h" #include "../core/Guard.hpp" +#include "../core/Optional.hpp" #include "../interface/Window.h" #include "../localisation/Date.h" #include "../localisation/Localisation.h" @@ -6881,14 +6882,17 @@ uint64_t ride_entry_get_supported_track_pieces(const rct_ride_entry* rideEntry) return supportedPieces; } -static int32_t ride_get_smallest_station_length(Ride* ride) +static opt::optional ride_get_smallest_station_length(Ride* ride) { - auto result = std::numeric_limits::max(); - for (int32_t i = 0; i < MAX_STATIONS; i++) + opt::optional result; + for (const auto& station : ride->stations) { - if (ride->stations[i].Start.xy != RCT_XY8_UNDEFINED) + if (station.Start.xy != RCT_XY8_UNDEFINED) { - result = std::min(result, ride->stations[i].Length); + if (!result.has_value() || station.Length < *result) + { + result = station.Length; + } } } return result; @@ -6996,11 +7000,11 @@ void Ride::UpdateMaxVehicles() min_max_cars_per_train = rideEntry->max_cars_in_train | (rideEntry->min_cars_in_train << 4); // Calculate maximum train length based on smallest station length - int32_t stationLength = ride_get_smallest_station_length(this); - if (stationLength == -1) + auto stationNumTiles = ride_get_smallest_station_length(this); + if (!stationNumTiles.has_value()) return; - stationLength = (stationLength * 0x44180) - 0x16B2A; + auto stationLength = (*stationNumTiles * 0x44180) - 0x16B2A; int32_t maxMass = RideData5[type].max_mass << 8; int32_t maxCarsPerTrain = 1; for (int32_t numCars = rideEntry->max_cars_in_train; numCars > 0; numCars--)