-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_cache.py
executable file
·281 lines (224 loc) · 8.46 KB
/
path_cache.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
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
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/env python
#
import sys, math
from pygame.locals import *
import pygame
from copy import copy
from geom_helpers import *
from astar import AStar
import navi_graph
import data
def Simplify(path):
'''Simplifies paths passed to it by minimizing the angle difference between path cells.'''
ANGLE_EPSILON = 0.392699082 # ~ 22.5 degrees
if len(path) <= 2:
return path
new_path = []
angle = None
for i in xrange(1,len(path)):
p0 = path[i-1]
p1 = path[i]
dx = p1[0] - p0[0]
dy = p1[1] - p0[1]
if angle is None:
angle = math.atan2(dy,dx)
new_path.append(p0)
new_path.append(p1)
continue
new_angle = math.atan2(dy,dx)
if abs(new_angle - angle) > ANGLE_EPSILON:
new_path.append(p1)
angle = new_angle
new_path.append( path[-1] )
return new_path
# 8 neighbors for rectangular grids.
# xl,yu x,yu xr,yu
# xl, y x, y xr, y
# xl,yd x,yd xr,yd
#offsets = [
# (-1, 1), (0, 1), (1, 1),
# (-1, 0), (1, 0),
# (-1, -1), (0, -1), (1, -1),
#]
GRID_NEIGHBORS = [
(0, 1),
(-1, 0), (1, 0),
(0, -1),
]
def GetNeighborsDiagonal( eval_func ):
'''Eval func takes an offset (one of the GRID_NEIGHBORS), and returns True or False.'''
# if 0 or 1 are False, add neighbor (-1, 1)
# if 0 or 2 are False, add neighbor ( 1, 1)
# if 1 or 3 are False, add neighbor (-1,-1)
# if 2 or 3 are False, add neighbor ( 1,-1)
diag_rules = {
(0,1):(-1, 1),
(0,2):( 1, 1),
(1,3):(-1,-1),
(2,3):( 1,-1)
}
neighbors = []
for neighbors,off in diag_rules.items():
if not eval_func(GRID_NEIGHBORS[neighbors[0]]) or \
not eval_func(GRID_NEIGHBORS[neighbors[1]]):
neighbors.append(off)
def unique(seq, idfun=None):
# order preserving
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in seq:
marker = idfun(item)
if marker in seen:
continue
seen[marker] = 1
result.append(item)
return result
class PathCache(object):
'''The path cache holds a hierarchy of paths and goals.'''
def __init__(self, grid_size):
self.ClearCache()
# b -> white
self.colors = [ (c,c,255) for c in xrange(0,256) ]
self.astar_vals = {}
self.astar_max = self.astar_min = 0
self.dead_ends = set()
self.grid_size = grid_size
self.creep_paths = []
def ClearCache(self):
self.creep_paths = []
def SetAStarMinMax(self):
self.astar_min = None
self.astar_max = None
for cost in self.astar_vals.itervalues():
if self.astar_min is None or cost < self.astar_min:
self.astar_min = cost
if self.astar_max is None or cost > self.astar_max:
self.astar_max = cost
def FindNewGoal(self, old_goal):
'''Returns a new goal point, given the old goal
If the old goal can't be found, the nearest point is returned.'''
assert len(self.creep_paths)
assert len(self.creep_paths[0])
if old_goal is None:
return self.creep_paths[0][0], self.creep_paths[0]
closest = self.creep_paths[0][-1]
closest_dist = Distance(self.creep_paths[0][-1], old_goal)
new_goal = None
src_path = None
for p in self.creep_paths:
for idx in xrange(len(p)-1):
if p[idx] == old_goal:
new_goal = p[idx+1]
src_path = p
# print "found old goal %s, returning new goal %s (%d total goals)" % \
# (old_goal, new_goal, len(p))
break
dist = Distance(p[idx], old_goal)
if dist < closest_dist:
closest_dist = dist
closest = p[idx]
src_path = p
# print "new goal, closest %s at %f" % (closest, closest_dist)
# Adjacent.
if new_goal is None and closest_dist < 20:
new_goal = closest
if new_goal is None:
graph = navi_graph.navi_graph
print "A*: %s to %s" % (old_goal, closest)
new_path, vals = AStar( graph, old_goal, data.end_pt )
if new_path is None or len(new_path) < 2:
print new_path
return data.end_pt
# Filter out duplicate entries.
new_path = unique( new_path )
print "new path: ", new_path
assert len(new_path) >= 2
src_path = new_path
new_goal = new_path[1]
self.creep_paths.append(new_path)
print "Used A* to find new goal (%d total goals)" % \
len(new_path)
data.gridCost.AddCostPath( new_path, 500)
if new_goal is None:
new_goal = closest
print "found closest %s, returning new goal %s (%d total goals)" % \
(closest, new_goal, len(self.creep_paths[0]))
return new_goal, src_path
def ResetPath(self, start, end):
self.creep_paths = []
self.GetPath(start,end)
def GetPath(self, p0, p1):
'''Returns the path and the path index for the given pt.
Returns an empty path, None if no path could be found.'''
start = data.GetGridCoord( p0 )
end = data.GetGridCoord( p1 )
print "looking for %s -> %s" % (start, end)
found_path = []
found_idx = None
for path in self.creep_paths:
# print " path %s:%s" % (path[0],path[-1])
if path[0] == start and path[-1] == end:
found_path = path
found_idx = 0
break
else:
for pt_idx in xrange(len(path)):
if path[pt_idx] == start:
found_idx = pt_idx+1
found_path = path
break
if len(found_path) == 0:
# print "a* calc miss, recalc %s -> %s" % (start, end)
graph = navi_graph.navi_graph
try:
found_path, self.astar_vals = AStar( graph, start, end )
self.SetAStarMinMax()
if len(found_path):
# print " new path %s:%s" % (found_path[0],found_path[-1])
found_idx = 0
self.creep_paths.append(found_path)
except ValueError:
pass
else:
# print "a* calc cache hit %s -> %s" % (start, end)
pass
# print "returned %d dead ends" % len(self.dead_ends)
return found_path, found_idx
def Draw(self, surf):
delta = 0
if self.astar_min != self.astar_max:
delta = self.astar_max - self.astar_min
for pt,cost in self.astar_vals.iteritems():
idx = int(((cost - self.astar_min) * 255) / delta);
if idx > 255:
print "idx: %d cost %f, [%f-%f]" % (idx, cost, self.astar_min, self.astar_max)
assert False
r = pygame.Rect( pt, (20, 20) )
surf.fill(self.colors[idx], r)
if len(self.creep_paths):
for p in self.creep_paths[0]:
color = (128,0,0)
if delta != 0 and p in self.astar_vals:
cost = self.astar_vals[p]
idx = int(((cost - self.astar_min) * 255) / delta);
color = self.colors[idx]
r = pygame.Rect( (p), (20, 20) )
surf.fill(color, r)
pygame.draw.rect(surf, (0,0,0), r, 3)
for pi in xrange(1,len(self.creep_paths)):
for p in self.creep_paths[pi]:
r = pygame.Rect( p, (20, 20) )
pygame.draw.rect(surf, (128,128,0), r, 1)
def __str__(self):
str = "%d paths:\n" % len(self.creep_paths)
for p in self.creep_paths:
str += "%s\n" % p
return str
if __name__ == '__main__':
mg = MultiLevelGrid( (640,480), (20,20), False)
mg.InitLevel(8)
mg.InitLevel(4)
for s,m in mg.levels:
print s