@@ -39,6 +39,8 @@ def __init__(self, width=0, height=0, matrix=None, inverse=False):
39
39
"""
40
40
self .width = width
41
41
self .height = height
42
+ self .passable_left_right_border = False
43
+ self .passable_up_down_border = False
42
44
if isinstance (matrix , (tuple , list )) or (
43
45
USE_NUMPY and isinstance (matrix , np .ndarray ) and
44
46
matrix .size > 0 ):
@@ -49,6 +51,12 @@ def __init__(self, width=0, height=0, matrix=None, inverse=False):
49
51
else :
50
52
self .nodes = [[]]
51
53
54
+ def set_passable_left_right_border (self ):
55
+ self .passable_left_right_border = True
56
+
57
+ def set_passable_up_down_border (self ):
58
+ self .passable_up_down_border = True
59
+
52
60
def node (self , x , y ):
53
61
"""
54
62
get node at position
@@ -84,21 +92,41 @@ def neighbors(self, node, diagonal_movement=DiagonalMovement.never):
84
92
s0 = d0 = s1 = d1 = s2 = d2 = s3 = d3 = False
85
93
86
94
# ↑
87
- if self .walkable (x , y - 1 ):
88
- neighbors .append (self .nodes [y - 1 ][x ])
89
- s0 = True
95
+ if y == 0 and self .passable_up_down_border :
96
+ if self .walkable (x , self .height - 1 ):
97
+ neighbors .append (self .nodes [self .height - 1 ][x ])
98
+ s0 = True
99
+ else :
100
+ if self .walkable (x , y - 1 ):
101
+ neighbors .append (self .nodes [y - 1 ][x ])
102
+ s0 = True
90
103
# →
91
- if self .walkable (x + 1 , y ):
92
- neighbors .append (self .nodes [y ][x + 1 ])
93
- s1 = True
104
+ if x == self .width - 1 and self .passable_left_right_border :
105
+ if self .walkable (0 , y ):
106
+ neighbors .append (self .nodes [y ][0 ])
107
+ s1 = True
108
+ else :
109
+ if self .walkable (x + 1 , y ):
110
+ neighbors .append (self .nodes [y ][x + 1 ])
111
+ s1 = True
94
112
# ↓
95
- if self .walkable (x , y + 1 ):
96
- neighbors .append (self .nodes [y + 1 ][x ])
97
- s2 = True
113
+ if y == self .height - 1 and self .passable_up_down_border :
114
+ if self .walkable (x , 0 ):
115
+ neighbors .append (self .nodes [0 ][x ])
116
+ s2 = True
117
+ else :
118
+ if self .walkable (x , y + 1 ):
119
+ neighbors .append (self .nodes [y + 1 ][x ])
120
+ s2 = True
98
121
# ←
99
- if self .walkable (x - 1 , y ):
100
- neighbors .append (self .nodes [y ][x - 1 ])
101
- s3 = True
122
+ if x == 0 and self .passable_left_right_border :
123
+ if self .walkable (self .width - 1 , y ):
124
+ neighbors .append (self .nodes [y ][self .width - 1 ])
125
+ s3 = True
126
+ else :
127
+ if self .walkable (x - 1 , y ):
128
+ neighbors .append (self .nodes [y ][x - 1 ])
129
+ s3 = True
102
130
103
131
if diagonal_movement == DiagonalMovement .never :
104
132
return neighbors
0 commit comments