From 50c750e1761bb13b6b31fe10eb704a6fdcfb514e Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 4 May 2020 17:10:57 +0200 Subject: [PATCH 1/2] Clean up GetOriginElement() --- src/openrct2/ride/Ride.cpp | 25 ++++++++++++------------- src/openrct2/ride/Ride.h | 1 + 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index f544bd5211..7f2f779ea0 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -5028,13 +5028,10 @@ static void ride_scroll_to_track_error(CoordsXYE* trackElement) * * rct2: 0x006B4F6B */ -static TileElement* loc_6B4F6B(ride_id_t rideIndex, int32_t x, int32_t y) +TrackElement* Ride::GetOriginElement(StationIndex stationIndex) const { - auto ride = get_ride(rideIndex); - if (ride == nullptr) - return nullptr; - - TileElement* tileElement = map_get_first_element_at({ x, y }); + auto stationLoc = stations[stationIndex].Start; + TileElement* tileElement = map_get_first_element_at(stationLoc); if (tileElement == nullptr) return nullptr; do @@ -5042,19 +5039,21 @@ static TileElement* loc_6B4F6B(ride_id_t rideIndex, int32_t x, int32_t y) if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK) continue; - if (RideTypeDescriptors[ride->type].Flags & RIDE_TYPE_FLAG_FLAT_RIDE) + auto* trackElement = tileElement->AsTrack(); + + if (RideTypeDescriptors[type].Flags & RIDE_TYPE_FLAG_FLAT_RIDE) { - if (!(FlatRideTrackSequenceProperties[tileElement->AsTrack()->GetTrackType()][0] & TRACK_SEQUENCE_FLAG_ORIGIN)) + if (!(FlatRideTrackSequenceProperties[trackElement->GetTrackType()][0] & TRACK_SEQUENCE_FLAG_ORIGIN)) continue; } else { - if (!(TrackSequenceProperties[tileElement->AsTrack()->GetTrackType()][0] & TRACK_SEQUENCE_FLAG_ORIGIN)) + if (!(TrackSequenceProperties[trackElement->GetTrackType()][0] & TRACK_SEQUENCE_FLAG_ORIGIN)) continue; } - if (tileElement->AsTrack()->GetRideIndex() == rideIndex) - return tileElement; + if (trackElement->GetRideIndex() == id) + return trackElement; } while (!(tileElement++)->IsLastForTile()); return nullptr; @@ -5098,7 +5097,7 @@ int32_t ride_is_valid_for_test(Ride* ride, int32_t status, bool isApplying) auto startLoc = ride->stations[stationIndex].Start; trackElement.x = startLoc.x; trackElement.y = startLoc.y; - trackElement.element = loc_6B4F6B(ride->id, trackElement.x, trackElement.y); + trackElement.element = reinterpret_cast(ride->GetOriginElement(stationIndex)); if (trackElement.element == nullptr) { // Maze is strange, station start is 0... investigation required @@ -5234,7 +5233,7 @@ int32_t ride_is_valid_for_open(Ride* ride, int32_t goingToBeOpen, bool isApplyin auto startLoc = ride->stations[stationIndex].Start; trackElement.x = startLoc.x; trackElement.y = startLoc.y; - trackElement.element = loc_6B4F6B(ride->id, trackElement.x, trackElement.y); + trackElement.element = reinterpret_cast(ride->GetOriginElement(stationIndex)); if (trackElement.element == nullptr) { // Maze is strange, station start is 0... investigation required diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index 5ce566c703..7e8c6e1386 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -431,6 +431,7 @@ public: uint64_t GetAvailableModes() const; const RideTypeDescriptor& GetRideTypeDescriptor() const; + TrackElement* GetOriginElement(StationIndex stationIndex) const; }; #pragma pack(push, 1) From f88e1c03a0fb0a01b9e63215526469fc66368a3b Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Mon, 4 May 2020 17:16:29 +0200 Subject: [PATCH 2/2] Fix #11624: Crash when testing ride with a ghost end-of-station --- src/openrct2/ride/Ride.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 7f2f779ea0..69e7f8536c 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3918,7 +3918,14 @@ static int32_t ride_check_block_brakes(CoordsXYE* input, CoordsXYE* output) */ static bool ride_check_track_contains_inversions(CoordsXYE* input, CoordsXYE* output) { - ride_id_t rideIndex = input->element->AsTrack()->GetRideIndex(); + if (input->element == nullptr) + return false; + + const auto* trackElement = input->element->AsTrack(); + if (trackElement == nullptr) + return false; + + ride_id_t rideIndex = trackElement->GetRideIndex(); auto ride = get_ride(rideIndex); if (ride != nullptr && ride->type == RIDE_TYPE_MAZE) return true; @@ -3966,7 +3973,14 @@ static bool ride_check_track_contains_inversions(CoordsXYE* input, CoordsXYE* ou */ static bool ride_check_track_contains_banked(CoordsXYE* input, CoordsXYE* output) { - auto rideIndex = input->element->AsTrack()->GetRideIndex(); + if (input->element == nullptr) + return false; + + const auto* trackElement = input->element->AsTrack(); + if (trackElement == nullptr) + return false; + + auto rideIndex = trackElement->GetRideIndex(); auto ride = get_ride(rideIndex); if (ride == nullptr) return false; @@ -5263,8 +5277,8 @@ int32_t ride_is_valid_for_open(Ride* ride, int32_t goingToBeOpen, bool isApplyin if (ride->subtype != RIDE_ENTRY_INDEX_NULL) { - rct_ride_entry* rideType = get_ride_entry(ride->subtype); - if (rideType->flags & RIDE_ENTRY_FLAG_NO_INVERSIONS) + rct_ride_entry* rideEntry = get_ride_entry(ride->subtype); + if (rideEntry->flags & RIDE_ENTRY_FLAG_NO_INVERSIONS) { gGameCommandErrorText = STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN; if (ride_check_track_contains_inversions(&trackElement, &problematicTrackElement)) @@ -5273,7 +5287,7 @@ int32_t ride_is_valid_for_open(Ride* ride, int32_t goingToBeOpen, bool isApplyin return 0; } } - if (rideType->flags & RIDE_ENTRY_FLAG_NO_BANKED_TRACK) + if (rideEntry->flags & RIDE_ENTRY_FLAG_NO_BANKED_TRACK) { gGameCommandErrorText = STR_TRACK_UNSUITABLE_FOR_TYPE_OF_TRAIN; if (ride_check_track_contains_banked(&trackElement, &problematicTrackElement))