From a3fe00f0a374d8fb960c7d1aa74e57f7b2842389 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 3 Aug 2019 17:27:50 +0100 Subject: [PATCH] Refactor ride list to a manager with iterator --- .../windows/EditorInventionsList.cpp | 8 +- src/openrct2-ui/windows/Guest.cpp | 41 ++- src/openrct2-ui/windows/NewCampaign.cpp | 78 ++--- src/openrct2-ui/windows/Ride.cpp | 36 ++- src/openrct2/actions/RideCreateAction.hpp | 6 +- src/openrct2/actions/RideSetPriceAction.hpp | 49 +-- src/openrct2/interface/InteractiveConsole.cpp | 11 +- src/openrct2/peep/Guest.cpp | 17 +- src/openrct2/peep/Peep.h | 4 +- src/openrct2/rct1/S4Importer.cpp | 2 +- src/openrct2/rct2/S6Exporter.cpp | 7 +- src/openrct2/rct2/S6Importer.cpp | 2 +- src/openrct2/ride/Ride.cpp | 287 ++++++++---------- src/openrct2/ride/Ride.h | 99 +++++- src/openrct2/ride/RideRatings.cpp | 11 +- src/openrct2/ride/RideRatings.h | 2 +- src/openrct2/ride/RideTypes.h | 1 + src/openrct2/scenario/Scenario.cpp | 20 +- src/openrct2/world/Park.cpp | 8 +- test/tests/RideRatings.cpp | 39 +-- 20 files changed, 387 insertions(+), 341 deletions(-) diff --git a/src/openrct2-ui/windows/EditorInventionsList.cpp b/src/openrct2-ui/windows/EditorInventionsList.cpp index 43fd558152..d521bc2673 100644 --- a/src/openrct2-ui/windows/EditorInventionsList.cpp +++ b/src/openrct2-ui/windows/EditorInventionsList.cpp @@ -186,13 +186,9 @@ static void research_rides_setup() } // Set research required for rides in use - for (uint16_t rideIndex = 0; rideIndex < MAX_RIDES; rideIndex++) + for (const auto &ride : GetRideManager()) { - auto ride = get_ride(rideIndex); - if (ride->type != RIDE_TYPE_NULL) - { - Editor::SetSelectedObject(OBJECT_TYPE_RIDE, ride->subtype, OBJECT_SELECTION_FLAG_SELECTED); - } + Editor::SetSelectedObject(OBJECT_TYPE_RIDE, ride.subtype, OBJECT_SELECTION_FLAG_SELECTED); } } diff --git a/src/openrct2-ui/windows/Guest.cpp b/src/openrct2-ui/windows/Guest.cpp index 5c05f09de0..b35588e437 100644 --- a/src/openrct2-ui/windows/Guest.cpp +++ b/src/openrct2-ui/windows/Guest.cpp @@ -1571,34 +1571,31 @@ void window_guest_rides_update(rct_window* w) widget_invalidate(w, WIDX_TAB_2); widget_invalidate(w, WIDX_TAB_3); - Peep* peep = GET_PEEP(w->number); - - // Every 2048 ticks do a full window_invalidate - int32_t number_of_ticks = gScenarioTicks - peep->time_in_park; - if (!(number_of_ticks & 0x7FF)) - window_invalidate(w); - - uint8_t curr_list_position = 0; - for (ride_id_t ride_id = 0; ride_id < MAX_RIDES; ride_id++) + auto peep = GET_PEEP(w->number); + auto guest = peep->AsGuest(); + if (guest != nullptr) { - // Offset to the ride_id bit in peep_rides_been_on - uint8_t ride_id_bit = ride_id % 8; - uint8_t ride_id_offset = ride_id / 8; - if (peep->rides_been_on[ride_id_offset] & (1 << ride_id_bit)) + // Every 2048 ticks do a full window_invalidate + int32_t number_of_ticks = gScenarioTicks - peep->time_in_park; + if (!(number_of_ticks & 0x7FF)) + window_invalidate(w); + + uint8_t curr_list_position = 0; + for (const auto& ride : GetRideManager()) { - Ride* ride = get_ride(ride_id); - if (gRideClassifications[ride->type] == RIDE_CLASS_RIDE) + if (guest->HasRidden(&ride) && gRideClassifications[ride.type] == RIDE_CLASS_RIDE) { - w->list_item_positions[curr_list_position] = ride_id; + w->list_item_positions[curr_list_position] = ride.id; curr_list_position++; } } - } - // If there are new items - if (w->no_list_items != curr_list_position) - { - w->no_list_items = curr_list_position; - window_invalidate(w); + + // If there are new items + if (w->no_list_items != curr_list_position) + { + w->no_list_items = curr_list_position; + window_invalidate(w); + } } } diff --git a/src/openrct2-ui/windows/NewCampaign.cpp b/src/openrct2-ui/windows/NewCampaign.cpp index 78748c3ec5..72a4448b7f 100644 --- a/src/openrct2-ui/windows/NewCampaign.cpp +++ b/src/openrct2-ui/windows/NewCampaign.cpp @@ -89,7 +89,7 @@ static rct_window_event_list window_new_campaign_events = { }; // clang-format on -static uint8_t window_new_campaign_rides[MAX_RIDES]; +static std::vector window_new_campaign_rides; static uint8_t window_new_campaign_shop_items[64]; static int32_t ride_value_compare(const void* a, const void* b) @@ -116,11 +116,7 @@ static int32_t ride_name_compare(const void* a, const void* b) */ rct_window* window_new_campaign_open(int16_t campaignType) { - rct_window* w; - Ride* ride; - int32_t i, numApplicableRides; - - w = window_bring_to_front_by_class(WC_NEW_CAMPAIGN); + auto w = window_bring_to_front_by_class(WC_NEW_CAMPAIGN); if (w != nullptr) { if (w->campaign.campaign_type == campaignType) @@ -148,36 +144,30 @@ rct_window* window_new_campaign_open(int16_t campaignType) w->campaign.ride_id = SELECTED_RIDE_UNDEFINED; // Get all applicable rides - numApplicableRides = 0; - window_new_campaign_rides[0] = RIDE_ID_NULL; - FOR_ALL_RIDES (i, ride) + window_new_campaign_rides.clear(); + for (const auto& ride : GetRideManager()) { - if (ride->status != RIDE_STATUS_OPEN) + if (ride.status == RIDE_STATUS_OPEN) { - continue; + if (!ride_type_has_flag( + ride.type, + RIDE_TYPE_FLAG_IS_SHOP | RIDE_TYPE_FLAG_SELLS_FOOD | RIDE_TYPE_FLAG_SELLS_DRINKS + | RIDE_TYPE_FLAG_IS_BATHROOM)) + { + window_new_campaign_rides.push_back(ride.id); + } } - if (ride_type_has_flag( - ride->type, - RIDE_TYPE_FLAG_IS_SHOP | RIDE_TYPE_FLAG_SELLS_FOOD | RIDE_TYPE_FLAG_SELLS_DRINKS | RIDE_TYPE_FLAG_IS_BATHROOM)) - { - continue; - } - - window_new_campaign_rides[numApplicableRides++] = i; } // Take top 128 most valuable rides - if (numApplicableRides > DROPDOWN_ITEMS_MAX_SIZE) + if (window_new_campaign_rides.size() > DROPDOWN_ITEMS_MAX_SIZE) { - qsort(window_new_campaign_rides, numApplicableRides, sizeof(uint8_t), ride_value_compare); - numApplicableRides = DROPDOWN_ITEMS_MAX_SIZE; + qsort(window_new_campaign_rides.data(), window_new_campaign_rides.size(), sizeof(ride_id_t), ride_value_compare); + window_new_campaign_rides.resize(DROPDOWN_ITEMS_MAX_SIZE); } // Sort rides by name - qsort(window_new_campaign_rides, numApplicableRides, sizeof(uint8_t), ride_name_compare); - - window_new_campaign_rides[numApplicableRides] = RIDE_ID_NULL; - + qsort(window_new_campaign_rides.data(), window_new_campaign_rides.size(), sizeof(ride_id_t), ride_name_compare); return w; } @@ -279,27 +269,25 @@ static void window_new_campaign_mousedown(rct_window* w, rct_widgetindex widgetI else { int32_t numItems = 0; - for (int32_t i = 0; i < DROPDOWN_ITEMS_MAX_SIZE; i++) + for (auto rideId : window_new_campaign_rides) { - if (window_new_campaign_rides[i] == RIDE_ID_NULL) - break; - - auto ride = get_ride(window_new_campaign_rides[i]); - if (ride == nullptr) - break; - - // HACK until dropdown items have longer argument buffers - gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL; - if (ride->custom_name.empty()) + auto ride = get_ride(rideId); + if (ride != nullptr) { - ride->FormatNameTo(&gDropdownItemsArgs[i]); + // HACK until dropdown items have longer argument buffers + gDropdownItemsFormat[numItems] = STR_DROPDOWN_MENU_LABEL; + if (ride->custom_name.empty()) + { + ride->FormatNameTo(&gDropdownItemsArgs[numItems]); + } + else + { + gDropdownItemsFormat[numItems] = STR_OPTIONS_DROPDOWN_ITEM; + set_format_arg_on( + (uint8_t*)&gDropdownItemsArgs[numItems], 0, const char*, ride->custom_name.c_str()); + } + numItems++; } - else - { - gDropdownItemsFormat[i] = STR_OPTIONS_DROPDOWN_ITEM; - set_format_arg_on((uint8_t*)&gDropdownItemsArgs[i], 0, const char*, ride->custom_name.c_str()); - } - numItems++; } window_dropdown_show_text_custom_width( @@ -328,7 +316,7 @@ static void window_new_campaign_dropdown(rct_window* w, rct_widgetindex widgetIn if (widgetIndex != WIDX_RIDE_DROPDOWN_BUTTON) return; - if (dropdownIndex == -1) + if (dropdownIndex < 0 || dropdownIndex >= window_new_campaign_rides.size()) return; if (w->campaign.campaign_type == ADVERTISING_CAMPAIGN_FOOD_OR_DRINK_FREE) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index 4fbd95d0c7..971f80b797 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -958,7 +958,7 @@ struct ride_overall_view { uint8_t zoom; }; -static ride_overall_view ride_overall_views[MAX_RIDES] = {}; +static std::vector ride_overall_views = {}; static constexpr const int32_t window_ride_tab_animation_divisor[] = { 0, 0, 2, 2, 4, 2, 8, 8, 2, 0 }; static constexpr const int32_t window_ride_tab_animation_frames[] = { 0, 0, 4, 16, 8, 16, 8, 8, 8, 0 }; @@ -1498,10 +1498,15 @@ static void window_ride_update_overall_view(Ride* ride) maxz = std::max(maxz, z2); } - auto view = &ride_overall_views[ride->id]; - view->x = (minx + maxx) / 2 + 16; - view->y = (miny + maxy) / 2 + 16; - view->z = (minz + maxz) / 2 - 8; + if (ride->id >= ride_overall_views.size()) + { + ride_overall_views.resize(ride->id + 1); + } + + auto& view = ride_overall_views[ride->id]; + view.x = (minx + maxx) / 2 + 16; + view.y = (miny + maxy) / 2 + 16; + view.z = (minz + maxz) / 2 - 8; // Calculate size to determine from how far away to view the ride int32_t dx = maxx - minx; @@ -1514,12 +1519,12 @@ static void window_ride_update_overall_view(Ride* ride) { // Each farther zoom level shows twice as many tiles (log) // Appropriate zoom is lowered by one to fill the entire view with the ride - view->zoom = std::clamp(std::ceil(std::log(size / 80)) - 1, 0, 3); + view.zoom = std::clamp(std::ceil(std::log(size / 80)) - 1, 0, 3); } else { // Small rides or stalls are zoomed in all the way. - view->zoom = 0; + view.zoom = 0; } } @@ -1906,14 +1911,15 @@ static void window_ride_init_viewport(rct_window* w) w->viewport_focus_coordinates.var_480 = 0; } - ride_overall_view* view = &ride_overall_views[w->number]; - - focus.coordinate.x = view->x; - focus.coordinate.y = view->y; - focus.coordinate.z = view->z; - focus.coordinate.zoom = view->zoom; - - focus.sprite.type |= VIEWPORT_FOCUS_TYPE_COORDINATE; + if (w->number < ride_overall_views.size()) + { + const auto& view = ride_overall_views[w->number]; + focus.coordinate.x = view.x; + focus.coordinate.y = view.y; + focus.coordinate.z = view.z; + focus.coordinate.zoom = view.zoom; + focus.sprite.type |= VIEWPORT_FOCUS_TYPE_COORDINATE; + } } focus.coordinate.var_480 = w->viewport_focus_coordinates.var_480; diff --git a/src/openrct2/actions/RideCreateAction.hpp b/src/openrct2/actions/RideCreateAction.hpp index 759683d904..e631af55d4 100644 --- a/src/openrct2/actions/RideCreateAction.hpp +++ b/src/openrct2/actions/RideCreateAction.hpp @@ -74,7 +74,7 @@ public: GameActionResult::Ptr Query() const override { - ride_id_t rideIndex = ride_get_empty_slot(); + auto rideIndex = GetNextFreeRideId(); if (rideIndex == RIDE_ID_NULL) { // No more free slots available. @@ -119,11 +119,11 @@ public: auto res = MakeResult(); int32_t rideEntryIndex = ride_get_entry_index(_rideType, _subType); - ride_id_t rideIndex = ride_get_empty_slot(); + auto rideIndex = GetNextFreeRideId(); res->rideIndex = rideIndex; - auto ride = get_ride(rideIndex); + auto ride = GetOrAllocateRide(rideIndex); rideEntry = get_ride_entry(rideEntryIndex); if (rideEntry == nullptr) { diff --git a/src/openrct2/actions/RideSetPriceAction.hpp b/src/openrct2/actions/RideSetPriceAction.hpp index bee8a205e8..cc056176a0 100644 --- a/src/openrct2/actions/RideSetPriceAction.hpp +++ b/src/openrct2/actions/RideSetPriceAction.hpp @@ -159,35 +159,42 @@ public: private: void RideSetCommonPrice(int32_t shopItem) const { - Ride* ride = get_ride(0); - for (uint8_t rideId = 0; rideId < MAX_RIDES; rideId++, ride++) + for (auto& ride : GetRideManager()) { - // Unplaced rides have a type of NULL - if (ride->type == RIDE_TYPE_NULL) - continue; - - rct_ride_entry* rideEntry = get_ride_entry(ride->subtype); - - if (ride->type != RIDE_TYPE_TOILETS || shopItem != SHOP_ITEM_ADMISSION) + auto invalidate = false; + auto rideEntry = get_ride_entry(ride.subtype); + if (ride.type == RIDE_TYPE_TOILETS && shopItem == SHOP_ITEM_ADMISSION) { - if (rideEntry->shop_item == shopItem) + if (ride.price != _price) { - ride->price = _price; - window_invalidate_by_number(WC_RIDE, rideId); + ride.price = _price; + invalidate = true; } } - else + else if (rideEntry != nullptr && rideEntry->shop_item == shopItem) { - ride->price = _price; - window_invalidate_by_number(WC_RIDE, rideId); + if (ride.price != _price) + { + ride.price = _price; + invalidate = true; + } } - - // If the shop item is the same or an on-ride photo - if (rideEntry->shop_item_secondary == shopItem - || (rideEntry->shop_item_secondary == SHOP_ITEM_NONE && shop_item_is_photo(shopItem))) + if (rideEntry != nullptr) { - ride->price_secondary = _price; - window_invalidate_by_number(WC_RIDE, rideId); + // If the shop item is the same or an on-ride photo + if (rideEntry->shop_item_secondary == shopItem + || (rideEntry->shop_item_secondary == SHOP_ITEM_NONE && shop_item_is_photo(shopItem))) + { + if (ride.price_secondary != _price) + { + ride.price_secondary = _price; + invalidate = true; + } + } + } + if (invalidate) + { + window_invalidate_by_number(WC_RIDE, ride.id); } } } diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index 7b22bd82e9..47865996c9 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -1225,16 +1225,7 @@ static int32_t cc_show_limits(InteractiveConsole& console, [[maybe_unused]] cons map_reorganise_elements(); int32_t tileElementCount = gNextFreeTileElement - gTileElements - 1; - int32_t rideCount = 0; - for (int32_t i = 0; i < MAX_RIDES; ++i) - { - Ride* ride = get_ride(i); - if (ride->type != RIDE_TYPE_NULL) - { - rideCount++; - } - } - + int32_t rideCount = ride_get_count(); int32_t spriteCount = 0; for (int32_t i = 1; i < SPRITE_LIST_COUNT; ++i) { diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index 269cf1bd1f..50f1833e83 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -1883,18 +1883,17 @@ Ride* Guest::FindBestRideToGoOn() // Pick the most exciting ride auto rideConsideration = FindRidesToGoOn(); Ride* mostExcitingRide = nullptr; - for (int32_t i = 0; i < MAX_RIDES; i++) + for (auto& ride : GetRideManager()) { - if (rideConsideration[i]) + if (rideConsideration.size() > ride.id && rideConsideration[ride.id]) { - auto ride = get_ride(i); - if (!(ride->lifecycle_flags & RIDE_LIFECYCLE_QUEUE_FULL)) + if (!(ride.lifecycle_flags & RIDE_LIFECYCLE_QUEUE_FULL)) { - if (ShouldGoOnRide(ride, 0, false, true) && ride_has_ratings(ride)) + if (ShouldGoOnRide(&ride, 0, false, true) && ride_has_ratings(&ride)) { - if (mostExcitingRide == nullptr || ride->excitement > mostExcitingRide->excitement) + if (mostExcitingRide == nullptr || ride.excitement > mostExcitingRide->excitement) { - mostExcitingRide = ride; + mostExcitingRide = &ride; } } } @@ -2372,13 +2371,13 @@ void Guest::SpendMoney(money16& peep_expend_type, money32 amount) audio_play_sound_at_location(SoundId::Purchase, x, y, z); } -void Guest::SetHasRidden(Ride* ride) +void Guest::SetHasRidden(const Ride* ride) { rides_been_on[ride->id / 8] |= 1 << (ride->id % 8); SetHasRiddenRideType(ride->type); } -bool Guest::HasRidden(Ride* ride) const +bool Guest::HasRidden(const Ride* ride) const { return rides_been_on[ride->id / 8] & (1 << (ride->id % 8)); } diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index d8bd7bfcef..24f698c623 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -763,8 +763,8 @@ public: bool UpdateWalkingFindBin(); void SpendMoney(money16& peep_expend_type, money32 amount); void SpendMoney(money32 amount); - void SetHasRidden(Ride* ride); - bool HasRidden(Ride* ride) const; + void SetHasRidden(const Ride* ride); + bool HasRidden(const Ride* ride) const; void SetHasRiddenRideType(int32_t rideType); bool HasRiddenRideType(int32_t rideType) const; int32_t HasFoodStandardFlag() const; diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index c3940367ae..7776be6070 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -741,7 +741,7 @@ private: { if (_s4.rides[i].type != RIDE_TYPE_NULL) { - ImportRide(get_ride(i), &_s4.rides[i], i); + ImportRide(GetOrAllocateRide(i), &_s4.rides[i], i); } } } diff --git a/src/openrct2/rct2/S6Exporter.cpp b/src/openrct2/rct2/S6Exporter.cpp index 5d7554e24c..b2acae8725 100644 --- a/src/openrct2/rct2/S6Exporter.cpp +++ b/src/openrct2/rct2/S6Exporter.cpp @@ -462,9 +462,14 @@ void S6Exporter::ExportParkName() void S6Exporter::ExportRides() { + const Ride nullRide{}; for (int32_t index = 0; index < RCT12_MAX_RIDES_IN_PARK; index++) { - auto src = get_ride(index); + const auto* src = get_ride(index); + if (src == nullptr) + { + src = &nullRide; + } auto dst = &_s6.rides[index]; *dst = {}; if (src->type == RIDE_TYPE_NULL) diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index c413877193..bb80667f2a 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -482,7 +482,7 @@ public: auto src = &_s6.rides[index]; if (src->type != RIDE_TYPE_NULL) { - auto dst = get_ride(index); + auto dst = GetOrAllocateRide(index); ImportRide(dst, src, index); } } diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index d6b78fb3ea..3c65cecf4f 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -150,7 +150,7 @@ static constexpr const int32_t RideInspectionInterval[] = { 10, 20, 30, 45, 60, 120, 0, 0, }; -Ride gRideList[MAX_RIDES]; +static std::vector _rides; uint16_t gRideCount; bool gGotoStartPlacementMode = false; @@ -212,18 +212,65 @@ static void ride_music_update(Ride* ride); static void ride_shop_connected(Ride* ride); void loc_6DDF9C(Ride* ride, TileElement* tileElement); -Ride* get_ride(int32_t index) +RideManager GetRideManager() { - if (index < 0 || index >= MAX_RIDES) + return {}; +} + +RideManager::Iterator RideManager::begin() +{ + return RideManager::Iterator(*this, 0, _rides.size()); +} + +RideManager::Iterator RideManager::end() +{ + return RideManager::Iterator(*this, _rides.size(), _rides.size()); +} + +ride_id_t GetNextFreeRideId() +{ + size_t result = _rides.size(); + for (size_t i = 0; i < _rides.size(); i++) { - log_error("invalid index %d for ride", index); - return nullptr; + if (_rides[i].type == RIDE_TYPE_NULL) + { + result = i; + break; + } } - auto ride = &gRideList[index]; -#ifdef DEBUG - assert(ride->id == index); -#endif - return ride; + if (result >= RIDE_ID_NULL) + { + return RIDE_ID_NULL; + } + return (ride_id_t)result; +} + +Ride* GetOrAllocateRide(ride_id_t index) +{ + Ride* result{}; + if (index < _rides.size()) + { + result = &_rides[index]; + } + else + { + result = &_rides.emplace_back(); + } + result->id = index; + return result; +} + +Ride* get_ride(ride_id_t index) +{ + if (index < _rides.size()) + { + auto& ride = _rides[index]; + if (ride.type != RIDE_TYPE_NULL) + { + return &ride; + } + } + return nullptr; } rct_ride_entry* get_ride_entry(int32_t index) @@ -321,12 +368,11 @@ uint8_t* get_ride_entry_indices_for_ride_type(uint8_t rideType) int32_t ride_get_count() { - Ride* ride; - int32_t i, count = 0; - - FOR_ALL_RIDES (i, ride) + int32_t count = 0; + for ([[maybe_unused]] auto& ride : GetRideManager()) + { count++; - + } return count; } @@ -398,13 +444,11 @@ void Ride::QueueInsertGuestAtFront(int32_t stationIndex, Peep* peep) */ void ride_update_favourited_stat() { - int32_t i; - Ride* ride; uint16_t spriteIndex; Peep* peep; - FOR_ALL_RIDES (i, ride) - ride->guests_favourite = 0; + for (auto& ride : GetRideManager()) + ride.guests_favourite = 0; FOR_ALL_PEEPS (spriteIndex, peep) { @@ -412,9 +456,12 @@ void ride_update_favourited_stat() return; if (peep->favourite_ride != RIDE_ID_NULL) { - ride = get_ride(peep->favourite_ride); - ride->guests_favourite++; - ride->window_invalidate_flags |= RIDE_INVALIDATE_RIDE_CUSTOMER; + auto ride = get_ride(peep->favourite_ride); + if (ride != nullptr) + { + ride->guests_favourite++; + ride->window_invalidate_flags |= RIDE_INVALIDATE_RIDE_CUSTOMER; + } } } @@ -952,13 +999,8 @@ bool Ride::SupportsStatus(int32_t s) const */ void ride_init_all() { - for (int32_t i = 0; i < MAX_RIDES; i++) - { - auto ride = &gRideList[i]; - *ride = {}; - ride->id = i; - ride->type = RIDE_TYPE_NULL; - } + _rides.clear(); + _rides.shrink_to_fit(); } /** @@ -967,11 +1009,8 @@ void ride_init_all() */ void reset_all_ride_build_dates() { - int32_t i; - Ride* ride; - - FOR_ALL_RIDES (i, ride) - ride->build_date -= gDateMonthsElapsed; + for (auto& ride : GetRideManager()) + ride.build_date -= gDateMonthsElapsed; } #pragma endregion @@ -2058,23 +2097,20 @@ int32_t ride_initialise_construction_window(Ride* ride) */ void Ride::UpdateAll() { - Ride* ride; - int32_t i; - // Remove all rides if scenario editor if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) { if (gS6Info.editor_step <= EDITOR_STEP_INVENTIONS_LIST_SET_UP) - FOR_ALL_RIDES (i, ride) - ride->Delete(); + for (auto& ride : GetRideManager()) + ride.Delete(); return; } window_update_viewport_ride_music(); // Update rides - FOR_ALL_RIDES (i, ride) - ride->Update(); + for (auto& ride : GetRideManager()) + ride.Update(); ride_music_update_final(); } @@ -3040,13 +3076,10 @@ void ride_measurements_update() return; // For each ride measurement - ride_id_t i{}; - Ride* ride{}; - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - auto measurement = ride->measurement.get(); - if (measurement != nullptr && (ride->lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK) - && ride->status != RIDE_STATUS_SIMULATING) + auto measurement = ride.measurement.get(); + if (measurement != nullptr && (ride.lifecycle_flags & RIDE_LIFECYCLE_ON_TRACK) && ride.status != RIDE_STATUS_SIMULATING) { if (measurement->flags & RIDE_MEASUREMENT_FLAG_RUNNING) { @@ -3055,9 +3088,9 @@ void ride_measurements_update() else { // For each vehicle - for (int32_t j = 0; j < ride->num_vehicles; j++) + for (int32_t j = 0; j < ride.num_vehicles; j++) { - uint16_t vehicleSpriteIdx = ride->vehicles[j]; + uint16_t vehicleSpriteIdx = ride.vehicles[j]; if (vehicleSpriteIdx != SPRITE_INDEX_NULL) { auto vehicle = GET_VEHICLE(vehicleSpriteIdx); @@ -3086,17 +3119,15 @@ static void ride_free_old_measurements() size_t numRideMeasurements; do { - ride_id_t i{}; - Ride* ride{}; Ride* lruRide{}; numRideMeasurements = 0; - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - if (ride->measurement != nullptr) + if (ride.measurement != nullptr) { - if (lruRide == nullptr || ride->measurement->last_use_tick > lruRide->measurement->last_use_tick) + if (lruRide == nullptr || ride.measurement->last_use_tick > lruRide->measurement->last_use_tick) { - lruRide = ride; + lruRide = &ride; } numRideMeasurements++; } @@ -3175,13 +3206,11 @@ vehicle_colour ride_get_vehicle_colour(Ride* ride, int32_t vehicleIndex) static bool ride_does_vehicle_colour_exist(uint8_t ride_sub_type, vehicle_colour* vehicleColour) { - int32_t i; - Ride* ride2; - FOR_ALL_RIDES (i, ride2) + for (auto& ride : GetRideManager()) { - if (ride2->subtype != ride_sub_type) + if (ride.subtype != ride_sub_type) continue; - if (ride2->vehicle_colours[0].Body != vehicleColour->main) + if (ride.vehicle_colours[0].Body != vehicleColour->main) continue; return false; } @@ -3262,20 +3291,17 @@ void ride_set_vehicle_colours_to_random_preset(Ride* ride, uint8_t preset_index) */ void ride_check_all_reachable() { - Ride* ride; - int32_t i; - - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - if (ride->connected_message_throttle != 0) - ride->connected_message_throttle--; - if (ride->status != RIDE_STATUS_OPEN || ride->connected_message_throttle != 0) + if (ride.connected_message_throttle != 0) + ride.connected_message_throttle--; + if (ride.status != RIDE_STATUS_OPEN || ride.connected_message_throttle != 0) continue; - if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) - ride_shop_connected(ride); + if (ride_type_has_flag(ride.type, RIDE_TYPE_FLAG_IS_SHOP)) + ride_shop_connected(&ride); else - ride_entrance_exit_connected(ride); + ride_entrance_exit_connected(&ride); } } @@ -5660,19 +5686,6 @@ void Ride::StopGuestsQueuing() } } -ride_id_t ride_get_empty_slot() -{ - for (ride_id_t i = 0; i < MAX_RIDES; i++) - { - Ride* ride = get_ride(i); - if (ride->type == RIDE_TYPE_NULL) - { - return i; - } - } - return RIDE_ID_NULL; -} - uint8_t Ride::GetDefaultMode() const { const rct_ride_entry* rideEntry = get_ride_entry(subtype); @@ -5695,18 +5708,15 @@ uint8_t Ride::GetDefaultMode() const static bool ride_with_colour_config_exists(uint8_t ride_type, const TrackColour* colours) { - Ride* ride; - int32_t i; - - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - if (ride->type != ride_type) + if (ride.type != ride_type) continue; - if (ride->track_colour[0].main != colours->main) + if (ride.track_colour[0].main != colours->main) continue; - if (ride->track_colour[0].additional != colours->additional) + if (ride.track_colour[0].additional != colours->additional) continue; - if (ride->track_colour[0].supports != colours->supports) + if (ride.track_colour[0].supports != colours->supports) continue; return true; @@ -5719,15 +5729,13 @@ bool Ride::NameExists(const std::string_view& name, ride_id_t excludeRideId) char buffer[256]{}; uint32_t formatArgs[32]{}; - Ride* ride; - int32_t i; - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - if (i != excludeRideId) + if (ride.id != excludeRideId) { - ride->FormatNameTo(formatArgs); + ride.FormatNameTo(formatArgs); format_string(buffer, 256, STR_STRINGID, formatArgs); - if (std::string_view(buffer) == name && ride_has_any_track_elements(ride)) + if (std::string_view(buffer) == name && ride_has_any_track_elements(&ride)) { return true; } @@ -5796,14 +5804,11 @@ void Ride::SetColourPreset(uint8_t index) money32 ride_get_common_price(Ride* forRide) { - Ride* ride; - int32_t i; - - FOR_ALL_RIDES (i, ride) + for (const auto& ride : GetRideManager()) { - if (ride->type == forRide->type && ride != forRide) + if (ride.type == forRide->type && &ride != forRide) { - return ride->price; + return ride.price; } } @@ -6112,24 +6117,6 @@ bool ride_has_any_track_elements(const Ride* ride) return false; } -void ride_all_has_any_track_elements(bool* rideIndexArray) -{ - tile_element_iterator it; - - std::fill_n(rideIndexArray, MAX_RIDES, false); - - tile_element_iterator_begin(&it); - while (tile_element_iterator_next(&it)) - { - if (it.element->GetType() != TILE_ELEMENT_TYPE_TRACK) - continue; - if (it.element->IsGhost()) - continue; - - rideIndexArray[it.element->AsTrack()->GetRideIndex()] = true; - } -} - /** * * rct2: 0x006847BA @@ -7240,11 +7227,9 @@ void Ride::Crash(uint8_t vehicleIndex) void ride_reset_all_names() { - int32_t i; - Ride* ride; - FOR_ALL_RIDES (i, ride) + for (auto& ride : GetRideManager()) { - ride->SetNameToDefault(); + ride.SetNameToDefault(); } } @@ -7615,13 +7600,11 @@ int32_t get_booster_speed(uint8_t rideType, int32_t rawSpeed) void fix_invalid_vehicle_sprite_sizes() { - Ride* ride; - uint16_t i; - FOR_ALL_RIDES (i, ride) + for (const auto& ride : GetRideManager()) { for (uint16_t j = 0; j < MAX_VEHICLES_PER_RIDE; j++) { - uint16_t rideSpriteIndex = ride->vehicles[j]; + uint16_t rideSpriteIndex = ride.vehicles[j]; while (rideSpriteIndex != SPRITE_INDEX_NULL) { rct_vehicle* vehicle = try_get_vehicle(rideSpriteIndex); @@ -7727,14 +7710,12 @@ void determine_ride_entrance_and_exit_locations() { log_verbose("Inspecting ride entrance / exit locations"); - ride_id_t rideIndex; - Ride* ride; - FOR_ALL_RIDES (rideIndex, ride) + for (auto& ride : GetRideManager()) { for (int32_t stationIndex = 0; stationIndex < MAX_STATIONS; stationIndex++) { - TileCoordsXYZD entranceLoc = ride->stations[stationIndex].Entrance; - TileCoordsXYZD exitLoc = ride->stations[stationIndex].Exit; + TileCoordsXYZD entranceLoc = ride.stations[stationIndex].Entrance; + TileCoordsXYZD exitLoc = ride.stations[stationIndex].Exit; bool fixEntrance = false; bool fixExit = false; @@ -7744,14 +7725,14 @@ void determine_ride_entrance_and_exit_locations() const EntranceElement* entranceElement = map_get_ride_entrance_element_at( entranceLoc.x * 32, entranceLoc.y * 32, entranceLoc.z, false); - if (entranceElement == nullptr || entranceElement->GetRideIndex() != rideIndex + if (entranceElement == nullptr || entranceElement->GetRideIndex() != ride.id || entranceElement->GetStationIndex() != stationIndex) { fixEntrance = true; } else { - ride->stations[stationIndex].Entrance.direction = (uint8_t)entranceElement->GetDirection(); + ride.stations[stationIndex].Entrance.direction = (uint8_t)entranceElement->GetDirection(); } } @@ -7760,14 +7741,14 @@ void determine_ride_entrance_and_exit_locations() const EntranceElement* entranceElement = map_get_ride_exit_element_at( exitLoc.x * 32, exitLoc.y * 32, entranceLoc.z, false); - if (entranceElement == nullptr || entranceElement->GetRideIndex() != rideIndex + if (entranceElement == nullptr || entranceElement->GetRideIndex() != ride.id || entranceElement->GetStationIndex() != stationIndex) { fixExit = true; } else { - ride->stations[stationIndex].Exit.direction = (uint8_t)entranceElement->GetDirection(); + ride.stations[stationIndex].Exit.direction = (uint8_t)entranceElement->GetDirection(); } } @@ -7795,7 +7776,7 @@ void determine_ride_entrance_and_exit_locations() continue; } const EntranceElement* entranceElement = tileElement->AsEntrance(); - if (entranceElement->GetRideIndex() != rideIndex) + if (entranceElement->GetRideIndex() != ride.id) { continue; } @@ -7805,15 +7786,15 @@ void determine_ride_entrance_and_exit_locations() } // The expected height is where entrances and exit reside in non-hacked parks. - const uint8_t expectedHeight = ride->stations[stationIndex].Height; + const uint8_t expectedHeight = ride.stations[stationIndex].Height; if (fixEntrance && entranceElement->GetEntranceType() == ENTRANCE_TYPE_RIDE_ENTRANCE) { if (alreadyFoundEntrance) { - if (ride->stations[stationIndex].Entrance.z == expectedHeight) + if (ride.stations[stationIndex].Entrance.z == expectedHeight) continue; - if (ride->stations[stationIndex].Entrance.z > entranceElement->base_height) + if (ride.stations[stationIndex].Entrance.z > entranceElement->base_height) continue; } @@ -7824,31 +7805,31 @@ void determine_ride_entrance_and_exit_locations() entranceElement->base_height, (uint8_t)entranceElement->GetDirection(), }; - ride_set_entrance_location(ride, stationIndex, newEntranceLoc); + ride_set_entrance_location(&ride, stationIndex, newEntranceLoc); alreadyFoundEntrance = true; log_verbose( - "Fixed disconnected entrance of ride %d, station %d to x = %d, y = %d and z = %d.", - rideIndex, stationIndex, x, y, entranceElement->base_height); + "Fixed disconnected entrance of ride %d, station %d to x = %d, y = %d and z = %d.", ride.id, + stationIndex, x, y, entranceElement->base_height); } else if (fixExit && entranceElement->GetEntranceType() == ENTRANCE_TYPE_RIDE_EXIT) { if (alreadyFoundExit) { - if (ride->stations[stationIndex].Exit.z == expectedHeight) + if (ride.stations[stationIndex].Exit.z == expectedHeight) continue; - if (ride->stations[stationIndex].Exit.z > entranceElement->base_height) + if (ride.stations[stationIndex].Exit.z > entranceElement->base_height) continue; } // Found our exit ride_set_exit_location( - ride, stationIndex, + &ride, stationIndex, { x, y, entranceElement->base_height, (uint8_t)entranceElement->GetDirection() }); alreadyFoundExit = true; log_verbose( - "Fixed disconnected exit of ride %d, station %d to x = %d, y = %d and z = %d.", rideIndex, + "Fixed disconnected exit of ride %d, station %d to x = %d, y = %d and z = %d.", ride.id, stationIndex, x, y, entranceElement->base_height); } } while (!(tileElement++)->IsLastForTile()); @@ -7858,13 +7839,13 @@ void determine_ride_entrance_and_exit_locations() if (fixEntrance && !alreadyFoundEntrance) { - ride_clear_entrance_location(ride, stationIndex); - log_verbose("Cleared disconnected entrance of ride %d, station %d.", rideIndex, stationIndex); + ride_clear_entrance_location(&ride, stationIndex); + log_verbose("Cleared disconnected entrance of ride %d, station %d.", ride.id, stationIndex); } if (fixExit && !alreadyFoundExit) { - ride_clear_exit_location(ride, stationIndex); - log_verbose("Cleared disconnected exit of ride %d, station %d.", rideIndex, stationIndex); + ride_clear_exit_location(&ride, stationIndex); + log_verbose("Cleared disconnected exit of ride %d, station %d.", ride.id, stationIndex); } } } diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index 4754f051c6..7afd19126f 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -40,6 +40,7 @@ struct Staff; #define MAX_STATIONS 4 #define MAX_RIDES 255 #define RIDE_ID_NULL 255 +#define RIDE_TYPE_NULL 255 #define RIDE_ADJACENCY_CHECK_DISTANCE 5 constexpr uint16_t const MAX_INVERSIONS = RCT12_MAX_INVERSIONS; @@ -201,8 +202,8 @@ struct RideMeasurement */ struct Ride { - ride_id_t id; - uint8_t type; + ride_id_t id = RIDE_ID_NULL; + uint8_t type = RIDE_TYPE_NULL; // pointer to static info. for example, wild mouse type is 0x36, subtype is // 0x4c. uint8_t subtype; @@ -544,7 +545,6 @@ enum enum { - RIDE_TYPE_NULL = 255, RIDE_TYPE_SPIRAL_ROLLER_COASTER = 0, RIDE_TYPE_STAND_UP_ROLLER_COASTER, RIDE_TYPE_SUSPENDED_SWINGING_COASTER, @@ -990,24 +990,92 @@ struct rct_ride_properties extern const rct_ride_properties RideProperties[RIDE_TYPE_COUNT]; -/** Helper macros until rides are stored in this module. */ -Ride* get_ride(int32_t index); +Ride* get_ride(ride_id_t index); + +struct RideManager +{ + const Ride* operator[](ride_id_t id) const + { + return get_ride(id); + } + + Ride* operator[](ride_id_t id) + { + return get_ride(id); + } + + class Iterator + { + friend RideManager; + + private: + RideManager& _rideManager; + size_t _index{}; + size_t _endIndex{}; + + public: + using difference_type = ride_id_t; + using value_type = Ride; + using pointer = const Ride*; + using reference = const Ride&; + using iterator_category = std::forward_iterator_tag; + + private: + Iterator(RideManager& rideManager, size_t beginIndex, size_t endIndex) + : _rideManager(rideManager) + , _index(beginIndex) + , _endIndex(endIndex) + { + if (_index < _endIndex && _rideManager[(ride_id_t)_index] == nullptr) + { + ++(*this); + } + } + + public: + Iterator& operator++() + { + do + { + _index++; + } while (_index < _endIndex && _rideManager[(ride_id_t)_index] == nullptr); + return *this; + } + Iterator operator++(int) + { + auto result = *this; + ++(*this); + return result; + } + bool operator==(Iterator other) const + { + return _index == other._index; + } + bool operator!=(Iterator other) const + { + return !(*this == other); + } + Ride& operator*() + { + return *_rideManager[(ride_id_t)_index]; + } + }; + + Iterator begin(); + Iterator end(); +}; + +RideManager GetRideManager(); +ride_id_t GetNextFreeRideId(); +Ride* GetOrAllocateRide(ride_id_t index); rct_ride_entry* get_ride_entry(int32_t index); std::string_view get_ride_entry_name(size_t index); RideMeasurement* get_ride_measurement(int32_t index); -/** - * Helper macro loop for enumerating through all the non null rides. - */ -#define FOR_ALL_RIDES(i, ride) \ - for (i = 0; i < MAX_RIDES; i++) \ - if ((ride = get_ride(i))->type != RIDE_TYPE_NULL) - extern money16 gTotalRideValueForMoney; extern const uint8_t gRideClassifications[MAX_RIDES]; -extern Ride gRideList[MAX_RIDES]; extern const rct_string_id ColourSchemeNames[4]; extern uint16_t gRideCount; @@ -1130,7 +1198,6 @@ uint8_t ride_get_helix_sections(Ride* ride); bool ride_type_has_flag(int32_t rideType, uint32_t flag); bool ride_has_any_track_elements(const Ride* ride); -void ride_all_has_any_track_elements(bool* rideIndexArray); void ride_construction_set_default_next_piece(); @@ -1209,4 +1276,8 @@ LocationXY16 ride_get_rotated_coords(int16_t x, int16_t y, int16_t z); void determine_ride_entrance_and_exit_locations(); void ride_clear_leftover_entrances(Ride* ride); +#define FOR_ALL_RIDES(i, ride) \ + for (auto& __ride : GetRideManager()) \ + if ((ride = &__ride) != nullptr && (i = __ride.id) != RIDE_ID_NULL) + #endif diff --git a/src/openrct2/ride/RideRatings.cpp b/src/openrct2/ride/RideRatings.cpp index 7286d9e18d..926978f2c3 100644 --- a/src/openrct2/ride/RideRatings.cpp +++ b/src/openrct2/ride/RideRatings.cpp @@ -97,12 +97,11 @@ static void ride_ratings_add(rating_tuple* rating, int32_t excitement, int32_t i * processed will be overwritten. * Only purpose of this function currently is for testing. */ -void ride_ratings_update_ride(ride_id_t rideIndex) +void ride_ratings_update_ride(const Ride& ride) { - Ride* ride = get_ride(rideIndex); - if (ride->type != RIDE_TYPE_NULL && ride->status != RIDE_STATUS_CLOSED) + if (ride.status != RIDE_STATUS_CLOSED) { - gRideRatingsCalcData.current_ride = rideIndex; + gRideRatingsCalcData.current_ride = ride.id; gRideRatingsCalcData.state = RIDE_RATINGS_STATE_INITIALISE; while (gRideRatingsCalcData.state != RIDE_RATINGS_STATE_FIND_NEXT_RIDE) { @@ -162,8 +161,8 @@ static void ride_ratings_update_state_0() currentRide = 0; } - Ride* ride = get_ride(currentRide); - if (ride->type != RIDE_TYPE_NULL && ride->status != RIDE_STATUS_CLOSED) + auto ride = get_ride(currentRide); + if (ride != nullptr && ride->status != RIDE_STATUS_CLOSED) { gRideRatingsCalcData.state = RIDE_RATINGS_STATE_INITIALISE; } diff --git a/src/openrct2/ride/RideRatings.h b/src/openrct2/ride/RideRatings.h index 3ec272f7e3..8cd3f2b5fd 100644 --- a/src/openrct2/ride/RideRatings.h +++ b/src/openrct2/ride/RideRatings.h @@ -58,5 +58,5 @@ struct rct_ride_rating_calc_data extern rct_ride_rating_calc_data gRideRatingsCalcData; -void ride_ratings_update_ride(ride_id_t rideIndex); +void ride_ratings_update_ride(const Ride& ride); void ride_ratings_update_all(); diff --git a/src/openrct2/ride/RideTypes.h b/src/openrct2/ride/RideTypes.h index 9a5b081ec4..473a6a8008 100644 --- a/src/openrct2/ride/RideTypes.h +++ b/src/openrct2/ride/RideTypes.h @@ -12,3 +12,4 @@ #include typedef uint8_t ride_id_t; +struct Ride; diff --git a/src/openrct2/scenario/Scenario.cpp b/src/openrct2/scenario/Scenario.cpp index b5b6b8152e..cfb89f6fcc 100644 --- a/src/openrct2/scenario/Scenario.cpp +++ b/src/openrct2/scenario/Scenario.cpp @@ -646,9 +646,27 @@ void scenario_fix_ghosts(rct_s6_data* s6) } } +static void ride_all_has_any_track_elements(bool* rideIndexArray) +{ + tile_element_iterator it; + + std::fill_n(rideIndexArray, MAX_RIDES, false); + + tile_element_iterator_begin(&it); + while (tile_element_iterator_next(&it)) + { + if (it.element->GetType() != TILE_ELEMENT_TYPE_TRACK) + continue; + if (it.element->IsGhost()) + continue; + + rideIndexArray[it.element->AsTrack()->GetRideIndex()] = true; + } +} + void scenario_remove_trackless_rides(rct_s6_data* s6) { - bool rideHasTrack[MAX_RIDES]; + bool rideHasTrack[RCT12_MAX_RIDES_IN_PARK]; ride_all_has_any_track_elements(rideHasTrack); for (int32_t i = 0; i < RCT12_MAX_RIDES_IN_PARK; i++) { diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index a8bf2651d8..74c55e9dd9 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -493,13 +493,11 @@ int32_t Park::CalculateParkRating() const money32 Park::CalculateParkValue() const { - money32 result = 0; - // Sum ride values - for (int32_t i = 0; i < MAX_RIDES; i++) + money32 result = 0; + for (const auto& ride : GetRideManager()) { - auto ride = get_ride(i); - result += CalculateRideValue(ride); + result += CalculateRideValue(&ride); } // +7.00 per guest diff --git a/test/tests/RideRatings.cpp b/test/tests/RideRatings.cpp index a556c06090..edb42e7783 100644 --- a/test/tests/RideRatings.cpp +++ b/test/tests/RideRatings.cpp @@ -28,34 +28,26 @@ class RideRatings : public testing::Test protected: void CalculateRatingsForAllRides() { - for (int rideId = 0; rideId < MAX_RIDES; rideId++) + for (const auto& ride : GetRideManager()) { - Ride* ride = get_ride(rideId); - if (ride->type != RIDE_TYPE_NULL) - { - ride_ratings_update_ride(rideId); - } + ride_ratings_update_ride(ride); } } void DumpRatings() { - for (int rideId = 0; rideId < MAX_RIDES; rideId++) + for (const auto& ride : GetRideManager()) { - Ride* ride = get_ride(rideId); - if (ride->type != RIDE_TYPE_NULL) - { - std::string line = FormatRatings(ride); - printf("%s\n", line.c_str()); - } + std::string line = FormatRatings(ride); + printf("%s\n", line.c_str()); } } - std::string FormatRatings(Ride* ride) + std::string FormatRatings(const Ride& ride) { - rating_tuple ratings = ride->ratings; + rating_tuple ratings = ride.ratings; std::string line = String::StdFormat( - "%s: (%d, %d, %d)", ride_type_get_enum_name(ride->type), (int)ratings.excitement, (int)ratings.intensity, + "%s: (%d, %d, %d)", ride_type_get_enum_name(ride.type), (int)ratings.excitement, (int)ratings.intensity, (int)ratings.nausea); return line; } @@ -77,6 +69,7 @@ TEST_F(RideRatings, all) // Check ride count to check load was successful ASSERT_EQ(gRideCount, 134); + ASSERT_EQ(ride_get_count(), 134); CalculateRatingsForAllRides(); @@ -86,16 +79,12 @@ TEST_F(RideRatings, all) // Check ride ratings int expI = 0; - for (int rideId = 0; rideId < MAX_RIDES; rideId++) + for (const auto& ride : GetRideManager()) { - Ride* ride = get_ride(rideId); - if (ride->type != RIDE_TYPE_NULL) - { - std::string actual = FormatRatings(ride); - std::string expected = expectedRatings[expI]; - ASSERT_STREQ(actual.c_str(), expected.c_str()); + auto actual = FormatRatings(ride); + auto expected = expectedRatings[expI]; + ASSERT_STREQ(actual.c_str(), expected.c_str()); - expI++; - } + expI++; } }