From 2b26e423e4b174dcc6a812b60af73371d3def949 Mon Sep 17 00:00:00 2001 From: Duncan Date: Sun, 21 Feb 2021 12:23:31 +0000 Subject: [PATCH] Remove AsGuest and AsStaff duplicated functions --- src/openrct2-ui/windows/Ride.cpp | 2 +- src/openrct2-ui/windows/StaffList.cpp | 2 +- src/openrct2/actions/StaffHireNewAction.cpp | 2 +- src/openrct2/peep/Guest.cpp | 12 +++---- src/openrct2/peep/GuestPathfinding.cpp | 11 +++--- src/openrct2/peep/Peep.cpp | 38 ++++++++------------- src/openrct2/peep/Peep.h | 3 -- src/openrct2/peep/Staff.cpp | 4 +-- src/openrct2/ride/Ride.cpp | 4 +-- src/openrct2/ride/Station.cpp | 4 +-- src/openrct2/scripting/ScEntity.hpp | 4 +-- 11 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/openrct2-ui/windows/Ride.cpp b/src/openrct2-ui/windows/Ride.cpp index ce8575434f..54d1ca3fc9 100644 --- a/src/openrct2-ui/windows/Ride.cpp +++ b/src/openrct2-ui/windows/Ride.cpp @@ -4130,7 +4130,7 @@ static void window_ride_maintenance_paint(rct_window* w, rct_drawpixelinfo* dpi) for (auto peep : EntityList(EntityListId::Peep)) { - if (peep->AssignedStaffType == StaffType::Mechanic) + if (peep->IsMechanic()) { stringId = STR_CALLING_MECHANIC; break; diff --git a/src/openrct2-ui/windows/StaffList.cpp b/src/openrct2-ui/windows/StaffList.cpp index 35e527cb58..f5de96e323 100644 --- a/src/openrct2-ui/windows/StaffList.cpp +++ b/src/openrct2-ui/windows/StaffList.cpp @@ -571,7 +571,7 @@ private: { continue; } - if (!peep->AsStaff()->IsLocationInPatrol(footpathCoords)) + if (!peep->IsLocationInPatrol(footpathCoords)) { continue; } diff --git a/src/openrct2/actions/StaffHireNewAction.cpp b/src/openrct2/actions/StaffHireNewAction.cpp index 75b124f2d4..bf60a3a45f 100644 --- a/src/openrct2/actions/StaffHireNewAction.cpp +++ b/src/openrct2/actions/StaffHireNewAction.cpp @@ -211,7 +211,7 @@ GameActions::Result::Ptr StaffHireNewAction::QueryExecute(bool execute) const } // Staff uses this - newPeep->AsStaff()->SetHireDate(gDateMonthsElapsed); + newPeep->As()->SetHireDate(gDateMonthsElapsed); newPeep->PathfindGoal.x = 0xFF; newPeep->PathfindGoal.y = 0xFF; newPeep->PathfindGoal.z = 0xFF; diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index e7f210fe0d..d399068ca0 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -254,16 +254,12 @@ void Guest::ApplyEasterEggToNearbyGuests(easter_egg_function easter_egg) if (!GuestHasValidXY()) return; - for (auto otherPeep : EntityTileList({ x, y })) + for (auto* otherGuest : EntityTileList({ x, y })) { - auto otherGuest = otherPeep->AsGuest(); - if (otherGuest) + auto zDiff = std::abs(otherGuest->z - z); + if (zDiff <= 32) { - auto zDiff = std::abs(otherPeep->z - z); - if (zDiff <= 32) - { - (*this.*easter_egg)(otherGuest); - } + (*this.*easter_egg)(otherGuest); } } } diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index 2f24c6361f..f60b53479e 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -712,9 +712,10 @@ static void peep_pathfind_heuristic_search( } bool nextInPatrolArea = inPatrolArea; - if (peep->AssignedPeepType == PeepType::Staff && peep->AssignedStaffType == StaffType::Mechanic) + auto* staff = peep->As(); + if (staff != nullptr && staff->IsMechanic()) { - nextInPatrolArea = peep->AsStaff()->IsLocationInPatrol(loc.ToCoordsXY()); + nextInPatrolArea = staff->IsLocationInPatrol(loc.ToCoordsXY()); if (inPatrolArea && !nextInPatrolArea) { /* The mechanic will leave his patrol area by taking @@ -823,7 +824,6 @@ static void peep_pathfind_heuristic_search( if (tileElement->AsPath()->IsWide()) { /* Check if staff can ignore this wide flag. */ - const Staff* staff = peep->As(); if (staff == nullptr || !staff->CanIgnoreWideFlag(loc.ToCoordsXYZ(), tileElement)) { searchResult = PATH_SEARCH_WIDE; @@ -1497,12 +1497,13 @@ Direction peep_pathfind_choose_direction(const TileCoordsXYZ& loc, Peep* peep) uint8_t endDirectionList[16] = { 0 }; bool inPatrolArea = false; - if (peep->AssignedPeepType == PeepType::Staff && peep->AssignedStaffType == StaffType::Mechanic) + auto* staff = peep->As(); + if (staff != nullptr && staff->IsMechanic()) { /* Mechanics are the only staff type that * pathfind to a destination. Determine if the * mechanic is in their patrol area. */ - inPatrolArea = peep->AsStaff()->IsLocationInPatrol(peep->NextLoc); + inPatrolArea = staff->IsLocationInPatrol(peep->NextLoc); } #if defined(DEBUG_LEVEL_2) && DEBUG_LEVEL_2 diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 386152485e..e29547bb57 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -321,16 +321,6 @@ template<> bool SpriteBase::Is() const return sprite_identifier == SpriteIdentifier::Peep; } -Guest* Peep::AsGuest() -{ - return AssignedPeepType == PeepType::Guest ? static_cast(this) : nullptr; -} - -Staff* Peep::AsStaff() -{ - return AssignedPeepType == PeepType::Staff ? static_cast(this) : nullptr; -} - uint8_t Peep::GetNextDirection() const { return NextFlags & PEEP_NEXT_FLAG_DIRECTION_MASK; @@ -457,14 +447,14 @@ void peep_update_all() */ static void peep_128_tick_update(Peep* peep, int32_t index) { - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest != nullptr) { guest->Tick128UpdateGuest(index); } else { - auto staff = peep->AsStaff(); + auto* staff = peep->As(); if (staff != nullptr) { staff->Tick128UpdateStaff(); @@ -744,7 +734,7 @@ void peep_window_state_update(Peep* peep) void Peep::Pickup() { - auto guest = AsGuest(); + auto* guest = As(); if (guest != nullptr) { guest->RemoveFromRide(); @@ -839,7 +829,7 @@ std::unique_ptr Peep::Place(const TileCoordsXYZ& location, */ void peep_sprite_remove(Peep* peep) { - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest != nullptr) { guest->RemoveFromRide(); @@ -953,7 +943,7 @@ void Peep::UpdateFalling() // Looks like we are drowning! MoveTo({ x, y, height }); - auto guest = AsGuest(); + auto* guest = As(); if (guest != nullptr) { // Drop balloon if held @@ -1149,7 +1139,7 @@ void Peep::Update() StepProgress = carryCheck; if (carryCheck <= 255) { - auto guest = AsGuest(); + auto* guest = As(); if (guest != nullptr) { guest->UpdateEasterEggInteractions(); @@ -1174,14 +1164,14 @@ void Peep::Update() break; default: { - auto guest = AsGuest(); + auto* guest = As(); if (guest != nullptr) { guest->UpdateGuest(); } else { - auto staff = AsStaff(); + auto* staff = As(); if (staff != nullptr) { staff->UpdateStaff(stepsToTake); @@ -2340,7 +2330,7 @@ static bool peep_interact_with_entrance(Peep* peep, const CoordsXYE& coords, uin if (ride == nullptr) return false; - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest == nullptr) { // Default staff behaviour attempting to enter a @@ -2413,7 +2403,7 @@ static bool peep_interact_with_entrance(Peep* peep, const CoordsXYE& coords, uin else { // PARK_ENTRANCE - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest == nullptr) { // Staff cannot leave the park, so go back. @@ -2771,7 +2761,7 @@ static void peep_interact_with_path(Peep* peep, const CoordsXYE& coords) } } - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest != nullptr && tile_element->AsPath()->IsQueue()) { auto rideIndex = tile_element->AsPath()->GetRideIndex(); @@ -2879,7 +2869,7 @@ static bool peep_interact_with_shop(Peep* peep, const CoordsXYE& coords) if (ride == nullptr || !ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_IS_SHOP)) return false; - auto guest = peep->AsGuest(); + auto* guest = peep->As(); if (guest == nullptr) { peep_return_to_centre_of_tile(peep); @@ -3000,14 +2990,14 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result) pathing_result |= PATHING_DESTINATION_REACHED; uint8_t result = 0; - auto guest = AsGuest(); + auto* guest = As(); if (guest != nullptr) { result = guest_path_finding(guest); } else { - auto staff = AsStaff(); + auto* staff = As(); result = staff->DoPathFinding(); } diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index 779706a7db..aa5017fb1e 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -728,9 +728,6 @@ struct Peep : SpriteBase uint64_t ItemFlags; public: // Peep - Guest* AsGuest(); - Staff* AsStaff(); - void Update(); std::optional UpdateAction(int16_t& xy_distance); std::optional UpdateAction(); diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index d58df21b5a..ba75c5d6e1 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -1820,9 +1820,7 @@ void Staff::Tick128UpdateStaff() bool Staff::IsMechanic() const { - return ( - sprite_identifier == SpriteIdentifier::Peep && AssignedPeepType == PeepType::Staff - && AssignedStaffType == StaffType::Mechanic); + return AssignedStaffType == StaffType::Mechanic; } void Staff::UpdateStaff(uint32_t stepsToTake) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 9fe57c0bc3..dba996bf80 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -2730,7 +2730,7 @@ Peep* find_closest_mechanic(const CoordsXY& entrancePosition, int32_t forInspect for (auto peep : EntityList(EntityListId::Peep)) { - if (peep->AssignedStaffType != StaffType::Mechanic) + if (!peep->IsMechanic()) continue; if (!forInspection) @@ -2754,7 +2754,7 @@ Peep* find_closest_mechanic(const CoordsXY& entrancePosition, int32_t forInspect auto location = entrancePosition.ToTileStart(); if (map_is_location_in_park(location)) - if (!peep->AsStaff()->IsLocationInPatrol(location)) + if (!peep->IsLocationInPatrol(location)) continue; if (peep->x == LOCATION_NULL) diff --git a/src/openrct2/ride/Station.cpp b/src/openrct2/ride/Station.cpp index a8472f8685..0283b951ca 100644 --- a/src/openrct2/ride/Station.cpp +++ b/src/openrct2/ride/Station.cpp @@ -280,11 +280,9 @@ static void ride_race_init_vehicle_speeds(Ride* ride) if (vehicle->num_peeps != 0) { - auto peep = GetEntity(vehicle->peep[0]); + auto* guest = GetEntity(vehicle->peep[0]); // Easter egg names should only work on guests - Guest* guest = peep->AsGuest(); - if (guest != nullptr) { switch (guest->GetEasterEggNameId()) diff --git a/src/openrct2/scripting/ScEntity.hpp b/src/openrct2/scripting/ScEntity.hpp index 46932893d8..37e0b5ecec 100644 --- a/src/openrct2/scripting/ScEntity.hpp +++ b/src/openrct2/scripting/ScEntity.hpp @@ -834,7 +834,7 @@ namespace OpenRCT2::Scripting auto peep = GetPeep(); if (peep != nullptr) { - return peep->AsGuest(); + return peep->As(); } return nullptr; } @@ -1123,7 +1123,7 @@ namespace OpenRCT2::Scripting auto peep = GetPeep(); if (peep != nullptr) { - return peep->AsStaff(); + return peep->As(); } return nullptr; }