-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_graph.py
executable file
·95 lines (72 loc) · 2.69 KB
/
generate_graph.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
#!/bin/python3
import glob
import re
import os
data_dir = os.path.abspath(os.environ["data_dir"])
graphviz_prefix_file = os.environ.get("graphviz_prefix", "prefix.dot")
print_dead = os.environ.get("print_dead", "false").lower() in ("yes", "true", "1")
print_npc = os.environ.get("npc", "true").lower() in ("yes", "true", "1")
with open(graphviz_prefix_file, 'rU') as opened_file:
graphviz = opened_file.read()
os.chdir(os.path.abspath(os.environ["data_dir"]))
def get_intermaps(filename):
result = []
with open(filename, 'rU') as opened_file:
for line in opened_file:
ref = re.search('intermap=(maps.*),.*,.*', line)
if ref:
ref = ref.group(1)
result.append(ref)
return result
def get_npc_files(filename):
result = []
with open(filename, 'rU') as opened_file:
for line in opened_file:
ref = re.search('filename=(npcs.*)', line)
if ref:
ref = ref.group(1)
result.append(ref)
return result
def get_map_name(filename):
with open(filename, 'rU') as opened_file:
for line in opened_file:
ref = re.search('title=(.*)', line)
if ref:
return ref.group(1)
map_to_map_direct = {}
map_to_map_npc = {}
all_maps = list(glob.iglob('maps/*.txt')) + \
list(glob.iglob('maps/*/*.txt'))
all_maps = list(map(lambda x: os.path.relpath(x, data_dir), all_maps))
for map_file in all_maps:
map_to_map_direct[map_file] = get_intermaps(map_file)
map_to_map_npc[map_file] = []
for npc_file in get_npc_files(map_file):
map_to_map_npc[map_file].extend(
get_intermaps(npc_file)
)
# print(map_to_map_direct)
# print(map_to_map_npc)
def clean(filename):
return os.path.splitext(os.path.basename(filename))[0]
traversed = {}
to_traverse = ['maps/spawn.txt']
for map_file in to_traverse:
traversed[map_file] = True
map_id = clean(map_file)
if print_npc:
for npc_child in map_to_map_npc[map_file]:
graphviz += '{} -> {} [label=npc style=dashed]\n'.format(map_id, clean(npc_child))
if npc_child not in traversed:
to_traverse.append(npc_child)
for direct_child in map_to_map_direct[map_file]:
graphviz += '{} -> {}\n'.format(map_id, clean(direct_child))
if direct_child not in traversed:
to_traverse.append(direct_child)
graphviz += '{} [label="{}"]\n'.format(map_id, get_map_name(map_file) or map_file)
if print_dead:
for map_file in all_maps:
map_name = get_map_name(map_file)
graphviz += '{} [label="{}"]\n'.format(clean(map_file), map_name or map_file)
graphviz += "}"
print(graphviz)