mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-23 07:43:01 +01:00
Pathfinding cleanup (#21407)
* Eliminate unnecessary abstraction for path finding * Pass the goal to ChooseDirection instead of using globals * Remove gPeepPathFindGoalPosition and pass it by parameter instead * Remove _peepPathFindIsStaff and make ignoring banners explicit * Code style and naming fixups * Apply clang-format * Add comment specifying to why it does not ignore banners * Apply review comments
This commit is contained in:
@@ -70,8 +70,6 @@ uint8_t gGuestChangeModifier;
|
|||||||
|
|
||||||
uint8_t gPeepWarningThrottle[16];
|
uint8_t gPeepWarningThrottle[16];
|
||||||
|
|
||||||
std::unique_ptr<GuestPathfinding> gGuestPathfinder = std::make_unique<OriginalPathfinding>();
|
|
||||||
|
|
||||||
static uint8_t _unk_F1AEF0;
|
static uint8_t _unk_F1AEF0;
|
||||||
static TileElement* _peepRideEntranceExitElement;
|
static TileElement* _peepRideEntranceExitElement;
|
||||||
|
|
||||||
@@ -2393,7 +2391,7 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
|
|||||||
|
|
||||||
if (guest != nullptr)
|
if (guest != nullptr)
|
||||||
{
|
{
|
||||||
result = gGuestPathfinder->CalculateNextDestination(*guest);
|
result = PathFinding::CalculateNextDestination(*guest);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -2853,3 +2851,20 @@ void Peep::Paint(PaintSession& session, int32_t imageDirection) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* rct2: 0x0069A98C
|
||||||
|
*/
|
||||||
|
void Peep::ResetPathfindGoal()
|
||||||
|
{
|
||||||
|
#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
|
if (_pathFindDebug)
|
||||||
|
{
|
||||||
|
LOG_INFO("Resetting PathfindGoal for %s", _pathFindDebugPeepName);
|
||||||
|
}
|
||||||
|
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
|
|
||||||
|
PathfindGoal.SetNull();
|
||||||
|
PathfindGoal.direction = INVALID_DIRECTION;
|
||||||
|
}
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ bool Staff::CanIgnoreWideFlag(const CoordsXYZ& staffPos, TileElement* path) cons
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* test_element is a path */
|
/* test_element is a path */
|
||||||
if (!GuestPathfinding::IsValidPathZAndDirection(test_element, adjacPos.z / COORDS_Z_STEP, adjac_dir))
|
if (!PathFinding::IsValidPathZAndDirection(test_element, adjacPos.z / COORDS_Z_STEP, adjac_dir))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* test_element is a connected path */
|
/* test_element is a connected path */
|
||||||
@@ -713,10 +713,6 @@ Direction Staff::MechanicDirectionPath(uint8_t validDirections, PathElement* pat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gPeepPathFindGoalPosition.x = location.x;
|
|
||||||
gPeepPathFindGoalPosition.y = location.y;
|
|
||||||
gPeepPathFindGoalPosition.z = location.z;
|
|
||||||
|
|
||||||
gPeepPathFindIgnoreForeignQueues = false;
|
gPeepPathFindIgnoreForeignQueues = false;
|
||||||
gPeepPathFindQueueRideIndex = RideId::GetNull();
|
gPeepPathFindQueueRideIndex = RideId::GetNull();
|
||||||
|
|
||||||
@@ -724,7 +720,8 @@ Direction Staff::MechanicDirectionPath(uint8_t validDirections, PathElement* pat
|
|||||||
PathfindLoggingEnable(*this);
|
PathfindLoggingEnable(*this);
|
||||||
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
|
|
||||||
Direction pathfindDirection = gGuestPathfinder->ChooseDirection(TileCoordsXYZ{ NextLoc }, *this);
|
const auto goalPos = TileCoordsXYZ{ location };
|
||||||
|
Direction pathfindDirection = PathFinding::ChooseDirection(TileCoordsXYZ{ NextLoc }, goalPos, *this);
|
||||||
|
|
||||||
#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
PathfindLoggingDisable();
|
PathfindLoggingDisable();
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -19,13 +19,6 @@ struct Peep;
|
|||||||
struct Guest;
|
struct Guest;
|
||||||
struct TileElement;
|
struct TileElement;
|
||||||
|
|
||||||
// The tile position of the place the peep is trying to get to (park entrance/exit, ride
|
|
||||||
// entrance/exit, or the end of the queue line for a ride).
|
|
||||||
//
|
|
||||||
// This gets copied into Peep::PathfindGoal. The two separate variables are needed because
|
|
||||||
// when the goal changes the peep's pathfind history needs to be reset.
|
|
||||||
extern TileCoordsXYZ gPeepPathFindGoalPosition;
|
|
||||||
|
|
||||||
// When the heuristic pathfinder is examining neighboring tiles, one possibility is that it finds a
|
// When the heuristic pathfinder is examining neighboring tiles, one possibility is that it finds a
|
||||||
// queue tile; furthermore, this queue tile may or may not be for the ride that the peep is trying
|
// queue tile; furthermore, this queue tile may or may not be for the ride that the peep is trying
|
||||||
// to get to, if any. This first var is used to store the ride that the peep is currently headed to.
|
// to get to, if any. This first var is used to store the ride that the peep is currently headed to.
|
||||||
@@ -39,68 +32,31 @@ extern RideId gPeepPathFindQueueRideIndex;
|
|||||||
// In practice, if this is false, gPeepPathFindQueueRideIndex is always RIDE_ID_NULL.
|
// In practice, if this is false, gPeepPathFindQueueRideIndex is always RIDE_ID_NULL.
|
||||||
extern bool gPeepPathFindIgnoreForeignQueues;
|
extern bool gPeepPathFindIgnoreForeignQueues;
|
||||||
|
|
||||||
class GuestPathfinding
|
namespace OpenRCT2::PathFinding
|
||||||
{
|
{
|
||||||
public:
|
Direction ChooseDirection(const TileCoordsXYZ& loc, const TileCoordsXYZ& goal, Peep& peep);
|
||||||
virtual ~GuestPathfinding() = default;
|
|
||||||
|
|
||||||
/**
|
int32_t CalculateNextDestination(Guest& peep);
|
||||||
* Given a peep 'peep' at tile 'loc', who is trying to get to 'gPeepPathFindGoalPosition', decide the direction the peep
|
|
||||||
* should walk in from the current tile.
|
|
||||||
*
|
|
||||||
* @param loc Reference to the peep's current tile location
|
|
||||||
* @param peep Reference to the current peep struct
|
|
||||||
* @return The direction the peep should walk in
|
|
||||||
*/
|
|
||||||
virtual Direction ChooseDirection(const TileCoordsXYZ& loc, Peep& peep) = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test whether the given tile can be walked onto, if the peep is currently at height currentZ and
|
|
||||||
* moving in direction currentDirection
|
|
||||||
*
|
|
||||||
* @param tileElement A pointer to the tile that is being tested
|
|
||||||
* @param currentZ The height coord the peep is at
|
|
||||||
* @param currentDirection The direction the peep is facing in
|
|
||||||
* @return True if the given tile can be walked onto, false otherwise
|
|
||||||
*/
|
|
||||||
static bool IsValidPathZAndDirection(TileElement* tileElement, int32_t currentZ, int32_t currentDirection);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Overall guest pathfinding AI. Sets up Peep::DestinationX/DestinationY (which they move to in a
|
|
||||||
* straight line, no pathfinding). Called whenever the guest has arrived at their previously set destination.
|
|
||||||
*
|
|
||||||
* @param peep A reference to a guest struct
|
|
||||||
* @returns 0 if the guest has successfully had a new destination set up, nonzero otherwise.
|
|
||||||
*/
|
|
||||||
virtual int32_t CalculateNextDestination(Guest& peep) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class OriginalPathfinding final : public GuestPathfinding
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Direction ChooseDirection(const TileCoordsXYZ& loc, Peep& peep) final override;
|
|
||||||
|
|
||||||
int32_t CalculateNextDestination(Guest& peep) final override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
int32_t GuestPathFindParkEntranceEntering(Peep& peep, uint8_t edges);
|
int32_t GuestPathFindParkEntranceEntering(Peep& peep, uint8_t edges);
|
||||||
|
|
||||||
int32_t GuestPathFindPeepSpawn(Peep& peep, uint8_t edges);
|
int32_t GuestPathFindPeepSpawn(Peep& peep, uint8_t edges);
|
||||||
|
|
||||||
int32_t GuestPathFindParkEntranceLeaving(Peep& peep, uint8_t edges);
|
int32_t GuestPathFindParkEntranceLeaving(Peep& peep, uint8_t edges);
|
||||||
};
|
|
||||||
|
|
||||||
extern std::unique_ptr<GuestPathfinding> gGuestPathfinder;
|
bool IsValidPathZAndDirection(TileElement* tileElement, int32_t currentZ, int32_t currentDirection);
|
||||||
|
|
||||||
#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
# define PATHFIND_DEBUG \
|
# define PATHFIND_DEBUG \
|
||||||
0 // Set to 0 to disable pathfinding debugging;
|
0 // Set to 0 to disable pathfinding debugging;
|
||||||
// Set to 1 to enable pathfinding debugging.
|
// Set to 1 to enable pathfinding debugging.
|
||||||
|
|
||||||
// When PATHFIND_DEBUG is 1 (nonzero):
|
// When PATHFIND_DEBUG is 1 (nonzero):
|
||||||
// The following calls configure debug logging for the given peep
|
// 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 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"
|
// If they're staff, pathfinding will be logged if their name is "Mechanic Debug"
|
||||||
void PathfindLoggingEnable(Peep& peep);
|
void PathfindLoggingEnable(Peep& peep);
|
||||||
void PathfindLoggingDisable();
|
void PathfindLoggingDisable();
|
||||||
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1
|
||||||
|
|
||||||
|
}; // namespace OpenRCT2::PathFinding
|
||||||
|
|||||||
@@ -85,8 +85,7 @@ protected:
|
|||||||
|
|
||||||
// Pick the direction the peep should initially move in, given the goal position.
|
// Pick the direction the peep should initially move in, given the goal position.
|
||||||
// This will also store the goal position and initialize pathfinding data for the peep.
|
// This will also store the goal position and initialize pathfinding data for the peep.
|
||||||
gPeepPathFindGoalPosition = goal;
|
const Direction moveDir = PathFinding::ChooseDirection(*pos, goal, *peep);
|
||||||
const Direction moveDir = gGuestPathfinder->ChooseDirection(*pos, *peep);
|
|
||||||
if (moveDir == INVALID_DIRECTION)
|
if (moveDir == INVALID_DIRECTION)
|
||||||
{
|
{
|
||||||
// Couldn't determine a direction to move off in
|
// Couldn't determine a direction to move off in
|
||||||
|
|||||||
Reference in New Issue
Block a user