-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes_short_path.py
204 lines (133 loc) · 5.22 KB
/
routes_short_path.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
def create_route_xml(seed_number,db_name):
# UNDER CONSTRUCTION
# Create a trader and xboat route table and extract for traveller map
import sqlite3
import pandas as pd
import numpy as np
import networkx as nx
def offset_to_cube(location):
x = int(location[0:2])
y = int(location[2:4])
q = x
r = y - (q + (q&1)) / 2
s = -q - r
return(q,r,s)
def cube_to_offset(location):
x = location[0]
y = int(location[1] + (x + (x&1)) / 2)
if x > 9:
x_string = str(x)
else:
x_string = '0' + str(x)
if y > 9:
y_string = str(y)
else:
y_string = '0' + str(y)
return (x_string+y_string)
def cube_direction(direction):
return cube_direction_vectors[direction]
def cube_add(cube, vec):
return (cube[0] + vec[0], cube[1] + vec[1], cube[2] + vec[2])
def cube_neighbor(cube, direction):
return cube_add(cube, cube_direction(direction))
def cube_subtract(a, b):
return (a[0] - b[0], a[1] - b[1], a[2] - b[2])
def cube_distance(a, b):
vec = cube_subtract(a, b)
return (abs(vec[0]) + abs(vec[1]) + abs(vec[2])) / 2
def jump_range(center,j_range):
results = []
for q in range(-j_range,j_range+1):
for r in range(-j_range,j_range+1):
for s in range(-j_range,j_range+1):
if q + r + s == 0:
results.append(cube_add(center,[q, r, s]))
return results
def off_distance(o_start,o_end):
try:
c_start = offset_to_cube(o_start)
except:
print('C_start failed to convert to cube',o_start)
try:
c_end = offset_to_cube(o_end)
except:
print('C_end failed to convert to cube',o_end)
return int(cube_distance(c_start,c_end))
######################################################################
cube_direction_vectors = [
(+1, 0, -1),
(+1, -1, 0),
(0, -1, +1),
(-1, 0, +1),
(-1, +1, 0),
(0, +1, -1)
]
conn = sqlite3.connect(db_name)
c = conn.cursor()
sql3_select = """ SELECT s.location, s.ix
FROM system_stats s
LEFT JOIN traveller_stats t
ON t.location = s.location
WHERE t.main_world = 1"""
try:
df = pd.read_sql_query(sql3_select,conn,index_col='location')
except:
print('Problem - df failed')
df['ix'] = df['ix'].str.replace('{','')
df['ix'] = df['ix'].str.replace('}','')
df['ix'] = df['ix'].astype(int)
df['ix_flag'] = np.where(df['ix'] >= 4,1,0)
#df = df.loc[df['ix_flag'] == 1]
l_location = list(df.index)
for l in l_location:
df[l] = df.index
df[l] = df[l].apply(off_distance,args=(l,))
df_imp = df.loc[df['ix_flag'] == 1]
l_important = list(df_imp.index)
d_distance = df.to_dict('index')
conn.commit()
conn.close()
########################################################################
G = nx.Graph()
elist = []
for loc in l_location:
for loc2 in l_location:
if loc < loc2:
if d_distance[loc][loc2] <= 4:
elist.append([loc,loc2])
G.add_edges_from(elist)
path_list = []
used_list = []
#max_list = 8
for max_list in range(4,20):
for loc in l_important:
for loc2 in l_important:
one_chain = []
if loc < loc2 and d_distance[loc][loc2] <= max_list:
if max_list <= 6 or (loc not in used_list):
used_list.append(loc)
one_chain = nx.shortest_path(G,source=loc,target=loc2)
for x,dest in enumerate(one_chain):
if x < (len(one_chain)-1):
path_list.append([dest,one_chain[x+1]])
route_text = ''
for each in path_list:
route_text += """ <Route Start='""" + each[0] + \
"""' End='""" + each[1] + """' />""" + '\n'
important_text = ''
for each in l_important:
important_text += """ <Label Hex='""" + each + \
"""' Color="red">Ix+</Label>""" + '\n'
file_name = db_name + '-routes.txt'
with open(file_name, 'w') as f:
f.write('<?xml version="1.0"?>' + '\n' \
+ '<Sector>' + '\n' \
+ '<Name>' + db_name + '</Name>' + '\n' \
+ '<Routes>...' + '\n' \
+ route_text
+ '</Routes>' + '\n' \
+ '<Labels>' + '\n' \
+ important_text
+ '</Labels>' + '\n' \
+ '</Sector>')
f.close()