Repository navigation
Expand file tree
/
Copy pathbuilder.py
More file actions
443 lines (373 loc) · 12.1 KB
/
Copy pathbuilder.py
File metadata and controls
443 lines (373 loc) · 12.1 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import math, sys, queue, threading, multiprocessing, re, signal, time
from collections import defaultdict
WAYPOINTS_FILE = 'waypoints.txt'
GRAPH_FILE = 'landmark_hl1.txt'
DESTINATION = ''
WAYPOINT_DISTANCE = 800
# This transitions will not be used in routes (one way direction ban!)
BANNED_TRANSITIONS = [
# from_map via_landmark to_map
]
VOID_MAPS = [
'c1a4g',
'c1a4j',
'c2a4',
]
MAX_PATH_LENGTH = 35
INT_MAX = 1 << 32
DST_MAP = ''
DST_ORIGIN = []
WARP_START_MAP = 'c1a1'
MAIN_COORD_MAP = WARP_START_MAP
THREAD_COUNT = multiprocessing.cpu_count()
def InitDestination(x):
global DST_MAP, DST_ORIGIN
part = x.split(' at: ')
DST_MAP = part[0]
DST_ORIGIN = [int(e[:-2]) for e in part[1].split(', ')]
#
# Common
#
def intersection(listA, listB):
return [x for x in listA if x in listB]
#
# Vector
#
def vec_len(vec):
tmp = 0
for val in vec:
tmp += val**2
return math.sqrt(tmp)
def vec_add(vec1, vec2):
assert(len(vec1) == len(vec2))
tmp = []
for i in range(len(vec1)):
tmp.append(vec1[i] + vec2[i])
return tmp
def vec_neg(vec):
tmp = []
for val in vec:
tmp.append(-val)
return tmp
def vec_sub(vec1, vec2):
return vec_add(vec1, vec_neg(vec2))
def vec_distance(vec1, vec2):
return vec_len(vec_sub(vec1, vec2))
def vec_mul_scalar(vec, c):
tmp = []
for i in range(len(vec)):
tmp.append(vec[i] * c)
return tmp
def vec_str(vec):
return ' '.join([str(x) for x in vec])
def distance(x, y):
return vec_len(vec_sub(x, y))
#
# Graph, maps
#
Graph = []
Maps = []
def ReadGraph(filename):
global Graph, Maps
fp = open(filename, 'r')
if not fp:
print('No graph in ' + filename)
exit(-1)
lines = fp.read().split('\n')
for line in lines:
data = line.split(',')
data = [e[1:-1] for e in data]
Graph += [(data[0], data[1], data[2], [int(float(e)) for e in data[3].split(' ')])]
fp.close()
#
Maps = set()
landmarks = set()
for node in Graph:
Maps.add(node[0])
Maps.add(node[2])
Maps = list(Maps)
def GetConnections(map_1, map_2):
global Graph
result = []
for node in Graph:
if node[0] == map_1 and node[2] == map_2 and [node[1]] not in result:
result.append(node[1])
return result
def GetOutConnections(map_1):
global Graph
result = []
for node in Graph:
if node[0] == map_1 and [node[2], node[1]] not in result:
result.append([node[2], node[1]])
return result
def GetInConnections(map_1):
global Graph
result = []
for node in Graph:
if node[2] == map_1 and [node[0], node[1]] not in result:
result.append([node[0], node[1]])
return result
def GetStrongPairConnections(map_1, map_2):
return intersection(GetConnections(map_1, map_2), GetConnections(map_2, map_1))
def GetStrongConnections(map):
result = []
out_connections = GetOutConnections(map)
for connect in out_connections:
strong_connections = GetStrongPairConnections(map, connect[0])
for x in strong_connections:
result += [[connect[0], x]]
return result
def GetAnyStrongConnection(map_1, map_2):
result = GetStrongPairConnections(map_1, map_2)
return result[0] if result != [] else None
def GetLandmarkOrigin(mapName, landmarkName):
global Graph
for node in Graph:
if node[0] == mapName and node[1] == landmarkName:
return node[3]
return [0, 0, 0]
#
# Graph d,p
#
Maps_d = []
Maps_p = []
def BuildWays():
global Maps, Maps_d, Maps_p
Maps_d = [[INT_MAX for i in range(len(Maps))] for j in range(len(Maps))]
Maps_p = [[-1 for i in range(len(Maps))] for j in range(len(Maps))]
for i in range(len(Maps)):
for j in range(len(Maps)):
lm = GetAnyStrongConnection(Maps[i], Maps[j])
if lm and i != j:
Maps_d[i][j] = 1;
Maps_d[j][i] = 1
Maps_p[i][j] = j
Maps_p[j][i] = i
for i in range(len(Maps)):
for u in range(len(Maps)):
for v in range(len(Maps)):
if u != v:
if Maps_d[u][i] < INT_MAX and Maps_d[i][v] < INT_MAX:
if Maps_d[u][i] + Maps_d[i][v] < Maps_d[u][v]:
Maps_d[u][v] = Maps_d[u][i] + Maps_d[i][v]
Maps_p[u][v] = Maps_p[u][i]
def FixOrigin(map_from, map_to, coord):
global Maps, Maps_d, Maps_p
START_IDX = Maps.index(map_from)
FINISH_IDX = Maps.index(map_to)
current = START_IDX
while Maps_d[current][FINISH_IDX] != INT_MAX:
next = Maps_p[current][FINISH_IDX]
landmark = GetAnyStrongConnection(Maps[next], Maps[current])
assert(landmark != None)
coord = vec_add(coord, vec_sub(
GetLandmarkOrigin(Maps[next], landmark),
GetLandmarkOrigin(Maps[current], landmark)))
current = next
return coord
#
# Map_order
#
Maps_order = []
def BuildMapOrder(map_start, map_finish):
global Maps, Maps_d, Maps_p, Maps_order
START_IDX = Maps.index(map_start)
FINISH_IDX = Maps.index(map_finish)
current = START_IDX
while Maps_d[current][FINISH_IDX] != INT_MAX:
Maps_order += [Maps[current]]
current = Maps_p[current][FINISH_IDX]
Maps_order += [Maps[current]]
# print('\n'.join(Maps_order))
def GetMapWarps(save_map):
global Maps_order
result = []
save_map_idx = Maps_order.index(save_map)
X = Maps_order[save_map_idx]
# landmarks
array_Y_landmark_XY = GetStrongConnections(X)
for Y, landmark_XY in array_Y_landmark_XY:
if Y not in Maps_order:
continue # map outside of our way
in_connections_Y = GetInConnections(Y)
for Z, landmark_ZY in in_connections_Y:
if Z not in Maps_order:
continue # map outside of our way
if [Z, landmark_ZY, Y] in BANNED_TRANSITIONS:
continue
# SWT: changelevel2 Y landmark_ZY
A = GetLandmarkOrigin(X, landmark_XY)
B = GetLandmarkOrigin(X, landmark_ZY)
C = GetLandmarkOrigin(Y, landmark_ZY)
D = GetLandmarkOrigin(Y, landmark_XY)
vec_result = vec_add(vec_sub(C, B), vec_sub(A, D))
if vec_result == [0, 0, 0]:
continue # save warp
direction = -1 if Maps_order.index(Y) < Maps_order.index(X) else 1
result += [[vec_result, Y, X, landmark_ZY, direction, landmark_XY]]
return result
#
# Waypoints
#
Waypoints = []
def ReadWaypoints(filename, main_map):
global Waypoints, Maps_order
fp = open(filename, 'r')
if not fp:
print('No waypoints in ' + filename)
return None
exit(-1);
for line in fp:
data = line.split(' | ')
if len(data) == 2:
coord = [int(e) for e in data[1].split(' ')]
#if data[0] not in Maps_order[:-1]:
# continue
coord_global = FixOrigin(data[0], main_map, coord)
Waypoints += [[coord_global, data[0]]]
#print('\n'.join([str(x) for x in Waypoints]))
fp.close()
def HowCloseToWaypoint(vec, isVoidMapInPath):
global Waypoints
for point in Waypoints[::-1]:
info = 0
shift_z = point[0][2] - vec[2]
if isVoidMapInPath and shift_z > 0:
dist = distance(point[0][:2], vec[:2])
info = shift_z
else:
dist = distance(point[0], vec)
if dist < WAYPOINT_DISTANCE:
return dist, point[1], info
return -1, '', 0
#
# Warp Vectors
#
WarpVectors = []
WarpDict = defaultdict(list) # dst -> [[vec_result, X, landmark_ZY, direction]]
def GetWarpVectors():
global Maps_order, WarpVectors
for map in Maps_order:
WarpVectors += GetMapWarps(map)
print('\n'.join([str(x) for x in WarpVectors]))
for warp in WarpVectors:
new_warp = [warp[0], warp[2], warp[3], warp[4]]
if new_warp not in WarpDict[warp[1]]:
WarpDict[warp[1]] += [new_warp]
#
# Threading
#
counterLock = threading.Lock()
lastRouteLen = 1
def simplifyRoute(route):
return re.sub(r'\(.+?\)', '', ''.join(route))
def calcRouteLen(route):
return len(route)
def reverseRoute(route):
return route[::-1]
processedSaved = set()
stdout_lock = threading.Lock()
def PathFinder(q, startMapIndex, dstMapIndex):
global BANNED_TRANSITIONS, MAIN_COORD_MAP, MAX_PATH_LENGTH, Maps_order, lastRouteLen, WarpDict, stdout_lock
while q.qsize():
currentMapIndex, currentCoordinates, isVoidMapCrossed, route = q.get()
if str((currentCoordinates, currentMapIndex)) not in processedSaved:
processedSaved.add(str((currentCoordinates, currentMapIndex)))
else:
q.task_done()
continue
routeLen = calcRouteLen(route)
if lastRouteLen / THREAD_COUNT < routeLen:
with counterLock:
lastRouteLen += 1
if lastRouteLen % THREAD_COUNT == 0:
with stdout_lock:
print('Search for path not longer than ' + str(int(lastRouteLen / THREAD_COUNT)) + ' steps complete!')
sys.stdout.flush()
if currentMapIndex < startMapIndex or currentMapIndex > dstMapIndex:
q.task_done()
continue
dist, save_map, shift_z = HowCloseToWaypoint(currentCoordinates, isVoidMapCrossed)
if dist >= 0:
if save_map == Maps_order[currentMapIndex] and routeLen > 0:
currentCoordinates_save_map = FixOrigin(MAIN_COORD_MAP, save_map, currentCoordinates)
with stdout_lock:
print("="*20)
print(" Destination: " + DESTINATION)
print(" " + str(int(dist)) + " - " + save_map)
shift_vec = [0, 0, -shift_z]
print(" map "+save_map+";w 70;noclip;bxt_ch_set_pos " + vec_str(vec_sub(currentCoordinates_save_map, shift_vec)))
if shift_vec != [0, 0, 0]:
print(" bxt_ch_set_pos_offset " + vec_str(shift_vec))
print(" // " + simplifyRoute(reverseRoute(route)))
print(" // " + ' '.join(reverseRoute(route)))
print("="*20)
sys.stdout.flush()
if routeLen == MAX_PATH_LENGTH:
q.task_done()
continue
if not isVoidMapCrossed:
if Maps_order[currentMapIndex] in VOID_MAPS:
isVoidMapCrossed = True
# sw
if currentMapIndex > startMapIndex:
for lm in GetStrongPairConnections(Maps_order[currentMapIndex], Maps_order[currentMapIndex - 1]):
if [Maps_order[currentMapIndex - 1], lm, Maps_order[currentMapIndex]] in BANNED_TRANSITIONS:
continue
q.put([currentMapIndex - 1, currentCoordinates, isVoidMapCrossed, route + ['l(' + Maps_order[currentMapIndex] + ',' + lm +')']])
break # any save warp
# bsw
if currentMapIndex < dstMapIndex:
for lm in GetStrongPairConnections(Maps_order[currentMapIndex], Maps_order[currentMapIndex + 1]):
if [Maps_order[currentMapIndex + 1], lm, Maps_order[currentMapIndex]] in BANNED_TRANSITIONS:
continue
q.put([currentMapIndex + 1, currentCoordinates, isVoidMapCrossed, route + ['k(' + Maps_order[currentMapIndex] + ',' + lm +')']])
break # any save warp
if Maps_order[currentMapIndex] in WarpDict:
for x in WarpDict[Maps_order[currentMapIndex]]:
if x[3] == +1: # ww
q.put([Maps_order.index(x[1]), vec_sub(currentCoordinates, x[0]), isVoidMapCrossed, route + ['x(' + Maps_order[currentMapIndex] + ',' + x[2]+')']])
if x[3] == -1: # bww
q.put([Maps_order.index(x[1]), vec_sub(currentCoordinates, x[0]), isVoidMapCrossed, route + ['v(' + Maps_order[currentMapIndex] + ',' + x[2]+')']])
q.task_done()
def SearchPathTo(startMapIndex, dstMapIndex, coordinates):
q = queue.Queue()
q.put([dstMapIndex, coordinates, False, []])
for i in range(THREAD_COUNT):
t = threading.Thread(target=PathFinder, args=(q, startMapIndex, dstMapIndex))
t.daemon = True
t.start()
while q.unfinished_tasks:
time.sleep(1)
print('DIE', q.qsize())
def signal_handler(signal, frame):
print('Ctrl+C triggered!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
InitDestination(DESTINATION)
ReadGraph(GRAPH_FILE)
BuildWays()
BuildMapOrder(WARP_START_MAP, DST_MAP)
ReadWaypoints(WAYPOINTS_FILE, MAIN_COORD_MAP)
GetWarpVectors()
print('')
print('Warps:')
for x in WarpDict:
print ('+ ', x)
for y in WarpDict[x]:
print (' ', y)
print('')
print('Config:')
print(' swarp_ban_clear')
for x in BANNED_TRANSITIONS:
print(' swarp_ban ' + ' '.join(x))
print(' swarp_rebuild_graph')
print('')
print('Searching for path to ' + DESTINATION)
print('Waypoints loaded: ' + str(len(Waypoints)))
# print(*Waypoints)
print('Warps loaded: ' + str(len(WarpVectors)))
print('Thread count: ' + str(THREAD_COUNT))
SearchPathTo(Maps_order.index(WARP_START_MAP), Maps_order.index(DST_MAP), FixOrigin(DST_MAP, MAIN_COORD_MAP, DST_ORIGIN))
time.sleep(2)
print('Complete! Terminating...')