-
Notifications
You must be signed in to change notification settings - Fork 11
/
fast_dijkstra.pyx
347 lines (257 loc) · 9.23 KB
/
fast_dijkstra.pyx
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from globals import WORLD_INFO, MAP_SIZE, SETTINGS
from libc.stdlib cimport malloc, free
import zones as zon
import numbers
import time
import copy
cdef distance(pos1, pos2):
cdef int x_dist, y_dist
cdef int *_pos1 = <int *>malloc(2 * 2 * sizeof(int))
cdef int *_pos2 = <int *>malloc(2 * 2 * sizeof(int))
_pos1[0] = pos1[0]
_pos1[1] = pos1[1]
_pos2[0] = pos2[0]
_pos2[1] = pos2[1]
x_dist = abs(_pos1[0]-_pos2[0])
y_dist = abs(_pos1[1]-_pos2[1])
free(_pos1)
free(_pos2)
if x_dist > y_dist:
return y_dist + (x_dist-y_dist)
else:
return x_dist + (y_dist-x_dist)
cdef create_map_array(val, size):
cdef int x, y
_map = []
for x in range(size[0]):
_y = []
for y in range(size[1]):
_y.append(val)
_map.append(_y)
return _map
#@profile
def dijkstra_map(start_pos, goals, zones, max_chunk_distance=5, rolldown=True, avoid_chunks=[], avoid_positions=[], return_score=False, return_score_in_range=[]):
#Some notes before we begin:
# * You can't get cython-created arrays out of this function
#TODO: We need to associate goals with zones, otherwise OOB errors occur
_init_time = time.time()
_avoid_positions = avoid_positions[:]
avoid_positions = []
for position in _avoid_positions:
if not position:
continue
avoid_positions.append(tuple(position[:2]))
cdef int x, y, _x, _y, _n_x, _n_y, _i, _number_of_goals
cdef float _score
cdef float _lowest_score
cdef int _world_map_size_x = MAP_SIZE[0]
cdef int _world_map_size_y = MAP_SIZE[1]
cdef int _dijkstra_map_size_x
cdef int _dijkstra_map_size_y
cdef int _chunk_size = WORLD_INFO['chunk_size']
cdef int *_top_left = <int *>malloc(2 * 2 * sizeof(int))
cdef int *_bot_right = <int *>malloc(2 * 2 * sizeof(int))
cdef int *_pos = <int *>malloc(2 * 2 * sizeof(int))
cdef int *_next_pos = <int *>malloc(2 * 2 * sizeof(int))
cdef int *_goals_x = <int *>malloc(len(goals) * len(goals) * sizeof(int))
cdef int *_goals_y = <int *>malloc(len(goals) * len(goals) * sizeof(int))
_number_of_goals = len(goals)
_chunk_keys = {}
_top_left[0] = _world_map_size_x
_top_left[1] = _world_map_size_y
_bot_right[0] = 0
_bot_right[1] = 0
TEMP_SIZE = 20
for _i in range(_number_of_goals):
_goals_x[_i] = goals[_i][0]
_goals_y[_i] = goals[_i][1]
if goals[_i][0] < _top_left[0]:
_top_left[0] = numbers.clip(goals[_i][0]-TEMP_SIZE, 0, MAP_SIZE[0])
if goals[_i][0] > _bot_right[0]:
_bot_right[0] = numbers.clip(goals[_i][0]+TEMP_SIZE, 0, MAP_SIZE[0])
if goals[_i][1] < _top_left[1]:
_top_left[1] = numbers.clip(goals[_i][1]-TEMP_SIZE, 0, MAP_SIZE[1])
if goals[_i][1] > _bot_right[1]:
_bot_right[1] = numbers.clip(goals[_i][1]+TEMP_SIZE, 0, MAP_SIZE[1])
if start_pos[0]<_top_left[0]:
_top_left[0] = numbers.clip(start_pos[0]-TEMP_SIZE, 0, MAP_SIZE[0])
if start_pos[0]>_bot_right[0]:
_bot_right[0] = numbers.clip(start_pos[0]+TEMP_SIZE, 0, MAP_SIZE[0])
if start_pos[1]<_top_left[1]:
_top_left[1] = numbers.clip(start_pos[1]-TEMP_SIZE, 0, MAP_SIZE[1])
if start_pos[1]>_bot_right[1]:
_bot_right[1] = numbers.clip(start_pos[1]+TEMP_SIZE, 0, MAP_SIZE[1])
if SETTINGS['print dijkstra maps']:
print 'debug'
print 'start_pos', start_pos
print 'top_left', (_top_left[0], _top_left[1])
print 'bot_right', (_bot_right[0], _bot_right[1])
for goal in goals:
print 'goal', goal
_open_map = create_map_array(-3, MAP_SIZE)
_avoid_goals = []
for zone in [zon.get_slice(z) for z in zones]:
_zone_top_left_x = numbers.clip(_top_left[0], zone['top_left'][0], zone['bot_right'][0])
_zone_top_left_y = numbers.clip(_top_left[1], zone['top_left'][1], zone['bot_right'][1])
_zone_bot_right_x = numbers.clip(_bot_right[0], zone['top_left'][0], zone['bot_right'][0])
_zone_bot_right_y = numbers.clip(_bot_right[1], zone['top_left'][1], zone['bot_right'][1])
for y in range(_zone_top_left_y, _zone_bot_right_y):
for x in range(_zone_top_left_x, _zone_bot_right_x):
if (x, y) in avoid_positions:
continue
if _open_map[x][y]>-3:
continue
_map_pos = WORLD_INFO['map'][x-zone['top_left'][0]-1][y-zone['top_left'][1]-1][zone['z']]
if not _map_pos or not 'z_id' in _map_pos:
continue
try:
if not _map_pos['z_id'] == zone['id']:
continue
#if not zone['_map'][x-zone['top_left'][0]-1][y-zone['top_left'][1]-1]:
# continue
except:
print 'Dijkstra crash dump:'
print 'Zones:', zones
print 'Start pos:', start_pos
if len(zones) == 2:
print 'Connected:', zon.can_path_to_zone(zones[0], zones[1])
raise Exception('Crash.')
_chunk_key = '%s,%s' % ((x/_chunk_size)*_chunk_size, (y/_chunk_size)*_chunk_size)
if avoid_chunks and _chunk_key in avoid_chunks:
_avoid_goals.append((x, y))
_open_map[x][y] = 1
_chunk = WORLD_INFO['chunk_map'][_chunk_key]
_pass = False
for i in range(0, _number_of_goals):
_goal_chunk_key = '%s,%s' % ((_goals_x[i]/_chunk_size)*_chunk_size, (_goals_y[i]/_chunk_size)*_chunk_size)
_goal_chunk = WORLD_INFO['chunk_map'][_goal_chunk_key]
if distance(_chunk['pos'], _goal_chunk['pos'])/_chunk_size<=max_chunk_distance:
_pass = True
break
if not _pass:
continue
_chunk_keys[_chunk_key] = zone['id']
_dijkstra_map_size_x = _bot_right[0]-_top_left[0]
_dijkstra_map_size_y = _bot_right[1]-_top_left[1]
cdef float *_dijkstra_map = <float *>malloc(500 * 500 * sizeof(float))
cdef float *_old_map = <float *>malloc(500 * 500 * sizeof(float))
for y in range(0, _dijkstra_map_size_y):
for x in range(0, _dijkstra_map_size_x):
_x = x+_top_left[0]
_y = y+_top_left[1]
if _open_map[_x][_y]<=0:
_dijkstra_map[x + y * 500] = -99999
_old_map[x + y * 500] = -99999
else:
_dijkstra_map[x + y * 500] = 99999
_old_map[x + y * 500] = 99999
goals.extend(_avoid_goals)
for goal in goals:
_x = goal[0]-_top_left[0]
_y = goal[1]-_top_left[1]
_dijkstra_map[_x + _y * 500] = 0
_changed = True
_change_time = time.time()
while _changed:
_changed = False
for y in range(0, _dijkstra_map_size_y):
for x in range(0, _dijkstra_map_size_x):
if _old_map[x + y * 500]<=0:
continue
_old_map[x + y * 500] = _dijkstra_map[x + y * 500]
_lowest_score = _old_map[x + y * 500]
for _n_x,_n_y in [(0, -1), (-1, 0), (1, 0), (0, 1)]:
_y = y+_n_y
if _y<0 or _y>=_dijkstra_map_size_y:
continue
_x = x+_n_x
if _x<0 or _x>=_dijkstra_map_size_x:
continue
if _old_map[_x + _y * 500]<0:
continue
_score = _old_map[_x + _y * 500]
if _score<_lowest_score:
_lowest_score = _score
if _old_map[x + y * 500]-_lowest_score>=2:
_dijkstra_map[x + y * 500] = _lowest_score+1
_changed=True
if not rolldown:
for y in range(0, _dijkstra_map_size_y):
for x in range(0, _dijkstra_map_size_x):
if _dijkstra_map[x + y * 500]<=0:
continue
_dijkstra_map[x + y * 500] *= -1.2
_old_map[x + y * 500] *= -1.2
if SETTINGS['print dijkstra maps']:
for y in range(0, _bot_right[1]-_top_left[1]):
for x in range(0, _bot_right[0]-_top_left[0]):
if [x+_top_left[0], y+_top_left[1]] == start_pos[:2]:
print 'X',
elif rolldown:
if _dijkstra_map[x + y * 500]>0:
print int(numbers.clip(_dijkstra_map[x + y * 500], 0, 9)),
else:
print '#',
else:
if _dijkstra_map[x + y * 500]<0:
print int(numbers.clip(-_dijkstra_map[x + y * 500], 0, 9)),
else:
print '#',
print
if return_score:
_score = _dijkstra_map[(start_pos[0]-_top_left[0]) + (start_pos[1]-_top_left[1]) * 500]
free(_old_map)
free(_dijkstra_map)
return _score
if return_score_in_range:
_positions = []
for y in range(0, _bot_right[1]-_top_left[1]):
for x in range(0, _bot_right[0]-_top_left[0]):
if _dijkstra_map[x + y * 500] in range(return_score_in_range[0], return_score_in_range[1]):
_positions.append((_top_left[0]+x, _top_left[1]+y))
free(_old_map)
free(_dijkstra_map)
return _positions
_path = []
_pos[0] = start_pos[0]-_top_left[0]
_pos[1] = start_pos[1]-_top_left[1]
while 1:
if rolldown and _dijkstra_map[_pos[0] + _pos[1] * 500]<=0:
break
elif not rolldown and _dijkstra_map[_pos[0] + _pos[1] * 500]>0:
break
_lowest_score = _old_map[_pos[0] + _pos[1] * 500]
_next_pos[0] = -1
_next_pos[1] = -1
for _n_y in range(-1, 2):
_y = _pos[1]+_n_y
if _y<0 or _y>=_dijkstra_map_size_y:
continue
for _n_x in range(-1, 2):
if _n_x == 0 and _n_y == 0:
continue
_x = _pos[0]+_n_x
if _x<0 or _x>=_dijkstra_map_size_x:
continue
if rolldown:
if _dijkstra_map[_x + _y * 500]<0:
continue
else:
if _dijkstra_map[_x + _y * 500]>=0 or _open_map[_x+_top_left[0]][_y+_top_left[1]]==-3:
continue
_score = _dijkstra_map[_x + _y * 500]
if _score<_lowest_score:
_lowest_score = _score
_next_pos[0] = _x
_next_pos[1] = _y
if _lowest_score == _old_map[_pos[0] + _pos[1] * 500]:
break
else:
_path.append((_next_pos[0]+_top_left[0], _next_pos[1]+_top_left[1], 2))
if (_next_pos[0], _next_pos[1]) == (_pos[0], _pos[1]):
break
_pos[0] = _next_pos[0]
_pos[1] = _next_pos[1]
free(_old_map)
free(_dijkstra_map)
return _path