-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paths.py
138 lines (108 loc) · 4.83 KB
/
Paths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Path:
def __init__(self, thispoint):
self.index = 0
self.points = [thispoint] #Point(thispoint)
def add(self, thispoint):
self.points.append(thispoint)
def poplast(self):
self.points.pop()
def next(self):
if self.index == len(self.points):
self.index = 0
raise StopIteration
self.index = self.index + 1
return self.points[self.index - 1]
def __iter__(self):
return self
def __getitem__(self, index):
return self.points[index]
class PathSet:
def __init__(self, apath):
self.paths = [apath] #Path(path)
self.longest = len(self.paths[0].points)
self.shortest = len(self.paths[0].points)
self.index = 0
def next(self):
if self.index == len(self.paths):
self.index = 0
raise StopIteration
self.index = self.index + 1
return self.paths[self.index - 1]
def __iter__(self):
return self
def addpoints(self, movesleft, mapcells):
lastpath = self.paths[0]
lastpoint = lastpath.points[-1]
#for i in range(0, movesleft):
#self.checkLeft(lastpoint, lastpath, movesleft, mapcells)
#for i in
self.checkLeft(lastpoint, lastpath, movesleft, mapcells)
self.checkRight(lastpoint, lastpath, movesleft, mapcells)
self.checkUp(lastpoint, lastpath, movesleft, mapcells)
self.checkDown(lastpoint, lastpath, movesleft, mapcells)
def checkLeft(self, thispoint, lastpath, movesleft, mapcells):
leftpoint = Point(thispoint.x - mapcells.cellsize, thispoint.y)
if leftpoint.x >= 0:
nextcell = mapcells.cells[(leftpoint.x, leftpoint.y)]
if nextcell.walkable == True:
leftpath = Path(Point(0, 0))
leftpath.poplast()
for apoint in lastpath:
leftpath.add(Point(apoint.x, apoint.y))
leftpath.add(leftpoint)
self.paths.append(leftpath)
if (movesleft - 1) > 0:
self.checkLeft(leftpoint, leftpath, movesleft - 1, mapcells)
self.checkUp(leftpoint, leftpath, movesleft - 1, mapcells)
self.checkDown(leftpoint, leftpath, movesleft - 1, mapcells)
def checkRight(self, thispoint, lastpath, movesleft, mapcells):
rightpoint = Point(thispoint.x + mapcells.cellsize, thispoint.y)
if rightpoint.x <= 288:
nextcell = mapcells.cells[(rightpoint.x, rightpoint.y)]
if nextcell.walkable == True:
rightpath = Path(Point(0, 0))
rightpath.poplast()
for apoint in lastpath:
rightpath.add(Point(apoint.x, apoint.y))
rightpath.add(rightpoint)
self.paths.append(rightpath)
if (movesleft - 1) > 0:
self.checkRight(rightpoint, rightpath, movesleft - 1, mapcells)
self.checkUp(rightpoint, rightpath, movesleft - 1, mapcells)
self.checkDown(rightpoint, rightpath, movesleft - 1, mapcells)
def checkUp(self, thispoint, lastpath, movesleft, mapcells):
uppoint = Point(thispoint.x, thispoint.y - mapcells.cellsize)
if uppoint.y >= 0:
nextcell = mapcells.cells[(uppoint.x, uppoint.y)]
if nextcell.walkable == True:
uppath = Path(Point(0, 0))
uppath.poplast()
for apoint in lastpath:
uppath.add(Point(apoint.x, apoint.y))
uppath.add(uppoint)
self.paths.append(uppath)
if (movesleft - 1) > 0:
self.checkLeft(uppoint, uppath, movesleft - 1, mapcells)
self.checkRight(uppoint, uppath, movesleft - 1, mapcells)
self.checkUp(uppoint, uppath, movesleft - 1, mapcells)
def checkDown(self, thispoint, lastpath, movesleft, mapcells):
downpoint = Point(thispoint.x, thispoint.y + mapcells.cellsize)
if downpoint.y <= 288:
nextcell = mapcells.cells[(downpoint.x, downpoint.y)]
if nextcell.walkable == True:
downpath = Path(Point(0, 0))
downpath.poplast()
for apoint in lastpath:
downpath.add(Point(apoint.x, apoint.y))
downpath.add(downpoint)
self.paths.append(downpath)
if (movesleft - 1) > 0:
self.checkLeft(downpoint, downpath, movesleft - 1, mapcells)
self.checkRight(downpoint, downpath, movesleft - 1, mapcells)
self.checkDown(downpoint, downpath, movesleft - 1, mapcells)
def __getitem__(self, index):
return self.paths[index]