From c2d9c7a6d44de59f48349a775a19d0a12f4fdc6d Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 09:50:39 +0100 Subject: [PATCH 01/10] Make path finding aware of ALL path elements when choosing a direction. This is applicable for overlaid paths placed with zero clearance. Previously the pathfinding only used the first path element. Notes: 1. Logic related to path slope still only uses the the first path element. Overlaying path elements with the same base height but different slopes may cause pathfinding glitches. 2. The heuristic search already uses a similar approach - the inconsistency could have caused pathfinding glitches. --- src/peep/peep.c | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 4f42005c54..80d8947549 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9462,21 +9462,50 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) // Get the path element at this location rct_map_element *dest_map_element = map_get_first_element_at(x / 32, y / 32); + /* Where there are multiple matching map elements placed with zero + * clearance, save the first one for later use to determine the path + * slope - this maintains the original behaviour (which only processes + * the first matching map element found) and is consistent with peep + * placement (i.e. height) on such paths with differing slopes. + * + * I cannot see a legitimate reason for building overlaid paths with + * differing slopes and do not recall ever seeing this in practise. + * Normal cases I have seen in practise are overlaid paths with the + * same slope (flat) in order to place scenery (e.g. benches) in the + * middle of a wide path that can still be walked through. + * Anyone attempting to overlay paths with different slopes should + * EXPECT to experience path finding irregularities due to those paths! + * In particular common edges at different heights will not work + * in a useful way. Simply do not do it! :-) */ + rct_map_element *first_map_element = NULL; + bool found = false; + uint8 permitted_edges = 0; + bool isThin = false; do { if (dest_map_element->base_height != z) continue; if (map_element_get_type(dest_map_element) != MAP_ELEMENT_TYPE_PATH) continue; found = true; - break; + if (first_map_element == NULL) { + first_map_element = dest_map_element; + } + + /* Check if this path element is a thin junction. + * Only 'thin' junctions are remembered in peep->pathfind_history. + * NO attempt is made to merge the overlaid path elements and + * check if the combination is 'thin'! + * The junction is considered 'thin' simply if any of the + * overlaid path elements there is a 'thin junction'. */ + isThin = isThin || path_is_thin_junction(dest_map_element, x, y, z); + + // Collect the permitted edges of ALL matching path elements at this location. + permitted_edges |= path_get_permitted_edges(dest_map_element); } while (!map_element_is_last_for_tile(dest_map_element++)); // Peep is not on a path. if (!found) return -1; - /* Determine if the path is a thin junction. - * Only 'thin' junctions are remembered in peep->pathfind_history. */ - bool isThin = path_is_thin_junction(dest_map_element, x, y, z); - - uint8 edges = 0xF; + permitted_edges &= 0xF; + uint8 edges = permitted_edges; if (isThin && peep->pathfind_goal.x == goal.x && peep->pathfind_goal.y == goal.y && peep->pathfind_goal.z == goal.z @@ -9544,8 +9573,8 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) edges &= ~(1 << test_edge); uint8 height = z; - if (footpath_element_is_sloped(dest_map_element) && - footpath_element_get_slope_direction(dest_map_element) == test_edge + if (footpath_element_is_sloped(first_map_element) && + footpath_element_get_slope_direction(first_map_element) == test_edge ) { height += 0x2; } @@ -9589,7 +9618,7 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) rct_xyz8 endJunctionList[16] = { 0 }; uint8 endDirectionList[16] = { 0 }; - peep_pathfind_heuristic_search(x, y, height, peep, dest_map_element, 0, &score, test_edge, &endJunctions, endJunctionList, endDirectionList, &endXYZ, &endSteps); + peep_pathfind_heuristic_search(x, y, height, peep, first_map_element, 0, &score, test_edge, &endJunctions, endJunctionList, endDirectionList, &endXYZ, &endSteps); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (gPathFindDebug) { log_verbose("Pathfind test edge: %d score: %d steps: %d end: %d,%d,%d junctions: %d", test_edge, score, endSteps, endXYZ.x, endXYZ.y, endXYZ.z, endJunctions); From 307ebc38be500e13494505d7d4620ae584c31059 Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 09:57:19 +0100 Subject: [PATCH 02/10] Fix initialisation of junctions added to pathfind_history. Fix initialisation of junctions added to pathfind_history. --- src/peep/peep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 80d8947549..22c2c620e9 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9715,7 +9715,7 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) peep->pathfind_history[i].x = x >> 5; peep->pathfind_history[i].y = y >> 5; peep->pathfind_history[i].z = z; - peep->pathfind_history[i].direction = 0xF; + peep->pathfind_history[i].direction = permitted_edges; peep->pathfind_history[i].direction &= ~(1 << chosen_edge); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (gPathFindDebug) { From afcf48080c5bfaa7256bfbbd6692cae8cc99c2fa Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:10:23 +0100 Subject: [PATCH 03/10] When choosing a direction, repair pathfind_history[i].direction. Remove directions that are not permitted by the path element(s) at that location. --- src/peep/peep.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 22c2c620e9..9bf5d9abd4 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9529,7 +9529,17 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) if (peep->pathfind_history[i].x == x / 32 && peep->pathfind_history[i].y == y / 32 && peep->pathfind_history[i].z == z) { - edges = peep->pathfind_history[i].direction & 0xF; + + /* Fix broken pathfind_history[i].direction + * which have untried directions that are not + * currently possible - could be due to pathing + * changes or in earlier code .directions was + * initialised to 0xF rather than the permitted + * edges. */ + peep->pathfind_history[i].direction &= permitted_edges; + + edges = peep->pathfind_history[i].direction; + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (gPathFindDebug) { log_verbose("Getting untried edges from pf_history for %d,%d,%d: %s,%s,%s,%s", x >> 5, y >> 5, z, (edges & 1) ? "0" : "-", (edges & 2) ? "1" : "-", (edges & 4) ? "2" : "-", (edges & 8) ? "3" : "-"); @@ -9540,8 +9550,6 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) } } - // Remove any edges that are not permitted - edges &= path_get_permitted_edges(dest_map_element); // Peep has tried all edges. if (edges == 0) return -1; From b83ab8d6e7a34d0e05644a534e96db5ea265f82c Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:15:33 +0100 Subject: [PATCH 04/10] Reset pathfind_history before the heuristic search function is called. Now that the heuristic search uses the pathfind_history for loop detection it is important that when the pathfind_goal is reset the pathfind_history gets reset before calling the heuristic search. --- src/peep/peep.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 9bf5d9abd4..19e4be157c 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9550,6 +9550,26 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) } } + /* If this is a new goal for the peep. Store it and reset the peep's + * pathfind_history. */ + if (peep->pathfind_goal.direction > 3 || + peep->pathfind_goal.x != goal.x || + peep->pathfind_goal.y != goal.y || + peep->pathfind_goal.z != goal.z + ) { + peep->pathfind_goal.x = goal.x; + peep->pathfind_goal.y = goal.y; + peep->pathfind_goal.z = goal.z; + peep->pathfind_goal.direction = 0; + + // Clear pathfinding history + memset(peep->pathfind_history, 0xFF, sizeof(peep->pathfind_history)); + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_verbose("New goal; clearing pf_history."); + } + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + } // Peep has tried all edges. if (edges == 0) return -1; @@ -9677,27 +9697,6 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 } - /* If this is a new goal for the peep. Store it and reset the peep's - * pathfind_history. */ - if (peep->pathfind_goal.direction > 3 || - peep->pathfind_goal.x != goal.x || - peep->pathfind_goal.y != goal.y || - peep->pathfind_goal.z != goal.z - ) { - peep->pathfind_goal.x = goal.x; - peep->pathfind_goal.y = goal.y; - peep->pathfind_goal.z = goal.z; - peep->pathfind_goal.direction = 0; - - // Clear pathfinding history - memset(peep->pathfind_history, 0xFF, sizeof(peep->pathfind_history)); - #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - if (gPathFindDebug) { - log_verbose("New goal; clearing pf_history."); - } - #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - } - if (isThin) { for (int i = 0; i < 4; ++i) { if (peep->pathfind_history[i].x == x >> 5 && From dd89a32016b08d9799b30327ed3751fb4250a0e8 Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:20:49 +0100 Subject: [PATCH 05/10] Reset pathfind_history[i].direction when all directions have been tried. When choosing a direction at a junction in pathfind_history (with no untried directions) the existing path finding falls back to aimless movement. This change makes the pathfinding try again. Useful for adjusting to path changes by the player and recovering from a stuck position in a savegame due to earlier bad pathfinding. --- src/peep/peep.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/peep/peep.c b/src/peep/peep.c index 19e4be157c..bd2b11ee2e 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9545,6 +9545,25 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) log_verbose("Getting untried edges from pf_history for %d,%d,%d: %s,%s,%s,%s", x >> 5, y >> 5, z, (edges & 1) ? "0" : "-", (edges & 2) ? "1" : "-", (edges & 4) ? "2" : "-", (edges & 8) ? "3" : "-"); } #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + + if (edges == 0) { + /* If peep has tried all edges, reset to + * all edges are untried. + * This permits the pathfinding to try + * again, which is good for getting + * unstuck when the player has edited + * the paths or the pathfinding itself + * has changed (been fixed) since + * the game was saved. */ + peep->pathfind_history[i].direction = permitted_edges; + edges = peep->pathfind_history[i].direction; + + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_verbose("All edges tried for %d,%d,%d - resetting to all untried", x >> 5, y >> 5, z); + } + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + } break; } } From f0cf7c2763eac47d2961aac6538554b412ca7dc2 Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:23:56 +0100 Subject: [PATCH 06/10] Fix loop detection in heuristic search based on pathfind_history. In the existing version, returning to a junction in the pathfind history is considered a loop. This is not useful as it prevents the pathfinding from backtracking, which is what the pathfind_history is intended to allow. Updated to consider it a loop only when returning to a junction with no more edges left to try. --- src/peep/peep.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index bd2b11ee2e..0637e0cd13 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9297,7 +9297,17 @@ static void peep_pathfind_heuristic_search(sint16 x, sint16 y, uint8 z, rct_peep if (peep->pathfind_history[i].x == x >> 5 && peep->pathfind_history[i].y == y >> 5 && peep->pathfind_history[i].z == z) { - pathLoop = true; + if (peep->pathfind_history[i].direction == 0) { + /* If all directions have already been tried while + * heading to this goal, this is a loop. */ + pathLoop = true; + } + else { + /* The peep remembers walking through this junction + * before, but has not yet tried all directions. + * Limit the edges to search to those not yet tried. */ + edges &= peep->pathfind_history[i].direction; + } break; } } From 2f4c5c4a06211afbfee9e4b17625aae3c773e6a1 Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:49:15 +0100 Subject: [PATCH 07/10] Reset pathfind_goal when choose direction fails. Currently pathfinding falls back to aimless movement in this case. Resetting the pathfind_goal (which in turn will cause the pathfind_history to be reset) makes the pathfinding try again afresh the next time around. Potentially useful for adjusting to path changes by the player and recovering from a stuck position due to earlier bad pathfinding. --- src/peep/peep.c | 12 ++++++++++-- src/peep/peep.h | 1 + src/peep/staff.c | 7 +++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index 0637e0cd13..aabc309143 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -143,7 +143,7 @@ static void peep_ride_is_too_intense(rct_peep *peep, int rideIndex, bool peepAtR static void peep_chose_not_to_go_on_ride(rct_peep *peep, int rideIndex, bool peepAtRide, bool updateLastRide); static void peep_tried_to_enter_full_queue(rct_peep *peep, int rideIndex); static bool peep_should_go_to_shop(rct_peep *peep, int rideIndex, bool peepAtShop); -static void peep_reset_pathfind_goal(rct_peep *peep); +void peep_reset_pathfind_goal(rct_peep *peep); static bool peep_find_ride_to_look_at(rct_peep *peep, uint8 edge, uint8 *rideToView, uint8 *rideSeatToView); static void peep_easter_egg_peep_interactions(rct_peep *peep); static int peep_get_height_on_slope(rct_peep *peep, int x, int y); @@ -10248,6 +10248,14 @@ static int guest_path_finding(rct_peep* peep) #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (direction == -1){ + /* Heuristic search failed for all directions. + * Reset the pathfind_goal - this means that the pathfind_history + * will be reset in the next call to peep_pathfind_choose_direction(). + * This lets the heuristic search "try again" in case the player has + * edited the path layout or the mechanic was already stuck in the + * save game (e.g. with a worse version of the pathfinding). */ + peep_reset_pathfind_goal(peep); + return guest_path_find_aimless(peep, edges); } return peep_move_one_tile(direction, peep); @@ -11005,7 +11013,7 @@ static bool peep_should_use_cash_machine(rct_peep *peep, int rideIndex) * * rct2: 0x0069A98C */ -static void peep_reset_pathfind_goal(rct_peep *peep) +void peep_reset_pathfind_goal(rct_peep *peep) { peep->pathfind_goal.x = 0xFF; peep->pathfind_goal.y = 0xFF; diff --git a/src/peep/peep.h b/src/peep/peep.h index bb86b94cb1..f7a30de629 100644 --- a/src/peep/peep.h +++ b/src/peep/peep.h @@ -685,5 +685,6 @@ money32 set_peep_name(int flags, int state, uint16 sprite_index, uint8* text_1, void game_command_set_guest_name(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep); +void peep_reset_pathfind_goal(rct_peep *peep); #endif diff --git a/src/peep/staff.c b/src/peep/staff.c index d8eac92e62..0ba60a2296 100644 --- a/src/peep/staff.c +++ b/src/peep/staff.c @@ -1129,6 +1129,13 @@ static uint8 staff_mechanic_direction_path(rct_peep* peep, uint8 validDirections #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (pathfindDirection == -1) { + /* Heuristic search failed for all directions. + * Reset the pathfind_goal - this means that the pathfind_history + * will be reset in the next call to peep_pathfind_choose_direction(). + * This lets the heuristic search "try again" in case the player has + * edited the path layout or the mechanic was already stuck in the + * save game (e.g. with a worse version of the pathfinding). */ + peep_reset_pathfind_goal(peep); return staff_mechanic_direction_path_rand(peep, pathDirections); } From 5ff6199ba13d9d852e64174c8436bca456d14506 Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 10:55:40 +0100 Subject: [PATCH 08/10] Changes/additions to the pathfinding logging messages. Refactored some code into functions for enabling/disabling pathfinding debugging for a specific peep. Some new log messages for improved clarity when debugging pathfinding. Added a #define to independently enable/disable pathfinding debugging to be friendly to others using debugging builds. --- src/peep/peep.c | 122 +++++++++++++++++++++++++++++++++++++---------- src/peep/peep.h | 19 +++++--- src/peep/staff.c | 12 ++--- 3 files changed, 114 insertions(+), 39 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index aabc309143..2c7c549927 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -9675,6 +9675,12 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) rct_xyz8 endJunctionList[16] = { 0 }; uint8 endDirectionList[16] = { 0 }; + #if defined(DEBUG_LEVEL_2) && DEBUG_LEVEL_2 + if (gPathFindDebug) { + log_verbose("Pathfind searching in direction: %d from %d,%d,%d", test_edge, x >> 5, y >> 5, z); + } + #endif // defined(DEBUG_LEVEL_2) && DEBUG_LEVEL_2 + peep_pathfind_heuristic_search(x, y, height, peep, first_map_element, 0, &score, test_edge, &endJunctions, endJunctionList, endDirectionList, &endXYZ, &endSteps); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (gPathFindDebug) { @@ -9755,7 +9761,7 @@ int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep) peep->pathfind_history[i].direction &= ~(1 << chosen_edge); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (gPathFindDebug) { - log_verbose("Storing new pf_history for %d,%d,%d without edge %d.", x >> 5, y >> 5, z, chosen_edge); + log_verbose("Storing new pf_history (in index: %d) for %d,%d,%d without edge %d.", i, x >> 5, y >> 5, z, chosen_edge); } #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 } @@ -9882,18 +9888,13 @@ static int guest_path_find_park_entrance(rct_peep* peep, rct_map_element *map_el gPeepPathFindQueueRideIndex = 255; #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - /* Determine if the pathfinding debugging is wanted for this peep. */ - /* For guests, use the existing PEEP_FLAGS_TRACKING flag to - * determine for which guest(s) the pathfinding debugging will - * be output for. */ - format_string(gPathFindDebugPeepName, sizeof(gPathFindDebugPeepName), peep->name_string_idx, &(peep->id)); - gPathFindDebug = peep->peep_flags & PEEP_FLAGS_TRACKING; + pathfind_logging_enable(peep); #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 int chosenDirection = peep_pathfind_choose_direction(peep->next_x, peep->next_y, peep->next_z, peep); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - gPathFindDebug = false; + pathfind_logging_disable(); #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (chosenDirection == -1) @@ -10033,6 +10034,13 @@ static int guest_path_finding(rct_peep* peep) { sint16 x, y, z; + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + pathfind_logging_enable(peep); + if (gPathFindDebug) { + log_info("Starting guest_path_finding for %s", gPathFindDebugPeepName); + } + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (peep->next_var_29 & 0x18) { return guest_surface_path_finding(peep); } @@ -10088,6 +10096,12 @@ static int guest_path_finding(rct_peep* peep) direction = bitscanforward(edges); // IF only one edge to choose from if ((edges & ~(1 << direction)) == 0) { + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - taking only direction available: %d.", gPathFindDebugPeepName, direction); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return peep_move_one_tile(direction, peep); } @@ -10096,6 +10110,12 @@ static int guest_path_finding(rct_peep* peep) // Peep is outside the park. // loc_694F19: if (peep->outside_of_park != 0){ + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - peep is outside the park.", gPathFindDebugPeepName); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 switch (peep->state) { case PEEP_STATE_ENTERING_PARK: return guest_path_find_entering_park(peep, mapElement, edges); @@ -10149,18 +10169,39 @@ static int guest_path_finding(rct_peep* peep) } } - if (peep->peep_flags & PEEP_FLAGS_LEAVING_PARK) + if (peep->peep_flags & PEEP_FLAGS_LEAVING_PARK) { + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - peep is leaving the park.", gPathFindDebugPeepName); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return guest_path_find_park_entrance(peep, mapElement, edges); + } - if (peep->guest_heading_to_ride_id == 0xFF) + if (peep->guest_heading_to_ride_id == 0xFF) { + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - peep is aimless.", gPathFindDebugPeepName); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return guest_path_find_aimless(peep, edges); + } // Peep is heading for a ride. uint8 rideIndex = peep->guest_heading_to_ride_id; rct_ride* ride = get_ride(rideIndex); - if (ride->status != RIDE_STATUS_OPEN) + if (ride->status != RIDE_STATUS_OPEN) { + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - peep is heading to closed ride == aimless.", gPathFindDebugPeepName); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return guest_path_find_aimless(peep, edges); + } // The ride is open. gPeepPathFindQueueRideIndex = rideIndex; @@ -10232,21 +10273,8 @@ static int guest_path_finding(rct_peep* peep) gPeepPathFindGoalPosition = (rct_xyz16) { x, y, z }; gPeepPathFindIgnoreForeignQueues = true; - #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - /* Determine if the pathfinding debugging is wanted for this peep. */ - /* For guests, use the existing PEEP_FLAGS_TRACKING flag to - * determine for which guest(s) the pathfinding debugging will - * be output for. */ - format_string(gPathFindDebugPeepName, sizeof(gPathFindDebugPeepName), peep->name_string_idx, &(peep->id)); - gPathFindDebug = peep->peep_flags & PEEP_FLAGS_TRACKING; - #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - direction = peep_pathfind_choose_direction(peep->next_x, peep->next_y, peep->next_z, peep); - #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - gPathFindDebug = false; - #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - if (direction == -1){ /* Heuristic search failed for all directions. * Reset the pathfind_goal - this means that the pathfind_history @@ -10256,8 +10284,21 @@ static int guest_path_finding(rct_peep* peep) * save game (e.g. with a worse version of the pathfinding). */ peep_reset_pathfind_goal(peep); + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - failed to choose a direction == aimless.", gPathFindDebugPeepName); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + return guest_path_find_aimless(peep, edges); } + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Completed guest_path_finding for %s - direction chosen: %d.", gPathFindDebugPeepName, direction); + } + pathfind_logging_disable(); + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 return peep_move_one_tile(direction, peep); } @@ -11015,6 +11056,13 @@ static bool peep_should_use_cash_machine(rct_peep *peep, int rideIndex) */ void peep_reset_pathfind_goal(rct_peep *peep) { + + #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + if (gPathFindDebug) { + log_info("Resetting pathfind_goal for %s", gPathFindDebugPeepName); + } + #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + peep->pathfind_goal.x = 0xFF; peep->pathfind_goal.y = 0xFF; peep->pathfind_goal.z = 0xFF; @@ -12573,3 +12621,29 @@ void game_command_set_guest_name(int *eax, int *ebx, int *ecx, int *edx, int *es (uint8*)edi ); } + +void pathfind_logging_enable(rct_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->peep_flags & 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 +} diff --git a/src/peep/peep.h b/src/peep/peep.h index f7a30de629..17a3daea98 100644 --- a/src/peep/peep.h +++ b/src/peep/peep.h @@ -20,12 +20,6 @@ #include "../common.h" #include "../world/map.h" -#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 -// Some variables used for the path finding debugging. -extern bool gPathFindDebug; -extern utf8 gPathFindDebugPeepName[256]; -#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - #define PEEP_MAX_THOUGHTS 5 #define PEEP_HUNGER_WARNING_THRESHOLD 25 @@ -687,4 +681,17 @@ void game_command_set_guest_name(int *eax, int *ebx, int *ecx, int *edx, int *es int peep_pathfind_choose_direction(sint16 x, sint16 y, uint8 z, rct_peep *peep); void peep_reset_pathfind_goal(rct_peep *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 (non zero) +void pathfind_logging_enable(rct_peep* peep); +void pathfind_logging_disable(); +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 + #endif diff --git a/src/peep/staff.c b/src/peep/staff.c index 0ba60a2296..1ee228216f 100644 --- a/src/peep/staff.c +++ b/src/peep/staff.c @@ -1073,7 +1073,7 @@ static uint8 staff_mechanic_direction_path(rct_peep* peep, uint8 validDirections gPeepPathFindGoalPosition.z = z; /* Find location of the exit for the target ride station - * or if the ride has no exit, the entrance */ + * or if the ride has no exit, the entrance. */ uint16 location = ride->exits[peep->current_ride_station]; if (location == 0xFFFF) { location = ride->entrances[peep->current_ride_station]; @@ -1113,19 +1113,13 @@ static uint8 staff_mechanic_direction_path(rct_peep* peep, uint8 validDirections gPeepPathFindQueueRideIndex = 255; #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - /* Determine if the pathfinding debugging is wanted for this peep. */ - /* 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. */ - format_string(gPathFindDebugPeepName, sizeof(gPathFindDebugPeepName), peep->name_string_idx, &(peep->id)); - - gPathFindDebug = strcmp(gPathFindDebugPeepName, "Mechanic Debug") == 0; + pathfind_logging_enable(peep); #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 int pathfindDirection = peep_pathfind_choose_direction(peep->next_x, peep->next_y, peep->next_z, peep); #if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 - gPathFindDebug = false; + pathfind_logging_disable(); #endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 if (pathfindDirection == -1) { From 11174af00bda88a6a80f56786db60b7aae65424d Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 11:00:57 +0100 Subject: [PATCH 09/10] Increment network version for pathfinding fixes. --- src/network/network.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/network.h b/src/network/network.h index 1e5bcfcbae..eec83f4f08 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -55,7 +55,7 @@ extern "C" { // This define specifies which version of network stream current build uses. // It is used for making sure only compatible builds get connected, even within // single OpenRCT2 version. -#define NETWORK_STREAM_VERSION "19" +#define NETWORK_STREAM_VERSION "20" #define NETWORK_STREAM_ID OPENRCT2_VERSION "-" NETWORK_STREAM_VERSION #ifdef __cplusplus From 1df30e55d297dc26fd38b476a188218c262b3e7e Mon Sep 17 00:00:00 2001 From: zaxcav Date: Fri, 2 Dec 2016 13:18:20 +0100 Subject: [PATCH 10/10] Fix CI errors Missing #if on some debug code. --- src/peep/peep.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/peep/peep.c b/src/peep/peep.c index 2c7c549927..42a7aa6d9e 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -12622,6 +12622,7 @@ void game_command_set_guest_name(int *eax, int *ebx, int *ecx, int *edx, int *es ); } +#if defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1 void pathfind_logging_enable(rct_peep* peep) { #if defined(PATHFIND_DEBUG) && PATHFIND_DEBUG /* Determine if the pathfinding debugging is wanted for this peep. */ @@ -12647,3 +12648,4 @@ void pathfind_logging_disable() { gPathFindDebug = false; #endif // defined(PATHFIND_DEBUG) && PATHFIND_DEBUG } +#endif // defined(DEBUG_LEVEL_1) && DEBUG_LEVEL_1