diff --git a/src/openrct2-ui/interface/ViewportInteraction.cpp b/src/openrct2-ui/interface/ViewportInteraction.cpp index 1536964c51..20e82e2dc9 100644 --- a/src/openrct2-ui/interface/ViewportInteraction.cpp +++ b/src/openrct2-ui/interface/ViewportInteraction.cpp @@ -620,11 +620,8 @@ static void viewport_interaction_remove_large_scenery(TileElement* tileElement, static Peep* viewport_interaction_get_closest_peep(ScreenCoordsXY screenCoords, int32_t maxDistance) { - int32_t distance, closestDistance; - uint16_t spriteIndex; rct_window* w; rct_viewport* viewport; - Peep *peep, *closestPeep; w = window_find_from_point(screenCoords); if (w == nullptr) @@ -637,14 +634,14 @@ static Peep* viewport_interaction_get_closest_peep(ScreenCoordsXY screenCoords, screenCoords.x = ((screenCoords.x - viewport->pos.x) * viewport->zoom) + viewport->viewPos.x; screenCoords.y = ((screenCoords.y - viewport->pos.y) * viewport->zoom) + viewport->viewPos.y; - closestPeep = nullptr; - closestDistance = 0xFFFF; - FOR_ALL_PEEPS (spriteIndex, peep) + Peep* closestPeep = nullptr; + auto closestDistance = std::numeric_limits::max(); + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->sprite_left == LOCATION_NULL) continue; - distance = abs(((peep->sprite_left + peep->sprite_right) / 2) - screenCoords.x) + auto distance = abs(((peep->sprite_left + peep->sprite_right) / 2) - screenCoords.x) + abs(((peep->sprite_top + peep->sprite_bottom) / 2) - screenCoords.y); if (distance > maxDistance) continue; diff --git a/src/openrct2-ui/windows/GuestList.cpp b/src/openrct2-ui/windows/GuestList.cpp index dc15393f82..9908a52b66 100644 --- a/src/openrct2-ui/windows/GuestList.cpp +++ b/src/openrct2-ui/windows/GuestList.cpp @@ -248,9 +248,7 @@ void window_guest_list_refresh_list() } GuestList.clear(); - Peep* peep = nullptr; - uint16_t spriteIndex; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { sprite_set_flashing(peep, false); if (peep->OutsideOfPark != 0) @@ -263,7 +261,7 @@ void window_guest_list_refresh_list() } if (!guest_should_be_visible(peep)) continue; - GuestList.push_back(spriteIndex); + GuestList.push_back(peep->sprite_index); } std::sort(GuestList.begin(), GuestList.end(), [](const uint16_t a, const uint16_t b) { return peep_compare(a, b) < 0; }); @@ -930,9 +928,6 @@ static FilterArguments get_arguments_from_peep(const Peep* peep) */ static void window_guest_list_find_groups() { - int32_t spriteIndex, spriteIndex2, groupIndex, faceIndex; - Peep *peep, *peep2; - uint32_t tick256 = floor2(gScenarioTicks, 256); if (_window_guest_list_selected_view == _window_guest_list_last_find_groups_selected_view) { @@ -948,18 +943,23 @@ static void window_guest_list_find_groups() _window_guest_list_num_groups = 0; // Set all guests to unassigned - FOR_ALL_GUESTS (spriteIndex, peep) - if (peep->OutsideOfPark == 0) - peep->flags |= SPRITE_FLAGS_PEEP_VISIBLE; - + { + for (auto peep : EntityList(SPRITE_LIST_PEEP)) + { + if (peep->OutsideOfPark == 0) + { + peep->flags |= SPRITE_FLAGS_PEEP_VISIBLE; + } + } + } // For each guest / group - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0 || !(peep->flags & SPRITE_FLAGS_PEEP_VISIBLE)) continue; // New group, cap at 240 though - groupIndex = _window_guest_list_num_groups; + int32_t groupIndex = _window_guest_list_num_groups; if (groupIndex >= 240) break; @@ -971,12 +971,12 @@ static void window_guest_list_find_groups() _window_guest_list_filter_arguments = _window_guest_list_groups_arguments[groupIndex]; _window_guest_list_group_index[groupIndex] = groupIndex; - faceIndex = groupIndex * 56; + auto faceIndex = groupIndex * 56; _window_guest_list_groups_guest_faces[faceIndex++] = get_peep_face_sprite_small(peep) - SPR_PEEP_SMALL_FACE_VERY_VERY_UNHAPPY; // Find more peeps that belong to same group - FOR_ALL_GUESTS (spriteIndex2, peep2) + for (auto peep2 : EntityList(SPRITE_LIST_PEEP)) { if (peep2->OutsideOfPark != 0 || !(peep2->flags & SPRITE_FLAGS_PEEP_VISIBLE)) continue; diff --git a/src/openrct2-ui/windows/Map.cpp b/src/openrct2-ui/windows/Map.cpp index f32b5fab79..c7defb81c1 100644 --- a/src/openrct2-ui/windows/Map.cpp +++ b/src/openrct2-ui/windows/Map.cpp @@ -1056,10 +1056,7 @@ static MapCoordsXY window_map_transform_to_map_coords(CoordsXY c) */ static void window_map_paint_peep_overlay(rct_drawpixelinfo* dpi) { - Peep* peep; - uint16_t spriteIndex; - - FOR_ALL_PEEPS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->x == LOCATION_NULL) continue; @@ -1106,11 +1103,11 @@ static void window_map_paint_peep_overlay(rct_drawpixelinfo* dpi) */ static void window_map_paint_train_overlay(rct_drawpixelinfo* dpi) { - auto list = EntityList(SPRITE_LIST_TRAIN_HEAD); - for (auto train : list) + for (auto train : EntityList(SPRITE_LIST_TRAIN_HEAD)) { Vehicle* vehicle = nullptr; - for (auto vehicle_index = train->sprite_index; vehicle_index != SPRITE_INDEX_NULL; vehicle_index = vehicle->next_vehicle_on_train) + for (auto vehicle_index = train->sprite_index; vehicle_index != SPRITE_INDEX_NULL; + vehicle_index = vehicle->next_vehicle_on_train) { vehicle = GET_VEHICLE(vehicle_index); if (vehicle->x == LOCATION_NULL) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index 9a49f337fb..7586ecb200 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -4343,9 +4343,7 @@ static void window_ride_maintenance_paint(rct_window* w, rct_drawpixelinfo* dpi) { stringId = STR_NO_MECHANICS_ARE_HIRED_MESSAGE; - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->StaffType == STAFF_TYPE_MECHANIC) { diff --git a/src/openrct2-ui/windows/StaffList.cpp b/src/openrct2-ui/windows/StaffList.cpp index 8c3da9fc06..b4e811ef2a 100644 --- a/src/openrct2-ui/windows/StaffList.cpp +++ b/src/openrct2-ui/windows/StaffList.cpp @@ -188,16 +188,15 @@ void WindowStaffListRefresh() return; } StaffList.clear(); - Peep* peep = nullptr; - uint16_t spriteIndex; - FOR_ALL_STAFF (spriteIndex, peep) + + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { sprite_set_flashing(peep, false); if (peep->StaffType != _windowStaffListSelectedTab) continue; sprite_set_flashing(peep, true); - StaffList.push_back(spriteIndex); + StaffList.push_back(peep->sprite_index); } std::sort(StaffList.begin(), StaffList.end(), [](const uint16_t a, const uint16_t b) { return peep_compare(a, b) < 0; }); @@ -338,10 +337,8 @@ void window_staff_list_update(rct_window* w) // Enable highlighting of these staff members in map window if (window_find_by_class(WC_MAP) != nullptr) { - int32_t spriteIndex; - Peep* peep; gWindowMapFlashingFlags |= (1 << 2); - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { sprite_set_flashing(peep, false); @@ -376,10 +373,10 @@ static void window_staff_list_tooldown(rct_window* w, rct_widgetindex widgetInde bool isPatrolAreaSet = staff_is_patrol_area_set_for_type(static_cast(selectedPeepType), footpathCoords); - uint16_t spriteIndex; - Peep *peep, *closestPeep = nullptr; + Peep* closestPeep = nullptr; int32_t closestPeepDistance = std::numeric_limits::max(); - FOR_ALL_STAFF (spriteIndex, peep) + + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->StaffType != selectedPeepType) continue; diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 68c8b5e558..4ecbe32408 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -432,22 +432,24 @@ void game_convert_strings_to_rct2(rct_s6_data* s6) void game_fix_save_vars() { // Recalculates peep count after loading a save to fix corrupted files - Peep* peep; - uint16_t spriteIndex; - uint32_t peepCount = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + uint32_t guestCount = 0; { - if (!peep->OutsideOfPark) - peepCount++; + for (auto guest : EntityList(SPRITE_LIST_PEEP)) + { + if (!guest->OutsideOfPark) + { + guestCount++; + } + } } - gNumGuestsInPark = peepCount; + gNumGuestsInPark = guestCount; // Peeps to remove have to be cached here, as removing them from within the loop breaks iteration std::vector peepsToRemove; // Fix possibly invalid field values - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->CurrentRideStation >= MAX_STATIONS) { @@ -460,7 +462,7 @@ void game_fix_save_vars() Ride* ride = get_ride(rideIdx); if (ride == nullptr) { - log_warning("Couldn't find ride %u, resetting ride on peep %u", rideIdx, spriteIndex); + log_warning("Couldn't find ride %u, resetting ride on peep %u", rideIdx, peep->sprite_index); peep->CurrentRide = RIDE_ID_NULL; continue; } @@ -468,11 +470,12 @@ void game_fix_save_vars() ft.Add(peep->Id); auto curName = peep->GetName(); log_warning( - "Peep %u (%s) has invalid ride station = %u for ride %u.", spriteIndex, curName.c_str(), srcStation, rideIdx); + "Peep %u (%s) has invalid ride station = %u for ride %u.", peep->sprite_index, curName.c_str(), srcStation, + rideIdx); auto station = ride_get_first_valid_station_exit(ride); if (station == STATION_INDEX_NULL) { - log_warning("Couldn't find station, removing peep %u", spriteIndex); + log_warning("Couldn't find station, removing peep %u", peep->sprite_index); peepsToRemove.push_back(peep); } else diff --git a/src/openrct2/actions/RideDemolishAction.hpp b/src/openrct2/actions/RideDemolishAction.hpp index 156d0508be..7430994478 100644 --- a/src/openrct2/actions/RideDemolishAction.hpp +++ b/src/openrct2/actions/RideDemolishAction.hpp @@ -153,9 +153,7 @@ private: } } - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { uint8_t ride_id_bit = _rideIndex % 8; uint8_t ride_id_offset = _rideIndex / 8; diff --git a/src/openrct2/actions/SetCheatAction.hpp b/src/openrct2/actions/SetCheatAction.hpp index da2a3f5090..56ccb113c0 100644 --- a/src/openrct2/actions/SetCheatAction.hpp +++ b/src/openrct2/actions/SetCheatAction.hpp @@ -427,8 +427,7 @@ private: void RemoveLitter() const { - auto list = EntityList(SPRITE_LIST_LITTER); - for (auto litter : list) + for (auto litter : EntityList(SPRITE_LIST_LITTER)) { sprite_remove(litter); } @@ -571,12 +570,8 @@ private: void SetGuestParameter(int32_t parameter, int32_t value) const { - int32_t spriteIndex; - Peep* p; - FOR_ALL_GUESTS (spriteIndex, p) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { - auto peep = p->AsGuest(); - assert(peep != nullptr); switch (parameter) { case GUEST_PARAMETER_HAPPINESS: @@ -619,12 +614,8 @@ private: void GiveObjectToGuests(int32_t object) const { - int32_t spriteIndex; - Peep* p; - FOR_ALL_GUESTS (spriteIndex, p) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { - auto peep = p->AsGuest(); - assert(peep != nullptr); switch (object) { case OBJECT_MONEY: @@ -695,8 +686,7 @@ private: // Do not use the FOR_ALL_PEEPS macro for this as next sprite index // will be fetched on a deleted peep. - auto list = EntityList(SPRITE_LIST_PEEP); - for (auto peep : list) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->AssignedPeepType == PEEP_TYPE_GUEST) { @@ -710,10 +700,7 @@ private: void ExplodeGuests() const { - int32_t sprite_index; - Peep* peep; - - FOR_ALL_GUESTS (sprite_index, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (scenario_rand_max(6) == 0) { @@ -724,10 +711,7 @@ private: void SetStaffSpeed(uint8_t value) const { - uint16_t spriteIndex; - Peep* peep; - - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { peep->Energy = value; peep->EnergyTarget = value; diff --git a/src/openrct2/actions/StaffHireNewAction.hpp b/src/openrct2/actions/StaffHireNewAction.hpp index 74274f8e4d..4082fddc63 100644 --- a/src/openrct2/actions/StaffHireNewAction.hpp +++ b/src/openrct2/actions/StaffHireNewAction.hpp @@ -176,22 +176,18 @@ private: newPeep->FavouriteRide = RIDE_ID_NULL; newPeep->StaffOrders = _staffOrders; - uint16_t idSearchSpriteIndex; - Peep* idSearchPeep; - // We search for the first available Id for a given staff type uint32_t newStaffId = 0; for (;;) { bool found = false; ++newStaffId; - - FOR_ALL_STAFF (idSearchSpriteIndex, idSearchPeep) + for (auto searchPeep : EntityList(SPRITE_LIST_PEEP)) { - if (idSearchPeep->StaffType != _staffType) + if (searchPeep->StaffType != _staffType) continue; - if (idSearchPeep->Id == newStaffId) + if (searchPeep->Id == newStaffId) { found = true; break; @@ -264,32 +260,33 @@ private: void AutoPositionNewStaff(Peep * newPeep) const { // Find a location to place new staff member - newPeep->State = PEEP_STATE_FALLING; - int16_t x, y, z; uint32_t count = 0; - uint16_t sprite_index; - Peep* guest = nullptr; PathElement* guest_tile = nullptr; // Count number of walking guests - FOR_ALL_GUESTS (sprite_index, guest) { - if (guest->State == PEEP_STATE_WALKING) + for (auto guest : EntityList(SPRITE_LIST_PEEP)) { - // Check the walking guest's tile. Only count them if they're on a path tile. - guest_tile = map_get_path_element_at(TileCoordsXYZ{ guest->NextLoc }); - if (guest_tile != nullptr) - ++count; + if (guest->State == PEEP_STATE_WALKING) + { + // Check the walking guest's tile. Only count them if they're on a path tile. + guest_tile = map_get_path_element_at(TileCoordsXYZ{ guest->NextLoc }); + if (guest_tile != nullptr) + ++count; + } } } + CoordsXYZ newLocation{}; if (count > 0) { // Place staff at a random guest uint32_t rand = scenario_rand_max(count); - FOR_ALL_GUESTS (sprite_index, guest) + Guest* chosenGuest = nullptr; + + for (auto guest : EntityList(SPRITE_LIST_PEEP)) { if (guest->State == PEEP_STATE_WALKING) { @@ -297,15 +294,29 @@ private: if (guest_tile != nullptr) { if (rand == 0) + { + chosenGuest = guest; break; + } --rand; } } } - x = guest->x; - y = guest->y; - z = guest->z; + if (chosenGuest != nullptr) + { + newLocation.x = chosenGuest->x; + newLocation.y = chosenGuest->y; + newLocation.z = chosenGuest->z; + } + else + { + // User must pick a location + newPeep->State = PEEP_STATE_PICKED; + newLocation.x = newPeep->x; + newLocation.y = newPeep->y; + newLocation.z = newPeep->z; + } } else { @@ -315,22 +326,21 @@ private: auto rand = scenario_rand_max(static_cast(gParkEntrances.size())); const auto& entrance = gParkEntrances[rand]; auto dir = entrance.direction; - x = entrance.x; - y = entrance.y; - z = entrance.z; - x += 16 + ((dir & 1) == 0 ? ((dir & 2) ? 32 : -32) : 0); - y += 16 + ((dir & 1) == 1 ? ((dir & 2) ? -32 : 32) : 0); + newLocation = entrance; + // TODO: Replace with CoordsDirectionDelta + newLocation.x += 16 + ((dir & 1) == 0 ? ((dir & 2) ? 32 : -32) : 0); + newLocation.y += 16 + ((dir & 1) == 1 ? ((dir & 2) ? -32 : 32) : 0); } else { // User must pick a location newPeep->State = PEEP_STATE_PICKED; - x = newPeep->x; - y = newPeep->y; - z = newPeep->z; + newLocation.x = newPeep->x; + newLocation.y = newPeep->y; + newLocation.z = newPeep->z; } } - newPeep->MoveTo({ x, y, z + 16 }); + newPeep->MoveTo(newLocation + CoordsXYZ{ 0, 0, 16 }); } }; diff --git a/src/openrct2/actions/StaffSetColourAction.hpp b/src/openrct2/actions/StaffSetColourAction.hpp index 185321cd23..3115bef2e9 100644 --- a/src/openrct2/actions/StaffSetColourAction.hpp +++ b/src/openrct2/actions/StaffSetColourAction.hpp @@ -65,9 +65,7 @@ public: } // Update each staff member's uniform - int32_t spriteIndex; - Peep* peep; - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->StaffType == _staffType) { diff --git a/src/openrct2/interface/InteractiveConsole.cpp b/src/openrct2/interface/InteractiveConsole.cpp index 953d985e5a..c2961ca56f 100644 --- a/src/openrct2/interface/InteractiveConsole.cpp +++ b/src/openrct2/interface/InteractiveConsole.cpp @@ -437,13 +437,12 @@ static int32_t cc_staff(InteractiveConsole& console, const arguments_t& argv) { if (argv[0] == "list") { - Peep* peep; - int32_t i; - FOR_ALL_STAFF (i, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { auto name = peep->GetName(); console.WriteFormatLine( - "staff id %03d type: %02u energy %03u name %s", i, peep->StaffType, peep->Energy, name.c_str()); + "staff id %03d type: %02u energy %03u name %s", peep->sprite_index, peep->StaffType, peep->Energy, + name.c_str()); } } else if (argv[0] == "set") diff --git a/src/openrct2/management/Award.cpp b/src/openrct2/management/Award.cpp index 18b0a0bed4..c1090416d1 100644 --- a/src/openrct2/management/Award.cpp +++ b/src/openrct2/management/Award.cpp @@ -76,9 +76,6 @@ bool award_is_positive(int32_t type) /** More than 1/16 of the total guests must be thinking untidy thoughts. */ static bool award_is_deserved_most_untidy(int32_t activeAwardTypes) { - uint16_t spriteIndex; - Peep* peep; - if (activeAwardTypes & (1 << PARK_AWARD_MOST_BEAUTIFUL)) return false; if (activeAwardTypes & (1 << PARK_AWARD_BEST_STAFF)) @@ -87,7 +84,7 @@ static bool award_is_deserved_most_untidy(int32_t activeAwardTypes) return false; uint32_t negativeCount = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -109,9 +106,6 @@ static bool award_is_deserved_most_untidy(int32_t activeAwardTypes) /** More than 1/64 of the total guests must be thinking tidy thoughts and less than 6 guests thinking untidy thoughts. */ static bool award_is_deserved_most_tidy(int32_t activeAwardTypes) { - uint16_t spriteIndex; - Peep* peep; - if (activeAwardTypes & (1 << PARK_AWARD_MOST_UNTIDY)) return false; if (activeAwardTypes & (1 << PARK_AWARD_MOST_DISAPPOINTING)) @@ -119,7 +113,7 @@ static bool award_is_deserved_most_tidy(int32_t activeAwardTypes) uint32_t positiveCount = 0; uint32_t negativeCount = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -192,9 +186,6 @@ static bool award_is_deserved_best_value(int32_t activeAwardTypes) /** More than 1/128 of the total guests must be thinking scenic thoughts and fewer than 16 untidy thoughts. */ static bool award_is_deserved_most_beautiful(int32_t activeAwardTypes) { - uint16_t spriteIndex; - Peep* peep; - if (activeAwardTypes & (1 << PARK_AWARD_MOST_UNTIDY)) return false; if (activeAwardTypes & (1 << PARK_AWARD_MOST_DISAPPOINTING)) @@ -202,7 +193,8 @@ static bool award_is_deserved_most_beautiful(int32_t activeAwardTypes) uint32_t positiveCount = 0; uint32_t negativeCount = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + auto list = EntityList(SPRITE_LIST_PEEP); + for (auto peep : list) { if (peep->OutsideOfPark != 0) continue; @@ -243,11 +235,8 @@ static bool award_is_deserved_worst_value(int32_t activeAwardTypes) /** No more than 2 people who think the vandalism is bad and no crashes. */ static bool award_is_deserved_safest([[maybe_unused]] int32_t activeAwardTypes) { - uint16_t spriteIndex; - Peep* peep; - auto peepsWhoDislikeVandalism = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -273,18 +262,13 @@ static bool award_is_deserved_safest([[maybe_unused]] int32_t activeAwardTypes) /** All staff types, at least 20 staff, one staff per 32 peeps. */ static bool award_is_deserved_best_staff(int32_t activeAwardTypes) { - uint16_t spriteIndex; - Peep* peep; - int32_t peepCount, staffCount; - int32_t staffTypeFlags; - if (activeAwardTypes & (1 << PARK_AWARD_MOST_UNTIDY)) return false; - peepCount = 0; - staffCount = 0; - staffTypeFlags = 0; - FOR_ALL_PEEPS (spriteIndex, peep) + auto peepCount = 0; + auto staffCount = 0; + auto staffTypeFlags = 0; + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->AssignedPeepType == PEEP_TYPE_STAFF) { @@ -333,9 +317,7 @@ static bool award_is_deserved_best_food(int32_t activeAwardTypes) // Count hungry peeps auto hungryPeeps = 0; - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -379,9 +361,7 @@ static bool award_is_deserved_worst_food(int32_t activeAwardTypes) // Count hungry peeps auto hungryPeeps = 0; - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -411,9 +391,7 @@ static bool award_is_deserved_best_restrooms([[maybe_unused]] int32_t activeAwar // Count number of guests who are thinking they need the restroom auto guestsWhoNeedRestroom = 0; - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; @@ -538,13 +516,9 @@ static bool award_is_deserved_most_dazzling_ride_colours(int32_t activeAwardType /** At least 10 peeps and more than 1/64 of total guests are lost or can't find something. */ static bool award_is_deserved_most_confusing_layout([[maybe_unused]] int32_t activeAwardTypes) { - uint32_t peepsCounted, peepsLost; - uint16_t spriteIndex; - Peep* peep; - - peepsCounted = 0; - peepsLost = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + uint32_t peepsCounted = 0; + uint32_t peepsLost = 0; + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0) continue; diff --git a/src/openrct2/management/Finance.cpp b/src/openrct2/management/Finance.cpp index 03e9759ac5..5f03b0bb42 100644 --- a/src/openrct2/management/Finance.cpp +++ b/src/openrct2/management/Finance.cpp @@ -106,15 +106,12 @@ void finance_payment(money32 amount, ExpenditureType type) */ void finance_pay_wages() { - Peep* peep; - uint16_t spriteIndex; - if (gParkFlags & PARK_FLAGS_NO_MONEY) { return; } - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { finance_payment(gStaffWageTable[peep->StaffType] / 4, ExpenditureType::Wages); } @@ -249,10 +246,7 @@ void finance_update_daily_profit() if (!(gParkFlags & PARK_FLAGS_NO_MONEY)) { // Staff costs - uint16_t sprite_index; - Peep* peep; - - FOR_ALL_STAFF (sprite_index, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { current_profit -= gStaffWageTable[peep->StaffType]; } diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index fb539bf474..bc3be0f50a 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -399,6 +399,12 @@ static void peep_head_for_nearest_ride_type(Guest* peep, int32_t rideType); static void peep_head_for_nearest_ride_with_flags(Guest* peep, int32_t rideTypeFlags); bool loc_690FD0(Peep* peep, uint8_t* rideToView, uint8_t* rideSeatToView, TileElement* tileElement); +template<> bool SpriteBase::Is() const +{ + auto peep = As(); + return peep && peep->AssignedPeepType == PEEP_TYPE_GUEST; +} + bool Guest::GuestHasValidXY() const { if (x != LOCATION_NULL) @@ -6245,10 +6251,7 @@ static void peep_update_walking_break_scenery(Peep* peep) return; } - Peep* inner_peep; - uint16_t sprite_index; - - FOR_ALL_STAFF (sprite_index, inner_peep) + for (auto inner_peep : EntityList(SPRITE_LIST_PEEP)) { if (inner_peep->StaffType != STAFF_TYPE_SECURITY) continue; diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index b2ebde2bec..3f0a0fbb93 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -386,12 +386,8 @@ Peep* try_get_guest(uint16_t spriteIndex) int32_t peep_get_staff_count() { - uint16_t spriteIndex; - Peep* peep; - int32_t count = 0; - - FOR_ALL_STAFF (spriteIndex, peep) - count++; + auto list = EntityList(SPRITE_LIST_PEEP); + auto count = std::distance(list.begin(), list.end()); return count; } @@ -406,10 +402,8 @@ void peep_update_all() return; int32_t i = 0; - // Do not use the FOR_ALL_PEEPS macro for this as next sprite index - // will be fetched on a delted peep if peep leaves the park. - auto list = EntityList(SPRITE_LIST_PEEP); - for (auto peep : list) + // Warning this loop can delete peeps + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (static_cast(i & 0x7F) != (gCurrentTicks & 0x7F)) { @@ -1175,14 +1169,12 @@ void Peep::Update() */ void peep_problem_warnings_update() { - Peep* peep; Ride* ride; - uint16_t spriteIndex; uint32_t hunger_counter = 0, lost_counter = 0, noexit_counter = 0, thirst_counter = 0, litter_counter = 0, disgust_counter = 0, toilet_counter = 0, vandalism_counter = 0; uint8_t* warning_throttle = gPeepWarningThrottle; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark != 0 || peep->Thoughts[0].freshness > 5) continue; @@ -1344,11 +1336,6 @@ void peep_stop_crowd_noise() */ void peep_update_crowd_noise() { - rct_viewport* viewport; - uint16_t spriteIndex; - Peep* peep; - int32_t visiblePeeps; - if (gGameSoundsOff) return; @@ -1358,14 +1345,14 @@ void peep_update_crowd_noise() if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) return; - viewport = g_music_tracking_viewport; + auto viewport = g_music_tracking_viewport; if (viewport == nullptr) return; // Count the number of peeps visible - visiblePeeps = 0; + auto visiblePeeps = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->sprite_left == LOCATION_NULL) continue; @@ -1428,12 +1415,8 @@ void peep_update_crowd_noise() */ void peep_applause() { - uint16_t spriteIndex; - Peep* p; - FOR_ALL_GUESTS (spriteIndex, p) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { - auto peep = p->AsGuest(); - assert(peep != nullptr); if (peep->OutsideOfPark != 0) continue; @@ -1460,10 +1443,7 @@ void peep_applause() */ void peep_update_days_in_queue() { - uint16_t sprite_index; - Peep* peep; - - FOR_ALL_GUESTS (sprite_index, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark == 0 && peep->State == PEEP_STATE_QUEUING) { diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index 14c251245e..a472867101 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -988,22 +988,6 @@ enum /** Helper macro until rides are stored in this module. */ #define GET_PEEP(sprite_index) &(get_sprite(sprite_index)->peep) -/** - * Helper macro loop for enumerating through all the peeps. To avoid needing a end loop counterpart, statements are - * applied in tautology if statements. TODO REMOVE - */ -#define FOR_ALL_PEEPS(sprite_index, peep) \ - for ((sprite_index) = gSpriteListHead[SPRITE_LIST_PEEP]; (sprite_index) != SPRITE_INDEX_NULL; (sprite_index) = peep->next) \ - if (((peep) = GetEntity(sprite_index)) != nullptr || 1) - -#define FOR_ALL_GUESTS(sprite_index, peep) \ - FOR_ALL_PEEPS (sprite_index, peep) \ - if ((peep)->AssignedPeepType == PEEP_TYPE_GUEST) - -#define FOR_ALL_STAFF(sprite_index, peep) \ - FOR_ALL_PEEPS (sprite_index, peep) \ - if ((peep)->AssignedPeepType == PEEP_TYPE_STAFF) - // rct2: 0x00982708 extern rct_peep_animation_entry g_peep_animation_entries[PEEP_SPRITE_TYPE_COUNT]; extern const bool gSpriteTypeToSlowWalkMap[48]; diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index ea77d0fc88..454a053957 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -79,6 +79,12 @@ colour_t gStaffHandymanColour; colour_t gStaffMechanicColour; colour_t gStaffSecurityColour; +template<> bool SpriteBase::Is() const +{ + auto peep = As(); + return peep && peep->AssignedPeepType == PEEP_TYPE_STAFF; +} + /** * * rct2: 0x006BD3A4 @@ -142,9 +148,6 @@ bool staff_hire_new_member(STAFF_TYPE staffType, ENTERTAINER_COSTUME entertainer */ void staff_update_greyed_patrol_areas() { - Peep* peep; - uint16_t sprite_index; - for (int32_t staff_type = 0; staff_type < STAFF_TYPE_COUNT; ++staff_type) { int32_t staffPatrolOffset = (staff_type + STAFF_MAX_COUNT) * STAFF_PATROL_AREA_SIZE; @@ -153,7 +156,7 @@ void staff_update_greyed_patrol_areas() gStaffPatrolAreas[staffPatrolOffset + i] = 0; } - FOR_ALL_STAFF (sprite_index, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->StaffType == staff_type) { @@ -361,10 +364,7 @@ static uint8_t staff_get_valid_patrol_directions(Staff* staff, const CoordsXY& l */ void staff_reset_stats() { - uint16_t spriteIndex; - Peep* peep; - - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { peep->TimeInPark = gDateMonthsElapsed; peep->StaffLawnsMown = 0; @@ -443,8 +443,7 @@ static uint8_t staff_handyman_direction_to_nearest_litter(Peep* peep) { uint16_t nearestLitterDist = 0xFFFF; Litter* nearestLitter = nullptr; - auto list = EntityList(SPRITE_LIST_LITTER); - for (auto litter : list) + for (auto litter : EntityList(SPRITE_LIST_LITTER)) { uint16_t distance = abs(litter->x - peep->x) + abs(litter->y - peep->y) + abs(litter->z - peep->z) * 4; @@ -1015,10 +1014,7 @@ bool Staff::DoMiscPathFinding() */ static void staff_entertainer_update_nearby_peeps(Peep* peep) { - uint16_t spriteIndex; - Peep* guest; - - FOR_ALL_GUESTS (spriteIndex, guest) + for (auto guest : EntityList(SPRITE_LIST_PEEP)) { if (guest->x == LOCATION_NULL) continue; diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index bcc8fb077f..45416519b3 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -1353,15 +1353,14 @@ private: FixRidePeepLinks(&ride, spriteIndexMap); } - int32_t i; - Peep* peep; - FOR_ALL_GUESTS (i, peep) { - FixPeepNextInQueue(peep, spriteIndexMap); + for (auto peep : EntityList(SPRITE_LIST_PEEP)) + { + FixPeepNextInQueue(peep, spriteIndexMap); + } } - // Fix the news items in advance - for (i = 0; i < MAX_NEWS_ITEMS; i++) + for (auto i = 0; i < MAX_NEWS_ITEMS; i++) { rct12_news_item* newsItem = &_s4.messages[i]; @@ -1377,7 +1376,7 @@ private: std::copy(std::begin(_s4.staff_modes), std::end(_s4.staff_modes), gStaffModes); - FOR_ALL_STAFF (i, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { ImportStaffPatrolArea(peep); } @@ -2999,9 +2998,7 @@ private: if (_s4.scenario_slot_index == SC_URBAN_PARK && _isScenario) { // First, make the queuing peep exit - int32_t i; - Peep* peep; - FOR_ALL_GUESTS (i, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->State == PEEP_STATE_QUEUING_FRONT && peep->CurrentRide == 0) { diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 422eae379a..24c0084b34 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -352,13 +352,10 @@ void Ride::QueueInsertGuestAtFront(StationIndex stationIndex, Peep* peep) */ void ride_update_favourited_stat() { - uint16_t spriteIndex; - Peep* peep; - for (auto& ride : GetRideManager()) ride.guests_favourite = 0; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->FavouriteRide != RIDE_ID_NULL) { @@ -1098,9 +1095,7 @@ void ride_remove_peeps(Ride* ride) } // Place all the peeps at exit - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_PEEPS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->State == PEEP_STATE_QUEUING_FRONT || peep->State == PEEP_STATE_ENTERING_RIDE || peep->State == PEEP_STATE_LEAVING_RIDE || peep->State == PEEP_STATE_ON_RIDE) @@ -2685,12 +2680,10 @@ Peep* ride_find_closest_mechanic(Ride* ride, int32_t forInspection) */ Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection) { - uint32_t closestDistance, distance; - uint16_t spriteIndex; - Peep *peep, *closestMechanic = nullptr; + Peep* closestMechanic = nullptr; + uint32_t closestDistance = std::numeric_limits::max(); - closestDistance = UINT_MAX; - FOR_ALL_STAFF (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->StaffType != STAFF_TYPE_MECHANIC) continue; @@ -2723,7 +2716,7 @@ Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection) continue; // Manhattan distance - distance = std::abs(peep->x - x) + std::abs(peep->y - y); + uint32_t distance = std::abs(peep->x - x) + std::abs(peep->y - y); if (distance < closestDistance) { closestDistance = distance; @@ -5485,10 +5478,7 @@ int32_t ride_get_refund_price(const Ride* ride) */ void Ride::StopGuestsQueuing() { - uint16_t spriteIndex; - Peep* peep; - - FOR_ALL_PEEPS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->State != PEEP_STATE_QUEUING) continue; diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 20b4edb4c6..c03be1fd30 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -5641,6 +5641,10 @@ SoundId Vehicle::UpdateScreamSound() for (uint16_t spriteIndex = sprite_index; spriteIndex != SPRITE_INDEX_NULL;) { auto vehicle2 = GetEntity(spriteIndex); + if (vehicle2 == nullptr) + { + return SoundId::Null; + } spriteIndex = vehicle2->next_vehicle_on_train; if (vehicle2->vehicle_sprite_type < 1) @@ -5661,8 +5665,11 @@ SoundId Vehicle::UpdateScreamSound() for (uint16_t spriteIndex = sprite_index; spriteIndex != SPRITE_INDEX_NULL;) { auto vehicle2 = GetEntity(spriteIndex); + if (vehicle2 == nullptr) + { + return SoundId::Null; + } spriteIndex = vehicle2->next_vehicle_on_train; - if (vehicle2->vehicle_sprite_type < 5) continue; if (vehicle2->vehicle_sprite_type <= 8) @@ -9694,6 +9701,10 @@ int32_t Vehicle::NumPeepsUntilTrainTail() const for (uint16_t spriteIndex = sprite_index; spriteIndex != SPRITE_INDEX_NULL;) { const Vehicle* vehicle = GetEntity(spriteIndex); + if (vehicle == nullptr) + { + return numPeeps; + } spriteIndex = vehicle->next_vehicle_on_train; numPeeps += vehicle->num_peeps; } diff --git a/src/openrct2/ride/gentle/MiniGolf.cpp b/src/openrct2/ride/gentle/MiniGolf.cpp index bb57347100..c45afa5219 100644 --- a/src/openrct2/ride/gentle/MiniGolf.cpp +++ b/src/openrct2/ride/gentle/MiniGolf.cpp @@ -1217,6 +1217,8 @@ void vehicle_visual_mini_golf_player( return; auto* peep = GetEntity(vehicle->peep[0]); + if (peep == nullptr) + return; uint8_t frame = mini_golf_peep_animation_frames[vehicle->mini_golf_current_animation][vehicle->animation_frame]; uint32_t ebx = (frame << 2) + (imageDirection >> 3); diff --git a/src/openrct2/scripting/ScMap.hpp b/src/openrct2/scripting/ScMap.hpp index 6f577d85f7..d217d5a5d6 100644 --- a/src/openrct2/scripting/ScMap.hpp +++ b/src/openrct2/scripting/ScMap.hpp @@ -131,15 +131,14 @@ namespace OpenRCT2::Scripting } std::vector result; - auto list = EntityList(targetList); - for (auto sprite : list) + for (auto sprite : EntityList(targetList)) { // Only the misc list checks the type property if (targetList != SPRITE_LIST_MISC || sprite->type == targetType) { if (targetList == SPRITE_LIST_PEEP) { - if (sprite->As()->AssignedPeepType == PEEP_TYPE_STAFF) + if (sprite->As()) result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); else result.push_back(GetObjectAsDukValue(_context, std::make_shared(sprite->sprite_index))); diff --git a/src/openrct2/world/Duck.cpp b/src/openrct2/world/Duck.cpp index 7cc6efb751..1d4abcbf36 100644 --- a/src/openrct2/world/Duck.cpp +++ b/src/openrct2/world/Duck.cpp @@ -363,8 +363,7 @@ void duck_press(Duck* duck) void duck_remove_all() { - auto list = EntityList(SPRITE_LIST_MISC); - for (auto duck : list) + for (auto duck : EntityList(SPRITE_LIST_MISC)) { duck->Remove(); } diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index 1a6cd726b1..c5c1e00a91 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -385,9 +385,7 @@ int32_t Park::CalculateParkRating() const // Find the number of happy peeps and the number of peeps who can't find the park exit uint32_t happyGuestCount = 0; uint32_t lostGuestCount = 0; - uint16_t spriteIndex; - Peep* peep; - FOR_ALL_GUESTS (spriteIndex, peep) + for (auto peep : EntityList(SPRITE_LIST_PEEP)) { if (peep->OutsideOfPark == 0) { @@ -470,8 +468,7 @@ int32_t Park::CalculateParkRating() const // Litter { int32_t litterCount = 0; - auto list = EntityList(SPRITE_LIST_LITTER); - for (auto litter : list) + for (auto litter : EntityList(SPRITE_LIST_LITTER)) { // Ignore recently dropped litter if (litter->creationTick - gScenarioTicks >= 7680)