Receive TileCoordsXYZ by const ref instead of copy

This commit is contained in:
Tulio Leao 2020-02-29 00:43:01 -03:00
parent 181eaf768d
commit b9186f4b0b
3 changed files with 60 additions and 52 deletions

View File

@ -219,20 +219,21 @@ static int32_t guest_surface_path_finding(Peep* peep)
* Returns the type of the next footpath tile a peep can get to from x,y,z /
* inputTileElement in the given direction.
*/
static uint8_t footpath_element_next_in_direction(TileCoordsXYZ loc, PathElement* pathElement, Direction chosenDirection)
static uint8_t footpath_element_next_in_direction(const TileCoordsXYZ& loc, PathElement* pathElement, Direction chosenDirection)
{
TileElement* nextTileElement;
auto adjustedLoc = loc;
if (pathElement->IsSloped())
{
if (pathElement->GetSlopeDirection() == chosenDirection)
{
loc.z += 2;
adjustedLoc.z += 2;
}
}
loc += TileDirectionDelta[chosenDirection];
nextTileElement = map_get_first_element_at(loc.ToCoordsXY());
adjustedLoc += TileDirectionDelta[chosenDirection];
nextTileElement = map_get_first_element_at(adjustedLoc.ToCoordsXY());
do
{
if (nextTileElement == nullptr)
@ -241,7 +242,7 @@ static uint8_t footpath_element_next_in_direction(TileCoordsXYZ loc, PathElement
continue;
if (nextTileElement->GetType() != TILE_ELEMENT_TYPE_PATH)
continue;
if (!is_valid_path_z_and_direction(nextTileElement, loc.z, chosenDirection))
if (!is_valid_path_z_and_direction(nextTileElement, adjustedLoc.z, chosenDirection))
continue;
if (nextTileElement->AsPath()->IsWide())
return PATH_SEARCH_WIDE;
@ -274,16 +275,18 @@ static uint8_t footpath_element_next_in_direction(TileCoordsXYZ loc, PathElement
* This is the recursive portion of footpath_element_destination_in_direction().
*/
static uint8_t footpath_element_dest_in_dir(
TileCoordsXYZ loc, Direction chosenDirection, ride_id_t* outRideIndex, int32_t level)
const TileCoordsXYZ& loc, Direction chosenDirection, ride_id_t* outRideIndex, int32_t level)
{
TileElement* tileElement;
Direction direction;
auto adjustedLoc = loc;
if (level > 25)
return PATH_SEARCH_LIMIT_REACHED;
loc += TileDirectionDelta[chosenDirection];
tileElement = map_get_first_element_at(loc.ToCoordsXY());
adjustedLoc += TileDirectionDelta[chosenDirection];
tileElement = map_get_first_element_at(adjustedLoc.ToCoordsXY());
if (tileElement == nullptr)
{
return PATH_SEARCH_FAILED;
@ -297,7 +300,7 @@ static uint8_t footpath_element_dest_in_dir(
{
case TILE_ELEMENT_TYPE_TRACK:
{
if (loc.z != tileElement->base_height)
if (adjustedLoc.z != tileElement->base_height)
continue;
ride_id_t rideIndex = tileElement->AsTrack()->GetRideIndex();
auto ride = get_ride(rideIndex);
@ -309,7 +312,7 @@ static uint8_t footpath_element_dest_in_dir(
}
break;
case TILE_ELEMENT_TYPE_ENTRANCE:
if (loc.z != tileElement->base_height)
if (adjustedLoc.z != tileElement->base_height)
continue;
switch (tileElement->AsEntrance()->GetEntranceType())
{
@ -334,14 +337,14 @@ static uint8_t footpath_element_dest_in_dir(
}
break;
case TILE_ELEMENT_TYPE_PATH:
if (!is_valid_path_z_and_direction(tileElement, loc.z, chosenDirection))
if (!is_valid_path_z_and_direction(tileElement, adjustedLoc.z, chosenDirection))
continue;
if (tileElement->AsPath()->IsWide())
return PATH_SEARCH_WIDE;
uint8_t edges = path_get_permitted_edges(tileElement->AsPath());
edges &= ~(1 << direction_reverse(chosenDirection));
loc.z = tileElement->base_height;
adjustedLoc.z = tileElement->base_height;
for (Direction dir : ALL_DIRECTIONS)
{
@ -356,10 +359,10 @@ static uint8_t footpath_element_dest_in_dir(
{
if (tileElement->AsPath()->GetSlopeDirection() == dir)
{
loc.z += 2;
adjustedLoc.z += 2;
}
}
return footpath_element_dest_in_dir(loc, dir, outRideIndex, level + 1);
return footpath_element_dest_in_dir(adjustedLoc, dir, outRideIndex, level + 1);
}
return PATH_SEARCH_DEAD_END;
}
@ -392,17 +395,18 @@ static uint8_t footpath_element_dest_in_dir(
* width path, for example that leads from a ride exit back to the main path.
*/
static uint8_t footpath_element_destination_in_direction(
TileCoordsXYZ loc, PathElement* pathElement, Direction chosenDirection, ride_id_t* outRideIndex)
const TileCoordsXYZ& loc, PathElement* pathElement, Direction chosenDirection, ride_id_t* outRideIndex)
{
auto adjustedLoc = loc;
if (pathElement->IsSloped())
{
if (pathElement->GetSlopeDirection() == chosenDirection)
{
loc.z += 2;
adjustedLoc.z += 2;
}
}
return footpath_element_dest_in_dir(loc, chosenDirection, outRideIndex, 0);
return footpath_element_dest_in_dir(adjustedLoc, chosenDirection, outRideIndex, 0);
}
/**
@ -471,7 +475,7 @@ static uint8_t peep_pathfind_get_max_number_junctions(Peep* peep)
* since entrances and ride queues coming off a path should not result in
* the path being considered a junction.
*/
static bool path_is_thin_junction(PathElement* path, TileCoordsXYZ loc)
static bool path_is_thin_junction(PathElement* path, const TileCoordsXYZ& loc)
{
uint8_t edges = path->GetEdges();
@ -503,7 +507,7 @@ static bool path_is_thin_junction(PathElement* path, TileCoordsXYZ loc)
return thin_junction;
}
static int32_t CalculateHeuristicPathingScore(TileCoordsXYZ loc1, TileCoordsXYZ loc2)
static int32_t CalculateHeuristicPathingScore(const TileCoordsXYZ& loc1, const TileCoordsXYZ& loc2)
{
auto xDelta = abs(loc1.x - loc2.x) * 32;
auto yDelta = abs(loc1.y - loc2.y) * 32;
@ -594,17 +598,19 @@ static int32_t CalculateHeuristicPathingScore(TileCoordsXYZ loc1, TileCoordsXYZ
* rct2: 0x0069A997
*/
static void peep_pathfind_heuristic_search(
TileCoordsXYZ loc, Peep* peep, TileElement* currentTileElement, bool inPatrolArea, uint8_t counter, uint16_t* endScore,
Direction test_edge, uint8_t* endJunctions, TileCoordsXYZ junctionList[16], uint8_t directionList[16],
const TileCoordsXYZ& loc, Peep* peep, TileElement* currentTileElement, bool inPatrolArea, uint8_t counter,
uint16_t* endScore, Direction test_edge, uint8_t* endJunctions, TileCoordsXYZ junctionList[16], uint8_t directionList[16],
TileCoordsXYZ* endXYZ, uint8_t* endSteps)
{
uint8_t searchResult = PATH_SEARCH_FAILED;
auto adjustedLoc = loc;
bool currentElementIsWide
= (currentTileElement->AsPath()->IsWide()
&& !staff_can_ignore_wide_flag(peep, loc.x * 32, loc.y * 32, loc.z, currentTileElement));
&& !staff_can_ignore_wide_flag(peep, adjustedLoc.x * 32, adjustedLoc.y * 32, adjustedLoc.z, currentTileElement));
loc += TileDirectionDelta[test_edge];
adjustedLoc += TileDirectionDelta[test_edge];
++counter;
_peepPathFindTilesChecked--;
@ -612,8 +618,9 @@ static void peep_pathfind_heuristic_search(
/* If this is where the search started this is a search loop and the
* current search path ends here.
* Return without updating the parameters (best result so far). */
if ((_peepPathFindHistory[0].location.x == (uint8_t)loc.x) && (_peepPathFindHistory[0].location.y == (uint8_t)loc.y)
&& (_peepPathFindHistory[0].location.z == loc.z))
if ((_peepPathFindHistory[0].location.x == (uint8_t)adjustedLoc.x)
&& (_peepPathFindHistory[0].location.y == (uint8_t)adjustedLoc.y)
&& (_peepPathFindHistory[0].location.z == adjustedLoc.z))
{
#if defined(DEBUG_LEVEL_2) && DEBUG_LEVEL_2
if (gPathFindDebug)
@ -627,7 +634,7 @@ static void peep_pathfind_heuristic_search(
bool nextInPatrolArea = inPatrolArea;
if (peep->type == PEEP_TYPE_STAFF && peep->staff_type == STAFF_TYPE_MECHANIC)
{
nextInPatrolArea = staff_is_location_in_patrol(peep, loc.x * 32, loc.y * 32);
nextInPatrolArea = staff_is_location_in_patrol(peep, adjustedLoc.x * 32, adjustedLoc.y * 32);
if (inPatrolArea && !nextInPatrolArea)
{
/* The mechanic will leave his patrol area by taking
@ -645,7 +652,7 @@ static void peep_pathfind_heuristic_search(
/* Get the next map element of interest in the direction of test_edge. */
bool found = false;
TileElement* tileElement = map_get_first_element_at(loc.ToCoordsXY());
TileElement* tileElement = map_get_first_element_at(adjustedLoc.ToCoordsXY());
if (tileElement == nullptr)
{
return;
@ -663,7 +670,7 @@ static void peep_pathfind_heuristic_search(
{
case TILE_ELEMENT_TYPE_TRACK:
{
if (loc.z != tileElement->base_height)
if (adjustedLoc.z != tileElement->base_height)
continue;
/* For peeps heading for a shop, the goal is the shop
* tile. */
@ -677,7 +684,7 @@ static void peep_pathfind_heuristic_search(
break;
}
case TILE_ELEMENT_TYPE_ENTRANCE:
if (loc.z != tileElement->base_height)
if (adjustedLoc.z != tileElement->base_height)
continue;
Direction direction;
searchResult = PATH_SEARCH_OTHER;
@ -727,16 +734,16 @@ static void peep_pathfind_heuristic_search(
* queue path.
* Otherwise, peeps walk on path tiles to get to the goal. */
if (!is_valid_path_z_and_direction(tileElement, loc.z, test_edge))
if (!is_valid_path_z_and_direction(tileElement, adjustedLoc.z, test_edge))
continue;
// Path may be sloped, so set z to path base height.
loc.z = tileElement->base_height;
adjustedLoc.z = tileElement->base_height;
if (tileElement->AsPath()->IsWide())
{
/* Check if staff can ignore this wide flag. */
if (!staff_can_ignore_wide_flag(peep, loc.x * 32, loc.y * 32, loc.z, tileElement))
if (!staff_can_ignore_wide_flag(peep, adjustedLoc.x * 32, adjustedLoc.y * 32, adjustedLoc.z, tileElement))
{
searchResult = PATH_SEARCH_WIDE;
found = true;
@ -796,7 +803,7 @@ static void peep_pathfind_heuristic_search(
* Ignore for now. */
// Calculate the heuristic score of this map element.
uint16_t new_score = CalculateHeuristicPathingScore(loc, gPeepPathFindGoalPosition);
uint16_t new_score = CalculateHeuristicPathingScore(adjustedLoc, gPeepPathFindGoalPosition);
/* If this map element is the search goal the current search path ends here. */
if (new_score == 0)
@ -809,7 +816,7 @@ static void peep_pathfind_heuristic_search(
*endScore = new_score;
*endSteps = counter;
// Update the end x,y,z
*endXYZ = loc;
*endXYZ = adjustedLoc;
// Update the telemetry
*endJunctions = _peepPathFindMaxJunctions - _peepPathFindNumJunctions;
for (uint8_t junctInd = 0; junctInd < *endJunctions; junctInd++)
@ -867,7 +874,7 @@ static void peep_pathfind_heuristic_search(
*endScore = new_score;
*endSteps = counter;
// Update the end x,y,z
*endXYZ = loc;
*endXYZ = adjustedLoc;
// Update the telemetry
*endJunctions = _peepPathFindMaxJunctions - _peepPathFindNumJunctions;
for (uint8_t junctInd = 0; junctInd < *endJunctions; junctInd++)
@ -935,7 +942,7 @@ static void peep_pathfind_heuristic_search(
*endScore = new_score;
*endSteps = counter;
// Update the end x,y,z
*endXYZ = loc;
*endXYZ = adjustedLoc;
// Update the telemetry
*endJunctions = _peepPathFindMaxJunctions - _peepPathFindNumJunctions;
for (uint8_t junctInd = 0; junctInd < *endJunctions; junctInd++)
@ -963,7 +970,7 @@ static void peep_pathfind_heuristic_search(
{
/* Check if this is a thin junction. And perform additional
* necessary checks. */
thin_junction = path_is_thin_junction(tileElement->AsPath(), loc);
thin_junction = path_is_thin_junction(tileElement->AsPath(), adjustedLoc);
if (thin_junction)
{
@ -983,7 +990,8 @@ static void peep_pathfind_heuristic_search(
* already been visited by the peep while heading for this goal. */
for (auto& pathfindHistory : peep->pathfind_history)
{
if (pathfindHistory.x == loc.x && pathfindHistory.y == loc.y && pathfindHistory.z == loc.z)
if (pathfindHistory.x == adjustedLoc.x && pathfindHistory.y == adjustedLoc.y
&& pathfindHistory.z == adjustedLoc.z)
{
if (pathfindHistory.direction == 0)
{
@ -1010,9 +1018,9 @@ static void peep_pathfind_heuristic_search(
for (int32_t junctionNum = _peepPathFindNumJunctions + 1; junctionNum <= _peepPathFindMaxJunctions;
junctionNum++)
{
if ((_peepPathFindHistory[junctionNum].location.x == (uint8_t)loc.x)
&& (_peepPathFindHistory[junctionNum].location.y == (uint8_t)loc.y)
&& (_peepPathFindHistory[junctionNum].location.z == loc.z))
if ((_peepPathFindHistory[junctionNum].location.x == (uint8_t)adjustedLoc.x)
&& (_peepPathFindHistory[junctionNum].location.y == (uint8_t)adjustedLoc.y)
&& (_peepPathFindHistory[junctionNum].location.z == adjustedLoc.z))
{
pathLoop = true;
break;
@ -1045,7 +1053,7 @@ static void peep_pathfind_heuristic_search(
*endScore = new_score;
*endSteps = counter;
// Update the end x,y,z
*endXYZ = loc;
*endXYZ = adjustedLoc;
// Update the telemetry
*endJunctions = _peepPathFindMaxJunctions; // - _peepPathFindNumJunctions;
for (uint8_t junctInd = 0; junctInd < *endJunctions; junctInd++)
@ -1070,9 +1078,9 @@ static void peep_pathfind_heuristic_search(
/* This junction was NOT previously visited in the current
* search path, so add the junction to the history. */
_peepPathFindHistory[_peepPathFindNumJunctions].location.x = (uint8_t)loc.x;
_peepPathFindHistory[_peepPathFindNumJunctions].location.y = (uint8_t)loc.y;
_peepPathFindHistory[_peepPathFindNumJunctions].location.z = loc.z;
_peepPathFindHistory[_peepPathFindNumJunctions].location.x = (uint8_t)adjustedLoc.x;
_peepPathFindHistory[_peepPathFindNumJunctions].location.y = (uint8_t)adjustedLoc.y;
_peepPathFindHistory[_peepPathFindNumJunctions].location.z = adjustedLoc.z;
// .direction take is added below.
_peepPathFindNumJunctions--;
@ -1086,7 +1094,7 @@ static void peep_pathfind_heuristic_search(
edges &= ~(1 << next_test_edge);
uint8_t savedNumJunctions = _peepPathFindNumJunctions;
uint8_t height = loc.z;
uint8_t height = adjustedLoc.z;
if (tileElement->AsPath()->IsSloped() && tileElement->AsPath()->GetSlopeDirection() == next_test_edge)
{
height += 2;
@ -1117,8 +1125,8 @@ static void peep_pathfind_heuristic_search(
}
peep_pathfind_heuristic_search(
{ loc.x, loc.y, height }, peep, tileElement, nextInPatrolArea, counter, endScore, next_test_edge, endJunctions,
junctionList, directionList, endXYZ, endSteps);
{ adjustedLoc.x, adjustedLoc.y, height }, peep, tileElement, nextInPatrolArea, counter, endScore,
next_test_edge, endJunctions, junctionList, directionList, endXYZ, endSteps);
_peepPathFindNumJunctions = savedNumJunctions;
#if defined(DEBUG_LEVEL_2) && DEBUG_LEVEL_2
@ -1161,7 +1169,7 @@ static void peep_pathfind_heuristic_search(
*
* rct2: 0x0069A5F0
*/
Direction peep_pathfind_choose_direction(TileCoordsXYZ loc, Peep* peep)
Direction peep_pathfind_choose_direction(const TileCoordsXYZ& loc, Peep* peep)
{
// The max number of thin junctions searched - a per-search-path limit.
_peepPathFindMaxJunctions = peep_pathfind_get_max_number_junctions(peep);

View File

@ -757,7 +757,7 @@ void Peep::PickupAbort(int32_t old_x)
}
// Returns true when a peep can be dropped at the given location. When apply is set to true the peep gets dropped.
bool Peep::Place(TileCoordsXYZ location, bool apply)
bool Peep::Place(const TileCoordsXYZ& location, bool apply)
{
auto* pathElement = map_get_path_element_at(location);
TileElement* tileElement = reinterpret_cast<TileElement*>(pathElement);

View File

@ -715,7 +715,7 @@ public: // Peep
void SetNextFlags(uint8_t next_direction, bool is_sloped, bool is_surface);
void Pickup();
void PickupAbort(int32_t old_x);
bool Place(TileCoordsXYZ location, bool apply);
bool Place(const TileCoordsXYZ& location, bool apply);
static Peep* Generate(const CoordsXYZ& coords);
void RemoveFromQueue();
void RemoveFromRide();
@ -985,7 +985,7 @@ void peep_update_names(bool realNames);
void guest_set_name(uint16_t spriteIndex, const char* name);
Direction peep_pathfind_choose_direction(TileCoordsXYZ loc, Peep* peep);
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);