From 542eb873fc6365bd09dc645523b62f5a23bf5677 Mon Sep 17 00:00:00 2001 From: Richard Fine Date: Fri, 11 Sep 2020 23:45:25 -0400 Subject: [PATCH] Consolidate pathfinding code Consolidate all the guest pathfinding code from Peep.cpp into GuestPathfinding.cpp, and make a dedicated header for GuestPathfinding to help make it easier to see what the actual public interface is to the pathfinding system. --- OpenRCT2.xcodeproj/project.pbxproj | 2 + src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/peep/Guest.cpp | 1 + src/openrct2/peep/GuestPathfinding.cpp | 91 +++++++++++++++++++++++++ src/openrct2/peep/GuestPathfinding.h | 45 +++++++++++++ src/openrct2/peep/Peep.cpp | 93 +------------------------- src/openrct2/peep/Peep.h | 24 ------- src/openrct2/peep/Staff.cpp | 1 + test/tests/Pathfinding.cpp | 1 + 9 files changed, 144 insertions(+), 115 deletions(-) create mode 100644 src/openrct2/peep/GuestPathfinding.h diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index d4ce0feeda..8758952a1c 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -976,6 +976,7 @@ 4CFE4E871F950164005243C2 /* TrackData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TrackData.h; sourceTree = ""; }; 4CFE4E8E1F9625B0005243C2 /* Track.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Track.cpp; sourceTree = ""; }; 4CFE4E8F1F9625B0005243C2 /* Track.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Track.h; sourceTree = ""; }; + 51160A24250C7A15002029F6 /* GuestPathfinding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GuestPathfinding.h; sourceTree = ""; }; 6341F4DF2400AA0E0052902B /* Drawing.Sprite.RLE.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Drawing.Sprite.RLE.cpp; sourceTree = ""; }; 6341F4E02400AA0F0052902B /* Drawing.Sprite.BMP.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Drawing.Sprite.BMP.cpp; sourceTree = ""; }; 6341F4E32400AA1C0052902B /* ZoomLevel.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ZoomLevel.hpp; sourceTree = ""; }; @@ -3099,6 +3100,7 @@ F76C84531EC4E7CC00FA49E2 /* peep */ = { isa = PBXGroup; children = ( + 51160A24250C7A15002029F6 /* GuestPathfinding.h */, 9346F9D6208A191900C77D91 /* Guest.cpp */, 9346F9D7208A191900C77D91 /* GuestPathfinding.cpp */, 4CFE4E7B1F90A3F1005243C2 /* Peep.cpp */, diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 0fac581d2b..71ee53303d 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -276,6 +276,7 @@ + diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index b078aa4aeb..a85bb9366b 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -36,6 +36,7 @@ #include "../world/Scenery.h" #include "../world/Sprite.h" #include "../world/Surface.h" +#include "GuestPathfinding.h" #include "Peep.h" #include "Staff.h" diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index 9878eb894c..57f17e95db 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -7,6 +7,8 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ +#include "GuestPathfinding.h" + #include "../core/Guard.hpp" #include "../ride/RideData.h" #include "../ride/Station.h" @@ -26,6 +28,17 @@ static int8_t _peepPathFindMaxJunctions; static int32_t _peepPathFindTilesChecked; static uint8_t _peepPathFindFewestNumSteps; +TileCoordsXYZ gPeepPathFindGoalPosition; +bool gPeepPathFindIgnoreForeignQueues; +ride_id_t gPeepPathFindQueueRideIndex; + +#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 +// Use to guard calls to log messages +bool gPathFindDebug = false; +// Use to put the peep name in the log message +utf8 gPathFindDebugPeepName[256]; +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + static int32_t guest_surface_path_finding(Peep* peep); /* A junction history for the peep pathfinding heuristic search @@ -2217,3 +2230,81 @@ int32_t guest_path_finding(Guest* peep) #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return peep_move_one_tile(direction, peep); } + +bool is_valid_path_z_and_direction(TileElement* tileElement, int32_t currentZ, int32_t currentDirection) +{ + if (tileElement->AsPath()->IsSloped()) + { + int32_t slopeDirection = tileElement->AsPath()->GetSlopeDirection(); + if (slopeDirection == currentDirection) + { + if (currentZ != tileElement->base_height) + return false; + } + else + { + slopeDirection = direction_reverse(slopeDirection); + if (slopeDirection != currentDirection) + return false; + if (currentZ != tileElement->base_height + 2) + return false; + } + } + else + { + if (currentZ != tileElement->base_height) + return false; + } + return true; +} + +/** + * + * rct2: 0x0069A98C + */ +void peep_reset_pathfind_goal(Peep* peep) +{ +#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) + { + log_info("Resetting PathfindGoal for %s", gPathFindDebugPeepName); + } +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + + peep->PathfindGoal.x = 0xFF; + peep->PathfindGoal.y = 0xFF; + peep->PathfindGoal.z = 0xFF; + peep->PathfindGoal.direction = INVALID_DIRECTION; +} + +#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 +void pathfind_logging_enable([[maybe_unused]] Peep* peep) +{ +# if defined(PATHFIND_DEBUG) && PATHFIND_DEBUG + /* Determine if the pathfinding debugging is wanted for this peep. */ + format_string(gPathFindDebugPeepName, sizeof(gPathFindDebugPeepName), peep->name_string_idx, &(peep->Id)); + + /* For guests, use the existing PEEP_FLAGS_TRACKING flag to + * determine for which guest(s) the pathfinding debugging will + * be output for. */ + if (peep->type == PEEP_TYPE_GUEST) + { + gPathFindDebug = peep->PeepFlags & PEEP_FLAGS_TRACKING; + } + /* For staff, there is no tracking button (any other similar + * suitable existing mechanism?), so fall back to a crude + * string comparison with a compile time hardcoded name. */ + else + { + gPathFindDebug = strcmp(gPathFindDebugPeepName, "Mechanic Debug") == 0; + } +# endif // defined(PATHFIND_DEBUG) && PATHFIND_DEBUG +} + +void pathfind_logging_disable() +{ +# if defined(PATHFIND_DEBUG) && PATHFIND_DEBUG + gPathFindDebug = false; +# endif // defined(PATHFIND_DEBUG) && PATHFIND_DEBUG +} +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 diff --git a/src/openrct2/peep/GuestPathfinding.h b/src/openrct2/peep/GuestPathfinding.h new file mode 100644 index 0000000000..d6e1312efc --- /dev/null +++ b/src/openrct2/peep/GuestPathfinding.h @@ -0,0 +1,45 @@ +/***************************************************************************** + * Copyright (c) 2014-2020 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#ifndef _GUESTPATHFINDING_H_ +#define _GUESTPATHFINDING_H_ + +#include "../common.h" +#include "../ride/RideTypes.h" +#include "../world/Location.hpp" + +struct Peep; +struct Guest; +struct TileElement; + +extern TileCoordsXYZ gPeepPathFindGoalPosition; +extern bool gPeepPathFindIgnoreForeignQueues; +extern ride_id_t gPeepPathFindQueueRideIndex; + +Direction peep_pathfind_choose_direction(const TileCoordsXYZ& loc, Peep* peep); +void peep_reset_pathfind_goal(Peep* peep); + +bool is_valid_path_z_and_direction(TileElement* tileElement, int32_t currentZ, int32_t currentDirection); + +int32_t guest_path_finding(Guest* peep); + +#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 +# define PATHFIND_DEBUG \ + 0 // Set to 0 to disable pathfinding debugging; + // Set to 1 to enable pathfinding debugging. + +// When PATHFIND_DEBUG is 1 (nonzero): +// The following calls configure debug logging for the given peep +// If they're a guest, pathfinding will be logged if they have PEEP_FLAGS_TRACKING set +// If they're staff, pathfinding will be logged if their name is "Mechanic Debug" +void pathfind_logging_enable(Peep* peep); +void pathfind_logging_disable(); +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + +#endif diff --git a/src/openrct2/peep/Peep.cpp b/src/openrct2/peep/Peep.cpp index 87673c5912..c1998b2fb3 100644 --- a/src/openrct2/peep/Peep.cpp +++ b/src/openrct2/peep/Peep.cpp @@ -44,17 +44,13 @@ #include "../world/SmallScenery.h" #include "../world/Sprite.h" #include "../world/Surface.h" +#include "GuestPathfinding.h" #include "Staff.h" #include #include #include -#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 -bool gPathFindDebug = false; -utf8 gPathFindDebugPeepName[256]; -#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - uint8_t gGuestChangeModifier; uint32_t gNumGuestsInPark; uint32_t gNumGuestsInParkLastWeek; @@ -69,10 +65,6 @@ uint32_t gNextGuestNumber; uint8_t gPeepWarningThrottle[16]; -TileCoordsXYZ gPeepPathFindGoalPosition; -bool gPeepPathFindIgnoreForeignQueues; -ride_id_t gPeepPathFindQueueRideIndex; - static uint8_t _unk_F1AEF0; static TileElement* _peepRideEntranceExitElement; @@ -1715,10 +1707,7 @@ Peep* Peep::Generate(const CoordsXYZ& coords) peep->CashInPocket = cash; peep->CashSpent = 0; peep->TimeInPark = -1; - peep->PathfindGoal.x = 0xFF; - peep->PathfindGoal.y = 0xFF; - peep->PathfindGoal.z = 0xFF; - peep->PathfindGoal.direction = INVALID_DIRECTION; + peep_reset_pathfind_goal(peep); peep->ItemStandardFlags = 0; peep->ItemExtraFlags = 0; peep->GuestHeadingToRideId = RIDE_ID_NULL; @@ -2954,33 +2943,6 @@ static bool peep_interact_with_shop(Peep* peep, const CoordsXYE& coords) return true; } -bool is_valid_path_z_and_direction(TileElement* tileElement, int32_t currentZ, int32_t currentDirection) -{ - if (tileElement->AsPath()->IsSloped()) - { - int32_t slopeDirection = tileElement->AsPath()->GetSlopeDirection(); - if (slopeDirection == currentDirection) - { - if (currentZ != tileElement->base_height) - return false; - } - else - { - slopeDirection = direction_reverse(slopeDirection); - if (slopeDirection != currentDirection) - return false; - if (currentZ != tileElement->base_height + 2) - return false; - } - } - else - { - if (currentZ != tileElement->base_height) - return false; - } - return true; -} - void Peep::PerformNextAction(uint8_t& pathing_result) { TileElement* tmpTile; @@ -3140,25 +3102,6 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result) peep_return_to_centre_of_tile(this); } -/** - * - * rct2: 0x0069A98C - */ -void peep_reset_pathfind_goal(Peep* peep) -{ -#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - if (gPathFindDebug) - { - log_info("Resetting PathfindGoal for %s", gPathFindDebugPeepName); - } -#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - - peep->PathfindGoal.x = 0xFF; - peep->PathfindGoal.y = 0xFF; - peep->PathfindGoal.z = 0xFF; - peep->PathfindGoal.direction = INVALID_DIRECTION; -} - /** * Gets the height including the bit depending on how far up the slope the peep * is. @@ -3266,38 +3209,6 @@ void peep_update_names(bool realNames) gfx_invalidate_screen(); } -#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 -void pathfind_logging_enable([[maybe_unused]] Peep* peep) -{ -# if defined(PATHFIND_DEBUG) && PATHFIND_DEBUG - /* Determine if the pathfinding debugging is wanted for this peep. */ - format_string(gPathFindDebugPeepName, sizeof(gPathFindDebugPeepName), peep->name_string_idx, &(peep->Id)); - - /* For guests, use the existing PEEP_FLAGS_TRACKING flag to - * determine for which guest(s) the pathfinding debugging will - * be output for. */ - if (peep->type == PEEP_TYPE_GUEST) - { - gPathFindDebug = peep->PeepFlags & PEEP_FLAGS_TRACKING; - } - /* For staff, there is no tracking button (any other similar - * suitable existing mechanism?), so fall back to a crude - * string comparison with a compile time hardcoded name. */ - else - { - gPathFindDebug = strcmp(gPathFindDebugPeepName, "Mechanic Debug") == 0; - } -# endif // defined(PATHFIND_DEBUG) && PATHFIND_DEBUG -} - -void pathfind_logging_disable() -{ -# if defined(PATHFIND_DEBUG) && PATHFIND_DEBUG - gPathFindDebug = false; -# endif // defined(PATHFIND_DEBUG) && PATHFIND_DEBUG -} -#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - void increment_guests_in_park() { if (gNumGuestsInPark < UINT32_MAX) diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index 66d7d46442..87e645fa86 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -1026,10 +1026,6 @@ extern uint32_t gNextGuestNumber; extern uint8_t gPeepWarningThrottle[16]; -extern TileCoordsXYZ gPeepPathFindGoalPosition; -extern bool gPeepPathFindIgnoreForeignQueues; -extern ride_id_t gPeepPathFindQueueRideIndex; - Peep* try_get_guest(uint16_t spriteIndex); int32_t peep_get_staff_count(); bool peep_can_be_picked_up(Peep* peep); @@ -1057,26 +1053,6 @@ void peep_update_names(bool realNames); void guest_set_name(uint16_t spriteIndex, const char* name); -Direction peep_pathfind_choose_direction(const TileCoordsXYZ& loc, Peep* peep); -void peep_reset_pathfind_goal(Peep* peep); - -bool is_valid_path_z_and_direction(TileElement* tileElement, int32_t currentZ, int32_t currentDirection); -int32_t guest_path_finding(Guest* peep); - -#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 -# define PATHFIND_DEBUG \ - 0 // Set to 0 to disable pathfinding debugging; - // Set to 1 to enable pathfinding debugging. -// Some variables used for the path finding debugging. -extern bool gPathFindDebug; // Use to guard calls to log messages -extern utf8 gPathFindDebugPeepName[256]; // Use to put the peep name in the log message - -// The following calls set the above two variables for a peep. -// ... when PATHFIND_DEBUG is 1 (nonzero) -void pathfind_logging_enable(Peep* peep); -void pathfind_logging_disable(); -#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - void increment_guests_in_park(); void increment_guests_heading_for_park(); void decrement_guests_in_park(); diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index 99cf4118d8..4e305ff4cf 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -37,6 +37,7 @@ #include "../world/SmallScenery.h" #include "../world/Sprite.h" #include "../world/Surface.h" +#include "GuestPathfinding.h" #include "Peep.h" #include diff --git a/test/tests/Pathfinding.cpp b/test/tests/Pathfinding.cpp index 8d1c9825bf..7dcdc9be71 100644 --- a/test/tests/Pathfinding.cpp +++ b/test/tests/Pathfinding.cpp @@ -1,5 +1,6 @@ #include "TestData.h" #include "openrct2/core/StringReader.hpp" +#include "openrct2/peep/GuestPathfinding.h" #include "openrct2/peep/Peep.h" #include "openrct2/ride/Station.h" #include "openrct2/scenario/Scenario.h"