Skip to content
Merged
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
52 changes: 40 additions & 12 deletions pathfinding/core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def __init__(self, width=0, height=0, matrix=None, inverse=False):
"""
self.width = width
self.height = height
self.passable_left_right_border = False
self.passable_up_down_border = False
if isinstance(matrix, (tuple, list)) or (
USE_NUMPY and isinstance(matrix, np.ndarray) and
matrix.size > 0):
Expand All @@ -49,6 +51,12 @@ def __init__(self, width=0, height=0, matrix=None, inverse=False):
else:
self.nodes = [[]]

def set_passable_left_right_border(self):
self.passable_left_right_border = True

def set_passable_up_down_border(self):
self.passable_up_down_border = True

def node(self, x, y):
"""
get node at position
Expand Down Expand Up @@ -84,21 +92,41 @@ def neighbors(self, node, diagonal_movement=DiagonalMovement.never):
s0 = d0 = s1 = d1 = s2 = d2 = s3 = d3 = False

# ↑
if self.walkable(x, y - 1):
neighbors.append(self.nodes[y - 1][x])
s0 = True
if y == 0 and self.passable_up_down_border:
if self.walkable(x, self.height - 1):
neighbors.append(self.nodes[self.height - 1][x])
s0 = True
else:
if self.walkable(x, y - 1):
neighbors.append(self.nodes[y - 1][x])
s0 = True
# →
if self.walkable(x + 1, y):
neighbors.append(self.nodes[y][x + 1])
s1 = True
if x == self.width - 1 and self.passable_left_right_border:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch with the -1 (self.width is the length and we are accessing array indices, so this might raise an IndexError)! I think this bug was not seen before? Good bugfix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think I made the bug myself and then fixed it :D Thanks anyway!

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oups... Anyway, thanks for the pull request!

if self.walkable(0, y):
neighbors.append(self.nodes[y][0])
s1 = True
else:
if self.walkable(x + 1, y):
neighbors.append(self.nodes[y][x + 1])
s1 = True
# ↓
if self.walkable(x, y + 1):
neighbors.append(self.nodes[y + 1][x])
s2 = True
if y == self.height - 1 and self.passable_up_down_border:
if self.walkable(x, 0):
neighbors.append(self.nodes[0][x])
s2 = True
else:
if self.walkable(x, y + 1):
neighbors.append(self.nodes[y + 1][x])
s2 = True
# ←
if self.walkable(x - 1, y):
neighbors.append(self.nodes[y][x - 1])
s3 = True
if x == 0 and self.passable_left_right_border:
if self.walkable(self.width - 1, y):
neighbors.append(self.nodes[y][self.width - 1])
s3 = True
else:
if self.walkable(x - 1, y):
neighbors.append(self.nodes[y][x - 1])
s3 = True

if diagonal_movement == DiagonalMovement.never:
return neighbors
Expand Down