From 401504aff958b8a84fcea835613b8d7ae1e0ef3a Mon Sep 17 00:00:00 2001 From: ThomasZ Date: Sat, 11 Apr 2020 18:16:53 +0200 Subject: [PATCH 1/3] Fix #11075: SpriteListHead Refactor Refactor "for (spriteIndex = gSpriteListHead[SPRITE_LIST_PEEP]; spriteIndex != SPRITE_INDEX_NULL; spriteIndex = nextSpriteIndex)" like for loops across files with replacing them by FOR_ALL_PEEPS(sprite_index, peep). The same for FOR_ALL_STAFF, FOR_ALL_GUESTS where necessary. modify incorrect codestyle modifying incorrect codestyle of the first attempted PR of issue 11075 (missing space after FOR_ALL_GUEST, FOR_ALL_STUFF ...) modify codestyle further Forgot to add one more space, correcting it. another codestyle correction apparently there is someting wrong with src/openrct2/peep/Peeps.cpp adding a newline after i = 0; event further modifying codestyle changing i = 0; to int i = 0; another codestyle modification, at this point just trying to pass CI clang adding another newline another modification --- contributors.md | 1 + src/openrct2/actions/SetCheatAction.hpp | 14 ++++++-------- src/openrct2/actions/StaffSetColourAction.hpp | 4 ++-- src/openrct2/peep/Peep.cpp | 8 ++------ src/openrct2/peep/Staff.cpp | 4 ++-- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/contributors.md b/contributors.md index a9d2eacb27..c4af6a2e87 100644 --- a/contributors.md +++ b/contributors.md @@ -138,6 +138,7 @@ The following people are not part of the development team, but have been contrib * Kevin Laframboise (klaframboise) * Tushar Sariya (TusharSariya) * (WantDiscussion) +* Tomáš Žilínek (TomasZilinek) ## Toolchain * (Balletie) - macOS diff --git a/src/openrct2/actions/SetCheatAction.hpp b/src/openrct2/actions/SetCheatAction.hpp index b3b65aaac2..4965b37c68 100644 --- a/src/openrct2/actions/SetCheatAction.hpp +++ b/src/openrct2/actions/SetCheatAction.hpp @@ -642,7 +642,7 @@ private: void RemoveAllGuests() const { - uint16_t spriteIndex, nextSpriteIndex; + uint16_t spriteIndex; for (auto& ride : GetRideManager()) { ride.num_riders = 0; @@ -685,14 +685,12 @@ private: } } - for (spriteIndex = gSpriteListHead[SPRITE_LIST_PEEP]; spriteIndex != SPRITE_INDEX_NULL; spriteIndex = nextSpriteIndex) + Peep* peep; + uint16_t sprite_index; + + FOR_ALL_GUESTS (sprite_index, peep) { - auto peep = &(get_sprite(spriteIndex)->peep); - nextSpriteIndex = peep->next; - if (peep->type == PEEP_TYPE_GUEST) - { - peep->Remove(); - } + peep->Remove(); } window_invalidate_by_class(WC_RIDE); diff --git a/src/openrct2/actions/StaffSetColourAction.hpp b/src/openrct2/actions/StaffSetColourAction.hpp index dd5f6aacb6..b47214dc6b 100644 --- a/src/openrct2/actions/StaffSetColourAction.hpp +++ b/src/openrct2/actions/StaffSetColourAction.hpp @@ -67,9 +67,9 @@ public: // Update each staff member's uniform int32_t spriteIndex; Peep* peep; - FOR_ALL_PEEPS (spriteIndex, peep) + FOR_ALL_STAFF (spriteIndex, peep) { - if (peep->type == PEEP_TYPE_STAFF && peep->staff_type == _staffType) + if (peep->staff_type == _staffType) { peep->tshirt_colour = _colour; peep->trousers_colour = _colour; diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 70d77a7c36..26c1c304a8 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -404,19 +404,15 @@ int32_t peep_get_staff_count() */ void peep_update_all() { - int32_t i; + int32_t i = 0; uint16_t spriteIndex; Peep* peep; if (gScreenFlags & SCREEN_FLAGS_EDITOR) return; - - spriteIndex = gSpriteListHead[SPRITE_LIST_PEEP]; - i = 0; - while (spriteIndex != SPRITE_INDEX_NULL) + FOR_ALL_PEEPS (spriteIndex, peep) { peep = &(get_sprite(spriteIndex)->peep); - spriteIndex = peep->next; if ((uint32_t)(i & 0x7F) != (gCurrentTicks & 0x7F)) { diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index aff8d6d7f7..f9ddab18f6 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -143,6 +143,7 @@ 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) { @@ -152,8 +153,7 @@ void staff_update_greyed_patrol_areas() gStaffPatrolAreas[staffPatrolOffset + i] = 0; } - for (uint16_t sprite_index = gSpriteListHead[SPRITE_LIST_PEEP]; sprite_index != SPRITE_INDEX_NULL; - sprite_index = peep->next) + FOR_ALL_STAFF (sprite_index, peep) { peep = GET_PEEP(sprite_index); From f9ae3dd9801bedbae5fc3dee396c1bab3f67e86e Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Sat, 18 Apr 2020 10:34:41 +0100 Subject: [PATCH 2/3] Make review changes --- src/openrct2/actions/SetCheatAction.hpp | 4 +--- src/openrct2/peep/Peep.cpp | 2 -- src/openrct2/peep/Staff.cpp | 4 +--- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/openrct2/actions/SetCheatAction.hpp b/src/openrct2/actions/SetCheatAction.hpp index 4965b37c68..a3038fa3a5 100644 --- a/src/openrct2/actions/SetCheatAction.hpp +++ b/src/openrct2/actions/SetCheatAction.hpp @@ -686,9 +686,7 @@ private: } Peep* peep; - uint16_t sprite_index; - - FOR_ALL_GUESTS (sprite_index, peep) + FOR_ALL_GUESTS (spriteIndex, peep) { peep->Remove(); } diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 26c1c304a8..f968a7285f 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -412,8 +412,6 @@ void peep_update_all() return; FOR_ALL_PEEPS (spriteIndex, peep) { - peep = &(get_sprite(spriteIndex)->peep); - if ((uint32_t)(i & 0x7F) != (gCurrentTicks & 0x7F)) { peep->Update(); diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index f9ddab18f6..2b24a1c1f2 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -155,9 +155,7 @@ void staff_update_greyed_patrol_areas() FOR_ALL_STAFF (sprite_index, peep) { - peep = GET_PEEP(sprite_index); - - if (peep->type == PEEP_TYPE_STAFF && staff_type == peep->staff_type) + if (peep->staff_type == staff_type) { int32_t peepPatrolOffset = peep->staff_id * STAFF_PATROL_AREA_SIZE; for (int32_t i = 0; i < STAFF_PATROL_AREA_SIZE; i++) From b81f6d0bedabf84ec53bdbc9e344e8f60d116d48 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Sat, 18 Apr 2020 11:41:05 +0100 Subject: [PATCH 3/3] Fix crash caused by bad pointers --- src/openrct2/peep/Peep.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index f968a7285f..616135486e 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -410,8 +410,14 @@ void peep_update_all() if (gScreenFlags & SCREEN_FLAGS_EDITOR) return; - FOR_ALL_PEEPS (spriteIndex, peep) + + // 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;) { + peep = GET_PEEP(spriteIndex); + spriteIndex = peep->next; + if ((uint32_t)(i & 0x7F) != (gCurrentTicks & 0x7F)) { peep->Update();