Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix off-by-one in path finding logic #7682

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/engine/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@
const auto it = explored.find(cur);
if (it == explored.end()) app_fatal("Failed to reconstruct path");
if (it->second.g == 0) break; // reached start
path[len++] = GetPathDirection(it->second.prev, cur);
cur = it->second.prev;
if (len == maxPathLength) {
// Path too long.
len = 0;
break;
}
path[len++] = GetPathDirection(it->second.prev, cur);
cur = it->second.prev;
}
std::reverse(path, path + len);
std::fill(path + len, path + maxPathLength, -1);
return len;

Check warning on line 173 in Source/engine/path.cpp

View workflow job for this annotation

GitHub Actions / build

'return': conversion from 'size_t' to 'int', possible loss of data
}

} // namespace
Expand Down
5 changes: 3 additions & 2 deletions test/path_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ std::vector<Dir> ToSyms(std::span<const int8_t> indices)

void CheckPath(Point startPosition, Point destinationPosition, std::vector<std::string> expectedSteps)
{
constexpr size_t MaxPathLength = 25;
// Restrict tests to the longest possible path length in vanilla Diablo
constexpr size_t MaxPathLength = 24;
int8_t pathSteps[MaxPathLength];
auto pathLength = FindPath(
/*canStep=*/[](Point, Point) { return true; },
Expand Down Expand Up @@ -153,7 +154,7 @@ TEST(PathTest, LongPaths)
// Starting from the middle of the world and trying to path to a border exceeds the maximum path size
CheckPath({ 56, 56 }, { 0, 0 }, {});

// Longest possible path is currently 24 steps meaning tiles 24 units away are reachable
// Longest possible path used to be 24 steps meaning tiles 24 units away are reachable
Point startingPosition { 56, 56 };
CheckPath(startingPosition, startingPosition + Displacement { 24, 24 }, std::vector<std::string>(24, "↘"));

Expand Down
Loading