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.
This commit is contained in:
Richard Fine 2020-09-11 23:45:25 -04:00
parent 12cc413732
commit 542eb873fc
9 changed files with 144 additions and 115 deletions

View File

@ -976,6 +976,7 @@
4CFE4E871F950164005243C2 /* TrackData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TrackData.h; sourceTree = "<group>"; };
4CFE4E8E1F9625B0005243C2 /* Track.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Track.cpp; sourceTree = "<group>"; };
4CFE4E8F1F9625B0005243C2 /* Track.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Track.h; sourceTree = "<group>"; };
51160A24250C7A15002029F6 /* GuestPathfinding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GuestPathfinding.h; sourceTree = "<group>"; };
6341F4DF2400AA0E0052902B /* Drawing.Sprite.RLE.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Drawing.Sprite.RLE.cpp; sourceTree = "<group>"; };
6341F4E02400AA0F0052902B /* Drawing.Sprite.BMP.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Drawing.Sprite.BMP.cpp; sourceTree = "<group>"; };
6341F4E32400AA1C0052902B /* ZoomLevel.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ZoomLevel.hpp; sourceTree = "<group>"; };
@ -3099,6 +3100,7 @@
F76C84531EC4E7CC00FA49E2 /* peep */ = {
isa = PBXGroup;
children = (
51160A24250C7A15002029F6 /* GuestPathfinding.h */,
9346F9D6208A191900C77D91 /* Guest.cpp */,
9346F9D7208A191900C77D91 /* GuestPathfinding.cpp */,
4CFE4E7B1F90A3F1005243C2 /* Peep.cpp */,

View File

@ -276,6 +276,7 @@
<ClInclude Include="paint\tile_element\Paint.TileElement.h" />
<ClInclude Include="paint\VirtualFloor.h" />
<ClInclude Include="ParkImporter.h" />
<ClInclude Include="peep\GuestPathfinding.h" />
<ClInclude Include="peep\Peep.h" />
<ClInclude Include="peep\Staff.h" />
<ClInclude Include="PlatformEnvironment.h" />

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -44,17 +44,13 @@
#include "../world/SmallScenery.h"
#include "../world/Sprite.h"
#include "../world/Surface.h"
#include "GuestPathfinding.h"
#include "Staff.h"
#include <algorithm>
#include <iterator>
#include <limits>
#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)

View File

@ -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();

View File

@ -37,6 +37,7 @@
#include "../world/SmallScenery.h"
#include "../world/Sprite.h"
#include "../world/Surface.h"
#include "GuestPathfinding.h"
#include "Peep.h"
#include <algorithm>

View File

@ -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"