diff --git a/src/openrct2-ui/windows/Map.cpp b/src/openrct2-ui/windows/Map.cpp index 486487dfa7..f32b5fab79 100644 --- a/src/openrct2-ui/windows/Map.cpp +++ b/src/openrct2-ui/windows/Map.cpp @@ -1106,13 +1106,11 @@ static void window_map_paint_peep_overlay(rct_drawpixelinfo* dpi) */ static void window_map_paint_train_overlay(rct_drawpixelinfo* dpi) { - Vehicle *train, *vehicle; - uint16_t train_index, vehicle_index; - - for (train_index = gSpriteListHead[SPRITE_LIST_TRAIN_HEAD]; train_index != SPRITE_INDEX_NULL; train_index = train->next) + auto list = EntityList(SPRITE_LIST_TRAIN_HEAD); + for (auto train : list) { - train = GET_VEHICLE(train_index); - for (vehicle_index = train_index; vehicle_index != SPRITE_INDEX_NULL; vehicle_index = vehicle->next_vehicle_on_train) + Vehicle* vehicle = nullptr; + 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/actions/SetCheatAction.hpp b/src/openrct2/actions/SetCheatAction.hpp index c2ba7fe6c6..da2a3f5090 100644 --- a/src/openrct2/actions/SetCheatAction.hpp +++ b/src/openrct2/actions/SetCheatAction.hpp @@ -427,10 +427,9 @@ private: void RemoveLitter() const { - for (uint16_t spriteIndex = gSpriteListHead[SPRITE_LIST_LITTER]; spriteIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_LITTER); + for (auto litter : list) { - auto litter = GetEntity(spriteIndex); - spriteIndex = litter->next; sprite_remove(litter); } @@ -696,10 +695,9 @@ private: // Do not use the FOR_ALL_PEEPS macro for this as next sprite index // will be fetched on a deleted peep. - for (spriteIndex = gSpriteListHead[SPRITE_LIST_PEEP]; spriteIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_PEEP); + for (auto peep : list) { - auto peep = GET_PEEP(spriteIndex); - spriteIndex = peep->next; if (peep->AssignedPeepType == PEEP_TYPE_GUEST) { peep->Remove(); diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index 123e2ba748..fb539bf474 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -3006,11 +3006,8 @@ static PeepThoughtType peep_assess_surroundings(int16_t centre_x, int16_t centre } } - for (uint16_t sprite_idx = gSpriteListHead[SPRITE_LIST_LITTER]; sprite_idx != SPRITE_INDEX_NULL;) + for (auto litter : EntityList(SPRITE_LIST_LITTER)) { - auto litter = GetEntity(sprite_idx); - sprite_idx = litter->next; - int16_t dist_x = abs(litter->x - centre_x); int16_t dist_y = abs(litter->y - centre_y); if (std::max(dist_x, dist_y) <= 160) diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index b8cd4cb645..b2ebde2bec 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -402,20 +402,15 @@ int32_t peep_get_staff_count() */ void peep_update_all() { - int32_t i = 0; - uint16_t spriteIndex; - Peep* peep; - if (gScreenFlags & SCREEN_FLAGS_EDITOR) 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. - for (spriteIndex = gSpriteListHead[SPRITE_LIST_PEEP]; spriteIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_PEEP); + for (auto peep : list) { - peep = GET_PEEP(spriteIndex); - spriteIndex = peep->next; - if (static_cast(i & 0x7F) != (gCurrentTicks & 0x7F)) { peep->Update(); diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index bb225dc959..14c251245e 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -990,7 +990,7 @@ enum /** * Helper macro loop for enumerating through all the peeps. To avoid needing a end loop counterpart, statements are - * applied in tautology if statements. + * 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) \ diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index 2b3e4f4df3..ea77d0fc88 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -443,11 +443,9 @@ static uint8_t staff_handyman_direction_to_nearest_litter(Peep* peep) { uint16_t nearestLitterDist = 0xFFFF; Litter* nearestLitter = nullptr; - for (uint16_t litterIndex = gSpriteListHead[SPRITE_LIST_LITTER]; litterIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_LITTER); + for (auto litter : list) { - auto litter = GetEntity(litterIndex); - litterIndex = litter->next; - uint16_t distance = abs(litter->x - peep->x) + abs(litter->y - peep->y) + abs(litter->z - peep->z) * 4; if (distance < nearestLitterDist) diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 2a069cd9c5..20b4edb4c6 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -1321,10 +1321,8 @@ void vehicle_sounds_update() vehicle_sounds_update_window_setup(); - for (uint16_t i = gSpriteListHead[SPRITE_LIST_TRAIN_HEAD]; i != SPRITE_INDEX_NULL;) + for (auto vehicle : EntityList(SPRITE_LIST_TRAIN_HEAD)) { - auto vehicle = GetEntity(i); - i = vehicle->next; vehicle->UpdateSoundParams(vehicleSoundParamsList); } @@ -1395,21 +1393,14 @@ void vehicle_sounds_update() */ void vehicle_update_all() { - uint16_t sprite_index; - Vehicle* vehicle; - if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) return; if ((gScreenFlags & SCREEN_FLAGS_TRACK_DESIGNER) && gS6Info.editor_step != EDITOR_STEP_ROLLERCOASTER_DESIGNER) return; - sprite_index = gSpriteListHead[SPRITE_LIST_TRAIN_HEAD]; - while (sprite_index != SPRITE_INDEX_NULL) + for (auto vehicle : EntityList(SPRITE_LIST_TRAIN_HEAD)) { - vehicle = GET_VEHICLE(sprite_index); - sprite_index = vehicle->next; - vehicle->Update(); } } diff --git a/src/openrct2/scripting/ScMap.hpp b/src/openrct2/scripting/ScMap.hpp index 7628f60de9..6f577d85f7 100644 --- a/src/openrct2/scripting/ScMap.hpp +++ b/src/openrct2/scripting/ScMap.hpp @@ -131,12 +131,9 @@ namespace OpenRCT2::Scripting } std::vector result; - - for (auto spriteId = gSpriteListHead[targetList]; spriteId != SPRITE_INDEX_NULL;) + auto list = EntityList(targetList); + for (auto sprite : list) { - auto sprite = GetEntity(spriteId); - spriteId = sprite->next; - // Only the misc list checks the type property if (targetList != SPRITE_LIST_MISC || sprite->type == targetType) { diff --git a/src/openrct2/world/Duck.cpp b/src/openrct2/world/Duck.cpp index ca83e93358..7cc6efb751 100644 --- a/src/openrct2/world/Duck.cpp +++ b/src/openrct2/world/Duck.cpp @@ -363,15 +363,10 @@ void duck_press(Duck* duck) void duck_remove_all() { - for (uint16_t spriteIndex = gSpriteListHead[SPRITE_LIST_MISC]; spriteIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_MISC); + for (auto duck : list) { - auto sprite = GetEntity(spriteIndex); - spriteIndex = sprite->next; - if (sprite->type == SPRITE_MISC_DUCK) - { - sprite->Invalidate1(); - sprite_remove(sprite); - } + duck->Remove(); } } diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index 208707c1b5..1a6cd726b1 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -470,11 +470,9 @@ int32_t Park::CalculateParkRating() const // Litter { int32_t litterCount = 0; - for (uint16_t spriteIndex = gSpriteListHead[SPRITE_LIST_LITTER]; spriteIndex != SPRITE_INDEX_NULL;) + auto list = EntityList(SPRITE_LIST_LITTER); + for (auto litter : list) { - auto litter = GetEntity(spriteIndex); - spriteIndex = litter->next; - // Ignore recently dropped litter if (litter->creationTick - gScenarioTicks >= 7680) { diff --git a/src/openrct2/world/Sprite.cpp b/src/openrct2/world/Sprite.cpp index 3b76ad8b2f..0bbc1709cc 100644 --- a/src/openrct2/world/Sprite.cpp +++ b/src/openrct2/world/Sprite.cpp @@ -358,11 +358,8 @@ static void sprite_reset(SpriteBase* sprite) */ void sprite_clear_all_unused() { - for (uint16_t spriteIndex = gSpriteListHead[SPRITE_LIST_FREE]; spriteIndex != SPRITE_INDEX_NULL;) + for (auto sprite : EntityList(SPRITE_LIST_FREE)) { - auto sprite = GetEntity(spriteIndex); - spriteIndex = sprite->next; - sprite_reset(sprite); sprite->linked_list_index = SPRITE_LIST_FREE; @@ -631,11 +628,10 @@ static void sprite_misc_update(rct_sprite* sprite) */ void sprite_misc_update_all() { - for (auto spriteIndex = gSpriteListHead[SPRITE_LIST_MISC]; spriteIndex != SPRITE_INDEX_NULL;) + for (auto entity : EntityList(SPRITE_LIST_MISC)) { - auto sprite = GetEntity(spriteIndex); - spriteIndex = sprite->next; - sprite_misc_update(reinterpret_cast(sprite)); + // TODO: Use more specific Sprite class + sprite_misc_update(reinterpret_cast(entity)); } } @@ -806,10 +802,8 @@ void litter_create(int32_t x, int32_t y, int32_t z, int32_t direction, int32_t t { Litter* newestLitter = nullptr; uint32_t newestLitterCreationTick = 0; - for (uint16_t spriteIndex = gSpriteListHead[SPRITE_LIST_LITTER]; spriteIndex != SPRITE_INDEX_NULL;) + for (auto litter : EntityList(SPRITE_LIST_LITTER)) { - Litter* litter = GetEntity(spriteIndex); - spriteIndex = litter->next; if (newestLitterCreationTick <= litter->creationTick) { newestLitterCreationTick = litter->creationTick; @@ -1066,10 +1060,9 @@ static SpriteBase* find_sprite_quadrant_cycle(uint16_t sprite_idx) static bool index_is_in_list(uint16_t index, enum SPRITE_LIST sl) { - for (uint16_t sprite_index = gSpriteListHead[sl]; sprite_index != SPRITE_INDEX_NULL; - sprite_index = GetEntity(sprite_index)->next) + for (auto entity : EntityList(sl)) { - if (sprite_index == index) + if (entity->sprite_index == index) { return true; }