-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.py
More file actions
376 lines (329 loc) · 12 KB
/
structs.py
File metadata and controls
376 lines (329 loc) · 12 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
import copy
DOUBLE_PRIMARIES = True
SPLIT_PRIMARIES = True
def coalesce(l):
l = list(l)
idx = 0
while idx < len(l) - 1:
comb = l[idx].combine_with(l[idx + 1])
if comb is not None:
del l[idx:idx + 2]
l.insert(idx, comb)
else:
idx += 1
return l
class Thingy(object):
name = None
tincture = None
between = None
# Return None or a combined thing.
def combine_with(self, other):
return None
class Parent(Thingy):
kid = None
category = None
class Field(Parent):
BOXES_FOR_COUNT = {
1: [(20,20,515,450)],
2: [(20,20,255,450),(290,20,255,450)],
3: [(20,20,255,255),(290,20,255,255),(155,295,255,255)]
}
def tree(self):
yield "Field: %s" % self.tincture
for l in self.kid.tree():
yield " " + l
def describe(self):
assert self.tincture is not None
for l in self.tincture.fielddescription():
yield l
if self.kid is not None:
for l in self.kid.describe():
yield l
def render(self):
assert self.between is None
# Draw a colored shield, then put our kids on it
ret = """<svg height="100" width="100" viewBox="0 0 561 640"
preserveAspectRatio="xMidYMid meet">
<path stroke="black" stroke-width="1" fill="%s"
d="M 0.00,319.00
C 7.00,482.00 140.00,580.00 279.00,639.00
418.00,580.00 553.00,482.00 560.00,320.00
560.00,320.00 560.00,0.00 560.00,0.00
560.00,0.00 0.00,0.00 0.00,0.00
0.00,0.00 0.00,319.00 0.00,319.00 Z" />""" % (self.tincture)
if isinstance(self.kid,Group):
bxs = self.BOXES_FOR_COUNT[len(self.kid)]
for lcv in xrange(len(self.kid)):
ret += """<svg x="%d" y="%d" width="%d" height="%d"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">""" % bxs[lcv]
ret += self.kid[lcv].render()
ret += """</svg>"""
else:
ret += """<svg x="0" y="0" width="561" height="640"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMin">"""
ret += self.kid.render()
ret += """</svg>"""
ret += """</svg>"""
return ret
def numtag_for(num):
if num == 'seme':
return 'seme'
elif num in (None, 'the'):
return None
elif num < 10:
return '%d' % num
else:
return '10 or more'
class Charge(Thingy):
name = None
number = None
multiplier = None
blazon = None
adj = None
def __init__(self, name, desc, category=False):
self.name = name
self.desc = desc
self.category = category
self.mods = []
self.tags = []
self.maintags = []
self.seealso = []
def combine_with(self, other):
if (other.name == self.name
and self.mods == other.mods
and self.tags == other.tags
and self.maintags == other.maintags
and self.between == other.between):
ret = copy.deepcopy(self)
if other.number is not None:
ret.number += other.number
if self.tincture != other.tincture:
ret.tincture = MultiTincture([self.tincture, other.tincture])
return ret
else:
return None
def render(self):
return """<circle cx="50" cy="50" r="50" fill="%s" /><text x="5" y="40" fill="grey">%s</text>""" % (self.tincture, self.name)
def tree(self):
yield "Charge: %s %s" % (self.tincture, self.name)
def describe(self, as_mod=False):
from words import PRIMTAGS_WHITELIST
assert self.between is None, self.between
tags = list(self.tags)
primtags = [t for t in tags if t in PRIMTAGS_WHITELIST]
if self.tincture and self.tincture.tincture != 'proper':
tags = [self.tincture.tincture] + tags
numtag = numtag_for(self.number)
if self.number and self.number != 'the':
if self.number < 4:
primtags = ['%d' % self.number] + primtags
else:
primtags = ['4 or more'] + primtags
tagbit = ''
if tags:
tagbit = ':' + ':'.join(tags)
numtagbit = ''
if numtag:
numtagbit = ':' + numtag
maintagbit = ''
if self.maintags:
maintagbit = ':' + ':'.join(self.maintags)
should_double = DOUBLE_PRIMARIES
was_split_primary = False
if SPLIT_PRIMARIES and 'primary' in tags and not as_mod:
if numtag:
tags = [numtag] + tags
tags.remove('primary')
was_split_primary = True
for t in tags + self.maintags:
yield "%s:primary:%s" % (self.desc, t)
if len(tags + self.maintags) > 1:
should_double = False
else:
yield "%s%s%s%s" % (self.desc, numtagbit, tagbit, maintagbit)
if not as_mod:
for c in self.seealso:
if was_split_primary and c.desc == self.desc:
continue
if c.number:
yield "?%s:%s%s" % (c.desc, numtag_for(c.number), tagbit)
elif c.multiplier and numtagbit:
yield "?%s:%s%s" % (c.desc,
numtag_for(self.number * c.multiplier),
tagbit)
else:
yield "?%s%s%s" % (c.desc, numtagbit, tagbit)
if self.tincture:
if 'primary' in self.tags and not as_mod and should_double:
# Add an additonal instance, without the tincture, for
# sig diffness
yield "?%s:%s" % (self.desc, ':'.join(primtags))
for c in self.tincture.chargeextras:
for d in c.describe():
yield d
for c in self.mods:
for d in c.describe(as_mod=True):
assert isinstance(d, basestring), d
yield d
def add_posture(self, posture):
self.tags.append(posture)
for c in self.mods:
c.tags.append(posture)
def __repr__(self):
return 'Charge:'+repr((self.name, self.desc, self.category, self.tags,
self.maintags))
class Ordinary(Parent):
arounds = []
class Bend(Ordinary):
BOXES_FOR_BETWEEN = {
2: [(55,5,40,40),(10,55,40,40)],
3: [(40,3,30,30),(65,27,30,30),(10,55,30,30)]
}
def render(self):
ret = """<line x1="0" y1="0" x2="100" y2="100" stroke-width="20" stroke="%s"/>""" % self.tincture
if self.between:
bxs = self.BOXES_FOR_BETWEEN[len(self.between)]
for lcv in xrange(len(self.between)):
ret += """<svg x="%d" y="%d" width="%d" height="%d"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">""" % bxs[lcv]
ret += self.between[lcv].render()
ret += """</svg>"""
return ret
class Peripheral(Parent):
outside = None
class Chief(Peripheral):
BOXES_FOR_COUNT = {
1: [(5,35,70,70)],
2: [(5,35,40,40),(55,35,40,40)],
3: [(5,32,30,30),(55,32,30,30),(30,67,30,30)]
}
def render(self):
ret = """<line x1="0" y1="15" x2="100" y2="15" stroke-width="30" stroke="%s"/>""" % self.tincture
if self.outside:
if isinstance(self.outside,Group):
bxs = self.BOXES_FOR_COUNT[len(self.outside)]
for lcv in xrange(len(self.outside)):
ret += """<svg x="%d" y="%d" width="%d" height="%d"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">""" % bxs[lcv]
ret += self.outside[lcv].render()
ret += """</svg>"""
else:
ret += """<svg x="0" y="30" width="100" height="100"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMin">"""
ret += self.outside.render()
ret += """</svg>"""
return ret
class Group(list, Thingy):
def tree(self):
for i in self:
for l in i.tree():
yield l
def describe(self):
l = coalesce(self)
for i in l:
for l in i.describe():
yield l
if self.between:
for i in coalesce(self.between):
for l in i.describe():
yield l
class Tincture(object):
def __init__(self, tincture, fielddesc=None):
self.tincture = tincture
self.fielddesc = fielddesc
self.fieldextras = []
self.chargeextras = []
self.codivisions = []
self.on_field = False
self.fieldcharge = None
self.fdtincts = None
def __str__(self):
return self.tincture
def add_treatment(self, treatment):
from words import CHARGES, FURS
if self.is_complex():
self.fdtincts[-1].add_treatment(treatment)
return
if treatment in FURS:
self.tincture = 'fur'
else:
self.tincture = 'multicolor'
if 'field treatment, %s' % treatment in CHARGES:
a = copy.deepcopy(CHARGES['field treatment, %s' % treatment])
elif 'field treatment, seme, %s' % treatment in CHARGES:
a = copy.deepcopy(CHARGES['field treatment, seme, %s' % treatment])
else:
a = copy.deepcopy(CHARGES[treatment])
self.fieldextras.append(a)
if 'charge treatment, %s' % treatment in CHARGES:
b = copy.deepcopy(CHARGES['charge treatment, %s' % treatment])
elif 'charge treatment, seme, %s' % treatment in CHARGES:
b = copy.deepcopy(CHARGES['charge treatment, seme, %s' % treatment])
else:
b = None
if b is not None:
self.chargeextras.append(b)
l = [a, b]
else:
l = [a]
return l
def add_extra(self, extra):
self.fieldextras.append(extra)
self.chargeextras.append(extra)
def fielddescription(self):
fd = self.fielddesc
if self.fdtincts and len(self.fdtincts) < 3:
fd += ':' + self.fdtincts[0].tincture
if len(self.fdtincts) > 1:
fd += ':~and ' + self.fdtincts[1].tincture
yield fd
for t in self.codivisions:
for d in t.fielddescription():
yield d
for e in self.fieldextras:
for d in e.describe():
yield d
if self.fdtincts:
fdextras = []
for fdt in self.fdtincts:
fdextras += fdt.fieldextras
for e in coalesce(fdextras):
for d in e.describe():
yield d
def complicate(self, fieldcharge):
self.fielddesc = fieldcharge.desc
self.fieldcharge = fieldcharge
self.fdtincts = []
if self.tincture != 'multicolor':
from words import TINCTURES
self.add_tincture(copy.deepcopy(TINCTURES[self.tincture]))
self.tincture = 'multicolor'
def is_complex(self):
return self.fieldcharge is not None
def add_tincture(self, tincture):
#print 'a_t', tincture
assert self.is_complex()
assert self.fdtincts is not None
assert isinstance(tincture, Tincture), tincture
self.fdtincts.append(tincture)
for t in self.codivisions:
t.add_tincture(tincture)
class Fieldless(Tincture):
def __init__(self):
Tincture.__init__(self, None)
def fielddescription(self):
return ()
def ComplexTincture(fieldcharge):
ret = Tincture('multicolor')
ret.complicate(fieldcharge)
return ret
class MultiTincture(Tincture):
def __init__(self, tincts):
Tincture.__init__(self, 'multicolor')
self.tincts = tincts
def fielddescription(self):
for t in self.tincts:
for d in t.fielddescription():
yield d
def add_tincture(self, tincture):
self.tincts.append(tincture)