-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsheep.py
366 lines (281 loc) · 9.44 KB
/
sheep.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
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
from collections import defaultdict, namedtuple
from color import RGB, clamp
import math
import types
import controls_model as controls
from eyes import Eye, MutableEye
from shows.geom import *
def load_geometry(mapfile):
"""
Load sheep neighbor geometry
Returns a map { panel: [(edge-neighbors), (vertex-neighbors)], ... }
"""
with open(mapfile, 'r') as f:
def blank_or_comment(l):
return l.startswith('#') or len(l) == 0
lines = [l.strip() for l in f.readlines()]
lines = [l for l in lines if not blank_or_comment(l)]
def to_ints(seq):
return [int(x) for x in seq]
def p(raw):
"returns a tuple containing ([a,a,a], [b,b,b]) given a raw string"
raw = raw.strip()
if ' ' not in raw:
return (to_ints(raw.split(',')), None)
else:
# print ">>%s<<" % raw
a,b = raw.split()
return (to_ints(a.split(',')), to_ints(b.split(',')))
dat = {} # defaultdict(list)
for line in lines:
# print line
(num, rest) = line.split(' ', 1)
dat[int(num)] = p(rest.strip())
return dat
_neighbor_map = load_geometry('data/geom.txt')
def edge_neighbors(panel):
"Return the list of panel ids that share an edge with a given panel"
try:
panel = int(panel)
out = _neighbor_map[panel][0]
if out is None:
return []
return out
except Exception as e:
return []
def vertex_neighbors(panel):
"Return the list of panel ids that share a vertex (but not an edge) with a given panel"
try:
panel = int(panel)
out = _neighbor_map[panel][1]
if out is None:
return []
return out
except Exception as e:
return []
##
## Convenience wrapper to pass around three separate sheep objects
##
SheepSides = namedtuple('SheepSides', ['both', 'party', 'business', 'party_eye', 'business_eye'])
def make_sheep(model):
return SheepSides(both=Sheep(model, 'a'),
party=Sheep(model, 'p'),
business=Sheep(model, 'b'),
party_eye=Eye(model, 'p'),
business_eye=Eye(model, 'b'))
def make_eyes_only_sheep(sides):
null = NullSheep()
return SheepSides(both=null, party=null, business=null, party_eye = sides.party_eye, business_eye = sides.business_eye)
def make_mutable_sheep(sides):
return SheepSides(
both=MutableSheep(sides.both),
party=MutableSheep(sides.party),
business=MutableSheep(sides.business),
party_eye=MutableEye(sides.party_eye),
business_eye=MutableEye(sides.business_eye)
)
##
## Sheep class to represent one or both sides of the sheep
##
VALID_SIDES=set(['a', 'b', 'p'])
TEST_COLORS = [
RGB(141,211,199),RGB(255,255,179),RGB(190,186,218),RGB(251,128,114),RGB(128,177,211),RGB(253,180,98),RGB(179,222,105),RGB(252,205,229),RGB(217,217,217),RGB(188,128,189),RGB(204,235,197),RGB(255,237,111)
]
class Sheep(object):
def __init__(self, model, side):
self.model = model
if side not in VALID_SIDES:
raise Exception("%s is not a valid side. use one of a,b,p")
self.side = side
# Figure out the list of valid side ids for us based on what our model
# will actually allow
model_ids = self.model.cell_ids()
print(self)
#print "model_ids = {}".format(model_ids)
if self.side == 'a':
valid_suffixes = ['b', 'p']
else:
valid_suffixes = [side]
self.cells = []
for mid in model_ids:
suffix = mid[-1]
val = mid[:-1]
if suffix in valid_suffixes:
try:
v = int(val)
except Exception as e:
pass
else:
if not v in self.cells:
self.cells.append(v)
print("cells = {}".format(sorted(self.cells)))
self.cm = None
self.handle_colorized = False
self._brightness = 0.5
def __repr__(self):
return "Sheep(%s, side='%s')" % (self.model, self.side)
def set_brightness(self, val):
self._brightness = val
def all_cells(self):
"Return the list of valid cell numbers (without the suffix)"
return self.cells
# handle setting both sides here to keep the commands sent
# to the simulator as close as possible to the actual hardware
def _resolve(self, cell):
"""
Translate an integer cell id into a model cell identifier
'a' will be translated into two cells
"""
if isinstance(cell, bytes):
return [cell]
if cell in self.cells:
if self.side == 'a':
return [str(cell)+'b', str(cell)+'p']
else:
return [str(cell) + self.side]
else:
#print "Did not find cell {} in {}. My cells are {}".format(cell, self, sorted(self.cells))
#if int(cell) in self.cells:
# print "Cast to int worked"
#exit()
return []
def _adapt_color(self, color):
if self.handle_colorized and self.cm:
color = color.colorize(self.cm.colorized)
if self._brightness < 1.0:
color = color.copy()
color.v = color.v * self._brightness
return color
def set_cell(self, cell, color):
if isinstance(cell, list):
return self.set_cells(cell, color)
# a single set_cell call may result in two panels being set
c = self._resolve(cell)
if not c:
return
color = self._adapt_color(color)
# print "setting", c
self.model.set_cells(c, color)
def set_cells(self, cells, color):
if cells is None:
return
resolved = []
for c in cells:
if isinstance(c, list):
for cb in c:
resolved.extend(self._resolve(cb))
else:
resolved.extend(self._resolve(c))
color = self._adapt_color(color)
# print "setting", resolved
self.model.set_cells(resolved, color)
def set_all_cells(self, color):
color = self._adapt_color(color)
self.model.set_all_cells(color)
def clear(self):
""
self.set_all_cells(RGB(0,0,0))
# AAck! Never call go like this. Let the main loop
# handle the timing!!! :(
# self.go()
def go(self):
self.model.go()
# convenience methods in case you only have a sheep object
def edge_neighbors(self, cell):
return edge_neighbors(cell)
def vertex_neighbors(self, cell):
return vertex_neighbors(cell)
def set_test_colors(self):
ix = 0
for p in self.model.cell_ids():
self.set_cell(p, TEST_COLORS[ix])
ix += 1
if ix == len(TEST_COLORS):
ix = 0
class NullSheep(object):
"""
An implementation of the Sheep side interface that does nothing. This
can be handed to a show which might try to modify it, and thus can run
without crashing, while only the eye modifications are used.
"""
# Just return an empty list because from our perspective there are no panels
def all_cells(self):
return []
def set_cell(self, cell, color):
pass
def set_cells(self, cells, color):
pass
def set_all_cells(self, color):
pass
def clear(self):
pass
def go(self):
pass
def edge_neighbors(self, cell):
return edge_neighbors(cell)
def vertex_neighbors(self, cell):
return vertex_neighbors(cell)
def set_test_colors(self):
pass
class MutableSheep(object):
"""
An implementation of the Sheep side interface which can be muted -
that is, when muted, this sheep will act like the NullSheep, but when
unmuted it will pass things to it's parent
"""
def __init__(self, parent):
self.parent = parent
self.muted = False
def set_cell(self, cell, color):
if self.muted:
return
self.parent.set_cell(cell, color)
def set_cells(self, cells, color):
if self.muted:
return
self.parent.set_cells(cells, color)
def set_all_cells(self, color):
if self.muted:
return
self.parent.set_all_cells(color)
def clear(self):
if self.muted:
return
self.parent.clear()
def go(self):
if self.muted:
return
self.parent.go()
def set_test_colors(self):
self.parent.set_test_colors()
def all_cells(self):
return self.parent.all_cells()
def edge_neighbors(self, cell):
return self.parent.edge_neighbors(cell)
def vertex_neighbors(self, cell):
return self.parent.vertex_neighbors(cell)
###########
if __name__=='__main__':
PANEL_MAP = {
'1p': 1,
'2p': 2,
'3p': 3,
'1b': 11,
'2b': 12,
'EYEp': 200,
}
class TestModel(object):
def __init__(self):
pass
# Model basics
def cell_ids(self):
# return PANEL_IDS
return list(PANEL_MAP.keys())
test_model = TestModel()
print(test_model.cell_ids())
sheep_p = Sheep(test_model, 'p')
print(sheep_p.cells)
sheep_b = Sheep(test_model, 'b')
print(sheep_b.cells)
sheep_a = Sheep(test_model, 'a')
print(sheep_a.cells)