From 4f48ccc5c8817af6becd2d1d6fbc31bdfadd8641 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Tue, 20 Dec 2022 21:56:37 +0100 Subject: [PATCH 1/4] Create Ride::ChangeStatusCheckCompleteCircuit() Co-authored-by: spacek531 --- src/openrct2/ride/Ride.cpp | 26 +++++++++++++++++++------- src/openrct2/ride/Ride.h | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index a3c1bfb978..0d01ab161e 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3998,16 +3998,13 @@ ResultWithMessage Ride::Open(bool isApplying) return { false }; } - if (mode == RideMode::Race || mode == RideMode::ContinuousCircuit || IsBlockSectioned()) + auto message = ChangeStatusCheckCompleteCircuit(trackElement); + if (!message.Successful) { - if (FindTrackGap(trackElement, &problematicTrackElement)) - { - ride_scroll_to_track_error(problematicTrackElement); - return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT }; - } + return message; } - auto message = ChangeStatusCheckTrackValidity(trackElement); + message = ChangeStatusCheckTrackValidity(trackElement); if (!message.Successful) { return message; @@ -5796,6 +5793,21 @@ std::vector GetTracklessRides() return result; } +ResultWithMessage Ride::ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement) +{ + CoordsXYE problematicTrackElement = {}; + if (mode == RideMode::Race || mode == RideMode::ContinuousCircuit || IsBlockSectioned()) + { + if (FindTrackGap(trackElement, &problematicTrackElement)) + { + ride_scroll_to_track_error(problematicTrackElement); + return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT }; + } + } + + return { true }; +} + ResultWithMessage Ride::ChangeStatusCheckTrackValidity(const CoordsXYE& trackElement) { CoordsXYE problematicTrackElement = {}; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index a607a21793..10ca50800f 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -309,6 +309,7 @@ private: void ChainQueues() const; void ConstructMissingEntranceOrExit() const; + ResultWithMessage ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCheckTrackValidity(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCreateVehicles(bool isApplying, const CoordsXYE& trackElement); From f848fe5bd473d5a5abbf037887fae2d4a80161fc Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Tue, 20 Dec 2022 22:15:44 +0100 Subject: [PATCH 2/4] Create Ride::ChangeStatusDoStationChecks() Co-authored-by: spacek531 --- src/openrct2/ride/Ride.cpp | 48 +++++++++++++++++++++++--------------- src/openrct2/ride/Ride.h | 1 + 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 0d01ab161e..65c74476b1 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3896,14 +3896,12 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) window_close_by_number(WindowClass::RideConstruction, id.ToUnderlying()); } - auto stationIndexCheck = ride_mode_check_station_present(this); - auto stationIndex = stationIndexCheck.StationIndex; - if (stationIndex.IsNull()) - return { false, stationIndexCheck.Message }; - - auto stationNumbersCheck = ride_mode_check_valid_station_numbers(this); - if (!stationNumbersCheck.Successful) - return { false, stationNumbersCheck.Message }; + StationIndex stationIndex = {}; + auto message = ChangeStatusDoStationChecks(stationIndex); + if (!message.Successful) + { + return message; + } if (newStatus != RideStatus::Simulating) { @@ -3937,7 +3935,7 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) } } - auto message = ChangeStatusCheckTrackValidity(trackElement); + message = ChangeStatusCheckTrackValidity(trackElement); if (!message.Successful) { return message; @@ -3951,7 +3949,7 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) */ ResultWithMessage Ride::Open(bool isApplying) { - CoordsXYE trackElement, problematicTrackElement = {}; + CoordsXYE trackElement = {}; // Check to see if construction tool is in use. If it is close the construction window // to set the track to its final state and clean up ghosts. @@ -3963,14 +3961,12 @@ ResultWithMessage Ride::Open(bool isApplying) window_close_by_number(WindowClass::RideConstruction, id.ToUnderlying()); } - auto stationIndexCheck = ride_mode_check_station_present(this); - auto stationIndex = stationIndexCheck.StationIndex; - if (stationIndex.IsNull()) - return { false, stationIndexCheck.Message }; - - auto stationNumbersCheck = ride_mode_check_valid_station_numbers(this); - if (!stationNumbersCheck.Successful) - return { false, stationNumbersCheck.Message }; + StationIndex stationIndex = {}; + auto message = ChangeStatusDoStationChecks(stationIndex); + if (!message.Successful) + { + return message; + } auto entranceExitCheck = ride_check_for_entrance_exit(id); if (!entranceExitCheck.Successful) @@ -3998,7 +3994,7 @@ ResultWithMessage Ride::Open(bool isApplying) return { false }; } - auto message = ChangeStatusCheckCompleteCircuit(trackElement); + message = ChangeStatusCheckCompleteCircuit(trackElement); if (!message.Successful) { return message; @@ -5793,6 +5789,20 @@ std::vector GetTracklessRides() return result; } +ResultWithMessage Ride::ChangeStatusDoStationChecks(StationIndex& stationIndex) +{ + auto stationIndexCheck = ride_mode_check_station_present(this); + stationIndex = stationIndexCheck.StationIndex; + if (stationIndex.IsNull()) + return { false, stationIndexCheck.Message }; + + auto stationNumbersCheck = ride_mode_check_valid_station_numbers(this); + if (!stationNumbersCheck.Successful) + return { false, stationNumbersCheck.Message }; + + return { true }; +} + ResultWithMessage Ride::ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement) { CoordsXYE problematicTrackElement = {}; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index 10ca50800f..717f4f352e 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -309,6 +309,7 @@ private: void ChainQueues() const; void ConstructMissingEntranceOrExit() const; + ResultWithMessage ChangeStatusDoStationChecks(StationIndex& stationIndex); ResultWithMessage ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCheckTrackValidity(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCreateVehicles(bool isApplying, const CoordsXYE& trackElement); From 3af26d2fe798b351e7a05760fb0317ff95663e7a Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Tue, 20 Dec 2022 22:45:48 +0100 Subject: [PATCH 3/4] Create Ride::ChangeStatusGetStartElement() Co-authored-by: spacek531 --- src/openrct2/ride/Ride.cpp | 49 ++++++++++++++++++++------------------ src/openrct2/ride/Ride.h | 1 + 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 65c74476b1..b107f5d34f 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3883,7 +3883,7 @@ TrackElement* Ride::GetOriginElement(StationIndex stationIndex) const ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) { - CoordsXYE trackElement, problematicTrackElement = {}; + CoordsXYE problematicTrackElement = {}; if (type == RIDE_TYPE_NULL) { @@ -3913,17 +3913,11 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) } } - // z = ride->stations[i].GetBaseZ(); - auto startLoc = GetStation(stationIndex).Start; - trackElement.x = startLoc.x; - trackElement.y = startLoc.y; - trackElement.element = reinterpret_cast(GetOriginElement(stationIndex)); - if (trackElement.element == nullptr) + CoordsXYE trackElement = {}; + message = ChangeStatusGetStartElement(stationIndex, trackElement); + if (!message.Successful) { - // Maze is strange, station start is 0... investigation required - const auto& rtd = GetRideTypeDescriptor(); - if (!rtd.HasFlag(RIDE_TYPE_FLAG_IS_MAZE)) - return { false }; + return message; } if (mode == RideMode::ContinuousCircuit || IsBlockSectioned()) @@ -3949,8 +3943,6 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) */ ResultWithMessage Ride::Open(bool isApplying) { - CoordsXYE trackElement = {}; - // Check to see if construction tool is in use. If it is close the construction window // to set the track to its final state and clean up ghosts. // We can't just call close as it would cause a stack overflow during shop creation @@ -3981,17 +3973,11 @@ ResultWithMessage Ride::Open(bool isApplying) lifecycle_flags |= RIDE_LIFECYCLE_EVER_BEEN_OPENED; } - // z = ride->stations[i].GetBaseZ(); - auto startLoc = GetStation(stationIndex).Start; - trackElement.x = startLoc.x; - trackElement.y = startLoc.y; - trackElement.element = reinterpret_cast(GetOriginElement(stationIndex)); - if (trackElement.element == nullptr) + CoordsXYE trackElement = {}; + message = ChangeStatusGetStartElement(stationIndex, trackElement); + if (!message.Successful) { - // Maze is strange, station start is 0... investigation required - const auto& rtd = GetRideTypeDescriptor(); - if (!rtd.HasFlag(RIDE_TYPE_FLAG_IS_MAZE)) - return { false }; + return message; } message = ChangeStatusCheckCompleteCircuit(trackElement); @@ -5803,6 +5789,23 @@ ResultWithMessage Ride::ChangeStatusDoStationChecks(StationIndex& stationIndex) return { true }; } +ResultWithMessage Ride::ChangeStatusGetStartElement(StationIndex stationIndex, CoordsXYE& trackElement) +{ + auto startLoc = GetStation(stationIndex).Start; + trackElement.x = startLoc.x; + trackElement.y = startLoc.y; + trackElement.element = reinterpret_cast(GetOriginElement(stationIndex)); + if (trackElement.element == nullptr) + { + // Maze is strange, station start is 0... investigation required + const auto& rtd = GetRideTypeDescriptor(); + if (!rtd.HasFlag(RIDE_TYPE_FLAG_IS_MAZE)) + return { false }; + } + + return { true }; +} + ResultWithMessage Ride::ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement) { CoordsXYE problematicTrackElement = {}; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index 717f4f352e..bf8479de43 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -310,6 +310,7 @@ private: void ConstructMissingEntranceOrExit() const; ResultWithMessage ChangeStatusDoStationChecks(StationIndex& stationIndex); + ResultWithMessage ChangeStatusGetStartElement(StationIndex stationIndex, CoordsXYE& trackElement); ResultWithMessage ChangeStatusCheckCompleteCircuit(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCheckTrackValidity(const CoordsXYE& trackElement); ResultWithMessage ChangeStatusCreateVehicles(bool isApplying, const CoordsXYE& trackElement); From ecef614c93ed6fac3c31e71e29b75e740bf99ce4 Mon Sep 17 00:00:00 2001 From: spacek531 Date: Tue, 20 Dec 2022 22:54:38 +0100 Subject: [PATCH 4/4] Split Ride::Test into Ride::Test and Ride::Simulate --- src/openrct2/actions/RideSetStatusAction.cpp | 38 +++++------ src/openrct2/ride/Ride.cpp | 67 ++++++++++++++------ src/openrct2/ride/Ride.h | 3 +- 3 files changed, 69 insertions(+), 39 deletions(-) diff --git a/src/openrct2/actions/RideSetStatusAction.cpp b/src/openrct2/actions/RideSetStatusAction.cpp index a71e22bc3a..a8368218a2 100644 --- a/src/openrct2/actions/RideSetStatusAction.cpp +++ b/src/openrct2/actions/RideSetStatusAction.cpp @@ -90,25 +90,27 @@ GameActions::Result RideSetStatusAction::Query() const return res; } - if (_status == RideStatus::Testing || _status == RideStatus::Simulating) + ResultWithMessage modeSwitchResult = { true }; + switch (_status) { - const auto modeSwitchResult = ride->Test(_status, false); - if (!modeSwitchResult.Successful) - { - res.Error = GameActions::Status::Unknown; - res.ErrorMessage = modeSwitchResult.Message; - return res; - } + case RideStatus::Open: + modeSwitchResult = ride->Open(false); + break; + case RideStatus::Testing: + modeSwitchResult = ride->Test(false); + break; + case RideStatus::Simulating: + modeSwitchResult = ride->Simulate(false); + break; + default: + break; } - else if (_status == RideStatus::Open) + + if (!modeSwitchResult.Successful) { - const auto modeSwitchResult = ride->Open(false); - if (!modeSwitchResult.Successful) - { - res.Error = GameActions::Status::Unknown; - res.ErrorMessage = modeSwitchResult.Message; - return res; - } + res.Error = GameActions::Status::Unknown; + res.ErrorMessage = modeSwitchResult.Message; + return res; } } return GameActions::Result(); @@ -165,7 +167,7 @@ GameActions::Result RideSetStatusAction::Execute() const ride_clear_for_construction(ride); ride->RemovePeeps(); - const auto modeSwitchResult = ride->Test(_status, true); + const auto modeSwitchResult = ride->Simulate(true); if (!modeSwitchResult.Successful) { res.Error = GameActions::Status::Unknown; @@ -207,7 +209,7 @@ GameActions::Result RideSetStatusAction::Execute() const if (_status == RideStatus::Testing) { - const auto modeSwitchResult = ride->Test(_status, true); + const auto modeSwitchResult = ride->Test(true); if (!modeSwitchResult.Successful) { res.Error = GameActions::Status::Unknown; diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index b107f5d34f..ffcdee2dbb 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3881,20 +3881,15 @@ TrackElement* Ride::GetOriginElement(StationIndex stationIndex) const return nullptr; } -ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) +ResultWithMessage Ride::Test(bool isApplying) { - CoordsXYE problematicTrackElement = {}; - if (type == RIDE_TYPE_NULL) { log_warning("Invalid ride type for ride %u", id.ToUnderlying()); return { false }; } - if (newStatus != RideStatus::Simulating) - { - window_close_by_number(WindowClass::RideConstruction, id.ToUnderlying()); - } + window_close_by_number(WindowClass::RideConstruction, id.ToUnderlying()); StationIndex stationIndex = {}; auto message = ChangeStatusDoStationChecks(stationIndex); @@ -3903,14 +3898,11 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) return message; } - if (newStatus != RideStatus::Simulating) + auto entranceExitCheck = ride_check_for_entrance_exit(id); + if (!entranceExitCheck.Successful) { - auto entranceExitCheck = ride_check_for_entrance_exit(id); - if (!entranceExitCheck.Successful) - { - ConstructMissingEntranceOrExit(); - return { false, entranceExitCheck.Message }; - } + ConstructMissingEntranceOrExit(); + return { false, entranceExitCheck.Message }; } CoordsXYE trackElement = {}; @@ -3920,13 +3912,10 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) return message; } - if (mode == RideMode::ContinuousCircuit || IsBlockSectioned()) + message = ChangeStatusCheckCompleteCircuit(trackElement); + if (!message.Successful) { - if (FindTrackGap(trackElement, &problematicTrackElement) && (newStatus != RideStatus::Simulating || IsBlockSectioned())) - { - ride_scroll_to_track_error(problematicTrackElement); - return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT }; - } + return message; } message = ChangeStatusCheckTrackValidity(trackElement); @@ -3937,6 +3926,44 @@ ResultWithMessage Ride::Test(RideStatus newStatus, bool isApplying) return ChangeStatusCreateVehicles(isApplying, trackElement); } + +ResultWithMessage Ride::Simulate(bool isApplying) +{ + CoordsXYE trackElement, problematicTrackElement = {}; + if (type == RIDE_TYPE_NULL) + { + log_warning("Invalid ride type for ride %u", id.ToUnderlying()); + return { false }; + } + + StationIndex stationIndex = {}; + auto message = ChangeStatusDoStationChecks(stationIndex); + if (!message.Successful) + { + return message; + } + + message = ChangeStatusGetStartElement(stationIndex, trackElement); + if (!message.Successful) + { + return message; + } + + if (IsBlockSectioned() && FindTrackGap(trackElement, &problematicTrackElement)) + { + ride_scroll_to_track_error(problematicTrackElement); + return { false, STR_TRACK_IS_NOT_A_COMPLETE_CIRCUIT }; + } + + message = ChangeStatusCheckTrackValidity(trackElement); + if (!message.Successful) + { + return message; + } + + return ChangeStatusCreateVehicles(isApplying, trackElement); +} + /** * * rct2: 0x006B4EEA diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index bf8479de43..69ca0895a9 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -346,7 +346,8 @@ public: void ValidateStations(); ResultWithMessage Open(bool isApplying); - ResultWithMessage Test(RideStatus newStatus, bool isApplying); + ResultWithMessage Test(bool isApplying); + ResultWithMessage Simulate(bool isApplying); RideMode GetDefaultMode() const;