-
Notifications
You must be signed in to change notification settings - Fork 31
/
rules2doc.py
232 lines (190 loc) · 6.39 KB
/
rules2doc.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
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
#! .venv\Scripts\python.exe
from typing import Set
from soundrts.lib import log
from soundrts.lib.package import Package
from soundrts.paths import BASE_PACKAGE_PATH
log.add_console_handler()
from soundrts.definitions import Rules
class RulesForDoc(Rules):
precision_properties: Set[str] = set()
precision_list_properties: Set[str] = set()
string_properties = Rules.string_properties | Rules.precision_properties
_s = ""
"""
stats
=====
.. contents::
"""
def pr(s=""):
global _s
_s += s + "\n\n"
def name(c, link=True):
if rules.get(c, "name"):
r = " ".join(rules.get(c, "name"))
else:
r = c
if link:
return "`" + r + "`_"
return r
def desc(c):
if rules.get(c, "desc"):
return (" ".join(rules.get(c, "desc"))).replace(r"\n", "\n\n")
else:
return ""
def comma_join(lst, p):
return p + " " + ", ".join(lst)
def _list(p, n, lst=None):
if lst is None:
lst = rules.get(c, n)
if lst:
return comma_join([name(_) for _ in lst], p)
else:
return ""
def cost(p, n):
v = rules.get(c, n)
if v:
s = p
lst = []
if v[0]:
lst += ["%s gold" % v[0]]
if v[1]:
lst += ["%s wood" % v[1]]
return s + " " + ", ".join(lst)
else:
return ""
def nb(u, n):
n = float(n)
if isinstance(u, tuple):
if n <= 1:
return u[0]
else:
return u[1]
if n <= 1:
if u.endswith("s"):
return u[:-1]
return u
def _int(p, n, u):
v = rules.get(c, n)
if v:
s = p
s += " {} {}".format(v, nb(u, v))
return s
else:
return ""
def _sint(p, n, u):
v = rules.get(c, n)
if v:
s = p
s += " %i %s" % (v, nb(u, v))
return s
else:
return ""
def _res(p, n):
v = rules.get(c, n)
if v:
return p + " " + ", ".join([("gold", "wood")[res] for res in rules.get(c, n)])
else:
return ""
def underline(s, u=","):
return s + "\n" + u * len(s)
def kcost(c):
r = 0
if rules.get(c, "cost"):
r += float(rules.get(c, "cost")[0])
r += float(rules.get(c, "cost")[1]) * 1.01
if not r: # special units
r += 1000 # end of list
return (r, name(c)) # name as a secondary key
def trained_by(c):
r = [
k
for k in rules.classnames()
if rules.get(k, "can_train") and c in rules.get(k, "can_train")
]
return sorted(r, key=kcost)
def can_use(c, t):
if not rules.get(c, "can_use"):
return []
r = [k for k in rules.get(c, "can_use") if rules.get(k, "class") == [t]]
return sorted(r, key=kcost)
rules = RulesForDoc()
base = Package.from_path(BASE_PACKAGE_PATH)
rules.load(base.open_text("rules.txt").read(), base.open_text("ui/rules_doc.txt").read())
for cat in (
("1. Units", ("worker", "soldier")),
("2. Buildings", ("building",)),
("3. Abilities", ("ability",)),
("4. Upgrades and research", ("upgrade",)),
):
pr(underline(cat[0], "^"))
for c in sorted(rules.classnames(), key=kcost):
if rules.get(c, "class")[0] not in cat[1]:
continue
pr(underline(name(c, link=False)))
pr(desc(c))
pr()
pr(_list("- trained by: ", None, trained_by(c)))
pr(_list("- requires:", "requirements"))
pr(_int("- mana cost:", "mana_cost", "mana points"))
pr(cost("- total cost:", "cost"))
pr(_sint("- total food cost:", "food_cost", "rations"))
pr(_int("- total time cost:", "time_cost", "seconds"))
if rules.get(c, "effect"):
if rules.get(c, "effect")[0] == "bonus":
pr(
"- effect: {} + {}".format(
rules.get(c, "effect")[1], rules.get(c, "effect")[2]
)
)
elif rules.get(c, "effect")[0] == "apply_bonus":
pr(
"- effect: applies the %s upgrade bonus of the unit"
% rules.get(c, "effect")[1]
)
pr(_int("- health: ", "hp_max", "hit points"))
pr(_int("- health regeneration: ", "hp_regen", "hit points per second"))
pr(_int("- armor: ", "armor", "hit points"))
pr(_int("- armor upgrade bonus: ", "armor_bonus", "hit points"))
if rules.get(c, "damage"):
pr(
"- attack: {} every {}".format(
_int("", "damage", "hit points"), _int("", "cooldown", "seconds")
)
)
pr(_int("- damage radius (area of effect): ", "damage_radius", "meters"))
pr(_int("- attack upgrade bonus: ", "damage_bonus", "hit points"))
pr(_int("- attack range: ", "range", "meters"))
if rules.get(c, "is_ballistic") == 1:
pr(
"- can attack units located in an adjacent square if their altitude is lower (new in SoundRTS 1.2 alpha 9)."
)
pr(_int("- speed: ", "speed", ("meter per second", "meters per second")))
pr(_sint("- food provided:", "food_provided", "rations"))
pr(_sint("- healing power:", "heal_level", ""))
pr(_res("- can store: ", "storable_resource_types"))
pr(_list("- can build:", "can_build"))
pr(_list("- can train:", "can_train"))
pr(_list("- can research:", "can_research"))
pr(_list("- can upgrade to:", "can_upgrade_to"))
pr(_list("- special abilities: ", None, can_use(c, "ability")))
if rules.get(c, "bonus_height") == 1:
pr("- have a height bonus (useful for sight and eventually attack range)")
if rules.get(c, "is_a_detector") == 1:
pr("- detects invisible units")
if rules.get(c, "is_invisible") == 1:
pr("- is invisible")
pr(_list("- potential improvements: ", None, can_use(c, "upgrade")))
if 0:
try:
pr(
"- combat efficiency ratio: %s (only takes into account: hit points, damage, cooldown, cost; formula: hit points * damage / cooldown / (gold cost + 3 * wood cost))"
% (
rules.get(c, "hp_max")
* rules.get(c, "damage")
/ float(rules.get(c, "cooldown"))
/ (rules.get(c, "cost")[0] + rules.get(c, "cost")[1] * 3)
)
)
except:
pass
stats = _s