Skip to content

Commit b8d8d5f

Browse files
committed
Implement include_non_walkable on grid neighbors.
1 parent 40771a0 commit b8d8d5f

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

pathfinding/core/grid.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def calc_cost(self, node_a, node_b, weighted=False):
116116

117117
def neighbors(
118118
self, node: GridNode,
119-
diagonal_movement: DiagonalMovement = DiagonalMovement.never
119+
diagonal_movement: DiagonalMovement = DiagonalMovement.never,
120+
include_non_walkable: bool = False
120121
) -> List[GridNode]:
121122
"""
122123
get all neighbors of one node
@@ -133,7 +134,7 @@ def neighbors(
133134
else:
134135
north_y = y - 1
135136

136-
if self.walkable(x, north_y):
137+
if self.walkable(x, north_y) or include_non_walkable and self.inside(x, north_y):
137138
neighbors.append(self.nodes[north_y][x])
138139
north = True
139140

@@ -143,7 +144,7 @@ def neighbors(
143144
else:
144145
east_x = x + 1
145146

146-
if self.walkable(east_x, y):
147+
if self.walkable(east_x, y) or include_non_walkable and self.inside(east_x, y):
147148
neighbors.append(self.nodes[y][east_x])
148149
east = True
149150

@@ -152,7 +153,7 @@ def neighbors(
152153
south_y = 0
153154
else:
154155
south_y = y + 1
155-
if self.walkable(x, south_y):
156+
if self.walkable(x, south_y) or include_non_walkable and self.inside(x, south_y):
156157
neighbors.append(self.nodes[south_y][x])
157158
south = True
158159

@@ -161,7 +162,7 @@ def neighbors(
161162
west_x = self.width - 1
162163
else:
163164
west_x = x - 1
164-
if self.walkable(west_x, y):
165+
if self.walkable(west_x, y) or include_non_walkable and self.inside(west_x, y):
165166
neighbors.append(self.nodes[y][west_x])
166167
west = True
167168

@@ -195,7 +196,7 @@ def neighbors(
195196
nw_y = self.height - 1
196197
else:
197198
nw_y = y - 1
198-
if self.walkable(nw_x, nw_y):
199+
if self.walkable(nw_x, nw_y) or include_non_walkable and self.inside(nw_x, nw_y):
199200
neighbors.append(self.nodes[nw_y][nw_x])
200201

201202
# ↗
@@ -208,7 +209,7 @@ def neighbors(
208209
ne_y = self.height - 1
209210
else:
210211
ne_y = y - 1
211-
if self.walkable(ne_x, ne_y):
212+
if self.walkable(ne_x, ne_y) or include_non_walkable and self.inside(ne_x, ne_y):
212213
neighbors.append(self.nodes[ne_y][ne_x])
213214

214215
# ↘
@@ -221,7 +222,7 @@ def neighbors(
221222
se_y = 0
222223
else:
223224
se_y = y + 1
224-
if self.walkable(se_x, se_y):
225+
if self.walkable(se_x, se_y) or include_non_walkable and self.inside(se_x, se_y):
225226
neighbors.append(self.nodes[se_y][se_x])
226227

227228
# ↙
@@ -234,7 +235,7 @@ def neighbors(
234235
sw_y = 0
235236
else:
236237
sw_y = y + 1
237-
if self.walkable(sw_x, sw_y):
238+
if self.walkable(sw_x, sw_y) or include_non_walkable and self.inside(sw_x, sw_y):
238239
neighbors.append(self.nodes[sw_y][sw_x])
239240

240241
return neighbors

0 commit comments

Comments
 (0)