-
Notifications
You must be signed in to change notification settings - Fork 1
/
newlife.py
executable file
·162 lines (137 loc) · 4.99 KB
/
newlife.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
# Remove some mon-data headers, test spawner, AXED_MON
# cat mon-data.h | python2 newlife.py > newlife.dot
# neato -Tpng newlife.dot > newlife.png (or any other dot algorithm)
import sys
import re
data = sys.stdin.read()
# Settings
indent = 4
# Strip out axed monster
data = re.sub("(?ms)#define AXED.*?Real mon.*?$", "", data)
# Strip out block comments
data = re.sub("(?ms)\/\*.*?\*\/", "", data)
# Strip other comments
data = re.sub('//.*', '', data)
data = re.sub('#.*', '', data)
# Replace energy:
def energy(x):
return "(%s, %s)" % (x.group(1), x.group(2))
data = re.sub('([A-Z0-9_-]*_ENERGY)\((.*?)\)', energy, data)
# Replace mrd entries (with placeholder for parens)
def mrd(x):
return "<<{{%s}},%s>>" % (x.group(1), x.group(2))
data = re.sub('(?ms)mrd\((.*?)(\d*)\s*\)', mrd, data)
# Handle lone mrds that must be wrapped as tuples
def tuplify(x):
return "%s(%s)," % x.groups()
data = re.sub('(\s*)(<<.*?>>,)', tuplify, data)
# Replace pipes by commas
def pipes(x):
return "(%s)" % re.sub('(?ms)\s*\|\s*', ',', x.group(0))
data = re.sub('(?m)(\(.*\)|<<.*>>|[A-Z0-9_-]*)(\s*\|\s*(\(.*\)|<<.*>>|[A-Z0-9_-]*))+', pipes, data)
# Stringify bare enums:
def stringify(x):
return "'%s'" % x.group(1)
data = re.sub("(?<!['\"])([A-Z0-9_-]{2,})", stringify, data)
# Replace placeholders
data = re.sub('(<<|{{)', '(', data)
data = re.sub('(>>|}})', ')', data)
# Replace }s by )s unless they're a monster glyph.
data = re.sub("}(?!['\"]);*", ')', data)
data = re.sub("{", '(', data)
# Last change needed to make it valid Python:
data = re.sub("static monsterentry mondata\[\] = ", "", data)
# Eval it, storing it as a tuple
mondata = eval(data)
# Titles for the fields, in order, indexed from 0
fields = (
'id',
'glyph',
'color',
'name',
'flags',
'resistances',
'mass',
'experience modifier',
'genus',
'species',
'holiness',
'magic resistance',
'attacks',
'hit dice',
'ac',
'ev',
'spellbook',
'corpse type',
'zombie type',
'shout type',
'intelligence',
'habitat',
'flight class',
'speed',
'energy',
'item use',
'eats',
'size',
)
# Formatting functions
def resist_dots(resist, level=1):
level = min(level, 3)
if re.search('VUL', resist) is not None:
return "%s%-20s (%s%s)" % (' ' * indent, resist, "x" * level, "."*(3-level))
else:
return "%s%-20s (%s%s)" % (' ' * indent, resist, "*" * level, "."*(3-level))
i = {}
for x in range(len(fields)):
i[fields[x]] = x
data = {'id' : {}, 'name' : {}, 'genus' : {}, 'species' : {}, 'holiness' : {}}
viz = {
'id': ("lightblue", "white", "12"),
'genus' : ("dodgerblue4", "white", "14"),
'species' : ("steelblue3", "white", "14"),
'holiness' : ("navy", "white", "18"),
'unique' : ("seagreen", "white", "18")
}
for m in mondata:
attr = {
'id' : m[i['id']],
'name' : m[i['name']],
'genus' : m[i['genus']],
'species' : m[i['species']],
#'holiness' : m[i['holiness']],
}
flags = m[i['flags']]
if not isinstance(flags, tuple):
flags = (flags,)
attr['unique'] = ('M_UNIQUE' in flags)
for k, v in list(attr.items()):
if k == 'name' or k == 'unique':
continue
if data[k].get(v) is None:
data[k][v] = [attr]
else:
data[k][v].append(attr)
nodes = ""
connections = ""
print("digraph Crawl {")
for type, dict in list(data.items()):
if type != 'id':
a = 1
else:
for key, monsters in list(dict.items()):
for monster in monsters:
if monster['genus'] == monster['species'] and monster['species'] == monster['id']:
nodes += "\n" + 'monster%s [label="%s", shape="box", style="filled", fillcolor="%s", fontcolor="%s", fontsize="%s"];' % (monster['id'], monster['name'], viz['genus'][0], viz['genus'][1], viz['genus'][2])
elif monster['species'] == monster['id']:
nodes += "\n" + 'monster%s [label="%s", shape="box", style="filled", fillcolor="%s", fontcolor="%s", fontsize="%s"];' % (monster['id'], monster['name'], viz['species'][0], viz['species'][1], viz['species'][2])
connections += "\n" + 'monster%s -> monster%s [arrowhead="none"];' % (monster['genus'], monster['species'])
else:
if monster.get('unique') is True:
nodes += "\n" + 'monster%s [label="%s", shape="box", style="filled", fillcolor="%s", fontcolor="%s", fontsize="%s"];' % (monster['id'], monster['name'], viz['unique'][0], viz['unique'][1], viz['unique'][2])
else:
nodes += "\n" + 'monster%s [label="%s", shape="box", style="filled", fillcolor="%s", fontcolor="%s", fontsize="%s"];' % (monster['id'], monster['name'], viz['id'][0], viz['id'][1], viz['id'][2])
connections += "\n" + 'monster%s -> monster%s [arrowhead="none"];' % (monster['species'], monster['id'])
print(nodes)
print(connections)
print("overlap=prism;")
print("}")