-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpathfinder.py
More file actions
271 lines (195 loc) · 6.36 KB
/
Copy pathpathfinder.py
File metadata and controls
271 lines (195 loc) · 6.36 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Written By:
Chris Humphreys
Email: < chris (--AT--) habitualcoder [--DOT--] com >
All python source code copyright Chris Humphreys 2010
Level data, Obliteration name and some media copyright
Flash Jester Punk 2007
Alien Breed name, concept and media copyright Team17 1992
This file is part of alien breed obliteration wii edition
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
"""
A* path finder classes for Alien movement.
Not currently enabled in game due to performance issues.
See
USE_A_STAR = False
in behaviour.py
"""
class Heuristic:
def _init__(self):
return
def get_cost(self, x, y, tx, ty):
return
class PathMap:
def __init__(self, tilemap):
return
def get_width(self):
return
def get_height(self):
return
def blocked(self, x, y):
return
def get_cost(self, start_x, start_y, targetl_x, target_y):
return
def is_valid_tile(self, x, y):
return
class Step:
def __init__(self, x, y):
self.x = x
self.y = y
def equals(self, x, y):
return self.x == x and self.y == y
class Path:
def __init__(self):
self.steps = []
def append(self, step):
self.steps.append(step)
def prepend(self, step):
self.steps.insert(0, step)
def length(self):
return len(self.steps)
def contains(self, x, y):
for s in self.steps:
if s.equals(x,y):
return True
return False
def get_step(self, index):
return self.steps[index]
class PathNode:
def __init__(self, x, y):
self.cost = 0
self.depth = 0
self.parent = None
self.heuristic = 0
self.x = x
self.y = y
def set_parent(self, p):
self.parent = p
self.depth = p.depth + 1
"""
def __cmp__(self, other):
print "Node compare"
f = self.cost + self.heuristic
of = other.cost + other.heuristic
if f < of:
return -1
elif f > of:
return 1
else:
return 0
def __eq__(self, other):
return self.x == other.x and self.y == other.y
"""
class PathNodeComparator:
@staticmethod
def compare(one, other):
f = one.cost + one.heuristic
of = other.cost + other.heuristic
if f < of:
return -1
elif f > of:
return 1
else:
return 0
class PathFinder:
def __init__(self, path_map, heuristic):
self.map = path_map
self.heuristic = heuristic
self.nodes = []
for y in range(0, self.map.get_width()):
row = []
self.nodes.append(row)
for x in range(0, self.map.get_height()):
row.append(PathNode(x,y))
self.open = []
self.closed = []
self.max_search_distance = 10
return
def find(self, start_x, start_y, target_x, target_y):
if self.map.blocked(target_x, target_y):
#No route to target as target is blocked
return None
#initialise starting position and target
#print "start %d,%d" % (start_x, start_y)
starting_node = self.nodes[start_y][start_x]
starting_node.cost = 0
starting_node.depth = 0
starting_node.parent = None
#print "target %d,%d" % (target_x, target_y)
target_node = self.nodes[target_y][target_x]
target_node.parent = None
#Initialise our working lists
del self.open[0:len(self.open)]
del self.closed[0:len(self.closed)]
self.open.append(starting_node)
max_depth = 0
#Repetitively loop through the candidate list ('open') for potential steps
#up until a maximum number of steps and then give up if not found target
while len(self.open) > 0 and max_depth < self.max_search_distance:
#print "Open %d " % len(self.open)
#print "About to find best option"
#Open list is ordered by heuristic
current = self.get_best_cadidate()
#print "Found current %d,%d cost %d" % (current.x, current.y, current.cost)
if current == target_node:
#found our route - we can stop looping...
#print "Found target"
break;
#Move node from candidate to searched list
self.open.remove(current)
self.closed.append(current)
#now check immediate neighbours of tile
for y in [-1,0,1]:
for x in [-1,0,1]:
if y ==0 and x==0:
#skip current position
continue
#print "tring neighbour"
neighbour_x = x + current.x
neighbour_y = y + current.y
if self.map.is_valid_tile(neighbour_x, neighbour_y):
#print "Valid"
neighbour = self.nodes[neighbour_y][neighbour_x]
#calc the cost of moving to the target node
cost = current.cost + self.map.get_cost(current.x, current.y, neighbour.x, neighbour.y)
#if we have already seen this node but this is a better route (lower cost) dscard previous route
if cost < neighbour.cost:
#print "Found lower cost"
if neighbour in self.open:
self.open.remove(neighbour)
if neighbour in self.closed:
self.closed.remove(neighbour)
#if we have not previously processed this node then record this route and add to candidates
if neighbour not in self.open and neighbour not in self.closed:
neighbour.cost = cost
neighbour.set_parent(current)
max_depth = max(max_depth, neighbour.depth)
neighbour.heuristic = self.heuristic.get_cost(neighbour_x, neighbour_y, target_x, target_y)
self.open.append(neighbour)
#print "Add neighbour %d,%d cost %d from %d,%d" % (neighbour.x, neighbour.y, cost,neighbour.parent.x, neighbour.parent.y)
#now check if we have a path, build and return
if not target_node.parent:
#No path to target found
return None
#print "About to construct path"
path = Path()
current = target_node
while current.parent:
#print "Added %d,%d %d,%d" % (current.x, current.y, current.parent.x, current.parent.y)
path.prepend(current)
current = current.parent
return path
def get_best_cadidate(self):
#TODO this list should be maintained sorted
self.open.sort(PathNodeComparator.compare)
return self.open[0]